Skip to content

Commit e02bbc0

Browse files
committed
ADD: PRAGMA journal_mode
1 parent a159b5d commit e02bbc0

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ Boolean values can be one of:
8080
| Defer Foreign Keys | `_defer_foreign_keys` \| `_defer_fk` | `boolean` | For more information see [PRAGMA defer_foreign_keys](https://www.sqlite.org/pragma.html#pragma_defer_foreign_keys) |
8181
| Foreign Keys | `_foreign_keys` \| `_fk` | `boolean` | For more information see [PRAGMA foreign_keys](https://www.sqlite.org/pragma.html#pragma_foreign_keys) |
8282
| Ignore CHECK Constraints | `_ignore_check_constraints` | `boolean` | For more information see [PRAGMA ignore_check_constraints](https://www.sqlite.org/pragma.html#pragma_ignore_check_constraints) |
83+
| Journal Mode | `_journal` | <ul><li>DELETE</li><li>TRUNCATE</li><li>PERSIST</li><li>MEMORY</li><li>WAL</li><li>OFF</li></ul> | For more information see [PRAGMA journal_mode](https://www.sqlite.org/pragma.html#pragma_journal_mode) |
8384
| Mode | `mode` | <ul><li>ro</li><li>rw</li><li>rwc</li><li>memory</li></ul> | Access Mode of the database. For more information see [SQLite Open](https://www.sqlite.org/c3ref/open.html) |
8485
| Mutex Locking | `_mutex` | <ul><li>no</li><li>full</li></ul> | Specify mutex mode. |
8586
| Recursive Triggers | `_recursive_triggers` \| `_rt` | `boolean` | For more information see [PRAGMA recursive_triggers](https://www.sqlite.org/pragma.html#pragma_recursive_triggers) |

sqlite3.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,10 @@ func errorString(err Error) string {
838838
// This pragma enables or disables the enforcement of CHECK constraints.
839839
// The default setting is off, meaning that CHECK constraints are enforced by default.
840840
//
841+
// _journal=MODE
842+
// Set journal mode for the databases associated with the current connection.
843+
// https://www.sqlite.org/pragma.html#pragma_journal_mode
844+
//
841845
// _recursive_triggers=Boolean | _rt=Boolean
842846
// Enable or disable recursive triggers.
843847
//
@@ -863,6 +867,7 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
863867
caseSensitiveLike := -1
864868
deferForeignKeys := -1
865869
ignoreCheckConstraints := -1
870+
journalMode := "DELETE"
866871
foreignKeys := -1
867872
recursiveTriggers := -1
868873

@@ -1022,6 +1027,19 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
10221027
}
10231028
}
10241029

1030+
// Journal Mode (_journal)
1031+
//
1032+
// https://www.sqlite.org/pragma.html#pragma_journal_mode
1033+
//
1034+
if val := params.Get("_journal"); val != "" {
1035+
switch strings.ToUpper(val) {
1036+
case "DELETE", "TRUNCATE", "PERSIST", "MEMORY", "WAL", "OFF":
1037+
journalMode = strings.ToUpper(val)
1038+
default:
1039+
return nil, fmt.Errorf("Invalid _journal: %v, expecting value of 'DELETE TRUNCATE PERSIST MEMORY WAL OFF'", val)
1040+
}
1041+
}
1042+
10251043
// Recursive Triggers (_recursive_triggers)
10261044
//
10271045
// https://www.sqlite.org/pragma.html#pragma_recursive_triggers
@@ -1118,6 +1136,13 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
11181136
}
11191137
}
11201138

1139+
// Journal Mode
1140+
// Because default Journal Mode is DELETE this PRAGMA can always be executed.
1141+
if err := exec(fmt.Sprintf("PRAGMA journal_mode = %s;", journalMode)); err != nil {
1142+
C.sqlite3_close_v2(db)
1143+
return nil, err
1144+
}
1145+
11211146
// Recursive Triggers
11221147
if recursiveTriggers > -1 {
11231148
if err := exec(fmt.Sprintf("PRAGMA recursive_triggers = %d;", recursiveTriggers)); err != nil {

0 commit comments

Comments
 (0)