Skip to content

Commit a2315bb

Browse files
committed
Updated sqlite
1 parent a68d40d commit a2315bb

File tree

4 files changed

+77
-39
lines changed

4 files changed

+77
-39
lines changed

etc/server.yaml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,14 @@ sqlite3:
2424
main: ":memory:"
2525
test: /tmp/test.sqlite
2626

27-
# Set create to true to allow databases which don't exist to be created.
28-
create: true
27+
# Set create to true to allow databases which don't exist to be created, or
28+
# else error will be reported on server start. In-memory databases can always
29+
# be created.
30+
create: false
2931

3032
# Set trace to true to enable the ability to profile queries. Profiling information
3133
# can be displayed through the API.
3234
trace: true
3335

3436
# Set max number of connections that can be simultaneously opened
35-
max: 100
36-
37+
max: 50

pkg/sqlite3/pool.go

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ var (
5858
Trace: false,
5959
Create: true,
6060
Schemas: map[string]string{defaultSchema: defaultMemory},
61-
Flags: sqlite3.DefaultFlags | sqlite3.SQLITE_OPEN_SHAREDCACHE,
61+
Flags: sqlite3.SQLITE_OPEN_CREATE | sqlite3.SQLITE_OPEN_SHAREDCACHE,
6262
}
6363
)
6464

