-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.go
More file actions
171 lines (157 loc) · 4.4 KB
/
functions.go
File metadata and controls
171 lines (157 loc) · 4.4 KB
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
package d1sql
import (
"database/sql"
"database/sql/driver"
"regexp"
"strings"
"github.com/cloudflare/cloudflare-go/v6"
"github.com/cloudflare/cloudflare-go/v6/d1"
)
// MetadataFunction represents a supported metadata function
type MetadataFunction struct {
Pattern *regexp.Regexp
Handler func(*Conn) (driver.Rows, error)
}
var (
// Supported metadata functions
metadataFunctions = []MetadataFunction{
{
Pattern: regexp.MustCompile(`(?i)^\s*LIST\s+DATABASES\s*;?\s*$`),
Handler: handleListDatabases,
},
{
Pattern: regexp.MustCompile(`(?i)^\s*SELECT\s+current_database\s*\(\s*\)\s*;?\s*$`),
Handler: handleCurrentDatabase,
},
{
Pattern: regexp.MustCompile(`(?i)^\s*SELECT\s+database\s*\(\s*\)\s*;?\s*$`),
Handler: handleCurrentDatabase,
},
{
Pattern: regexp.MustCompile(`(?i)^\s*SELECT\s+current_user\s*\(\s*\)\s*;?\s*$`),
Handler: handleCurrentUser,
},
{
Pattern: regexp.MustCompile(`(?i)^\s*SELECT\s+user\s*\(\s*\)\s*;?\s*$`),
Handler: handleCurrentUser,
},
{
Pattern: regexp.MustCompile(`(?i)^\s*SELECT\s+version\s*\(\s*\)\s*;?\s*$`),
Handler: handleVersion,
},
{
Pattern: regexp.MustCompile(`(?i)^\s*SELECT\s+connection_id\s*\(\s*\)\s*;?\s*$`),
Handler: handleConnectionID,
},
}
)
// tryMetadataFunction attempts to execute a metadata function
// Returns (rows, true) if it's a metadata function, (nil, false) otherwise
func tryMetadataFunction(conn *Conn, query string) (driver.Rows, bool) {
query = strings.TrimSpace(query)
for _, fn := range metadataFunctions {
if fn.Pattern.MatchString(query) {
rows, err := fn.Handler(conn)
if err != nil {
// If handler fails, return empty result but still indicate it was handled
return &Rows{
columns: []string{"error"},
rows: [][]interface{}{},
index: -1,
}, true
}
return rows, true
}
}
return nil, false
}
// handleListDatabases returns the list of databases
func handleListDatabases(conn *Conn) (driver.Rows, error) {
// Check if client is initialized (may be nil in tests)
if conn.client == nil {
return &Rows{
columns: []string{"name", "uuid", "version"},
rows: [][]any{},
index: -1,
}, nil
}
databases, err := conn.client.D1.Database.List(conn.ctx, d1.DatabaseListParams{
AccountID: cloudflare.F(conn.config.AccountID),
})
if err != nil {
return nil, err
}
rows := make([][]any, len(databases.Result))
for i := range databases.Result {
database := databases.Result[i]
rows[i] = []any{database.Name, database.UUID, database.Version}
}
return &Rows{
columns: []string{"name", "uuid", "version"},
rows: rows,
index: -1,
}, nil
}
// handleCurrentDatabase returns the current database ID
func handleCurrentDatabase(conn *Conn) (driver.Rows, error) {
// Check if client is initialized (may be nil in tests)
if conn.client == nil {
// Fallback: return database ID when client is not available
return &Rows{
columns: []string{"current_database()"},
rows: [][]any{{conn.config.DatabaseID}},
index: -1,
}, nil
}
// Fetch actual database name from API
databases, err := conn.client.D1.Database.List(conn.ctx, d1.DatabaseListParams{
AccountID: cloudflare.F(conn.config.AccountID),
})
if err != nil {
return nil, err
}
for i := range databases.Result {
database := databases.Result[i]
if database.UUID == conn.config.DatabaseID {
return &Rows{
columns: []string{"current_database()"},
rows: [][]any{{database.Name}},
index: -1,
}, nil
}
}
return nil, sql.ErrNoRows
}
// handleCurrentUser returns the current account ID
func handleCurrentUser(conn *Conn) (driver.Rows, error) {
return &Rows{
columns: []string{"current_user()"},
rows: [][]interface{}{
{conn.config.AccountID},
},
index: -1,
}, nil
}
// handleVersion returns the driver version and D1 info
func handleVersion(conn *Conn) (driver.Rows, error) {
version := "D1 Go SQL Driver v0.1.0 (Cloudflare D1 - SQLite compatible)"
return &Rows{
columns: []string{"version()"},
rows: [][]interface{}{
{version},
},
index: -1,
}, nil
}
// handleConnectionID returns a pseudo connection ID based on database and account
func handleConnectionID(conn *Conn) (driver.Rows, error) {
// Generate a deterministic connection ID from account and database
connID := conn.config.AccountID + ":" + conn.config.DatabaseID
return &Rows{
columns: []string{"connection_id()"},
rows: [][]interface{}{
{connID},
},
index: -1,
}, nil
}