-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
sqlite3.go
71 lines (61 loc) · 1.47 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
// Package sqlite3 wraps go-sqlite3 driver as an adapter for grimoire.
//
// Usage:
// // open sqlite3 connection.
// adapter, err := sqlite3.Open("dev.db")
// if err != nil {
// panic(err)
// }
// defer adapter.Close()
//
// // initialize grimoire's repo.
// repo := grimoire.New(adapter)
package sqlite3
import (
db "database/sql"
"strings"
"github.com/Fs02/grimoire"
"github.com/Fs02/grimoire/adapter/sql"
"github.com/Fs02/grimoire/errors"
"github.com/mattn/go-sqlite3"
)
// Adapter definition for mysql database.
type Adapter struct {
*sql.Adapter
}
var _ grimoire.Adapter = (*Adapter)(nil)
// Open mysql connection using dsn.
func Open(dsn string) (*Adapter, error) {
var err error
adapter := &Adapter{
Adapter: &sql.Adapter{
Config: &sql.Config{
Placeholder: "?",
EscapeChar: "`",
InsertDefaultValues: true,
IncrementFunc: incrementFunc,
ErrorFunc: errorFunc,
},
},
}
adapter.DB, err = db.Open("sqlite3", dsn)
return adapter, err
}
func incrementFunc(adapter sql.Adapter) int {
// decrement
return -1
}
func errorFunc(err error) error {
if err == nil {
return nil
}
e, _ := err.(sqlite3.Error)
switch e.ExtendedCode {
case sqlite3.ErrConstraintUnique:
return errors.New(e.Error(), strings.Split(e.Error(), "failed: ")[1], errors.UniqueConstraint)
case sqlite3.ErrConstraintCheck:
return errors.New(e.Error(), strings.Split(e.Error(), "failed: ")[1], errors.CheckConstraint)
default:
return err
}
}