Skip to content

Commit df5c166

Browse files
c9845 (AA)c9845 (AA)
c9845 (AA)
authored and
c9845 (AA)
committed
Cleaning up and rewriting how connection strings are build, expecially around handling of of SQLite PRAGMAs. Work in progress.
1 parent 0ca1c67 commit df5c166

File tree

8 files changed

+302
-190
lines changed

8 files changed

+302
-190
lines changed

go.mod

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,7 @@ require (
2424
golang.org/x/tools v0.17.0 // indirect
2525
lukechampine.com/uint128 v1.3.0 // indirect
2626
modernc.org/cc/v3 v3.41.0 // indirect
27-
modernc.org/cc/v4 v4.2.1 // indirect
2827
modernc.org/ccgo/v3 v3.16.15 // indirect
29-
modernc.org/ccgo/v4 v4.0.0-20230612200659-63de3e82e68d // indirect
30-
modernc.org/gc/v2 v2.1.2-0.20220923113132-f3b5abcf8083 // indirect
3128
modernc.org/libc v1.40.5 // indirect
3229
modernc.org/mathutil v1.6.0 // indirect
3330
modernc.org/memory v1.7.2 // indirect

go.sum

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
66
github.com/denisenkom/go-mssqldb v0.12.3 h1:pBSGx9Tq67pBOTLmxNuirNTeB8Vjmf886Kx+8Y+8shw=
77
github.com/denisenkom/go-mssqldb v0.12.3/go.mod h1:k0mtMFOnU+AihqFxPMiF05rtiDrorD1Vrm1KEz5hxDo=
88
github.com/dnaeon/go-vcr v1.2.0/go.mod h1:R4UdLID7HZT3taECzJs4YgbbH6PIGXB6W/sc5OLb6RQ=
9-
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
109
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
1110
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
1211
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
@@ -21,8 +20,6 @@ github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
2120
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
2221
github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26 h1:Xim43kblpZXfIBQsbuBVKCudVG457BR2GZFIz3uw3hQ=
2322
github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26/go.mod h1:dDKJzRmX4S37WGHujM7tX//fmj1uioxKzKxz3lo4HJo=
24-
github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4=
25-
github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
2623
github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
2724
github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
2825
github.com/jmoiron/sqlx v1.3.5 h1:vFFPA71p1o5gAeqtEAwLU4dnX2napprKtHr7PYIcN3g=
@@ -34,8 +31,6 @@ github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
3431
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
3532
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
3633
github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
37-
github.com/mattn/go-sqlite3 v1.14.18 h1:JL0eqdCOq6DJVNPSvArO/bIV9/P7fbGrV00LZHc+5aI=
38-
github.com/mattn/go-sqlite3 v1.14.18/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
3934
github.com/mattn/go-sqlite3 v1.14.19 h1:fhGleo2h1p8tVChob4I9HpmVFIAkKGpiukdrgQbWfGI=
4035
github.com/mattn/go-sqlite3 v1.14.19/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
4136
github.com/modocache/gover v0.0.0-20171022184752-b58185e213c5/go.mod h1:caMODM3PzxT8aQXRPkAt8xlV/e7d7w8GM5g0fa5F0D8=
@@ -49,35 +44,27 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
4944
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
5045
golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
5146
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
52-
golang.org/x/crypto v0.15.0 h1:frVn1TEaCEaZcn3Tmd7Y2b5KKPaZ+I32Q2OA3kYp5TA=
53-
golang.org/x/crypto v0.15.0/go.mod h1:4ChreQoLWfG3xLDer1WdlH5NdlQ3+mwnQq1YTKY+72g=
5447
golang.org/x/crypto v0.18.0 h1:PGVlW0xEltQnzFZ55hkuX5+KLyrMYhHld1YHO4AKcdc=
5548
golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1mg=
56-
golang.org/x/exp v0.0.0-20181106170214-d68db9428509/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
5749
golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0=
5850
golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
5951
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
6052
golang.org/x/net v0.0.0-20210610132358-84b48f89b13b/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
6153
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
62-
golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE=
63-
golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
6454
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
55+
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
6556
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
6657
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
6758
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
6859
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
6960
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
7061
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
71-
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
72-
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
7362
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
7463
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
7564
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
7665
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
7766
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
7867
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
79-
golang.org/x/tools v0.15.0 h1:zdAyfUGbYmuVokhzVmghFl2ZJh5QhcfebBgmVPFYA+8=
80-
golang.org/x/tools v0.15.0/go.mod h1:hpksKq4dtpQWS1uQ61JkdqWM3LscIS6Slf+VVkm+wQk=
8168
golang.org/x/tools v0.17.0 h1:FvmRgNOcs3kOa+T20R1uhfP9F6HgG2mfxDv1vrx1Htc=
8269
golang.org/x/tools v0.17.0/go.mod h1:xsh6VxdV005rRVaS6SSAf9oiAqljS7UZUacMZ8Bnsps=
8370
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
@@ -89,23 +76,12 @@ lukechampine.com/uint128 v1.3.0 h1:cDdUVfRwDUDovz610ABgFD17nXD4/uDgVHl2sC3+sbo=
8976
lukechampine.com/uint128 v1.3.0/go.mod h1:c4eWIwlEGaxC/+H1VguhU4PHXNWDCDMUlWdIWl2j1gk=
9077
modernc.org/cc/v3 v3.41.0 h1:QoR1Sn3YWlmA1T4vLaKZfawdVtSiGx8H+cEojbC7v1Q=
9178
modernc.org/cc/v3 v3.41.0/go.mod h1:Ni4zjJYJ04CDOhG7dn640WGfwBzfE0ecX8TyMB0Fv0Y=
92-
modernc.org/cc/v4 v4.2.1 h1:xwwaXFwiPaVZpGRMd19NPLsaiNyNBO8oChey4501g1M=
93-
modernc.org/cc/v4 v4.2.1/go.mod h1:0O8vuqhQfwBy+piyfEjzWIUGV4I3TPsXSf0W05+lgN8=
9479
modernc.org/ccgo/v3 v3.16.15 h1:KbDR3ZAVU+wiLyMESPtbtE/Add4elztFyfsWoNTgxS0=
9580
modernc.org/ccgo/v3 v3.16.15/go.mod h1:yT7B+/E2m43tmMOT51GMoM98/MtHIcQQSleGnddkUNI=
96-
modernc.org/ccgo/v4 v4.0.0-20230612200659-63de3e82e68d h1:3yB/pQNL5kVPDifGFqoZjeRxf8m0+Us15rB7ertNASQ=
97-
modernc.org/ccgo/v4 v4.0.0-20230612200659-63de3e82e68d/go.mod h1:austqj6cmEDRfewsUvmGmyIgsI/Nq87oTXlfTgY85Fc=
9881
modernc.org/ccorpus v1.11.6 h1:J16RXiiqiCgua6+ZvQot4yUuUy8zxgqbqEEUuGPlISk=
9982
modernc.org/ccorpus v1.11.6/go.mod h1:2gEUTrWqdpH2pXsmTM1ZkjeSrUWDpjMu2T6m29L/ErQ=
100-
modernc.org/fileutil v1.0.0/go.mod h1:JHsWpkrk/CnVV1H/eGlFf85BEpfkrp56ro8nojIq9Q8=
101-
modernc.org/gc/v2 v2.1.2-0.20220923113132-f3b5abcf8083 h1:rGoLVwiOxdeVkGYMOF/8Pw7xpDd3OqScJU/tqHgvY1c=
102-
modernc.org/gc/v2 v2.1.2-0.20220923113132-f3b5abcf8083/go.mod h1:Zt5HLUW0j+l02wj99UsPs+1DOFwwsGnqfcw+BGyyP/A=
10383
modernc.org/httpfs v1.0.6 h1:AAgIpFZRXuYnkjftxTAZwMIiwEqAfk8aVB2/oA6nAeM=
10484
modernc.org/httpfs v1.0.6/go.mod h1:7dosgurJGp0sPaRanU53W4xZYKh14wfzX420oZADeHM=
105-
modernc.org/lex v1.1.0/go.mod h1:+ojes+j0JYCaqwKYCBjcUavscJHmWFKvViUTMU4VjLA=
106-
modernc.org/lexer v1.0.0/go.mod h1:F/Dld0YKYdZCLQ7bD0USbWL4YKCyTDRDHiDTOs0q0vk=
107-
modernc.org/libc v1.34.9 h1:yhQGs5jsWHJIU7jY6nCe8GJw27j+z6xYi0eorwPpGgI=
108-
modernc.org/libc v1.34.9/go.mod h1:YAXkAZ8ktnkCKaN9sw/UDeUVkGYJ/YquGO4FTi5nmHE=
10985
modernc.org/libc v1.40.5 h1:B9KljZSWzWCV2WtgQ54xu0Ig4imof21SLnKFx7qZ3os=
11086
modernc.org/libc v1.40.5/go.mod h1:YAXkAZ8ktnkCKaN9sw/UDeUVkGYJ/YquGO4FTi5nmHE=
11187
modernc.org/mathutil v1.6.0 h1:fRe9+AmYlaej+64JsEEhoWuAYBkOtQiMEU7n/XgfYi4=
@@ -114,16 +90,12 @@ modernc.org/memory v1.7.2 h1:Klh90S215mmH8c9gO98QxQFsY+W451E8AnzjoE2ee1E=
11490
modernc.org/memory v1.7.2/go.mod h1:NO4NVCQy0N7ln+T9ngWqOQfi7ley4vpwvARR+Hjw95E=
11591
modernc.org/opt v0.1.3 h1:3XOZf2yznlhC+ibLltsDGzABUGVx8J6pnFMS3E4dcq4=
11692
modernc.org/opt v0.1.3/go.mod h1:WdSiB5evDcignE70guQKxYUl14mgWtbClRi5wmkkTX0=
117-
modernc.org/scannertest v1.0.0/go.mod h1:9qnOCV+wSvq1o9hcOPNwRorND4qpZdtmTvmcdKyN3iE=
118-
modernc.org/sqlite v1.27.0 h1:MpKAHoyYB7xqcwnUwkuD+npwEa0fojF0B5QRbN+auJ8=
119-
modernc.org/sqlite v1.27.0/go.mod h1:Qxpazz0zH8Z1xCFyi5GSL3FzbtZ3fvbjmywNogldEW0=
12093
modernc.org/sqlite v1.28.0 h1:Zx+LyDDmXczNnEQdvPuEfcFVA2ZPyaD7UCZDjef3BHQ=
12194
modernc.org/sqlite v1.28.0/go.mod h1:Qxpazz0zH8Z1xCFyi5GSL3FzbtZ3fvbjmywNogldEW0=
12295
modernc.org/strutil v1.2.0 h1:agBi9dp1I+eOnxXeiZawM8F4LawKv4NzGWSaLfyeNZA=
12396
modernc.org/strutil v1.2.0/go.mod h1:/mdcBmfOibveCTBxUl5B5l6W+TTH1FXPLHZE6bTosX0=
12497
modernc.org/tcl v1.15.2 h1:C4ybAYCGJw968e+Me18oW55kD/FexcHbqH2xak1ROSY=
12598
modernc.org/tcl v1.15.2/go.mod h1:3+k/ZaEbKrC8ePv8zJWPtBSW0V7Gg9g8rkmhI1Kfs3c=
126-
modernc.org/token v1.0.1/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM=
12799
modernc.org/token v1.1.0 h1:Xl7Ap9dKaEs5kLoOQeQmPWevfnk/DM5qcLcYlA8ys6Y=
128100
modernc.org/token v1.1.0/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM=
129101
modernc.org/z v1.7.3 h1:zDJf6iHjrnB+WRD88stbXokugjyc0/pB91ri1gO6LZY=

sqldb-sqlite-mattn.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818

1919
const (
2020
//sqliteLibrary is used in logging.
21-
sqliteLibrary = "github.com/mattn/go-sqlite3"
21+
sqliteLibrary = sqliteLibraryMattn
2222

2323
//sqliteDriverName is used in Connect() when calling [database/sql.Open].
2424
sqliteDriverName = "sqlite3"

sqldb-sqlite-modernc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818

1919
const (
2020
//sqliteLibrary is used in logging.
21-
sqliteLibrary = "modernc.org/sqlite"
21+
sqliteLibrary = sqliteLibraryModernc
2222

2323
//sqliteDriverName is used in Connect() when calling [database/sql.Open].
2424
sqliteDriverName = "sqlite"

sqldb-sqlite.go

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,31 @@ import (
77
"github.com/jmoiron/sqlx"
88
)
99

10+
// library is used for handling the SQLite libraries/drivers that can be used.
11+
type library string
12+
1013
const (
1114
//Possible SQLite libraries. These are used in comparisons, such as when building
1215
//the connection string PRAGMAs.
13-
sqliteLibraryMattn = "github.com/mattn/go-sqlite3"
14-
sqliteLibraryModernc = "modernc.org/sqlite"
16+
sqliteLibraryMattn library = "github.com/mattn/go-sqlite3"
17+
sqliteLibraryModernc library = "modernc.org/sqlite"
18+
)
1519

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+
const (
21+
//SQLiteInMemoryFilePathRacy is the path to provide for SQLitePath when you want
22+
//to use an in-memory database instead of a file on disk. This is racy because
23+
//each call to Connect() will open a brand new database. If you only call
24+
//Connect() once then this is safe to use.
2025
//
2126
//This is good for running tests since then each test runs with a separate
2227
//in-memory db.
23-
InMemoryFilePathRacy = ":memory:"
28+
SQLiteInMemoryFilePathRacy = ":memory:"
2429

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,
30+
//SQLiteInMemoryFilePathRaceSafe is the path to provide for SQLitePath when you
31+
//want to use an in-memory database instead of a file on disk. This is race safe
32+
//since multiple calls of Connect() will connect to the same in-memory database,
2833
//although connecting more than once to the same database would be very odd.
29-
InMemoryFilePathRaceSafe = "file::memory:?cache=shared"
34+
SQLiteInMemoryFilePathRaceSafe = "file::memory:?cache=shared"
3035
)
3136

3237
// NewSQLite is a shorthand for calling New() and then manually setting the applicable
@@ -61,7 +66,7 @@ func GetSQLiteVersion() (version string, err error) {
6166
driver := getDriver(DBTypeSQLite)
6267

6368
//Connect.
64-
conn, err := sqlx.Open(driver, InMemoryFilePathRacy)
69+
conn, err := sqlx.Open(driver, SQLiteInMemoryFilePathRacy)
6570
if err != nil {
6671
return
6772
}
@@ -78,24 +83,26 @@ func GetSQLiteVersion() (version string, err error) {
7883

7984
// GetSQLiteLibrary returns the SQLite library that was used to build the binary. The
8085
// library is set at build/run with go build tags.
81-
func GetSQLiteLibrary() string {
86+
func GetSQLiteLibrary() library {
8287
return sqliteLibrary
8388
}
8489

85-
// pragmsQueriesToString takes SQLite PRAGMAs in query format and retuns them in the
86-
// format needed to be appended to a SQLite database filepath per the in-use SQLite
87-
// driver.
90+
// pragmasToURLValues takes SQLite PRAGMAs in SQLite query format and retuns them in
91+
// a url.Values for appending to a SQLite filepath URL.
8892
//
8993
// SQLite PRAGMAs need to be set upon initially connecting to the database. The
90-
// PRAGMAs are added to the database file's path as query parameters (?...&...).
94+
// PRAGMAs are added to the database's filepath as query parameters (?...&...).
9195
// However, the format of these appended query parameters differs between SQLite
92-
// libraries. This translates PRAGMA statements into the format required by the
93-
// library the binary is built with.
96+
// libraries (mattn vs modernc). This func translates PRAGMA statements, written in
97+
// the SQLite query format, into the filepath format required by the SQLite driver
98+
// the binary is built with.
9499
//
95-
// "PRAGMA busy_timeout = 5000" becomes "_pragma=busy_timeout=5000" when using the
96-
// modernc library.
97-
func pragmsQueriesToString(pragmas []string) (filenamePragmaString string) {
98-
v := url.Values{}
100+
// Example:
101+
// - SQLite Query Format: "PRAGMA busy_timeout = 5000".
102+
// - Mattn Format: "_busy_timeout=5000".
103+
// - Modernc: Format: "_pragma=busy_timeout=5000".
104+
func pragmasToURLValues(pragmas []string, lib library) (v url.Values) {
105+
v = url.Values{}
99106

100107
for _, p := range pragmas {
101108
//Sanitize, to make replace/stripping of "PRAGMA" keyword easier.
@@ -104,10 +111,9 @@ func pragmsQueriesToString(pragmas []string) (filenamePragmaString string) {
104111
//Strip out the PRAGMA keyword.
105112
p = strings.TrimPrefix(p, "pragma")
106113

107-
//Build pragma as expected by library in use.
108-
switch GetSQLiteLibrary() {
114+
//Build pragma key-value pairs as expected by driver/library in use.
115+
switch lib {
109116
case sqliteLibraryMattn:
110-
//Library's format: _busy_timeout=5000
111117
key, value, found := strings.Cut(p, "=")
112118
if !found {
113119
continue
@@ -120,7 +126,6 @@ func pragmsQueriesToString(pragmas []string) (filenamePragmaString string) {
120126
v.Add(key, value)
121127

122128
case sqliteLibraryModernc:
123-
//Library's format: _pragma=busy_timeout=5000
124129
key := "_pragma"
125130
value := p
126131

@@ -134,5 +139,5 @@ func pragmsQueriesToString(pragmas []string) (filenamePragmaString string) {
134139
}
135140
}
136141

137-
return v.Encode()
142+
return
138143
}

sqldb-translators_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66

77
func TestRunTranslators(t *testing.T) {
88
//Define config.
9-
c := NewSQLite(InMemoryFilePathRaceSafe)
9+
c := NewSQLite(SQLiteInMemoryFilePathRaceSafe)
1010
c.DeployQueryTranslators = []Translator{
1111
TranslateMariaDBToSQLiteCreateTable,
1212
}

sqldb.go

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ package sqldb
111111
import (
112112
"errors"
113113
"fmt"
114+
"log"
114115
"net"
115116
"net/url"
116117
"os"
@@ -393,7 +394,7 @@ func (c *Config) Connect() (err error) {
393394
//If the database is in-memory, we can ignore this error though, since, the
394395
//database will never exist yet an is in fact created when Open() and Ping() are
395396
//called below.
396-
if c.IsSQLite() && c.SQLitePath != InMemoryFilePathRacy && c.SQLitePath != InMemoryFilePathRaceSafe {
397+
if c.IsSQLite() && c.SQLitePath != SQLiteInMemoryFilePathRacy && c.SQLitePath != SQLiteInMemoryFilePathRaceSafe {
397398
_, err = os.Stat(c.SQLitePath)
398399
if os.IsNotExist(err) {
399400
return err
@@ -428,9 +429,10 @@ func (c *Config) Connect() (err error) {
428429
case DBTypeMySQL, DBTypeMariaDB, DBTypeMSSQL:
429430
c.infoLn("sqldb.Connect", "Connecting to database "+c.Name+" on "+c.Host+" with user "+c.User+".")
430431
case DBTypeSQLite:
432+
lib := GetSQLiteLibrary()
433+
431434
c.infoLn("sqldb.Connect", "Connecting to database: "+c.SQLitePath+".")
432-
c.debugLn("sqldb.Connect", "SQLite Library: "+GetSQLiteLibrary()+".")
433-
c.debugLn("sqldb.Connect", "SQLite PRAGMAs: "+pragmsQueriesToString(c.SQLitePragmas)+".")
435+
c.debugLn("sqldb.Connect", "SQLite Library: "+lib+".")
434436
default:
435437
//This can never occur because we called validate() above to verify that a
436438
//valid database type was provided.
@@ -543,19 +545,29 @@ func (c *Config) buildConnectionString(deployingDB bool) (connString string) {
543545
connString = c.SQLitePath
544546

545547
//For SQLite, the connection string is simply a path to a file. However, we
546-
//need to append pragmas as needed.
548+
//may need to append PRAGMAs as needed. PRAGMAs are appended to end of
549+
//filepath as query parameters.
547550
if len(c.SQLitePragmas) != 0 {
548-
pragmasToAdd := pragmsQueriesToString(c.SQLitePragmas)
551+
u, err := url.Parse(c.SQLitePath)
552+
if err != nil {
553+
log.Fatalln("Could not parse SQLite path.", c.SQLitePath, err)
554+
os.Exit(1)
555+
return
556+
}
549557

550-
if strings.Contains(connString, "?") {
551-
//handle InMemoryFilePathRaceSafe
552-
connString += "&" + pragmasToAdd
558+
lib := GetSQLiteLibrary()
559+
pragmasToAdd := pragmasToURLValues(c.SQLitePragmas, lib)
560+
561+
if len(u.Query()) > 0 {
562+
u.RawQuery = u.RawQuery + "&" + pragmasToAdd.Encode()
553563
} else {
554-
connString += "&" + pragmasToAdd
564+
u.RawQuery = "?" + pragmasToAdd.Encode()
555565
}
556566

567+
connString = u.String()
568+
557569
c.debugLn("sqldb.buildConnectionString", "PRAGMAs provided: ", c.SQLitePragmas)
558-
c.debugLn("sqldb.buildConnectionString", "PRAGMA String: ", pragmasToAdd)
570+
c.debugLn("sqldb.buildConnectionString", "PRAGMA String: ", pragmasToAdd.Encode())
559571
c.debugLn("sqldb.buildConnectionString", "Path With PRAGMAS:", connString)
560572
}
561573

@@ -567,7 +579,10 @@ func (c *Config) buildConnectionString(deployingDB bool) (connString string) {
567579
}
568580

569581
q := url.Values{}
570-
q.Add("database", c.Name)
582+
583+
if !deployingDB {
584+
q.Add("database", c.Name)
585+
}
571586

572587
//Handle other connection options.
573588
if len(c.ConnectionOptions) > 0 {

0 commit comments

Comments
 (0)