-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsqlite3.go
185 lines (159 loc) · 4.86 KB
/
sqlite3.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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Package sqlite3 wraps go-sqlite3 driver as an adapter for rel.
//
// Usage:
//
// // open sqlite3 connection.
// adapter, err := sqlite3.Open("dev.db")
// if err != nil {
// panic(err)
// }
// defer adapter.Close()
//
// // initialize rel's repo.
// repo := rel.New(adapter)
package sqlite3
import (
db "database/sql"
"log"
"strings"
"github.com/go-rel/rel"
"github.com/go-rel/sql"
"github.com/go-rel/sql/builder"
)
// SQLite3 adapter
type SQLite3 struct {
sql.SQL
}
// Name of database type this adapter implements.
const Name string = "sqlite3"
// New sqlite3 adapter using existing connection.
func New(database *db.DB) rel.Adapter {
var (
bufferFactory = builder.BufferFactory{ArgumentPlaceholder: "?", BoolTrueValue: "1", BoolFalseValue: "0", Quoter: builder.Quote{IDPrefix: "\"", IDSuffix: "\"", IDSuffixEscapeChar: "\"", ValueQuote: "'", ValueQuoteEscapeChar: "'"}}
filterBuilder = builder.Filter{}
queryBuilder = builder.Query{BufferFactory: bufferFactory, Filter: filterBuilder}
OnConflictBuilder = builder.OnConflict{Statement: "ON CONFLICT", IgnoreStatement: "DO NOTHING", UpdateStatement: "DO UPDATE SET", TableQualifier: "excluded", SupportKey: true}
InsertBuilder = builder.Insert{BufferFactory: bufferFactory, InsertDefaultValues: true, OnConflict: OnConflictBuilder}
insertAllBuilder = builder.InsertAll{BufferFactory: bufferFactory, OnConflict: OnConflictBuilder}
updateBuilder = builder.Update{BufferFactory: bufferFactory, Query: queryBuilder, Filter: filterBuilder}
deleteBuilder = builder.Delete{BufferFactory: bufferFactory, Query: queryBuilder, Filter: filterBuilder}
ddlBufferFactory = builder.BufferFactory{InlineValues: true, BoolTrueValue: "1", BoolFalseValue: "0", Quoter: builder.Quote{IDPrefix: "\"", IDSuffix: "\"", IDSuffixEscapeChar: "\"", ValueQuote: "'", ValueQuoteEscapeChar: "'"}}
ddlQueryBuilder = builder.Query{BufferFactory: ddlBufferFactory, Filter: filterBuilder}
tableBuilder = builder.Table{BufferFactory: ddlBufferFactory, ColumnMapper: columnMapper, ColumnOptionsMapper: columnOptionsMapper, DefinitionFilter: definitionFilter, DropKeyMapper: sql.DropKeyMapper}
indexBuilder = builder.Index{BufferFactory: ddlBufferFactory, Query: ddlQueryBuilder, Filter: filterBuilder, SupportFilter: true}
)
return &SQLite3{
SQL: sql.SQL{
QueryBuilder: queryBuilder,
InsertBuilder: InsertBuilder,
InsertAllBuilder: insertAllBuilder,
UpdateBuilder: updateBuilder,
DeleteBuilder: deleteBuilder,
TableBuilder: tableBuilder,
IndexBuilder: indexBuilder,
Increment: -1,
ErrorMapper: errorMapper,
DB: database,
},
}
}
// Open sqlite3 connection using dsn.
func Open(dsn string) (rel.Adapter, error) {
database, err := db.Open("sqlite3", dsn)
return New(database), err
}
// MustOpen sqlite3 connection using dsn.
func MustOpen(dsn string) rel.Adapter {
database, err := db.Open("sqlite3", dsn)
if err != nil {
panic(err)
}
return New(database)
}
// Name of database adapter.
func (SQLite3) Name() string {
return Name
}
func errorMapper(err error) error {
if err == nil {
return nil
}
var (
msg = err.Error()
failedSep = " failed: "
failedIndex = strings.Index(msg, failedSep)
failedLen = 9 // len(failedSep)
)
if failedIndex < 0 {
failedIndex = 0
}
switch msg[:failedIndex] {
case "UNIQUE constraint":
return rel.ConstraintError{
Key: msg[failedIndex+failedLen:],
Type: rel.UniqueConstraint,
Err: err,
}
case "CHECK constraint":
return rel.ConstraintError{
Key: msg[failedIndex+failedLen:],
Type: rel.CheckConstraint,
Err: err,
}
default:
return err
}
}
func columnMapper(column *rel.Column) (string, int, int) {
var (
typ string
m, n int
)
switch column.Type {
case rel.ID:
return "INTEGER", 0, 0
case rel.BigID:
return "INTEGER", 0, 0
case rel.Int:
typ = "INTEGER"
m = column.Limit
default:
typ, m, n = sql.ColumnMapper(column)
}
if column.Unsigned {
typ = "UNSIGNED " + typ
}
return typ, m, n
}
func columnOptionsMapper(column *rel.Column) string {
var buffer strings.Builder
if column.Primary {
buffer.WriteString(" PRIMARY KEY")
if column.Type == rel.ID || column.Type == rel.BigID {
buffer.WriteString(" AUTOINCREMENT")
}
}
if column.Required {
buffer.WriteString(" NOT NULL")
}
if column.Unique {
buffer.WriteString(" UNIQUE")
}
buf := buffer.String()
if buf != "" {
buf = buf[1:]
}
return buf
}
func definitionFilter(table rel.Table, def rel.TableDefinition) bool {
if table.Op == rel.SchemaAlter {
// https://www.sqlite.org/omitted.html
// > Only the RENAME TABLE, ADD COLUMN, RENAME COLUMN, and DROP COLUMN variants of the ALTER TABLE command are supported.
_, ok := def.(rel.Key)
if ok {
log.Print("[REL] SQLite3 adapter does not support adding keys when modifying tables")
return false
}
}
return true
}