From 74aa5112a07eafe98bf76e1124b14468af61a34c Mon Sep 17 00:00:00 2001 From: lijianbin <513837235@.qq.com> Date: Thu, 24 Jul 2025 10:48:34 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84pgsql=E5=85=B3=E9=94=AE?= =?UTF-8?q?=E6=9B=BF=E6=8D=A2=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common.go | 4 ++++ db.go | 3 ++- db_test.go | 40 ++++++++++++++++++++++++++++++++++++++-- 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/common.go b/common.go index f31d763..2cfed53 100644 --- a/common.go +++ b/common.go @@ -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 } diff --git a/db.go b/db.go index b04035e..ef2c30a 100644 --- a/db.go +++ b/db.go @@ -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 { diff --git a/db_test.go b/db_test.go index 75b5677..876f0d2 100644 --- a/db_test.go +++ b/db_test.go @@ -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()) + } }