Skip to content

Commit cb04140

Browse files
committed
ADD: PRAGMA synchronous
1 parent 619236f commit cb04140

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ Boolean values can be one of:
8989
| Recursive Triggers | `_recursive_triggers` \| `_rt` | `boolean` | For more information see [PRAGMA recursive_triggers](https://www.sqlite.org/pragma.html#pragma_recursive_triggers) |
9090
| Secure Delete | `_secure_delete` | `boolean` \| `FAST` | For more information see [PRAGMA secure_delete](https://www.sqlite.org/pragma.html#pragma_secure_delete) |
9191
| Shared-Cache Mode | `cache` | <ul><li>shared</li><li>private</li></ul> | Set cache mode for more information see [sqlite.org](https://www.sqlite.org/sharedcache.html) |
92+
| Synchronous | `_synchronous` \| `_sync` | <ul><li>0 \| OFF</li><li>1 \| NORMAL</li><li>2 \| FULL</li><li>3 \| EXTRA</li></ul> | For more information see [PRAGMA synchronous](https://www.sqlite.org/pragma.html#pragma_synchronous) |
9293
| Time Zone Location | `_loc` | auto | Specify location of time format. |
9394
| Transaction Lock | `_txlock` | <ul><li>immediate</li><li>deferred</li><li>exclusive</li></ul> | Specify locking behavior for transactions. |
9495

sqlite3.go

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,10 @@ func errorString(err Error) string {
866866
// When secure_delete is on, SQLite overwrites deleted content with zeros.
867867
// https://www.sqlite.org/pragma.html#pragma_secure_delete
868868
//
869+
// _synchronous=X | _sync=X
870+
// Change the setting of the "synchronous" flag.
871+
// https://www.sqlite.org/pragma.html#pragma_synchronous
872+
//
869873
// _vacuum=X
870874
// 0 | none - Auto Vacuum disabled
871875
// 1 | full - Auto Vacuum FULL
@@ -894,6 +898,7 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
894898
queryOnly := -1
895899
recursiveTriggers := -1
896900
secureDelete := "DEFAULT"
901+
synchronousMode := "NORMAL"
897902

898903
pos := strings.IndexRune(dsn, '?')
899904
if pos >= 1 {
@@ -1057,8 +1062,14 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
10571062
//
10581063
if val := params.Get("_journal"); val != "" {
10591064
switch strings.ToUpper(val) {
1060-
case "DELETE", "TRUNCATE", "PERSIST", "MEMORY", "WAL", "OFF":
1065+
case "DELETE", "TRUNCATE", "PERSIST", "MEMORY", "OFF":
1066+
journalMode = strings.ToUpper(val)
1067+
case "WAL":
10611068
journalMode = strings.ToUpper(val)
1069+
1070+
// For WAL Mode set Synchronous Mode to 'NORMAL'
1071+
// See https://www.sqlite.org/pragma.html#pragma_synchronous
1072+
synchronousMode = "NORMAL"
10621073
default:
10631074
return nil, fmt.Errorf("Invalid _journal: %v, expecting value of 'DELETE TRUNCATE PERSIST MEMORY WAL OFF'", val)
10641075
}
@@ -1131,6 +1142,26 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
11311142
}
11321143
}
11331144

1145+
// Synchronous Mode (_synchronous | _sync)
1146+
//
1147+
// https://www.sqlite.org/pragma.html#pragma_synchronous
1148+
//
1149+
pkey = "" // Reset pkey
1150+
if _, ok := params["_synchronous"]; ok {
1151+
pkey = "_synchronous"
1152+
}
1153+
if _, ok := params["_sync"]; ok {
1154+
pkey = "_sync"
1155+
}
1156+
if val := params.Get(pkey); val != "" {
1157+
switch strings.ToUpper(val) {
1158+
case "0", "OFF", "1", "NORMAL", "2", "FULL", "3", "EXTRA":
1159+
synchronousMode = strings.ToUpper(val)
1160+
default:
1161+
return nil, fmt.Errorf("Invalid _synchronous: %v, expecting value of '0 OFF 1 NORMAL 2 FULL 3 EXTRA'", val)
1162+
}
1163+
}
1164+
11341165
if !strings.HasPrefix(dsn, "file:") {
11351166
dsn = dsn[:pos]
11361167
}
@@ -1248,6 +1279,14 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
12481279
}
12491280
}
12501281

1282+
// Synchronous Mode
1283+
//
1284+
// Because default is NORMAL this statement is always executed
1285+
if err := exec(fmt.Sprintf("PRAGMA synchronous = %s;", synchronousMode)); err != nil {
1286+
C.sqlite3_close_v2(db)
1287+
return nil, err
1288+
}
1289+
12511290
conn := &SQLiteConn{db: db, loc: loc, txlock: txlock}
12521291

12531292
if len(d.Extensions) > 0 {

0 commit comments

Comments
 (0)