Skip to content
This repository was archived by the owner on Jun 14, 2019. It is now read-only.

Commit c923dd4

Browse files
BetaCat0lunny
authored andcommitted
Feature limit & benchmark tests & executable test moudle (#26)
* builder feature: union * complete README.md * UNION support *Builder only(it'll be better to reuse other components to avoid unnecessary problems) * add file head comment * modify file head comments * Uppercase: union -> UNION * enhance the function builder.From: allow query from a sub one * update related tests & update README.md * add one case in TestBuilder_From for simple from testing * fix bug & add test case * feature-limit & some tests add nested flag in builder that allow empty table name * Add file head comments Adjust references * Adjust references * Fix issue, adjust union function (will pass db dialect from base to its unions) * Fix unintended consequence in selectWriteTo * Update README.md * Fix issues & optimize codes * more test cases * optimize codes & update test cases * optimize codes & add benchmark test for limit query * update benchmark test for limit query * update benchmark test for limit query * Add benchmark test for basic queries & update some test cases * update benchmark test * update From(take out NestedFlag) & optimize Limit(use ROW_NUMBER() supported by mssql 2005 or later) & update test cases * add file head comments * update oracle limit * limit has been completed in the main & reconstruct feature UNION & update benchmark tests * remove redundant codes * add sql fiddler & test data sql scripts * update ci config * merge sql_fidder -> sql_test * rename func
1 parent df941ac commit c923dd4

17 files changed

+1396
-45
lines changed

.circleci/config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ jobs:
2424
# specify any bash command here prefixed with `run: `
2525
- run: go get -u github.com/golang/lint/golint
2626
- run: go get -u github.com/stretchr/testify/assert
27+
- run: go get -u github.com/go-xorm/sqlfiddle
2728
- run: golint ./...
2829
- run: go test -v -race -coverprofile=coverage.txt -covermode=atomic
2930
- run: bash <(curl -s https://codecov.io/bash)

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ sql, args, err := Select("sub.id").From("sub", Select("c").From("table1").Where(
2929
sql, args, err = Select("sub.id").From("sub",
3030
Select("id").From("table1").Where(Eq{"a": 1}).
3131
Union("all", Select("id").From("table1").Where(Eq{"a": 2}))).Where(Eq{"b": 1}).ToSQL()
32+
// With order by
33+
sql, args, err = Select("a", "b", "c").From("table1").Where(Eq{"f1": "v1", "f2": "v2"}).
34+
OrderBy("a ASC").ToSQL()
35+
// With limit.
36+
// Be careful! You should set up specific dialect for builder before performing a query with LIMIT
37+
sql, args, err = Dialect(MYSQL).Select("a", "b", "c").From("table1").OrderBy("a ASC").
38+
Limit(5, 10).ToSQL()
3239
```
3340

3441
# Update

builder.go

Lines changed: 53 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ const (
1515
unionType // union
1616
)
1717

18+
const (
19+
POSTGRES = "postgres"
20+
SQLITE = "sqlite3"
21+
MYSQL = "mysql"
22+
MSSQL = "mssql"
23+
ORACLE = "oracle"
24+
)
25+
1826
type join struct {
1927
joinType string
2028
joinTable string
@@ -26,20 +34,33 @@ type union struct {
2634
builder *Builder
2735
}
2836

37+
type limit struct {
38+
limitN int
39+
offset int
40+
}
41+
2942
// Builder describes a SQL statement
3043
type Builder struct {
3144
optype
32-
tableName string
33-
subQuery *Builder
34-
cond Cond
35-
selects []string
36-
joins []join
37-
unions []union
38-
inserts Eq
39-
updates []Eq
40-
orderBy string
41-
groupBy string
42-
having string
45+
dialect string
46+
tableName string
47+
subQuery *Builder
48+
cond Cond
49+
selects []string
50+
joins []join
51+
unions []union
52+
limitation *limit
53+
inserts Eq
54+
updates []Eq
55+
orderBy string
56+
groupBy string
57+
having string
58+
}
59+
60+
// Dialect sets the db dialect of Builder.
61+
func Dialect(dialect string) *Builder {
62+
builder := &Builder{cond: NewCond(), dialect: dialect}
63+
return builder
4364
}
4465

4566
// Where sets where SQL
@@ -88,23 +109,41 @@ func (b *Builder) Union(unionTp string, unionCond *Builder) *Builder {
88109
if b.optype != unionType {
89110
builder = &Builder{cond: NewCond()}
90111
builder.optype = unionType
112+
builder.dialect = b.dialect
113+
builder.selects = b.selects
91114

92115
currentUnions := b.unions
93116
// erase sub unions (actually append to new Builder.unions)
94117
b.unions = nil
95118

119+
for e := range currentUnions {
120+
currentUnions[e].builder.dialect = b.dialect
121+
}
122+
96123
builder.unions = append(append(builder.unions, union{"", b}), currentUnions...)
97124
} else {
98125
builder = b
99126
}
100127

101128
if unionCond != nil {
129+
unionCond.dialect = builder.dialect
102130
builder.unions = append(builder.unions, union{unionTp, unionCond})
103131
}
104132

105133
return builder
106134
}
107135

136+
// Limit sets limitN condition
137+
func (b *Builder) Limit(limitN int, offset ...int) *Builder {
138+
b.limitation = &limit{limitN: limitN}
139+
140+
if len(offset) > 0 {
141+
b.limitation.offset = offset[0]
142+
}
143+
144+
return b
145+
}
146+
108147
// InnerJoin sets inner join
109148
func (b *Builder) InnerJoin(joinTable string, joinCond interface{}) *Builder {
110149
return b.Join("INNER", joinTable, joinCond)
@@ -205,12 +244,12 @@ func (b *Builder) ToSQL() (string, []interface{}, error) {
205244
return w.writer.String(), w.args, nil
206245
}
207246

208-
// ToBindedSQL
209-
func (b *Builder) ToBindedSQL() (string, error) {
247+
// ToBoundSQL
248+
func (b *Builder) ToBoundSQL() (string, error) {
210249
w := NewWriter()
211250
if err := b.WriteTo(w); err != nil {
212251
return "", err
213252
}
214253

215-
return ConvertToBindedSQL(w.writer.String(), w.args)
254+
return ConvertToBoundSQL(w.writer.String(), w.args)
216255
}

0 commit comments

Comments
 (0)