-
Notifications
You must be signed in to change notification settings - Fork 124
/
toSql.go
101 lines (88 loc) · 3.22 KB
/
toSql.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
package gorose
import (
"errors"
"fmt"
"github.com/gohouse/gorose/v3/builder"
"github.com/gohouse/gorose/v3/parser"
"reflect"
)
func (db *Database) ToSqlSelect() (sql4prepare string, binds []any) {
return db.Driver.ToSqlSelect(db.Context)
}
func (db *Database) ToSqlTable() (sql4prepare string, values []any, err error) {
return db.Driver.ToSqlTable(db.Context)
}
func (db *Database) ToSqlJoin() (sql4prepare string, binds []any, err error) {
return db.Driver.ToSqlJoin(db.Context)
}
func (db *Database) ToSqlWhere() (sql4prepare string, values []any, err error) {
return db.Driver.ToSqlWhere(db.Context)
}
func (db *Database) ToSqlOrderBy() (sql4prepare string) {
return db.Driver.ToSqlOrderBy(db.Context)
}
func (db *Database) ToSqlLimitOffset() (sqlSegment string, binds []any) {
return db.Driver.ToSqlLimitOffset(db.Context)
}
func (db *Database) ToSql() (sql4prepare string, values []any, err error) {
return db.Driver.ToSql(db.Context)
}
func (db *Database) ToSqlExists(bind ...any) (sql4prepare string, values []any, err error) {
if len(bind) > 0 {
sql4prepare, values, err = db.ToSqlTo(bind[0])
} else {
sql4prepare, values, err = db.Driver.ToSql(db.Context)
}
if err != nil {
return
}
sql4prepare = fmt.Sprintf("SELECT EXISTS (%s) AS exist", sql4prepare)
return
}
func (db *Database) ToSqlAggregate(function, column string) (sql4prepare string, values []any, err error) {
var ctx = *db.Context
ctx.SelectClause.Columns = append(ctx.SelectClause.Columns, builder.Column{
Name: fmt.Sprintf("%s(%s)", function, column),
Alias: function,
IsRaw: true,
Binds: []any{},
})
return db.Driver.ToSql(&ctx)
}
func (db *Database) ToSqlTo(obj any, mustColumn ...string) (sql4prepare string, binds []any, err error) {
rfv := reflect.Indirect(reflect.ValueOf(obj))
columns, fieldStruct, _ := parser.StructsParse(obj)
switch rfv.Kind() {
case reflect.Struct:
var data = make(map[string]any)
data, err = parser.StructDataToMap(rfv, columns, fieldStruct, mustColumn...)
if err != nil {
return
}
sql4prepare, binds, err = db.Table(obj).Select(columns...).Where(data).Limit(1).ToSql()
case reflect.Slice:
if rfv.Type().Elem().Kind() == reflect.Struct {
sql4prepare, binds, err = db.Table(obj).Select(columns...).ToSql()
}
default:
err = errors.New("obj must be struct(slice) or map(slice)")
}
return
}
func (db *Database) ToSqlInsert(obj any, args ...builder.TypeToSqlInsertCase) (sqlSegment string, binds []any, err error) {
return db.Driver.ToSqlInsert(db.Context, obj, args...)
}
func (db *Database) ToSqlDelete(obj any, mustColumn ...string) (sqlSegment string, binds []any, err error) {
return db.Driver.ToSqlDelete(db.Context, obj, mustColumn...)
}
func (db *Database) ToSqlUpdate(obj any, mustColumn ...string) (sqlSegment string, binds []any, err error) {
return db.Driver.ToSqlUpdate(db.Context, builder.TypeToSqlUpdateCase{BindOrData: obj, MustColumn: mustColumn})
}
// ToSqlIncDec
//
// symbol: +/-
// data: {count: 2} => count = count + 2
func (db *Database) ToSqlIncDec(symbol string, data map[string]any) (sql4prepare string, values []any, err error) {
//return db.Driver.ToSqlIncDec(db.Context, symbol, data)
return db.Driver.ToSqlUpdate(db.Context, builder.TypeToSqlIncDecCase{Symbol: symbol, Data: data})
}