Skip to content

Commit 81fbf42

Browse files
authored
Merge branch 'mattn:master' into master
2 parents 3f8fe99 + f76bae4 commit 81fbf42

File tree

70 files changed

+34948
-14191
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+34948
-14191
lines changed

.github/workflows/cifuzz.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
dry-run: false
2323
sanitizer: ${{ matrix.sanitizer }}
2424
- name: Upload Crash
25-
uses: actions/upload-artifact@v1
25+
uses: actions/upload-artifact@v4
2626
if: failure()
2727
with:
2828
name: ${{ matrix.sanitizer }}-artifacts

.github/workflows/docker.yaml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,5 @@ jobs:
1717

1818
- name: Run example - simple
1919
run: |
20-
cd ./_example/simple
21-
docker build -t simple .
22-
docker run simple | grep 99\ こんにちわ世界099
20+
docker build -t simple -f ./_example/simple/Dockerfile .
21+
docker run simple | grep 99\ こんにちは世界099

.github/workflows/go.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
strategy:
1515
matrix:
1616
os: [ubuntu-latest, macos-latest]
17-
go: ['1.17', '1.18', '1.19']
17+
go: ['1.19', '1.20', '1.21']
1818
fail-fast: false
1919
env:
2020
OS: ${{ matrix.os }}
@@ -44,7 +44,7 @@ jobs:
4444
run: go-acc . -- -race -v -tags "libsqlite3"
4545

4646
- name: 'Tags: full'
47-
run: go-acc . -- -race -v -tags "sqlite_allow_uri_authority sqlite_app_armor sqlite_foreign_keys sqlite_fts5 sqlite_icu sqlite_introspect sqlite_json sqlite_os_trace sqlite_preupdate_hook sqlite_secure_delete sqlite_see sqlite_stat4 sqlite_trace sqlite_userauth sqlite_vacuum_incr sqlite_vtable sqlite_unlock_notify sqlite_column_metadata"
47+
run: go-acc . -- -race -v -tags "sqlite_allow_uri_authority sqlite_app_armor sqlite_column_metadata sqlite_foreign_keys sqlite_fts5 sqlite_icu sqlite_introspect sqlite_json sqlite_math_functions sqlite_os_trace sqlite_preupdate_hook sqlite_secure_delete sqlite_see sqlite_stat4 sqlite_trace sqlite_unlock_notify sqlite_userauth sqlite_vacuum_incr sqlite_vtable"
4848

4949
- name: 'Tags: vacuum'
5050
run: go-acc . -- -race -v -tags "sqlite_vacuum_full"
@@ -64,7 +64,7 @@ jobs:
6464

6565
strategy:
6666
matrix:
67-
go: ['1.17', '1.18', '1.19']
67+
go: ['1.19', '1.20', '1.21']
6868
fail-fast: false
6969
env:
7070
OS: windows-latest
@@ -99,7 +99,7 @@ jobs:
9999
- name: 'Tags: full'
100100
run: |
101101
echo 'skip this test'
102-
echo go build -race -v -tags "sqlite_allow_uri_authority sqlite_app_armor sqlite_foreign_keys sqlite_fts5 sqlite_icu sqlite_introspect sqlite_json sqlite_preupdate_hook sqlite_secure_delete sqlite_see sqlite_stat4 sqlite_trace sqlite_userauth sqlite_vacuum_incr sqlite_vtable sqlite_unlock_notify"
102+
echo go build -race -v -tags "sqlite_allow_uri_authority sqlite_app_armor sqlite_column_metadata sqlite_foreign_keys sqlite_fts5 sqlite_icu sqlite_introspect sqlite_json sqlite_math_functions sqlite_preupdate_hook sqlite_secure_delete sqlite_see sqlite_stat4 sqlite_trace sqlite_unlock_notify sqlite_userauth sqlite_vacuum_incr sqlite_vtable"
103103
shell: msys2 {0}
104104

105105
- name: 'Tags: vacuum'

