Skip to content

Commit f14a756

Browse files
committed
ADD: PRAGMA locking_mode
1 parent e02bbc0 commit f14a756

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ Boolean values can be one of:
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) |
8383
| 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) |
84+
| Locking Mode | `_locking` | <ul><li>NORMAL</li><li>EXCLUSIVE</li></ul> | For more information see [PRAGMA locking_mode](https://www.sqlite.org/pragma.html#pragma_locking_mode) |
8485
| 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) |
8586
| Mutex Locking | `_mutex` | <ul><li>no</li><li>full</li></ul> | Specify mutex mode. |
8687
| 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: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,11 @@ func errorString(err Error) string {
842842
// Set journal mode for the databases associated with the current connection.
843843
// https://www.sqlite.org/pragma.html#pragma_journal_mode
844844
//
845+
// _locking=X
846+
// Sets the database connection locking-mode.
847+
// The locking-mode is either NORMAL or EXCLUSIVE.
848+
// https://www.sqlite.org/pragma.html#pragma_locking_mode
849+
//
845850
// _recursive_triggers=Boolean | _rt=Boolean
846851
// Enable or disable recursive triggers.
847852
//
@@ -866,9 +871,10 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
866871
busyTimeout := 5000
867872
caseSensitiveLike := -1
868873
deferForeignKeys := -1
874+
foreignKeys := -1
869875
ignoreCheckConstraints := -1
870876
journalMode := "DELETE"
871-
foreignKeys := -1
877+
lockingMode := "NORMAL"
872878
recursiveTriggers := -1
873879

874880
pos := strings.IndexRune(dsn, '?')
@@ -1040,6 +1046,19 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
10401046
}
10411047
}
10421048

1049+
// Locking Mode (_locking)
1050+
//
1051+
// https://www.sqlite.org/pragma.html#pragma_locking_mode
1052+
//
1053+
if val := params.Get("_locking"); val != "" {
1054+
switch strings.ToUpper(val) {
1055+
case "NORMAL", "EXCLUSIVE":
1056+
lockingMode = strings.ToUpper(val)
1057+
default:
1058+
return nil, fmt.Errorf("Invalid _locking: %v, expecting value of 'NORMAL EXCLUSIVE", val)
1059+
}
1060+
}
1061+
10431062
// Recursive Triggers (_recursive_triggers)
10441063
//
10451064
// https://www.sqlite.org/pragma.html#pragma_recursive_triggers
@@ -1143,6 +1162,14 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
11431162
return nil, err
11441163
}
11451164

1165+
// Locking Mode
1166+
// Because the default is NORMAL and this is not changed in this package
1167+
// by using the compile time SQLITE_DEFAULT_LOCKING_MODE this PRAGMA can always be executed
1168+
if err := exec(fmt.Sprintf("PRAGMA locking_mode = %s;", lockingMode)); err != nil {
1169+
C.sqlite3_close_v2(db)
1170+
return nil, err
1171+
}
1172+
11461173
// Recursive Triggers
11471174
if recursiveTriggers > -1 {
11481175
if err := exec(fmt.Sprintf("PRAGMA recursive_triggers = %d;", recursiveTriggers)); err != nil {

0 commit comments

Comments
 (0)