-
Notifications
You must be signed in to change notification settings - Fork 123
/
builder_test.go
153 lines (114 loc) · 4.64 KB
/
builder_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright 2018 Huan Du. All rights reserved.
// Licensed under the MIT license that can be found in the LICENSE file.
package sqlbuilder
import (
"database/sql"
"fmt"
"testing"
"github.com/huandu/go-assert"
)
func ExampleBuildf() {
sb := NewSelectBuilder()
sb.Select("id").From("user")
explain := Buildf("EXPLAIN %v LEFT JOIN SELECT * FROM banned WHERE state IN (%v, %v)", sb, 1, 2)
s, args := explain.Build()
fmt.Println(s)
fmt.Println(args)
// Output:
// EXPLAIN SELECT id FROM user LEFT JOIN SELECT * FROM banned WHERE state IN (?, ?)
// [1 2]
}
func ExampleBuild() {
sb := NewSelectBuilder()
sb.Select("id").From("user").Where(sb.In("status", 1, 2))
b := Build("EXPLAIN $? LEFT JOIN SELECT * FROM $? WHERE created_at > $? AND state IN (${states}) AND modified_at BETWEEN $2 AND $?",
sb, Raw("banned"), 1514458225, 1514544625, Named("states", List([]int{3, 4, 5})))
s, args := b.Build()
fmt.Println(s)
fmt.Println(args)
// Output:
// EXPLAIN SELECT id FROM user WHERE status IN (?, ?) LEFT JOIN SELECT * FROM banned WHERE created_at > ? AND state IN (?, ?, ?) AND modified_at BETWEEN ? AND ?
// [1 2 1514458225 3 4 5 1514458225 1514544625]
}
func ExampleBuildNamed() {
b := BuildNamed("SELECT * FROM ${table} WHERE status IN (${status}) AND name LIKE ${name} AND created_at > ${time} AND modified_at < ${time} + 86400",
map[string]interface{}{
"time": sql.Named("start", 1234567890),
"status": List([]int{1, 2, 5}),
"name": "Huan%",
"table": Raw("user"),
})
s, args := b.Build()
fmt.Println(s)
fmt.Println(args)
// Output:
// SELECT * FROM user WHERE status IN (?, ?, ?) AND name LIKE ? AND created_at > @start AND modified_at < @start + 86400
// [1 2 5 Huan% {{} start 1234567890}]
}
func ExampleWithFlavor() {
sql, args := WithFlavor(Buildf("SELECT * FROM foo WHERE id = %v", 1234), PostgreSQL).Build()
fmt.Println(sql)
fmt.Println(args)
// Explicitly use MySQL as the flavor.
sql, args = WithFlavor(Buildf("SELECT * FROM foo WHERE id = %v", 1234), PostgreSQL).BuildWithFlavor(MySQL)
fmt.Println(sql)
fmt.Println(args)
// Explicitly use MySQL as the informix.
sql, args = WithFlavor(Buildf("SELECT * FROM foo WHERE id = %v", 1234), Informix).Build()
fmt.Println(sql)
fmt.Println(args)
// Output:
// SELECT * FROM foo WHERE id = $1
// [1234]
// SELECT * FROM foo WHERE id = ?
// [1234]
// SELECT * FROM foo WHERE id = ?
// [1234]
}
func TestBuildWithPostgreSQL(t *testing.T) {
a := assert.New(t)
sb1 := PostgreSQL.NewSelectBuilder()
sb1.Select("col1", "col2").From("t1").Where(sb1.E("id", 1234), sb1.G("level", 2))
sb2 := PostgreSQL.NewSelectBuilder()
sb2.Select("col3", "col4").From("t2").Where(sb2.E("id", 4567), sb2.LE("level", 5))
// Use DefaultFlavor (MySQL) instead of PostgreSQL.
sql, args := Build("SELECT $1 AS col5 LEFT JOIN $0 LEFT JOIN $2", sb1, 7890, sb2).Build()
a.Equal(sql, "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 <= ?")
a.Equal(args, []interface{}{7890, 1234, 2, 4567, 5})
old := DefaultFlavor
DefaultFlavor = PostgreSQL
defer func() {
DefaultFlavor = old
}()
sql, args = Build("SELECT $1 AS col5 LEFT JOIN $0 LEFT JOIN $2", sb1, 7890, sb2).Build()
a.Equal(sql, "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")
a.Equal(args, []interface{}{7890, 1234, 2, 4567, 5})
}
func TestBuildWithCQL(t *testing.T) {
a := assert.New(t)
ib1 := CQL.NewInsertBuilder()
ib1.InsertInto("t1").Cols("col1", "col2").Values(1, 2)
ib2 := CQL.NewInsertBuilder()
ib2.InsertInto("t2").Cols("col3", "col4").Values(3, 4)
old := DefaultFlavor
DefaultFlavor = CQL
defer func() {
DefaultFlavor = old
}()
sql, args := Build("BEGIN BATCH USING TIMESTAMP $0 $1; $2; APPLY BATCH;", 1481124356754405, ib1, ib2).Build()
a.Equal(sql, "BEGIN BATCH USING TIMESTAMP ? INSERT INTO t1 (col1, col2) VALUES (?, ?); INSERT INTO t2 (col3, col4) VALUES (?, ?); APPLY BATCH;")
a.Equal(args, []interface{}{1481124356754405, 1, 2, 3, 4})
}
func TestBuilderGetFlavor(t *testing.T) {
a := assert.New(t)
defaultBuilder := Build("SELECT * FROM foo WHERE id = $0", 1234)
a.Equal(DefaultFlavor, defaultBuilder.Flavor())
buildfBuilder := Buildf("SELECT * FROM foo WHERE id = %v", 1234)
a.Equal(DefaultFlavor, buildfBuilder.Flavor())
namedBuilder := Buildf("SELECT * FROM ${table} WHERE id = 1234", map[string]interface{}{
"table": "foo",
})
a.Equal(DefaultFlavor, namedBuilder.Flavor())
flavoredBuilder := WithFlavor(Build("SELECT * FROM foo WHERE id = $0", 1234), PostgreSQL)
a.Equal(PostgreSQL, flavoredBuilder.Flavor())
}