From ac9be6134b2d581acb501724a0b02332a056f89d Mon Sep 17 00:00:00 2001 From: John Date: Fri, 7 Feb 2020 17:21:05 +0800 Subject: [PATCH] add global schema access support for mssql in gdb --- database/gdb/gdb_func.go | 18 +++++++++++++----- database/gdb/gdb_unit_z_func_test.go | 8 ++++++++ net/ghttp/ghttp_server.go | 2 +- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/database/gdb/gdb_func.go b/database/gdb/gdb_func.go index 1e495ce68..08e2c84ac 100644 --- a/database/gdb/gdb_func.go +++ b/database/gdb/gdb_func.go @@ -57,16 +57,21 @@ var ( // 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. func doHandleTableName(table, prefix, charLeft, charRight string) string { + index := 0 array1 := gstr.SplitAndTrim(table, ",") for k1, v1 := range array1 { array2 := gstr.SplitAndTrim(v1, " ") // Trim the security chars. array2[0] = gstr.TrimLeftStr(array2[0], charLeft) array2[0] = gstr.TrimRightStr(array2[0], charRight) + // Check whether it has database name. + array3 := gstr.Split(gstr.Trim(array2[0]), ".") + index = len(array3) - 1 // If the table name already has the prefix, skips the prefix adding. - if len(array2[0]) <= len(prefix) || array2[0][:len(prefix)] != prefix { - array2[0] = prefix + array2[0] + if len(array3[index]) <= len(prefix) || array3[index][:len(prefix)] != prefix { + array3[index] = prefix + array3[index] } + array2[0] = gstr.Join(array3, ".") // Add the security chars. array2[0] = doQuoteString(array2[0], charLeft, charRight) array1[k1] = gstr.Join(array2, " ") @@ -90,12 +95,15 @@ func doQuoteString(s, charLeft, charRight string) string { array1 := gstr.SplitAndTrim(s, ",") for k1, v1 := range array1 { array2 := gstr.SplitAndTrim(v1, " ") - array3 := gstr.SplitAndTrim(array2[0], ".") + array3 := gstr.Split(gstr.Trim(array2[0]), ".") if len(array3) == 1 { array3[0] = doQuoteWord(array3[0], charLeft, charRight) - } else if len(array3) == 2 { + } else if len(array3) >= 2 { array3[0] = doQuoteWord(array3[0], charLeft, charRight) - array3[1] = doQuoteWord(array3[1], charLeft, charRight) + // Note: + // mysql: u.uid + // mssql double dots: Database..Table + array3[len(array3)-1] = doQuoteWord(array3[len(array3)-1], charLeft, charRight) } array2[0] = gstr.Join(array3, ".") array1[k1] = gstr.Join(array2, " ") diff --git a/database/gdb/gdb_unit_z_func_test.go b/database/gdb/gdb_unit_z_func_test.go index c2ccde219..b416bb69a 100644 --- a/database/gdb/gdb_unit_z_func_test.go +++ b/database/gdb/gdb_unit_z_func_test.go @@ -39,6 +39,8 @@ func Test_Func_doQuoteString(t *testing.T) { "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", + // mssql global schema access with double dots. + "user..user u, user.user_detail ut": "`user`..`user` u,`user`.`user_detail` ut", } for k, v := range array { gtest.Assert(doQuoteString(k, "`", "`"), v) @@ -56,6 +58,9 @@ func Test_Func_addTablePrefix(t *testing.T) { "user,user_detail": "`user`,`user_detail`", "user u, user_detail ut": "`user` u,`user_detail` ut", "user as u, user_detail as ut": "`user` as u,`user_detail` as ut", + "UserCenter.user as u, UserCenter.user_detail as ut": "`UserCenter`.`user` as u,`UserCenter`.`user_detail` as ut", + // mssql global schema access with double dots. + "UserCenter..user as u, user_detail as ut": "`UserCenter`..`user` as u,`user_detail` as ut", } for k, v := range array { gtest.Assert(doHandleTableName(k, prefix, "`", "`"), v) @@ -70,6 +75,9 @@ func Test_Func_addTablePrefix(t *testing.T) { "user,user_detail": "`gf_user`,`gf_user_detail`", "user u, user_detail ut": "`gf_user` u,`gf_user_detail` ut", "user as u, user_detail as ut": "`gf_user` as u,`gf_user_detail` as ut", + "UserCenter.user as u, UserCenter.user_detail as ut": "`UserCenter`.`gf_user` as u,`UserCenter`.`gf_user_detail` as ut", + // mssql global schema access with double dots. + "UserCenter..user as u, user_detail as ut": "`UserCenter`..`gf_user` as u,`gf_user_detail` as ut", } for k, v := range array { gtest.Assert(doHandleTableName(k, prefix, "`", "`"), v) diff --git a/net/ghttp/ghttp_server.go b/net/ghttp/ghttp_server.go index bc21fc661..82c83571d 100644 --- a/net/ghttp/ghttp_server.go +++ b/net/ghttp/ghttp_server.go @@ -166,7 +166,7 @@ var ( // The process can only be initialized once. serverProcessInited = gtype.NewBool() - // gracefulEnabled is used for graceful restarting feature, which is false in default. + // gracefulEnabled is used for graceful reload feature, which is false in default. gracefulEnabled = false )