-
Notifications
You must be signed in to change notification settings - Fork 1
/
entry.go
50 lines (47 loc) · 1.14 KB
/
entry.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
package sysql
import (
"database/sql"
"database/sql/driver"
"github.com/dean2021/go-sqlite3"
"github.com/dean2021/sysql/extend/functions"
_ "github.com/dean2021/sysql/extend/tables"
"github.com/dean2021/sysql/table"
)
const DriverName = "SQLITE3_SYSQL_EXTENSIONS"
func Initialize() {
sql.Register(DriverName, &sqlite3.SQLiteDriver{
ConnectHook: func(conn *sqlite3.SQLiteConn) error {
for name, function := range functions.Functions {
err := conn.RegisterFunc(name, function, true)
if err != nil {
return err
}
}
tables := table.GetAll()
for name, tab := range tables {
module := &Module{
TablePlugin: tab,
}
vTab := &VirtualTable{
TablePlugin: tab,
}
vTab.Cursor = &Cursor{
TablePlugin: tab,
}
module.VirtualTable = vTab
err := conn.CreateModule(name, module)
if err != nil {
return err
}
statement := table.ColumnDefinition(tab.Columns())
format := "CREATE VIRTUAL TABLE temp." + name + " USING " + name + statement
var values []driver.Value
_, err = conn.Exec(format, values)
if err != nil {
return err
}
}
return nil
},
})
}