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