From f14a7566f95301542aab53506f6b304e89865f5d Mon Sep 17 00:00:00 2001 From: Gert-Jan Timmer Date: Tue, 29 May 2018 13:19:40 +0200 Subject: [PATCH] ADD: PRAGMA locking_mode --- README.md | 1 + sqlite3.go | 29 ++++++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 19190e1..2e1312f 100644 --- a/README.md +++ b/README.md @@ -81,6 +81,7 @@ Boolean values can be one of: | Foreign Keys | `_foreign_keys` \| `_fk` | `boolean` | For more information see [PRAGMA foreign_keys](https://www.sqlite.org/pragma.html#pragma_foreign_keys) | | 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) | | Journal Mode | `_journal` | | For more information see [PRAGMA journal_mode](https://www.sqlite.org/pragma.html#pragma_journal_mode) | +| Locking Mode | `_locking` | | For more information see [PRAGMA locking_mode](https://www.sqlite.org/pragma.html#pragma_locking_mode) | | Mode | `mode` | | Access Mode of the database. For more information see [SQLite Open](https://www.sqlite.org/c3ref/open.html) | | Mutex Locking | `_mutex` | | Specify mutex mode. | | Recursive Triggers | `_recursive_triggers` \| `_rt` | `boolean` | For more information see [PRAGMA recursive_triggers](https://www.sqlite.org/pragma.html#pragma_recursive_triggers) | diff --git a/sqlite3.go b/sqlite3.go index 3855882..b0a330d 100644 --- a/sqlite3.go +++ b/sqlite3.go @@ -842,6 +842,11 @@ func errorString(err Error) string { // Set journal mode for the databases associated with the current connection. // https://www.sqlite.org/pragma.html#pragma_journal_mode // +// _locking=X +// Sets the database connection locking-mode. +// The locking-mode is either NORMAL or EXCLUSIVE. +// https://www.sqlite.org/pragma.html#pragma_locking_mode +// // _recursive_triggers=Boolean | _rt=Boolean // Enable or disable recursive triggers. // @@ -866,9 +871,10 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) { busyTimeout := 5000 caseSensitiveLike := -1 deferForeignKeys := -1 + foreignKeys := -1 ignoreCheckConstraints := -1 journalMode := "DELETE" - foreignKeys := -1 + lockingMode := "NORMAL" recursiveTriggers := -1 pos := strings.IndexRune(dsn, '?') @@ -1040,6 +1046,19 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) { } } + // Locking Mode (_locking) + // + // https://www.sqlite.org/pragma.html#pragma_locking_mode + // + if val := params.Get("_locking"); val != "" { + switch strings.ToUpper(val) { + case "NORMAL", "EXCLUSIVE": + lockingMode = strings.ToUpper(val) + default: + return nil, fmt.Errorf("Invalid _locking: %v, expecting value of 'NORMAL EXCLUSIVE", val) + } + } + // Recursive Triggers (_recursive_triggers) // // https://www.sqlite.org/pragma.html#pragma_recursive_triggers @@ -1143,6 +1162,14 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) { return nil, err } + // Locking Mode + // Because the default is NORMAL and this is not changed in this package + // by using the compile time SQLITE_DEFAULT_LOCKING_MODE this PRAGMA can always be executed + if err := exec(fmt.Sprintf("PRAGMA locking_mode = %s;", lockingMode)); err != nil { + C.sqlite3_close_v2(db) + return nil, err + } + // Recursive Triggers if recursiveTriggers > -1 { if err := exec(fmt.Sprintf("PRAGMA recursive_triggers = %d;", recursiveTriggers)); err != nil {