Skip to content

Commit e7f5604

Browse files
committed
Stricter vtabs, get VFS name.
1 parent e500839 commit e7f5604

6 files changed

Lines changed: 42 additions & 12 deletions

File tree

‎config.go‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,16 +157,20 @@ func (c *Conn) FileControl(schema string, op FcntlOpcode, arg ...any) (any, erro
157157
stk_t(op), stk_t(ptr)))
158158
ret = util.Read32[vfs.LockLevel](c.mod, ptr)
159159

160-
case FCNTL_VFS_POINTER:
160+
case FCNTL_VFSNAME, FCNTL_VFS_POINTER:
161161
rc = res_t(c.call("sqlite3_file_control",
162162
stk_t(c.handle), stk_t(schemaPtr),
163-
stk_t(op), stk_t(ptr)))
163+
stk_t(FCNTL_VFS_POINTER), stk_t(ptr)))
164164
if rc == _OK {
165165
const zNameOffset = 16
166166
ptr = util.Read32[ptr_t](c.mod, ptr)
167167
ptr = util.Read32[ptr_t](c.mod, ptr+zNameOffset)
168168
name := util.ReadString(c.mod, ptr, _MAX_NAME)
169-
ret = vfs.Find(name)
169+
if op == FCNTL_VFS_POINTER {
170+
ret = vfs.Find(name)
171+
} else {
172+
ret = name
173+
}
170174
}
171175

172176
case FCNTL_FILE_POINTER, FCNTL_JOURNAL_POINTER:

‎const.go‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,14 +173,15 @@ const (
173173

174174
// PrepareFlag is a flag that can be passed to [Conn.PrepareFlags].
175175
//
176-
// https://sqlite.org/c3ref/c_prepare_normalize.html
176+
// https://sqlite.org/c3ref/c_prepare_dont_log.html
177177
type PrepareFlag uint32
178178

179179
const (
180180
PREPARE_PERSISTENT PrepareFlag = 0x01
181181
PREPARE_NORMALIZE PrepareFlag = 0x02
182182
PREPARE_NO_VTAB PrepareFlag = 0x04
183183
PREPARE_DONT_LOG PrepareFlag = 0x10
184+
PREPARE_FROM_DDL PrepareFlag = 0x20
184185
)
185186

186187
// FunctionFlag is a flag that can be passed to
@@ -280,6 +281,7 @@ const (
280281
FCNTL_CHUNK_SIZE FcntlOpcode = 6
281282
FCNTL_FILE_POINTER FcntlOpcode = 7
282283
FCNTL_PERSIST_WAL FcntlOpcode = 10
284+
FCNTL_VFSNAME FcntlOpcode = 12
283285
FCNTL_POWERSAFE_OVERWRITE FcntlOpcode = 13
284286
FCNTL_VFS_POINTER FcntlOpcode = 27
285287
FCNTL_JOURNAL_POINTER FcntlOpcode = 28
@@ -307,6 +309,7 @@ const (
307309
LIMIT_VARIABLE_NUMBER LimitCategory = 9
308310
LIMIT_TRIGGER_DEPTH LimitCategory = 10
309311
LIMIT_WORKER_THREADS LimitCategory = 11
312+
LIMIT_PARSER_DEPTH LimitCategory = 12
310313
)
311314

312315
// AuthorizerActionCode are the integer action codes

‎ext/pivot/pivot.go‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,13 @@ func declare(db *sqlite3.Conn, _, _, _ string, arg ...string) (ret *table, err e
4343

4444
// Row key query.
4545
t.scan = "SELECT * FROM\n" + arg[0]
46-
stmt, _, err := db.Prepare(t.scan)
46+
stmt, tail, err := db.PrepareFlags(t.scan, sqlite3.PREPARE_FROM_DDL)
4747
if err != nil {
4848
return nil, err
4949
}
50+
if tail != "" {
51+
return nil, util.TailErr
52+
}
5053
defer stmt.Close()
5154

5255
t.keys = make([]string, stmt.ColumnCount())
@@ -62,10 +65,13 @@ func declare(db *sqlite3.Conn, _, _, _ string, arg ...string) (ret *table, err e
6265
stmt.Close()
6366

6467
// Column definition query.
65-
stmt, _, err = db.Prepare("SELECT * FROM\n" + arg[1])
68+
stmt, tail, err = db.PrepareFlags("SELECT * FROM\n"+arg[1], sqlite3.PREPARE_FROM_DDL)
6669
if err != nil {
6770
return nil, err
6871
}
72+
if tail != "" {
73+
return nil, util.TailErr
74+
}
6975

7076
if stmt.ColumnCount() != 2 {
7177
return nil, util.ErrorString("pivot: column definition query expects 2 result columns")
@@ -83,10 +89,13 @@ func declare(db *sqlite3.Conn, _, _, _ string, arg ...string) (ret *table, err e
8389

8490
// Pivot cell query.
8591
t.cell = "SELECT * FROM\n" + arg[2]
86-
stmt, _, err = db.Prepare(t.cell)
92+
stmt, tail, err = db.PrepareFlags(t.cell, sqlite3.PREPARE_FROM_DDL)
8793
if err != nil {
8894
return nil, err
8995
}
96+
if tail != "" {
97+
return nil, util.TailErr
98+
}
9099

91100
if stmt.ColumnCount() != 1 {
92101
return nil, util.ErrorString("pivot: cell query expects 1 result columns")
@@ -187,7 +196,7 @@ func (c *cursor) Filter(idxNum int, idxStr string, arg ...sqlite3.Value) error {
187196
return err
188197
}
189198

190-
c.scan, _, err = c.table.db.Prepare(idxStr)
199+
c.scan, _, err = c.table.db.PrepareFlags(idxStr, sqlite3.PREPARE_FROM_DDL)
191200
if err != nil {
192201
return err
193202
}
@@ -199,7 +208,7 @@ func (c *cursor) Filter(idxNum int, idxStr string, arg ...sqlite3.Value) error {
199208
}
200209

201210
if c.cell == nil {
202-
c.cell, _, err = c.table.db.Prepare(c.table.cell)
211+
c.cell, _, err = c.table.db.PrepareFlags(c.table.cell, sqlite3.PREPARE_PERSISTENT|sqlite3.PREPARE_FROM_DDL)
203212
if err != nil {
204213
return err
205214
}

‎ext/statement/stmt.go‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,13 @@ func declare(db *sqlite3.Conn, _, _, _ string, arg ...string) (*table, error) {
3535

3636
sql := "SELECT * FROM\n" + arg[0]
3737

38-
stmt, _, err := db.PrepareFlags(sql, sqlite3.PREPARE_PERSISTENT)
38+
stmt, tail, err := db.PrepareFlags(sql, sqlite3.PREPARE_PERSISTENT|sqlite3.PREPARE_FROM_DDL)
3939
if err != nil {
4040
return nil, err
4141
}
42+
if tail != "" {
43+
return nil, util.TailErr
44+
}
4245

4346
var sep string
4447
var str strings.Builder
@@ -129,7 +132,7 @@ func (t *table) Open() (_ sqlite3.VTabCursor, err error) {
129132
if !t.inuse {
130133
t.inuse = true
131134
} else {
132-
stmt, _, err = t.stmt.Conn().Prepare(t.sql)
135+
stmt, _, err = t.stmt.Conn().PrepareFlags(t.sql, sqlite3.PREPARE_FROM_DDL)
133136
if err != nil {
134137
return nil, err
135138
}

‎tests/config_test.go‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,13 +189,23 @@ func TestConn_FileControl(t *testing.T) {
189189
}
190190
})
191191

192+
t.Run("FCNTL_VFSNAME", func(t *testing.T) {
193+
o, err := db.FileControl("", sqlite3.FCNTL_VFSNAME)
194+
if err != nil {
195+
t.Fatal(err)
196+
}
197+
if o != "os" {
198+
t.Errorf(`got %q, want "os"`, o)
199+
}
200+
})
201+
192202
t.Run("FCNTL_VFS_POINTER", func(t *testing.T) {
193203
o, err := db.FileControl("", sqlite3.FCNTL_VFS_POINTER)
194204
if err != nil {
195205
t.Fatal(err)
196206
}
197207
if o != vfs.Find("os") {
198-
t.Errorf("got %v, want os", o)
208+
t.Errorf(`got %v, want "os"`, o)
199209
}
200210
})
201211

‎vfs/const.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ const (
239239
_FCNTL_RESET_CACHE _FcntlOpcode = 42
240240
_FCNTL_NULL_IO _FcntlOpcode = 43
241241
_FCNTL_BLOCK_ON_CONNECT _FcntlOpcode = 44
242+
_FCNTL_FILESTAT _FcntlOpcode = 45
242243
)
243244

244245
// https://sqlite.org/c3ref/c_shm_exclusive.html

0 commit comments

Comments
 (0)