Browse Source

完善pgsql关键替换问题

master
lijianbin 1 day ago
parent
commit
74aa5112a0
3 changed files with 44 additions and 3 deletions
  1. +4
    -0
      common.go
  2. +2
    -1
      db.go
  3. +38
    -2
      db_test.go

+ 4
- 0
common.go View File

@ -2,6 +2,7 @@ package dbquery
import (
"git.tetele.net/tgo/helper"
"log"
"strings"
)
@ -15,6 +16,9 @@ func SqlReplace(sql_s, sql_type string) string {
sql = strings.Replace(sql, " user ", ` "user" `, -1)
sql = strings.Replace(sql, " user.", ` "user".`, -1)
sql = strings.Replace(sql, "=user.", `="user".`, -1)
log.Println("replace sql:", sql)
return sql
}

+ 2
- 1
db.go View File

@ -436,10 +436,10 @@ func GetRow(dbName, table_name, alias string, titles string, with, join [][]stri
if orderby != "" {
sql_str = helper.StringJoin(sql_str, " order by ", orderby)
}
if debug {
log.Println("query sql:", sql_str, valueList)
}
condition_len := 0 //所有条件数
for _, ch2 := range sql_str {
if string(ch2) == "?" {
@ -469,6 +469,7 @@ func GetRow(dbName, table_name, alias string, titles string, with, join [][]stri
sql_str = sqlx.Rebind(sqlx.DOLLAR, sql_str)
sql_str = SqlReplace(sql_str, "")
}
rows, err = db.Query(sql_str, valueList...)
if err == nil {


+ 38
- 2
db_test.go View File

@ -6,7 +6,6 @@ import (
)
func Test_Connet(t *testing.T) {
//go func() {
for i := 0; i < 1; i++ {
dbhost := "localhost"
dbname := "shop"
@ -46,6 +45,43 @@ func Test_Connet(t *testing.T) {
log.Println(err.Error())
}
}
//}()
}
func Test_Query(t *testing.T) {
token := "67c121aa-6e1c-011f-ebb6-976d855fd777"
dbhost := "192.168.233.134"
dbname := "canyin"
dbusername := "bin"
dbpassword := "Bin123456"
dbport := "5432"
table := "ttl_user_token"
err := PgConnect(dbhost, dbusername, dbpassword, dbname, dbport)
if err != nil {
log.Println(err.Error())
}
title := "user.*,ut.expiretime"
alias := "ut"
join := [][]string{}
join = append(join, []string{"ttl_user as user", "ut.user_id= user.id"})
where := []string{
"ut.token=?",
}
where_or := []string{}
valueList := []interface{}{
token,
}
orderby := ""
debug := true
count, row, err := GetRow(dbname, table, alias, title, [][]string{}, join, where, where_or, valueList, orderby, "", "", debug)
//count, row, err := FetchRows(dbname, table, alias, title, join, join, where, where_or, valueList, orderby, "", "", 1, 10, debug)
log.Println(count)
log.Println(row)
log.Println(err)
if err != nil {
log.Println(err.Error())
}
}

Loading…
Cancel
Save