Skip to content

Commit efc41bc

Browse files
committed
Update Connection Options
* Rewrite order of options * ADD: PRAGMA auto_vacuum * ADD: Multi Boolean values * UPD: README * FIX: Case-Sensitive values * Reduced code for: - foreign_keys - recursive_triggers
1 parent a5cc8a2 commit efc41bc

File tree

2 files changed

+90
-46
lines changed

2 files changed

+90
-46
lines changed

README.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,20 @@ Options can be given using the following format: `KEYWORD=VALUE` and multiple op
6868

6969
This library supports dsn options of SQLite itself and provides additional options.
7070

71+
Boolean values can be one of:
72+
* `0` `no` `false` `off`
73+
* `1` `yes` `true` `on`
74+
7175
| Name | Key | Value(s) | Description |
7276
|------|-----|----------|-------------|
77+
| 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) |
78+
| Busy Timeout | _busy_timeout | `int` | Specify value for sqlite3_busy_timeout. |
79+
| Foreign Keys | _foreign_keys | `boolean` | Enable or disable enforcement of foreign keys. |
80+
| Mutex Locking | _mutex | <ul><li>no</li><li>full</li></ul> | Specify mutex mode. |
81+
| Recursive Triggers | _recursive_triggers | `boolean` | Enable or disable recursive triggers. |
7382
| 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) |
7483
| Time Zone Location | _loc | auto | Specify location of time format. |
75-
| Busy Timeout | _busy_timeout | `int` | Specify value for sqlite3_busy_timeout. |
7684
| Transaction Lock | _txlock | <ul><li>immediate</li><li>deferred</li><li>exclusive</li></ul> | Specify locking behavior for transactions. |
77-
| Foreign Keys | _foreign_keys | <ul><li>0</li><li>1</li></ul> | Enable or disable enforcement of foreign keys. |
78-
| Recursive Triggers | _recursive_triggers | <ul><li>0</li><li>1</li></ul> | Enable or disable recursive triggers. |
79-
| Mutex Locking | _mutex | <ul><li>no</li><li>full</li></ul> | Specify mutex mode. |
8085

8186
## DSN Examples
8287

sqlite3.go

Lines changed: 81 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,14 @@ func errorString(err Error) string {
779779
}
780780

781781
// Open database and return a new connection.
782+
//
783+
// A pragma can take either zero or one argument.
784+
// The argument is may be either in parentheses or it may be separated from
785+
// the pragma name by an equal sign. The two syntaxes yield identical results.
786+
// In many pragmas, the argument is a boolean. The boolean can be one of:
787+
// 1 yes true on
788+
// 0 no false off
789+
//
782790
// You can specify a DSN string using a URI as the filename.
783791
// test.db
784792
// file:test.db?cache=shared&mode=memory
@@ -787,28 +795,43 @@ func errorString(err Error) string {
787795
// go-sqlite3 adds the following query parameters to those used by SQLite:
788796
// _loc=XXX
789797
// Specify location of time format. It's possible to specify "auto".
790-
// _busy_timeout=XXX
791-
// Specify value for sqlite3_busy_timeout.
798+
//
799+
// _mutex=XXX
800+
// Specify mutex mode. XXX can be "no", "full".
801+
//
792802
// _txlock=XXX
793803
// Specify locking behavior for transactions. XXX can be "immediate",
794804
// "deferred", "exclusive".
805+
//
806+
// _busy_timeout=XXX
807+
// Specify value for sqlite3_busy_timeout.
808+
//
795809
// _foreign_keys=X
796810
// Enable or disable enforcement of foreign keys. X can be 1 or 0.
811+
//
797812
// _recursive_triggers=X
798813
// Enable or disable recursive triggers. X can be 1 or 0.
799-
// _mutex=XXX
800-
// Specify mutex mode. XXX can be "no", "full".
814+
//
815+
// _vacuum=X
816+
// 0 | none - Auto Vacuum disabled
817+
// 1 | full - Auto Vacuum FULL
818+
// 2 | incremental - Auto Vacuum Incremental
801819
func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
802820
if C.sqlite3_threadsafe() == 0 {
803821
return nil, errors.New("sqlite library was not compiled for thread-safe operation")
804822
}
805823

824+
// Options
806825
var loc *time.Location
826+
mutex := C.int(C.SQLITE_OPEN_FULLMUTEX)
807827
txlock := "BEGIN"
828+
829+
// PRAGMA's
830+
autoVacuum := -1
808831
busyTimeout := 5000
809832
foreignKeys := -1
810833
recursiveTriggers := -1
811-
mutex := C.int(C.SQLITE_OPEN_FULLMUTEX)
834+
812835
pos := strings.IndexRune(dsn, '?')
813836
if pos >= 1 {
814837
params, err := url.ParseQuery(dsn[pos+1:])
@@ -828,13 +851,16 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
828851
}
829852
}
830853