@@ -142,6 +142,7 @@ func (p *Pool) Close() error {
142142

143143
func (p *Pool) String() string {
144144
str := "<pool"
145+
str += fmt.Sprintf(" ver=%q", Version())
145146
str += fmt.Sprint(" cur=", p.Cur())
146147
str += fmt.Sprint(" max=", p.Max())
147148
str += fmt.Sprint(" flags=", p.Flags)
@@ -234,7 +235,7 @@ func (p *Pool) new() (*Conn, error) {
234235
// Always allow memory databases to be created
235236
flags := p.Flags
236237
if defaultPath == defaultMemory {
237-
flags |= sqlite3.SQLITE_OPEN_MEMORY | sqlite3.SQLITE_OPEN_CREATE
238+
flags |= sqlite3.SQLITE_OPEN_CREATE
238239
}
239240

240241
// Perform the open
@@ -243,6 +244,14 @@ func (p *Pool) new() (*Conn, error) {
243244
return nil, err
244245
}
245246

247+
// Set trace
248+
if p.PoolConfig.Trace {
249+
conn.SetTraceHook(func(_ sqlite3.TraceType, a, b unsafe.Pointer) int {
250+
p.trace(conn, (*sqlite3.Statement)(a), *(*int64)(b))
251+
return 0
252+
}, sqlite3.SQLITE_TRACE_PROFILE)
253+
}
254+
246255
// Attach additional databases
247256
var result error
248257
for schema := range p.Schemas {
@@ -258,14 +267,6 @@ func (p *Pool) new() (*Conn, error) {
258267
}
259268
}
260269

261-
// Set trace
262-
if p.PoolConfig.Trace {
263-
conn.SetTraceHook(func(_ sqlite3.TraceType, a, b unsafe.Pointer) int {
264-
p.trace(conn, (*sqlite3.Statement)(a), *(*int64)(b))
265-
return 0
266-
}, sqlite3.SQLITE_TRACE_PROFILE)
267-
}
268-
269270
// Set auth
270271
if p.PoolConfig.Auth != nil {
271272
conn.SetAuthorizerHook(func(action sqlite3.SQAction, args [4]string) sqlite3.SQAuth {
@@ -328,14 +329,6 @@ func (p *Pool) err(err error) {
328329
}
329330
}
330331

331-
// maxInt64 returns the maximum of two values
332-
func maxInt64(a, b int64) int64 {
333-
if a > b {
334-
return a
335-
}
336-
return b
337-
}
338-
339332
// Attach database as schema. If path is empty then a new in-memory database
340333
// is attached.
341334
func (p *Pool) attach(conn *Conn, schema, path string) error {
@@ -348,29 +341,18 @@ func (p *Pool) attach(conn *Conn, schema, path string) error {
348341
// Create a new database or return an error if it doesn't exist
349342
if path != defaultMemory {
350343
if _, err := os.Stat(path); os.IsNotExist(err) {
351-
if err := p.attachcreate(path); err != nil {
344+
if err := p.attachCreate(path); err != nil {
352345
return err
353346
}
354347
} else if err != nil {
355348
return err
356349
}
357350
}
358-
fmt.Println(Q("ATTACH DATABASE ", Quote(path), " AS ", QuoteIdentifier(schema)))
359351
return conn.Exec(Q("ATTACH DATABASE ", Quote(path), " AS ", QuoteIdentifier(schema)), nil)
360352
}
361353

362-
// Detach named database as schema
363-
func (p *Pool) detach(conn *Conn, schema string) error {
364-
return conn.Exec(Q("DETACH DATABASE ", QuoteIdentifier(schema)), nil)
365-
}
366-
367-
// Trace
368-
func (p *Pool) trace(c *Conn, s *sqlite3.Statement, ns int64) {
369-
fmt.Printf("TRACE %q => %v\n", s, time.Duration(ns)*time.Nanosecond)
370-
}
371-
372354
// Create a database before attaching
373-
func (p *Pool) attachcreate(path string) error {
355+
func (p *Pool) attachCreate(path string) error {
374356
if p.PoolConfig.Flags&sqlite3.SQLITE_OPEN_CREATE == 0 {
375357
return ErrBadParameter.Withf("Database does not exist: %q", path)
376358
}
@@ -383,3 +365,16 @@ func (p *Pool) attachcreate(path string) error {
383365
return nil
384366
}
385367
}
368+
369+
// Trace
370+
func (p *Pool) trace(c *Conn, s *sqlite3.Statement, ns int64) {
371+
fmt.Printf("TRACE %q => %v\n", s, time.Duration(ns)*time.Nanosecond)
372+
}
373+
374+
// maxInt64 returns the maximum of two values
375+
func maxInt64(a, b int64) int64 {
376+
if a > b {
377+
return a
378+
}
379+
return b
380+
}

plugin/sqlite3/handlers.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,16 +149,14 @@ func (p *plugin) ServeSchema(w http.ResponseWriter, req *http.Request) {
149149
Filename: conn.Filename(params[0]),
150150
}
151151

152-
if err := conn.(*sqlite3.Conn).Exec(Q("PRAGMA database_list"), func(row, col []string) bool {
152+
if err := conn.(*sqlite3.Conn).Exec(Q("PRAGMA database_list;"), func(row, col []string) bool {
153153
fmt.Printf("%q => %q\n", col, row)
154154
return false
155155
}); err != nil {
156156
router.ServeError(w, http.StatusInternalServerError, err.Error())
157157
return
158158
}
159159

160-
fmt.Printf("%v => %q\n", params[0], conn.Filename(params[0]))
161-
162160
// Set memory flag
163161
if response.Filename == "" {
164162
response.Memory = true

sys/sqlite3/conn.go

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import "C"
1818

1919
import (
2020
"fmt"
21+
"strings"
2122
"unsafe"
2223

2324
// Modules
@@ -37,6 +38,7 @@ type Conn C.sqlite3
3738
// GLOBALS
3839

3940
const (
41+
SQLITE_OPEN_NONE OpenFlags = 0
4042
SQLITE_OPEN_READONLY OpenFlags = C.SQLITE_OPEN_READONLY // The database is opened in read-only mode. If the database does not already exist, an error is returned.
4143
SQLITE_OPEN_READWRITE OpenFlags = C.SQLITE_OPEN_READWRITE // The database is opened for reading and writing if possible, or reading only if the file is write protected by the operating system. In either case the database must already exist, otherwise an error is returned.
4244
SQLITE_OPEN_CREATE OpenFlags = C.SQLITE_OPEN_CREATE | C.SQLITE_OPEN_READWRITE // The database is created if it does not already exist
@@ -46,7 +48,9 @@ const (
4648
SQLITE_OPEN_FULLMUTEX OpenFlags = C.SQLITE_OPEN_FULLMUTEX // The new database connection will use the "serialized" threading mode. This means the multiple threads can safely attempt to use the same database connection at the same time. (Mutexes will block any actual concurrency, but in this mode there is no harm in trying.)
4749
SQLITE_OPEN_SHAREDCACHE OpenFlags = C.SQLITE_OPEN_SHAREDCACHE // The database is opened shared cache enabled, overriding the default shared cache setting provided by sqlite3_enable_shared_cache().
4850
SQLITE_OPEN_PRIVATECACHE OpenFlags = C.SQLITE_OPEN_PRIVATECACHE // The database is opened shared cache disabled, overriding the default shared cache setting provided by sqlite3_enable_shared_cache().
49-
// SQLITE_OPEN_NOFOLLOW OpenFlags = C.SQLITE_OPEN_NOFOLLOW // The database filename is not allowed to be a symbolic link
51+
SQLITE_OPEN_NOFOLLOW OpenFlags = C.SQLITE_OPEN_NOFOLLOW // The database filename is not allowed to be a symbolic link
52+
SQLITE_OPEN_MIN = SQLITE_OPEN_READONLY
53+
SQLITE_OPEN_MAX = SQLITE_OPEN_NOFOLLOW
5054
)
5155

5256
const (
@@ -84,6 +88,46 @@ func (c *Conn) String() string {
8488
return str + ">"
8589
}
8690

91+
func (v OpenFlags) StringFlag() string {
92+
switch v {
93+
case SQLITE_OPEN_NONE:
94+
return "SQLITE_OPEN_NONE"
95+
case SQLITE_OPEN_READONLY:
96+
return "SQLITE_OPEN_READONLY"
97+
case SQLITE_OPEN_READWRITE:
98+
return "SQLITE_OPEN_READWRITE"
99+
case SQLITE_OPEN_CREATE:
100+
return "SQLITE_OPEN_CREATE"
101+
case SQLITE_OPEN_URI:
102+
return "SQLITE_OPEN_URI"
103+
case SQLITE_OPEN_MEMORY:
104+
return "SQLITE_OPEN_MEMORY"
105+
case SQLITE_OPEN_NOMUTEX:
106+
return "SQLITE_OPEN_NOMUTEX"
107+
case SQLITE_OPEN_FULLMUTEX:
108+
return "SQLITE_OPEN_FULLMUTEX"
109+
case SQLITE_OPEN_SHAREDCACHE:
110+
return "SQLITE_OPEN_SHAREDCACHE"
111+
case SQLITE_OPEN_PRIVATECACHE:
112+
return "SQLITE_OPEN_PRIVATECACHE"
113+
default:
114+
return "[?? Invalid OpenFlags value]"
115+
}
116+
}
117+
118+
func (v OpenFlags) String() string {
119+
if v == SQLITE_OPEN_NONE {
120+
return v.StringFlag()
121+
}
122+
str := ""
123+
for f := SQLITE_OPEN_MIN; f <= SQLITE_OPEN_MAX; f <<= 1 {
124+
if v&f != 0 {
125+
str += "|" + f.StringFlag()
126+
}
127+
}
128+
return strings.TrimPrefix(str, "|")
129+
}
130+
87131
///////////////////////////////////////////////////////////////////////////////
88132
// METHODS
89133

0 commit comments

Comments
 (0)