-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqlite.go
85 lines (77 loc) · 1.79 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
package sqlite
import (
"database/sql"
"strings"
"github.com/gopsql/db"
"github.com/gopsql/standard"
_ "modernc.org/sqlite"
)
type (
sqliteDB struct {
*standard.DB
}
)
func (d *sqliteDB) FieldDataType(fieldName, fieldType string) (dataType string) {
return FieldDataType(fieldName, fieldType)
}
var _ db.DB = (*sqliteDB)(nil)
// MustOpen is like Open but panics if connect operation fails.
func MustOpen(conn string) *sqliteDB {
c, err := Open(conn)
if err != nil {
panic(err)
}
return c
}
// Open creates and establishes one connection to database.
func Open(conn string) (*sqliteDB, error) {
c, err := sql.Open("sqlite", conn)
if err != nil {
return nil, err
}
if err := c.Ping(); err != nil {
return nil, err
}
return &sqliteDB{standard.NewDB("sqlite", c)}, nil
}
// Generate data type based on struct's field name and type.
func FieldDataType(fieldName, fieldType string) (dataType string) {
if strings.ToLower(fieldName) == "id" && strings.Contains(fieldType, "int") {
dataType = "INTEGER PRIMARY KEY AUTOINCREMENT"
return
}
var null bool
if strings.HasPrefix(fieldType, "*") {
fieldType = strings.TrimPrefix(fieldType, "*")
null = true
}
var defValue string
switch fieldType {
case "int8", "int16", "int32", "uint8", "uint16", "uint32":
dataType = "integer"
defValue = "0"
case "int64", "uint64", "int", "uint":
dataType = "bigint"
defValue = "0"
case "time.Time":
dataType = "timestamp"
defValue = "CURRENT_TIMESTAMP"
case "float32", "float64":
dataType = "decimal(10, 2)"
defValue = "0.0"
case "decimal.Decimal":
dataType = "decimal(10, 2)"
defValue = "0.0"
case "bool":
dataType = "boolean"
defValue = "false"
default:
dataType = "text"
defValue = "''"
}
dataType += " DEFAULT " + defValue
if !null {
dataType += " NOT NULL"
}
return
}