README.md

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ This package follows the official [Golang Release Policy](https://golang.org/doc
3535
- [Android](#android)
3636
- [ARM](#arm)
3737
- [Cross Compile](#cross-compile)
38-
- [Google Cloud Platform](#google-cloud-platform)
38+
- [Compiling](#compiling)
3939
- [Linux](#linux)
4040
- [Alpine](#alpine)
4141
- [Fedora](#fedora)
4242
- [Ubuntu](#ubuntu)
43-
- [Mac OSX](#mac-osx)
43+
- [macOS](#mac-osx)
4444
- [Windows](#windows)
4545
- [Errors](#errors)
4646
- [User Authentication](#user-authentication)
@@ -70,9 +70,8 @@ This package can be installed with the `go get` command:
7070

7171
_go-sqlite3_ is *cgo* package.
7272
If you want to build your app using go-sqlite3, you need gcc.
73-
However, after you have built and installed _go-sqlite3_ with `go install github.com/mattn/go-sqlite3` (which requires gcc), you can build your app without relying on gcc in future.
7473

75-
***Important: because this is a `CGO` enabled package, you are required to set the environment variable `CGO_ENABLED=1` and have a `gcc` compile present within your path.***
74+
***Important: because this is a `CGO` enabled package, you are required to set the environment variable `CGO_ENABLED=1` and have a `gcc` compiler present within your path.***
7675

7776
# API Reference
7877

@@ -145,7 +144,7 @@ Click [here](https://golang.org/pkg/go/build/#hdr-Build_Constraints) for more in
145144
If you wish to build this library with additional extensions / features, use the following command:
146145

147146
```bash
148-
go build --tags "<FEATURE>"
147+
go build -tags "<FEATURE>"
149148
```
150149

151150
For available features, see the extension list.
@@ -154,7 +153,7 @@ When using multiple build tags, all the different tags should be space delimited
154153
Example:
155154

156155
```bash
157-
go build --tags "icu json1 fts5 secure_delete"
156+
go build -tags "icu json1 fts5 secure_delete"
158157
```
159158

160159
### Feature / Extension List
@@ -165,19 +164,22 @@ go build --tags "icu json1 fts5 secure_delete"
165164
| Allow URI Authority | sqlite_allow_uri_authority | URI filenames normally throws an error if the authority section is not either empty or "localhost".<br><br>However, if SQLite is compiled with the SQLITE_ALLOW_URI_AUTHORITY compile-time option, then the URI is converted into a Uniform Naming Convention (UNC) filename and passed down to the underlying operating system that way |
166165
| App Armor | sqlite_app_armor | When defined, this C-preprocessor macro activates extra code that attempts to detect misuse of the SQLite API, such as passing in NULL pointers to required parameters or using objects after they have been destroyed. <br><br>App Armor is not available under `Windows`. |
167166
| Disable Load Extensions | sqlite_omit_load_extension | Loading of external extensions is enabled by default.<br><br>To disable extension loading add the build tag `sqlite_omit_load_extension`. |
167+
| Enable Serialization with `libsqlite3` | sqlite_serialize | Serialization and deserialization of a SQLite database is available by default, unless the build tag `libsqlite3` is set.<br><br>To enable this functionality even if `libsqlite3` is set, add the build tag `sqlite_serialize`. |
168168
| Foreign Keys | sqlite_foreign_keys | This macro determines whether enforcement of foreign key constraints is enabled or disabled by default for new database connections.<br><br>Each database connection can always turn enforcement of foreign key constraints on and off and run-time using the foreign_keys pragma.<br><br>Enforcement of foreign key constraints is normally off by default, but if this compile-time parameter is set to 1, enforcement of foreign key constraints will be on by default |
169169
| Full Auto Vacuum | sqlite_vacuum_full | Set the default auto vacuum to full |
170170
| Incremental Auto Vacuum | sqlite_vacuum_incr | Set the default auto vacuum to incremental |
171171
| Full Text Search Engine | sqlite_fts5 | When this option is defined in the amalgamation, versions 5 of the full-text search engine (fts5) is added to the build automatically |
172172
| International Components for Unicode | sqlite_icu | This option causes the International Components for Unicode or "ICU" extension to SQLite to be added to the build |
173173
| Introspect PRAGMAS | sqlite_introspect | This option adds some extra PRAGMA statements. <ul><li>PRAGMA function_list</li><li>PRAGMA module_list</li><li>PRAGMA pragma_list</li></ul> |
174174
| JSON SQL Functions | sqlite_json | When this option is defined in the amalgamation, the JSON SQL functions are added to the build automatically |
175+
| Math Functions | sqlite_math_functions | This compile-time option enables built-in scalar math functions. For more information see [Built-In Mathematical SQL Functions](https://www.sqlite.org/lang_mathfunc.html) |
175176
| OS Trace | sqlite_os_trace | This option enables OSTRACE() debug logging. This can be verbose and should not be used in production. |
176177
| Pre Update Hook | sqlite_preupdate_hook | Registers a callback function that is invoked prior to each INSERT, UPDATE, and DELETE operation on a database table. |
177178
| Secure Delete | sqlite_secure_delete | This compile-time option changes the default setting of the secure_delete pragma.<br><br>When this option is not used, secure_delete defaults to off. When this option is present, secure_delete defaults to on.<br><br>The secure_delete setting causes deleted content to be overwritten with zeros. There is a small performance penalty since additional I/O must occur.<br><br>On the other hand, secure_delete can prevent fragments of sensitive information from lingering in unused parts of the database file after it has been deleted. See the documentation on the secure_delete pragma for additional information |
178179
| Secure Delete (FAST) | sqlite_secure_delete_fast | For more information see [PRAGMA secure_delete](https://www.sqlite.org/pragma.html#pragma_secure_delete) |
179180
| Tracing / Debug | sqlite_trace | Activate trace functions |
180181
| User Authentication | sqlite_userauth | SQLite User Authentication see [User Authentication](#user-authentication) for more information. |
182+
| Virtual Tables | sqlite_vtable | SQLite Virtual Tables see [SQLite Official VTABLE Documentation](https://www.sqlite.org/vtab.html) for more information, and a [full example here](https://github.com/mattn/go-sqlite3/tree/master/_example/vtable) |
181183

182184
# Compilation
183185

@@ -191,7 +193,7 @@ This package can be compiled for android.
191193
Compile with:
192194

193195
```bash
194-
go build --tags "android"
196+
go build -tags "android"
195197
```
196198

197199
For more information see [#201](https://github.com/mattn/go-sqlite3/issues/201)
@@ -216,21 +218,16 @@ This library can be cross-compiled.
216218

217219
In some cases you are required to the `CC` environment variable with the cross compiler.
218220

219-
## Cross Compiling from MAC OSX
220-
The simplest way to cross compile from OSX is to use [xgo](https://github.com/karalabe/xgo).
221+
## Cross Compiling from macOS
222+
The simplest way to cross compile from macOS is to use [xgo](https://github.com/karalabe/xgo).
221223

222224
Steps:
223-
- Install [xgo](https://github.com/karalabe/xgo) (`go get github.com/karalabe/xgo`).
224-
- Ensure that your project is within your `GOPATH`.
225-
- Run `xgo local/path/to/project`.
225+
- Install [musl-cross](https://github.com/FiloSottile/homebrew-musl-cross) (`brew install FiloSottile/musl-cross/musl-cross`).
226+
- Run `CC=x86_64-linux-musl-gcc CXX=x86_64-linux-musl-g++ GOARCH=amd64 GOOS=linux CGO_ENABLED=1 go build -ldflags "-linkmode external -extldflags -static"`.
226227

227-
Please refer to the project's [README](https://github.com/karalabe/xgo/blob/master/README.md) for further information.
228+
Please refer to the project's [README](https://github.com/FiloSottile/homebrew-musl-cross#readme) for further information.
228229

229-
# Google Cloud Platform
230-
231-
Building on GCP is not possible because Google Cloud Platform does not allow `gcc` to be executed.
232-
233-
Please work only with compiled final binaries.
230+
# Compiling
234231

235232
## Linux
236233

@@ -239,13 +236,13 @@ To compile this package on Linux, you must install the development tools for you
239236
To compile under linux use the build tag `linux`.
240237

241238
```bash
242-
go build --tags "linux"
239+
go build -tags "linux"
243240
```
244241

245242
If you wish to link directly to libsqlite3 then you can use the `libsqlite3` build tag.
246243

247244
```
248-
go build --tags "libsqlite3 linux"
245+
go build -tags "libsqlite3 linux"
249246
```
250247

251248
### Alpine
@@ -268,34 +265,43 @@ sudo yum groupinstall "Development Tools" "Development Libraries"
268265
sudo apt-get install build-essential
269266
```
270267

271-
## Mac OSX
268+
## macOS
272269

273-
OSX should have all the tools present to compile this package. If not, install XCode to add all the developers tools.
270+
macOS should have all the tools present to compile this package. If not, install XCode to add all the developers tools.
274271

275272
Required dependency:
276273

277274
```bash
278275
brew install sqlite3
279276
```
280277

281-
For OSX, there is an additional package to install which is required if you wish to build the `icu` extension.
278+
For macOS, there is an additional package to install which is required if you wish to build the `icu` extension.
282279

283280
This additional package can be installed with `homebrew`:
284281

285282
```bash
286283
brew upgrade icu4c
287284
```
288285

289-
To compile for Mac OSX:
286+
To compile for macOS on x86:
290287

291288
```bash
292-
go build --tags "darwin"
289+
go build -tags "darwin amd64"
290+
```
291+
292+
To compile for macOS on ARM chips:
293+
294+
```bash
295+
go build -tags "darwin arm64"
293296
```
294297

295298
If you wish to link directly to libsqlite3, use the `libsqlite3` build tag:
296299

297300
```
298-
go build --tags "libsqlite3 darwin"
301+
# x86
302+
go build -tags "libsqlite3 darwin amd64"
303+
# ARM
304+
go build -tags "libsqlite3 darwin arm64"
299305
```
300306

301307
Additional information:

_example/json/json.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package main
2+
3+
import (
4+
"database/sql"
5+
"database/sql/driver"
6+
"encoding/json"
7+
"fmt"
8+
_ "github.com/mattn/go-sqlite3"
9+
"log"
10+
"os"
11+
)
12+
13+
type Tag struct {
14+
Name string `json:"name"`
15+
Country string `json:"country"`
16+
}
17+
18+
func (t *Tag) Scan(value interface{}) error {
19+
return json.Unmarshal([]byte(value.(string)), t)
20+
}
21+
22+
func (t *Tag) Value() (driver.Value, error) {
23+
b, err := json.Marshal(t)
24+
return string(b), err
25+
}
26+
27+
func main() {
28+
os.Remove("./foo.db")
29+
30+
db, err := sql.Open("sqlite3", "./foo.db")
31+
if err != nil {
32+
log.Fatal(err)
33+
}
34+
defer db.Close()
35+
36+
_, err = db.Exec(`create table foo (tag jsonb)`)
37+
if err != nil {
38+
log.Fatal(err)
39+
}
40+
41+
stmt, err := db.Prepare("insert into foo(tag) values(?)")
42+
if err != nil {
43+
log.Fatal(err)
44+
}
45+
defer stmt.Close()
46+
_, err = stmt.Exec(`{"name": "mattn", "country": "japan"}`)
47+
if err != nil {
48+
log.Fatal(err)
49+
}
50+
_, err = stmt.Exec(`{"name": "michael", "country": "usa"}`)
51+
if err != nil {
52+
log.Fatal(err)
53+
}
54+
55+
var country string
56+
err = db.QueryRow("select tag->>'country' from foo where tag->>'name' = 'mattn'").Scan(&country)
57+
if err != nil {
58+
log.Fatal(err)
59+
}
60+
fmt.Println(country)
61+
62+
var tag Tag
63+
err = db.QueryRow("select tag from foo where tag->>'name' = 'mattn'").Scan(&tag)
64+
if err != nil {
65+
log.Fatal(err)
66+
}
67+
68+
fmt.Println(tag.Name)
69+
70+
tag.Country = "日本"
71+
_, err = db.Exec(`update foo set tag = ? where tag->>'name' == 'mattn'`, &tag)
72+
if err != nil {
73+
log.Fatal(err)
74+
}
75+
76+
err = db.QueryRow("select tag->>'country' from foo where tag->>'name' = 'mattn'").Scan(&country)
77+
if err != nil {
78+
log.Fatal(err)
79+
}
80+
fmt.Println(country)
81+
}

_example/limit/limit.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ import (
1010
"github.com/mattn/go-sqlite3"
1111
)
1212

13-
func createBulkInsertQuery(n int, start int) (query string, args []interface{}) {
13+
func createBulkInsertQuery(n int, start int) (query string, args []any) {
1414
values := make([]string, n)
15-
args = make([]interface{}, n*2)
15+
args = make([]any, n*2)
1616
pos := 0
1717
for i := 0; i < n; i++ {
1818
values[i] = "(?, ?)"
1919
args[pos] = start + i
20-
args[pos+1] = fmt.Sprintf("こんにちわ世界%03d", i)
20+
args[pos+1] = fmt.Sprintf("こんにちは世界%03d", i)
2121
pos += 2
2222
}
2323
query = fmt.Sprintf(
@@ -27,7 +27,7 @@ func createBulkInsertQuery(n int, start int) (query string, args []interface{})
2727
return
2828
}
2929

30-
func bulkInsert(db *sql.DB, query string, args []interface{}) (err error) {
30+
func bulkInsert(db *sql.DB, query string, args []any) (err error) {
3131
stmt, err := db.Prepare(query)
3232
if err != nil {
3333
return

_example/simple/Dockerfile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
# -----------------------------------------------------------------------------
1010
# Build Stage
1111
# -----------------------------------------------------------------------------
12-
FROM golang:alpine AS build
12+
FROM golang:alpine3.18 AS build
1313

1414
# Important:
1515
# Because this is a CGO enabled package, you are required to set it as 1.
@@ -26,14 +26,16 @@ WORKDIR /workspace
2626
COPY . /workspace/
2727

2828
RUN \
29+
cd _example/simple && \
2930
go mod init github.com/mattn/sample && \
31+
go mod edit -replace=github.com/mattn/go-sqlite3=../.. && \
3032
go mod tidy && \
3133
go install -ldflags='-s -w -extldflags "-static"' ./simple.go
3234

3335
RUN \
3436
# Smoke test
3537
set -o pipefail; \
36-
/go/bin/simple | grep 99\ こんにちわ世界099
38+
/go/bin/simple | grep 99\ こんにちは世界099
3739

3840
# -----------------------------------------------------------------------------
3941
# Main Stage

_example/simple/simple.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func main() {
3737
}
3838
defer stmt.Close()
3939
for i := 0; i < 100; i++ {
40-
_, err = stmt.Exec(i, fmt.Sprintf("こんにちわ世界%03d", i))
40+
_, err = stmt.Exec(i, fmt.Sprintf("こんにちは世界%03d", i))
4141
if err != nil {
4242
log.Fatal(err)
4343
}

_example/vtable/vtable.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,13 @@ func (v *ghRepoTable) Open() (sqlite3.VTabCursor, error) {
6262
return &ghRepoCursor{0, repos}, nil
6363
}
6464

65-
func (v *ghRepoTable) BestIndex(cst []sqlite3.InfoConstraint, ob []sqlite3.InfoOrderBy) (*sqlite3.IndexResult, error) {
66-
return &sqlite3.IndexResult{}, nil
65+
func (v *ghRepoTable) BestIndex(csts []sqlite3.InfoConstraint, ob []sqlite3.InfoOrderBy) (*sqlite3.IndexResult, error) {
66+
used := make([]bool, len(csts))
67+
return &sqlite3.IndexResult{
68+
IdxNum: 0,
69+
IdxStr: "default",
70+
Used: used,
71+
}, nil
6772
}
6873

6974
func (v *ghRepoTable) Disconnect() error { return nil }
@@ -88,7 +93,7 @@ func (vc *ghRepoCursor) Column(c *sqlite3.SQLiteContext, col int) error {
8893
return nil
8994
}
9095

91-
func (vc *ghRepoCursor) Filter(idxNum int, idxStr string, vals []interface{}) error {
96+
func (vc *ghRepoCursor) Filter(idxNum int, idxStr string, vals []any) error {
9297
vc.index = 0
9398
return nil
9499
}

0 commit comments

Comments
 (0)