Skip to content

Commit a5c99af

Browse files
committed
Updated bugs
1 parent 31d192b commit a5c99af

File tree

6 files changed

+42
-26
lines changed

6 files changed

+42
-26
lines changed

etc/server.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ handlers:
2121
sqlite3:
2222
# Databases to load and/or create. Only the 'main' database is required.
2323
databases:
24-
main: ":memory:"
25-
test: /tmp/test.sqlite
26-
24+
main: /tmp/test.sqlite
25+
test: ":memory:"
26+
2727
# Set create to true to allow databases which don't exist to be created, or
2828
# else error will be reported on server start. In-memory databases can always
2929
# be created.
30-
create: false
30+
create: true
3131

3232
# Set trace to true to enable the ability to profile queries. Profiling information
3333
# can be displayed through the API.

pkg/sqlite3/pool.go

Lines changed: 2 additions & 2 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.SQLITE_OPEN_CREATE | sqlite3.SQLITE_OPEN_SHAREDCACHE,
61+
Flags: sqlite3.SQLITE_OPEN_CREATE | sqlite3.SQLITE_OPEN_READWRITE | sqlite3.SQLITE_OPEN_SHAREDCACHE,
6262
}
6363
)
6464

@@ -257,7 +257,7 @@ func (p *Pool) new() (*Conn, error) {
257257
for schema := range p.Schemas {
258258
schema = strings.TrimSpace(schema)
259259
path := p.pathForSchema(schema)
260-
if path == defaultPath {
260+
if schema == defaultSchema {
261261
continue
262262
}
263263
if path == "" {

pkg/sqlite3/schema.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package sqlite3
22

33
import (
4-
"fmt"
54

65
// Namespace imports
76
. "github.com/djthorpe/go-sqlite"
@@ -13,7 +12,7 @@ import (
1312

1413
// Schemas returns a list of schemas
1514
func (c *Conn) Schemas() []string {
16-
var schemas []string
15+
schemas := []string{}
1716
if err := c.Exec(Q("PRAGMA database_list"), func(row, col []string) bool {
1817
schemas = append(schemas, row[1])
1918
return false
@@ -44,7 +43,7 @@ func (c *Conn) ColumnsForTable(schema, table string) []SQColumn {
4443
if schema == "" {
4544
return c.ColumnsForTable(defaultSchema, table)
4645
}
47-
var result []SQColumn
46+
result := []SQColumn{}
4847
if err := c.Exec(Q("PRAGMA ", N(schema), ".table_info(", N(table), ")"), func(row, k []string) bool {
4948
// k is "cid" "name" "type" "notnull" "dflt_value" "pk"
5049
col := C(row[1]).WithType(row[2])
@@ -58,7 +57,6 @@ func (c *Conn) ColumnsForTable(schema, table string) []SQColumn {
5857
result = append(result, col)
5958
return false
6059
}); err != nil {
61-
fmt.Println(err)
6260
return nil
6361
}
6462
return result
@@ -70,7 +68,7 @@ func (c *Conn) ColumnsForIndex(schema, index string) []string {
7068
return c.ColumnsForIndex(defaultSchema, index)
7169
}
7270

73-
var result []string
71+
result := []string{}
7472
if err := c.Exec(Q("PRAGMA ", N(schema), ".index_info(", N(index), ")"), func(row, c []string) bool {
7573
result = append(result, row[2])
7674
return false
@@ -87,7 +85,7 @@ func (c *Conn) IndexesForTable(schema, table string) []SQIndexView {
8785
} else if schema == "" {
8886
return c.IndexesForTable(defaultSchema, table)
8987
}
90-
var result []SQIndexView
88+
result := []SQIndexView{}
9189
if err := c.ExecEx(Q("PRAGMA ", N(schema), ".index_list(", N(table), ")").Query(), func(row, _ []string) bool {
9290
// columns are is "seq" "name" "unique" "origin" "partial"
9391

@@ -128,7 +126,7 @@ func (c *Conn) Views(schema string) []string {
128126
// provided, then only modules with those name prefixes are returned.
129127
func (c *Conn) Modules(prefix ...string) []string {
130128
// Get the names, return
131-
var result []string
129+
result := []string{}
132130
if err := c.Exec(Q("PRAGMA module_list"), func(row, _ []string) bool {
133131
if module := row[0]; len(prefix) == 0 || inList(prefix, module, true) {
134132
result = append(result, module)

plugin/sqlite3/handlers.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,16 @@ type PoolResponse struct {
3838
}
3939

4040
type SchemaResponse struct {
41-
Schema string `json:"schema"`
42-
Filename string `json:"filename,omitempty"`
43-
Memory bool `json:"memory,omitempty"`
41+
Schema string `json:"schema"`
42+
Filename string `json:"filename,omitempty"`
43+
Memory bool `json:"memory,omitempty"`
44+
Tables []SchemaTableResponse `json:"tables,omitempty"`
45+
}
46+
47+
type SchemaTableResponse struct {
48+
Name string `json:"name"`
49+
Schema string `json:"schema"`
50+
Indexes []string `json:"indexes,omitempty"`
4451
}
4552

4653
type SqlRequest struct {
@@ -147,6 +154,7 @@ func (p *plugin) ServeSchema(w http.ResponseWriter, req *http.Request) {
147154
response := SchemaResponse{
148155
Schema: params[0],
149156
Filename: conn.Filename(params[0]),
157+
Tables: []SchemaTableResponse{},
150158
}
151159

152160
if err := conn.(*sqlite3.Conn).Exec(Q("PRAGMA database_list;"), func(row, col []string) bool {
@@ -162,6 +170,14 @@ func (p *plugin) ServeSchema(w http.ResponseWriter, req *http.Request) {
162170
response.Memory = true
163171
}
164172

173+
// Populate tables
174+
for _, table := range conn.Tables(params[0]) {
175+
response.Tables = append(response.Tables, SchemaTableResponse{
176+
Name: table,
177+
Schema: params[0],
178+
})
179+
}
180+
165181
// Serve response
166182
router.ServeJSON(w, response, http.StatusOK, 2)
167183
}

sys/sqlite3/conn.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ type Conn C.sqlite3
3939

4040
const (
4141
SQLITE_OPEN_NONE OpenFlags = 0
42-
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.
43-
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.
44-
SQLITE_OPEN_CREATE OpenFlags = C.SQLITE_OPEN_CREATE | C.SQLITE_OPEN_READWRITE // The database is created if it does not already exist
45-
SQLITE_OPEN_URI OpenFlags = C.SQLITE_OPEN_URI // The filename can be interpreted as a URI if this flag is set.
46-
SQLITE_OPEN_MEMORY OpenFlags = C.SQLITE_OPEN_MEMORY // The database will be opened as an in-memory database. The database is named by the "filename" argument for the purposes of cache-sharing, if shared cache mode is enabled, but the "filename" is otherwise ignored.
47-
SQLITE_OPEN_NOMUTEX OpenFlags = C.SQLITE_OPEN_NOMUTEX // The new database connection will use the "multi-thread" threading mode. This means that separate threads are allowed to use SQLite at the same time, as long as each thread is using a different database connection.
48-
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.)
49-
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().
50-
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().
42+
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.
43+
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.
44+
SQLITE_OPEN_CREATE OpenFlags = C.SQLITE_OPEN_CREATE // The database is created if it does not already exist
45+
SQLITE_OPEN_URI OpenFlags = C.SQLITE_OPEN_URI // The filename can be interpreted as a URI if this flag is set.
46+
SQLITE_OPEN_MEMORY OpenFlags = C.SQLITE_OPEN_MEMORY // The database will be opened as an in-memory database. The database is named by the "filename" argument for the purposes of cache-sharing, if shared cache mode is enabled, but the "filename" is otherwise ignored.
47+
SQLITE_OPEN_NOMUTEX OpenFlags = C.SQLITE_OPEN_NOMUTEX // The new database connection will use the "multi-thread" threading mode. This means that separate threads are allowed to use SQLite at the same time, as long as each thread is using a different database connection.
48+
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.)
49+
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().
50+
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().
5151
// SQLITE_OPEN_NOFOLLOW OpenFlags = C.SQLITE_OPEN_NOFOLLOW // The database filename is not allowed to be a symbolic link
5252
SQLITE_OPEN_MIN = SQLITE_OPEN_READONLY
5353
SQLITE_OPEN_MAX = SQLITE_OPEN_PRIVATECACHE
@@ -56,7 +56,7 @@ const (
5656
const (
5757
DefaultSchema = "main"
5858
DefaultMemory = ":memory:"
59-
DefaultFlags = SQLITE_OPEN_CREATE
59+
DefaultFlags = SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE
6060
)
6161

6262
func init() {

sys/sqlite3/connex.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import "C"
5151

5252
import (
5353
"errors"
54+
"fmt"
5455
"reflect"
5556
"sync"
5657
"time"
@@ -503,6 +504,7 @@ func go_authorizer_hook(userInfo unsafe.Pointer, op C.int, a1, a2, a3, a4 *C.cha
503504

504505
//export go_exec_handler
505506
func go_exec_handler(userInfo unsafe.Pointer, nargs C.int, row, cols **C.char) C.int {
507+
fmt.Println("nargs=", nargs, "row=", row, "cols=", cols)
506508
if c := cb.get(uintptr(userInfo)); c != nil && c.ExecFunc != nil {
507509
return C.int(boolToInt(c.ExecFunc(go_string_slice(int(nargs), row), go_string_slice(int(nargs), cols))))
508510
} else {

0 commit comments

Comments
 (0)