@@ -7,73 +7,51 @@ import (
7
7
"github.com/jmoiron/sqlx"
8
8
)
9
9
10
- // defaults
11
10
const (
12
- //Possible libraries. Used in comparisons, such as when building connection string
13
- //pragmas .
11
+ //Possible SQLite libraries. These are used in comparisons, such as when building
12
+ //the connection string PRAGMAs .
14
13
sqliteLibraryMattn = "github.com/mattn/go-sqlite3"
15
14
sqliteLibraryModernc = "modernc.org/sqlite"
16
15
17
- //InMemoryFilePathRacy is the "path" to provide for the SQLite file when you want
18
- //to use an in-memory database instead of a filesystem file database. This is racy
19
- //because each "Connect" call to :memory: will open a brand new database.
16
+ //InMemoryFilePathRacy is the path to provide for SQLitePath when you want to use
17
+ //an in-memory database instead of a file on disk. This is racy because each call
18
+ //to Connect() will open a brand new database. If you only call Connect() once
19
+ //then this is safe to use.
20
20
//
21
21
//This is good for running tests since then each test runs with a separate
22
22
//in-memory db.
23
23
InMemoryFilePathRacy = ":memory:"
24
24
25
- //InMemoryFilePathRaceSafe is the " path" to provide for the SQLite file when you
26
- //want to use an in-memory database between multiple "Connect" calls . This is race
27
- //safe since multiple calls of " Connect" will connect to the same in-memory db ,
28
- //although connecting more than once to the same db would be very odd.
25
+ //InMemoryFilePathRaceSafe is the path to provide for SQLitePath when you want to
26
+ //use an in-memory database instead of a file on disk . This is race safe since
27
+ //multiple calls of Connect() will connect to the same in-memory database ,
28
+ //although connecting more than once to the same database would be very odd.
29
29
InMemoryFilePathRaceSafe = "file::memory:?cache=shared"
30
30
)
31
31
32
- // NewSQLiteConfig returns a config for connecting to a SQLite database.
33
- func NewSQLiteConfig (pathToFile string ) (cfg * Config ) {
34
- //The returned error can be ignored since it only returns if a bad db type is
35
- //provided but we are providing a known-good db type.
36
- cfg , _ = NewConfig (DBTypeSQLite )
37
-
38
- cfg .SQLitePath = pathToFile
39
- cfg .SQLitePragmas = sqliteDefaultPragmas
40
- cfg .UseDefaultTranslateFuncs ()
41
-
42
- return
43
- }
44
-
45
- // DefaultSQLiteConfig initializes the globally accessible package level config with
46
- // some defaults set.
47
- func DefaultSQLiteConfig (pathToFile string ) {
48
- cfg := NewSQLiteConfig (pathToFile )
49
- config = * cfg
50
- }
51
-
52
- // IsSQLite returns true if the database is a SQLite database. This is easier
53
- // than checking for equality against the Type field in the config.
54
- func (cfg * Config ) IsSQLite () bool {
55
- return cfg .Type == DBTypeSQLite
32
+ // IsSQLite returns true if the database is a SQLite database.
33
+ func (c * Config ) IsSQLite () bool {
34
+ return c .Type == DBTypeSQLite
56
35
}
57
36
58
- // IsSQLite returns true if the database is a SQLite database. This is easier
59
- // than checking for equality against the Type field in the config.
37
+ // IsSQLite returns true if the database is a SQLite database.
60
38
func IsSQLite () bool {
61
- return config .IsSQLite ()
39
+ return cfg .IsSQLite ()
62
40
}
63
41
64
- // GetSQLiteVersion returns the version of SQLite that is embedded into the app. This
65
- // works by creating a temporary in-memory SQLite database to run a query against. We
66
- // don't use the config or an already established connection because we may want to
67
- // get the SQLiter version before a database is connected to!
42
+ // GetSQLiteVersion returns the version of SQLite that is embedded into your app.
43
+ // This works by creating a temporary in-memory SQLite database to run a query
44
+ // against.
45
+ //
46
+ // A separate database connection is established because you might want to get the
47
+ // SQLite version before you call Connect(). This also just keeps things separate
48
+ // from your own connection.
68
49
func GetSQLiteVersion () (version string , err error ) {
69
50
//Get driver name based on SQLite library in use.
70
- driver , err := getDriver (DBTypeSQLite )
71
- if err != nil {
72
- return
73
- }
51
+ driver := getDriver (DBTypeSQLite )
74
52
75
53
//Connect.
76
- conn , err := sqlx .Open (driver , ":memory:" )
54
+ conn , err := sqlx .Open (driver , InMemoryFilePathRacy )
77
55
if err != nil {
78
56
return
79
57
}
@@ -89,66 +67,62 @@ func GetSQLiteVersion() (version string, err error) {
89
67
}
90
68
91
69
// GetSQLiteLibrary returns the SQLite library that was used to build the binary. The
92
- // library is set at build/run with -tags {mattn || modernc} .
70
+ // library is set at build/run with go build tags .
93
71
func GetSQLiteLibrary () string {
94
72
return sqliteLibrary
95
73
}
96
74
97
- // SQLitePragmasAsString builds the string of pragmas that should be appended to the
98
- // filename when connecting to a SQLite database. This is needed to set pragmas reliably
99
- // since pragmas must be set upon initially connecting to the database. The difficulty
100
- // in setting pragmas is that each SQLite library (mattn vs modernc) has a slighly
101
- // different format for setting pragmas. This takes the list of pragmas in SQLite query
102
- // format (PRAGMA busy_timeout = 5000) and translates them to the correct format for
103
- // the SQLite library in use.
104
- func (cfg * Config ) SQLitePragmasAsString () (filenamePragmaString string ) {
75
+ // pragmsQueriesToString takes SQLite PRAGMAs in query format and retuns them in the
76
+ // format needed to be appended to a SQLite database filepath per the in-use SQLite
77
+ // driver.
78
+ //
79
+ // SQLite PRAGMAs need to be set upon initially connecting to the database. The
80
+ // PRAGMAs are added to the database file's path as query parameter (?...&...).
81
+ // However, the format of these appended query parameters differs between SQLite
82
+ // libraries. This translates PRAGMA statements into the format required by the
83
+ // library the binary is built with.
84
+ //
85
+ // "PRAGMA busy_timeout = 5000" becomes "_pragma=busy_timeout=5000" when using the
86
+ // modernc library.
87
+ func pragmsQueriesToString (pragmas []string ) (filenamePragmaString string ) {
105
88
v := url.Values {}
106
89
107
- for _ , p := range cfg . SQLitePragmas {
90
+ for _ , p := range pragmas {
108
91
//Sanitize, to make replace/stripping of "PRAGMA" keyword easier.
109
92
p = strings .ToLower (p )
110
93
111
94
//Strip out the PRAGMA keyword.
112
- p = strings .Replace (p , "pragma" , "" , 1 )
95
+ p = strings .TrimPrefix (p , "pragma" )
113
96
114
- //Build filename pragma as expected by SQLite library is use.
97
+ //Build pragma as expected by library in use.
115
98
switch GetSQLiteLibrary () {
116
99
case sqliteLibraryMattn :
117
- //ex: _busy_timeout=5000
100
+ //Library's format: _busy_timeout=5000
118
101
key , value , found := strings .Cut (p , "=" )
119
102
if ! found {
120
103
continue
121
104
}
122
105
123
- //trim
124
106
key = strings .TrimSpace (key )
125
107
value = strings .TrimSpace (value )
126
108
127
- //add
128
109
key = "_" + key
129
110
v .Add (key , value )
111
+
130
112
case sqliteLibraryModernc :
131
- //ex : _pragma=busy_timeout=5000
113
+ //Library's format : _pragma=busy_timeout=5000
132
114
key := "_pragma"
133
115
value := p
134
116
135
- //trim
136
117
value = strings .TrimSpace (value )
137
118
value = strings .Replace (value , " " , "" , - 1 )
138
119
139
- //add
140
120
v .Add (key , value )
121
+
141
122
default :
142
- //this can never happen since we hardcode the supported sqlite libraries.
123
+ //This can never happen since we hardcode the supported SQLite libraries.
143
124
}
144
125
}
145
126
146
127
return "?" + v .Encode ()
147
128
}
148
-
149
- // GetDefaultSQLitePragmas returns the default PRAGMAs this package defines for use with
150
- // either SQLite library. This can be helpful for debugging. We don't just export the
151
- // sqliteDefaultPragmas slice so that it cannot be modified.
152
- func GetDefaultSQLitePragmas () []string {
153
- return sqliteDefaultPragmas
154
- }
0 commit comments