github.com/wangyougui/gf/v2@v2.6.5/database/gdb/gdb_z_mysql_internal_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/wangyougui/gf. 6 7 package gdb 8 9 import ( 10 "context" 11 "fmt" 12 "testing" 13 14 "github.com/wangyougui/gf/v2/test/gtest" 15 "github.com/wangyougui/gf/v2/text/gregex" 16 ) 17 18 var ( 19 db DB 20 ctx = context.TODO() 21 ) 22 23 func Test_HookSelect_Regex(t *testing.T) { 24 gtest.C(t, func(t *gtest.T) { 25 var ( 26 err error 27 toBeCommittedSql = `select * from "user" where 1=1` 28 ) 29 toBeCommittedSql, err = gregex.ReplaceStringFuncMatch( 30 `(?i) FROM ([\S]+)`, 31 toBeCommittedSql, 32 func(match []string) string { 33 34 return fmt.Sprintf(` FROM "%s"`, "user_1") 35 }, 36 ) 37 t.AssertNil(err) 38 t.Assert(toBeCommittedSql, `select * FROM "user_1" where 1=1`) 39 }) 40 gtest.C(t, func(t *gtest.T) { 41 var ( 42 err error 43 toBeCommittedSql = `select * from user` 44 ) 45 toBeCommittedSql, err = gregex.ReplaceStringFuncMatch( 46 `(?i) FROM ([\S]+)`, 47 toBeCommittedSql, 48 func(match []string) string { 49 return fmt.Sprintf(` FROM %s`, "user_1") 50 }, 51 ) 52 t.AssertNil(err) 53 t.Assert(toBeCommittedSql, `select * FROM user_1`) 54 }) 55 } 56 57 func Test_parseConfigNodeLink_WithType(t *testing.T) { 58 gtest.C(t, func(t *gtest.T) { 59 node := &ConfigNode{ 60 Link: `mysql:root:CxzhD*624:27jh@tcp(9.135.69.119:3306)/khaos_oss?loc=Local&parseTime=true&charset=latin`, 61 } 62 newNode := parseConfigNodeLink(node) 63 t.Assert(newNode.Type, `mysql`) 64 t.Assert(newNode.User, `root`) 65 t.Assert(newNode.Pass, `CxzhD*624:27jh`) 66 t.Assert(newNode.Host, `9.135.69.119`) 67 t.Assert(newNode.Port, `3306`) 68 t.Assert(newNode.Name, `khaos_oss`) 69 t.Assert(newNode.Extra, `loc=Local&parseTime=true&charset=latin`) 70 t.Assert(newNode.Charset, `latin`) 71 t.Assert(newNode.Protocol, `tcp`) 72 }) 73 gtest.C(t, func(t *gtest.T) { 74 node := &ConfigNode{ 75 Link: `mysql:root:CxzhD*624:27jh@tcp(9.135.69.119:3306)/khaos_oss?`, 76 } 77 newNode := parseConfigNodeLink(node) 78 t.Assert(newNode.Type, `mysql`) 79 t.Assert(newNode.User, `root`) 80 t.Assert(newNode.Pass, `CxzhD*624:27jh`) 81 t.Assert(newNode.Host, `9.135.69.119`) 82 t.Assert(newNode.Port, `3306`) 83 t.Assert(newNode.Name, `khaos_oss`) 84 t.Assert(newNode.Extra, ``) 85 t.Assert(newNode.Charset, defaultCharset) 86 t.Assert(newNode.Protocol, `tcp`) 87 }) 88 gtest.C(t, func(t *gtest.T) { 89 node := &ConfigNode{ 90 Link: `mysql:root:CxzhD*624:27jh@tcp(9.135.69.119:3306)/khaos_oss`, 91 } 92 newNode := parseConfigNodeLink(node) 93 t.Assert(newNode.Type, `mysql`) 94 t.Assert(newNode.User, `root`) 95 t.Assert(newNode.Pass, `CxzhD*624:27jh`) 96 t.Assert(newNode.Host, `9.135.69.119`) 97 t.Assert(newNode.Port, `3306`) 98 t.Assert(newNode.Name, `khaos_oss`) 99 t.Assert(newNode.Extra, ``) 100 t.Assert(newNode.Charset, defaultCharset) 101 t.Assert(newNode.Protocol, `tcp`) 102 }) 103 // empty database preselect. 104 gtest.C(t, func(t *gtest.T) { 105 node := &ConfigNode{ 106 Link: `mysql:root:CxzhD*624:27jh@tcp(9.135.69.119:3306)/?loc=Local&parseTime=true&charset=latin`, 107 } 108 newNode := parseConfigNodeLink(node) 109 t.Assert(newNode.Type, `mysql`) 110 t.Assert(newNode.User, `root`) 111 t.Assert(newNode.Pass, `CxzhD*624:27jh`) 112 t.Assert(newNode.Host, `9.135.69.119`) 113 t.Assert(newNode.Port, `3306`) 114 t.Assert(newNode.Name, ``) 115 t.Assert(newNode.Extra, `loc=Local&parseTime=true&charset=latin`) 116 t.Assert(newNode.Charset, `latin`) 117 t.Assert(newNode.Protocol, `tcp`) 118 }) 119 gtest.C(t, func(t *gtest.T) { 120 node := &ConfigNode{ 121 Link: `mysql:root:CxzhD*624:27jh@tcp(9.135.69.119:3306)?loc=Local&parseTime=true&charset=latin`, 122 } 123 newNode := parseConfigNodeLink(node) 124 t.Assert(newNode.Type, `mysql`) 125 t.Assert(newNode.User, `root`) 126 t.Assert(newNode.Pass, `CxzhD*624:27jh`) 127 t.Assert(newNode.Host, `9.135.69.119`) 128 t.Assert(newNode.Port, `3306`) 129 t.Assert(newNode.Name, ``) 130 t.Assert(newNode.Extra, `loc=Local&parseTime=true&charset=latin`) 131 t.Assert(newNode.Charset, `latin`) 132 t.Assert(newNode.Protocol, `tcp`) 133 }) 134 gtest.C(t, func(t *gtest.T) { 135 node := &ConfigNode{ 136 Link: `mysql:root:CxzhD*624:27jh@tcp(9.135.69.119:3306)/`, 137 } 138 newNode := parseConfigNodeLink(node) 139 t.Assert(newNode.Type, `mysql`) 140 t.Assert(newNode.User, `root`) 141 t.Assert(newNode.Pass, `CxzhD*624:27jh`) 142 t.Assert(newNode.Host, `9.135.69.119`) 143 t.Assert(newNode.Port, `3306`) 144 t.Assert(newNode.Name, ``) 145 t.Assert(newNode.Extra, ``) 146 t.Assert(newNode.Charset, defaultCharset) 147 t.Assert(newNode.Protocol, `tcp`) 148 }) 149 gtest.C(t, func(t *gtest.T) { 150 node := &ConfigNode{ 151 Link: `mysql:root:CxzhD*624:27jh@tcp(9.135.69.119:3306)`, 152 } 153 newNode := parseConfigNodeLink(node) 154 t.Assert(newNode.Type, `mysql`) 155 t.Assert(newNode.User, `root`) 156 t.Assert(newNode.Pass, `CxzhD*624:27jh`) 157 t.Assert(newNode.Host, `9.135.69.119`) 158 t.Assert(newNode.Port, `3306`) 159 t.Assert(newNode.Name, ``) 160 t.Assert(newNode.Extra, ``) 161 t.Assert(newNode.Charset, defaultCharset) 162 t.Assert(newNode.Protocol, `tcp`) 163 }) 164 // udp. 165 gtest.C(t, func(t *gtest.T) { 166 node := &ConfigNode{ 167 Link: `mysql:root:CxzhD*624:27jh@udp(9.135.69.119:3306)`, 168 } 169 newNode := parseConfigNodeLink(node) 170 t.Assert(newNode.Type, `mysql`) 171 t.Assert(newNode.User, `root`) 172 t.Assert(newNode.Pass, `CxzhD*624:27jh`) 173 t.Assert(newNode.Host, `9.135.69.119`) 174 t.Assert(newNode.Port, `3306`) 175 t.Assert(newNode.Name, ``) 176 t.Assert(newNode.Extra, ``) 177 t.Assert(newNode.Charset, defaultCharset) 178 t.Assert(newNode.Protocol, `udp`) 179 }) 180 gtest.C(t, func(t *gtest.T) { 181 node := &ConfigNode{ 182 Link: `sqlite:root:CxzhD*624:27jh@file(/var/data/db.sqlite3)?local=Local&parseTime=true`, 183 } 184 newNode := parseConfigNodeLink(node) 185 t.Assert(newNode.Type, `sqlite`) 186 t.Assert(newNode.User, `root`) 187 t.Assert(newNode.Pass, `CxzhD*624:27jh`) 188 t.Assert(newNode.Host, ``) 189 t.Assert(newNode.Port, ``) 190 t.Assert(newNode.Name, `/var/data/db.sqlite3`) 191 t.Assert(newNode.Extra, `local=Local&parseTime=true`) 192 t.Assert(newNode.Charset, defaultCharset) 193 t.Assert(newNode.Protocol, `file`) 194 }) 195 gtest.C(t, func(t *gtest.T) { 196 node := &ConfigNode{ 197 Link: `sqlite::CxzhD*624:2@7jh@file(/var/data/db.sqlite3)`, 198 } 199 newNode := parseConfigNodeLink(node) 200 t.Assert(newNode.Type, `sqlite`) 201 t.Assert(newNode.User, ``) 202 t.Assert(newNode.Pass, `CxzhD*624:2@7jh`) 203 t.Assert(newNode.Host, ``) 204 t.Assert(newNode.Port, ``) 205 t.Assert(newNode.Name, `/var/data/db.sqlite3`) 206 t.Assert(newNode.Extra, ``) 207 t.Assert(newNode.Charset, defaultCharset) 208 t.Assert(newNode.Protocol, `file`) 209 }) 210 gtest.C(t, func(t *gtest.T) { 211 node := &ConfigNode{ 212 Link: `sqlite::@file(/var/data/db.sqlite3)`, 213 } 214 newNode := parseConfigNodeLink(node) 215 t.Assert(newNode.Type, `sqlite`) 216 t.Assert(newNode.User, ``) 217 t.Assert(newNode.Pass, ``) 218 t.Assert(newNode.Host, ``) 219 t.Assert(newNode.Port, ``) 220 t.Assert(newNode.Name, `/var/data/db.sqlite3`) 221 t.Assert(newNode.Extra, ``) 222 t.Assert(newNode.Charset, defaultCharset) 223 t.Assert(newNode.Protocol, `file`) 224 }) 225 // #3146 226 gtest.C(t, func(t *gtest.T) { 227 node := &ConfigNode{ 228 Link: `pgsql:BASIC$xxxx:123456@tcp(xxxx.hologres.aliyuncs.com:80)/xxx`, 229 } 230 newNode := parseConfigNodeLink(node) 231 t.Assert(newNode.Type, `pgsql`) 232 t.Assert(newNode.User, `BASIC$xxxx`) 233 t.Assert(newNode.Pass, `123456`) 234 t.Assert(newNode.Host, `xxxx.hologres.aliyuncs.com`) 235 t.Assert(newNode.Port, `80`) 236 t.Assert(newNode.Name, `xxx`) 237 t.Assert(newNode.Extra, ``) 238 t.Assert(newNode.Charset, defaultCharset) 239 t.Assert(newNode.Protocol, `tcp`) 240 }) 241 } 242 243 func Test_Func_doQuoteWord(t *testing.T) { 244 gtest.C(t, func(t *gtest.T) { 245 array := map[string]string{ 246 "user": "`user`", 247 "user u": "user u", 248 "user_detail": "`user_detail`", 249 "user,user_detail": "user,user_detail", 250 "user u, user_detail ut": "user u, user_detail ut", 251 "u.id asc": "u.id asc", 252 "u.id asc, ut.uid desc": "u.id asc, ut.uid desc", 253 } 254 for k, v := range array { 255 t.Assert(doQuoteWord(k, "`", "`"), v) 256 } 257 }) 258 } 259 260 func Test_Func_doQuoteString(t *testing.T) { 261 gtest.C(t, func(t *gtest.T) { 262 array := map[string]string{ 263 "user": "`user`", 264 "user u": "`user` u", 265 "user,user_detail": "`user`,`user_detail`", 266 "user u, user_detail ut": "`user` u,`user_detail` ut", 267 "u.id, u.name, u.age": "`u`.`id`,`u`.`name`,`u`.`age`", 268 "u.id asc": "`u`.`id` asc", 269 "u.id asc, ut.uid desc": "`u`.`id` asc,`ut`.`uid` desc", 270 "user.user u, user.user_detail ut": "`user`.`user` u,`user`.`user_detail` ut", 271 // mssql global schema access with double dots. 272 "user..user u, user.user_detail ut": "`user`..`user` u,`user`.`user_detail` ut", 273 } 274 for k, v := range array { 275 t.Assert(doQuoteString(k, "`", "`"), v) 276 } 277 }) 278 } 279 280 func Test_Func_addTablePrefix(t *testing.T) { 281 gtest.C(t, func(t *gtest.T) { 282 prefix := "" 283 array := map[string]string{ 284 "user": "`user`", 285 "user u": "`user` u", 286 "user as u": "`user` as u", 287 "user,user_detail": "`user`,`user_detail`", 288 "user u, user_detail ut": "`user` u,`user_detail` ut", 289 "`user`.user_detail": "`user`.`user_detail`", 290 "`user`.`user_detail`": "`user`.`user_detail`", 291 "user as u, user_detail as ut": "`user` as u,`user_detail` as ut", 292 "UserCenter.user as u, UserCenter.user_detail as ut": "`UserCenter`.`user` as u,`UserCenter`.`user_detail` as ut", 293 // mssql global schema access with double dots. 294 "UserCenter..user as u, user_detail as ut": "`UserCenter`..`user` as u,`user_detail` as ut", 295 } 296 for k, v := range array { 297 t.Assert(doQuoteTableName(k, prefix, "`", "`"), v) 298 } 299 }) 300 gtest.C(t, func(t *gtest.T) { 301 prefix := "gf_" 302 array := map[string]string{ 303 "user": "`gf_user`", 304 "user u": "`gf_user` u", 305 "user as u": "`gf_user` as u", 306 "user,user_detail": "`gf_user`,`gf_user_detail`", 307 "user u, user_detail ut": "`gf_user` u,`gf_user_detail` ut", 308 "`user`.user_detail": "`user`.`gf_user_detail`", 309 "`user`.`user_detail`": "`user`.`gf_user_detail`", 310 "user as u, user_detail as ut": "`gf_user` as u,`gf_user_detail` as ut", 311 "UserCenter.user as u, UserCenter.user_detail as ut": "`UserCenter`.`gf_user` as u,`UserCenter`.`gf_user_detail` as ut", 312 // mssql global schema access with double dots. 313 "UserCenter..user as u, user_detail as ut": "`UserCenter`..`gf_user` as u,`gf_user_detail` as ut", 314 } 315 for k, v := range array { 316 t.Assert(doQuoteTableName(k, prefix, "`", "`"), v) 317 } 318 }) 319 } 320 321 func Test_isSubQuery(t *testing.T) { 322 gtest.C(t, func(t *gtest.T) { 323 t.Assert(isSubQuery("user"), false) 324 t.Assert(isSubQuery("user.uid"), false) 325 t.Assert(isSubQuery("u, user.uid"), false) 326 t.Assert(isSubQuery("select 1"), true) 327 }) 328 }