ADD: PRAGMA defer_foreign_keys

This commit is contained in:
Gert-Jan Timmer 2018-05-29 12:09:56 +02:00
parent 4ab48f1935
commit 7393095f07
2 changed files with 35 additions and 0 deletions

View File

@ -77,6 +77,7 @@ Boolean values can be one of:
| Auto Vacuum | `_vacuum` | <ul><li>`0` \| `none`</li><li>`1` \| `full`</li><li>`2` \| `incremental`</li></ul> | For more information see [PRAGMA auto_vacuum](https://www.sqlite.org/pragma.html#pragma_auto_vacuum) | | Auto Vacuum | `_vacuum` | <ul><li>`0` \| `none`</li><li>`1` \| `full`</li><li>`2` \| `incremental`</li></ul> | For more information see [PRAGMA auto_vacuum](https://www.sqlite.org/pragma.html#pragma_auto_vacuum) |
| Busy Timeout | `_busy_timeout` \| `_timeout` | `int` | Specify value for sqlite3_busy_timeout. For more information see [PRAGMA busy_timeout](https://www.sqlite.org/pragma.html#pragma_busy_timeout) | | Busy Timeout | `_busy_timeout` \| `_timeout` | `int` | Specify value for sqlite3_busy_timeout. For more information see [PRAGMA busy_timeout](https://www.sqlite.org/pragma.html#pragma_busy_timeout) |
| Case Sensitive LIKE | `_cslike` | `boolean` | For more information see [PRAGMA case_sensitive_like](https://www.sqlite.org/pragma.html#pragma_case_sensitive_like) | | Case Sensitive LIKE | `_cslike` | `boolean` | For more information see [PRAGMA case_sensitive_like](https://www.sqlite.org/pragma.html#pragma_case_sensitive_like) |
| 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) |
| Foreign Keys | `_foreign_keys` \| `_fk` | `boolean` | For more information see [PRAGMA foreign_keys](https://www.sqlite.org/pragma.html#pragma_foreign_keys) | | Foreign Keys | `_foreign_keys` \| `_fk` | `boolean` | For more information see [PRAGMA foreign_keys](https://www.sqlite.org/pragma.html#pragma_foreign_keys) |
| Mutex Locking | `_mutex` | <ul><li>no</li><li>full</li></ul> | Specify mutex mode. | | Mutex Locking | `_mutex` | <ul><li>no</li><li>full</li></ul> | 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) | | Recursive Triggers | `_recursive_triggers` \| `_rt` | `boolean` | For more information see [PRAGMA recursive_triggers](https://www.sqlite.org/pragma.html#pragma_recursive_triggers) |

View File

@ -810,6 +810,9 @@ func errorString(err Error) string {
// Default or disabled the LIKE operation is case-insensitive. // Default or disabled the LIKE operation is case-insensitive.
// When enabling this options behaviour of LIKE will become case-sensitive. // When enabling this options behaviour of LIKE will become case-sensitive.
// //
// _defer_foreign_keys=Boolean | _defer_fk=Boolean
// Defer Foreign Keys until outermost transaction is committed.
//
// _foreign_keys=Boolean | _fk=Boolean // _foreign_keys=Boolean | _fk=Boolean
// Enable or disable enforcement of foreign keys. // Enable or disable enforcement of foreign keys.
// //
@ -836,6 +839,7 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
autoVacuum := -1 autoVacuum := -1
busyTimeout := 5000 busyTimeout := 5000
caseSensitiveLike := -1 caseSensitiveLike := -1
deferForeignKeys := -1
foreignKeys := -1 foreignKeys := -1
recursiveTriggers := -1 recursiveTriggers := -1
@ -935,6 +939,28 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
} }
} }
// Defer Foreign Keys
//
// https://www.sqlite.org/pragma.html#pragma_defer_foreign_keys
//
pkey = "" // Reset pkey
if _, ok := params["_defer_foreign_keys"]; ok {
pkey = "_defer_foreign_keys"
}
if _, ok := params["_defer_fk"]; ok {
pkey = "_defer_fk"
}
if val := params.Get(pkey); val != "" {
switch strings.ToLower(val) {
case "0", "no", "false", "off":
deferForeignKeys = 0
case "1", "yes", "true", "on":
deferForeignKeys = 1
default:
return nil, fmt.Errorf("Invalid _foreign_keys: %v, expecting boolean value of '0 1 false true no yes off on'", val)
}
}
// Foreign Keys (_foreign_keys) // Foreign Keys (_foreign_keys)
// //
// https://www.sqlite.org/pragma.html#pragma_foreign_keys // https://www.sqlite.org/pragma.html#pragma_foreign_keys
@ -1028,6 +1054,14 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
} }
} }
// Defer Foreign Keys
if deferForeignKeys > -1 {
if err := exec(fmt.Sprintf("PRAGMA defer_foreign_keys = %d;", deferForeignKeys)); err != nil {
C.sqlite3_close_v2(db)
return nil, err
}
}
// Forgein Keys // Forgein Keys
if foreignKeys > -1 { if foreignKeys > -1 {
if err := exec(fmt.Sprintf("PRAGMA foreign_keys = %d;", foreignKeys)); err != nil { if err := exec(fmt.Sprintf("PRAGMA foreign_keys = %d;", foreignKeys)); err != nil {