From 33a899d32e2fc224b630616d8231c7ac76a0d274 Mon Sep 17 00:00:00 2001 From: John Date: Wed, 8 Jan 2020 21:24:33 +0800 Subject: [PATCH] add As function for gdb.Model; improve string quote handling for gdb --- .example/database/gdb/mysql/gdb_value.go | 13 +++---------- database/gdb/gdb_func.go | 10 ++++++---- database/gdb/gdb_model.go | 10 ++++++++++ database/gdb/gdb_unit_z_func_test.go | 13 +++++++------ database/gdb/gdb_unit_z_mysql_model_test.go | 7 +++++++ 5 files changed, 33 insertions(+), 20 deletions(-) diff --git a/.example/database/gdb/mysql/gdb_value.go b/.example/database/gdb/mysql/gdb_value.go index fb032f53b..a8adade8a 100644 --- a/.example/database/gdb/mysql/gdb_value.go +++ b/.example/database/gdb/mysql/gdb_value.go @@ -2,23 +2,16 @@ package main import ( "github.com/gogf/gf/frame/g" - "github.com/gogf/gf/os/gtime" ) func main() { db := g.DB() - //db.SetDebug(true) + db.SetDebug(true) - type User struct { - Id int - Name *gtime.Time - } - - user := new(User) - e := db.Table("test").Where("id", 10000).Struct(user) + one, e := db.Table("order.order o").LeftJoin("user.user u", "o.uid=u.id").Where("u.id", 1).One() if e != nil { panic(e) } - g.Dump(user) + g.Dump(one) } diff --git a/database/gdb/gdb_func.go b/database/gdb/gdb_func.go index 0aef26d34..1e495ce68 100644 --- a/database/gdb/gdb_func.go +++ b/database/gdb/gdb_func.go @@ -52,7 +52,7 @@ var ( ) // handleTableName adds prefix string and quote chars for the table. It handles table string like: -// "user", "user u", "user,user_detail", "user u, user_detail ut", "user as u, user_detail as ut". +// "user", "user u", "user,user_detail", "user u, user_detail ut", "user as u, user_detail as ut", "user.user u". // // Note that, this will automatically checks the table prefix whether already added, if true it does // nothing to the table name, or else adds the prefix to the table name. @@ -68,7 +68,7 @@ func doHandleTableName(table, prefix, charLeft, charRight string) string { array2[0] = prefix + array2[0] } // Add the security chars. - array2[0] = doQuoteWord(array2[0], charLeft, charRight) + array2[0] = doQuoteString(array2[0], charLeft, charRight) array1[k1] = gstr.Join(array2, " ") } return gstr.Join(array1, ",") @@ -84,7 +84,8 @@ func doQuoteWord(s, charLeft, charRight string) string { } // doQuoteString quotes string with quote chars. It handles strings like: -// "user", "user u", "user,user_detail", "user u, user_detail ut", "u.id asc". +// "user", "user u", "user,user_detail", "user u, user_detail ut", +// "user.user u, user.user_detail ut", "u.id asc". func doQuoteString(s, charLeft, charRight string) string { array1 := gstr.SplitAndTrim(s, ",") for k1, v1 := range array1 { @@ -93,6 +94,7 @@ func doQuoteString(s, charLeft, charRight string) string { if len(array3) == 1 { array3[0] = doQuoteWord(array3[0], charLeft, charRight) } else if len(array3) == 2 { + array3[0] = doQuoteWord(array3[0], charLeft, charRight) array3[1] = doQuoteWord(array3[1], charLeft, charRight) } array2[0] = gstr.Join(array3, ".") @@ -236,7 +238,7 @@ func formatWhere(db DB, where interface{}, args []interface{}, omitEmpty bool) ( if gstr.Pos(newWhere, "?") == -1 { if lastOperatorReg.MatchString(newWhere) { newWhere += "?" - } else if quoteWordReg.MatchString(newWhere) { + } else if gregex.IsMatchString(`^[\w\.\-]+$`, newWhere) { newWhere += "=?" } } diff --git a/database/gdb/gdb_model.go b/database/gdb/gdb_model.go index 9bec6fca9..18d731752 100644 --- a/database/gdb/gdb_model.go +++ b/database/gdb/gdb_model.go @@ -124,6 +124,16 @@ func (tx *TX) From(tables string) *Model { return tx.Table(tables) } +// As sets an alias name for current table. +func (m *Model) As(as string) *Model { + if m.tables != "" { + model := m.getModel() + model.tables = gstr.TrimRight(model.tables) + " AS " + as + return model + } + return m +} + // DB sets/changes the db object for current operation. func (m *Model) DB(db DB) *Model { model := m.getModel() diff --git a/database/gdb/gdb_unit_z_func_test.go b/database/gdb/gdb_unit_z_func_test.go index 645b5de85..c2ccde219 100644 --- a/database/gdb/gdb_unit_z_func_test.go +++ b/database/gdb/gdb_unit_z_func_test.go @@ -32,12 +32,13 @@ func Test_Func_doQuoteString(t *testing.T) { gtest.Case(t, func() { // "user", "user u", "user,user_detail", "user u, user_detail ut", "u.id asc". array := map[string]string{ - "user": "`user`", - "user u": "`user` u", - "user,user_detail": "`user`,`user_detail`", - "user u, user_detail ut": "`user` u,`user_detail` ut", - "u.id asc": "u.`id` asc", - "u.id asc, ut.uid desc": "u.`id` asc,ut.`uid` desc", + "user": "`user`", + "user u": "`user` u", + "user,user_detail": "`user`,`user_detail`", + "user u, user_detail ut": "`user` u,`user_detail` ut", + "u.id asc": "`u`.`id` asc", + "u.id asc, ut.uid desc": "`u`.`id` asc,`ut`.`uid` desc", + "user.user u, user.user_detail ut": "`user`.`user` u,`user`.`user_detail` ut", } for k, v := range array { gtest.Assert(doQuoteString(k, "`", "`"), v) diff --git a/database/gdb/gdb_unit_z_mysql_model_test.go b/database/gdb/gdb_unit_z_mysql_model_test.go index 66ec62aef..fdd4be7b2 100644 --- a/database/gdb/gdb_unit_z_mysql_model_test.go +++ b/database/gdb/gdb_unit_z_mysql_model_test.go @@ -1670,6 +1670,13 @@ func Test_Model_Prefix(t *testing.T) { gtest.Assert(r[0]["id"], "1") gtest.Assert(r[1]["id"], "2") }) + gtest.Case(t, func() { + r, err := db.Table(table).As("u1").LeftJoin(table+" as u2", "u2.id=u1.id").Where("u1.id in (?)", g.Slice{1, 2}).Order("u1.id asc").All() + gtest.Assert(err, nil) + gtest.Assert(len(r), 2) + gtest.Assert(r[0]["id"], "1") + gtest.Assert(r[1]["id"], "2") + }) } func Test_Model_Schema(t *testing.T) {