Skip to content

Commit d86b495

Browse files
authored
sqlite: add sync and journal funcs, docs (#14970)
1 parent f0ce7fb commit d86b495

File tree

2 files changed

+66
-7
lines changed

2 files changed

+66
-7
lines changed

vlib/sqlite/README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@ library installed on your system.
2626

2727
# Performance Tips
2828

29-
When performing a large amount of database calls (i.e. INSERTS), significant performance increase
30-
can be obtained by issuing to sqlite the following pragma commands.
29+
When performing a large amount of database calls (i.e. INSERTS), significant performance increase can be obtained by controlling the synchronization and journal modes.
3130

32-
```
33-
db.exec('pragma synchronous = off;')
34-
db.exec('pragma journal_mode = MEMORY;')
31+
For instance,
32+
```v
33+
db := sqlite.connect('foo.db') or {panic(err)}
34+
db.synchronization_mode(sqlite.SyncMode.off)
35+
db.journal_mode(sqlite.JournalMode.memory)
3536
```

vlib/sqlite/sqlite.v

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,20 @@ pub const (
2121
sqlite_done = 101
2222
)
2323

24+
pub enum SyncMode {
25+
off
26+
normal
27+
full
28+
}
29+
30+
pub enum JournalMode {
31+
off
32+
delete
33+
truncate
34+
persist
35+
memory
36+
}
37+
2438
struct C.sqlite3 {
2539
}
2640

@@ -219,7 +233,8 @@ pub fn (db DB) error_message(code int, query string) IError {
219233
})
220234
}
221235

222-
// In case you don't expect any result, but still want an error code
236+
// Execute a query returning only the result code.
237+
// In case you don't expect any row results, but still want a result code.
223238
// e.g. INSERT INTO ... VALUES (...)
224239
pub fn (db DB) exec_none(query string) int {
225240
stmt := &C.sqlite3_stmt(0)
@@ -235,11 +250,54 @@ pub fn (db DB) exec_param(query string, param string) []Row {
235250
}
236251
*/
237252

253+
// Issue a "create table if not exists" command to the db.
254+
// Creates table named 'table_name', with columns generated from 'columns' array.
255+
// Default columns type will be TEXT.
238256
pub fn (db DB) create_table(table_name string, columns []string) {
239257
db.exec('create table if not exists $table_name (' + columns.join(',\n') + ')')
240258
}
241259

242-
// Set a busy timeout in milliseconds https://www.sqlite.org/c3ref/busy_timeout.html
260+
// Set a busy timeout in milliseconds.
261+
// Sleeps for a specified amount of time when a table is locked. The handler
262+
// will sleep multiple times until at least "ms" milliseconds of sleeping have accumulated.
263+
// (see https://www.sqlite.org/c3ref/busy_timeout.html)
243264
pub fn (db DB) busy_timeout(ms int) int {
244265
return C.sqlite3_busy_timeout(db.conn, ms)
245266
}
267+
268+
// Sets disk synchronization mode,
269+
// which controls how aggressively SQLite will write data to physical storage.
270+
// off: No syncs at all. (fastest)
271+
// normal: Sync after each sequence of critical disk operations.
272+
// full: Sync after each critical disk operation (slowest).
273+
pub fn (db DB) synchronization_mode(sync_mode SyncMode) {
274+
if sync_mode == .off {
275+
db.exec('pragma synchronous = OFF;')
276+
} else if sync_mode == .full {
277+
db.exec('pragma synchronous = FULL;')
278+
} else {
279+
db.exec('pragma synchronous = NORMAL;')
280+
}
281+
}
282+
283+
// Controls how the journal file is stored and processed.
284+
// off: No journal record is kept. (fastest)
285+
// memory: Journal record is held in memory, rather than on disk.
286+
// delete: At the conclusion of a transaction, journal file is deleted.
287+
// truncate: Journal file is truncated to a length of zero bytes.
288+
// persist: Journal file is left in place, but the header is overwritten to indicate journal is no longer valid.
289+
pub fn (db DB) journal_mode(journal_mode JournalMode) {
290+
if journal_mode == .off {
291+
db.exec('pragma journal_mode = OFF;')
292+
} else if journal_mode == .delete {
293+
db.exec('pragma journal_mode = DELETE;')
294+
} else if journal_mode == .truncate {
295+
db.exec('pragma journal_mode = TRUNCATE;')
296+
} else if journal_mode == .persist {
297+
db.exec('pragma journal_mode = PERSIST;')
298+
} else if journal_mode == .memory {
299+
db.exec('pragma journal_mode = MEMORY;')
300+
} else {
301+
db.exec('pragma journal_mode = MEMORY;')
302+
}
303+
}

0 commit comments

Comments
 (0)