builder_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright 2018 Huan Du. All rights reserved.
  2. // Licensed under the MIT license that can be found in the LICENSE file.
  3. package sqlbuilder
  4. import (
  5. "database/sql"
  6. "fmt"
  7. "reflect"
  8. "testing"
  9. )
  10. func ExampleBuildf() {
  11. sb := NewSelectBuilder()
  12. sb.Select("id").From("user")
  13. explain := Buildf("EXPLAIN %v LEFT JOIN SELECT * FROM banned WHERE state IN (%v, %v)", sb, 1, 2)
  14. sql, args := explain.Build()
  15. fmt.Println(sql)
  16. fmt.Println(args)
  17. // Output:
  18. // EXPLAIN SELECT id FROM user LEFT JOIN SELECT * FROM banned WHERE state IN (?, ?)
  19. // [1 2]
  20. }
  21. func ExampleBuild() {
  22. sb := NewSelectBuilder()
  23. sb.Select("id").From("user").Where(sb.In("status", 1, 2))
  24. b := Build("EXPLAIN $? LEFT JOIN SELECT * FROM $? WHERE created_at > $? AND state IN (${states}) AND modified_at BETWEEN $2 AND $?",
  25. sb, Raw("banned"), 1514458225, 1514544625, Named("states", List([]int{3, 4, 5})))
  26. sql, args := b.Build()
  27. fmt.Println(sql)
  28. fmt.Println(args)
  29. // Output:
  30. // EXPLAIN SELECT id FROM user WHERE status IN (?, ?) LEFT JOIN SELECT * FROM banned WHERE created_at > ? AND state IN (?, ?, ?) AND modified_at BETWEEN ? AND ?
  31. // [1 2 1514458225 3 4 5 1514458225 1514544625]
  32. }
  33. func ExampleBuildNamed() {
  34. b := BuildNamed("SELECT * FROM ${table} WHERE status IN (${status}) AND name LIKE ${name} AND created_at > ${time} AND modified_at < ${time} + 86400",
  35. map[string]interface{}{
  36. "time": sql.Named("start", 1234567890),
  37. "status": List([]int{1, 2, 5}),
  38. "name": "Huan%",
  39. "table": Raw("user"),
  40. })
  41. sql, args := b.Build()
  42. fmt.Println(sql)
  43. fmt.Println(args)
  44. // Output:
  45. // SELECT * FROM user WHERE status IN (?, ?, ?) AND name LIKE ? AND created_at > @start AND modified_at < @start + 86400
  46. // [1 2 5 Huan% {{} start 1234567890}]
  47. }
  48. func ExampleWithFlavor() {
  49. sql, args := WithFlavor(Buildf("SELECT * FROM foo WHERE id = %v", 1234), PostgreSQL).Build()
  50. fmt.Println(sql)
  51. fmt.Println(args)
  52. // Explicitly use MySQL as the flavor.
  53. sql, args = WithFlavor(Buildf("SELECT * FROM foo WHERE id = %v", 1234), PostgreSQL).BuildWithFlavor(MySQL)
  54. fmt.Println(sql)
  55. fmt.Println(args)
  56. // Output:
  57. // SELECT * FROM foo WHERE id = $1
  58. // [1234]
  59. // SELECT * FROM foo WHERE id = ?
  60. // [1234]
  61. }
  62. func TestBuildWithPostgreSQL(t *testing.T) {
  63. sb1 := PostgreSQL.NewSelectBuilder()
  64. sb1.Select("col1", "col2").From("t1").Where(sb1.E("id", 1234), sb1.G("level", 2))
  65. sb2 := PostgreSQL.NewSelectBuilder()
  66. sb2.Select("col3", "col4").From("t2").Where(sb2.E("id", 4567), sb2.LE("level", 5))
  67. // Use DefaultFlavor (MySQL) instead of PostgreSQL.
  68. sql, args := Build("SELECT $1 AS col5 LEFT JOIN $0 LEFT JOIN $2", sb1, 7890, sb2).Build()
  69. if expected := "SELECT ? AS col5 LEFT JOIN SELECT col1, col2 FROM t1 WHERE id = ? AND level > ? LEFT JOIN SELECT col3, col4 FROM t2 WHERE id = ? AND level <= ?"; sql != expected {
  70. t.Fatalf("invalid sql. [expected:%v] [actual:%v]", expected, sql)
  71. }
  72. if expected := []interface{}{7890, 1234, 2, 4567, 5}; !reflect.DeepEqual(args, expected) {
  73. t.Fatalf("invalid args. [expected:%v] [actual:%v]", expected, args)
  74. }
  75. old := DefaultFlavor
  76. DefaultFlavor = PostgreSQL
  77. defer func() {
  78. DefaultFlavor = old
  79. }()
  80. sql, args = Build("SELECT $1 AS col5 LEFT JOIN $0 LEFT JOIN $2", sb1, 7890, sb2).Build()
  81. if expected := "SELECT $1 AS col5 LEFT JOIN SELECT col1, col2 FROM t1 WHERE id = $2 AND level > $3 LEFT JOIN SELECT col3, col4 FROM t2 WHERE id = $4 AND level <= $5"; sql != expected {
  82. t.Fatalf("invalid sql. [expected:%v] [actual:%v]", expected, sql)
  83. }
  84. if expected := []interface{}{7890, 1234, 2, 4567, 5}; !reflect.DeepEqual(args, expected) {
  85. t.Fatalf("invalid args. [expected:%v] [actual:%v]", expected, args)
  86. }
  87. }