-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
107 lines (87 loc) · 2.21 KB
/
main.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
package msqb // import "git.ultraware.nl/NiseVoid/qb/driver/msqb"
import (
"database/sql"
"strconv"
"strings"
"git.ultraware.nl/NiseVoid/qb"
"git.ultraware.nl/NiseVoid/qb/driver/msqb/msqf"
"git.ultraware.nl/NiseVoid/qb/qbdb"
"git.ultraware.nl/NiseVoid/qb/qf"
)
// Driver implements MSSQL-specific features
type Driver struct{}
// New returns the driver
func New(db *sql.DB) *qbdb.DB {
return qbdb.New(Driver{}, db)
}
// ValueString returns a the SQL for a parameter value
func (d Driver) ValueString(i int) string {
return `@p` + strconv.Itoa(i)
}
// BoolString formats a boolean in a format supported by PostgreSQL
func (d Driver) BoolString(v bool) string {
if v {
return `1`
}
return `0`
}
// UpsertSQL implements qb.Driver
func (d Driver) UpsertSQL(t *qb.Table, _ []qb.Field, q qb.Query) (string, []interface{}) {
panic(`mssql does not support upsert`)
}
// Limit implements qb.Driver
func (d Driver) Limit(sql qb.SQL, limit int) {
s := sql.String()
sql.Rewrite(`SELECT TOP ` + strconv.Itoa(limit) + s[6:])
}
// Returning implements qb.Driver
func (d Driver) Returning(q qb.Query, f []qb.Field) (string, []interface{}) {
b := qb.NewSQLBuilder(d)
sql, v := q.SQL(b)
t, insertBefore := `INSERTED`, `WHERE`
switch strings.SplitN(sql, ` `, 2)[0] {
case `DELETE`:
t = `DELETED`
case `INSERT`:
insertBefore = `VALUES`
}
line := ``
for k, field := range f {
if k > 0 {
line += `, `
}
line += t + `.` + field.QueryString(b.Context)
}
index := strings.Index(sql, insertBefore)
if index < 0 {
sql = sql + `OUTPUT ` + line + qb.NEWLINE
} else {
sql = sql[:index] + `OUTPUT ` + line + qb.NEWLINE + sql[index:]
}
return sql, v
}
var types = map[qb.DataType]string{
qb.Int: `int`,
qb.String: `text`,
qb.Bool: `bit`,
qb.Float: `float`,
qb.Date: `date`,
qb.Time: `datetime`,
}
// TypeName implements qb.Driver
func (d Driver) TypeName(t qb.DataType) string {
if s, ok := types[t]; ok {
return s
}
panic(`Unknown type`)
}
var override = qb.OverrideMap{}
func init() {
override.Add(qf.Concat, msqf.Concat)
override.Add(qf.Extract, msqf.DatePart)
override.Add(qf.Now, msqf.GetDate)
}
// Override implements qb.Driver
func (d Driver) Override() qb.OverrideMap {
return override
}