Skip to content

Commit 91d9738

Browse files
committed
Updated sqobj
1 parent 0689196 commit 91d9738

File tree

15 files changed

+607
-274
lines changed

15 files changed

+607
-274
lines changed

pkg/sqlite3/conn.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type Conn struct {
3131

3232
type Txn struct {
3333
*Conn
34+
f SQFlag
3435
}
3536

3637
type ExecFunc sqlite3.ExecFunc
@@ -111,6 +112,11 @@ func (conn *Conn) Exec(st SQStatement, fn ExecFunc) error {
111112
return conn.ConnEx.Exec(st.Query(), sqlite3.ExecFunc(fn))
112113
}
113114

115+
// Execute SQL statement outside of transaction - currently not implemented
116+
func (conn *Conn) Query(st SQStatement, v ...interface{}) (SQResults, error) {
117+
return nil, ErrNotImplemented.With("Query")
118+
}
119+
114120
// Perform a transaction, rollback if error is returned
115121
func (conn *Conn) Do(ctx context.Context, flag SQFlag, fn func(SQTransaction) error) error {
116122
conn.Mutex.Lock()
@@ -152,7 +158,7 @@ func (conn *Conn) Do(ctx context.Context, flag SQFlag, fn func(SQTransaction) er
152158
conn.SetProgressHandler(100, func() bool {
153159
return ctx != nil && ctx.Err() != nil
154160
})
155-
if err := fn(&Txn{conn}); err != nil {
161+
if err := fn(&Txn{conn, flag}); err != nil {
156162
result = multierror.Append(result, err)
157163
}
158164
conn.SetProgressHandler(0, nil)
@@ -188,7 +194,7 @@ func (txn *Txn) Query(st SQStatement, v ...interface{}) (SQResults, error) {
188194
}
189195

190196
// Get a results object
191-
r, err := txn.ConnCache.Prepare(txn.Conn.ConnEx, st.Query())
197+
r, err := txn.Conn.ConnCache.Prepare(txn.Conn.ConnEx, st.Query())
192198
if err != nil {
193199
return nil, err
194200
}
@@ -205,3 +211,8 @@ func (txn *Txn) Query(st SQStatement, v ...interface{}) (SQResults, error) {
205211
func (c *Conn) Flags() SQFlag {
206212
return c.f
207213
}
214+
215+
// Flags returns the Open Flags or'd with Transaction Flags
216+
func (t *Txn) Flags() SQFlag {
217+
return t.f | t.Conn.f
218+
}

pkg/sqlite3/pool.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ var (
5656
Max: 5,
5757
Trace: false,
5858
Create: true,
59-
Schemas: map[string]string{defaultSchema: defaultMemory},
59+
Schemas: map[string]string{DefaultSchema: defaultMemory},
6060
Flags: SQFlag(sqlite3.SQLITE_OPEN_CREATE) | SQFlag(sqlite3.SQLITE_OPEN_SHAREDCACHE) | SQLITE_OPEN_CACHE,
6161
}
6262
)
@@ -70,7 +70,7 @@ var (
7070
func NewPool(path string, errs chan<- error) (*Pool, error) {
7171
cfg := defaultPoolConfig
7272
if path != "" {
73-
cfg.Schemas = map[string]string{defaultSchema: path}
73+
cfg.Schemas = map[string]string{DefaultSchema: path}
7474
}
7575
return OpenPool(cfg, errs)
7676
}
@@ -226,9 +226,9 @@ func (p *Pool) Put(conn SQConnection) {
226226
// complete operation
227227
func (p *Pool) new() (*Conn, error) {
228228
// Open connection to main schema, which is required
229-
defaultPath := p.pathForSchema(defaultSchema)
229+
defaultPath := p.pathForSchema(DefaultSchema)
230230
if defaultPath == "" {
231-
return nil, ErrNotFound.Withf("No default schema %q found", defaultSchema)
231+
return nil, ErrNotFound.Withf("No default schema %q found", DefaultSchema)
232232
}
233233

234234
// Always allow memory databases to be created and read/write
@@ -256,7 +256,7 @@ func (p *Pool) new() (*Conn, error) {
256256
for schema := range p.Schemas {
257257
schema = strings.TrimSpace(schema)
258258
path := p.pathForSchema(schema)
259-
if schema == defaultSchema {
259+
if schema == DefaultSchema {
260260
continue
261261
}
262262
if path == "" {
@@ -313,7 +313,7 @@ func (p *Pool) put(conn *Conn) {
313313
// or an empty string if the schema name is not valid
314314
func (p *Pool) pathForSchema(schema string) string {
315315
if schema == "" {
316-
return p.pathForSchema(defaultSchema)
316+
return p.pathForSchema(DefaultSchema)
317317
} else if !reSchemaName.MatchString(schema) {
318318
return ""
319319
} else if path, exists := p.Schemas[schema]; !exists {

pkg/sqlite3/schema.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ func (c *Conn) Schemas() []string {
2828
// Filename returns the filename for a schema
2929
func (c *Conn) Filename(schema string) string {
3030
if schema == "" {
31-
return c.Filename(defaultSchema)
31+
return c.Filename(DefaultSchema)
3232
}
3333
return c.ConnEx.Filename(schema)
3434
}
3535

3636
// Tables returns a list of table names in a schema
3737
func (c *Conn) Tables(schema string) []string {
3838
if schema == "" {
39-
return c.Tables(defaultSchema)
39+
return c.Tables(DefaultSchema)
4040
}
4141
return c.objectsInSchema(schema, "table")
4242
}
@@ -46,7 +46,7 @@ func (c *Conn) Count(schema, table string) int64 {
4646
if table == "" {
4747
return -1
4848
} else if schema == "" {
49-
return c.Count(defaultSchema, table)
49+
return c.Count(DefaultSchema, table)
5050
}
5151
result := int64(-1)
5252
if err := c.Exec(Q("SELECT COUNT(*) FROM ", N(table).WithSchema(schema)), func(row, k []string) bool {
@@ -63,7 +63,7 @@ func (c *Conn) Count(schema, table string) int64 {
6363
// ColumnsForTable returns the columns in a table
6464
func (c *Conn) ColumnsForTable(schema, table string) []SQColumn {
6565
if schema == "" {
66-
return c.ColumnsForTable(defaultSchema, table)
66+
return c.ColumnsForTable(DefaultSchema, table)
6767
}
6868
result := []SQColumn{}
6969
if err := c.Exec(Q("PRAGMA ", N(schema), ".table_info(", N(table), ")"), func(row, k []string) bool {
@@ -87,7 +87,7 @@ func (c *Conn) ColumnsForTable(schema, table string) []SQColumn {
8787
// ColumnsForIndex returns the indexes associated with a table
8888
func (c *Conn) ColumnsForIndex(schema, index string) []string {
8989
if schema == "" {
90-
return c.ColumnsForIndex(defaultSchema, index)
90+
return c.ColumnsForIndex(DefaultSchema, index)
9191
}
9292

9393
result := []string{}
@@ -105,7 +105,7 @@ func (c *Conn) IndexesForTable(schema, table string) []SQIndexView {
105105
if table == "" {
106106
return nil
107107
} else if schema == "" {
108-
return c.IndexesForTable(defaultSchema, table)
108+
return c.IndexesForTable(DefaultSchema, table)
109109
}
110110
result := []SQIndexView{}
111111
if err := c.ExecEx(Q("PRAGMA ", N(schema), ".index_list(", N(table), ")").Query(), func(row, col []string) bool {
@@ -140,7 +140,7 @@ func (c *Conn) IndexesForTable(schema, table string) []SQIndexView {
140140
// Views returns a list of view names in a schema
141141
func (c *Conn) Views(schema string) []string {
142142
if schema == "" {
143-
return c.Views(defaultSchema)
143+
return c.Views(DefaultSchema)
144144
}
145145
return c.objectsInSchema(schema, "view")
146146
}

pkg/sqlite3/sqlite3.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import (
1414
const (
1515
// DefaultFlags are the default flags for a new database connection
1616
DefaultFlags = SQFlag(sqlite3.SQLITE_OPEN_CREATE | sqlite3.SQLITE_OPEN_READWRITE)
17+
DefaultSchema = sqlite3.DefaultSchema
1718
defaultMemory = sqlite3.DefaultMemory
18-
defaultSchema = sqlite3.DefaultSchema
1919
tempSchema = "temp"
2020
)
2121

0 commit comments

Comments
 (0)