Skip to content

Commit d81573c

Browse files
committed
Updated sqlite3 package
1 parent b42be62 commit d81573c

File tree

11 files changed

+383
-447
lines changed

11 files changed

+383
-447
lines changed

cmd/indexer/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func main() {
134134
wg.Add(1)
135135
go func(i uint) {
136136
defer wg.Done()
137-
conn := pool.Get(ctx)
137+
conn := pool.Get()
138138
if conn == nil {
139139
return
140140
}
@@ -176,7 +176,7 @@ func sep(r rune) bool {
176176
}
177177

178178
func CreateSchema(ctx context.Context, pool SQPool) error {
179-
conn := pool.Get(ctx)
179+
conn := pool.Get()
180180
if conn == nil {
181181
return errors.New("Unable to get a connection from pool")
182182
}
@@ -202,7 +202,7 @@ func Process(ctx context.Context, conn SQConnection, evt *indexer.QueueEvent) er
202202
if result, err := txn.Query(Q(`REPLACE INTO files (name, path) VALUES (?, ?)`), evt.Name, evt.Path); err != nil {
203203
return err
204204
} else if result.LastInsertId() > 0 {
205-
fmt.Println("ADDED:", evt)
205+
//fmt.Println("ADDED:", evt)
206206
}
207207
case indexer.EventRemove:
208208
if result, err := txn.Query(Q(`DELETE FROM files WHERE name=? AND path=?`), evt.Name, evt.Path); err != nil {

pkg/sqlite3/auth.go

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,74 +17,74 @@ import (
1717
func (p *Pool) auth(ctx context.Context, action sqlite3.SQAction, args [4]string) error {
1818
switch action {
1919
case sqlite3.SQLITE_CREATE_INDEX:
20-
return p.Auth.CanExec(ctx, SQLITE_AUTH_INDEX|SQLITE_AUTH_CREATE, args[2], args[1], args[0])
20+
return p.cfg.Auth.CanExec(ctx, SQLITE_AUTH_INDEX|SQLITE_AUTH_CREATE, args[2], args[1], args[0])
2121
case sqlite3.SQLITE_CREATE_TABLE: // 2 /* Table Name NULL */
22-
return p.Auth.CanExec(ctx, SQLITE_AUTH_TABLE|SQLITE_AUTH_CREATE, args[2], args[0])
22+
return p.cfg.Auth.CanExec(ctx, SQLITE_AUTH_TABLE|SQLITE_AUTH_CREATE, args[2], args[0])
2323
case sqlite3.SQLITE_CREATE_TEMP_INDEX: // 3 /* Index Name Table Name */
24-
return p.Auth.CanExec(ctx, SQLITE_AUTH_INDEX|SQLITE_AUTH_CREATE|SQLITE_AUTH_TEMP, args[2], args[1], args[0])
24+
return p.cfg.Auth.CanExec(ctx, SQLITE_AUTH_INDEX|SQLITE_AUTH_CREATE|SQLITE_AUTH_TEMP, args[2], args[1], args[0])
2525
case sqlite3.SQLITE_CREATE_TEMP_TABLE: // 4 /* Table Name NULL */
26-
return p.Auth.CanExec(ctx, SQLITE_AUTH_TABLE|SQLITE_AUTH_CREATE|SQLITE_AUTH_TEMP, args[2], args[0])
26+
return p.cfg.Auth.CanExec(ctx, SQLITE_AUTH_TABLE|SQLITE_AUTH_CREATE|SQLITE_AUTH_TEMP, args[2], args[0])
2727
case sqlite3.SQLITE_CREATE_TEMP_TRIGGER: // 5 /* Trigger Name Table Name */
28-
return p.Auth.CanExec(ctx, SQLITE_AUTH_TRIGGER|SQLITE_AUTH_CREATE|SQLITE_AUTH_TEMP, args[2], args[1], args[0])
28+
return p.cfg.Auth.CanExec(ctx, SQLITE_AUTH_TRIGGER|SQLITE_AUTH_CREATE|SQLITE_AUTH_TEMP, args[2], args[1], args[0])
2929
case sqlite3.SQLITE_CREATE_TEMP_VIEW: // 6 /* View Name NULL */
30-
return p.Auth.CanExec(ctx, SQLITE_AUTH_VIEW|SQLITE_AUTH_CREATE|SQLITE_AUTH_TEMP, args[2], args[1], args[0])
30+
return p.cfg.Auth.CanExec(ctx, SQLITE_AUTH_VIEW|SQLITE_AUTH_CREATE|SQLITE_AUTH_TEMP, args[2], args[1], args[0])
3131
case sqlite3.SQLITE_CREATE_TRIGGER: // 7 /* Trigger Name Table Name */
32-
return p.Auth.CanExec(ctx, SQLITE_AUTH_TRIGGER|SQLITE_AUTH_CREATE, args[2], args[1], args[0])
32+
return p.cfg.Auth.CanExec(ctx, SQLITE_AUTH_TRIGGER|SQLITE_AUTH_CREATE, args[2], args[1], args[0])
3333
case sqlite3.SQLITE_CREATE_VIEW: // 8 /* View Name NULL */
34-
return p.Auth.CanExec(ctx, SQLITE_AUTH_VIEW|SQLITE_AUTH_CREATE, args[2], args[1], args[0])
34+
return p.cfg.Auth.CanExec(ctx, SQLITE_AUTH_VIEW|SQLITE_AUTH_CREATE, args[2], args[1], args[0])
3535
case sqlite3.SQLITE_DELETE: // 9 /* Table Name NULL */
36-
return p.Auth.CanExec(ctx, SQLITE_AUTH_TABLE|SQLITE_AUTH_DELETE, args[2], args[0])
36+
return p.cfg.Auth.CanExec(ctx, SQLITE_AUTH_TABLE|SQLITE_AUTH_DELETE, args[2], args[0])
3737
case sqlite3.SQLITE_DROP_INDEX: // 10 /* Index Name Table Name */
38-
return p.Auth.CanExec(ctx, SQLITE_AUTH_INDEX|SQLITE_AUTH_DROP, args[2], args[1], args[0])
38+
return p.cfg.Auth.CanExec(ctx, SQLITE_AUTH_INDEX|SQLITE_AUTH_DROP, args[2], args[1], args[0])
3939
case sqlite3.SQLITE_DROP_TABLE: // 11 /* Table Name NULL */
40-
return p.Auth.CanExec(ctx, SQLITE_AUTH_TABLE|SQLITE_AUTH_DROP, args[2], args[0])
40+
return p.cfg.Auth.CanExec(ctx, SQLITE_AUTH_TABLE|SQLITE_AUTH_DROP, args[2], args[0])
4141
case sqlite3.SQLITE_DROP_TEMP_INDEX: // 12 /* Index Name Table Name */
42-
return p.Auth.CanExec(ctx, SQLITE_AUTH_INDEX|SQLITE_AUTH_DROP|SQLITE_AUTH_TEMP, args[2], args[1], args[0])
42+
return p.cfg.Auth.CanExec(ctx, SQLITE_AUTH_INDEX|SQLITE_AUTH_DROP|SQLITE_AUTH_TEMP, args[2], args[1], args[0])
4343
case sqlite3.SQLITE_DROP_TEMP_TABLE: // 13 /* Table Name NULL */
44-
return p.Auth.CanExec(ctx, SQLITE_AUTH_TABLE|SQLITE_AUTH_DROP|SQLITE_AUTH_TEMP, args[2], args[0])
44+
return p.cfg.Auth.CanExec(ctx, SQLITE_AUTH_TABLE|SQLITE_AUTH_DROP|SQLITE_AUTH_TEMP, args[2], args[0])
4545
case sqlite3.SQLITE_DROP_TEMP_TRIGGER: // 14 /* Trigger Name Table Name */
46-
return p.Auth.CanExec(ctx, SQLITE_AUTH_TRIGGER|SQLITE_AUTH_DROP|SQLITE_AUTH_TEMP, args[2], args[1], args[0])
46+
return p.cfg.Auth.CanExec(ctx, SQLITE_AUTH_TRIGGER|SQLITE_AUTH_DROP|SQLITE_AUTH_TEMP, args[2], args[1], args[0])
4747
case sqlite3.SQLITE_DROP_TEMP_VIEW: // 15 /* View Name NULL */
48-
return p.Auth.CanExec(ctx, SQLITE_AUTH_VIEW|SQLITE_AUTH_DROP|SQLITE_AUTH_TEMP, args[2], args[1], args[0])
48+
return p.cfg.Auth.CanExec(ctx, SQLITE_AUTH_VIEW|SQLITE_AUTH_DROP|SQLITE_AUTH_TEMP, args[2], args[1], args[0])
4949
case sqlite3.SQLITE_DROP_TRIGGER: // 16 /* Trigger Name Table Name */
50-
return p.Auth.CanExec(ctx, SQLITE_AUTH_TRIGGER|SQLITE_AUTH_DROP, args[2], args[1], args[0])
50+
return p.cfg.Auth.CanExec(ctx, SQLITE_AUTH_TRIGGER|SQLITE_AUTH_DROP, args[2], args[1], args[0])
5151
case sqlite3.SQLITE_DROP_VIEW: // 17 /* View Name NULL */
52-
return p.Auth.CanExec(ctx, SQLITE_AUTH_VIEW|SQLITE_AUTH_DROP, args[2], args[1], args[0])
52+
return p.cfg.Auth.CanExec(ctx, SQLITE_AUTH_VIEW|SQLITE_AUTH_DROP, args[2], args[1], args[0])
5353
case sqlite3.SQLITE_INSERT: // 18 /* Table Name NULL */
54-
return p.Auth.CanExec(ctx, SQLITE_AUTH_TABLE|SQLITE_AUTH_INSERT, args[2], args[0])
54+
return p.cfg.Auth.CanExec(ctx, SQLITE_AUTH_TABLE|SQLITE_AUTH_INSERT, args[2], args[0])
5555
case sqlite3.SQLITE_PRAGMA:
5656
// 19 /* Pragma Name 1st arg or NULL */
5757
if args[1] == "" {
58-
return p.Auth.CanExec(ctx, SQLITE_AUTH_PRAGMA, args[0])
58+
return p.cfg.Auth.CanExec(ctx, SQLITE_AUTH_PRAGMA, args[0])
5959
} else {
60-
return p.Auth.CanExec(ctx, SQLITE_AUTH_PRAGMA, args[0], args[1])
60+
return p.cfg.Auth.CanExec(ctx, SQLITE_AUTH_PRAGMA, args[0], args[1])
6161
}
6262
case sqlite3.SQLITE_SELECT: // 21 /* NULL NULL */
63-
return p.Auth.CanSelect(ctx)
63+
return p.cfg.Auth.CanSelect(ctx)
6464
case sqlite3.SQLITE_ALTER_TABLE: // 26 /* Database Name Table Name */
65-
return p.Auth.CanExec(ctx, SQLITE_AUTH_TABLE|SQLITE_AUTH_ALTER, args[0], args[1])
65+
return p.cfg.Auth.CanExec(ctx, SQLITE_AUTH_TABLE|SQLITE_AUTH_ALTER, args[0], args[1])
6666
case sqlite3.SQLITE_CREATE_VTABLE: // 29 /* Table Name Module Name */
67-
return p.Auth.CanExec(ctx, SQLITE_AUTH_VTABLE|SQLITE_AUTH_CREATE, args[2], args[0], args[1])
67+
return p.cfg.Auth.CanExec(ctx, SQLITE_AUTH_VTABLE|SQLITE_AUTH_CREATE, args[2], args[0], args[1])
6868
case sqlite3.SQLITE_DROP_VTABLE: // 30 /* Table Name Module Name */
69-
return p.Auth.CanExec(ctx, SQLITE_AUTH_VTABLE|SQLITE_AUTH_DROP, args[2], args[0], args[1])
69+
return p.cfg.Auth.CanExec(ctx, SQLITE_AUTH_VTABLE|SQLITE_AUTH_DROP, args[2], args[0], args[1])
7070
case sqlite3.SQLITE_ANALYZE: // 28 /* Table Name NULL */
71-
return p.Auth.CanExec(ctx, SQLITE_AUTH_TABLE|SQLITE_AUTH_ANALYZE, args[2], args[0])
71+
return p.cfg.Auth.CanExec(ctx, SQLITE_AUTH_TABLE|SQLITE_AUTH_ANALYZE, args[2], args[0])
7272
case sqlite3.SQLITE_FUNCTION: // 31 /* NULL Function Name */
73-
return p.Auth.CanExec(ctx, SQLITE_AUTH_FUNCTION, args[1])
73+
return p.cfg.Auth.CanExec(ctx, SQLITE_AUTH_FUNCTION, args[1])
7474
case sqlite3.SQLITE_TRANSACTION: // 22 /* Operation NULL */
7575
switch args[0] {
7676
case "BEGIN":
77-
return p.Auth.CanTransaction(ctx, SQLITE_AUTH_TRANSACTION|SQLITE_AUTH_BEGIN)
77+
return p.cfg.Auth.CanTransaction(ctx, SQLITE_AUTH_TRANSACTION|SQLITE_AUTH_BEGIN)
7878
case "ROLLBACK":
79-
return p.Auth.CanTransaction(ctx, SQLITE_AUTH_TRANSACTION|SQLITE_AUTH_ROLLBACK)
79+
return p.cfg.Auth.CanTransaction(ctx, SQLITE_AUTH_TRANSACTION|SQLITE_AUTH_ROLLBACK)
8080
case "COMMIT":
81-
return p.Auth.CanTransaction(ctx, SQLITE_AUTH_TRANSACTION|SQLITE_AUTH_COMMIT)
81+
return p.cfg.Auth.CanTransaction(ctx, SQLITE_AUTH_TRANSACTION|SQLITE_AUTH_COMMIT)
8282
}
8383
// TODO: Op is BEGIN, ROLLBACK or COMMIT so use this
8484
case sqlite3.SQLITE_READ: // 20 /* Table Name Column Name */
85-
return p.Auth.CanExec(ctx, SQLITE_AUTH_TABLE|SQLITE_AUTH_READ, args[2], args[0], args[1])
85+
return p.cfg.Auth.CanExec(ctx, SQLITE_AUTH_TABLE|SQLITE_AUTH_READ, args[2], args[0], args[1])
8686
case sqlite3.SQLITE_UPDATE: // 23 /* Table Name Column Name */
87-
return p.Auth.CanExec(ctx, SQLITE_AUTH_TABLE|SQLITE_AUTH_UPDATE, args[2], args[0], args[1])
87+
return p.cfg.Auth.CanExec(ctx, SQLITE_AUTH_TABLE|SQLITE_AUTH_UPDATE, args[2], args[0], args[1])
8888
// TODO case sqlite3.SQLITE_SAVEPOINT: // 32 /* Operation Savepoint Name */
8989
// TODO case sqlite3.SQLITE_ATTACH: // 24 /* Filename NULL */
9090
// TODO case sqlite3.SQLITE_DETACH: // 25 /* Database Name NULL */

pkg/sqlite3/auth_test.go

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,19 @@ import (
1515
)
1616

1717
func Test_Auth_001(t *testing.T) {
18-
errs, cancel := catchErrors(t)
19-
defer cancel()
20-
21-
// Create the pool
22-
pool, err := OpenPool(PoolConfig{
23-
Schemas: map[string]string{"main": ":memory:"},
24-
Auth: NewAuth(t),
25-
}, errs)
18+
errs, cancel := handleErrors(t)
19+
cfg := NewConfig().WithAuth(NewAuth(t))
20+
pool, err := OpenPool(cfg, errs)
2621
if err != nil {
27-
t.Error(err)
22+
t.Fatal(err)
23+
} else {
24+
t.Log(pool)
2825
}
2926
defer pool.Close()
27+
defer cancel()
3028

31-
// Get conn
32-
conn := pool.Get(context.Background())
33-
if conn == nil {
34-
t.Fatal("conn is nil")
35-
}
29+
// Get connection
30+
conn := pool.Get()
3631
defer pool.Put(conn)
3732

3833
// Make various requests

pkg/sqlite3/conn.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ func (conn *Conn) Close() error {
113113

114114
// Execute SQL statement without preparing, and invoke a callback for each row of results
115115
// which may return true to abort
116-
func (conn *Conn) Exec(st SQStatement, fn ExecFunc) error {
116+
func (conn *Conn) Exec(st SQStatement, fn SQExecFunc) error {
117117
if st == nil {
118118
return ErrBadParameter.With("Exec")
119119
}

pkg/sqlite3/foreignkeys_test.go

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package sqlite3_test
22

33
import (
4-
"context"
54
"testing"
5+
"time"
66

77
// Module imports
88

@@ -11,24 +11,23 @@ import (
1111
)
1212

1313
func Test_ForeignKeys_001(t *testing.T) {
14-
errs, cancel := catchErrors(t)
15-
defer cancel()
16-
17-
// Create the pool
18-
pool, err := OpenPool(PoolConfig{
19-
Schemas: map[string]string{"main": ":memory:"},
20-
Trace: true,
21-
}, errs)
14+
errs, cancel := handleErrors(t)
15+
cfg := NewConfig().WithTrace(func(sql string, d time.Duration) {
16+
if d > 0 {
17+
t.Log(sql, "=>", d)
18+
}
19+
})
20+
pool, err := OpenPool(cfg, errs)
2221
if err != nil {
23-
t.Error(err)
22+
t.Fatal(err)
23+
} else {
24+
t.Log(pool)
2425
}
2526
defer pool.Close()
27+
defer cancel()
2628

27-
// Get conn
28-
conn := pool.Get(context.Background())
29-
if conn == nil {
30-
t.Fatal("conn is nil")
31-
}
29+
// Get connection
30+
conn := pool.Get()
3231
defer pool.Put(conn)
3332

3433
if err := conn.(*Conn).SetForeignKeyConstraints(true); err != nil {

0 commit comments

Comments
 (0)