-
Notifications
You must be signed in to change notification settings - Fork 1
/
sqlite.go
107 lines (94 loc) · 2.04 KB
/
sqlite.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
//go:build cgo
// +build cgo
// 因为 go-sqlite3 库是一个 cgo 库,编译代码时需要 gcc 环境。
package database
import (
"database/sql"
"fmt"
_ "github.com/mattn/go-sqlite3"
"github.com/whaios/showdocdb/log"
)
func init() {
sqliteFactory = newSqlite
}
func newSqlite(file string) Query {
// "file:test.db?cache=shared&mode=memory"
dsn := fmt.Sprintf("file:%s", file)
return &sqlite{
dsn: dsn,
}
}
type sqlite struct {
dsn string
db *sql.DB
}
func (d *sqlite) Open() error {
db, err := sql.Open(SQlite, d.dsn)
d.db = db
return err
}
func (d *sqlite) Close() error {
if d.db != nil {
return d.db.Close()
}
return nil
}
func (d *sqlite) Query() ([]*Table, error) {
tbs, err := d.queryTable()
if err != nil {
return nil, err
}
for _, tb := range tbs {
log.Debug("table: %s", tb.Name)
cols, err := d.queryColumn(tb.Name)
if err != nil {
return nil, err
}
tb.Columns = cols
}
return tbs, nil
}
func (d *sqlite) queryTable() ([]*Table, error) {
querySql := "SELECT name FROM sqlite_master WHERE type ='table' ORDER BY name"
rows, err := d.db.Query(querySql)
if err != nil {
return nil, err
}
defer rows.Close()
tbs := make([]*Table, 0)
for rows.Next() {
var name string
if err = rows.Scan(&name); err != nil {
return tbs, err
}
tbs = append(tbs, &Table{Name: name})
}
return tbs, nil
}
func (d *sqlite) queryColumn(tableName string) ([]*Column, error) {
querySql := fmt.Sprintf("PRAGMA table_info([%s])", tableName)
rows, err := d.db.Query(querySql)
if err != nil {
return nil, err
}
defer rows.Close()
cols := make([]*Column, 0)
for rows.Next() {
var dflt_value sql.NullString
var cid, name, tpe, notnull, pk string
if err = rows.Scan(&cid, &name, &tpe, ¬null, &dflt_value, &pk); err != nil {
return cols, err
}
col := &Column{
TableName: tableName,
Name: name,
Default: dflt_value.String,
IsNull: notnull,
Type: tpe,
Comment: "",
}
cols = append(cols, col)
log.Debug("column: %s.%s", col.TableName, col.Name)
}
return cols, nil
}