Skip to content

Commit 9e4258b

Browse files
committed
Better fix #249.
1 parent b645721 commit 9e4258b

File tree

4 files changed

+24
-16
lines changed

4 files changed

+24
-16
lines changed

config.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,8 @@ func (c *Conn) WALCheckpoint(schema string, mode CheckpointMode) (nLog, nCkpt in
279279
nLogPtr := c.arena.new(ptrlen)
280280
nCkptPtr := c.arena.new(ptrlen)
281281
schemaPtr := c.arena.string(schema)
282+
283+
c.checkInterrupt()
282284
rc := res_t(c.call("sqlite3_wal_checkpoint_v2",
283285
stk_t(c.handle), stk_t(schemaPtr), stk_t(mode),
284286
stk_t(nLogPtr), stk_t(nCkptPtr)))
@@ -350,8 +352,6 @@ func (c *Conn) HardHeapLimit(n int64) int64 {
350352
}
351353

352354
// EnableChecksums enables checksums on a database.
353-
// If the database is in WAL mode,
354-
// you should shutdown and reopen all database connections before continuing.
355355
//
356356
// https://sqlite.org/cksumvfs.html
357357
func (c *Conn) EnableChecksums(schema string) error {
@@ -390,6 +390,6 @@ func (c *Conn) EnableChecksums(schema string) error {
390390
}
391391

392392
// Checkpoint the WAL.
393-
_, _, err = c.WALCheckpoint(schema, CHECKPOINT_RESTART)
393+
_, _, err = c.WALCheckpoint(schema, CHECKPOINT_FULL)
394394
return err
395395
}

conn.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,10 @@ func (c *Conn) Close() error {
185185
// https://sqlite.org/c3ref/exec.html
186186
func (c *Conn) Exec(sql string) error {
187187
defer c.arena.mark()()
188-
sqlPtr := c.arena.string(sql)
188+
textPtr := c.arena.string(sql)
189189

190190
c.checkInterrupt()
191-
rc := res_t(c.call("sqlite3_exec", stk_t(c.handle), stk_t(sqlPtr), 0, 0, 0))
191+
rc := res_t(c.call("sqlite3_exec", stk_t(c.handle), stk_t(textPtr), 0, 0, 0))
192192
return c.error(rc, sql)
193193
}
194194

vfs/cksm.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,7 @@ func (c cksmFile) ReadAt(p []byte, off int64) (n int, err error) {
4949
n, err = c.File.ReadAt(p, off)
5050
p = p[:n]
5151

52-
// SQLite is reading the header of a database file.
53-
if c.isDB && off == 0 && len(p) >= 100 &&
54-
bytes.HasPrefix(p, []byte("SQLite format 3\000")) {
52+
if isHeader(c.isDB, p, off) {
5553
c.init((*[100]byte)(p))
5654
}
5755

@@ -67,9 +65,7 @@ func (c cksmFile) ReadAt(p []byte, off int64) (n int, err error) {
6765
}
6866

6967
func (c cksmFile) WriteAt(p []byte, off int64) (n int, err error) {
70-
// SQLite is writing the first page of a database file.
71-
if (!c.isDB || off == 0) && sql3util.ValidPageSize(len(p)) &&
72-
bytes.HasPrefix(p, []byte("SQLite format 3\000")) {
68+
if isHeader(c.isDB, p, off) {
7369
c.init((*[100]byte)(p))
7470
}
7571

@@ -116,9 +112,11 @@ func (c cksmFile) fileControl(ctx context.Context, mod api.Module, op _FcntlOpco
116112
c.inCkpt = true
117113
case _FCNTL_CKPT_DONE:
118114
c.inCkpt = false
119-
}
120-
if rc := vfsFileControlImpl(ctx, mod, c, op, pArg); rc != _NOTFOUND {
121-
return rc
115+
case _FCNTL_PRAGMA:
116+
rc := vfsFileControlImpl(ctx, mod, c, op, pArg)
117+
if rc != _NOTFOUND {
118+
return rc
119+
}
122120
}
123121
return vfsFileControlImpl(ctx, mod, c.File, op, pArg)
124122
}
@@ -135,6 +133,14 @@ func (f *cksmFlags) init(header *[100]byte) {
135133
}
136134
}
137135

136+
func isHeader(isDB bool, p []byte, off int64) bool {
137+
check := sql3util.ValidPageSize(len(p))
138+
if isDB {
139+
check = off == 0 && len(p) >= 100
140+
}
141+
return check && bytes.HasPrefix(p, []byte("SQLite format 3\000"))
142+
}
143+
138144
func cksmCompute(a []byte) (cksm [8]byte) {
139145
var s1, s2 uint32
140146
for len(a) >= 8 {

vtab.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,10 @@ func implements[T any](typ reflect.Type) bool {
8080
// https://sqlite.org/c3ref/declare_vtab.html
8181
func (c *Conn) DeclareVTab(sql string) error {
8282
defer c.arena.mark()()
83-
sqlPtr := c.arena.string(sql)
84-
rc := res_t(c.call("sqlite3_declare_vtab", stk_t(c.handle), stk_t(sqlPtr)))
83+
textPtr := c.arena.string(sql)
84+
85+
c.checkInterrupt()
86+
rc := res_t(c.call("sqlite3_declare_vtab", stk_t(c.handle), stk_t(textPtr)))
8587
return c.error(rc)
8688
}
8789

0 commit comments

Comments
 (0)