-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoki.go
200 lines (168 loc) · 4.69 KB
/
toki.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package toki
import (
"fmt"
"reflect"
"strings"
"sync"
)
// Builder represents the main query builder structure
type Builder struct {
parts []string
args []interface{}
argIndex int
pool *sync.Pool
table string
tx *Transaction
}
// New creates a new query builder
func New() *Builder {
return &Builder{
pool: &sync.Pool{
New: func() interface{} {
return &strings.Builder{}
},
},
}
}
// WithTransaction sets the transaction for the builder
func (b *Builder) WithTransaction(tx *Transaction) *Builder {
b.tx = tx
return b
}
// Select initializes a SELECT query
func (b *Builder) Select(columns ...string) *Builder {
b.parts = append(b.parts, fmt.Sprintf("SELECT %s", strings.Join(columns, ", ")))
return b
}
// From adds FROM clause
func (b *Builder) From(table string) *Builder {
b.table = table
b.parts = append(b.parts, fmt.Sprintf("FROM %s", b.table))
return b
}
// Where adds WHERE conditions
func (b *Builder) Where(condition string, args ...interface{}) *Builder {
if len(b.parts) > 0 && !strings.HasSuffix(b.parts[len(b.parts)-1], "WHERE") {
b.parts = append(b.parts, "WHERE")
}
b.parts = append(b.parts, b.convertPlaceholders(condition))
b.args = append(b.args, args...)
return b
}
// AndWhere adds AND condition
func (b *Builder) AndWhere(condition string, args ...interface{}) *Builder {
b.parts = append(b.parts, "AND", b.convertPlaceholders(condition))
b.args = append(b.args, args...)
return b
}
// OrWhere adds OR condition
func (b *Builder) OrWhere(condition string, args ...interface{}) *Builder {
b.parts = append(b.parts, "OR", b.convertPlaceholders(condition))
b.args = append(b.args, args...)
return b
}
// OrderBy adds ORDER BY clause
func (b *Builder) OrderBy(columns ...string) *Builder {
b.parts = append(b.parts, fmt.Sprintf("ORDER BY %s", strings.Join(columns, ", ")))
return b
}
// Update initializes an UPDATE query
func (b *Builder) Update(table string) *Builder {
b.parts = append(b.parts, fmt.Sprintf("UPDATE %s", table))
return b
}
// Set adds SET clause for UPDATE
func (b *Builder) Set(updates map[string]interface{}) *Builder {
sets := make([]string, 0, len(updates))
for col, val := range updates {
if expr, ok := val.(SQLExpression); ok {
sets = append(sets, fmt.Sprintf("%s = %s", col, expr.SQL()))
} else {
b.argIndex++
sets = append(sets, fmt.Sprintf("%s = $%d", col, b.argIndex))
b.args = append(b.args, val)
}
}
b.parts = append(b.parts, fmt.Sprintf("SET %s", strings.Join(sets, ", ")))
return b
}
// Insert initializes an INSERT query
func (b *Builder) Insert(table string, columns ...string) *Builder {
b.parts = append(b.parts, fmt.Sprintf("INSERT INTO %s (%s)", table, strings.Join(columns, ", ")))
return b
}
// Values adds VALUES clause for INSERT
func (b *Builder) Values(values ...interface{}) *Builder {
placeholders := make([]string, len(values))
for i := range values {
b.argIndex++
placeholders[i] = fmt.Sprintf("$%d", b.argIndex)
}
b.parts = append(b.parts, fmt.Sprintf("VALUES (%s)", strings.Join(placeholders, ", ")))
b.args = append(b.args, values...)
return b
}
// Delete initializes a DELETE query
func (b *Builder) Delete(table string) *Builder {
b.parts = append(b.parts, fmt.Sprintf("DELETE FROM %s", table))
return b
}
// DeleteFrom is an alias for Delete for more expressive API
func (b *Builder) DeleteFrom(table string) *Builder {
return b.Delete(table)
}
// Returning adds a RETURNING clause to the DELETE statement
func (b *Builder) Returning(columns ...string) *Builder {
if len(columns) > 0 {
b.parts = append(b.parts, "RETURNING", strings.Join(columns, ", "))
}
return b
}
// String builds the final query string
func (b *Builder) String() string {
sb := b.pool.Get().(*strings.Builder)
defer func() {
sb.Reset()
b.pool.Put(sb)
}()
for i, part := range b.parts {
if i > 0 {
sb.WriteByte(' ')
}
sb.WriteString(part)
}
return sb.String()
}
// Bind creates a struct binding for database columns
func (b *Builder) Bind(dest interface{}) map[string]interface{} {
val := reflect.ValueOf(dest)
if val.Kind() == reflect.Ptr {
val = val.Elem()
}
typ := val.Type()
result := make(map[string]interface{})
for i := 0; i < val.NumField(); i++ {
field := typ.Field(i)
tag := field.Tag.Get("db")
if tag != "" {
result[tag] = val.Field(i).Interface()
}
}
if b.table == "" {
b.table = strings.ToLower(typ.Name())
}
return result
}
// convertPlaceholders converts ? placeholders to $1, $2, etc.
func (b *Builder) convertPlaceholders(query string) string {
result := strings.Builder{}
for _, c := range query {
if c == '?' {
b.argIndex++
result.WriteString(fmt.Sprintf("$%d", b.argIndex))
} else {
result.WriteRune(c)
}
}
return result.String()
}