-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintrospect.go
More file actions
43 lines (37 loc) · 1.08 KB
/
Copy pathintrospect.go
File metadata and controls
43 lines (37 loc) · 1.08 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
package sqlite
import (
"github.com/tinywasm/orm"
)
func tableColumns(q interface {
Query(string, ...any) (orm.Rows, error)
}, table string) ([]string, error) {
// PRAGMA table_info does not support parameter binding.
// Table name comes from ModelName(), which is usually trusted,
// but we could add basic validation if needed.
rows, err := q.Query("PRAGMA table_info(" + table + ")")
if err != nil {
return nil, err
}
defer rows.Close()
var cols []string
for rows.Next() {
var cid int
var name, ctype string
var notnull, pk int
var dflt any
if err := rows.Scan(&cid, &name, &ctype, ¬null, &dflt, &pk); err != nil {
return nil, err
}
cols = append(cols, name)
}
return cols, rows.Err()
}
func (e *sqliteExecutor) TableColumns(table string) ([]string, error) {
return tableColumns(e, table)
}
func (e *sqliteTxExecutor) TableColumns(table string) ([]string, error) {
return tableColumns(e, table)
}
// Ensure both executors implement TableIntrospector
var _ orm.TableIntrospector = (*sqliteExecutor)(nil)
var _ orm.TableIntrospector = (*sqliteTxExecutor)(nil)