forked from umisama/go-sqlbuilder
-
Notifications
You must be signed in to change notification settings - Fork 2
/
select_test.go
155 lines (146 loc) · 3.8 KB
/
select_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
154
155
package sqlbuilder
import (
"reflect"
"testing"
)
func TestSelect(t *testing.T) {
table1 := NewTable(
"TABLE_A",
&TableOption{},
IntColumn("id", &ColumnOption{
PrimaryKey: true,
}),
IntColumn("test1", nil),
IntColumn("test2", nil),
)
table2 := NewTable(
"TABLE_B",
&TableOption{},
IntColumn("id", &ColumnOption{
PrimaryKey: true,
}),
)
tableJoined := table1.InnerJoin(table2, table1.C("test1").Eq(table2.C("id")))
acol_id := table1.C("id").As("tbl1id")
var cases = []statementTestCase{{
stmt: Select(table1).
Columns(table1.C("test1"), table1.C("test2")).
Where(
And(
table1.C("id").Eq(1),
table1.C("test1").Eq(2),
)).
Distinct().
OrderBy(false, table1.C("id")).
GroupBy(table1.C("id")).
Having(table1.C("id").Eq(1)).
Limit(10).
Offset(20),
query: `SELECT DISTINCT "TABLE_A"."test1", "TABLE_A"."test2" ` +
`FROM "TABLE_A" WHERE "TABLE_A"."id"=? AND "TABLE_A"."test1"=? ` +
`GROUP BY "TABLE_A"."id" HAVING "TABLE_A"."id"=? ORDER BY "TABLE_A"."id" ASC ` +
`LIMIT ? OFFSET ?;`,
args: []interface{}{int64(1), int64(2), int64(1), 10, 20},
errmsg: "",
}, {
stmt: Select(table1).
Columns(table1.C("test1"), table1.C("test2")),
query: `SELECT "TABLE_A"."test1", "TABLE_A"."test2" FROM "TABLE_A";`,
args: []interface{}{},
errmsg: "",
}, {
stmt: Select(table1).Columns(acol_id).
Where(acol_id.Eq(1)),
query: `SELECT "TABLE_A"."id" AS "tbl1id" FROM "TABLE_A" WHERE "tbl1id"=?;`,
args: []interface{}{int64(1)},
errmsg: "",
}, {
stmt: Select(table1).Columns(acol_id).
Where(acol_id.Eq(1)).
OrderBy(false, table1.C("test1")).
OrderBy(true, table1.C("test2")),
query: `SELECT "TABLE_A"."id" AS "tbl1id" FROM "TABLE_A" WHERE "tbl1id"=? ORDER BY "TABLE_A"."test1" ASC, "TABLE_A"."test2" DESC;`,
args: []interface{}{int64(1)},
errmsg: "",
}, {
stmt: Select(table1).Columns(Star),
query: `SELECT * FROM "TABLE_A";`,
args: []interface{}{},
errmsg: "",
}, {
stmt: Select(table1),
query: `SELECT * FROM "TABLE_A";`,
args: []interface{}{},
errmsg: "",
}, {
stmt: Select(tableJoined).
Columns(Star),
query: `SELECT * FROM "TABLE_A" INNER JOIN "TABLE_B" ON "TABLE_A"."test1"="TABLE_B"."id";`,
args: []interface{}{},
errmsg: "",
}, {
stmt: Select(nil).
Columns(table1.C("test1"), table1.C("test2")),
query: ``,
args: []interface{}{},
errmsg: "sqlbuilder: table is nil.",
}, {
stmt: Select(table1).
Columns(table1.C("test1"), table1.C("test2")).
Having(table1.C("id").Eq(1)),
query: ``,
args: []interface{}{},
errmsg: "sqlbuilder: GROUP BY clause is not found.",
}}
for num, c := range cases {
mes, args, ok := c.Run()
if !ok {
t.Errorf(mes+" (case no.%d)", append(args, num)...)
}
}
}
func TestSubquery(t *testing.T) {
table1 := NewTable(
"TABLE_A",
&TableOption{},
IntColumn("id", &ColumnOption{
PrimaryKey: true,
}),
IntColumn("test1", nil),
IntColumn("test2", nil),
)
subquery := Select(table1).Columns(table1.C("id")).ToSubquery("SQ1")
query, attrs, err := Select(subquery).
Columns(subquery.C("id")).
Where(subquery.C("id").Eq(1)).ToSql()
if `SELECT "SQ1"."id" FROM ( SELECT "TABLE_A"."id" FROM "TABLE_A" ) AS SQ1 WHERE "SQ1"."id"=?;` != query {
t.Errorf("failed \ngot %s", query)
}
if !reflect.DeepEqual([]interface{}{int64(1)}, attrs) {
t.Errorf("failed \ngot %#v", attrs)
}
if err != nil {
t.Error("failed \ngot %#v", err.Error())
}
}
func BenchmarkSelect(b *testing.B) {
table1 := NewTable(
"TABLE_A",
&TableOption{},
IntColumn("id", &ColumnOption{
PrimaryKey: true,
}),
IntColumn("test1", nil),
IntColumn("test2", nil),
)
for i := 0; i < b.N; i++ {
Select(table1).
Columns(table1.C("test1"), table1.C("test2")).
Where(
And(
table1.C("id").Eq(1),
table1.C("test1").Eq(2),
)).
ToSql()
}
}