...
Source file
src/tawesoft.co.uk/go/sqlp/sqlite3/sqlite.go
Documentation:
src/tawesoft.co.uk/go/sqlp/sqlite3/sqlite.go
1 package sqlite3
2
3 import (
4 "database/sql"
5 "fmt"
6 "strings"
7
8 "github.com/mattn/go-sqlite3"
9 "tawesoft.co.uk/go/sqlp"
10 )
11
12
13 var Features = sqlp.Features{
14 EscapeIdentifier: escapeIdentifier,
15 EscapeString: escapeString,
16 IsUniqueConstraintError: isUniqueConstraintError,
17 }
18
19
20 func isUniqueConstraintError(err error) bool {
21 if sqliteErr, ok := err.(sqlite3.Error); ok {
22 return (sqliteErr.Code == sqlite3.ErrConstraint) &&
23 (sqliteErr.ExtendedCode == sqlite3.ErrConstraintUnique)
24 }
25 return false
26 }
27
28 func escapeIdentifier(s string) (string, error) {
29 s = strings.ReplaceAll(s, `"`, `""`)
30 return `"`+s+`"`, nil
31 }
32
33 func escapeString(s string) (string, error) {
34 s = strings.ReplaceAll(s, `'`, `''`)
35 return `'`+s+`'`, nil
36 }
37
38
39 type Collation struct {
40 Name string
41 Cmp func(string, string) int
42 }
43
44
45 var Utf8Collation = Collation{"utf8", strings.Compare}
46
47
48 type JournalMode string
49 const (
50 JournalModeDelete = JournalMode("DELETE")
51 JournalModeTruncate = JournalMode("TRUNCATE")
52 JournalModePersist = JournalMode("PERSIST")
53 JournalModeMemory = JournalMode("MEMORY")
54 JournalModeWAL = JournalMode("WAL")
55 JournalModeOff = JournalMode("OFF")
56 )
57
58
59 type Config struct {
60
61 ForeignKeys bool
62
63
64 SecureDelete bool
65
66
67 JournalMode JournalMode
68 }
69
70
71
72
73
74
75
76
77 func Register(driverName string, collations []Collation, extensions []interface{}) {
78
79 sql.Register(driverName,
80 &sqlite3.SQLiteDriver{
81 ConnectHook: func(conn *sqlite3.SQLiteConn) error {
82 for _, col := range collations {
83 err := conn.RegisterCollation(col.Name, col.Cmp)
84 if err != nil {
85 return fmt.Errorf("error registering %s database collation %s: %+v",
86 driverName, col.Name, err)
87 }
88 }
89 return nil
90 },
91 })
92 }
93
94 func onOff(x bool) string {
95 if x { return "ON" }
96 return "OFF"
97 }
98
99
100 func Opener(config Config) (func(string, string) (*sql.DB, error)) {
101 return func(driverName string, dataSource string) (*sql.DB, error) {
102 return Open(driverName, dataSource, config)
103 }
104 }
105
106
107
108
109
110
111
112 func Open(driverName string, dataSource string, config Config) (*sql.DB, error) {
113
114 if driverName == "" {
115 driverName = "sqlite3"
116 }
117
118 db, err := sql.Open(driverName, dataSource)
119 if err != nil { return nil, err }
120
121 var stmt = `
122 PRAGMA foreign_keys = `+onOff(config.ForeignKeys)+`;
123 PRAGMA secure_delete = `+onOff(config.SecureDelete)+`;
124 PRAGMA journal_mode = `+string(config.JournalMode)+`;
125 `
126 _, err = db.Exec(stmt)
127 if err != nil {
128 db.Close();
129 return nil, fmt.Errorf("error setting database PRAGMAs: %+v", err)
130 }
131
132 return db, nil
133 }
134
View as plain text