831-
// _busy_timeout
832-
if val := params.Get("_busy_timeout"); val != "" {
833-
iv, err := strconv.ParseInt(val, 10, 64)
834-
if err != nil {
835-
return nil, fmt.Errorf("Invalid _busy_timeout: %v: %v", val, err)
854+
// _mutex
855+
if val := params.Get("_mutex"); val != "" {
856+
switch val {
857+
case "no":
858+
mutex = C.SQLITE_OPEN_NOMUTEX
859+
case "full":
860+
mutex = C.SQLITE_OPEN_FULLMUTEX
861+
default:
862+
return nil, fmt.Errorf("Invalid _mutex: %v", val)
836863
}
837-
busyTimeout = int(iv)
838864
}
839865

840866
// _txlock
@@ -851,42 +877,53 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
851877
}
852878
}
853879

880+
// auto_vacuum
881+
if val := params.Get("_vacuum"); val != "" {
882+
switch strings.ToLower(val) {
883+
case "0", "none":
884+
autoVacuum = 0
885+
case "1", "full":
886+
autoVacuum = 1
887+
case "2", "incremental":
888+
autoVacuum = 2
889+
default:
890+
return nil, fmt.Errorf("Invalid _vacuum: %v", val)
891+
}
892+
}
893+
894+
// _busy_timeout
895+
if val := params.Get("_busy_timeout"); val != "" {
896+
iv, err := strconv.ParseInt(val, 10, 64)
897+
if err != nil {
898+
return nil, fmt.Errorf("Invalid _busy_timeout: %v: %v", val, err)
899+
}
900+
busyTimeout = int(iv)
901+
}
902+
854903
// _foreign_keys
855904
if val := params.Get("_foreign_keys"); val != "" {
856-
switch val {
857-
case "1":
858-
foreignKeys = 1
859-
case "0":
905+
switch strings.ToLower(val) {
906+
case "0", "no", "false", "off":
860907
foreignKeys = 0
908+
case "1", "yes", "true", "on":
909+
foreignKeys = 1
861910
default:
862911
return nil, fmt.Errorf("Invalid _foreign_keys: %v", val)
863912
}
864913
}
865914

866915
// _recursive_triggers
867916
if val := params.Get("_recursive_triggers"); val != "" {
868-
switch val {
869-
case "1":
870-
recursiveTriggers = 1
871-
case "0":
917+
switch strings.ToLower(val) {
918+
case "0", "no", "false", "off":
872919
recursiveTriggers = 0
920+
case "1", "yes", "true", "on":
921+
recursiveTriggers = 1
873922
default:
874923
return nil, fmt.Errorf("Invalid _recursive_triggers: %v", val)
875924
}
876925
}
877926

878-
// _mutex
879-
if val := params.Get("_mutex"); val != "" {
880-
switch val {
881-
case "no":
882-
mutex = C.SQLITE_OPEN_NOMUTEX
883-
case "full":
884-
mutex = C.SQLITE_OPEN_FULLMUTEX
885-
default:
886-
return nil, fmt.Errorf("Invalid _mutex: %v", val)
887-
}
888-
}
889-
890927
if !strings.HasPrefix(dsn, "file:") {
891928
dsn = dsn[:pos]
892929
}
@@ -920,24 +957,26 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
920957
}
921958
return nil
922959
}
923-
if foreignKeys == 0 {
924-
if err := exec("PRAGMA foreign_keys = OFF;"); err != nil {
925-
C.sqlite3_close_v2(db)
926-
return nil, err
927-
}
928-
} else if foreignKeys == 1 {
929-
if err := exec("PRAGMA foreign_keys = ON;"); err != nil {
960+
961+
// Auto Vacuum
962+
if autoVacuum > -1 {
963+
if err := exec(fmt.Sprintf("PRAGMA auto_vacuum = %d;", autoVacuum)); err != nil {
930964
C.sqlite3_close_v2(db)
931965
return nil, err
932966
}
933967
}
934-
if recursiveTriggers == 0 {
935-
if err := exec("PRAGMA recursive_triggers = OFF;"); err != nil {
968+
969+
// Forgein Keys
970+
if foreignKeys > -1 {
971+
if err := exec(fmt.Sprintf("PRAGMA foreign_keys = %d;", foreignKeys)); err != nil {
936972
C.sqlite3_close_v2(db)
937973
return nil, err
938974
}
939-
} else if recursiveTriggers == 1 {
940-
if err := exec("PRAGMA recursive_triggers = ON;"); err != nil {
975+
}
976+
977+
// Recursive Triggers
978+
if recursiveTriggers > -1 {
979+
if err := exec(fmt.Sprintf("PRAGMA recursive_triggers = %d;", recursiveTriggers)); err != nil {
941980
C.sqlite3_close_v2(db)
942981
return nil, err
943982
}

0 commit comments

Comments
 (0)