Simple and Powerful ORM for Go, support mysql,postgres,tidb,sqlite3,mssql,oracle https://xorm.io
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

session_query_test.go 10 KiB

Add test for join subquery (#1528) Fix test Fix subquery with schema Add test for join subquery Add makefile (#1531) Fix drone Fix ci Add deps Improve drone Fix envs Add makefile Reviewed-on: https://gitea.com/xorm/xorm/pulls/1531 Add password for postgres drone image (#1530) Add password for postgres drone image Reviewed-on: https://gitea.com/xorm/xorm/pulls/1530 format time when sqlTypeName is core.Varchar (#1026) fix time test add test for time format sign codes according to contributing rules. format time when sqlTypeName is core.Varchar. Same with core.DateTime or core.TimeStamp Add test for second insert error (#1527) Add test for second insert error Reviewed-on: https://gitea.com/xorm/xorm/pulls/1527 Add tests for table name (#1517) add tests for table name Fix test (#1526) Fix test Reviewed-on: https://gitea.com/xorm/xorm/pulls/1526 Fix test (#1526) Fix test Reviewed-on: https://gitea.com/xorm/xorm/pulls/1526 Fix wrong warning log on autoincrement column when sync table (#1525) improve doc Fix wrong warning log on autoincrement column when sync table Reviewed-on: https://gitea.com/xorm/xorm/pulls/1525 Fixed Join strings on func Exist (#1520) fix test fixed Join strings on func Exist Co-authored-by: Tomofumi Kusana <tkusana@morisawa.co.jp> Reviewed-on: https://gitea.com/xorm/xorm/pulls/1520 For nullable columns, store nil values as NULL (#531) Merge branch 'master' into jcsalem/fix/nil_ptr_is_nullable fix bug when buffersize with iterate (#941) Merge branch 'master' into lunny/fix_buffer_iterate Exclude schema from index name (#1505) Merge branch 'master' into fix-schema-idx SetExpr support more go types (#1499) Improve tests SetExpr support more go types fix vet fix drone lint remove go1.10 test on drone Reviewed-on: https://gitea.com/xorm/xorm/pulls/1499 fix vet fix drone lint remove go1.10 test on drone Exclude schema from the index name Co-authored-by: Guillermo Prandi <guillep2k@users.noreply.github.com> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com> Reviewed-on: https://gitea.com/xorm/xorm/pulls/1505 fix test fix bug fix bug when buffersize with iterate SetExpr support more go types (#1499) Improve tests SetExpr support more go types fix vet fix drone lint remove go1.10 test on drone Reviewed-on: https://gitea.com/xorm/xorm/pulls/1499 fix vet fix drone lint remove go1.10 test on drone Fix update with Alias (#1455) Co-authored-by: Guillermo Prandi <guillep2k@noreply.gitea.io> Reviewed-on: https://gitea.com/xorm/xorm/pulls/941 fix update map with version (#1448) fix test fix update map with version SetExpr support more go types (#1499) Improve tests SetExpr support more go types fix vet fix drone lint remove go1.10 test on drone Reviewed-on: https://gitea.com/xorm/xorm/pulls/1499 fix vet fix drone lint remove go1.10 test on drone Fix update with Alias (#1455) Reviewed-on: https://gitea.com/xorm/xorm/pulls/1448 Exclude schema from index name (#1505) Merge branch 'master' into fix-schema-idx SetExpr support more go types (#1499) Improve tests SetExpr support more go types fix vet fix drone lint remove go1.10 test on drone Reviewed-on: https://gitea.com/xorm/xorm/pulls/1499 fix vet fix drone lint remove go1.10 test on drone Exclude schema from the index name Co-authored-by: Guillermo Prandi <guillep2k@users.noreply.github.com> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com> Reviewed-on: https://gitea.com/xorm/xorm/pulls/1505 SetExpr support more go types (#1499) Improve tests SetExpr support more go types fix vet fix drone lint remove go1.10 test on drone Reviewed-on: https://gitea.com/xorm/xorm/pulls/1499 For nullable columns, store nil values as NULL fix vet fix drone lint remove go1.10 test on drone Fix update with Alias (#1455) Improve ci tests (#1477) Rewrite Engine.QuoteTo() to accept multi-part identifiers (#1476) Support local sql log (#1338) Fix go mod and update version (#1460) Move github.com/go-xorm/xorm to xorm.io/xorm (#1459) add support custom type Nullfloat64 (#1450) fix bug when query map condtion with no quote (#1449) Don't warn when bool column default is 1 but not true (#1447) * don't warn when bool column default is 1 but not true * fix default case sensitive Fix sync2 with custom table name (#1445) * fix sync2 with custom table name * fix bug on postgres * fix bug on postgres fix bug when update with setexpr (#1446) add tidb tests on drone ci (#1444) improve sync2 (#1443) Fix wrong dbmetas (#1442) * add tests for db metas * add more tests * fix bug on mssql Fix default value parse bugs (#1437) * fix default value * fix default value tags * fix postgres default * fix default on postgres * fix default on postgres * fix mssql default fix arg conversion (#1441) * fix arg conversion * fix bugs * fix bug on postgres * use traditional positional parameters on insert into select * remove unnecessary tests upgrade core (#1440) add tests (#1439) add go1.13 tests on drone (#1416) Fix bug on insert where (#1436) * fix bug on insert where * fix bug * fix lint fix bug when insert multiple... Reviewed-on: https://gitea.com/xorm/xorm/pulls/1528
6 days ago
Add test for join subquery (#1528) Fix test Fix subquery with schema Add test for join subquery Add makefile (#1531) Fix drone Fix ci Add deps Improve drone Fix envs Add makefile Reviewed-on: https://gitea.com/xorm/xorm/pulls/1531 Add password for postgres drone image (#1530) Add password for postgres drone image Reviewed-on: https://gitea.com/xorm/xorm/pulls/1530 format time when sqlTypeName is core.Varchar (#1026) fix time test add test for time format sign codes according to contributing rules. format time when sqlTypeName is core.Varchar. Same with core.DateTime or core.TimeStamp Add test for second insert error (#1527) Add test for second insert error Reviewed-on: https://gitea.com/xorm/xorm/pulls/1527 Add tests for table name (#1517) add tests for table name Fix test (#1526) Fix test Reviewed-on: https://gitea.com/xorm/xorm/pulls/1526 Fix test (#1526) Fix test Reviewed-on: https://gitea.com/xorm/xorm/pulls/1526 Fix wrong warning log on autoincrement column when sync table (#1525) improve doc Fix wrong warning log on autoincrement column when sync table Reviewed-on: https://gitea.com/xorm/xorm/pulls/1525 Fixed Join strings on func Exist (#1520) fix test fixed Join strings on func Exist Co-authored-by: Tomofumi Kusana <tkusana@morisawa.co.jp> Reviewed-on: https://gitea.com/xorm/xorm/pulls/1520 For nullable columns, store nil values as NULL (#531) Merge branch 'master' into jcsalem/fix/nil_ptr_is_nullable fix bug when buffersize with iterate (#941) Merge branch 'master' into lunny/fix_buffer_iterate Exclude schema from index name (#1505) Merge branch 'master' into fix-schema-idx SetExpr support more go types (#1499) Improve tests SetExpr support more go types fix vet fix drone lint remove go1.10 test on drone Reviewed-on: https://gitea.com/xorm/xorm/pulls/1499 fix vet fix drone lint remove go1.10 test on drone Exclude schema from the index name Co-authored-by: Guillermo Prandi <guillep2k@users.noreply.github.com> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com> Reviewed-on: https://gitea.com/xorm/xorm/pulls/1505 fix test fix bug fix bug when buffersize with iterate SetExpr support more go types (#1499) Improve tests SetExpr support more go types fix vet fix drone lint remove go1.10 test on drone Reviewed-on: https://gitea.com/xorm/xorm/pulls/1499 fix vet fix drone lint remove go1.10 test on drone Fix update with Alias (#1455) Co-authored-by: Guillermo Prandi <guillep2k@noreply.gitea.io> Reviewed-on: https://gitea.com/xorm/xorm/pulls/941 fix update map with version (#1448) fix test fix update map with version SetExpr support more go types (#1499) Improve tests SetExpr support more go types fix vet fix drone lint remove go1.10 test on drone Reviewed-on: https://gitea.com/xorm/xorm/pulls/1499 fix vet fix drone lint remove go1.10 test on drone Fix update with Alias (#1455) Reviewed-on: https://gitea.com/xorm/xorm/pulls/1448 Exclude schema from index name (#1505) Merge branch 'master' into fix-schema-idx SetExpr support more go types (#1499) Improve tests SetExpr support more go types fix vet fix drone lint remove go1.10 test on drone Reviewed-on: https://gitea.com/xorm/xorm/pulls/1499 fix vet fix drone lint remove go1.10 test on drone Exclude schema from the index name Co-authored-by: Guillermo Prandi <guillep2k@users.noreply.github.com> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com> Reviewed-on: https://gitea.com/xorm/xorm/pulls/1505 SetExpr support more go types (#1499) Improve tests SetExpr support more go types fix vet fix drone lint remove go1.10 test on drone Reviewed-on: https://gitea.com/xorm/xorm/pulls/1499 For nullable columns, store nil values as NULL fix vet fix drone lint remove go1.10 test on drone Fix update with Alias (#1455) Improve ci tests (#1477) Rewrite Engine.QuoteTo() to accept multi-part identifiers (#1476) Support local sql log (#1338) Fix go mod and update version (#1460) Move github.com/go-xorm/xorm to xorm.io/xorm (#1459) add support custom type Nullfloat64 (#1450) fix bug when query map condtion with no quote (#1449) Don't warn when bool column default is 1 but not true (#1447) * don't warn when bool column default is 1 but not true * fix default case sensitive Fix sync2 with custom table name (#1445) * fix sync2 with custom table name * fix bug on postgres * fix bug on postgres fix bug when update with setexpr (#1446) add tidb tests on drone ci (#1444) improve sync2 (#1443) Fix wrong dbmetas (#1442) * add tests for db metas * add more tests * fix bug on mssql Fix default value parse bugs (#1437) * fix default value * fix default value tags * fix postgres default * fix default on postgres * fix default on postgres * fix mssql default fix arg conversion (#1441) * fix arg conversion * fix bugs * fix bug on postgres * use traditional positional parameters on insert into select * remove unnecessary tests upgrade core (#1440) add tests (#1439) add go1.13 tests on drone (#1416) Fix bug on insert where (#1436) * fix bug on insert where * fix bug * fix lint fix bug when insert multiple... Reviewed-on: https://gitea.com/xorm/xorm/pulls/1528
6 days ago
Add test for join subquery (#1528) Fix test Fix subquery with schema Add test for join subquery Add makefile (#1531) Fix drone Fix ci Add deps Improve drone Fix envs Add makefile Reviewed-on: https://gitea.com/xorm/xorm/pulls/1531 Add password for postgres drone image (#1530) Add password for postgres drone image Reviewed-on: https://gitea.com/xorm/xorm/pulls/1530 format time when sqlTypeName is core.Varchar (#1026) fix time test add test for time format sign codes according to contributing rules. format time when sqlTypeName is core.Varchar. Same with core.DateTime or core.TimeStamp Add test for second insert error (#1527) Add test for second insert error Reviewed-on: https://gitea.com/xorm/xorm/pulls/1527 Add tests for table name (#1517) add tests for table name Fix test (#1526) Fix test Reviewed-on: https://gitea.com/xorm/xorm/pulls/1526 Fix test (#1526) Fix test Reviewed-on: https://gitea.com/xorm/xorm/pulls/1526 Fix wrong warning log on autoincrement column when sync table (#1525) improve doc Fix wrong warning log on autoincrement column when sync table Reviewed-on: https://gitea.com/xorm/xorm/pulls/1525 Fixed Join strings on func Exist (#1520) fix test fixed Join strings on func Exist Co-authored-by: Tomofumi Kusana <tkusana@morisawa.co.jp> Reviewed-on: https://gitea.com/xorm/xorm/pulls/1520 For nullable columns, store nil values as NULL (#531) Merge branch 'master' into jcsalem/fix/nil_ptr_is_nullable fix bug when buffersize with iterate (#941) Merge branch 'master' into lunny/fix_buffer_iterate Exclude schema from index name (#1505) Merge branch 'master' into fix-schema-idx SetExpr support more go types (#1499) Improve tests SetExpr support more go types fix vet fix drone lint remove go1.10 test on drone Reviewed-on: https://gitea.com/xorm/xorm/pulls/1499 fix vet fix drone lint remove go1.10 test on drone Exclude schema from the index name Co-authored-by: Guillermo Prandi <guillep2k@users.noreply.github.com> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com> Reviewed-on: https://gitea.com/xorm/xorm/pulls/1505 fix test fix bug fix bug when buffersize with iterate SetExpr support more go types (#1499) Improve tests SetExpr support more go types fix vet fix drone lint remove go1.10 test on drone Reviewed-on: https://gitea.com/xorm/xorm/pulls/1499 fix vet fix drone lint remove go1.10 test on drone Fix update with Alias (#1455) Co-authored-by: Guillermo Prandi <guillep2k@noreply.gitea.io> Reviewed-on: https://gitea.com/xorm/xorm/pulls/941 fix update map with version (#1448) fix test fix update map with version SetExpr support more go types (#1499) Improve tests SetExpr support more go types fix vet fix drone lint remove go1.10 test on drone Reviewed-on: https://gitea.com/xorm/xorm/pulls/1499 fix vet fix drone lint remove go1.10 test on drone Fix update with Alias (#1455) Reviewed-on: https://gitea.com/xorm/xorm/pulls/1448 Exclude schema from index name (#1505) Merge branch 'master' into fix-schema-idx SetExpr support more go types (#1499) Improve tests SetExpr support more go types fix vet fix drone lint remove go1.10 test on drone Reviewed-on: https://gitea.com/xorm/xorm/pulls/1499 fix vet fix drone lint remove go1.10 test on drone Exclude schema from the index name Co-authored-by: Guillermo Prandi <guillep2k@users.noreply.github.com> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com> Reviewed-on: https://gitea.com/xorm/xorm/pulls/1505 SetExpr support more go types (#1499) Improve tests SetExpr support more go types fix vet fix drone lint remove go1.10 test on drone Reviewed-on: https://gitea.com/xorm/xorm/pulls/1499 For nullable columns, store nil values as NULL fix vet fix drone lint remove go1.10 test on drone Fix update with Alias (#1455) Improve ci tests (#1477) Rewrite Engine.QuoteTo() to accept multi-part identifiers (#1476) Support local sql log (#1338) Fix go mod and update version (#1460) Move github.com/go-xorm/xorm to xorm.io/xorm (#1459) add support custom type Nullfloat64 (#1450) fix bug when query map condtion with no quote (#1449) Don't warn when bool column default is 1 but not true (#1447) * don't warn when bool column default is 1 but not true * fix default case sensitive Fix sync2 with custom table name (#1445) * fix sync2 with custom table name * fix bug on postgres * fix bug on postgres fix bug when update with setexpr (#1446) add tidb tests on drone ci (#1444) improve sync2 (#1443) Fix wrong dbmetas (#1442) * add tests for db metas * add more tests * fix bug on mssql Fix default value parse bugs (#1437) * fix default value * fix default value tags * fix postgres default * fix default on postgres * fix default on postgres * fix mssql default fix arg conversion (#1441) * fix arg conversion * fix bugs * fix bug on postgres * use traditional positional parameters on insert into select * remove unnecessary tests upgrade core (#1440) add tests (#1439) add go1.13 tests on drone (#1416) Fix bug on insert where (#1436) * fix bug on insert where * fix bug * fix lint fix bug when insert multiple... Reviewed-on: https://gitea.com/xorm/xorm/pulls/1528
6 days ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. // Copyright 2017 The Xorm Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package xorm
  5. import (
  6. "fmt"
  7. "strconv"
  8. "testing"
  9. "time"
  10. "xorm.io/builder"
  11. "xorm.io/xorm/schemas"
  12. "github.com/stretchr/testify/assert"
  13. )
  14. func TestQueryString(t *testing.T) {
  15. assert.NoError(t, prepareEngine())
  16. type GetVar2 struct {
  17. Id int64 `xorm:"autoincr pk"`
  18. Msg string `xorm:"varchar(255)"`
  19. Age int
  20. Money float32
  21. Created time.Time `xorm:"created"`
  22. }
  23. assert.NoError(t, testEngine.Sync2(new(GetVar2)))
  24. var data = GetVar2{
  25. Msg: "hi",
  26. Age: 28,
  27. Money: 1.5,
  28. }
  29. _, err := testEngine.InsertOne(data)
  30. assert.NoError(t, err)
  31. records, err := testEngine.QueryString("select * from " + testEngine.TableName("get_var2", true))
  32. assert.NoError(t, err)
  33. assert.Equal(t, 1, len(records))
  34. assert.Equal(t, 5, len(records[0]))
  35. assert.Equal(t, "1", records[0]["id"])
  36. assert.Equal(t, "hi", records[0]["msg"])
  37. assert.Equal(t, "28", records[0]["age"])
  38. assert.Equal(t, "1.5", records[0]["money"])
  39. }
  40. func TestQueryString2(t *testing.T) {
  41. assert.NoError(t, prepareEngine())
  42. type GetVar3 struct {
  43. Id int64 `xorm:"autoincr pk"`
  44. Msg bool `xorm:"bit"`
  45. }
  46. assert.NoError(t, testEngine.Sync2(new(GetVar3)))
  47. var data = GetVar3{
  48. Msg: false,
  49. }
  50. _, err := testEngine.Insert(data)
  51. assert.NoError(t, err)
  52. records, err := testEngine.QueryString("select * from " + testEngine.TableName("get_var3", true))
  53. assert.NoError(t, err)
  54. assert.Equal(t, 1, len(records))
  55. assert.Equal(t, 2, len(records[0]))
  56. assert.Equal(t, "1", records[0]["id"])
  57. assert.True(t, "0" == records[0]["msg"] || "false" == records[0]["msg"])
  58. }
  59. func toString(i interface{}) string {
  60. switch i.(type) {
  61. case []byte:
  62. return string(i.([]byte))
  63. case string:
  64. return i.(string)
  65. }
  66. return fmt.Sprintf("%v", i)
  67. }
  68. func toInt64(i interface{}) int64 {
  69. switch i.(type) {
  70. case []byte:
  71. n, _ := strconv.ParseInt(string(i.([]byte)), 10, 64)
  72. return n
  73. case int:
  74. return int64(i.(int))
  75. case int64:
  76. return i.(int64)
  77. }
  78. return 0
  79. }
  80. func toFloat64(i interface{}) float64 {
  81. switch i.(type) {
  82. case []byte:
  83. n, _ := strconv.ParseFloat(string(i.([]byte)), 64)
  84. return n
  85. case float64:
  86. return i.(float64)
  87. case float32:
  88. return float64(i.(float32))
  89. }
  90. return 0
  91. }
  92. func TestQueryInterface(t *testing.T) {
  93. assert.NoError(t, prepareEngine())
  94. type GetVarInterface struct {
  95. Id int64 `xorm:"autoincr pk"`
  96. Msg string `xorm:"varchar(255)"`
  97. Age int
  98. Money float32
  99. Created time.Time `xorm:"created"`
  100. }
  101. assert.NoError(t, testEngine.Sync2(new(GetVarInterface)))
  102. var data = GetVarInterface{
  103. Msg: "hi",
  104. Age: 28,
  105. Money: 1.5,
  106. }
  107. _, err := testEngine.InsertOne(data)
  108. assert.NoError(t, err)
  109. records, err := testEngine.QueryInterface("select * from " + testEngine.TableName("get_var_interface", true))
  110. assert.NoError(t, err)
  111. assert.Equal(t, 1, len(records))
  112. assert.Equal(t, 5, len(records[0]))
  113. assert.EqualValues(t, 1, toInt64(records[0]["id"]))
  114. assert.Equal(t, "hi", toString(records[0]["msg"]))
  115. assert.EqualValues(t, 28, toInt64(records[0]["age"]))
  116. assert.EqualValues(t, 1.5, toFloat64(records[0]["money"]))
  117. }
  118. func TestQueryNoParams(t *testing.T) {
  119. assert.NoError(t, prepareEngine())
  120. type QueryNoParams struct {
  121. Id int64 `xorm:"autoincr pk"`
  122. Msg string `xorm:"varchar(255)"`
  123. Age int
  124. Money float32
  125. Created time.Time `xorm:"created"`
  126. }
  127. testEngine.ShowSQL(true)
  128. assert.NoError(t, testEngine.Sync2(new(QueryNoParams)))
  129. var q = QueryNoParams{
  130. Msg: "message",
  131. Age: 20,
  132. Money: 3000,
  133. }
  134. cnt, err := testEngine.Insert(&q)
  135. assert.NoError(t, err)
  136. assert.EqualValues(t, 1, cnt)
  137. assertResult := func(t *testing.T, results []map[string][]byte) {
  138. assert.EqualValues(t, 1, len(results))
  139. id, err := strconv.ParseInt(string(results[0]["id"]), 10, 64)
  140. assert.NoError(t, err)
  141. assert.EqualValues(t, 1, id)
  142. assert.Equal(t, "message", string(results[0]["msg"]))
  143. age, err := strconv.Atoi(string(results[0]["age"]))
  144. assert.NoError(t, err)
  145. assert.EqualValues(t, 20, age)
  146. money, err := strconv.ParseFloat(string(results[0]["money"]), 32)
  147. assert.NoError(t, err)
  148. assert.EqualValues(t, 3000, money)
  149. }
  150. results, err := testEngine.Table("query_no_params").Limit(10).Query()
  151. assert.NoError(t, err)
  152. assertResult(t, results)
  153. results, err = testEngine.SQL("select * from " + testEngine.TableName("query_no_params", true)).Query()
  154. assert.NoError(t, err)
  155. assertResult(t, results)
  156. }
  157. func TestQueryStringNoParam(t *testing.T) {
  158. assert.NoError(t, prepareEngine())
  159. type GetVar4 struct {
  160. Id int64 `xorm:"autoincr pk"`
  161. Msg bool `xorm:"bit"`
  162. }
  163. assert.NoError(t, testEngine.Sync2(new(GetVar4)))
  164. var data = GetVar4{
  165. Msg: false,
  166. }
  167. _, err := testEngine.Insert(data)
  168. assert.NoError(t, err)
  169. records, err := testEngine.Table("get_var4").Limit(1).QueryString()
  170. assert.NoError(t, err)
  171. assert.EqualValues(t, 1, len(records))
  172. assert.EqualValues(t, "1", records[0]["id"])
  173. if testEngine.Dialect().DBType() == schemas.POSTGRES || testEngine.Dialect().DBType() == schemas.MSSQL {
  174. assert.EqualValues(t, "false", records[0]["msg"])
  175. } else {
  176. assert.EqualValues(t, "0", records[0]["msg"])
  177. }
  178. records, err = testEngine.Table("get_var4").Where(builder.Eq{"id": 1}).QueryString()
  179. assert.NoError(t, err)
  180. assert.EqualValues(t, 1, len(records))
  181. assert.EqualValues(t, "1", records[0]["id"])
  182. if testEngine.Dialect().DBType() == schemas.POSTGRES || testEngine.Dialect().DBType() == schemas.MSSQL {
  183. assert.EqualValues(t, "false", records[0]["msg"])
  184. } else {
  185. assert.EqualValues(t, "0", records[0]["msg"])
  186. }
  187. }
  188. func TestQuerySliceStringNoParam(t *testing.T) {
  189. assert.NoError(t, prepareEngine())
  190. type GetVar6 struct {
  191. Id int64 `xorm:"autoincr pk"`
  192. Msg bool `xorm:"bit"`
  193. }
  194. assert.NoError(t, testEngine.Sync2(new(GetVar6)))
  195. var data = GetVar6{
  196. Msg: false,
  197. }
  198. _, err := testEngine.Insert(data)
  199. assert.NoError(t, err)
  200. records, err := testEngine.Table("get_var6").Limit(1).QuerySliceString()
  201. assert.NoError(t, err)
  202. assert.EqualValues(t, 1, len(records))
  203. assert.EqualValues(t, "1", records[0][0])
  204. if testEngine.Dialect().DBType() == schemas.POSTGRES || testEngine.Dialect().DBType() == schemas.MSSQL {
  205. assert.EqualValues(t, "false", records[0][1])
  206. } else {
  207. assert.EqualValues(t, "0", records[0][1])
  208. }
  209. records, err = testEngine.Table("get_var6").Where(builder.Eq{"id": 1}).QuerySliceString()
  210. assert.NoError(t, err)
  211. assert.EqualValues(t, 1, len(records))
  212. assert.EqualValues(t, "1", records[0][0])
  213. if testEngine.Dialect().DBType() == schemas.POSTGRES || testEngine.Dialect().DBType() == schemas.MSSQL {
  214. assert.EqualValues(t, "false", records[0][1])
  215. } else {
  216. assert.EqualValues(t, "0", records[0][1])
  217. }
  218. }
  219. func TestQueryInterfaceNoParam(t *testing.T) {
  220. assert.NoError(t, prepareEngine())
  221. type GetVar5 struct {
  222. Id int64 `xorm:"autoincr pk"`
  223. Msg bool `xorm:"bit"`
  224. }
  225. assert.NoError(t, testEngine.Sync2(new(GetVar5)))
  226. var data = GetVar5{
  227. Msg: false,
  228. }
  229. _, err := testEngine.Insert(data)
  230. assert.NoError(t, err)
  231. records, err := testEngine.Table("get_var5").Limit(1).QueryInterface()
  232. assert.NoError(t, err)
  233. assert.EqualValues(t, 1, len(records))
  234. assert.EqualValues(t, 1, toInt64(records[0]["id"]))
  235. assert.EqualValues(t, 0, toInt64(records[0]["msg"]))
  236. records, err = testEngine.Table("get_var5").Where(builder.Eq{"id": 1}).QueryInterface()
  237. assert.NoError(t, err)
  238. assert.EqualValues(t, 1, len(records))
  239. assert.EqualValues(t, 1, toInt64(records[0]["id"]))
  240. assert.EqualValues(t, 0, toInt64(records[0]["msg"]))
  241. }
  242. func TestQueryWithBuilder(t *testing.T) {
  243. assert.NoError(t, prepareEngine())
  244. type QueryWithBuilder struct {
  245. Id int64 `xorm:"autoincr pk"`
  246. Msg string `xorm:"varchar(255)"`
  247. Age int
  248. Money float32
  249. Created time.Time `xorm:"created"`
  250. }
  251. testEngine.ShowSQL(true)
  252. assert.NoError(t, testEngine.Sync2(new(QueryWithBuilder)))
  253. var q = QueryWithBuilder{
  254. Msg: "message",
  255. Age: 20,
  256. Money: 3000,
  257. }
  258. cnt, err := testEngine.Insert(&q)
  259. assert.NoError(t, err)
  260. assert.EqualValues(t, 1, cnt)
  261. assertResult := func(t *testing.T, results []map[string][]byte) {
  262. assert.EqualValues(t, 1, len(results))
  263. id, err := strconv.ParseInt(string(results[0]["id"]), 10, 64)
  264. assert.NoError(t, err)
  265. assert.EqualValues(t, 1, id)
  266. assert.Equal(t, "message", string(results[0]["msg"]))
  267. age, err := strconv.Atoi(string(results[0]["age"]))
  268. assert.NoError(t, err)
  269. assert.EqualValues(t, 20, age)
  270. money, err := strconv.ParseFloat(string(results[0]["money"]), 32)
  271. assert.NoError(t, err)
  272. assert.EqualValues(t, 3000, money)
  273. }
  274. results, err := testEngine.Query(builder.Select("*").From(testEngine.TableName("query_with_builder", true)))
  275. assert.NoError(t, err)
  276. assertResult(t, results)
  277. }
  278. func TestJoinWithSubQuery(t *testing.T) {
  279. assert.NoError(t, prepareEngine())
  280. type JoinWithSubQuery1 struct {
  281. Id int64 `xorm:"autoincr pk"`
  282. Msg string `xorm:"varchar(255)"`
  283. DepartId int64
  284. Money float32
  285. }
  286. type JoinWithSubQueryDepart struct {
  287. Id int64 `xorm:"autoincr pk"`
  288. Name string
  289. }
  290. testEngine.ShowSQL(true)
  291. assert.NoError(t, testEngine.Sync2(new(JoinWithSubQuery1), new(JoinWithSubQueryDepart)))
  292. var depart = JoinWithSubQueryDepart{
  293. Name: "depart1",
  294. }
  295. cnt, err := testEngine.Insert(&depart)
  296. assert.NoError(t, err)
  297. assert.EqualValues(t, 1, cnt)
  298. var q = JoinWithSubQuery1{
  299. Msg: "message",
  300. DepartId: depart.Id,
  301. Money: 3000,
  302. }
  303. cnt, err = testEngine.Insert(&q)
  304. assert.NoError(t, err)
  305. assert.EqualValues(t, 1, cnt)
  306. tbName := testEngine.Quote(testEngine.TableName("join_with_sub_query_depart", true))
  307. var querys []JoinWithSubQuery1
  308. err = testEngine.Join("INNER", builder.Select("id").From(tbName),
  309. "join_with_sub_query_depart.id = join_with_sub_query1.depart_id").Find(&querys)
  310. assert.NoError(t, err)
  311. assert.EqualValues(t, 1, len(querys))
  312. assert.EqualValues(t, q, querys[0])
  313. querys = make([]JoinWithSubQuery1, 0, 1)
  314. err = testEngine.Join("INNER", "(SELECT id FROM "+tbName+") join_with_sub_query_depart", "join_with_sub_query_depart.id = join_with_sub_query1.depart_id").
  315. Find(&querys)
  316. assert.NoError(t, err)
  317. assert.EqualValues(t, 1, len(querys))
  318. assert.EqualValues(t, q, querys[0])
  319. }