Skip to content

Commit b93a377

Browse files
committed
add sqlbuilder
1 parent 8ddfb81 commit b93a377

4 files changed

Lines changed: 56 additions & 15 deletions

File tree

api.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,16 @@ func QueryPageMapContext(queryer Queryer, ctx context.Context, querySql string,
148148
return queryPageMap(queryer, ctx, querySql, args...)
149149
}
150150

151-
func StmtWhereIn(drvName string, paramStartIdx, paramsLen int) string {
152-
return stmtWhereIn(drvName, paramStartIdx, paramsLen)
151+
// Extend stmt for the where in
152+
//
153+
// Example for the first input:
154+
// fmt.Sprintf("select * from table_name where in (%s)", qsql.StmtWhereIn(0,len(args))
155+
// Or
156+
// fmt.Sprintf("select * from table_name where in (%s)", qsql.StmtWhereIn(0,len(args), qsql.DRV_NAME_MYSQL)
157+
//
158+
// Example for the second input:
159+
// fmt.Sprintf("select * from table_name where id=? in (%s)", qsql.StmtWhereIn(1,len(args))
160+
161+
func StmtIn(paramStartIdx, paramsLen int, drvName ...string) string {
162+
return stmtIn(paramStartIdx, paramsLen, drvName...)
153163
}

db.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,10 @@ func (db *DB) QueryPageMap(querySql string, args ...interface{}) (titles []strin
107107
func (db *DB) QueryPageMapContext(ctx context.Context, querySql string, args ...interface{}) (titles []string, result []map[string]interface{}, err error) {
108108
return queryPageMap(db, ctx, querySql, args...)
109109
}
110-
func (db *DB) StmtWhereIn(paramStartIdx, paramsLen int) string {
111-
return stmtWhereIn(db.DriverName(), paramStartIdx, paramsLen)
110+
111+
// Return "?,?,?,?..." for default, or "@p1,@p2,@p3..." for mssql, or ":1,:2,:3..." for pgsql when paramStartIdx is 0.
112+
func (db *DB) StmtIn(paramStartIdx, paramsLen int) string {
113+
return stmtIn(paramStartIdx, paramsLen, db.DriverName())
112114
}
113115

114116
// A lazy function to commit the *sql.Tx

sqlbuilder.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package qsql
2+
3+
type SqlBuilder struct {
4+
table []string
5+
selectStr []string
6+
whereAndStr []string
7+
whereParams []interface{}
8+
whereOrStr []string
9+
whereOrParams []interface{}
10+
groupByStr []string
11+
havingStr []string
12+
havingParams []interface{}
13+
havingOrStr []string
14+
havingOrParams []interface{}
15+
orderByStr []string
16+
offset int
17+
limit int
18+
}
19+
20+
func NewSqlBuilder(table string) *SqlBuilder {
21+
return &SqlBuilder{
22+
table: []string{table},
23+
}
24+
}
25+
26+
func (b *SqlBuilder) Joins(table string) *SqlBuilder {
27+
b.table = append(b.table, table)
28+
return b
29+
}
30+
31+
func (b *SqlBuilder) Where(cond string, args ...interface{}) *SqlBuilder {
32+
b.whereAndStr = append(b.whereAndStr, cond)
33+
b.whereParams = append(b.whereParams, args...)
34+
return b
35+
}

where.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,11 @@ import (
55
"reflect"
66
)
77

8-
// Extend the where in stmt
9-
//
10-
// Example for the first input:
11-
// fmt.Sprintf("select * from table_name where in (%s)", qsql.StmtWhereIn(0,len(args))
12-
// Or
13-
// fmt.Sprintf("select * from table_name where in (%s)", qsql.StmtWhereIn(0,len(args), qsql.DRV_NAME_MYSQL)
14-
//
15-
// Example for the second input:
16-
// fmt.Sprintf("select * from table_name where id=? in (%s)", qsql.StmtWhereIn(1,len(args))
17-
//
18-
func stmtWhereIn(driverName string, paramIdx, paramsLen int) string {
8+
func stmtIn(paramIdx, paramsLen int, driverNames ...string) string {
9+
driverName := ""
10+
if len(driverNames) > 0 {
11+
driverName = driverNames[0]
12+
}
1913
drvName := getDrvName(nil, driverName)
2014
switch drvName {
2115
case DRV_NAME_ORACLE, _DRV_NAME_OCI8:

0 commit comments

Comments
 (0)