Bump github.com/mattn/go-sqlite3 to fix musl builds
See https://github.com/mattn/go-sqlite3/issues/1164
This commit is contained in:
@@ -258,7 +258,7 @@ require (
|
|||||||
github.com/mattn/go-colorable v0.1.13 // indirect
|
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||||
github.com/mattn/go-isatty v0.0.19 // indirect
|
github.com/mattn/go-isatty v0.0.19 // indirect
|
||||||
github.com/mattn/go-runewidth v0.0.13 // indirect
|
github.com/mattn/go-runewidth v0.0.13 // indirect
|
||||||
github.com/mattn/go-sqlite3 v2.0.3+incompatible // indirect
|
github.com/mattn/go-sqlite3 v1.14.19 // indirect
|
||||||
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect
|
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect
|
||||||
github.com/maxymania/go-system v0.0.0-20170110133659-647cc364bf0b // indirect
|
github.com/maxymania/go-system v0.0.0-20170110133659-647cc364bf0b // indirect
|
||||||
github.com/mendsley/gojwk v0.0.0-20141217222730-4d5ec6e58103 // indirect
|
github.com/mendsley/gojwk v0.0.0-20141217222730-4d5ec6e58103 // indirect
|
||||||
@@ -355,3 +355,7 @@ replace github.com/go-micro/plugins/v4/store/nats-js-kv => github.com/kobergj/pl
|
|||||||
replace github.com/studio-b12/gowebdav => github.com/aduffeck/gowebdav v0.0.0-20231215102054-212d4a4374f6
|
replace github.com/studio-b12/gowebdav => github.com/aduffeck/gowebdav v0.0.0-20231215102054-212d4a4374f6
|
||||||
|
|
||||||
replace github.com/egirna/icap-client => github.com/fschade/icap-client v0.0.0-20240105150744-9c2d8aff3ef2
|
replace github.com/egirna/icap-client => github.com/fschade/icap-client v0.0.0-20240105150744-9c2d8aff3ef2
|
||||||
|
|
||||||
|
// exclude the v2 line of go-sqlite3 which was released accidentally and prevents pulling in newer versions of go-sqlite3
|
||||||
|
// see https://github.com/mattn/go-sqlite3/issues/965 for more details
|
||||||
|
exclude github.com/mattn/go-sqlite3 v2.0.3+incompatible
|
||||||
|
|||||||
@@ -1669,8 +1669,8 @@ github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4
|
|||||||
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
|
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
|
||||||
github.com/mattn/go-sqlite3 v1.14.14/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
|
github.com/mattn/go-sqlite3 v1.14.14/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
|
||||||
github.com/mattn/go-sqlite3 v1.14.15/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
|
github.com/mattn/go-sqlite3 v1.14.15/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
|
||||||
github.com/mattn/go-sqlite3 v2.0.3+incompatible h1:gXHsfypPkaMZrKbD5209QV9jbUTJKjyR5WD3HYQSd+U=
|
github.com/mattn/go-sqlite3 v1.14.19 h1:fhGleo2h1p8tVChob4I9HpmVFIAkKGpiukdrgQbWfGI=
|
||||||
github.com/mattn/go-sqlite3 v2.0.3+incompatible/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
|
github.com/mattn/go-sqlite3 v1.14.19/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
|
||||||
github.com/mattn/go-tty v0.0.0-20180219170247-931426f7535a/go.mod h1:XPvLUNfbS4fJH25nqRHfWLMa1ONC8Amw+mIA639KxkE=
|
github.com/mattn/go-tty v0.0.0-20180219170247-931426f7535a/go.mod h1:XPvLUNfbS4fJH25nqRHfWLMa1ONC8Amw+mIA639KxkE=
|
||||||
github.com/mattn/go-tty v0.0.3/go.mod h1:ihxohKRERHTVzN+aSVRwACLCeqIoZAWpoICkkvrWyR0=
|
github.com/mattn/go-tty v0.0.3/go.mod h1:ihxohKRERHTVzN+aSVRwACLCeqIoZAWpoICkkvrWyR0=
|
||||||
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
|
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
|
||||||
|
|||||||
+4
@@ -0,0 +1,4 @@
|
|||||||
|
coverage:
|
||||||
|
status:
|
||||||
|
project: off
|
||||||
|
patch: off
|
||||||
-33
@@ -1,33 +0,0 @@
|
|||||||
language: go
|
|
||||||
|
|
||||||
os:
|
|
||||||
- linux
|
|
||||||
- osx
|
|
||||||
|
|
||||||
addons:
|
|
||||||
apt:
|
|
||||||
update: true
|
|
||||||
|
|
||||||
go:
|
|
||||||
- 1.9.x
|
|
||||||
- 1.10.x
|
|
||||||
- 1.11.x
|
|
||||||
- 1.12.x
|
|
||||||
- 1.13.x
|
|
||||||
- master
|
|
||||||
|
|
||||||
before_install:
|
|
||||||
- |
|
|
||||||
if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then
|
|
||||||
brew update
|
|
||||||
fi
|
|
||||||
- go get github.com/smartystreets/goconvey
|
|
||||||
- go get github.com/mattn/goveralls
|
|
||||||
- go get golang.org/x/tools/cmd/cover
|
|
||||||
|
|
||||||
script:
|
|
||||||
- $HOME/gopath/bin/goveralls -repotoken 3qJVUE0iQwqnCbmNcDsjYu1nh4J4KIFXx
|
|
||||||
- go test -race -v . -tags ""
|
|
||||||
- go test -race -v . -tags "libsqlite3"
|
|
||||||
- go test -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"
|
|
||||||
- go test -race -v . -tags "sqlite_vacuum_full"
|
|
||||||
+101
-70
@@ -1,21 +1,23 @@
|
|||||||
go-sqlite3
|
go-sqlite3
|
||||||
==========
|
==========
|
||||||
|
|
||||||
[](http://godoc.org/github.com/mattn/go-sqlite3)
|
[](https://pkg.go.dev/github.com/mattn/go-sqlite3)
|
||||||
[](https://travis-ci.org/mattn/go-sqlite3)
|
[](https://github.com/mattn/go-sqlite3/actions?query=workflow%3AGo)
|
||||||
[](https://opencollective.com/mattn-go-sqlite3)
|
[](https://opencollective.com/mattn-go-sqlite3)
|
||||||
[](https://coveralls.io/r/mattn/go-sqlite3?branch=master)
|
[](https://codecov.io/gh/mattn/go-sqlite3)
|
||||||
[](https://goreportcard.com/report/github.com/mattn/go-sqlite3)
|
[](https://goreportcard.com/report/github.com/mattn/go-sqlite3)
|
||||||
|
|
||||||
NOTE: v2.0.1 or higher is unfortunatal release. So there are no big changes. And does not provide v2 feature.
|
Latest stable version is v1.14 or later, not v2.
|
||||||
|
|
||||||
|
~~**NOTE:** The increase to v2 was an accident. There were no major changes or features.~~
|
||||||
|
|
||||||
# Description
|
# Description
|
||||||
|
|
||||||
sqlite3 driver conforming to the built-in database/sql interface
|
A sqlite3 driver that conforms to the built-in database/sql interface.
|
||||||
|
|
||||||
Supported Golang version: See .travis.yml
|
Supported Golang version: See [.github/workflows/go.yaml](./.github/workflows/go.yaml).
|
||||||
|
|
||||||
[This package follows the official Golang Release Policy.](https://golang.org/doc/devel/release.html#policy)
|
This package follows the official [Golang Release Policy](https://golang.org/doc/devel/release.html#policy).
|
||||||
|
|
||||||
### Overview
|
### Overview
|
||||||
|
|
||||||
@@ -38,7 +40,7 @@ Supported Golang version: See .travis.yml
|
|||||||
- [Alpine](#alpine)
|
- [Alpine](#alpine)
|
||||||
- [Fedora](#fedora)
|
- [Fedora](#fedora)
|
||||||
- [Ubuntu](#ubuntu)
|
- [Ubuntu](#ubuntu)
|
||||||
- [Mac OSX](#mac-osx)
|
- [macOS](#mac-osx)
|
||||||
- [Windows](#windows)
|
- [Windows](#windows)
|
||||||
- [Errors](#errors)
|
- [Errors](#errors)
|
||||||
- [User Authentication](#user-authentication)
|
- [User Authentication](#user-authentication)
|
||||||
@@ -62,7 +64,7 @@ Supported Golang version: See .travis.yml
|
|||||||
|
|
||||||
# Installation
|
# Installation
|
||||||
|
|
||||||
This package can be installed with the go get command:
|
This package can be installed with the `go get` command:
|
||||||
|
|
||||||
go get github.com/mattn/go-sqlite3
|
go get github.com/mattn/go-sqlite3
|
||||||
|
|
||||||
@@ -70,28 +72,28 @@ _go-sqlite3_ is *cgo* package.
|
|||||||
If you want to build your app using go-sqlite3, you need gcc.
|
If you want to build your app using go-sqlite3, you need gcc.
|
||||||
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.
|
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.
|
||||||
|
|
||||||
***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.***
|
***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.***
|
||||||
|
|
||||||
# API Reference
|
# API Reference
|
||||||
|
|
||||||
API documentation can be found here: http://godoc.org/github.com/mattn/go-sqlite3
|
API documentation can be found [here](http://godoc.org/github.com/mattn/go-sqlite3).
|
||||||
|
|
||||||
Examples can be found under the [examples](./_example) directory
|
Examples can be found under the [examples](./_example) directory.
|
||||||
|
|
||||||
# Connection String
|
# Connection String
|
||||||
|
|
||||||
When creating a new SQLite database or connection to an existing one, with the file name additional options can be given.
|
When creating a new SQLite database or connection to an existing one, with the file name additional options can be given.
|
||||||
This is also known as a DSN string. (Data Source Name).
|
This is also known as a DSN (Data Source Name) string.
|
||||||
|
|
||||||
Options are append after the filename of the SQLite database.
|
Options are append after the filename of the SQLite database.
|
||||||
The database filename and options are seperated by an `?` (Question Mark).
|
The database filename and options are separated by an `?` (Question Mark).
|
||||||
Options should be URL-encoded (see [url.QueryEscape](https://golang.org/pkg/net/url/#QueryEscape)).
|
Options should be URL-encoded (see [url.QueryEscape](https://golang.org/pkg/net/url/#QueryEscape)).
|
||||||
|
|
||||||
This also applies when using an in-memory database instead of a file.
|
This also applies when using an in-memory database instead of a file.
|
||||||
|
|
||||||
Options can be given using the following format: `KEYWORD=VALUE` and multiple options can be combined with the `&` ampersand.
|
Options can be given using the following format: `KEYWORD=VALUE` and multiple options can be combined with the `&` ampersand.
|
||||||
|
|
||||||
This library supports dsn options of SQLite itself and provides additional options.
|
This library supports DSN options of SQLite itself and provides additional options.
|
||||||
|
|
||||||
Boolean values can be one of:
|
Boolean values can be one of:
|
||||||
* `0` `no` `false` `off`
|
* `0` `no` `false` `off`
|
||||||
@@ -123,6 +125,8 @@ Boolean values can be one of:
|
|||||||
| Time Zone Location | `_loc` | auto | Specify location of time format. |
|
| Time Zone Location | `_loc` | auto | Specify location of time format. |
|
||||||
| Transaction Lock | `_txlock` | <ul><li>immediate</li><li>deferred</li><li>exclusive</li></ul> | Specify locking behavior for transactions. |
|
| Transaction Lock | `_txlock` | <ul><li>immediate</li><li>deferred</li><li>exclusive</li></ul> | Specify locking behavior for transactions. |
|
||||||
| Writable Schema | `_writable_schema` | `Boolean` | When this pragma is on, the SQLITE_MASTER tables in which database can be changed using ordinary UPDATE, INSERT, and DELETE statements. Warning: misuse of this pragma can easily result in a corrupt database file. |
|
| Writable Schema | `_writable_schema` | `Boolean` | When this pragma is on, the SQLITE_MASTER tables in which database can be changed using ordinary UPDATE, INSERT, and DELETE statements. Warning: misuse of this pragma can easily result in a corrupt database file. |
|
||||||
|
| Cache Size | `_cache_size` | `int` | Maximum cache size; default is 2000K (2M). See [PRAGMA cache_size](https://sqlite.org/pragma.html#pragma_cache_size) |
|
||||||
|
|
||||||
|
|
||||||
## DSN Examples
|
## DSN Examples
|
||||||
|
|
||||||
@@ -134,24 +138,23 @@ file:test.db?cache=shared&mode=memory
|
|||||||
|
|
||||||
This package allows additional configuration of features available within SQLite3 to be enabled or disabled by golang build constraints also known as build `tags`.
|
This package allows additional configuration of features available within SQLite3 to be enabled or disabled by golang build constraints also known as build `tags`.
|
||||||
|
|
||||||
[Click here for more information about build tags / constraints.](https://golang.org/pkg/go/build/#hdr-Build_Constraints)
|
Click [here](https://golang.org/pkg/go/build/#hdr-Build_Constraints) for more information about build tags / constraints.
|
||||||
|
|
||||||
### Usage
|
### Usage
|
||||||
|
|
||||||
If you wish to build this library with additional extensions / features.
|
If you wish to build this library with additional extensions / features, use the following command:
|
||||||
Use the following command.
|
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
go build --tags "<FEATURE>"
|
go build -tags "<FEATURE>"
|
||||||
```
|
```
|
||||||
|
|
||||||
For available features see the extension list.
|
For available features, see the extension list.
|
||||||
When using multiple build tags, all the different tags should be space delimted.
|
When using multiple build tags, all the different tags should be space delimited.
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
go build --tags "icu json1 fts5 secure_delete"
|
go build -tags "icu json1 fts5 secure_delete"
|
||||||
```
|
```
|
||||||
|
|
||||||
### Feature / Extension List
|
### Feature / Extension List
|
||||||
@@ -162,6 +165,7 @@ go build --tags "icu json1 fts5 secure_delete"
|
|||||||
| 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 |
|
| 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 |
|
||||||
| 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`. |
|
| 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`. |
|
||||||
| 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`. |
|
| 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`. |
|
||||||
|
| 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`. |
|
||||||
| 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 |
|
| 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 |
|
||||||
| Full Auto Vacuum | sqlite_vacuum_full | Set the default auto vacuum to full |
|
| Full Auto Vacuum | sqlite_vacuum_full | Set the default auto vacuum to full |
|
||||||
| Incremental Auto Vacuum | sqlite_vacuum_incr | Set the default auto vacuum to incremental |
|
| Incremental Auto Vacuum | sqlite_vacuum_incr | Set the default auto vacuum to incremental |
|
||||||
@@ -169,17 +173,20 @@ go build --tags "icu json1 fts5 secure_delete"
|
|||||||
| 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 |
|
| 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 |
|
||||||
| 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> |
|
| 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> |
|
||||||
| JSON SQL Functions | sqlite_json | When this option is defined in the amalgamation, the JSON SQL functions are added to the build automatically |
|
| JSON SQL Functions | sqlite_json | When this option is defined in the amalgamation, the JSON SQL functions are added to the build automatically |
|
||||||
|
| 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) |
|
||||||
|
| OS Trace | sqlite_os_trace | This option enables OSTRACE() debug logging. This can be verbose and should not be used in production. |
|
||||||
| 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. |
|
| 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. |
|
||||||
| 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 |
|
| 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 |
|
||||||
| Secure Delete (FAST) | sqlite_secure_delete_fast | For more information see [PRAGMA secure_delete](https://www.sqlite.org/pragma.html#pragma_secure_delete) |
|
| Secure Delete (FAST) | sqlite_secure_delete_fast | For more information see [PRAGMA secure_delete](https://www.sqlite.org/pragma.html#pragma_secure_delete) |
|
||||||
| Tracing / Debug | sqlite_trace | Activate trace functions |
|
| Tracing / Debug | sqlite_trace | Activate trace functions |
|
||||||
| User Authentication | sqlite_userauth | SQLite User Authentication see [User Authentication](#user-authentication) for more information. |
|
| User Authentication | sqlite_userauth | SQLite User Authentication see [User Authentication](#user-authentication) for more information. |
|
||||||
|
| 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) |
|
||||||
|
|
||||||
# Compilation
|
# Compilation
|
||||||
|
|
||||||
This package requires `CGO_ENABLED=1` ennvironment variable if not set by default, and the presence of the `gcc` compiler.
|
This package requires the `CGO_ENABLED=1` environment variable if not set by default, and the presence of the `gcc` compiler.
|
||||||
|
|
||||||
If you need to add additional CFLAGS or LDFLAGS to the build command, and do not want to modify this package. Then this can be achieved by using the `CGO_CFLAGS` and `CGO_LDFLAGS` environment variables.
|
If you need to add additional CFLAGS or LDFLAGS to the build command, and do not want to modify this package, then this can be achieved by using the `CGO_CFLAGS` and `CGO_LDFLAGS` environment variables.
|
||||||
|
|
||||||
## Android
|
## Android
|
||||||
|
|
||||||
@@ -187,14 +194,14 @@ This package can be compiled for android.
|
|||||||
Compile with:
|
Compile with:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
go build --tags "android"
|
go build -tags "android"
|
||||||
```
|
```
|
||||||
|
|
||||||
For more information see [#201](https://github.com/mattn/go-sqlite3/issues/201)
|
For more information see [#201](https://github.com/mattn/go-sqlite3/issues/201)
|
||||||
|
|
||||||
# ARM
|
# ARM
|
||||||
|
|
||||||
To compile for `ARM` use the following environment.
|
To compile for `ARM` use the following environment:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
env CC=arm-linux-gnueabihf-gcc CXX=arm-linux-gnueabihf-g++ \
|
env CC=arm-linux-gnueabihf-gcc CXX=arm-linux-gnueabihf-g++ \
|
||||||
@@ -212,9 +219,14 @@ This library can be cross-compiled.
|
|||||||
|
|
||||||
In some cases you are required to the `CC` environment variable with the cross compiler.
|
In some cases you are required to the `CC` environment variable with the cross compiler.
|
||||||
|
|
||||||
Additional information:
|
## Cross Compiling from macOS
|
||||||
- [#491](https://github.com/mattn/go-sqlite3/issues/491)
|
The simplest way to cross compile from macOS is to use [xgo](https://github.com/karalabe/xgo).
|
||||||
- [#560](https://github.com/mattn/go-sqlite3/issues/560)
|
|
||||||
|
Steps:
|
||||||
|
- Install [musl-cross](https://github.com/FiloSottile/homebrew-musl-cross) (`brew install FiloSottile/musl-cross/musl-cross`).
|
||||||
|
- 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"`.
|
||||||
|
|
||||||
|
Please refer to the project's [README](https://github.com/FiloSottile/homebrew-musl-cross#readme) for further information.
|
||||||
|
|
||||||
# Google Cloud Platform
|
# Google Cloud Platform
|
||||||
|
|
||||||
@@ -224,23 +236,23 @@ Please work only with compiled final binaries.
|
|||||||
|
|
||||||
## Linux
|
## Linux
|
||||||
|
|
||||||
To compile this package on Linux you must install the development tools for your linux distribution.
|
To compile this package on Linux, you must install the development tools for your linux distribution.
|
||||||
|
|
||||||
To compile under linux use the build tag `linux`.
|
To compile under linux use the build tag `linux`.
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
go build --tags "linux"
|
go build -tags "linux"
|
||||||
```
|
```
|
||||||
|
|
||||||
If you wish to link directly to libsqlite3 then you can use the `libsqlite3` build tag.
|
If you wish to link directly to libsqlite3 then you can use the `libsqlite3` build tag.
|
||||||
|
|
||||||
```
|
```
|
||||||
go build --tags "libsqlite3 linux"
|
go build -tags "libsqlite3 linux"
|
||||||
```
|
```
|
||||||
|
|
||||||
### Alpine
|
### Alpine
|
||||||
|
|
||||||
When building in an `alpine` container run the following command before building.
|
When building in an `alpine` container run the following command before building:
|
||||||
|
|
||||||
```
|
```
|
||||||
apk add --update gcc musl-dev
|
apk add --update gcc musl-dev
|
||||||
@@ -258,34 +270,43 @@ sudo yum groupinstall "Development Tools" "Development Libraries"
|
|||||||
sudo apt-get install build-essential
|
sudo apt-get install build-essential
|
||||||
```
|
```
|
||||||
|
|
||||||
## Mac OSX
|
## macOS
|
||||||
|
|
||||||
OSX should have all the tools present to compile this package, if not install XCode this will add all the developers tools.
|
macOS should have all the tools present to compile this package. If not, install XCode to add all the developers tools.
|
||||||
|
|
||||||
Required dependency
|
Required dependency:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
brew install sqlite3
|
brew install sqlite3
|
||||||
```
|
```
|
||||||
|
|
||||||
For OSX there is an additional package install which is required if you wish to build the `icu` extension.
|
For macOS, there is an additional package to install which is required if you wish to build the `icu` extension.
|
||||||
|
|
||||||
This additional package can be installed with `homebrew`.
|
This additional package can be installed with `homebrew`:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
brew upgrade icu4c
|
brew upgrade icu4c
|
||||||
```
|
```
|
||||||
|
|
||||||
To compile for Mac OSX.
|
To compile for macOS on x86:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
go build --tags "darwin"
|
go build -tags "darwin amd64"
|
||||||
```
|
```
|
||||||
|
|
||||||
If you wish to link directly to libsqlite3 then you can use the `libsqlite3` build tag.
|
To compile for macOS on ARM chips:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
go build -tags "darwin arm64"
|
||||||
|
```
|
||||||
|
|
||||||
|
If you wish to link directly to libsqlite3, use the `libsqlite3` build tag:
|
||||||
|
|
||||||
```
|
```
|
||||||
go build --tags "libsqlite3 darwin"
|
# x86
|
||||||
|
go build -tags "libsqlite3 darwin amd64"
|
||||||
|
# ARM
|
||||||
|
go build -tags "libsqlite3 darwin arm64"
|
||||||
```
|
```
|
||||||
|
|
||||||
Additional information:
|
Additional information:
|
||||||
@@ -294,14 +315,14 @@ Additional information:
|
|||||||
|
|
||||||
## Windows
|
## Windows
|
||||||
|
|
||||||
To compile this package on Windows OS you must have the `gcc` compiler installed.
|
To compile this package on Windows, you must have the `gcc` compiler installed.
|
||||||
|
|
||||||
1) Install a Windows `gcc` toolchain.
|
1) Install a Windows `gcc` toolchain.
|
||||||
2) Add the `bin` folders to the Windows path if the installer did not do this by default.
|
2) Add the `bin` folder to the Windows path, if the installer did not do this by default.
|
||||||
3) Open a terminal for the TDM-GCC toolchain, can be found in the Windows Start menu.
|
3) Open a terminal for the TDM-GCC toolchain, which can be found in the Windows Start menu.
|
||||||
4) Navigate to your project folder and run the `go build ...` command for this package.
|
4) Navigate to your project folder and run the `go build ...` command for this package.
|
||||||
|
|
||||||
For example the TDM-GCC Toolchain can be found [here](https://sourceforge.net/projects/tdm-gcc/).
|
For example the TDM-GCC Toolchain can be found [here](https://jmeubank.github.io/tdm-gcc/).
|
||||||
|
|
||||||
## Errors
|
## Errors
|
||||||
|
|
||||||
@@ -339,28 +360,28 @@ This package supports the SQLite User Authentication module.
|
|||||||
|
|
||||||
## Compile
|
## Compile
|
||||||
|
|
||||||
To use the User authentication module the package has to be compiled with the tag `sqlite_userauth`. See [Features](#features).
|
To use the User authentication module, the package has to be compiled with the tag `sqlite_userauth`. See [Features](#features).
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
### Create protected database
|
### Create protected database
|
||||||
|
|
||||||
To create a database protected by user authentication provide the following argument to the connection string `_auth`.
|
To create a database protected by user authentication, provide the following argument to the connection string `_auth`.
|
||||||
This will enable user authentication within the database. This option however requires two additional arguments:
|
This will enable user authentication within the database. This option however requires two additional arguments:
|
||||||
|
|
||||||
- `_auth_user`
|
- `_auth_user`
|
||||||
- `_auth_pass`
|
- `_auth_pass`
|
||||||
|
|
||||||
When `_auth` is present on the connection string user authentication will be enabled and the provided user will be created
|
When `_auth` is present in the connection string user authentication will be enabled and the provided user will be created
|
||||||
as an `admin` user. After initial creation, the parameter `_auth` has no effect anymore and can be omitted from the connection string.
|
as an `admin` user. After initial creation, the parameter `_auth` has no effect anymore and can be omitted from the connection string.
|
||||||
|
|
||||||
Example connection string:
|
Example connection strings:
|
||||||
|
|
||||||
Create an user authentication database with user `admin` and password `admin`.
|
Create an user authentication database with user `admin` and password `admin`:
|
||||||
|
|
||||||
`file:test.s3db?_auth&_auth_user=admin&_auth_pass=admin`
|
`file:test.s3db?_auth&_auth_user=admin&_auth_pass=admin`
|
||||||
|
|
||||||
Create an user authentication database with user `admin` and password `admin` and use `SHA1` for the password encoding.
|
Create an user authentication database with user `admin` and password `admin` and use `SHA1` for the password encoding:
|
||||||
|
|
||||||
`file:test.s3db?_auth&_auth_user=admin&_auth_pass=admin&_auth_crypt=sha1`
|
`file:test.s3db?_auth&_auth_user=admin&_auth_pass=admin&_auth_crypt=sha1`
|
||||||
|
|
||||||
@@ -386,11 +407,11 @@ salt this can be configured with `_auth_salt`.
|
|||||||
|
|
||||||
### Restrictions
|
### Restrictions
|
||||||
|
|
||||||
Operations on the database regarding to user management can only be preformed by an administrator user.
|
Operations on the database regarding user management can only be preformed by an administrator user.
|
||||||
|
|
||||||
### Support
|
### Support
|
||||||
|
|
||||||
The user authentication supports two kinds of users
|
The user authentication supports two kinds of users:
|
||||||
|
|
||||||
- administrators
|
- administrators
|
||||||
- regular users
|
- regular users
|
||||||
@@ -401,7 +422,7 @@ User management can be done by directly using the `*SQLiteConn` or by SQL.
|
|||||||
|
|
||||||
#### SQL
|
#### SQL
|
||||||
|
|
||||||
The following sql functions are available for user management.
|
The following sql functions are available for user management:
|
||||||
|
|
||||||
| Function | Arguments | Description |
|
| Function | Arguments | Description |
|
||||||
|----------|-----------|-------------|
|
|----------|-----------|-------------|
|
||||||
@@ -410,7 +431,7 @@ The following sql functions are available for user management.
|
|||||||
| `auth_user_change` | username `string`, password `string`, admin `int` | Function to modify an user. Users can change their own password, but only an administrator can change the administrator flag. |
|
| `auth_user_change` | username `string`, password `string`, admin `int` | Function to modify an user. Users can change their own password, but only an administrator can change the administrator flag. |
|
||||||
| `authUserDelete` | username `string` | Delete an user from the database. Can only be used by an administrator. The current logged in administrator cannot be deleted. This is to make sure their is always an administrator remaining. |
|
| `authUserDelete` | username `string` | Delete an user from the database. Can only be used by an administrator. The current logged in administrator cannot be deleted. This is to make sure their is always an administrator remaining. |
|
||||||
|
|
||||||
These functions will return an integer.
|
These functions will return an integer:
|
||||||
|
|
||||||
- 0 (SQLITE_OK)
|
- 0 (SQLITE_OK)
|
||||||
- 23 (SQLITE_AUTH) Failed to perform due to authentication or insufficient privileges
|
- 23 (SQLITE_AUTH) Failed to perform due to authentication or insufficient privileges
|
||||||
@@ -431,7 +452,7 @@ SELECT user_delete('user');
|
|||||||
|
|
||||||
#### *SQLiteConn
|
#### *SQLiteConn
|
||||||
|
|
||||||
The following functions are available for User authentication from the `*SQLiteConn`.
|
The following functions are available for User authentication from the `*SQLiteConn`:
|
||||||
|
|
||||||
| Function | Description |
|
| Function | Description |
|
||||||
|----------|-------------|
|
|----------|-------------|
|
||||||
@@ -442,16 +463,26 @@ The following functions are available for User authentication from the `*SQLiteC
|
|||||||
|
|
||||||
### Attached database
|
### Attached database
|
||||||
|
|
||||||
When using attached databases. SQLite will use the authentication from the `main` database for the attached database(s).
|
When using attached databases, SQLite will use the authentication from the `main` database for the attached database(s).
|
||||||
|
|
||||||
# Extensions
|
# Extensions
|
||||||
|
|
||||||
If you want your own extension to be listed here or you want to add a reference to an extension; please submit an Issue for this.
|
If you want your own extension to be listed here, or you want to add a reference to an extension; please submit an Issue for this.
|
||||||
|
|
||||||
## Spatialite
|
## Spatialite
|
||||||
|
|
||||||
Spatialite is available as an extension to SQLite, and can be used in combination with this repository.
|
Spatialite is available as an extension to SQLite, and can be used in combination with this repository.
|
||||||
For an example see [shaxbee/go-spatialite](https://github.com/shaxbee/go-spatialite).
|
For an example, see [shaxbee/go-spatialite](https://github.com/shaxbee/go-spatialite).
|
||||||
|
|
||||||
|
## extension-functions.c from SQLite3 Contrib
|
||||||
|
|
||||||
|
extension-functions.c is available as an extension to SQLite, and provides the following functions:
|
||||||
|
|
||||||
|
- Math: acos, asin, atan, atn2, atan2, acosh, asinh, atanh, difference, degrees, radians, cos, sin, tan, cot, cosh, sinh, tanh, coth, exp, log, log10, power, sign, sqrt, square, ceil, floor, pi.
|
||||||
|
- String: replicate, charindex, leftstr, rightstr, ltrim, rtrim, trim, replace, reverse, proper, padl, padr, padc, strfilter.
|
||||||
|
- Aggregate: stdev, variance, mode, median, lower_quartile, upper_quartile
|
||||||
|
|
||||||
|
For an example, see [dinedal/go-sqlite3-extension-functions](https://github.com/dinedal/go-sqlite3-extension-functions).
|
||||||
|
|
||||||
# FAQ
|
# FAQ
|
||||||
|
|
||||||
@@ -471,7 +502,7 @@ For an example see [shaxbee/go-spatialite](https://github.com/shaxbee/go-spatial
|
|||||||
|
|
||||||
- Can I use this in multiple routines concurrently?
|
- Can I use this in multiple routines concurrently?
|
||||||
|
|
||||||
Yes for readonly. But, No for writable. See [#50](https://github.com/mattn/go-sqlite3/issues/50), [#51](https://github.com/mattn/go-sqlite3/issues/51), [#209](https://github.com/mattn/go-sqlite3/issues/209), [#274](https://github.com/mattn/go-sqlite3/issues/274).
|
Yes for readonly. But not for writable. See [#50](https://github.com/mattn/go-sqlite3/issues/50), [#51](https://github.com/mattn/go-sqlite3/issues/51), [#209](https://github.com/mattn/go-sqlite3/issues/209), [#274](https://github.com/mattn/go-sqlite3/issues/274).
|
||||||
|
|
||||||
- Why I'm getting `no such table` error?
|
- Why I'm getting `no such table` error?
|
||||||
|
|
||||||
@@ -485,7 +516,7 @@ For an example see [shaxbee/go-spatialite](https://github.com/shaxbee/go-spatial
|
|||||||
|
|
||||||
Note that if the last database connection in the pool closes, the in-memory database is deleted. Make sure the [max idle connection limit](https://golang.org/pkg/database/sql/#DB.SetMaxIdleConns) is > 0, and the [connection lifetime](https://golang.org/pkg/database/sql/#DB.SetConnMaxLifetime) is infinite.
|
Note that if the last database connection in the pool closes, the in-memory database is deleted. Make sure the [max idle connection limit](https://golang.org/pkg/database/sql/#DB.SetMaxIdleConns) is > 0, and the [connection lifetime](https://golang.org/pkg/database/sql/#DB.SetConnMaxLifetime) is infinite.
|
||||||
|
|
||||||
For more information see
|
For more information see:
|
||||||
* [#204](https://github.com/mattn/go-sqlite3/issues/204)
|
* [#204](https://github.com/mattn/go-sqlite3/issues/204)
|
||||||
* [#511](https://github.com/mattn/go-sqlite3/issues/511)
|
* [#511](https://github.com/mattn/go-sqlite3/issues/511)
|
||||||
* https://www.sqlite.org/sharedcache.html#shared_cache_and_in_memory_databases
|
* https://www.sqlite.org/sharedcache.html#shared_cache_and_in_memory_databases
|
||||||
@@ -495,20 +526,20 @@ For an example see [shaxbee/go-spatialite](https://github.com/shaxbee/go-spatial
|
|||||||
|
|
||||||
OS X limits OS-wide to not have more than 1000 files open simultaneously by default.
|
OS X limits OS-wide to not have more than 1000 files open simultaneously by default.
|
||||||
|
|
||||||
For more information see [#289](https://github.com/mattn/go-sqlite3/issues/289)
|
For more information, see [#289](https://github.com/mattn/go-sqlite3/issues/289)
|
||||||
|
|
||||||
- Trying to execute a `.` (dot) command throws an error.
|
- Trying to execute a `.` (dot) command throws an error.
|
||||||
|
|
||||||
Error: `Error: near ".": syntax error`
|
Error: `Error: near ".": syntax error`
|
||||||
Dot command are part of SQLite3 CLI not of this library.
|
Dot command are part of SQLite3 CLI, not of this library.
|
||||||
|
|
||||||
You need to implement the feature or call the sqlite3 cli.
|
You need to implement the feature or call the sqlite3 cli.
|
||||||
|
|
||||||
More information see [#305](https://github.com/mattn/go-sqlite3/issues/305)
|
More information see [#305](https://github.com/mattn/go-sqlite3/issues/305).
|
||||||
|
|
||||||
- Error: `database is locked`
|
- Error: `database is locked`
|
||||||
|
|
||||||
When you get a database is locked. Please use the following options.
|
When you get a database is locked, please use the following options.
|
||||||
|
|
||||||
Add to DSN: `cache=shared`
|
Add to DSN: `cache=shared`
|
||||||
|
|
||||||
@@ -517,24 +548,24 @@ For an example see [shaxbee/go-spatialite](https://github.com/shaxbee/go-spatial
|
|||||||
db, err := sql.Open("sqlite3", "file:locked.sqlite?cache=shared")
|
db, err := sql.Open("sqlite3", "file:locked.sqlite?cache=shared")
|
||||||
```
|
```
|
||||||
|
|
||||||
Second please set the database connections of the SQL package to 1.
|
Next, please set the database connections of the SQL package to 1:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
db.SetMaxOpenConns(1)
|
db.SetMaxOpenConns(1)
|
||||||
```
|
```
|
||||||
|
|
||||||
More information see [#209](https://github.com/mattn/go-sqlite3/issues/209)
|
For more information, see [#209](https://github.com/mattn/go-sqlite3/issues/209).
|
||||||
|
|
||||||
## Contributors
|
## Contributors
|
||||||
|
|
||||||
### Code Contributors
|
### Code Contributors
|
||||||
|
|
||||||
This project exists thanks to all the people who contribute. [[Contribute](CONTRIBUTING.md)].
|
This project exists thanks to all the people who [[contribute](CONTRIBUTING.md)].
|
||||||
<a href="https://github.com/mattn/go-sqlite3/graphs/contributors"><img src="https://opencollective.com/mattn-go-sqlite3/contributors.svg?width=890&button=false" /></a>
|
<a href="https://github.com/mattn/go-sqlite3/graphs/contributors"><img src="https://opencollective.com/mattn-go-sqlite3/contributors.svg?width=890&button=false" /></a>
|
||||||
|
|
||||||
### Financial Contributors
|
### Financial Contributors
|
||||||
|
|
||||||
Become a financial contributor and help us sustain our community. [[Contribute](https://opencollective.com/mattn-go-sqlite3/contribute)]
|
Become a financial contributor and help us sustain our community. [[Contribute here](https://opencollective.com/mattn-go-sqlite3/contribute)].
|
||||||
|
|
||||||
#### Individuals
|
#### Individuals
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -7,7 +7,7 @@ package sqlite3
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
#ifndef USE_LIBSQLITE3
|
#ifndef USE_LIBSQLITE3
|
||||||
#include <sqlite3-binding.h>
|
#include "sqlite3-binding.h"
|
||||||
#else
|
#else
|
||||||
#include <sqlite3.h>
|
#include <sqlite3.h>
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
+45
-32
@@ -12,7 +12,7 @@ package sqlite3
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
#ifndef USE_LIBSQLITE3
|
#ifndef USE_LIBSQLITE3
|
||||||
#include <sqlite3-binding.h>
|
#include "sqlite3-binding.h"
|
||||||
#else
|
#else
|
||||||
#include <sqlite3.h>
|
#include <sqlite3.h>
|
||||||
#endif
|
#endif
|
||||||
@@ -35,56 +35,55 @@ import (
|
|||||||
//export callbackTrampoline
|
//export callbackTrampoline
|
||||||
func callbackTrampoline(ctx *C.sqlite3_context, argc int, argv **C.sqlite3_value) {
|
func callbackTrampoline(ctx *C.sqlite3_context, argc int, argv **C.sqlite3_value) {
|
||||||
args := (*[(math.MaxInt32 - 1) / unsafe.Sizeof((*C.sqlite3_value)(nil))]*C.sqlite3_value)(unsafe.Pointer(argv))[:argc:argc]
|
args := (*[(math.MaxInt32 - 1) / unsafe.Sizeof((*C.sqlite3_value)(nil))]*C.sqlite3_value)(unsafe.Pointer(argv))[:argc:argc]
|
||||||
fi := lookupHandle(uintptr(C.sqlite3_user_data(ctx))).(*functionInfo)
|
fi := lookupHandle(C.sqlite3_user_data(ctx)).(*functionInfo)
|
||||||
fi.Call(ctx, args)
|
fi.Call(ctx, args)
|
||||||
}
|
}
|
||||||
|
|
||||||
//export stepTrampoline
|
//export stepTrampoline
|
||||||
func stepTrampoline(ctx *C.sqlite3_context, argc C.int, argv **C.sqlite3_value) {
|
func stepTrampoline(ctx *C.sqlite3_context, argc C.int, argv **C.sqlite3_value) {
|
||||||
args := (*[(math.MaxInt32 - 1) / unsafe.Sizeof((*C.sqlite3_value)(nil))]*C.sqlite3_value)(unsafe.Pointer(argv))[:int(argc):int(argc)]
|
args := (*[(math.MaxInt32 - 1) / unsafe.Sizeof((*C.sqlite3_value)(nil))]*C.sqlite3_value)(unsafe.Pointer(argv))[:int(argc):int(argc)]
|
||||||
ai := lookupHandle(uintptr(C.sqlite3_user_data(ctx))).(*aggInfo)
|
ai := lookupHandle(C.sqlite3_user_data(ctx)).(*aggInfo)
|
||||||
ai.Step(ctx, args)
|
ai.Step(ctx, args)
|
||||||
}
|
}
|
||||||
|
|
||||||
//export doneTrampoline
|
//export doneTrampoline
|
||||||
func doneTrampoline(ctx *C.sqlite3_context) {
|
func doneTrampoline(ctx *C.sqlite3_context) {
|
||||||
handle := uintptr(C.sqlite3_user_data(ctx))
|
ai := lookupHandle(C.sqlite3_user_data(ctx)).(*aggInfo)
|
||||||
ai := lookupHandle(handle).(*aggInfo)
|
|
||||||
ai.Done(ctx)
|
ai.Done(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
//export compareTrampoline
|
//export compareTrampoline
|
||||||
func compareTrampoline(handlePtr uintptr, la C.int, a *C.char, lb C.int, b *C.char) C.int {
|
func compareTrampoline(handlePtr unsafe.Pointer, la C.int, a *C.char, lb C.int, b *C.char) C.int {
|
||||||
cmp := lookupHandle(handlePtr).(func(string, string) int)
|
cmp := lookupHandle(handlePtr).(func(string, string) int)
|
||||||
return C.int(cmp(C.GoStringN(a, la), C.GoStringN(b, lb)))
|
return C.int(cmp(C.GoStringN(a, la), C.GoStringN(b, lb)))
|
||||||
}
|
}
|
||||||
|
|
||||||
//export commitHookTrampoline
|
//export commitHookTrampoline
|
||||||
func commitHookTrampoline(handle uintptr) int {
|
func commitHookTrampoline(handle unsafe.Pointer) int {
|
||||||
callback := lookupHandle(handle).(func() int)
|
callback := lookupHandle(handle).(func() int)
|
||||||
return callback()
|
return callback()
|
||||||
}
|
}
|
||||||
|
|
||||||
//export rollbackHookTrampoline
|
//export rollbackHookTrampoline
|
||||||
func rollbackHookTrampoline(handle uintptr) {
|
func rollbackHookTrampoline(handle unsafe.Pointer) {
|
||||||
callback := lookupHandle(handle).(func())
|
callback := lookupHandle(handle).(func())
|
||||||
callback()
|
callback()
|
||||||
}
|
}
|
||||||
|
|
||||||
//export updateHookTrampoline
|
//export updateHookTrampoline
|
||||||
func updateHookTrampoline(handle uintptr, op int, db *C.char, table *C.char, rowid int64) {
|
func updateHookTrampoline(handle unsafe.Pointer, op int, db *C.char, table *C.char, rowid int64) {
|
||||||
callback := lookupHandle(handle).(func(int, string, string, int64))
|
callback := lookupHandle(handle).(func(int, string, string, int64))
|
||||||
callback(op, C.GoString(db), C.GoString(table), rowid)
|
callback(op, C.GoString(db), C.GoString(table), rowid)
|
||||||
}
|
}
|
||||||
|
|
||||||
//export authorizerTrampoline
|
//export authorizerTrampoline
|
||||||
func authorizerTrampoline(handle uintptr, op int, arg1 *C.char, arg2 *C.char, arg3 *C.char) int {
|
func authorizerTrampoline(handle unsafe.Pointer, op int, arg1 *C.char, arg2 *C.char, arg3 *C.char) int {
|
||||||
callback := lookupHandle(handle).(func(int, string, string, string) int)
|
callback := lookupHandle(handle).(func(int, string, string, string) int)
|
||||||
return callback(op, C.GoString(arg1), C.GoString(arg2), C.GoString(arg3))
|
return callback(op, C.GoString(arg1), C.GoString(arg2), C.GoString(arg3))
|
||||||
}
|
}
|
||||||
|
|
||||||
//export preUpdateHookTrampoline
|
//export preUpdateHookTrampoline
|
||||||
func preUpdateHookTrampoline(handle uintptr, dbHandle uintptr, op int, db *C.char, table *C.char, oldrowid int64, newrowid int64) {
|
func preUpdateHookTrampoline(handle unsafe.Pointer, dbHandle uintptr, op int, db *C.char, table *C.char, oldrowid int64, newrowid int64) {
|
||||||
hval := lookupHandleVal(handle)
|
hval := lookupHandleVal(handle)
|
||||||
data := SQLitePreUpdateData{
|
data := SQLitePreUpdateData{
|
||||||
Conn: hval.db,
|
Conn: hval.db,
|
||||||
@@ -105,33 +104,27 @@ type handleVal struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var handleLock sync.Mutex
|
var handleLock sync.Mutex
|
||||||
var handleVals = make(map[uintptr]handleVal)
|
var handleVals = make(map[unsafe.Pointer]handleVal)
|
||||||
var handleIndex uintptr = 100
|
|
||||||
|
|
||||||
func newHandle(db *SQLiteConn, v interface{}) uintptr {
|
func newHandle(db *SQLiteConn, v interface{}) unsafe.Pointer {
|
||||||
handleLock.Lock()
|
handleLock.Lock()
|
||||||
defer handleLock.Unlock()
|
defer handleLock.Unlock()
|
||||||
i := handleIndex
|
val := handleVal{db: db, val: v}
|
||||||
handleIndex++
|
var p unsafe.Pointer = C.malloc(C.size_t(1))
|
||||||
handleVals[i] = handleVal{db, v}
|
if p == nil {
|
||||||
return i
|
panic("can't allocate 'cgo-pointer hack index pointer': ptr == nil")
|
||||||
}
|
|
||||||
|
|
||||||
func lookupHandleVal(handle uintptr) handleVal {
|
|
||||||
handleLock.Lock()
|
|
||||||
defer handleLock.Unlock()
|
|
||||||
r, ok := handleVals[handle]
|
|
||||||
if !ok {
|
|
||||||
if handle >= 100 && handle < handleIndex {
|
|
||||||
panic("deleted handle")
|
|
||||||
} else {
|
|
||||||
panic("invalid handle")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return r
|
handleVals[p] = val
|
||||||
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
func lookupHandle(handle uintptr) interface{} {
|
func lookupHandleVal(handle unsafe.Pointer) handleVal {
|
||||||
|
handleLock.Lock()
|
||||||
|
defer handleLock.Unlock()
|
||||||
|
return handleVals[handle]
|
||||||
|
}
|
||||||
|
|
||||||
|
func lookupHandle(handle unsafe.Pointer) interface{} {
|
||||||
return lookupHandleVal(handle).val
|
return lookupHandleVal(handle).val
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -141,6 +134,7 @@ func deleteHandles(db *SQLiteConn) {
|
|||||||
for handle, val := range handleVals {
|
for handle, val := range handleVals {
|
||||||
if val.db == db {
|
if val.db == db {
|
||||||
delete(handleVals, handle)
|
delete(handleVals, handle)
|
||||||
|
C.free(handle)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -359,6 +353,20 @@ func callbackRetNil(ctx *C.sqlite3_context, v reflect.Value) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func callbackRetGeneric(ctx *C.sqlite3_context, v reflect.Value) error {
|
||||||
|
if v.IsNil() {
|
||||||
|
C.sqlite3_result_null(ctx)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
cb, err := callbackRet(v.Elem().Type())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return cb(ctx, v.Elem())
|
||||||
|
}
|
||||||
|
|
||||||
func callbackRet(typ reflect.Type) (callbackRetConverter, error) {
|
func callbackRet(typ reflect.Type) (callbackRetConverter, error) {
|
||||||
switch typ.Kind() {
|
switch typ.Kind() {
|
||||||
case reflect.Interface:
|
case reflect.Interface:
|
||||||
@@ -366,6 +374,11 @@ func callbackRet(typ reflect.Type) (callbackRetConverter, error) {
|
|||||||
if typ.Implements(errorInterface) {
|
if typ.Implements(errorInterface) {
|
||||||
return callbackRetNil, nil
|
return callbackRetNil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if typ.NumMethod() == 0 {
|
||||||
|
return callbackRetGeneric, nil
|
||||||
|
}
|
||||||
|
|
||||||
fallthrough
|
fallthrough
|
||||||
case reflect.Slice:
|
case reflect.Slice:
|
||||||
if typ.Elem().Kind() != reflect.Uint8 {
|
if typ.Elem().Kind() != reflect.Uint8 {
|
||||||
|
|||||||
+29
-6
@@ -79,9 +79,8 @@ Then, you can use this extension.
|
|||||||
|
|
||||||
Connection Hook
|
Connection Hook
|
||||||
|
|
||||||
You can hook and inject your code when the connection is established. database/sql
|
You can hook and inject your code when the connection is established by setting
|
||||||
doesn't provide a way to get native go-sqlite3 interfaces. So if you want,
|
ConnectHook to get the SQLiteConn.
|
||||||
you need to set ConnectHook and get the SQLiteConn.
|
|
||||||
|
|
||||||
sql.Register("sqlite3_with_hook_example",
|
sql.Register("sqlite3_with_hook_example",
|
||||||
&sqlite3.SQLiteDriver{
|
&sqlite3.SQLiteDriver{
|
||||||
@@ -91,21 +90,45 @@ you need to set ConnectHook and get the SQLiteConn.
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
You can also use database/sql.Conn.Raw (Go >= 1.13):
|
||||||
|
|
||||||
|
conn, err := db.Conn(context.Background())
|
||||||
|
// if err != nil { ... }
|
||||||
|
defer conn.Close()
|
||||||
|
err = conn.Raw(func (driverConn interface{}) error {
|
||||||
|
sqliteConn := driverConn.(*sqlite3.SQLiteConn)
|
||||||
|
// ... use sqliteConn
|
||||||
|
})
|
||||||
|
// if err != nil { ... }
|
||||||
|
|
||||||
Go SQlite3 Extensions
|
Go SQlite3 Extensions
|
||||||
|
|
||||||
If you want to register Go functions as SQLite extension functions,
|
If you want to register Go functions as SQLite extension functions
|
||||||
call RegisterFunction from ConnectHook.
|
you can make a custom driver by calling RegisterFunction from
|
||||||
|
ConnectHook.
|
||||||
|
|
||||||
regex = func(re, s string) (bool, error) {
|
regex = func(re, s string) (bool, error) {
|
||||||
return regexp.MatchString(re, s)
|
return regexp.MatchString(re, s)
|
||||||
}
|
}
|
||||||
sql.Register("sqlite3_with_go_func",
|
sql.Register("sqlite3_extended",
|
||||||
&sqlite3.SQLiteDriver{
|
&sqlite3.SQLiteDriver{
|
||||||
ConnectHook: func(conn *sqlite3.SQLiteConn) error {
|
ConnectHook: func(conn *sqlite3.SQLiteConn) error {
|
||||||
return conn.RegisterFunc("regexp", regex, true)
|
return conn.RegisterFunc("regexp", regex, true)
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
You can then use the custom driver by passing its name to sql.Open.
|
||||||
|
|
||||||
|
var i int
|
||||||
|
conn, err := sql.Open("sqlite3_extended", "./foo.db")
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
err = db.QueryRow(`SELECT regexp("foo.*", "seafood")`).Scan(&i)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
See the documentation of RegisterFunc for more details.
|
See the documentation of RegisterFunc for more details.
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|||||||
+1
-1
@@ -7,7 +7,7 @@ package sqlite3
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
#ifndef USE_LIBSQLITE3
|
#ifndef USE_LIBSQLITE3
|
||||||
#include <sqlite3-binding.h>
|
#include "sqlite3-binding.h"
|
||||||
#else
|
#else
|
||||||
#include <sqlite3.h>
|
#include <sqlite3.h>
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
+51208
-27046
File diff suppressed because it is too large
Load Diff
+2150
-977
File diff suppressed because it is too large
Load Diff
+262
-101
@@ -4,6 +4,7 @@
|
|||||||
// Use of this source code is governed by an MIT-style
|
// Use of this source code is governed by an MIT-style
|
||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
//go:build cgo
|
||||||
// +build cgo
|
// +build cgo
|
||||||
|
|
||||||
package sqlite3
|
package sqlite3
|
||||||
@@ -15,16 +16,15 @@ package sqlite3
|
|||||||
#cgo CFLAGS: -DHAVE_USLEEP=1
|
#cgo CFLAGS: -DHAVE_USLEEP=1
|
||||||
#cgo CFLAGS: -DSQLITE_ENABLE_FTS3
|
#cgo CFLAGS: -DSQLITE_ENABLE_FTS3
|
||||||
#cgo CFLAGS: -DSQLITE_ENABLE_FTS3_PARENTHESIS
|
#cgo CFLAGS: -DSQLITE_ENABLE_FTS3_PARENTHESIS
|
||||||
#cgo CFLAGS: -DSQLITE_ENABLE_FTS4_UNICODE61
|
|
||||||
#cgo CFLAGS: -DSQLITE_TRACE_SIZE_LIMIT=15
|
#cgo CFLAGS: -DSQLITE_TRACE_SIZE_LIMIT=15
|
||||||
#cgo CFLAGS: -DSQLITE_OMIT_DEPRECATED
|
#cgo CFLAGS: -DSQLITE_OMIT_DEPRECATED
|
||||||
#cgo CFLAGS: -DSQLITE_DISABLE_INTRINSIC
|
|
||||||
#cgo CFLAGS: -DSQLITE_DEFAULT_WAL_SYNCHRONOUS=1
|
#cgo CFLAGS: -DSQLITE_DEFAULT_WAL_SYNCHRONOUS=1
|
||||||
#cgo CFLAGS: -DSQLITE_ENABLE_UPDATE_DELETE_LIMIT
|
#cgo CFLAGS: -DSQLITE_ENABLE_UPDATE_DELETE_LIMIT
|
||||||
#cgo CFLAGS: -Wno-deprecated-declarations
|
#cgo CFLAGS: -Wno-deprecated-declarations
|
||||||
#cgo linux,!android CFLAGS: -DHAVE_PREAD64=1 -DHAVE_PWRITE64=1
|
#cgo openbsd CFLAGS: -I/usr/local/include
|
||||||
|
#cgo openbsd LDFLAGS: -L/usr/local/lib
|
||||||
#ifndef USE_LIBSQLITE3
|
#ifndef USE_LIBSQLITE3
|
||||||
#include <sqlite3-binding.h>
|
#include "sqlite3-binding.h"
|
||||||
#else
|
#else
|
||||||
#include <sqlite3.h>
|
#include <sqlite3.h>
|
||||||
#endif
|
#endif
|
||||||
@@ -47,6 +47,18 @@ package sqlite3
|
|||||||
# define SQLITE_DETERMINISTIC 0
|
# define SQLITE_DETERMINISTIC 0
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if defined(HAVE_PREAD64) && defined(HAVE_PWRITE64)
|
||||||
|
# undef USE_PREAD
|
||||||
|
# undef USE_PWRITE
|
||||||
|
# define USE_PREAD64 1
|
||||||
|
# define USE_PWRITE64 1
|
||||||
|
#elif defined(HAVE_PREAD) && defined(HAVE_PWRITE)
|
||||||
|
# undef USE_PREAD
|
||||||
|
# undef USE_PWRITE
|
||||||
|
# define USE_PREAD64 1
|
||||||
|
# define USE_PWRITE64 1
|
||||||
|
#endif
|
||||||
|
|
||||||
static int
|
static int
|
||||||
_sqlite3_open_v2(const char *filename, sqlite3 **ppDb, int flags, const char *zVfs) {
|
_sqlite3_open_v2(const char *filename, sqlite3 **ppDb, int flags, const char *zVfs) {
|
||||||
#ifdef SQLITE_OPEN_URI
|
#ifdef SQLITE_OPEN_URI
|
||||||
@@ -233,8 +245,14 @@ const (
|
|||||||
columnTimestamp string = "timestamp"
|
columnTimestamp string = "timestamp"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// This variable can be replaced with -ldflags like below:
|
||||||
|
// go build -ldflags="-X 'github.com/mattn/go-sqlite3.driverName=my-sqlite3'"
|
||||||
|
var driverName = "sqlite3"
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
sql.Register("sqlite3", &SQLiteDriver{})
|
if driverName != "" {
|
||||||
|
sql.Register(driverName, &SQLiteDriver{})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Version returns SQLite library version information.
|
// Version returns SQLite library version information.
|
||||||
@@ -290,6 +308,51 @@ const (
|
|||||||
/*SQLITE_RECURSIVE = C.SQLITE_RECURSIVE*/
|
/*SQLITE_RECURSIVE = C.SQLITE_RECURSIVE*/
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Standard File Control Opcodes
|
||||||
|
// See: https://www.sqlite.org/c3ref/c_fcntl_begin_atomic_write.html
|
||||||
|
const (
|
||||||
|
SQLITE_FCNTL_LOCKSTATE = int(1)
|
||||||
|
SQLITE_FCNTL_GET_LOCKPROXYFILE = int(2)
|
||||||
|
SQLITE_FCNTL_SET_LOCKPROXYFILE = int(3)
|
||||||
|
SQLITE_FCNTL_LAST_ERRNO = int(4)
|
||||||
|
SQLITE_FCNTL_SIZE_HINT = int(5)
|
||||||
|
SQLITE_FCNTL_CHUNK_SIZE = int(6)
|
||||||
|
SQLITE_FCNTL_FILE_POINTER = int(7)
|
||||||
|
SQLITE_FCNTL_SYNC_OMITTED = int(8)
|
||||||
|
SQLITE_FCNTL_WIN32_AV_RETRY = int(9)
|
||||||
|
SQLITE_FCNTL_PERSIST_WAL = int(10)
|
||||||
|
SQLITE_FCNTL_OVERWRITE = int(11)
|
||||||
|
SQLITE_FCNTL_VFSNAME = int(12)
|
||||||
|
SQLITE_FCNTL_POWERSAFE_OVERWRITE = int(13)
|
||||||
|
SQLITE_FCNTL_PRAGMA = int(14)
|
||||||
|
SQLITE_FCNTL_BUSYHANDLER = int(15)
|
||||||
|
SQLITE_FCNTL_TEMPFILENAME = int(16)
|
||||||
|
SQLITE_FCNTL_MMAP_SIZE = int(18)
|
||||||
|
SQLITE_FCNTL_TRACE = int(19)
|
||||||
|
SQLITE_FCNTL_HAS_MOVED = int(20)
|
||||||
|
SQLITE_FCNTL_SYNC = int(21)
|
||||||
|
SQLITE_FCNTL_COMMIT_PHASETWO = int(22)
|
||||||
|
SQLITE_FCNTL_WIN32_SET_HANDLE = int(23)
|
||||||
|
SQLITE_FCNTL_WAL_BLOCK = int(24)
|
||||||
|
SQLITE_FCNTL_ZIPVFS = int(25)
|
||||||
|
SQLITE_FCNTL_RBU = int(26)
|
||||||
|
SQLITE_FCNTL_VFS_POINTER = int(27)
|
||||||
|
SQLITE_FCNTL_JOURNAL_POINTER = int(28)
|
||||||
|
SQLITE_FCNTL_WIN32_GET_HANDLE = int(29)
|
||||||
|
SQLITE_FCNTL_PDB = int(30)
|
||||||
|
SQLITE_FCNTL_BEGIN_ATOMIC_WRITE = int(31)
|
||||||
|
SQLITE_FCNTL_COMMIT_ATOMIC_WRITE = int(32)
|
||||||
|
SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE = int(33)
|
||||||
|
SQLITE_FCNTL_LOCK_TIMEOUT = int(34)
|
||||||
|
SQLITE_FCNTL_DATA_VERSION = int(35)
|
||||||
|
SQLITE_FCNTL_SIZE_LIMIT = int(36)
|
||||||
|
SQLITE_FCNTL_CKPT_DONE = int(37)
|
||||||
|
SQLITE_FCNTL_RESERVE_BYTES = int(38)
|
||||||
|
SQLITE_FCNTL_CKPT_START = int(39)
|
||||||
|
SQLITE_FCNTL_EXTERNAL_READER = int(40)
|
||||||
|
SQLITE_FCNTL_CKSM_FILE = int(41)
|
||||||
|
)
|
||||||
|
|
||||||
// SQLiteDriver implements driver.Driver.
|
// SQLiteDriver implements driver.Driver.
|
||||||
type SQLiteDriver struct {
|
type SQLiteDriver struct {
|
||||||
Extensions []string
|
Extensions []string
|
||||||
@@ -442,10 +505,12 @@ func (ai *aggInfo) Done(ctx *C.sqlite3_context) {
|
|||||||
// Commit transaction.
|
// Commit transaction.
|
||||||
func (tx *SQLiteTx) Commit() error {
|
func (tx *SQLiteTx) Commit() error {
|
||||||
_, err := tx.c.exec(context.Background(), "COMMIT", nil)
|
_, err := tx.c.exec(context.Background(), "COMMIT", nil)
|
||||||
if err != nil && err.(Error).Code == C.SQLITE_BUSY {
|
if err != nil {
|
||||||
// sqlite3 will leave the transaction open in this scenario.
|
// sqlite3 may leave the transaction open in this scenario.
|
||||||
// However, database/sql considers the transaction complete once we
|
// However, database/sql considers the transaction complete once we
|
||||||
// return from Commit() - we must clean up to honour its semantics.
|
// return from Commit() - we must clean up to honour its semantics.
|
||||||
|
// We don't know if the ROLLBACK is strictly necessary, but according
|
||||||
|
// to sqlite's docs, there is no harm in calling ROLLBACK unnecessarily.
|
||||||
tx.c.exec(context.Background(), "ROLLBACK", nil)
|
tx.c.exec(context.Background(), "ROLLBACK", nil)
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
@@ -473,7 +538,7 @@ func (c *SQLiteConn) RegisterCollation(name string, cmp func(string, string) int
|
|||||||
handle := newHandle(c, cmp)
|
handle := newHandle(c, cmp)
|
||||||
cname := C.CString(name)
|
cname := C.CString(name)
|
||||||
defer C.free(unsafe.Pointer(cname))
|
defer C.free(unsafe.Pointer(cname))
|
||||||
rv := C.sqlite3_create_collation(c.db, cname, C.SQLITE_UTF8, unsafe.Pointer(handle), (*[0]byte)(unsafe.Pointer(C.compareTrampoline)))
|
rv := C.sqlite3_create_collation(c.db, cname, C.SQLITE_UTF8, handle, (*[0]byte)(unsafe.Pointer(C.compareTrampoline)))
|
||||||
if rv != C.SQLITE_OK {
|
if rv != C.SQLITE_OK {
|
||||||
return c.lastError()
|
return c.lastError()
|
||||||
}
|
}
|
||||||
@@ -491,7 +556,7 @@ func (c *SQLiteConn) RegisterCommitHook(callback func() int) {
|
|||||||
if callback == nil {
|
if callback == nil {
|
||||||
C.sqlite3_commit_hook(c.db, nil, nil)
|
C.sqlite3_commit_hook(c.db, nil, nil)
|
||||||
} else {
|
} else {
|
||||||
C.sqlite3_commit_hook(c.db, (*[0]byte)(C.commitHookTrampoline), unsafe.Pointer(newHandle(c, callback)))
|
C.sqlite3_commit_hook(c.db, (*[0]byte)(C.commitHookTrampoline), newHandle(c, callback))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -504,7 +569,7 @@ func (c *SQLiteConn) RegisterRollbackHook(callback func()) {
|
|||||||
if callback == nil {
|
if callback == nil {
|
||||||
C.sqlite3_rollback_hook(c.db, nil, nil)
|
C.sqlite3_rollback_hook(c.db, nil, nil)
|
||||||
} else {
|
} else {
|
||||||
C.sqlite3_rollback_hook(c.db, (*[0]byte)(C.rollbackHookTrampoline), unsafe.Pointer(newHandle(c, callback)))
|
C.sqlite3_rollback_hook(c.db, (*[0]byte)(C.rollbackHookTrampoline), newHandle(c, callback))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -521,7 +586,7 @@ func (c *SQLiteConn) RegisterUpdateHook(callback func(int, string, string, int64
|
|||||||
if callback == nil {
|
if callback == nil {
|
||||||
C.sqlite3_update_hook(c.db, nil, nil)
|
C.sqlite3_update_hook(c.db, nil, nil)
|
||||||
} else {
|
} else {
|
||||||
C.sqlite3_update_hook(c.db, (*[0]byte)(C.updateHookTrampoline), unsafe.Pointer(newHandle(c, callback)))
|
C.sqlite3_update_hook(c.db, (*[0]byte)(C.updateHookTrampoline), newHandle(c, callback))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -535,7 +600,7 @@ func (c *SQLiteConn) RegisterAuthorizer(callback func(int, string, string, strin
|
|||||||
if callback == nil {
|
if callback == nil {
|
||||||
C.sqlite3_set_authorizer(c.db, nil, nil)
|
C.sqlite3_set_authorizer(c.db, nil, nil)
|
||||||
} else {
|
} else {
|
||||||
C.sqlite3_set_authorizer(c.db, (*[0]byte)(C.authorizerTrampoline), unsafe.Pointer(newHandle(c, callback)))
|
C.sqlite3_set_authorizer(c.db, (*[0]byte)(C.authorizerTrampoline), newHandle(c, callback))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -616,8 +681,8 @@ func (c *SQLiteConn) RegisterFunc(name string, impl interface{}, pure bool) erro
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func sqlite3CreateFunction(db *C.sqlite3, zFunctionName *C.char, nArg C.int, eTextRep C.int, pApp uintptr, xFunc unsafe.Pointer, xStep unsafe.Pointer, xFinal unsafe.Pointer) C.int {
|
func sqlite3CreateFunction(db *C.sqlite3, zFunctionName *C.char, nArg C.int, eTextRep C.int, pApp unsafe.Pointer, xFunc unsafe.Pointer, xStep unsafe.Pointer, xFinal unsafe.Pointer) C.int {
|
||||||
return C._sqlite3_create_function(db, zFunctionName, nArg, eTextRep, C.uintptr_t(pApp), (*[0]byte)(xFunc), (*[0]byte)(xStep), (*[0]byte)(xFinal))
|
return C._sqlite3_create_function(db, zFunctionName, nArg, eTextRep, C.uintptr_t(uintptr(pApp)), (*[0]byte)(xFunc), (*[0]byte)(xStep), (*[0]byte)(xFinal))
|
||||||
}
|
}
|
||||||
|
|
||||||
// RegisterAggregator makes a Go type available as a SQLite aggregation function.
|
// RegisterAggregator makes a Go type available as a SQLite aggregation function.
|
||||||
@@ -783,9 +848,9 @@ func lastError(db *C.sqlite3) error {
|
|||||||
|
|
||||||
// Exec implements Execer.
|
// Exec implements Execer.
|
||||||
func (c *SQLiteConn) Exec(query string, args []driver.Value) (driver.Result, error) {
|
func (c *SQLiteConn) Exec(query string, args []driver.Value) (driver.Result, error) {
|
||||||
list := make([]namedValue, len(args))
|
list := make([]driver.NamedValue, len(args))
|
||||||
for i, v := range args {
|
for i, v := range args {
|
||||||
list[i] = namedValue{
|
list[i] = driver.NamedValue{
|
||||||
Ordinal: i + 1,
|
Ordinal: i + 1,
|
||||||
Value: v,
|
Value: v,
|
||||||
}
|
}
|
||||||
@@ -793,7 +858,7 @@ func (c *SQLiteConn) Exec(query string, args []driver.Value) (driver.Result, err
|
|||||||
return c.exec(context.Background(), query, list)
|
return c.exec(context.Background(), query, list)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *SQLiteConn) exec(ctx context.Context, query string, args []namedValue) (driver.Result, error) {
|
func (c *SQLiteConn) exec(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
|
||||||
start := 0
|
start := 0
|
||||||
for {
|
for {
|
||||||
s, err := c.prepare(ctx, query)
|
s, err := c.prepare(ctx, query)
|
||||||
@@ -802,42 +867,49 @@ func (c *SQLiteConn) exec(ctx context.Context, query string, args []namedValue)
|
|||||||
}
|
}
|
||||||
var res driver.Result
|
var res driver.Result
|
||||||
if s.(*SQLiteStmt).s != nil {
|
if s.(*SQLiteStmt).s != nil {
|
||||||
|
stmtArgs := make([]driver.NamedValue, 0, len(args))
|
||||||
na := s.NumInput()
|
na := s.NumInput()
|
||||||
if len(args) < na {
|
if len(args)-start < na {
|
||||||
s.Close()
|
s.Close()
|
||||||
return nil, fmt.Errorf("not enough args to execute query: want %d got %d", na, len(args))
|
return nil, fmt.Errorf("not enough args to execute query: want %d got %d", na, len(args))
|
||||||
}
|
}
|
||||||
for i := 0; i < na; i++ {
|
// consume the number of arguments used in the current
|
||||||
args[i].Ordinal -= start
|
// statement and append all named arguments not
|
||||||
|
// contained therein
|
||||||
|
stmtArgs = append(stmtArgs, args[start:start+na]...)
|
||||||
|
for i := range args {
|
||||||
|
if (i < start || i >= na) && args[i].Name != "" {
|
||||||
|
stmtArgs = append(stmtArgs, args[i])
|
||||||
|
}
|
||||||
}
|
}
|
||||||
res, err = s.(*SQLiteStmt).exec(ctx, args[:na])
|
for i := range stmtArgs {
|
||||||
|
stmtArgs[i].Ordinal = i + 1
|
||||||
|
}
|
||||||
|
res, err = s.(*SQLiteStmt).exec(ctx, stmtArgs)
|
||||||
if err != nil && err != driver.ErrSkip {
|
if err != nil && err != driver.ErrSkip {
|
||||||
s.Close()
|
s.Close()
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
args = args[na:]
|
|
||||||
start += na
|
start += na
|
||||||
}
|
}
|
||||||
tail := s.(*SQLiteStmt).t
|
tail := s.(*SQLiteStmt).t
|
||||||
s.Close()
|
s.Close()
|
||||||
if tail == "" {
|
if tail == "" {
|
||||||
|
if res == nil {
|
||||||
|
// https://github.com/mattn/go-sqlite3/issues/963
|
||||||
|
res = &SQLiteResult{0, 0}
|
||||||
|
}
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
query = tail
|
query = tail
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type namedValue struct {
|
|
||||||
Name string
|
|
||||||
Ordinal int
|
|
||||||
Value driver.Value
|
|
||||||
}
|
|
||||||
|
|
||||||
// Query implements Queryer.
|
// Query implements Queryer.
|
||||||
func (c *SQLiteConn) Query(query string, args []driver.Value) (driver.Rows, error) {
|
func (c *SQLiteConn) Query(query string, args []driver.Value) (driver.Rows, error) {
|
||||||
list := make([]namedValue, len(args))
|
list := make([]driver.NamedValue, len(args))
|
||||||
for i, v := range args {
|
for i, v := range args {
|
||||||
list[i] = namedValue{
|
list[i] = driver.NamedValue{
|
||||||
Ordinal: i + 1,
|
Ordinal: i + 1,
|
||||||
Value: v,
|
Value: v,
|
||||||
}
|
}
|
||||||
@@ -845,27 +917,36 @@ func (c *SQLiteConn) Query(query string, args []driver.Value) (driver.Rows, erro
|
|||||||
return c.query(context.Background(), query, list)
|
return c.query(context.Background(), query, list)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *SQLiteConn) query(ctx context.Context, query string, args []namedValue) (driver.Rows, error) {
|
func (c *SQLiteConn) query(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
|
||||||
start := 0
|
start := 0
|
||||||
for {
|
for {
|
||||||
|
stmtArgs := make([]driver.NamedValue, 0, len(args))
|
||||||
s, err := c.prepare(ctx, query)
|
s, err := c.prepare(ctx, query)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
s.(*SQLiteStmt).cls = true
|
s.(*SQLiteStmt).cls = true
|
||||||
na := s.NumInput()
|
na := s.NumInput()
|
||||||
if len(args) < na {
|
if len(args)-start < na {
|
||||||
return nil, fmt.Errorf("not enough args to execute query: want %d got %d", na, len(args))
|
return nil, fmt.Errorf("not enough args to execute query: want %d got %d", na, len(args)-start)
|
||||||
}
|
}
|
||||||
for i := 0; i < na; i++ {
|
// consume the number of arguments used in the current
|
||||||
args[i].Ordinal -= start
|
// statement and append all named arguments not contained
|
||||||
|
// therein
|
||||||
|
stmtArgs = append(stmtArgs, args[start:start+na]...)
|
||||||
|
for i := range args {
|
||||||
|
if (i < start || i >= na) && args[i].Name != "" {
|
||||||
|
stmtArgs = append(stmtArgs, args[i])
|
||||||
|
}
|
||||||
}
|
}
|
||||||
rows, err := s.(*SQLiteStmt).query(ctx, args[:na])
|
for i := range stmtArgs {
|
||||||
|
stmtArgs[i].Ordinal = i + 1
|
||||||
|
}
|
||||||
|
rows, err := s.(*SQLiteStmt).query(ctx, stmtArgs)
|
||||||
if err != nil && err != driver.ErrSkip {
|
if err != nil && err != driver.ErrSkip {
|
||||||
s.Close()
|
s.Close()
|
||||||
return rows, err
|
return rows, err
|
||||||
}
|
}
|
||||||
args = args[na:]
|
|
||||||
start += na
|
start += na
|
||||||
tail := s.(*SQLiteStmt).t
|
tail := s.(*SQLiteStmt).t
|
||||||
if tail == "" {
|
if tail == "" {
|
||||||
@@ -1023,6 +1104,8 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
|
|||||||
secureDelete := "DEFAULT"
|
secureDelete := "DEFAULT"
|
||||||
synchronousMode := "NORMAL"
|
synchronousMode := "NORMAL"
|
||||||
writableSchema := -1
|
writableSchema := -1
|
||||||
|
vfsName := ""
|
||||||
|
var cacheSize *int64
|
||||||
|
|
||||||
pos := strings.IndexRune(dsn, '?')
|
pos := strings.IndexRune(dsn, '?')
|
||||||
if pos >= 1 {
|
if pos >= 1 {
|
||||||
@@ -1346,6 +1429,22 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Cache size (_cache_size)
|
||||||
|
//
|
||||||
|
// https://sqlite.org/pragma.html#pragma_cache_size
|
||||||
|
//
|
||||||
|
if val := params.Get("_cache_size"); val != "" {
|
||||||
|
iv, err := strconv.ParseInt(val, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("Invalid _cache_size: %v: %v", val, err)
|
||||||
|
}
|
||||||
|
cacheSize = &iv
|
||||||
|
}
|
||||||
|
|
||||||
|
if val := params.Get("vfs"); val != "" {
|
||||||
|
vfsName = val
|
||||||
|
}
|
||||||
|
|
||||||
if !strings.HasPrefix(dsn, "file:") {
|
if !strings.HasPrefix(dsn, "file:") {
|
||||||
dsn = dsn[:pos]
|
dsn = dsn[:pos]
|
||||||
}
|
}
|
||||||
@@ -1354,9 +1453,14 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
|
|||||||
var db *C.sqlite3
|
var db *C.sqlite3
|
||||||
name := C.CString(dsn)
|
name := C.CString(dsn)
|
||||||
defer C.free(unsafe.Pointer(name))
|
defer C.free(unsafe.Pointer(name))
|
||||||
|
var vfs *C.char
|
||||||
|
if vfsName != "" {
|
||||||
|
vfs = C.CString(vfsName)
|
||||||
|
defer C.free(unsafe.Pointer(vfs))
|
||||||
|
}
|
||||||
rv := C._sqlite3_open_v2(name, &db,
|
rv := C._sqlite3_open_v2(name, &db,
|
||||||
mutex|C.SQLITE_OPEN_READWRITE|C.SQLITE_OPEN_CREATE,
|
mutex|C.SQLITE_OPEN_READWRITE|C.SQLITE_OPEN_CREATE,
|
||||||
nil)
|
vfs)
|
||||||
if rv != 0 {
|
if rv != 0 {
|
||||||
// Save off the error _before_ closing the database.
|
// Save off the error _before_ closing the database.
|
||||||
// This is safe even if db is nil.
|
// This is safe even if db is nil.
|
||||||
@@ -1370,12 +1474,6 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
|
|||||||
return nil, errors.New("sqlite succeeded without returning a database")
|
return nil, errors.New("sqlite succeeded without returning a database")
|
||||||
}
|
}
|
||||||
|
|
||||||
rv = C.sqlite3_busy_timeout(db, C.int(busyTimeout))
|
|
||||||
if rv != C.SQLITE_OK {
|
|
||||||
C.sqlite3_close_v2(db)
|
|
||||||
return nil, Error{Code: ErrNo(rv)}
|
|
||||||
}
|
|
||||||
|
|
||||||
exec := func(s string) error {
|
exec := func(s string) error {
|
||||||
cs := C.CString(s)
|
cs := C.CString(s)
|
||||||
rv := C.sqlite3_exec(db, cs, nil, nil, nil)
|
rv := C.sqlite3_exec(db, cs, nil, nil, nil)
|
||||||
@@ -1386,6 +1484,12 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Busy timeout
|
||||||
|
if err := exec(fmt.Sprintf("PRAGMA busy_timeout = %d;", busyTimeout)); err != nil {
|
||||||
|
C.sqlite3_close_v2(db)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
// USER AUTHENTICATION
|
// USER AUTHENTICATION
|
||||||
//
|
//
|
||||||
// User Authentication is always performed even when
|
// User Authentication is always performed even when
|
||||||
@@ -1637,7 +1741,7 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
|
|||||||
//
|
//
|
||||||
// Because default is NORMAL this statement is always executed
|
// Because default is NORMAL this statement is always executed
|
||||||
if err := exec(fmt.Sprintf("PRAGMA synchronous = %s;", synchronousMode)); err != nil {
|
if err := exec(fmt.Sprintf("PRAGMA synchronous = %s;", synchronousMode)); err != nil {
|
||||||
C.sqlite3_close_v2(db)
|
conn.Close()
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1649,6 +1753,14 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Cache Size
|
||||||
|
if cacheSize != nil {
|
||||||
|
if err := exec(fmt.Sprintf("PRAGMA cache_size = %d;", *cacheSize)); err != nil {
|
||||||
|
C.sqlite3_close_v2(db)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if len(d.Extensions) > 0 {
|
if len(d.Extensions) > 0 {
|
||||||
if err := conn.loadExtensions(d.Extensions); err != nil {
|
if err := conn.loadExtensions(d.Extensions); err != nil {
|
||||||
conn.Close()
|
conn.Close()
|
||||||
@@ -1753,6 +1865,31 @@ func (c *SQLiteConn) SetLimit(id int, newVal int) int {
|
|||||||
return int(C._sqlite3_limit(c.db, C.int(id), C.int(newVal)))
|
return int(C._sqlite3_limit(c.db, C.int(id), C.int(newVal)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetFileControlInt invokes the xFileControl method on a given database. The
|
||||||
|
// dbName is the name of the database. It will default to "main" if left blank.
|
||||||
|
// The op is one of the opcodes prefixed by "SQLITE_FCNTL_". The arg argument
|
||||||
|
// and return code are both opcode-specific. Please see the SQLite documentation.
|
||||||
|
//
|
||||||
|
// This method is not thread-safe as the returned error code can be changed by
|
||||||
|
// another call if invoked concurrently.
|
||||||
|
//
|
||||||
|
// See: sqlite3_file_control, https://www.sqlite.org/c3ref/file_control.html
|
||||||
|
func (c *SQLiteConn) SetFileControlInt(dbName string, op int, arg int) error {
|
||||||
|
if dbName == "" {
|
||||||
|
dbName = "main"
|
||||||
|
}
|
||||||
|
|
||||||
|
cDBName := C.CString(dbName)
|
||||||
|
defer C.free(unsafe.Pointer(cDBName))
|
||||||
|
|
||||||
|
cArg := C.int(arg)
|
||||||
|
rv := C.sqlite3_file_control(c.db, cDBName, C.int(op), unsafe.Pointer(&cArg))
|
||||||
|
if rv != C.SQLITE_OK {
|
||||||
|
return c.lastError()
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// Close the statement.
|
// Close the statement.
|
||||||
func (s *SQLiteStmt) Close() error {
|
func (s *SQLiteStmt) Close() error {
|
||||||
s.mu.Lock()
|
s.mu.Lock()
|
||||||
@@ -1778,65 +1915,71 @@ func (s *SQLiteStmt) NumInput() int {
|
|||||||
return int(C.sqlite3_bind_parameter_count(s.s))
|
return int(C.sqlite3_bind_parameter_count(s.s))
|
||||||
}
|
}
|
||||||
|
|
||||||
type bindArg struct {
|
|
||||||
n int
|
|
||||||
v driver.Value
|
|
||||||
}
|
|
||||||
|
|
||||||
var placeHolder = []byte{0}
|
var placeHolder = []byte{0}
|
||||||
|
|
||||||
func (s *SQLiteStmt) bind(args []namedValue) error {
|
func (s *SQLiteStmt) bind(args []driver.NamedValue) error {
|
||||||
rv := C.sqlite3_reset(s.s)
|
rv := C.sqlite3_reset(s.s)
|
||||||
if rv != C.SQLITE_ROW && rv != C.SQLITE_OK && rv != C.SQLITE_DONE {
|
if rv != C.SQLITE_ROW && rv != C.SQLITE_OK && rv != C.SQLITE_DONE {
|
||||||
return s.c.lastError()
|
return s.c.lastError()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bindIndices := make([][3]int, len(args))
|
||||||
|
prefixes := []string{":", "@", "$"}
|
||||||
for i, v := range args {
|
for i, v := range args {
|
||||||
|
bindIndices[i][0] = args[i].Ordinal
|
||||||
if v.Name != "" {
|
if v.Name != "" {
|
||||||
cname := C.CString(":" + v.Name)
|
for j := range prefixes {
|
||||||
args[i].Ordinal = int(C.sqlite3_bind_parameter_index(s.s, cname))
|
cname := C.CString(prefixes[j] + v.Name)
|
||||||
C.free(unsafe.Pointer(cname))
|
bindIndices[i][j] = int(C.sqlite3_bind_parameter_index(s.s, cname))
|
||||||
|
C.free(unsafe.Pointer(cname))
|
||||||
|
}
|
||||||
|
args[i].Ordinal = bindIndices[i][0]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, arg := range args {
|
for i, arg := range args {
|
||||||
n := C.int(arg.Ordinal)
|
for j := range bindIndices[i] {
|
||||||
switch v := arg.Value.(type) {
|
if bindIndices[i][j] == 0 {
|
||||||
case nil:
|
continue
|
||||||
rv = C.sqlite3_bind_null(s.s, n)
|
}
|
||||||
case string:
|
n := C.int(bindIndices[i][j])
|
||||||
if len(v) == 0 {
|
switch v := arg.Value.(type) {
|
||||||
rv = C._sqlite3_bind_text(s.s, n, (*C.char)(unsafe.Pointer(&placeHolder[0])), C.int(0))
|
case nil:
|
||||||
} else {
|
rv = C.sqlite3_bind_null(s.s, n)
|
||||||
b := []byte(v)
|
case string:
|
||||||
|
if len(v) == 0 {
|
||||||
|
rv = C._sqlite3_bind_text(s.s, n, (*C.char)(unsafe.Pointer(&placeHolder[0])), C.int(0))
|
||||||
|
} else {
|
||||||
|
b := []byte(v)
|
||||||
|
rv = C._sqlite3_bind_text(s.s, n, (*C.char)(unsafe.Pointer(&b[0])), C.int(len(b)))
|
||||||
|
}
|
||||||
|
case int64:
|
||||||
|
rv = C.sqlite3_bind_int64(s.s, n, C.sqlite3_int64(v))
|
||||||
|
case bool:
|
||||||
|
if v {
|
||||||
|
rv = C.sqlite3_bind_int(s.s, n, 1)
|
||||||
|
} else {
|
||||||
|
rv = C.sqlite3_bind_int(s.s, n, 0)
|
||||||
|
}
|
||||||
|
case float64:
|
||||||
|
rv = C.sqlite3_bind_double(s.s, n, C.double(v))
|
||||||
|
case []byte:
|
||||||
|
if v == nil {
|
||||||
|
rv = C.sqlite3_bind_null(s.s, n)
|
||||||
|
} else {
|
||||||
|
ln := len(v)
|
||||||
|
if ln == 0 {
|
||||||
|
v = placeHolder
|
||||||
|
}
|
||||||
|
rv = C._sqlite3_bind_blob(s.s, n, unsafe.Pointer(&v[0]), C.int(ln))
|
||||||
|
}
|
||||||
|
case time.Time:
|
||||||
|
b := []byte(v.Format(SQLiteTimestampFormats[0]))
|
||||||
rv = C._sqlite3_bind_text(s.s, n, (*C.char)(unsafe.Pointer(&b[0])), C.int(len(b)))
|
rv = C._sqlite3_bind_text(s.s, n, (*C.char)(unsafe.Pointer(&b[0])), C.int(len(b)))
|
||||||
}
|
}
|
||||||
case int64:
|
if rv != C.SQLITE_OK {
|
||||||
rv = C.sqlite3_bind_int64(s.s, n, C.sqlite3_int64(v))
|
return s.c.lastError()
|
||||||
case bool:
|
|
||||||
if v {
|
|
||||||
rv = C.sqlite3_bind_int(s.s, n, 1)
|
|
||||||
} else {
|
|
||||||
rv = C.sqlite3_bind_int(s.s, n, 0)
|
|
||||||
}
|
}
|
||||||
case float64:
|
|
||||||
rv = C.sqlite3_bind_double(s.s, n, C.double(v))
|
|
||||||
case []byte:
|
|
||||||
if v == nil {
|
|
||||||
rv = C.sqlite3_bind_null(s.s, n)
|
|
||||||
} else {
|
|
||||||
ln := len(v)
|
|
||||||
if ln == 0 {
|
|
||||||
v = placeHolder
|
|
||||||
}
|
|
||||||
rv = C._sqlite3_bind_blob(s.s, n, unsafe.Pointer(&v[0]), C.int(ln))
|
|
||||||
}
|
|
||||||
case time.Time:
|
|
||||||
b := []byte(v.Format(SQLiteTimestampFormats[0]))
|
|
||||||
rv = C._sqlite3_bind_text(s.s, n, (*C.char)(unsafe.Pointer(&b[0])), C.int(len(b)))
|
|
||||||
}
|
|
||||||
if rv != C.SQLITE_OK {
|
|
||||||
return s.c.lastError()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
@@ -1844,9 +1987,9 @@ func (s *SQLiteStmt) bind(args []namedValue) error {
|
|||||||
|
|
||||||
// Query the statement with arguments. Return records.
|
// Query the statement with arguments. Return records.
|
||||||
func (s *SQLiteStmt) Query(args []driver.Value) (driver.Rows, error) {
|
func (s *SQLiteStmt) Query(args []driver.Value) (driver.Rows, error) {
|
||||||
list := make([]namedValue, len(args))
|
list := make([]driver.NamedValue, len(args))
|
||||||
for i, v := range args {
|
for i, v := range args {
|
||||||
list[i] = namedValue{
|
list[i] = driver.NamedValue{
|
||||||
Ordinal: i + 1,
|
Ordinal: i + 1,
|
||||||
Value: v,
|
Value: v,
|
||||||
}
|
}
|
||||||
@@ -1854,7 +1997,7 @@ func (s *SQLiteStmt) Query(args []driver.Value) (driver.Rows, error) {
|
|||||||
return s.query(context.Background(), list)
|
return s.query(context.Background(), list)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SQLiteStmt) query(ctx context.Context, args []namedValue) (driver.Rows, error) {
|
func (s *SQLiteStmt) query(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) {
|
||||||
if err := s.bind(args); err != nil {
|
if err := s.bind(args); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -1884,9 +2027,9 @@ func (r *SQLiteResult) RowsAffected() (int64, error) {
|
|||||||
|
|
||||||
// Exec execute the statement with arguments. Return result object.
|
// Exec execute the statement with arguments. Return result object.
|
||||||
func (s *SQLiteStmt) Exec(args []driver.Value) (driver.Result, error) {
|
func (s *SQLiteStmt) Exec(args []driver.Value) (driver.Result, error) {
|
||||||
list := make([]namedValue, len(args))
|
list := make([]driver.NamedValue, len(args))
|
||||||
for i, v := range args {
|
for i, v := range args {
|
||||||
list[i] = namedValue{
|
list[i] = driver.NamedValue{
|
||||||
Ordinal: i + 1,
|
Ordinal: i + 1,
|
||||||
Value: v,
|
Value: v,
|
||||||
}
|
}
|
||||||
@@ -1894,8 +2037,16 @@ func (s *SQLiteStmt) Exec(args []driver.Value) (driver.Result, error) {
|
|||||||
return s.exec(context.Background(), list)
|
return s.exec(context.Background(), list)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isInterruptErr(err error) bool {
|
||||||
|
sqliteErr, ok := err.(Error)
|
||||||
|
if ok {
|
||||||
|
return sqliteErr.Code == ErrInterrupt
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
// exec executes a query that doesn't return rows. Attempts to honor context timeout.
|
// exec executes a query that doesn't return rows. Attempts to honor context timeout.
|
||||||
func (s *SQLiteStmt) exec(ctx context.Context, args []namedValue) (driver.Result, error) {
|
func (s *SQLiteStmt) exec(ctx context.Context, args []driver.NamedValue) (driver.Result, error) {
|
||||||
if ctx.Done() == nil {
|
if ctx.Done() == nil {
|
||||||
return s.execSync(args)
|
return s.execSync(args)
|
||||||
}
|
}
|
||||||
@@ -1909,22 +2060,25 @@ func (s *SQLiteStmt) exec(ctx context.Context, args []namedValue) (driver.Result
|
|||||||
r, err := s.execSync(args)
|
r, err := s.execSync(args)
|
||||||
resultCh <- result{r, err}
|
resultCh <- result{r, err}
|
||||||
}()
|
}()
|
||||||
|
var rv result
|
||||||
select {
|
select {
|
||||||
case rv := <-resultCh:
|
case rv = <-resultCh:
|
||||||
return rv.r, rv.err
|
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
select {
|
select {
|
||||||
case <-resultCh: // no need to interrupt
|
case rv = <-resultCh: // no need to interrupt, operation completed in db
|
||||||
default:
|
default:
|
||||||
// this is still racy and can be no-op if executed between sqlite3_* calls in execSync.
|
// this is still racy and can be no-op if executed between sqlite3_* calls in execSync.
|
||||||
C.sqlite3_interrupt(s.c.db)
|
C.sqlite3_interrupt(s.c.db)
|
||||||
<-resultCh // ensure goroutine completed
|
rv = <-resultCh // wait for goroutine completed
|
||||||
|
if isInterruptErr(rv.err) {
|
||||||
|
return nil, ctx.Err()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return nil, ctx.Err()
|
|
||||||
}
|
}
|
||||||
|
return rv.r, rv.err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SQLiteStmt) execSync(args []namedValue) (driver.Result, error) {
|
func (s *SQLiteStmt) execSync(args []driver.NamedValue) (driver.Result, error) {
|
||||||
if err := s.bind(args); err != nil {
|
if err := s.bind(args); err != nil {
|
||||||
C.sqlite3_reset(s.s)
|
C.sqlite3_reset(s.s)
|
||||||
C.sqlite3_clear_bindings(s.s)
|
C.sqlite3_clear_bindings(s.s)
|
||||||
@@ -1943,6 +2097,13 @@ func (s *SQLiteStmt) execSync(args []namedValue) (driver.Result, error) {
|
|||||||
return &SQLiteResult{id: int64(rowid), changes: int64(changes)}, nil
|
return &SQLiteResult{id: int64(rowid), changes: int64(changes)}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Readonly reports if this statement is considered readonly by SQLite.
|
||||||
|
//
|
||||||
|
// See: https://sqlite.org/c3ref/stmt_readonly.html
|
||||||
|
func (s *SQLiteStmt) Readonly() bool {
|
||||||
|
return C.sqlite3_stmt_readonly(s.s) == 1
|
||||||
|
}
|
||||||
|
|
||||||
// Close the rows.
|
// Close the rows.
|
||||||
func (rc *SQLiteRows) Close() error {
|
func (rc *SQLiteRows) Close() error {
|
||||||
rc.s.mu.Lock()
|
rc.s.mu.Lock()
|
||||||
|
|||||||
+1
-1
@@ -8,7 +8,7 @@ package sqlite3
|
|||||||
/*
|
/*
|
||||||
|
|
||||||
#ifndef USE_LIBSQLITE3
|
#ifndef USE_LIBSQLITE3
|
||||||
#include <sqlite3-binding.h>
|
#include "sqlite3-binding.h"
|
||||||
#else
|
#else
|
||||||
#include <sqlite3.h>
|
#include <sqlite3.h>
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
+4
-20
@@ -25,20 +25,12 @@ func (c *SQLiteConn) Ping(ctx context.Context) error {
|
|||||||
|
|
||||||
// QueryContext implement QueryerContext.
|
// QueryContext implement QueryerContext.
|
||||||
func (c *SQLiteConn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
|
func (c *SQLiteConn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
|
||||||
list := make([]namedValue, len(args))
|
return c.query(ctx, query, args)
|
||||||
for i, nv := range args {
|
|
||||||
list[i] = namedValue(nv)
|
|
||||||
}
|
|
||||||
return c.query(ctx, query, list)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ExecContext implement ExecerContext.
|
// ExecContext implement ExecerContext.
|
||||||
func (c *SQLiteConn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
|
func (c *SQLiteConn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
|
||||||
list := make([]namedValue, len(args))
|
return c.exec(ctx, query, args)
|
||||||
for i, nv := range args {
|
|
||||||
list[i] = namedValue(nv)
|
|
||||||
}
|
|
||||||
return c.exec(ctx, query, list)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// PrepareContext implement ConnPrepareContext.
|
// PrepareContext implement ConnPrepareContext.
|
||||||
@@ -53,18 +45,10 @@ func (c *SQLiteConn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver
|
|||||||
|
|
||||||
// QueryContext implement QueryerContext.
|
// QueryContext implement QueryerContext.
|
||||||
func (s *SQLiteStmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) {
|
func (s *SQLiteStmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) {
|
||||||
list := make([]namedValue, len(args))
|
return s.query(ctx, args)
|
||||||
for i, nv := range args {
|
|
||||||
list[i] = namedValue(nv)
|
|
||||||
}
|
|
||||||
return s.query(ctx, list)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ExecContext implement ExecerContext.
|
// ExecContext implement ExecerContext.
|
||||||
func (s *SQLiteStmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) {
|
func (s *SQLiteStmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) {
|
||||||
list := make([]namedValue, len(args))
|
return s.exec(ctx, args)
|
||||||
for i, nv := range args {
|
|
||||||
list[i] = namedValue(nv)
|
|
||||||
}
|
|
||||||
return s.exec(ctx, list)
|
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-2
@@ -10,9 +10,12 @@ package sqlite3
|
|||||||
/*
|
/*
|
||||||
#cgo CFLAGS: -DUSE_LIBSQLITE3
|
#cgo CFLAGS: -DUSE_LIBSQLITE3
|
||||||
#cgo linux LDFLAGS: -lsqlite3
|
#cgo linux LDFLAGS: -lsqlite3
|
||||||
#cgo darwin LDFLAGS: -L/usr/local/opt/sqlite/lib -lsqlite3
|
#cgo darwin,amd64 LDFLAGS: -L/usr/local/opt/sqlite/lib -lsqlite3
|
||||||
#cgo darwin CFLAGS: -I/usr/local/opt/sqlite/include
|
#cgo darwin,amd64 CFLAGS: -I/usr/local/opt/sqlite/include
|
||||||
|
#cgo darwin,arm64 LDFLAGS: -L/opt/homebrew/opt/sqlite/lib -lsqlite3
|
||||||
|
#cgo darwin,arm64 CFLAGS: -I/opt/homebrew/opt/sqlite/include
|
||||||
#cgo openbsd LDFLAGS: -lsqlite3
|
#cgo openbsd LDFLAGS: -lsqlite3
|
||||||
#cgo solaris LDFLAGS: -lsqlite3
|
#cgo solaris LDFLAGS: -lsqlite3
|
||||||
|
#cgo windows LDFLAGS: -lsqlite3
|
||||||
*/
|
*/
|
||||||
import "C"
|
import "C"
|
||||||
|
|||||||
+28
-14
@@ -9,7 +9,7 @@ package sqlite3
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
#ifndef USE_LIBSQLITE3
|
#ifndef USE_LIBSQLITE3
|
||||||
#include <sqlite3-binding.h>
|
#include "sqlite3-binding.h"
|
||||||
#else
|
#else
|
||||||
#include <sqlite3.h>
|
#include <sqlite3.h>
|
||||||
#endif
|
#endif
|
||||||
@@ -28,12 +28,9 @@ func (c *SQLiteConn) loadExtensions(extensions []string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, extension := range extensions {
|
for _, extension := range extensions {
|
||||||
cext := C.CString(extension)
|
if err := c.loadExtension(extension, nil); err != nil {
|
||||||
defer C.free(unsafe.Pointer(cext))
|
|
||||||
rv = C.sqlite3_load_extension(c.db, cext, nil, nil)
|
|
||||||
if rv != C.SQLITE_OK {
|
|
||||||
C.sqlite3_enable_load_extension(c.db, 0)
|
C.sqlite3_enable_load_extension(c.db, 0)
|
||||||
return errors.New(C.GoString(C.sqlite3_errmsg(c.db)))
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -41,6 +38,7 @@ func (c *SQLiteConn) loadExtensions(extensions []string) error {
|
|||||||
if rv != C.SQLITE_OK {
|
if rv != C.SQLITE_OK {
|
||||||
return errors.New(C.GoString(C.sqlite3_errmsg(c.db)))
|
return errors.New(C.GoString(C.sqlite3_errmsg(c.db)))
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -51,14 +49,9 @@ func (c *SQLiteConn) LoadExtension(lib string, entry string) error {
|
|||||||
return errors.New(C.GoString(C.sqlite3_errmsg(c.db)))
|
return errors.New(C.GoString(C.sqlite3_errmsg(c.db)))
|
||||||
}
|
}
|
||||||
|
|
||||||
clib := C.CString(lib)
|
if err := c.loadExtension(lib, &entry); err != nil {
|
||||||
defer C.free(unsafe.Pointer(clib))
|
C.sqlite3_enable_load_extension(c.db, 0)
|
||||||
centry := C.CString(entry)
|
return err
|
||||||
defer C.free(unsafe.Pointer(centry))
|
|
||||||
|
|
||||||
rv = C.sqlite3_load_extension(c.db, clib, centry, nil)
|
|
||||||
if rv != C.SQLITE_OK {
|
|
||||||
return errors.New(C.GoString(C.sqlite3_errmsg(c.db)))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
rv = C.sqlite3_enable_load_extension(c.db, 0)
|
rv = C.sqlite3_enable_load_extension(c.db, 0)
|
||||||
@@ -68,3 +61,24 @@ func (c *SQLiteConn) LoadExtension(lib string, entry string) error {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *SQLiteConn) loadExtension(lib string, entry *string) error {
|
||||||
|
clib := C.CString(lib)
|
||||||
|
defer C.free(unsafe.Pointer(clib))
|
||||||
|
|
||||||
|
var centry *C.char
|
||||||
|
if entry != nil {
|
||||||
|
centry = C.CString(*entry)
|
||||||
|
defer C.free(unsafe.Pointer(centry))
|
||||||
|
}
|
||||||
|
|
||||||
|
var errMsg *C.char
|
||||||
|
defer C.sqlite3_free(unsafe.Pointer(errMsg))
|
||||||
|
|
||||||
|
rv := C.sqlite3_load_extension(c.db, clib, centry, &errMsg)
|
||||||
|
if rv != C.SQLITE_OK {
|
||||||
|
return errors.New(C.GoString(errMsg))
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
+21
@@ -0,0 +1,21 @@
|
|||||||
|
// +build sqlite_column_metadata
|
||||||
|
|
||||||
|
package sqlite3
|
||||||
|
|
||||||
|
/*
|
||||||
|
#ifndef USE_LIBSQLITE3
|
||||||
|
#cgo CFLAGS: -DSQLITE_ENABLE_COLUMN_METADATA
|
||||||
|
#include <sqlite3-binding.h>
|
||||||
|
#else
|
||||||
|
#include <sqlite3.h>
|
||||||
|
#endif
|
||||||
|
*/
|
||||||
|
import "C"
|
||||||
|
|
||||||
|
// ColumnTableName returns the table that is the origin of a particular result
|
||||||
|
// column in a SELECT statement.
|
||||||
|
//
|
||||||
|
// See https://www.sqlite.org/c3ref/column_database_name.html
|
||||||
|
func (s *SQLiteStmt) ColumnTableName(n int) string {
|
||||||
|
return C.GoString(C.sqlite3_column_table_name(s.s, C.int(n)))
|
||||||
|
}
|
||||||
+4
-2
@@ -10,8 +10,10 @@ package sqlite3
|
|||||||
/*
|
/*
|
||||||
#cgo LDFLAGS: -licuuc -licui18n
|
#cgo LDFLAGS: -licuuc -licui18n
|
||||||
#cgo CFLAGS: -DSQLITE_ENABLE_ICU
|
#cgo CFLAGS: -DSQLITE_ENABLE_ICU
|
||||||
#cgo darwin CFLAGS: -I/usr/local/opt/icu4c/include
|
#cgo darwin,amd64 CFLAGS: -I/usr/local/opt/icu4c/include
|
||||||
#cgo darwin LDFLAGS: -L/usr/local/opt/icu4c/lib
|
#cgo darwin,amd64 LDFLAGS: -L/usr/local/opt/icu4c/lib
|
||||||
|
#cgo darwin,arm64 CFLAGS: -I/opt/homebrew/opt/icu4c/include
|
||||||
|
#cgo darwin,arm64 LDFLAGS: -L/opt/homebrew/opt/icu4c/lib
|
||||||
#cgo openbsd LDFLAGS: -lsqlite3
|
#cgo openbsd LDFLAGS: -lsqlite3
|
||||||
*/
|
*/
|
||||||
import "C"
|
import "C"
|
||||||
|
|||||||
-13
@@ -1,13 +0,0 @@
|
|||||||
// Copyright (C) 2019 Yasuhiro Matsumoto <mattn.jp@gmail.com>.
|
|
||||||
//
|
|
||||||
// Use of this source code is governed by an MIT-style
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
// +build sqlite_json sqlite_json1 json1
|
|
||||||
|
|
||||||
package sqlite3
|
|
||||||
|
|
||||||
/*
|
|
||||||
#cgo CFLAGS: -DSQLITE_ENABLE_JSON1
|
|
||||||
*/
|
|
||||||
import "C"
|
|
||||||
+14
@@ -0,0 +1,14 @@
|
|||||||
|
// Copyright (C) 2022 Yasuhiro Matsumoto <mattn.jp@gmail.com>.
|
||||||
|
//
|
||||||
|
// Use of this source code is governed by an MIT-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
// +build sqlite_math_functions
|
||||||
|
|
||||||
|
package sqlite3
|
||||||
|
|
||||||
|
/*
|
||||||
|
#cgo CFLAGS: -DSQLITE_ENABLE_MATH_FUNCTIONS
|
||||||
|
#cgo LDFLAGS: -lm
|
||||||
|
*/
|
||||||
|
import "C"
|
||||||
+15
@@ -0,0 +1,15 @@
|
|||||||
|
// Copyright (C) 2022 Yasuhiro Matsumoto <mattn.jp@gmail.com>.
|
||||||
|
//
|
||||||
|
// Use of this source code is governed by an MIT-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
//go:build sqlite_os_trace
|
||||||
|
// +build sqlite_os_trace
|
||||||
|
|
||||||
|
package sqlite3
|
||||||
|
|
||||||
|
/*
|
||||||
|
#cgo CFLAGS: -DSQLITE_FORCE_OS_TRACE=1
|
||||||
|
#cgo CFLAGS: -DSQLITE_DEBUG_OS_TRACE=1
|
||||||
|
*/
|
||||||
|
import "C"
|
||||||
+2
-2
@@ -13,7 +13,7 @@ package sqlite3
|
|||||||
#cgo LDFLAGS: -lm
|
#cgo LDFLAGS: -lm
|
||||||
|
|
||||||
#ifndef USE_LIBSQLITE3
|
#ifndef USE_LIBSQLITE3
|
||||||
#include <sqlite3-binding.h>
|
#include "sqlite3-binding.h"
|
||||||
#else
|
#else
|
||||||
#include <sqlite3.h>
|
#include <sqlite3.h>
|
||||||
#endif
|
#endif
|
||||||
@@ -33,7 +33,7 @@ import (
|
|||||||
// The callback is passed a SQLitePreUpdateData struct with the data for
|
// The callback is passed a SQLitePreUpdateData struct with the data for
|
||||||
// the update, as well as methods for fetching copies of impacted data.
|
// the update, as well as methods for fetching copies of impacted data.
|
||||||
//
|
//
|
||||||
// If there is an existing update hook for this connection, it will be
|
// If there is an existing preupdate hook for this connection, it will be
|
||||||
// removed. If callback is nil the existing hook (if any) will be removed
|
// removed. If callback is nil the existing hook (if any) will be removed
|
||||||
// without creating a new one.
|
// without creating a new one.
|
||||||
func (c *SQLiteConn) RegisterPreUpdateHook(callback func(SQLitePreUpdateData)) {
|
func (c *SQLiteConn) RegisterPreUpdateHook(callback func(SQLitePreUpdateData)) {
|
||||||
|
|||||||
+1
-1
@@ -13,7 +13,7 @@ package sqlite3
|
|||||||
// The callback is passed a SQLitePreUpdateData struct with the data for
|
// The callback is passed a SQLitePreUpdateData struct with the data for
|
||||||
// the update, as well as methods for fetching copies of impacted data.
|
// the update, as well as methods for fetching copies of impacted data.
|
||||||
//
|
//
|
||||||
// If there is an existing update hook for this connection, it will be
|
// If there is an existing preupdate hook for this connection, it will be
|
||||||
// removed. If callback is nil the existing hook (if any) will be removed
|
// removed. If callback is nil the existing hook (if any) will be removed
|
||||||
// without creating a new one.
|
// without creating a new one.
|
||||||
func (c *SQLiteConn) RegisterPreUpdateHook(callback func(SQLitePreUpdateData)) {
|
func (c *SQLiteConn) RegisterPreUpdateHook(callback func(SQLitePreUpdateData)) {
|
||||||
|
|||||||
+82
@@ -0,0 +1,82 @@
|
|||||||
|
// +build !libsqlite3 sqlite_serialize
|
||||||
|
|
||||||
|
package sqlite3
|
||||||
|
|
||||||
|
/*
|
||||||
|
#ifndef USE_LIBSQLITE3
|
||||||
|
#include <sqlite3-binding.h>
|
||||||
|
#else
|
||||||
|
#include <sqlite3.h>
|
||||||
|
#endif
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
*/
|
||||||
|
import "C"
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"math"
|
||||||
|
"reflect"
|
||||||
|
"unsafe"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Serialize returns a byte slice that is a serialization of the database.
|
||||||
|
//
|
||||||
|
// See https://www.sqlite.org/c3ref/serialize.html
|
||||||
|
func (c *SQLiteConn) Serialize(schema string) ([]byte, error) {
|
||||||
|
if schema == "" {
|
||||||
|
schema = "main"
|
||||||
|
}
|
||||||
|
var zSchema *C.char
|
||||||
|
zSchema = C.CString(schema)
|
||||||
|
defer C.free(unsafe.Pointer(zSchema))
|
||||||
|
|
||||||
|
var sz C.sqlite3_int64
|
||||||
|
ptr := C.sqlite3_serialize(c.db, zSchema, &sz, 0)
|
||||||
|
if ptr == nil {
|
||||||
|
return nil, fmt.Errorf("serialize failed")
|
||||||
|
}
|
||||||
|
defer C.sqlite3_free(unsafe.Pointer(ptr))
|
||||||
|
|
||||||
|
if sz > C.sqlite3_int64(math.MaxInt) {
|
||||||
|
return nil, fmt.Errorf("serialized database is too large (%d bytes)", sz)
|
||||||
|
}
|
||||||
|
|
||||||
|
cBuf := *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
|
||||||
|
Data: uintptr(unsafe.Pointer(ptr)),
|
||||||
|
Len: int(sz),
|
||||||
|
Cap: int(sz),
|
||||||
|
}))
|
||||||
|
|
||||||
|
res := make([]byte, int(sz))
|
||||||
|
copy(res, cBuf)
|
||||||
|
return res, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Deserialize causes the connection to disconnect from the current database and
|
||||||
|
// then re-open as an in-memory database based on the contents of the byte slice.
|
||||||
|
//
|
||||||
|
// See https://www.sqlite.org/c3ref/deserialize.html
|
||||||
|
func (c *SQLiteConn) Deserialize(b []byte, schema string) error {
|
||||||
|
if schema == "" {
|
||||||
|
schema = "main"
|
||||||
|
}
|
||||||
|
var zSchema *C.char
|
||||||
|
zSchema = C.CString(schema)
|
||||||
|
defer C.free(unsafe.Pointer(zSchema))
|
||||||
|
|
||||||
|
tmpBuf := (*C.uchar)(C.sqlite3_malloc64(C.sqlite3_uint64(len(b))))
|
||||||
|
cBuf := *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
|
||||||
|
Data: uintptr(unsafe.Pointer(tmpBuf)),
|
||||||
|
Len: len(b),
|
||||||
|
Cap: len(b),
|
||||||
|
}))
|
||||||
|
copy(cBuf, b)
|
||||||
|
|
||||||
|
rc := C.sqlite3_deserialize(c.db, zSchema, tmpBuf, C.sqlite3_int64(len(b)),
|
||||||
|
C.sqlite3_int64(len(b)), C.SQLITE_DESERIALIZE_FREEONCLOSE)
|
||||||
|
if rc != C.SQLITE_OK {
|
||||||
|
return fmt.Errorf("deserialize failed with return %v", rc)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
+20
@@ -0,0 +1,20 @@
|
|||||||
|
// +build libsqlite3,!sqlite_serialize
|
||||||
|
|
||||||
|
package sqlite3
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
/*
|
||||||
|
#cgo CFLAGS: -DSQLITE_OMIT_DESERIALIZE
|
||||||
|
*/
|
||||||
|
import "C"
|
||||||
|
|
||||||
|
func (c *SQLiteConn) Serialize(schema string) ([]byte, error) {
|
||||||
|
return nil, errors.New("sqlite3: Serialize requires the sqlite_serialize build tag when using the libsqlite3 build tag")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *SQLiteConn) Deserialize(b []byte, schema string) error {
|
||||||
|
return errors.New("sqlite3: Deserialize requires the sqlite_serialize build tag when using the libsqlite3 build tag")
|
||||||
|
}
|
||||||
+1
-1
@@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
|
#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <sqlite3-binding.h>
|
#include "sqlite3-binding.h"
|
||||||
|
|
||||||
extern int unlock_notify_wait(sqlite3 *db);
|
extern int unlock_notify_wait(sqlite3 *db);
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -12,7 +12,7 @@ package sqlite3
|
|||||||
#cgo CFLAGS: -DSQLITE_ENABLE_UNLOCK_NOTIFY
|
#cgo CFLAGS: -DSQLITE_ENABLE_UNLOCK_NOTIFY
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <sqlite3-binding.h>
|
#include "sqlite3-binding.h"
|
||||||
|
|
||||||
extern void unlock_notify_callback(void *arg, int argc);
|
extern void unlock_notify_callback(void *arg, int argc);
|
||||||
*/
|
*/
|
||||||
|
|||||||
+1
-1
@@ -11,7 +11,7 @@ package sqlite3
|
|||||||
#cgo CFLAGS: -DSQLITE_USER_AUTHENTICATION
|
#cgo CFLAGS: -DSQLITE_USER_AUTHENTICATION
|
||||||
#cgo LDFLAGS: -lm
|
#cgo LDFLAGS: -lm
|
||||||
#ifndef USE_LIBSQLITE3
|
#ifndef USE_LIBSQLITE3
|
||||||
#include <sqlite3-binding.h>
|
#include "sqlite3-binding.h"
|
||||||
#else
|
#else
|
||||||
#include <sqlite3.h>
|
#include <sqlite3.h>
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
+103
-33
@@ -19,7 +19,7 @@ package sqlite3
|
|||||||
#cgo CFLAGS: -Wno-deprecated-declarations
|
#cgo CFLAGS: -Wno-deprecated-declarations
|
||||||
|
|
||||||
#ifndef USE_LIBSQLITE3
|
#ifndef USE_LIBSQLITE3
|
||||||
#include <sqlite3-binding.h>
|
#include "sqlite3-binding.h"
|
||||||
#else
|
#else
|
||||||
#include <sqlite3.h>
|
#include <sqlite3.h>
|
||||||
#endif
|
#endif
|
||||||
@@ -226,11 +226,43 @@ static sqlite3_module goModule = {
|
|||||||
0 // xRollbackTo
|
0 // xRollbackTo
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// See https://sqlite.org/vtab.html#eponymous_only_virtual_tables
|
||||||
|
static sqlite3_module goModuleEponymousOnly = {
|
||||||
|
0, // iVersion
|
||||||
|
0, // xCreate - create a table, which here is null
|
||||||
|
cXConnect, // xConnect - connect to an existing table
|
||||||
|
cXBestIndex, // xBestIndex - Determine search strategy
|
||||||
|
cXDisconnect, // xDisconnect - Disconnect from a table
|
||||||
|
cXDestroy, // xDestroy - Drop a table
|
||||||
|
cXOpen, // xOpen - open a cursor
|
||||||
|
cXClose, // xClose - close a cursor
|
||||||
|
cXFilter, // xFilter - configure scan constraints
|
||||||
|
cXNext, // xNext - advance a cursor
|
||||||
|
cXEof, // xEof
|
||||||
|
cXColumn, // xColumn - read data
|
||||||
|
cXRowid, // xRowid - read data
|
||||||
|
cXUpdate, // xUpdate - write data
|
||||||
|
// Not implemented
|
||||||
|
0, // xBegin - begin transaction
|
||||||
|
0, // xSync - sync transaction
|
||||||
|
0, // xCommit - commit transaction
|
||||||
|
0, // xRollback - rollback transaction
|
||||||
|
0, // xFindFunction - function overloading
|
||||||
|
0, // xRename - rename the table
|
||||||
|
0, // xSavepoint
|
||||||
|
0, // xRelease
|
||||||
|
0 // xRollbackTo
|
||||||
|
};
|
||||||
|
|
||||||
void goMDestroy(void*);
|
void goMDestroy(void*);
|
||||||
|
|
||||||
static int _sqlite3_create_module(sqlite3 *db, const char *zName, uintptr_t pClientData) {
|
static int _sqlite3_create_module(sqlite3 *db, const char *zName, uintptr_t pClientData) {
|
||||||
return sqlite3_create_module_v2(db, zName, &goModule, (void*) pClientData, goMDestroy);
|
return sqlite3_create_module_v2(db, zName, &goModule, (void*) pClientData, goMDestroy);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int _sqlite3_create_module_eponymous_only(sqlite3 *db, const char *zName, uintptr_t pClientData) {
|
||||||
|
return sqlite3_create_module_v2(db, zName, &goModuleEponymousOnly, (void*) pClientData, goMDestroy);
|
||||||
|
}
|
||||||
*/
|
*/
|
||||||
import "C"
|
import "C"
|
||||||
|
|
||||||
@@ -288,10 +320,13 @@ type InfoOrderBy struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func constraints(info *C.sqlite3_index_info) []InfoConstraint {
|
func constraints(info *C.sqlite3_index_info) []InfoConstraint {
|
||||||
l := info.nConstraint
|
slice := *(*[]C.struct_sqlite3_index_constraint)(unsafe.Pointer(&reflect.SliceHeader{
|
||||||
slice := (*[1 << 30]C.struct_sqlite3_index_constraint)(unsafe.Pointer(info.aConstraint))[:l:l]
|
Data: uintptr(unsafe.Pointer(info.aConstraint)),
|
||||||
|
Len: int(info.nConstraint),
|
||||||
|
Cap: int(info.nConstraint),
|
||||||
|
}))
|
||||||
|
|
||||||
cst := make([]InfoConstraint, 0, l)
|
cst := make([]InfoConstraint, 0, len(slice))
|
||||||
for _, c := range slice {
|
for _, c := range slice {
|
||||||
var usable bool
|
var usable bool
|
||||||
if c.usable > 0 {
|
if c.usable > 0 {
|
||||||
@@ -307,10 +342,13 @@ func constraints(info *C.sqlite3_index_info) []InfoConstraint {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func orderBys(info *C.sqlite3_index_info) []InfoOrderBy {
|
func orderBys(info *C.sqlite3_index_info) []InfoOrderBy {
|
||||||
l := info.nOrderBy
|
slice := *(*[]C.struct_sqlite3_index_orderby)(unsafe.Pointer(&reflect.SliceHeader{
|
||||||
slice := (*[1 << 30]C.struct_sqlite3_index_orderby)(unsafe.Pointer(info.aOrderBy))[:l:l]
|
Data: uintptr(unsafe.Pointer(info.aOrderBy)),
|
||||||
|
Len: int(info.nOrderBy),
|
||||||
|
Cap: int(info.nOrderBy),
|
||||||
|
}))
|
||||||
|
|
||||||
ob := make([]InfoOrderBy, 0, l)
|
ob := make([]InfoOrderBy, 0, len(slice))
|
||||||
for _, c := range slice {
|
for _, c := range slice {
|
||||||
var desc bool
|
var desc bool
|
||||||
if c.desc > 0 {
|
if c.desc > 0 {
|
||||||
@@ -347,7 +385,7 @@ func mPrintf(format, arg string) *C.char {
|
|||||||
|
|
||||||
//export goMInit
|
//export goMInit
|
||||||
func goMInit(db, pClientData unsafe.Pointer, argc C.int, argv **C.char, pzErr **C.char, isCreate C.int) C.uintptr_t {
|
func goMInit(db, pClientData unsafe.Pointer, argc C.int, argv **C.char, pzErr **C.char, isCreate C.int) C.uintptr_t {
|
||||||
m := lookupHandle(uintptr(pClientData)).(*sqliteModule)
|
m := lookupHandle(pClientData).(*sqliteModule)
|
||||||
if m.c.db != (*C.sqlite3)(db) {
|
if m.c.db != (*C.sqlite3)(db) {
|
||||||
*pzErr = mPrintf("%s", "Inconsistent db handles")
|
*pzErr = mPrintf("%s", "Inconsistent db handles")
|
||||||
return 0
|
return 0
|
||||||
@@ -373,12 +411,12 @@ func goMInit(db, pClientData unsafe.Pointer, argc C.int, argv **C.char, pzErr **
|
|||||||
}
|
}
|
||||||
vt := sqliteVTab{m, vTab}
|
vt := sqliteVTab{m, vTab}
|
||||||
*pzErr = nil
|
*pzErr = nil
|
||||||
return C.uintptr_t(newHandle(m.c, &vt))
|
return C.uintptr_t(uintptr(newHandle(m.c, &vt)))
|
||||||
}
|
}
|
||||||
|
|
||||||
//export goVRelease
|
//export goVRelease
|
||||||
func goVRelease(pVTab unsafe.Pointer, isDestroy C.int) *C.char {
|
func goVRelease(pVTab unsafe.Pointer, isDestroy C.int) *C.char {
|
||||||
vt := lookupHandle(uintptr(pVTab)).(*sqliteVTab)
|
vt := lookupHandle(pVTab).(*sqliteVTab)
|
||||||
var err error
|
var err error
|
||||||
if isDestroy == 1 {
|
if isDestroy == 1 {
|
||||||
err = vt.vTab.Destroy()
|
err = vt.vTab.Destroy()
|
||||||
@@ -393,7 +431,7 @@ func goVRelease(pVTab unsafe.Pointer, isDestroy C.int) *C.char {
|
|||||||
|
|
||||||
//export goVOpen
|
//export goVOpen
|
||||||
func goVOpen(pVTab unsafe.Pointer, pzErr **C.char) C.uintptr_t {
|
func goVOpen(pVTab unsafe.Pointer, pzErr **C.char) C.uintptr_t {
|
||||||
vt := lookupHandle(uintptr(pVTab)).(*sqliteVTab)
|
vt := lookupHandle(pVTab).(*sqliteVTab)
|
||||||
vTabCursor, err := vt.vTab.Open()
|
vTabCursor, err := vt.vTab.Open()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
*pzErr = mPrintf("%s", err.Error())
|
*pzErr = mPrintf("%s", err.Error())
|
||||||
@@ -401,12 +439,12 @@ func goVOpen(pVTab unsafe.Pointer, pzErr **C.char) C.uintptr_t {
|
|||||||
}
|
}
|
||||||
vtc := sqliteVTabCursor{vt, vTabCursor}
|
vtc := sqliteVTabCursor{vt, vTabCursor}
|
||||||
*pzErr = nil
|
*pzErr = nil
|
||||||
return C.uintptr_t(newHandle(vt.module.c, &vtc))
|
return C.uintptr_t(uintptr(newHandle(vt.module.c, &vtc)))
|
||||||
}
|
}
|
||||||
|
|
||||||
//export goVBestIndex
|
//export goVBestIndex
|
||||||
func goVBestIndex(pVTab unsafe.Pointer, icp unsafe.Pointer) *C.char {
|
func goVBestIndex(pVTab unsafe.Pointer, icp unsafe.Pointer) *C.char {
|
||||||
vt := lookupHandle(uintptr(pVTab)).(*sqliteVTab)
|
vt := lookupHandle(pVTab).(*sqliteVTab)
|
||||||
info := (*C.sqlite3_index_info)(icp)
|
info := (*C.sqlite3_index_info)(icp)
|
||||||
csts := constraints(info)
|
csts := constraints(info)
|
||||||
res, err := vt.vTab.BestIndex(csts, orderBys(info))
|
res, err := vt.vTab.BestIndex(csts, orderBys(info))
|
||||||
@@ -418,22 +456,37 @@ func goVBestIndex(pVTab unsafe.Pointer, icp unsafe.Pointer) *C.char {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get a pointer to constraint_usage struct so we can update in place.
|
// Get a pointer to constraint_usage struct so we can update in place.
|
||||||
l := info.nConstraint
|
|
||||||
s := (*[1 << 30]C.struct_sqlite3_index_constraint_usage)(unsafe.Pointer(info.aConstraintUsage))[:l:l]
|
slice := *(*[]C.struct_sqlite3_index_constraint_usage)(unsafe.Pointer(&reflect.SliceHeader{
|
||||||
|
Data: uintptr(unsafe.Pointer(info.aConstraintUsage)),
|
||||||
|
Len: int(info.nConstraint),
|
||||||
|
Cap: int(info.nConstraint),
|
||||||
|
}))
|
||||||
index := 1
|
index := 1
|
||||||
for i := C.int(0); i < info.nConstraint; i++ {
|
for i := range slice {
|
||||||
if res.Used[i] {
|
if res.Used[i] {
|
||||||
s[i].argvIndex = C.int(index)
|
slice[i].argvIndex = C.int(index)
|
||||||
s[i].omit = C.uchar(1)
|
slice[i].omit = C.uchar(1)
|
||||||
index++
|
index++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
info.idxNum = C.int(res.IdxNum)
|
info.idxNum = C.int(res.IdxNum)
|
||||||
idxStr := C.CString(res.IdxStr)
|
info.idxStr = (*C.char)(C.sqlite3_malloc(C.int(len(res.IdxStr) + 1)))
|
||||||
defer C.free(unsafe.Pointer(idxStr))
|
if info.idxStr == nil {
|
||||||
info.idxStr = idxStr
|
// C.malloc and C.CString ordinarily do this for you. See https://golang.org/cmd/cgo/
|
||||||
info.needToFreeIdxStr = C.int(0)
|
panic("out of memory")
|
||||||
|
}
|
||||||
|
info.needToFreeIdxStr = C.int(1)
|
||||||
|
|
||||||
|
idxStr := *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
|
||||||
|
Data: uintptr(unsafe.Pointer(info.idxStr)),
|
||||||
|
Len: len(res.IdxStr) + 1,
|
||||||
|
Cap: len(res.IdxStr) + 1,
|
||||||
|
}))
|
||||||
|
copy(idxStr, res.IdxStr)
|
||||||
|
idxStr[len(idxStr)-1] = 0 // null-terminated string
|
||||||
|
|
||||||
if res.AlreadyOrdered {
|
if res.AlreadyOrdered {
|
||||||
info.orderByConsumed = C.int(1)
|
info.orderByConsumed = C.int(1)
|
||||||
}
|
}
|
||||||
@@ -445,7 +498,7 @@ func goVBestIndex(pVTab unsafe.Pointer, icp unsafe.Pointer) *C.char {
|
|||||||
|
|
||||||
//export goVClose
|
//export goVClose
|
||||||
func goVClose(pCursor unsafe.Pointer) *C.char {
|
func goVClose(pCursor unsafe.Pointer) *C.char {
|
||||||
vtc := lookupHandle(uintptr(pCursor)).(*sqliteVTabCursor)
|
vtc := lookupHandle(pCursor).(*sqliteVTabCursor)
|
||||||
err := vtc.vTabCursor.Close()
|
err := vtc.vTabCursor.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return mPrintf("%s", err.Error())
|
return mPrintf("%s", err.Error())
|
||||||
@@ -455,13 +508,13 @@ func goVClose(pCursor unsafe.Pointer) *C.char {
|
|||||||
|
|
||||||
//export goMDestroy
|
//export goMDestroy
|
||||||
func goMDestroy(pClientData unsafe.Pointer) {
|
func goMDestroy(pClientData unsafe.Pointer) {
|
||||||
m := lookupHandle(uintptr(pClientData)).(*sqliteModule)
|
m := lookupHandle(pClientData).(*sqliteModule)
|
||||||
m.module.DestroyModule()
|
m.module.DestroyModule()
|
||||||
}
|
}
|
||||||
|
|
||||||
//export goVFilter
|
//export goVFilter
|
||||||
func goVFilter(pCursor unsafe.Pointer, idxNum C.int, idxName *C.char, argc C.int, argv **C.sqlite3_value) *C.char {
|
func goVFilter(pCursor unsafe.Pointer, idxNum C.int, idxName *C.char, argc C.int, argv **C.sqlite3_value) *C.char {
|
||||||
vtc := lookupHandle(uintptr(pCursor)).(*sqliteVTabCursor)
|
vtc := lookupHandle(pCursor).(*sqliteVTabCursor)
|
||||||
args := (*[(math.MaxInt32 - 1) / unsafe.Sizeof((*C.sqlite3_value)(nil))]*C.sqlite3_value)(unsafe.Pointer(argv))[:argc:argc]
|
args := (*[(math.MaxInt32 - 1) / unsafe.Sizeof((*C.sqlite3_value)(nil))]*C.sqlite3_value)(unsafe.Pointer(argv))[:argc:argc]
|
||||||
vals := make([]interface{}, 0, argc)
|
vals := make([]interface{}, 0, argc)
|
||||||
for _, v := range args {
|
for _, v := range args {
|
||||||
@@ -480,7 +533,7 @@ func goVFilter(pCursor unsafe.Pointer, idxNum C.int, idxName *C.char, argc C.int
|
|||||||
|
|
||||||
//export goVNext
|
//export goVNext
|
||||||
func goVNext(pCursor unsafe.Pointer) *C.char {
|
func goVNext(pCursor unsafe.Pointer) *C.char {
|
||||||
vtc := lookupHandle(uintptr(pCursor)).(*sqliteVTabCursor)
|
vtc := lookupHandle(pCursor).(*sqliteVTabCursor)
|
||||||
err := vtc.vTabCursor.Next()
|
err := vtc.vTabCursor.Next()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return mPrintf("%s", err.Error())
|
return mPrintf("%s", err.Error())
|
||||||
@@ -490,7 +543,7 @@ func goVNext(pCursor unsafe.Pointer) *C.char {
|
|||||||
|
|
||||||
//export goVEof
|
//export goVEof
|
||||||
func goVEof(pCursor unsafe.Pointer) C.int {
|
func goVEof(pCursor unsafe.Pointer) C.int {
|
||||||
vtc := lookupHandle(uintptr(pCursor)).(*sqliteVTabCursor)
|
vtc := lookupHandle(pCursor).(*sqliteVTabCursor)
|
||||||
err := vtc.vTabCursor.EOF()
|
err := vtc.vTabCursor.EOF()
|
||||||
if err {
|
if err {
|
||||||
return 1
|
return 1
|
||||||
@@ -500,7 +553,7 @@ func goVEof(pCursor unsafe.Pointer) C.int {
|
|||||||
|
|
||||||
//export goVColumn
|
//export goVColumn
|
||||||
func goVColumn(pCursor, cp unsafe.Pointer, col C.int) *C.char {
|
func goVColumn(pCursor, cp unsafe.Pointer, col C.int) *C.char {
|
||||||
vtc := lookupHandle(uintptr(pCursor)).(*sqliteVTabCursor)
|
vtc := lookupHandle(pCursor).(*sqliteVTabCursor)
|
||||||
c := (*SQLiteContext)(cp)
|
c := (*SQLiteContext)(cp)
|
||||||
err := vtc.vTabCursor.Column(c, int(col))
|
err := vtc.vTabCursor.Column(c, int(col))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -511,7 +564,7 @@ func goVColumn(pCursor, cp unsafe.Pointer, col C.int) *C.char {
|
|||||||
|
|
||||||
//export goVRowid
|
//export goVRowid
|
||||||
func goVRowid(pCursor unsafe.Pointer, pRowid *C.sqlite3_int64) *C.char {
|
func goVRowid(pCursor unsafe.Pointer, pRowid *C.sqlite3_int64) *C.char {
|
||||||
vtc := lookupHandle(uintptr(pCursor)).(*sqliteVTabCursor)
|
vtc := lookupHandle(pCursor).(*sqliteVTabCursor)
|
||||||
rowid, err := vtc.vTabCursor.Rowid()
|
rowid, err := vtc.vTabCursor.Rowid()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return mPrintf("%s", err.Error())
|
return mPrintf("%s", err.Error())
|
||||||
@@ -522,7 +575,7 @@ func goVRowid(pCursor unsafe.Pointer, pRowid *C.sqlite3_int64) *C.char {
|
|||||||
|
|
||||||
//export goVUpdate
|
//export goVUpdate
|
||||||
func goVUpdate(pVTab unsafe.Pointer, argc C.int, argv **C.sqlite3_value, pRowid *C.sqlite3_int64) *C.char {
|
func goVUpdate(pVTab unsafe.Pointer, argc C.int, argv **C.sqlite3_value, pRowid *C.sqlite3_int64) *C.char {
|
||||||
vt := lookupHandle(uintptr(pVTab)).(*sqliteVTab)
|
vt := lookupHandle(pVTab).(*sqliteVTab)
|
||||||
|
|
||||||
var tname string
|
var tname string
|
||||||
if n, ok := vt.vTab.(interface {
|
if n, ok := vt.vTab.(interface {
|
||||||
@@ -585,6 +638,13 @@ type Module interface {
|
|||||||
DestroyModule()
|
DestroyModule()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// EponymousOnlyModule is a "virtual table module" (as above), but
|
||||||
|
// for defining "eponymous only" virtual tables See: https://sqlite.org/vtab.html#eponymous_only_virtual_tables
|
||||||
|
type EponymousOnlyModule interface {
|
||||||
|
Module
|
||||||
|
EponymousOnlyModule()
|
||||||
|
}
|
||||||
|
|
||||||
// VTab describes a particular instance of the virtual table.
|
// VTab describes a particular instance of the virtual table.
|
||||||
// See: http://sqlite.org/c3ref/vtab.html
|
// See: http://sqlite.org/c3ref/vtab.html
|
||||||
type VTab interface {
|
type VTab interface {
|
||||||
@@ -642,9 +702,19 @@ func (c *SQLiteConn) CreateModule(moduleName string, module Module) error {
|
|||||||
mname := C.CString(moduleName)
|
mname := C.CString(moduleName)
|
||||||
defer C.free(unsafe.Pointer(mname))
|
defer C.free(unsafe.Pointer(mname))
|
||||||
udm := sqliteModule{c, moduleName, module}
|
udm := sqliteModule{c, moduleName, module}
|
||||||
rv := C._sqlite3_create_module(c.db, mname, C.uintptr_t(newHandle(c, &udm)))
|
switch module.(type) {
|
||||||
if rv != C.SQLITE_OK {
|
case EponymousOnlyModule:
|
||||||
return c.lastError()
|
rv := C._sqlite3_create_module_eponymous_only(c.db, mname, C.uintptr_t(uintptr(newHandle(c, &udm))))
|
||||||
|
if rv != C.SQLITE_OK {
|
||||||
|
return c.lastError()
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
case Module:
|
||||||
|
rv := C._sqlite3_create_module(c.db, mname, C.uintptr_t(uintptr(newHandle(c, &udm))))
|
||||||
|
if rv != C.SQLITE_OK {
|
||||||
|
return c.lastError()
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-3
@@ -9,7 +9,7 @@ package sqlite3
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
#ifndef USE_LIBSQLITE3
|
#ifndef USE_LIBSQLITE3
|
||||||
#include <sqlite3-binding.h>
|
#include "sqlite3-binding.h"
|
||||||
#else
|
#else
|
||||||
#include <sqlite3.h>
|
#include <sqlite3.h>
|
||||||
#endif
|
#endif
|
||||||
@@ -215,7 +215,6 @@ func addTraceMapping(connHandle uintptr, traceConf TraceConfig) {
|
|||||||
traceConf, connHandle, oldEntryCopy.config))
|
traceConf, connHandle, oldEntryCopy.config))
|
||||||
}
|
}
|
||||||
traceMap[connHandle] = traceMapEntry{config: traceConf}
|
traceMap[connHandle] = traceMapEntry{config: traceConf}
|
||||||
fmt.Printf("Added trace config %v: handle 0x%x.\n", traceConf, connHandle)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func lookupTraceMapping(connHandle uintptr) (TraceConfig, bool) {
|
func lookupTraceMapping(connHandle uintptr) (TraceConfig, bool) {
|
||||||
@@ -234,7 +233,6 @@ func popTraceMapping(connHandle uintptr) (TraceConfig, bool) {
|
|||||||
entryCopy, found := traceMap[connHandle]
|
entryCopy, found := traceMap[connHandle]
|
||||||
if found {
|
if found {
|
||||||
delete(traceMap, connHandle)
|
delete(traceMap, connHandle)
|
||||||
fmt.Printf("Pop handle 0x%x: deleted trace config %v.\n", connHandle, entryCopy.config)
|
|
||||||
}
|
}
|
||||||
return entryCopy.config, found
|
return entryCopy.config, found
|
||||||
}
|
}
|
||||||
|
|||||||
+68
-22
@@ -1,5 +1,4 @@
|
|||||||
// Copyright (C) 2019 Yasuhiro Matsumoto <mattn.jp@gmail.com>.
|
// Copyright (C) 2019 Yasuhiro Matsumoto <mattn.jp@gmail.com>.
|
||||||
//
|
|
||||||
// Use of this source code is governed by an MIT-style
|
// Use of this source code is governed by an MIT-style
|
||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
@@ -7,15 +6,16 @@ package sqlite3
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
#ifndef USE_LIBSQLITE3
|
#ifndef USE_LIBSQLITE3
|
||||||
#include <sqlite3-binding.h>
|
#include "sqlite3-binding.h"
|
||||||
#else
|
#else
|
||||||
#include <sqlite3.h>
|
#include <sqlite3.h>
|
||||||
#endif
|
#endif
|
||||||
*/
|
*/
|
||||||
import "C"
|
import "C"
|
||||||
import (
|
import (
|
||||||
|
"database/sql"
|
||||||
"reflect"
|
"reflect"
|
||||||
"time"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ColumnTypeDatabaseTypeName implement RowsColumnTypeDatabaseTypeName.
|
// ColumnTypeDatabaseTypeName implement RowsColumnTypeDatabaseTypeName.
|
||||||
@@ -40,23 +40,69 @@ func (rc *SQLiteRows) ColumnTypeNullable(i int) (nullable, ok bool) {
|
|||||||
|
|
||||||
// ColumnTypeScanType implement RowsColumnTypeScanType.
|
// ColumnTypeScanType implement RowsColumnTypeScanType.
|
||||||
func (rc *SQLiteRows) ColumnTypeScanType(i int) reflect.Type {
|
func (rc *SQLiteRows) ColumnTypeScanType(i int) reflect.Type {
|
||||||
switch C.sqlite3_column_type(rc.s.s, C.int(i)) {
|
//ct := C.sqlite3_column_type(rc.s.s, C.int(i)) // Always returns 5
|
||||||
case C.SQLITE_INTEGER:
|
return scanType(C.GoString(C.sqlite3_column_decltype(rc.s.s, C.int(i))))
|
||||||
switch C.GoString(C.sqlite3_column_decltype(rc.s.s, C.int(i))) {
|
}
|
||||||
case "timestamp", "datetime", "date":
|
|
||||||
return reflect.TypeOf(time.Time{})
|
const (
|
||||||
case "boolean":
|
SQLITE_INTEGER = iota
|
||||||
return reflect.TypeOf(false)
|
SQLITE_TEXT
|
||||||
}
|
SQLITE_BLOB
|
||||||
return reflect.TypeOf(int64(0))
|
SQLITE_REAL
|
||||||
case C.SQLITE_FLOAT:
|
SQLITE_NUMERIC
|
||||||
return reflect.TypeOf(float64(0))
|
SQLITE_TIME
|
||||||
case C.SQLITE_BLOB:
|
SQLITE_BOOL
|
||||||
return reflect.SliceOf(reflect.TypeOf(byte(0)))
|
SQLITE_NULL
|
||||||
case C.SQLITE_NULL:
|
)
|
||||||
return reflect.TypeOf(nil)
|
|
||||||
case C.SQLITE_TEXT:
|
func scanType(cdt string) reflect.Type {
|
||||||
return reflect.TypeOf("")
|
t := strings.ToUpper(cdt)
|
||||||
}
|
i := databaseTypeConvSqlite(t)
|
||||||
return reflect.SliceOf(reflect.TypeOf(byte(0)))
|
switch i {
|
||||||
|
case SQLITE_INTEGER:
|
||||||
|
return reflect.TypeOf(sql.NullInt64{})
|
||||||
|
case SQLITE_TEXT:
|
||||||
|
return reflect.TypeOf(sql.NullString{})
|
||||||
|
case SQLITE_BLOB:
|
||||||
|
return reflect.TypeOf(sql.RawBytes{})
|
||||||
|
case SQLITE_REAL:
|
||||||
|
return reflect.TypeOf(sql.NullFloat64{})
|
||||||
|
case SQLITE_NUMERIC:
|
||||||
|
return reflect.TypeOf(sql.NullFloat64{})
|
||||||
|
case SQLITE_BOOL:
|
||||||
|
return reflect.TypeOf(sql.NullBool{})
|
||||||
|
case SQLITE_TIME:
|
||||||
|
return reflect.TypeOf(sql.NullTime{})
|
||||||
|
}
|
||||||
|
return reflect.TypeOf(new(interface{}))
|
||||||
|
}
|
||||||
|
|
||||||
|
func databaseTypeConvSqlite(t string) int {
|
||||||
|
if strings.Contains(t, "INT") {
|
||||||
|
return SQLITE_INTEGER
|
||||||
|
}
|
||||||
|
if t == "CLOB" || t == "TEXT" ||
|
||||||
|
strings.Contains(t, "CHAR") {
|
||||||
|
return SQLITE_TEXT
|
||||||
|
}
|
||||||
|
if t == "BLOB" {
|
||||||
|
return SQLITE_BLOB
|
||||||
|
}
|
||||||
|
if t == "REAL" || t == "FLOAT" ||
|
||||||
|
strings.Contains(t, "DOUBLE") {
|
||||||
|
return SQLITE_REAL
|
||||||
|
}
|
||||||
|
if t == "DATE" || t == "DATETIME" ||
|
||||||
|
t == "TIMESTAMP" {
|
||||||
|
return SQLITE_TIME
|
||||||
|
}
|
||||||
|
if t == "NUMERIC" ||
|
||||||
|
strings.Contains(t, "DECIMAL") {
|
||||||
|
return SQLITE_NUMERIC
|
||||||
|
}
|
||||||
|
if t == "BOOLEAN" {
|
||||||
|
return SQLITE_BOOL
|
||||||
|
}
|
||||||
|
|
||||||
|
return SQLITE_NULL
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-1
@@ -15,7 +15,9 @@ package sqlite3
|
|||||||
// This code should improve performance on windows because
|
// This code should improve performance on windows because
|
||||||
// without the presence of usleep SQLite waits 1 second.
|
// without the presence of usleep SQLite waits 1 second.
|
||||||
//
|
//
|
||||||
// Source: https://stackoverflow.com/questions/5801813/c-usleep-is-obsolete-workarounds-for-windows-mingw?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
|
// Source: https://github.com/php/php-src/blob/PHP-5.0/win32/time.c
|
||||||
|
// License: https://github.com/php/php-src/blob/PHP-5.0/LICENSE
|
||||||
|
// Details: https://stackoverflow.com/questions/5801813/c-usleep-is-obsolete-workarounds-for-windows-mingw?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
|
||||||
|
|
||||||
/*
|
/*
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
|
|||||||
-1
@@ -12,7 +12,6 @@ package sqlite3
|
|||||||
#cgo CFLAGS: -fno-stack-check
|
#cgo CFLAGS: -fno-stack-check
|
||||||
#cgo CFLAGS: -fno-stack-protector
|
#cgo CFLAGS: -fno-stack-protector
|
||||||
#cgo CFLAGS: -mno-stack-arg-probe
|
#cgo CFLAGS: -mno-stack-arg-probe
|
||||||
#cgo LDFLAGS: -lmingwex -lmingw32
|
|
||||||
#cgo windows,386 CFLAGS: -D_USE_32BIT_TIME_T
|
#cgo windows,386 CFLAGS: -D_USE_32BIT_TIME_T
|
||||||
*/
|
*/
|
||||||
import "C"
|
import "C"
|
||||||
|
|||||||
+73
@@ -19,6 +19,10 @@
|
|||||||
#ifndef SQLITE3EXT_H
|
#ifndef SQLITE3EXT_H
|
||||||
#define SQLITE3EXT_H
|
#define SQLITE3EXT_H
|
||||||
#include "sqlite3-binding.h"
|
#include "sqlite3-binding.h"
|
||||||
|
#ifdef __clang__
|
||||||
|
#define assert(condition) ((void)0)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** The following structure holds pointers to all of the SQLite API
|
** The following structure holds pointers to all of the SQLite API
|
||||||
@@ -331,6 +335,42 @@ struct sqlite3_api_routines {
|
|||||||
const char *(*filename_database)(const char*);
|
const char *(*filename_database)(const char*);
|
||||||
const char *(*filename_journal)(const char*);
|
const char *(*filename_journal)(const char*);
|
||||||
const char *(*filename_wal)(const char*);
|
const char *(*filename_wal)(const char*);
|
||||||
|
/* Version 3.32.0 and later */
|
||||||
|
const char *(*create_filename)(const char*,const char*,const char*,
|
||||||
|
int,const char**);
|
||||||
|
void (*free_filename)(const char*);
|
||||||
|
sqlite3_file *(*database_file_object)(const char*);
|
||||||
|
/* Version 3.34.0 and later */
|
||||||
|
int (*txn_state)(sqlite3*,const char*);
|
||||||
|
/* Version 3.36.1 and later */
|
||||||
|
sqlite3_int64 (*changes64)(sqlite3*);
|
||||||
|
sqlite3_int64 (*total_changes64)(sqlite3*);
|
||||||
|
/* Version 3.37.0 and later */
|
||||||
|
int (*autovacuum_pages)(sqlite3*,
|
||||||
|
unsigned int(*)(void*,const char*,unsigned int,unsigned int,unsigned int),
|
||||||
|
void*, void(*)(void*));
|
||||||
|
/* Version 3.38.0 and later */
|
||||||
|
int (*error_offset)(sqlite3*);
|
||||||
|
int (*vtab_rhs_value)(sqlite3_index_info*,int,sqlite3_value**);
|
||||||
|
int (*vtab_distinct)(sqlite3_index_info*);
|
||||||
|
int (*vtab_in)(sqlite3_index_info*,int,int);
|
||||||
|
int (*vtab_in_first)(sqlite3_value*,sqlite3_value**);
|
||||||
|
int (*vtab_in_next)(sqlite3_value*,sqlite3_value**);
|
||||||
|
/* Version 3.39.0 and later */
|
||||||
|
int (*deserialize)(sqlite3*,const char*,unsigned char*,
|
||||||
|
sqlite3_int64,sqlite3_int64,unsigned);
|
||||||
|
unsigned char *(*serialize)(sqlite3*,const char *,sqlite3_int64*,
|
||||||
|
unsigned int);
|
||||||
|
const char *(*db_name)(sqlite3*,int);
|
||||||
|
/* Version 3.40.0 and later */
|
||||||
|
int (*value_encoding)(sqlite3_value*);
|
||||||
|
/* Version 3.41.0 and later */
|
||||||
|
int (*is_interrupted)(sqlite3*);
|
||||||
|
/* Version 3.43.0 and later */
|
||||||
|
int (*stmt_explain)(sqlite3_stmt*,int);
|
||||||
|
/* Version 3.44.0 and later */
|
||||||
|
void *(*get_clientdata)(sqlite3*,const char*);
|
||||||
|
int (*set_clientdata)(sqlite3*, const char*, void*, void(*)(void*));
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -631,6 +671,39 @@ typedef int (*sqlite3_loadext_entry)(
|
|||||||
#define sqlite3_filename_database sqlite3_api->filename_database
|
#define sqlite3_filename_database sqlite3_api->filename_database
|
||||||
#define sqlite3_filename_journal sqlite3_api->filename_journal
|
#define sqlite3_filename_journal sqlite3_api->filename_journal
|
||||||
#define sqlite3_filename_wal sqlite3_api->filename_wal
|
#define sqlite3_filename_wal sqlite3_api->filename_wal
|
||||||
|
/* Version 3.32.0 and later */
|
||||||
|
#define sqlite3_create_filename sqlite3_api->create_filename
|
||||||
|
#define sqlite3_free_filename sqlite3_api->free_filename
|
||||||
|
#define sqlite3_database_file_object sqlite3_api->database_file_object
|
||||||
|
/* Version 3.34.0 and later */
|
||||||
|
#define sqlite3_txn_state sqlite3_api->txn_state
|
||||||
|
/* Version 3.36.1 and later */
|
||||||
|
#define sqlite3_changes64 sqlite3_api->changes64
|
||||||
|
#define sqlite3_total_changes64 sqlite3_api->total_changes64
|
||||||
|
/* Version 3.37.0 and later */
|
||||||
|
#define sqlite3_autovacuum_pages sqlite3_api->autovacuum_pages
|
||||||
|
/* Version 3.38.0 and later */
|
||||||
|
#define sqlite3_error_offset sqlite3_api->error_offset
|
||||||
|
#define sqlite3_vtab_rhs_value sqlite3_api->vtab_rhs_value
|
||||||
|
#define sqlite3_vtab_distinct sqlite3_api->vtab_distinct
|
||||||
|
#define sqlite3_vtab_in sqlite3_api->vtab_in
|
||||||
|
#define sqlite3_vtab_in_first sqlite3_api->vtab_in_first
|
||||||
|
#define sqlite3_vtab_in_next sqlite3_api->vtab_in_next
|
||||||
|
/* Version 3.39.0 and later */
|
||||||
|
#ifndef SQLITE_OMIT_DESERIALIZE
|
||||||
|
#define sqlite3_deserialize sqlite3_api->deserialize
|
||||||
|
#define sqlite3_serialize sqlite3_api->serialize
|
||||||
|
#endif
|
||||||
|
#define sqlite3_db_name sqlite3_api->db_name
|
||||||
|
/* Version 3.40.0 and later */
|
||||||
|
#define sqlite3_value_encoding sqlite3_api->value_encoding
|
||||||
|
/* Version 3.41.0 and later */
|
||||||
|
#define sqlite3_is_interrupted sqlite3_api->is_interrupted
|
||||||
|
/* Version 3.43.0 and later */
|
||||||
|
#define sqlite3_stmt_explain sqlite3_api->stmt_explain
|
||||||
|
/* Version 3.44.0 and later */
|
||||||
|
#define sqlite3_get_clientdata sqlite3_api->get_clientdata
|
||||||
|
#define sqlite3_set_clientdata sqlite3_api->set_clientdata
|
||||||
#endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */
|
#endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */
|
||||||
|
|
||||||
#if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
|
#if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
|
||||||
|
|||||||
+19
-8
@@ -13,14 +13,25 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
|
||||||
sql.Register("sqlite3", &SQLiteDriverMock{})
|
|
||||||
}
|
|
||||||
|
|
||||||
type SQLiteDriverMock struct{}
|
|
||||||
|
|
||||||
var errorMsg = errors.New("Binary was compiled with 'CGO_ENABLED=0', go-sqlite3 requires cgo to work. This is a stub")
|
var errorMsg = errors.New("Binary was compiled with 'CGO_ENABLED=0', go-sqlite3 requires cgo to work. This is a stub")
|
||||||
|
|
||||||
func (SQLiteDriverMock) Open(s string) (driver.Conn, error) {
|
func init() {
|
||||||
return nil, errorMsg
|
sql.Register("sqlite3", &SQLiteDriver{})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type (
|
||||||
|
SQLiteDriver struct {
|
||||||
|
Extensions []string
|
||||||
|
ConnectHook func(*SQLiteConn) error
|
||||||
|
}
|
||||||
|
SQLiteConn struct{}
|
||||||
|
)
|
||||||
|
|
||||||
|
func (SQLiteDriver) Open(s string) (driver.Conn, error) { return nil, errorMsg }
|
||||||
|
func (c *SQLiteConn) RegisterAggregator(string, interface{}, bool) error { return errorMsg }
|
||||||
|
func (c *SQLiteConn) RegisterAuthorizer(func(int, string, string, string) int) {}
|
||||||
|
func (c *SQLiteConn) RegisterCollation(string, func(string, string) int) error { return errorMsg }
|
||||||
|
func (c *SQLiteConn) RegisterCommitHook(func() int) {}
|
||||||
|
func (c *SQLiteConn) RegisterFunc(string, interface{}, bool) error { return errorMsg }
|
||||||
|
func (c *SQLiteConn) RegisterRollbackHook(func()) {}
|
||||||
|
func (c *SQLiteConn) RegisterUpdateHook(func(int, string, string, int64)) {}
|
||||||
|
|||||||
Vendored
+2
-2
@@ -1310,8 +1310,8 @@ github.com/mattn/go-isatty
|
|||||||
# github.com/mattn/go-runewidth v0.0.13
|
# github.com/mattn/go-runewidth v0.0.13
|
||||||
## explicit; go 1.9
|
## explicit; go 1.9
|
||||||
github.com/mattn/go-runewidth
|
github.com/mattn/go-runewidth
|
||||||
# github.com/mattn/go-sqlite3 v2.0.3+incompatible
|
# github.com/mattn/go-sqlite3 v1.14.19
|
||||||
## explicit
|
## explicit; go 1.16
|
||||||
github.com/mattn/go-sqlite3
|
github.com/mattn/go-sqlite3
|
||||||
# github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0
|
# github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0
|
||||||
## explicit; go 1.19
|
## explicit; go 1.19
|
||||||
|
|||||||
Reference in New Issue
Block a user