-
Notifications
You must be signed in to change notification settings - Fork 0
/
comfy_driver.go
230 lines (188 loc) · 4.48 KB
/
comfy_driver.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package comfylite3
import (
"context"
"database/sql"
"database/sql/driver"
"fmt"
"io"
"strings"
)
type ComfyDriver struct {
comfy *ComfyDB
connStr string
}
func (cd *ComfyDriver) Open(name string) (driver.Conn, error) {
return &comfyConn{comfy: cd.comfy, connStr: cd.connStr}, nil
}
func (cd *ComfyDriver) Connect(ctx context.Context) (driver.Conn, error) {
return cd.Open("")
}
func (cd *ComfyDriver) Driver() driver.Driver {
return cd
}
type comfyConn struct {
comfy *ComfyDB
connStr string
}
func (cc *comfyConn) Prepare(query string) (driver.Stmt, error) {
return &comfyStmt{comfy: cc.comfy, query: query}, nil
}
func (cc *comfyConn) Close() error {
return nil
}
func (cc *comfyConn) Begin() (driver.Tx, error) {
return &comfyTx{comfy: cc.comfy}, nil
}
type comfyStmt struct {
comfy *ComfyDB
query string
}
func (cs *comfyStmt) Close() error {
return nil
}
func (cs *comfyStmt) NumInput() int {
return -1
}
func (cs *comfyStmt) Exec(args []driver.Value) (driver.Result, error) {
id := cs.comfy.New(func(db *sql.DB) (interface{}, error) {
return db.Exec(cs.query, convertValues(args)...)
})
result := <-cs.comfy.WaitForChn(id)
if err, ok := result.(error); ok {
return nil, err
}
return result.(sql.Result), nil
}
func (cs *comfyStmt) Query(args []driver.Value) (driver.Rows, error) {
id := cs.comfy.New(func(db *sql.DB) (interface{}, error) {
return db.Query(cs.query, convertValues(args)...)
})
result := <-cs.comfy.WaitForChn(id)
if err, ok := result.(error); ok {
return nil, err
}
return &comfyRows{rows: result.(*sql.Rows)}, nil
}
type comfyRows struct {
rows *sql.Rows
}
func (cr *comfyRows) Columns() []string {
cols, _ := cr.rows.Columns()
return cols
}
func (cr *comfyRows) Close() error {
return cr.rows.Close()
}
func (cr *comfyRows) Next(dest []driver.Value) error {
if !cr.rows.Next() {
return io.EOF
}
columns, err := cr.rows.Columns()
if err != nil {
return err
}
if len(dest) != len(columns) {
return fmt.Errorf("expected %d columns but got %d", len(dest), len(columns))
}
// Prepare a slice of pointers to empty interfaces to pass to rows.Scan
values := make([]interface{}, len(dest))
for i := range values {
values[i] = new(interface{})
}
if err := cr.rows.Scan(values...); err != nil {
return err
}
for i, v := range values {
val := *(v.(*interface{}))
dest[i] = driver.Value(val)
}
return nil
}
type comfyTx struct {
comfy *ComfyDB
}
func (ct *comfyTx) Commit() error {
return nil
}
func (ct *comfyTx) Rollback() error {
return nil
}
func convertValues(vals []driver.Value) []interface{} {
result := make([]interface{}, len(vals))
for i, v := range vals {
result[i] = v
}
return result
}
type OpenDBOptions struct {
options []string
withForeignKeys bool
}
type OpenDBOption func(*OpenDBOptions)
func WithForeignKeys() func(*OpenDBOptions) {
return func(o *OpenDBOptions) {
o.withForeignKeys = true
}
}
func WithOption(options string) func(*OpenDBOptions) {
return func(o *OpenDBOptions) {
o.options = append(o.options, options)
}
}
// OpenDB creates a new sql.DB instance using ComfyDB
func OpenDB(comfy *ComfyDB, opts ...OpenDBOption) *sql.DB {
connStr := comfy.conn
// If comfy.conn is empty, use the default connection string
if connStr == "" {
if comfy.memory {
connStr = "file::memory:"
} else {
connStr = fmt.Sprintf("file:%s", comfy.path)
}
}
// Parse existing options
existingOptions := make(map[string]bool)
if strings.Contains(connStr, "?") {
parts := strings.SplitN(connStr, "?", 2)
connStr = parts[0]
for _, opt := range strings.Split(parts[1], "&") {
key := strings.SplitN(opt, "=", 2)[0]
existingOptions[key] = true
}
}
cfg := OpenDBOptions{}
for _, opt := range opts {
opt(&cfg)
}
// Add new options
newOptions := []string{}
for _, opt := range cfg.options {
key := strings.SplitN(opt, "=", 2)[0]
if !existingOptions[key] {
newOptions = append(newOptions, opt)
existingOptions[key] = true
}
}
// Append new options to connection string
if len(newOptions) > 0 {
if strings.Contains(connStr, "?") {
connStr += "&"
} else {
connStr += "?"
}
connStr += strings.Join(newOptions, "&")
}
// fmt.Printf("Connection string: %s\n", connStr) // Debug print
db := sql.OpenDB(&ComfyDriver{
comfy: comfy,
connStr: connStr,
})
// Explicitly enable foreign keys
if cfg.withForeignKeys {
_, err := db.Exec("PRAGMA foreign_keys = ON;")
if err != nil {
fmt.Printf("Error setting foreign_keys pragma: %v\n", err)
}
}
return db
}