@ -0,0 +1,4 @@ | |||
coverage: | |||
status: | |||
project: off | |||
patch: off |
@ -0,0 +1,14 @@ | |||
*.db | |||
*.exe | |||
*.dll | |||
*.o | |||
# VSCode | |||
.vscode | |||
# Exclude from upgrade | |||
upgrade/*.c | |||
upgrade/*.h | |||
# Exclude upgrade binary | |||
upgrade/upgrade |
@ -0,0 +1,21 @@ | |||
The MIT License (MIT) | |||
Copyright (c) 2014 Yasuhiro Matsumoto | |||
Permission is hereby granted, free of charge, to any person obtaining a copy | |||
of this software and associated documentation files (the "Software"), to deal | |||
in the Software without restriction, including without limitation the rights | |||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |||
copies of the Software, and to permit persons to whom the Software is | |||
furnished to do so, subject to the following conditions: | |||
The above copyright notice and this permission notice shall be included in all | |||
copies or substantial portions of the Software. | |||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |||
SOFTWARE. |
@ -0,0 +1,603 @@ | |||
go-sqlite3 | |||
========== | |||
[](https://pkg.go.dev/github.com/mattn/go-sqlite3) | |||
[](https://github.com/mattn/go-sqlite3/actions?query=workflow%3AGo) | |||
[](https://opencollective.com/mattn-go-sqlite3) | |||
[](https://codecov.io/gh/mattn/go-sqlite3) | |||
[](https://goreportcard.com/report/github.com/mattn/go-sqlite3) | |||
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 | |||
A sqlite3 driver that conforms to the built-in database/sql interface. | |||
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). | |||
### Overview | |||
- [go-sqlite3](#go-sqlite3) | |||
- [Description](#description) | |||
- [Overview](#overview) | |||
- [Installation](#installation) | |||
- [API Reference](#api-reference) | |||
- [Connection String](#connection-string) | |||
- [DSN Examples](#dsn-examples) | |||
- [Features](#features) | |||
- [Usage](#usage) | |||
- [Feature / Extension List](#feature--extension-list) | |||
- [Compilation](#compilation) | |||
- [Android](#android) | |||
- [ARM](#arm) | |||
- [Cross Compile](#cross-compile) | |||
- [Google Cloud Platform](#google-cloud-platform) | |||
- [Linux](#linux) | |||
- [Alpine](#alpine) | |||
- [Fedora](#fedora) | |||
- [Ubuntu](#ubuntu) | |||
- [macOS](#mac-osx) | |||
- [Windows](#windows) | |||
- [Errors](#errors) | |||
- [User Authentication](#user-authentication) | |||
- [Compile](#compile) | |||
- [Usage](#usage-1) | |||
- [Create protected database](#create-protected-database) | |||
- [Password Encoding](#password-encoding) | |||
- [Available Encoders](#available-encoders) | |||
- [Restrictions](#restrictions) | |||
- [Support](#support) | |||
- [User Management](#user-management) | |||
- [SQL](#sql) | |||
- [Examples](#examples) | |||
- [*SQLiteConn](#sqliteconn) | |||
- [Attached database](#attached-database) | |||
- [Extensions](#extensions) | |||
- [Spatialite](#spatialite) | |||
- [FAQ](#faq) | |||
- [License](#license) | |||
- [Author](#author) | |||
# Installation | |||
This package can be installed with the `go get` command: | |||
go get github.com/mattn/go-sqlite3 | |||
_go-sqlite3_ is *cgo* package. | |||
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. | |||
***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.*** | |||
# API Reference | |||
API documentation can be found [here](http://godoc.org/github.com/mattn/go-sqlite3). | |||
Examples can be found under the [examples](./_example) directory. | |||
# Connection String | |||
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 (Data Source Name) string. | |||
Options are append after the filename of the SQLite database. | |||
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)). | |||
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. | |||
This library supports DSN options of SQLite itself and provides additional options. | |||
Boolean values can be one of: | |||
* `0` `no` `false` `off` | |||
* `1` `yes` `true` `on` | |||
| Name | Key | Value(s) | Description | | |||
|------|-----|----------|-------------| | |||
| UA - Create | `_auth` | - | Create User Authentication, for more information see [User Authentication](#user-authentication) | | |||
| UA - Username | `_auth_user` | `string` | Username for User Authentication, for more information see [User Authentication](#user-authentication) | | |||
| UA - Password | `_auth_pass` | `string` | Password for User Authentication, for more information see [User Authentication](#user-authentication) | | |||
| UA - Crypt | `_auth_crypt` | <ul><li>SHA1</li><li>SSHA1</li><li>SHA256</li><li>SSHA256</li><li>SHA384</li><li>SSHA384</li><li>SHA512</li><li>SSHA512</li></ul> | Password encoder to use for User Authentication, for more information see [User Authentication](#user-authentication) | | |||
| UA - Salt | `_auth_salt` | `string` | Salt to use if the configure password encoder requires a salt, for User Authentication, for more information see [User Authentication](#user-authentication) | | |||
| Auto Vacuum | `_auto_vacuum` \| `_vacuum` | <ul><li>`0` \| `none`</li><li>`1` \| `full`</li><li>`2` \| `incremental`</li></ul> | For more information see [PRAGMA auto_vacuum](https://www.sqlite.org/pragma.html#pragma_auto_vacuum) | | |||
| Busy Timeout | `_busy_timeout` \| `_timeout` | `int` | Specify value for sqlite3_busy_timeout. For more information see [PRAGMA busy_timeout](https://www.sqlite.org/pragma.html#pragma_busy_timeout) | | |||
| Case Sensitive LIKE | `_case_sensitive_like` \| `_cslike` | `boolean` | For more information see [PRAGMA case_sensitive_like](https://www.sqlite.org/pragma.html#pragma_case_sensitive_like) | | |||
| Defer Foreign Keys | `_defer_foreign_keys` \| `_defer_fk` | `boolean` | For more information see [PRAGMA defer_foreign_keys](https://www.sqlite.org/pragma.html#pragma_defer_foreign_keys) | | |||
| Foreign Keys | `_foreign_keys` \| `_fk` | `boolean` | For more information see [PRAGMA foreign_keys](https://www.sqlite.org/pragma.html#pragma_foreign_keys) | | |||
| Ignore CHECK Constraints | `_ignore_check_constraints` | `boolean` | For more information see [PRAGMA ignore_check_constraints](https://www.sqlite.org/pragma.html#pragma_ignore_check_constraints) | | |||
| Immutable | `immutable` | `boolean` | For more information see [Immutable](https://www.sqlite.org/c3ref/open.html) | | |||
| Journal Mode | `_journal_mode` \| `_journal` | <ul><li>DELETE</li><li>TRUNCATE</li><li>PERSIST</li><li>MEMORY</li><li>WAL</li><li>OFF</li></ul> | For more information see [PRAGMA journal_mode](https://www.sqlite.org/pragma.html#pragma_journal_mode) | | |||
| Locking Mode | `_locking_mode` \| `_locking` | <ul><li>NORMAL</li><li>EXCLUSIVE</li></ul> | For more information see [PRAGMA locking_mode](https://www.sqlite.org/pragma.html#pragma_locking_mode) | | |||
| Mode | `mode` | <ul><li>ro</li><li>rw</li><li>rwc</li><li>memory</li></ul> | Access Mode of the database. For more information see [SQLite Open](https://www.sqlite.org/c3ref/open.html) | | |||
| Mutex Locking | `_mutex` | <ul><li>no</li><li>full</li></ul> | Specify mutex mode. | | |||
| Query Only | `_query_only` | `boolean` | For more information see [PRAGMA query_only](https://www.sqlite.org/pragma.html#pragma_query_only) | | |||
| Recursive Triggers | `_recursive_triggers` \| `_rt` | `boolean` | For more information see [PRAGMA recursive_triggers](https://www.sqlite.org/pragma.html#pragma_recursive_triggers) | | |||
| Secure Delete | `_secure_delete` | `boolean` \| `FAST` | For more information see [PRAGMA secure_delete](https://www.sqlite.org/pragma.html#pragma_secure_delete) | | |||
| Shared-Cache Mode | `cache` | <ul><li>shared</li><li>private</li></ul> | Set cache mode for more information see [sqlite.org](https://www.sqlite.org/sharedcache.html) | | |||
| Synchronous | `_synchronous` \| `_sync` | <ul><li>0 \| OFF</li><li>1 \| NORMAL</li><li>2 \| FULL</li><li>3 \| EXTRA</li></ul> | For more information see [PRAGMA synchronous](https://www.sqlite.org/pragma.html#pragma_synchronous) | | |||
| 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. | | |||
| 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 | |||
``` | |||
file:test.db?cache=shared&mode=memory | |||
``` | |||
# Features | |||
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](https://golang.org/pkg/go/build/#hdr-Build_Constraints) for more information about build tags / constraints. | |||
### Usage | |||
If you wish to build this library with additional extensions / features, use the following command: | |||
```bash | |||
go build -tags "<FEATURE>" | |||
``` | |||
For available features, see the extension list. | |||
When using multiple build tags, all the different tags should be space delimited. | |||
Example: | |||
```bash | |||
go build -tags "icu json1 fts5 secure_delete" | |||
``` | |||
### Feature / Extension List | |||
| Extension | Build Tag | Description | | |||
|-----------|-----------|-------------| | |||
| Additional Statistics | sqlite_stat4 | This option adds additional logic to the ANALYZE command and to the query planner that can help SQLite to chose a better query plan under certain situations. The ANALYZE command is enhanced to collect histogram data from all columns of every index and store that data in the sqlite_stat4 table.<br><br>The query planner will then use the histogram data to help it make better index choices. The downside of this compile-time option is that it violates the query planner stability guarantee making it more difficult to ensure consistent performance in mass-produced applications.<br><br>SQLITE_ENABLE_STAT4 is an enhancement of SQLITE_ENABLE_STAT3. STAT3 only recorded histogram data for the left-most column of each index whereas the STAT4 enhancement records histogram data from all columns of each index.<br><br>The SQLITE_ENABLE_STAT3 compile-time option is a no-op and is ignored if the SQLITE_ENABLE_STAT4 compile-time option is used | | |||
| 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`. | | |||
| 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 | | |||
| 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 | | |||
| Full Text Search Engine | sqlite_fts5 | When this option is defined in the amalgamation, versions 5 of the full-text search engine (fts5) is added to the build automatically | | |||
| 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> | | |||
| 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. | | |||
| 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) | | |||
| Tracing / Debug | sqlite_trace | Activate trace functions | | |||
| 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 | |||
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. | |||
## Android | |||
This package can be compiled for android. | |||
Compile with: | |||
```bash | |||
go build -tags "android" | |||
``` | |||
For more information see [#201](https://github.com/mattn/go-sqlite3/issues/201) | |||
# ARM | |||
To compile for `ARM` use the following environment: | |||
```bash | |||
env CC=arm-linux-gnueabihf-gcc CXX=arm-linux-gnueabihf-g++ \ | |||
CGO_ENABLED=1 GOOS=linux GOARCH=arm GOARM=7 \ | |||
go build -v | |||
``` | |||
Additional information: | |||
- [#242](https://github.com/mattn/go-sqlite3/issues/242) | |||
- [#504](https://github.com/mattn/go-sqlite3/issues/504) | |||
# Cross Compile | |||
This library can be cross-compiled. | |||
In some cases you are required to the `CC` environment variable with the cross compiler. | |||
## Cross Compiling from macOS | |||
The simplest way to cross compile from macOS is to use [xgo](https://github.com/karalabe/xgo). | |||
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 | |||
Building on GCP is not possible because Google Cloud Platform does not allow `gcc` to be executed. | |||
Please work only with compiled final binaries. | |||
## Linux | |||
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`. | |||
```bash | |||
go build -tags "linux" | |||
``` | |||
If you wish to link directly to libsqlite3 then you can use the `libsqlite3` build tag. | |||
``` | |||
go build -tags "libsqlite3 linux" | |||
``` | |||
### Alpine | |||
When building in an `alpine` container run the following command before building: | |||
``` | |||
apk add --update gcc musl-dev | |||
``` | |||
### Fedora | |||
```bash | |||
sudo yum groupinstall "Development Tools" "Development Libraries" | |||
``` | |||
### Ubuntu | |||
```bash | |||
sudo apt-get install build-essential | |||
``` | |||
## macOS | |||
macOS should have all the tools present to compile this package. If not, install XCode to add all the developers tools. | |||
Required dependency: | |||
```bash | |||
brew install sqlite3 | |||
``` | |||
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`: | |||
```bash | |||
brew upgrade icu4c | |||
``` | |||
To compile for macOS on x86: | |||
```bash | |||
go build -tags "darwin amd64" | |||
``` | |||
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: | |||
``` | |||
# x86 | |||
go build -tags "libsqlite3 darwin amd64" | |||
# ARM | |||
go build -tags "libsqlite3 darwin arm64" | |||
``` | |||
Additional information: | |||
- [#206](https://github.com/mattn/go-sqlite3/issues/206) | |||
- [#404](https://github.com/mattn/go-sqlite3/issues/404) | |||
## Windows | |||
To compile this package on Windows, you must have the `gcc` compiler installed. | |||
1) Install a Windows `gcc` toolchain. | |||
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, which can be found in the Windows Start menu. | |||
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://jmeubank.github.io/tdm-gcc/). | |||
## Errors | |||
- Compile error: `can not be used when making a shared object; recompile with -fPIC` | |||
When receiving a compile time error referencing recompile with `-FPIC` then you | |||
are probably using a hardend system. | |||
You can compile the library on a hardend system with the following command. | |||
```bash | |||
go build -ldflags '-extldflags=-fno-PIC' | |||
``` | |||
More details see [#120](https://github.com/mattn/go-sqlite3/issues/120) | |||
- Can't build go-sqlite3 on windows 64bit. | |||
> Probably, you are using go 1.0, go1.0 has a problem when it comes to compiling/linking on windows 64bit. | |||
> See: [#27](https://github.com/mattn/go-sqlite3/issues/27) | |||
- `go get github.com/mattn/go-sqlite3` throws compilation error. | |||
`gcc` throws: `internal compiler error` | |||
Remove the download repository from your disk and try re-install with: | |||
```bash | |||
go install github.com/mattn/go-sqlite3 | |||
``` | |||
# User Authentication | |||
This package supports the SQLite User Authentication module. | |||
## Compile | |||
To use the User authentication module, the package has to be compiled with the tag `sqlite_userauth`. See [Features](#features). | |||
## Usage | |||
### Create protected database | |||
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: | |||
- `_auth_user` | |||
- `_auth_pass` | |||
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. | |||
Example connection strings: | |||
Create an user authentication database with user `admin` and password `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: | |||
`file:test.s3db?_auth&_auth_user=admin&_auth_pass=admin&_auth_crypt=sha1` | |||
### Password Encoding | |||
The passwords within the user authentication module of SQLite are encoded with the SQLite function `sqlite_cryp`. | |||
This function uses a ceasar-cypher which is quite insecure. | |||
This library provides several additional password encoders which can be configured through the connection string. | |||
The password cypher can be configured with the key `_auth_crypt`. And if the configured password encoder also requires an | |||
salt this can be configured with `_auth_salt`. | |||
#### Available Encoders | |||
- SHA1 | |||
- SSHA1 (Salted SHA1) | |||
- SHA256 | |||
- SSHA256 (salted SHA256) | |||
- SHA384 | |||
- SSHA384 (salted SHA384) | |||
- SHA512 | |||
- SSHA512 (salted SHA512) | |||
### Restrictions | |||
Operations on the database regarding user management can only be preformed by an administrator user. | |||
### Support | |||
The user authentication supports two kinds of users: | |||
- administrators | |||
- regular users | |||
### User Management | |||
User management can be done by directly using the `*SQLiteConn` or by SQL. | |||
#### SQL | |||
The following sql functions are available for user management: | |||
| Function | Arguments | Description | | |||
|----------|-----------|-------------| | |||
| `authenticate` | username `string`, password `string` | Will authenticate an user, this is done by the connection; and should not be used manually. | | |||
| `auth_user_add` | username `string`, password `string`, admin `int` | This function will add an user to the database.<br>if the database is not protected by user authentication it will enable it. Argument `admin` is an integer identifying if the added user should be an administrator. Only Administrators can add administrators. | | |||
| `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. | | |||
These functions will return an integer: | |||
- 0 (SQLITE_OK) | |||
- 23 (SQLITE_AUTH) Failed to perform due to authentication or insufficient privileges | |||
##### Examples | |||
```sql | |||
// Autheticate user | |||
// Create Admin User | |||
SELECT auth_user_add('admin2', 'admin2', 1); | |||
// Change password for user | |||
SELECT auth_user_change('user', 'userpassword', 0); | |||
// Delete user | |||
SELECT user_delete('user'); | |||
``` | |||
#### *SQLiteConn | |||
The following functions are available for User authentication from the `*SQLiteConn`: | |||
| Function | Description | | |||
|----------|-------------| | |||
| `Authenticate(username, password string) error` | Authenticate user | | |||
| `AuthUserAdd(username, password string, admin bool) error` | Add user | | |||
| `AuthUserChange(username, password string, admin bool) error` | Modify user | | |||
| `AuthUserDelete(username string) error` | Delete user | | |||
### Attached database | |||
When using attached databases, SQLite will use the authentication from the `main` database for the attached database(s). | |||
# 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. | |||
## Spatialite | |||
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). | |||
## 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 | |||
- Getting insert error while query is opened. | |||
> You can pass some arguments into the connection string, for example, a URI. | |||
> See: [#39](https://github.com/mattn/go-sqlite3/issues/39) | |||
- Do you want to cross compile? mingw on Linux or Mac? | |||
> See: [#106](https://github.com/mattn/go-sqlite3/issues/106) | |||
> See also: http://www.limitlessfx.com/cross-compile-golang-app-for-windows-from-linux.html | |||
- Want to get time.Time with current locale | |||
Use `_loc=auto` in SQLite3 filename schema like `file:foo.db?_loc=auto`. | |||
- Can I use this in multiple routines concurrently? | |||
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 is it racy if I use a `sql.Open("sqlite3", ":memory:")` database? | |||
Each connection to `":memory:"` opens a brand new in-memory sql database, so if | |||
the stdlib's sql engine happens to open another connection and you've only | |||
specified `":memory:"`, that connection will see a brand new database. A | |||
workaround is to use `"file::memory:?cache=shared"` (or `"file:foobar?mode=memory&cache=shared"`). Every | |||
connection to this string will point to the same in-memory database. | |||
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: | |||
* [#204](https://github.com/mattn/go-sqlite3/issues/204) | |||
* [#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/inmemorydb.html#sharedmemdb | |||
- Reading from database with large amount of goroutines fails on OSX. | |||
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) | |||
- Trying to execute a `.` (dot) command throws an error. | |||
Error: `Error: near ".": syntax error` | |||
Dot command are part of SQLite3 CLI, not of this library. | |||
You need to implement the feature or call the sqlite3 cli. | |||
More information see [#305](https://github.com/mattn/go-sqlite3/issues/305). | |||
- Error: `database is locked` | |||
When you get a database is locked, please use the following options. | |||
Add to DSN: `cache=shared` | |||
Example: | |||
```go | |||
db, err := sql.Open("sqlite3", "file:locked.sqlite?cache=shared") | |||
``` | |||
Next, please set the database connections of the SQL package to 1: | |||
```go | |||
db.SetMaxOpenConns(1) | |||
``` | |||
For more information, see [#209](https://github.com/mattn/go-sqlite3/issues/209). | |||
## Contributors | |||
### Code Contributors | |||
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> | |||
### Financial Contributors | |||
Become a financial contributor and help us sustain our community. [[Contribute here](https://opencollective.com/mattn-go-sqlite3/contribute)]. | |||
#### Individuals | |||
<a href="https://opencollective.com/mattn-go-sqlite3"><img src="https://opencollective.com/mattn-go-sqlite3/individuals.svg?width=890"></a> | |||
#### Organizations | |||
Support this project with your organization. Your logo will show up here with a link to your website. [[Contribute](https://opencollective.com/mattn-go-sqlite3/contribute)] | |||
<a href="https://opencollective.com/mattn-go-sqlite3/organization/0/website"><img src="https://opencollective.com/mattn-go-sqlite3/organization/0/avatar.svg"></a> | |||
<a href="https://opencollective.com/mattn-go-sqlite3/organization/1/website"><img src="https://opencollective.com/mattn-go-sqlite3/organization/1/avatar.svg"></a> | |||
<a href="https://opencollective.com/mattn-go-sqlite3/organization/2/website"><img src="https://opencollective.com/mattn-go-sqlite3/organization/2/avatar.svg"></a> | |||
<a href="https://opencollective.com/mattn-go-sqlite3/organization/3/website"><img src="https://opencollective.com/mattn-go-sqlite3/organization/3/avatar.svg"></a> | |||
<a href="https://opencollective.com/mattn-go-sqlite3/organization/4/website"><img src="https://opencollective.com/mattn-go-sqlite3/organization/4/avatar.svg"></a> | |||
<a href="https://opencollective.com/mattn-go-sqlite3/organization/5/website"><img src="https://opencollective.com/mattn-go-sqlite3/organization/5/avatar.svg"></a> | |||
<a href="https://opencollective.com/mattn-go-sqlite3/organization/6/website"><img src="https://opencollective.com/mattn-go-sqlite3/organization/6/avatar.svg"></a> | |||
<a href="https://opencollective.com/mattn-go-sqlite3/organization/7/website"><img src="https://opencollective.com/mattn-go-sqlite3/organization/7/avatar.svg"></a> | |||
<a href="https://opencollective.com/mattn-go-sqlite3/organization/8/website"><img src="https://opencollective.com/mattn-go-sqlite3/organization/8/avatar.svg"></a> | |||
<a href="https://opencollective.com/mattn-go-sqlite3/organization/9/website"><img src="https://opencollective.com/mattn-go-sqlite3/organization/9/avatar.svg"></a> | |||
# License | |||
MIT: http://mattn.mit-license.org/2018 | |||
sqlite3-binding.c, sqlite3-binding.h, sqlite3ext.h | |||
The -binding suffix was added to avoid build failures under gccgo. | |||
In this repository, those files are an amalgamation of code that was copied from SQLite3. The license of that code is the same as the license of SQLite3. | |||
# Author | |||
Yasuhiro Matsumoto (a.k.a mattn) | |||
G.J.R. Timmer |
@ -0,0 +1,12 @@ | |||
TARGET = custom_driver_name | |||
ifeq ($(OS),Windows_NT) | |||
TARGET := $(TARGET).exe | |||
endif | |||
all : $(TARGET) | |||
$(TARGET) : main.go | |||
go build -ldflags="-X 'github.com/mattn/go-sqlite3.driverName=my-sqlite3'" | |||
clean : | |||
rm -f $(TARGET) |
@ -0,0 +1,13 @@ | |||
package main | |||
import ( | |||
"database/sql" | |||
_ "github.com/mattn/go-sqlite3" | |||
) | |||
func main() { | |||
for _, driver := range sql.Drivers() { | |||
println(driver) | |||
} | |||
} |
@ -0,0 +1,133 @@ | |||
package main | |||
import ( | |||
"database/sql" | |||
"fmt" | |||
"log" | |||
"math" | |||
"math/rand" | |||
sqlite "github.com/mattn/go-sqlite3" | |||
) | |||
// Computes x^y | |||
func pow(x, y int64) int64 { | |||
return int64(math.Pow(float64(x), float64(y))) | |||
} | |||
// Computes the bitwise exclusive-or of all its arguments | |||
func xor(xs ...int64) int64 { | |||
var ret int64 | |||
for _, x := range xs { | |||
ret ^= x | |||
} | |||
return ret | |||
} | |||
// Returns a random number. It's actually deterministic here because | |||
// we don't seed the RNG, but it's an example of a non-pure function | |||
// from SQLite's POV. | |||
func getrand() int64 { | |||
return rand.Int63() | |||
} | |||
// Computes the standard deviation of a GROUPed BY set of values | |||
type stddev struct { | |||
xs []int64 | |||
// Running average calculation | |||
sum int64 | |||
n int64 | |||
} | |||
func newStddev() *stddev { return &stddev{} } | |||
func (s *stddev) Step(x int64) { | |||
s.xs = append(s.xs, x) | |||
s.sum += x | |||
s.n++ | |||
} | |||
func (s *stddev) Done() float64 { | |||
mean := float64(s.sum) / float64(s.n) | |||
var sqDiff []float64 | |||
for _, x := range s.xs { | |||
sqDiff = append(sqDiff, math.Pow(float64(x)-mean, 2)) | |||
} | |||
var dev float64 | |||
for _, x := range sqDiff { | |||
dev += x | |||
} | |||
dev /= float64(len(sqDiff)) | |||
return math.Sqrt(dev) | |||
} | |||
func main() { | |||
sql.Register("sqlite3_custom", &sqlite.SQLiteDriver{ | |||
ConnectHook: func(conn *sqlite.SQLiteConn) error { | |||
if err := conn.RegisterFunc("pow", pow, true); err != nil { | |||
return err | |||
} | |||
if err := conn.RegisterFunc("xor", xor, true); err != nil { | |||
return err | |||
} | |||
if err := conn.RegisterFunc("rand", getrand, false); err != nil { | |||
return err | |||
} | |||
if err := conn.RegisterAggregator("stddev", newStddev, true); err != nil { | |||
return err | |||
} | |||
return nil | |||
}, | |||
}) | |||
db, err := sql.Open("sqlite3_custom", ":memory:") | |||
if err != nil { | |||
log.Fatal("Failed to open database:", err) | |||
} | |||
defer db.Close() | |||
var i int64 | |||
err = db.QueryRow("SELECT pow(2,3)").Scan(&i) | |||
if err != nil { | |||
log.Fatal("POW query error:", err) | |||
} | |||
fmt.Println("pow(2,3) =", i) // 8 | |||
err = db.QueryRow("SELECT xor(1,2,3,4,5,6)").Scan(&i) | |||
if err != nil { | |||
log.Fatal("XOR query error:", err) | |||
} | |||
fmt.Println("xor(1,2,3,4,5) =", i) // 7 | |||
err = db.QueryRow("SELECT rand()").Scan(&i) | |||
if err != nil { | |||
log.Fatal("RAND query error:", err) | |||
} | |||
fmt.Println("rand() =", i) // pseudorandom | |||
_, err = db.Exec("create table foo (department integer, profits integer)") | |||
if err != nil { | |||
log.Fatal("Failed to create table:", err) | |||
} | |||
_, err = db.Exec("insert into foo values (1, 10), (1, 20), (1, 45), (2, 42), (2, 115)") | |||
if err != nil { | |||
log.Fatal("Failed to insert records:", err) | |||
} | |||
rows, err := db.Query("select department, stddev(profits) from foo group by department") | |||
if err != nil { | |||
log.Fatal("STDDEV query error:", err) | |||
} | |||
defer rows.Close() | |||
for rows.Next() { | |||
var dept int64 | |||
var dev float64 | |||
if err := rows.Scan(&dept, &dev); err != nil { | |||
log.Fatal(err) | |||
} | |||
fmt.Printf("dept=%d stddev=%f\n", dept, dev) | |||
} | |||
if err := rows.Err(); err != nil { | |||
log.Fatal(err) | |||
} | |||
} |
@ -0,0 +1,30 @@ | |||
package sqlite3_fuzz | |||
import ( | |||
"bytes" | |||
"database/sql" | |||
"io/ioutil" | |||
_ "github.com/mattn/go-sqlite3" | |||
) | |||
func FuzzOpenExec(data []byte) int { | |||
sep := bytes.IndexByte(data, 0) | |||
if sep <= 0 { | |||
return 0 | |||
} | |||
err := ioutil.WriteFile("/tmp/fuzz.db", data[sep+1:], 0644) | |||
if err != nil { | |||
return 0 | |||
} | |||
db, err := sql.Open("sqlite3", "/tmp/fuzz.db") | |||
if err != nil { | |||
return 0 | |||
} | |||
defer db.Close() | |||
_, err = db.Exec(string(data[:sep-1])) | |||
if err != nil { | |||
return 0 | |||
} | |||
return 1 | |||
} |
@ -0,0 +1,78 @@ | |||
package main | |||
import ( | |||
"database/sql" | |||
"log" | |||
"os" | |||
"github.com/mattn/go-sqlite3" | |||
) | |||
func main() { | |||
sqlite3conn := []*sqlite3.SQLiteConn{} | |||
sql.Register("sqlite3_with_hook_example", | |||
&sqlite3.SQLiteDriver{ | |||
ConnectHook: func(conn *sqlite3.SQLiteConn) error { | |||
sqlite3conn = append(sqlite3conn, conn) | |||
conn.RegisterUpdateHook(func(op int, db string, table string, rowid int64) { | |||
switch op { | |||
case sqlite3.SQLITE_INSERT: | |||
log.Println("Notified of insert on db", db, "table", table, "rowid", rowid) | |||
} | |||
}) | |||
return nil | |||
}, | |||
}) | |||
os.Remove("./foo.db") | |||
os.Remove("./bar.db") | |||
srcDb, err := sql.Open("sqlite3_with_hook_example", "./foo.db") | |||
if err != nil { | |||
log.Fatal(err) | |||
} | |||
defer srcDb.Close() | |||
srcDb.Ping() | |||
_, err = srcDb.Exec("create table foo(id int, value text)") | |||
if err != nil { | |||
log.Fatal(err) | |||
} | |||
_, err = srcDb.Exec("insert into foo values(1, 'foo')") | |||
if err != nil { | |||
log.Fatal(err) | |||
} | |||
_, err = srcDb.Exec("insert into foo values(2, 'bar')") | |||
if err != nil { | |||
log.Fatal(err) | |||
} | |||
_, err = srcDb.Query("select * from foo") | |||
if err != nil { | |||
log.Fatal(err) | |||
} | |||
destDb, err := sql.Open("sqlite3_with_hook_example", "./bar.db") | |||
if err != nil { | |||
log.Fatal(err) | |||
} | |||
defer destDb.Close() | |||
destDb.Ping() | |||
bk, err := sqlite3conn[1].Backup("main", sqlite3conn[0], "main") | |||
if err != nil { | |||
log.Fatal(err) | |||
} | |||
_, err = bk.Step(-1) | |||
if err != nil { | |||
log.Fatal(err) | |||
} | |||
_, err = destDb.Query("select * from foo") | |||
if err != nil { | |||
log.Fatal(err) | |||
} | |||
_, err = destDb.Exec("insert into foo values(3, 'bar')") | |||
if err != nil { | |||
log.Fatal(err) | |||
} | |||
bk.Finish() | |||
} |
@ -0,0 +1,113 @@ | |||
package main | |||
import ( | |||
"database/sql" | |||
"fmt" | |||
"log" | |||
"os" | |||
"strings" | |||
"github.com/mattn/go-sqlite3" | |||
) | |||
func createBulkInsertQuery(n int, start int) (query string, args []interface{}) { | |||
values := make([]string, n) | |||
args = make([]interface{}, n*2) | |||
pos := 0 | |||
for i := 0; i < n; i++ { | |||
values[i] = "(?, ?)" | |||
args[pos] = start + i | |||
args[pos+1] = fmt.Sprintf("こんにちは世界%03d", i) | |||
pos += 2 | |||
} | |||
query = fmt.Sprintf( | |||
"insert into foo(id, name) values %s", | |||
strings.Join(values, ", "), | |||
) | |||
return | |||
} | |||
func bulkInsert(db *sql.DB, query string, args []interface{}) (err error) { | |||
stmt, err := db.Prepare(query) | |||
if err != nil { | |||
return | |||
} | |||
_, err = stmt.Exec(args...) | |||
if err != nil { | |||
return | |||
} | |||
return | |||
} | |||
func main() { | |||
var sqlite3conn *sqlite3.SQLiteConn | |||
sql.Register("sqlite3_with_limit", &sqlite3.SQLiteDriver{ | |||
ConnectHook: func(conn *sqlite3.SQLiteConn) error { | |||
sqlite3conn = conn | |||
return nil | |||
}, | |||
}) | |||
os.Remove("./foo.db") | |||
db, err := sql.Open("sqlite3_with_limit", "./foo.db") | |||
if err != nil { | |||
log.Fatal(err) | |||
} | |||
defer db.Close() | |||
sqlStmt := ` | |||
create table foo (id integer not null primary key, name text); | |||
delete from foo; | |||
` | |||
_, err = db.Exec(sqlStmt) | |||
if err != nil { | |||
log.Printf("%q: %s\n", err, sqlStmt) | |||
return | |||
} | |||
if sqlite3conn == nil { | |||
log.Fatal("not set sqlite3 connection") | |||
} | |||
limitVariableNumber := sqlite3conn.GetLimit(sqlite3.SQLITE_LIMIT_VARIABLE_NUMBER) | |||
log.Printf("default SQLITE_LIMIT_VARIABLE_NUMBER: %d", limitVariableNumber) | |||
num := 400 | |||
query, args := createBulkInsertQuery(num, 0) | |||
err = bulkInsert(db, query, args) | |||
if err != nil { | |||
log.Fatal(err) | |||
} | |||
smallLimitVariableNumber := 100 | |||
sqlite3conn.SetLimit(sqlite3.SQLITE_LIMIT_VARIABLE_NUMBER, smallLimitVariableNumber) | |||
limitVariableNumber = sqlite3conn.GetLimit(sqlite3.SQLITE_LIMIT_VARIABLE_NUMBER) | |||
log.Printf("updated SQLITE_LIMIT_VARIABLE_NUMBER: %d", limitVariableNumber) | |||
query, args = createBulkInsertQuery(num, num) | |||
err = bulkInsert(db, query, args) | |||
if err != nil { | |||
if err != nil { | |||
log.Printf("expect failed since SQLITE_LIMIT_VARIABLE_NUMBER is too small: %v", err) | |||
} | |||
} | |||
bigLimitVariableNumber := 999999 | |||
sqlite3conn.SetLimit(sqlite3.SQLITE_LIMIT_VARIABLE_NUMBER, bigLimitVariableNumber) | |||
limitVariableNumber = sqlite3conn.GetLimit(sqlite3.SQLITE_LIMIT_VARIABLE_NUMBER) | |||
log.Printf("set SQLITE_LIMIT_VARIABLE_NUMBER: %d", bigLimitVariableNumber) | |||
log.Printf("updated SQLITE_LIMIT_VARIABLE_NUMBER: %d", limitVariableNumber) | |||
query, args = createBulkInsertQuery(500, num+num) | |||
err = bulkInsert(db, query, args) | |||
if err != nil { | |||
if err != nil { | |||
log.Fatal(err) | |||
} | |||
} | |||
log.Println("no error if SQLITE_LIMIT_VARIABLE_NUMBER > 999") | |||
} |
@ -0,0 +1,27 @@ | |||
ifeq ($(OS),Windows_NT) | |||
EXE=extension.exe | |||
LIB_EXT=dll | |||
RM=cmd /c del | |||
LDFLAG= | |||
else | |||
EXE=extension | |||
ifeq ($(shell uname -s),Darwin) | |||
LIB_EXT=dylib | |||
else | |||
LIB_EXT=so | |||
endif | |||
RM=rm -f | |||
LDFLAG=-fPIC | |||
endif | |||
LIB=sqlite3_mod_regexp.$(LIB_EXT) | |||
all : $(EXE) $(LIB) | |||
$(EXE) : extension.go | |||
go build $< | |||
$(LIB) : sqlite3_mod_regexp.c | |||
gcc $(LDFLAG) -shared -o $@ $< -lsqlite3 -lpcre | |||
clean : | |||
@-$(RM) $(EXE) $(LIB) |
@ -0,0 +1,43 @@ | |||
package main | |||
import ( | |||
"database/sql" | |||
"fmt" | |||
"github.com/mattn/go-sqlite3" | |||
"log" | |||
) | |||
func main() { | |||
sql.Register("sqlite3_with_extensions", | |||
&sqlite3.SQLiteDriver{ | |||
Extensions: []string{ | |||
"sqlite3_mod_regexp", | |||
}, | |||
}) | |||
db, err := sql.Open("sqlite3_with_extensions", ":memory:") | |||
if err != nil { | |||
log.Fatal(err) | |||
} | |||
defer db.Close() | |||
// Force db to make a new connection in pool | |||
// by putting the original in a transaction | |||
tx, err := db.Begin() | |||
if err != nil { | |||
log.Fatal(err) | |||
} | |||
defer tx.Commit() | |||
// New connection works (hopefully!) | |||
rows, err := db.Query("select 'hello world' where 'hello world' regexp '^hello.*d$'") | |||
if err != nil { | |||
log.Fatal(err) | |||
} | |||
defer rows.Close() | |||
for rows.Next() { | |||
var helloworld string | |||
rows.Scan(&helloworld) | |||
fmt.Println(helloworld) | |||
} | |||
} |
@ -0,0 +1,35 @@ | |||
#include <pcre.h> | |||
#include <string.h> | |||
#include <stdio.h> | |||
#include <sqlite3ext.h> | |||
SQLITE_EXTENSION_INIT1 | |||
static void regexp_func(sqlite3_context *context, int argc, sqlite3_value **argv) { | |||
if (argc >= 2) { | |||
const char *target = (const char *)sqlite3_value_text(argv[1]); | |||
const char *pattern = (const char *)sqlite3_value_text(argv[0]); | |||
const char* errstr = NULL; | |||
int erroff = 0; | |||
int vec[500]; | |||
int n, rc; | |||
pcre* re = pcre_compile(pattern, 0, &errstr, &erroff, NULL); | |||
if (!re) { | |||
sqlite3_result_error(context, errstr, 0); | |||
return; | |||
} | |||
rc = pcre_exec(re, NULL, target, strlen(target), 0, 0, vec, 500); | |||
if (rc <= 0) { | |||
sqlite3_result_int(context, 0); | |||
return; | |||
} | |||
sqlite3_result_int(context, 1); | |||
} | |||
} | |||
#ifdef _WIN32 | |||
__declspec(dllexport) | |||
#endif | |||
int sqlite3_extension_init(sqlite3 *db, char **errmsg, const sqlite3_api_routines *api) { | |||
SQLITE_EXTENSION_INIT2(api); | |||
return sqlite3_create_function(db, "regexp", 2, SQLITE_UTF8, (void*)db, regexp_func, NULL, NULL); | |||
} |
@ -0,0 +1,29 @@ | |||
ifeq ($(OS),Windows_NT) | |||
EXE=extension.exe | |||
LIB_EXT=dll | |||
RM=cmd /c del | |||
LIBCURL=-lcurldll | |||
LDFLAG= | |||
else | |||
EXE=extension | |||
ifeq ($(shell uname -s),Darwin) | |||
LIB_EXT=dylib | |||
else | |||
LIB_EXT=so | |||
endif | |||
RM=rm -f | |||
LDFLAG=-fPIC | |||
LIBCURL=-lcurl | |||
endif | |||
LIB=sqlite3_mod_vtable.$(LIB_EXT) | |||
all : $(EXE) $(LIB) | |||
$(EXE) : extension.go | |||
go build $< | |||
$(LIB) : sqlite3_mod_vtable.cc | |||
g++ $(LDFLAG) -shared -o $@ $< -lsqlite3 $(LIBCURL) | |||
clean : | |||
@-$(RM) $(EXE) $(LIB) |
@ -0,0 +1,37 @@ | |||
package main | |||
import ( | |||
"database/sql" | |||
"fmt" | |||
"log" | |||
"github.com/mattn/go-sqlite3" | |||
) | |||
func main() { | |||
sql.Register("sqlite3_with_extensions", | |||
&sqlite3.SQLiteDriver{ | |||
Extensions: []string{ | |||
"sqlite3_mod_vtable", | |||
}, | |||
}) | |||
db, err := sql.Open("sqlite3_with_extensions", ":memory:") | |||
if err != nil { | |||
log.Fatal(err) | |||
} | |||
defer db.Close() | |||
db.Exec("create virtual table repo using github(id, full_name, description, html_url)") | |||
rows, err := db.Query("select id, full_name, description, html_url from repo") | |||
if err != nil { | |||
log.Fatal(err) | |||
} | |||
defer rows.Close() | |||
for rows.Next() { | |||
var id, fullName, description, htmlURL string | |||
rows.Scan(&id, &fullName, &description, &htmlURL) | |||
fmt.Printf("%s: %s\n\t%s\n\t%s\n\n", id, fullName, description, htmlURL) | |||
} | |||
} |
@ -0,0 +1,238 @@ | |||
#include <string> | |||
#include <sstream> | |||
#include <sqlite3.h> | |||
#include <sqlite3ext.h> | |||
#include <curl/curl.h> | |||
#include "picojson.h" | |||
#ifdef _WIN32 | |||
# define EXPORT __declspec(dllexport) | |||
#else | |||
# define EXPORT | |||
#endif | |||
SQLITE_EXTENSION_INIT1; | |||
typedef struct { | |||
char* data; // response data from server | |||
size_t size; // response size of data | |||
} MEMFILE; | |||
MEMFILE* | |||
memfopen() { | |||
MEMFILE* mf = (MEMFILE*) malloc(sizeof(MEMFILE)); | |||
if (mf) { | |||
mf->data = NULL; | |||
mf->size = 0; | |||
} | |||
return mf; | |||
} | |||
void | |||
memfclose(MEMFILE* mf) { | |||
if (mf->data) free(mf->data); | |||
free(mf); | |||
} | |||
size_t | |||
memfwrite(char* ptr, size_t size, size_t nmemb, void* stream) { | |||
MEMFILE* mf = (MEMFILE*) stream; | |||
int block = size * nmemb; | |||
if (!mf) return block; // through | |||
if (!mf->data) | |||
mf->data = (char*) malloc(block); | |||
else | |||
mf->data = (char*) realloc(mf->data, mf->size + block); | |||
if (mf->data) { | |||
memcpy(mf->data + mf->size, ptr, block); | |||
mf->size += block; | |||
} | |||
return block; | |||
} | |||
char* | |||
memfstrdup(MEMFILE* mf) { | |||
char* buf; | |||
if (mf->size == 0) return NULL; | |||
buf = (char*) malloc(mf->size + 1); | |||
memcpy(buf, mf->data, mf->size); | |||
buf[mf->size] = 0; | |||
return buf; | |||
} | |||
static int | |||
my_connect(sqlite3 *db, void *pAux, int argc, const char * const *argv, sqlite3_vtab **ppVTab, char **c) { | |||
std::stringstream ss; | |||
ss << "CREATE TABLE " << argv[0] | |||
<< "(id int, full_name text, description text, html_url text)"; | |||
int rc = sqlite3_declare_vtab(db, ss.str().c_str()); | |||
*ppVTab = (sqlite3_vtab *) sqlite3_malloc(sizeof(sqlite3_vtab)); | |||
memset(*ppVTab, 0, sizeof(sqlite3_vtab)); | |||
return rc; | |||
} | |||
static int | |||
my_create(sqlite3 *db, void *pAux, int argc, const char * const * argv, sqlite3_vtab **ppVTab, char **c) { | |||
return my_connect(db, pAux, argc, argv, ppVTab, c); | |||
} | |||
static int my_disconnect(sqlite3_vtab *pVTab) { | |||
sqlite3_free(pVTab); | |||
return SQLITE_OK; | |||
} | |||
static int | |||
my_destroy(sqlite3_vtab *pVTab) { | |||
sqlite3_free(pVTab); | |||
return SQLITE_OK; | |||
} | |||
typedef struct { | |||
sqlite3_vtab_cursor base; | |||
int index; | |||
picojson::value* rows; | |||
} cursor; | |||
static int | |||
my_open(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor) { | |||
MEMFILE* mf; | |||
CURL* curl; | |||
char* json; | |||
CURLcode res = CURLE_OK; | |||
char error[CURL_ERROR_SIZE] = {0}; | |||
char* cert_file = getenv("SSL_CERT_FILE"); | |||
mf = memfopen(); | |||
curl = curl_easy_init(); | |||
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1); | |||
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2); | |||
curl_easy_setopt(curl, CURLOPT_USERAGENT, "curl/7.29.0"); | |||
curl_easy_setopt(curl, CURLOPT_URL, "https://api.github.com/repositories"); | |||
if (cert_file) | |||
curl_easy_setopt(curl, CURLOPT_CAINFO, cert_file); | |||
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); | |||
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error); | |||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, mf); | |||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, memfwrite); | |||
res = curl_easy_perform(curl); | |||
curl_easy_cleanup(curl); | |||
if (res != CURLE_OK) { | |||
std::cerr << error << std::endl; | |||
return SQLITE_FAIL; | |||
} | |||
picojson::value* v = new picojson::value; | |||
std::string err; | |||
picojson::parse(*v, mf->data, mf->data + mf->size, &err); | |||
memfclose(mf); | |||
if (!err.empty()) { | |||
delete v; | |||
std::cerr << err << std::endl; | |||
return SQLITE_FAIL; | |||
} | |||
cursor *c = (cursor *)sqlite3_malloc(sizeof(cursor)); | |||
c->rows = v; | |||
c->index = 0; | |||
*ppCursor = &c->base; | |||
return SQLITE_OK; | |||
} | |||
static int | |||
my_close(cursor *c) { | |||
delete c->rows; | |||
sqlite3_free(c); | |||
return SQLITE_OK; | |||
} | |||
static int | |||
my_filter(cursor *c, int idxNum, const char *idxStr, int argc, sqlite3_value **argv) { | |||
c->index = 0; | |||
return SQLITE_OK; | |||
} | |||
static int | |||
my_next(cursor *c) { | |||
c->index++; | |||
return SQLITE_OK; | |||
} | |||
static int | |||
my_eof(cursor *c) { | |||
return c->index >= c->rows->get<picojson::array>().size() ? 1 : 0; | |||
} | |||
static int | |||
my_column(cursor *c, sqlite3_context *ctxt, int i) { | |||
picojson::value v = c->rows->get<picojson::array>()[c->index]; | |||
picojson::object row = v.get<picojson::object>(); | |||
const char* p = NULL; | |||
switch (i) { | |||
case 0: | |||
p = row["id"].to_str().c_str(); | |||
break; | |||
case 1: | |||
p = row["full_name"].to_str().c_str(); | |||
break; | |||
case 2: | |||
p = row["description"].to_str().c_str(); | |||
break; | |||
case 3: | |||
p = row["html_url"].to_str().c_str(); | |||
break; | |||
} | |||
sqlite3_result_text(ctxt, strdup(p), strlen(p), free); | |||
return SQLITE_OK; | |||
} | |||
static int | |||
my_rowid(cursor *c, sqlite3_int64 *pRowid) { | |||
*pRowid = c->index; | |||
return SQLITE_OK; | |||
} | |||
static int | |||
my_bestindex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo) { | |||
return SQLITE_OK; | |||
} | |||
static const sqlite3_module module = { | |||
0, | |||
my_create, | |||
my_connect, | |||
my_bestindex, | |||
my_disconnect, | |||
my_destroy, | |||
my_open, | |||
(int (*)(sqlite3_vtab_cursor *)) my_close, | |||
(int (*)(sqlite3_vtab_cursor *, int, char const *, int, sqlite3_value **)) my_filter, | |||
(int (*)(sqlite3_vtab_cursor *)) my_next, | |||
(int (*)(sqlite3_vtab_cursor *)) my_eof, | |||
(int (*)(sqlite3_vtab_cursor *, sqlite3_context *, int)) my_column, | |||
(int (*)(sqlite3_vtab_cursor *, sqlite3_int64 *)) my_rowid, | |||
NULL, // my_update | |||
NULL, // my_begin | |||
NULL, // my_sync | |||
NULL, // my_commit | |||
NULL, // my_rollback | |||
NULL, // my_findfunction | |||
NULL, // my_rename | |||
}; | |||
static void | |||
destructor(void *arg) { | |||
return; | |||
} | |||
extern "C" { | |||
EXPORT int | |||
sqlite3_extension_init(sqlite3 *db, char **errmsg, const sqlite3_api_routines *api) { | |||
SQLITE_EXTENSION_INIT2(api); | |||
sqlite3_create_module_v2(db, "github", &module, NULL, destructor); | |||
return 0; | |||
} | |||
} |
@ -0,0 +1,45 @@ | |||
# ============================================================================= | |||
# Multi-stage Dockerfile Example | |||
# ============================================================================= | |||
# This is a simple Dockerfile that will build an image of scratch-base image. | |||
# Usage: | |||
# docker build -t simple:local . && docker run --rm simple:local | |||
# ============================================================================= | |||
# ----------------------------------------------------------------------------- | |||
# Build Stage | |||
# ----------------------------------------------------------------------------- | |||
FROM golang:alpine AS build | |||
# Important: | |||
# Because this is a CGO enabled package, you are required to set it as 1. | |||
ENV CGO_ENABLED=1 | |||
RUN apk add --no-cache \ | |||
# Important: required for go-sqlite3 | |||
gcc \ | |||
# Required for Alpine | |||
musl-dev | |||
WORKDIR /workspace | |||
COPY . /workspace/ | |||
RUN \ | |||
go mod init github.com/mattn/sample && \ | |||
go mod tidy && \ | |||
go install -ldflags='-s -w -extldflags "-static"' ./simple.go | |||
RUN \ | |||
# Smoke test | |||
set -o pipefail; \ | |||
/go/bin/simple | grep 99\ こんにちは世界099 | |||
# ----------------------------------------------------------------------------- | |||
# Main Stage | |||
# ----------------------------------------------------------------------------- | |||
FROM scratch | |||
COPY --from=build /go/bin/simple /usr/local/bin/simple | |||
ENTRYPOINT [ "/usr/local/bin/simple" ] |
@ -0,0 +1,109 @@ | |||
package main | |||
import ( | |||
"database/sql" | |||
"fmt" | |||
_ "github.com/mattn/go-sqlite3" | |||
"log" | |||
"os" | |||
) | |||
func main() { | |||
os.Remove("./foo.db") | |||
db, err := sql.Open("sqlite3", "./foo.db") | |||
if err != nil { | |||
log.Fatal(err) | |||
} | |||
defer db.Close() | |||
sqlStmt := ` | |||
create table foo (id integer not null primary key, name text); | |||
delete from foo; | |||
` | |||
_, err = db.Exec(sqlStmt) | |||
if err != nil { | |||
log.Printf("%q: %s\n", err, sqlStmt) | |||
return | |||
} | |||
tx, err := db.Begin() | |||
if err != nil { | |||
log.Fatal(err) | |||
} | |||
stmt, err := tx.Prepare("insert into foo(id, name) values(?, ?)") | |||
if err != nil { | |||
log.Fatal(err) | |||
} | |||
defer stmt.Close() | |||
for i := 0; i < 100; i++ { | |||
_, err = stmt.Exec(i, fmt.Sprintf("こんにちは世界%03d", i)) | |||
if err != nil { | |||
log.Fatal(err) | |||
} | |||
} | |||
err = tx.Commit() | |||
if err != nil { | |||
log.Fatal(err) | |||
} | |||
rows, err := db.Query("select id, name from foo") | |||
if err != nil { | |||
log.Fatal(err) | |||
} | |||
defer rows.Close() | |||
for rows.Next() { | |||
var id int | |||
var name string | |||
err = rows.Scan(&id, &name) | |||
if err != nil { | |||
log.Fatal(err) | |||
} | |||
fmt.Println(id, name) | |||
} | |||
err = rows.Err() | |||
if err != nil { | |||
log.Fatal(err) | |||
} | |||
stmt, err = db.Prepare("select name from foo where id = ?") | |||
if err != nil { | |||
log.Fatal(err) | |||
} | |||
defer stmt.Close() | |||
var name string | |||
err = stmt.QueryRow("3").Scan(&name) | |||
if err != nil { | |||
log.Fatal(err) | |||
} | |||
fmt.Println(name) | |||
_, err = db.Exec("delete from foo") | |||
if err != nil { | |||
log.Fatal(err) | |||
} | |||
_, err = db.Exec("insert into foo(id, name) values(1, 'foo'), (2, 'bar'), (3, 'baz')") | |||
if err != nil { | |||
log.Fatal(err) | |||
} | |||
rows, err = db.Query("select id, name from foo") | |||
if err != nil { | |||
log.Fatal(err) | |||
} | |||
defer rows.Close() | |||
for rows.Next() { | |||
var id int | |||
var name string | |||
err = rows.Scan(&id, &name) | |||
if err != nil { | |||
log.Fatal(err) | |||
} | |||
fmt.Println(id, name) | |||
} | |||
err = rows.Err() | |||
if err != nil { | |||
log.Fatal(err) | |||
} | |||
} |
@ -0,0 +1,264 @@ | |||
package main | |||
import ( | |||
"database/sql" | |||
"fmt" | |||
"log" | |||
"os" | |||
sqlite3 "github.com/mattn/go-sqlite3" | |||
) | |||
func traceCallback(info sqlite3.TraceInfo) int { | |||
// Not very readable but may be useful; uncomment next line in case of doubt: | |||
//fmt.Printf("Trace: %#v\n", info) | |||
var dbErrText string | |||
if info.DBError.Code != 0 || info.DBError.ExtendedCode != 0 { | |||
dbErrText = fmt.Sprintf("; DB error: %#v", info.DBError) | |||
} else { | |||
dbErrText = "." | |||
} | |||
// Show the Statement-or-Trigger text in curly braces ('{', '}') | |||
// since from the *paired* ASCII characters they are | |||
// the least used in SQL syntax, therefore better visual delimiters. | |||
// Maybe show 'ExpandedSQL' the same way as 'StmtOrTrigger'. | |||
// | |||
// A known use of curly braces (outside strings) is | |||
// for ODBC escape sequences. Not likely to appear here. | |||
// | |||
// Template languages, etc. don't matter, we should see their *result* | |||
// at *this* level. | |||
// Strange curly braces in SQL code that reached the database driver | |||
// suggest that there is a bug in the application. | |||
// The braces are likely to be either template syntax or | |||
// a programming language's string interpolation syntax. | |||
var expandedText string | |||
if info.ExpandedSQL != "" { | |||
if info.ExpandedSQL == info.StmtOrTrigger { | |||
expandedText = " = exp" | |||
} else { | |||
expandedText = fmt.Sprintf(" expanded {%q}", info.ExpandedSQL) | |||
} | |||
} else { | |||
expandedText = "" | |||
} | |||
// SQLite docs as of September 6, 2016: Tracing and Profiling Functions | |||
// https://www.sqlite.org/c3ref/profile.html | |||
// | |||
// The profile callback time is in units of nanoseconds, however | |||
// the current implementation is only capable of millisecond resolution | |||
// so the six least significant digits in the time are meaningless. | |||
// Future versions of SQLite might provide greater resolution on the profiler callback. | |||
var runTimeText string | |||
if info.RunTimeNanosec == 0 { | |||
if info.EventCode == sqlite3.TraceProfile { | |||
//runTimeText = "; no time" // seems confusing | |||
runTimeText = "; time 0" // no measurement unit | |||
} else { | |||
//runTimeText = "; no time" // seems useless and confusing | |||
} | |||
} else { | |||
const nanosPerMillisec = 1000000 | |||
if info.RunTimeNanosec%nanosPerMillisec == 0 { | |||
runTimeText = fmt.Sprintf("; time %d ms", info.RunTimeNanosec/nanosPerMillisec) | |||
} else { | |||
// unexpected: better than millisecond resolution | |||
runTimeText = fmt.Sprintf("; time %d ns!!!", info.RunTimeNanosec) | |||
} | |||
} | |||
var modeText string | |||
if info.AutoCommit { | |||
modeText = "-AC-" | |||
} else { | |||
modeText = "+Tx+" | |||
} | |||
fmt.Printf("Trace: ev %d %s conn 0x%x, stmt 0x%x {%q}%s%s%s\n", | |||
info.EventCode, modeText, info.ConnHandle, info.StmtHandle, | |||
info.StmtOrTrigger, expandedText, | |||
runTimeText, | |||
dbErrText) | |||
return 0 | |||
} | |||
func main() { | |||
eventMask := sqlite3.TraceStmt | sqlite3.TraceProfile | sqlite3.TraceRow | sqlite3.TraceClose | |||
sql.Register("sqlite3_tracing", | |||
&sqlite3.SQLiteDriver{ | |||
ConnectHook: func(conn *sqlite3.SQLiteConn) error { | |||
err := conn.SetTrace(&sqlite3.TraceConfig{ | |||
Callback: traceCallback, | |||
EventMask: eventMask, | |||
WantExpandedSQL: true, | |||
}) | |||
return err | |||
}, | |||
}) | |||
os.Exit(dbMain()) | |||
} | |||
// Harder to do DB work in main(). | |||
// It's better with a separate function because | |||
// 'defer' and 'os.Exit' don't go well together. | |||
// | |||
// DO NOT use 'log.Fatal...' below: remember that it's equivalent to | |||
// Print() followed by a call to os.Exit(1) --- and | |||
// we want to avoid Exit() so 'defer' can do cleanup. | |||
// Use 'log.Panic...' instead. | |||
func dbMain() int { | |||
db, err := sql.Open("sqlite3_tracing", ":memory:") | |||
if err != nil { | |||
fmt.Printf("Failed to open database: %#+v\n", err) | |||
return 1 | |||
} | |||
defer db.Close() | |||
err = db.Ping() | |||
if err != nil { | |||
log.Panic(err) | |||
} | |||
dbSetup(db) | |||
dbDoInsert(db) | |||
dbDoInsertPrepared(db) | |||
dbDoSelect(db) | |||
dbDoSelectPrepared(db) | |||
return 0 | |||
} | |||
// 'DDL' stands for "Data Definition Language": | |||
// Note: "INTEGER PRIMARY KEY NOT NULL AUTOINCREMENT" causes the error | |||
// 'near "AUTOINCREMENT": syntax error'; without "NOT NULL" it works. | |||
const tableDDL = `CREATE TABLE t1 ( | |||
id INTEGER PRIMARY KEY AUTOINCREMENT, | |||
note VARCHAR NOT NULL | |||
)` | |||
// 'DML' stands for "Data Manipulation Language": | |||
const insertDML = "INSERT INTO t1 (note) VALUES (?)" | |||
const selectDML = "SELECT id, note FROM t1 WHERE note LIKE ?" | |||
const textPrefix = "bla-1234567890-" | |||
const noteTextPattern = "%Prep%" | |||
const nGenRows = 4 // Number of Rows to Generate (for *each* approach tested) | |||
func dbSetup(db *sql.DB) { | |||
var err error | |||
_, err = db.Exec("DROP TABLE IF EXISTS t1") | |||
if err != nil { | |||
log.Panic(err) | |||
} | |||
_, err = db.Exec(tableDDL) | |||
if err != nil { | |||
log.Panic(err) | |||
} | |||
} | |||
func dbDoInsert(db *sql.DB) { | |||
const Descr = "DB-Exec" | |||
for i := 0; i < nGenRows; i++ { | |||
result, err := db.Exec(insertDML, textPrefix+Descr) | |||
if err != nil { | |||
log.Panic(err) | |||
} | |||
resultDoCheck(result, Descr, i) | |||
} | |||
} | |||
func dbDoInsertPrepared(db *sql.DB) { | |||
const Descr = "DB-Prepare" | |||
stmt, err := db.Prepare(insertDML) | |||
if err != nil { | |||
log.Panic(err) | |||
} | |||
defer stmt.Close() | |||
for i := 0; i < nGenRows; i++ { | |||
result, err := stmt.Exec(textPrefix + Descr) | |||
if err != nil { | |||
log.Panic(err) | |||
} | |||
resultDoCheck(result, Descr, i) | |||
} | |||
} | |||
func resultDoCheck(result sql.Result, callerDescr string, callIndex int) { | |||
lastID, err := result.LastInsertId() | |||
if err != nil { | |||
log.Panic(err) | |||
} | |||
nAffected, err := result.RowsAffected() | |||
if err != nil { | |||
log.Panic(err) | |||
} | |||
log.Printf("Exec result for %s (%d): ID = %d, affected = %d\n", callerDescr, callIndex, lastID, nAffected) | |||
} | |||
func dbDoSelect(db *sql.DB) { | |||
const Descr = "DB-Query" | |||
rows, err := db.Query(selectDML, noteTextPattern) | |||
if err != nil { | |||
log.Panic(err) | |||
} | |||
defer rows.Close() | |||
rowsDoFetch(rows, Descr) | |||
} | |||
func dbDoSelectPrepared(db *sql.DB) { | |||
const Descr = "DB-Prepare" | |||
stmt, err := db.Prepare(selectDML) | |||
if err != nil { | |||
log.Panic(err) | |||
} | |||
defer stmt.Close() | |||
rows, err := stmt.Query(noteTextPattern) | |||
if err != nil { | |||
log.Panic(err) | |||
} | |||
defer rows.Close() | |||
rowsDoFetch(rows, Descr) | |||
} | |||
func rowsDoFetch(rows *sql.Rows, callerDescr string) { | |||
var nRows int | |||
var id int64 | |||
var note string | |||
for rows.Next() { | |||
err := rows.Scan(&id, ¬e) | |||
if err != nil { | |||
log.Panic(err) | |||
} | |||
log.Printf("Row for %s (%d): id=%d, note=%q\n", | |||
callerDescr, nRows, id, note) | |||
nRows++ | |||
} | |||
if err := rows.Err(); err != nil { | |||
log.Panic(err) | |||
} | |||
log.Printf("Total %d rows for %s.\n", nRows, callerDescr) | |||
} |
@ -0,0 +1,38 @@ | |||
package main | |||
import ( | |||
"database/sql" | |||
"fmt" | |||
"log" | |||
"github.com/mattn/go-sqlite3" | |||
) | |||
func main() { | |||
sql.Register("sqlite3_with_extensions", &sqlite3.SQLiteDriver{ | |||
ConnectHook: func(conn *sqlite3.SQLiteConn) error { | |||
return conn.CreateModule("github", &githubModule{}) | |||
}, | |||
}) | |||
db, err := sql.Open("sqlite3_with_extensions", ":memory:") | |||
if err != nil { | |||
log.Fatal(err) | |||
} | |||
defer db.Close() | |||
_, err = db.Exec("create virtual table repo using github(id, full_name, description, html_url)") | |||
if err != nil { | |||
log.Fatal(err) | |||
} | |||
rows, err := db.Query("select id, full_name, description, html_url from repo") | |||
if err != nil { | |||
log.Fatal(err) | |||
} | |||
defer rows.Close() | |||
for rows.Next() { | |||
var id, fullName, description, htmlURL string | |||
rows.Scan(&id, &fullName, &description, &htmlURL) | |||
fmt.Printf("%s: %s\n\t%s\n\t%s\n\n", id, fullName, description, htmlURL) | |||
} | |||
} |
@ -0,0 +1,116 @@ | |||
package main | |||
import ( | |||
"encoding/json" | |||
"fmt" | |||
"io/ioutil" | |||
"net/http" | |||
"github.com/mattn/go-sqlite3" | |||
) | |||
type githubRepo struct { | |||
ID int `json:"id"` | |||
FullName string `json:"full_name"` | |||
Description string `json:"description"` | |||
HTMLURL string `json:"html_url"` | |||
} | |||
type githubModule struct { | |||
} | |||
func (m *githubModule) Create(c *sqlite3.SQLiteConn, args []string) (sqlite3.VTab, error) { | |||
err := c.DeclareVTab(fmt.Sprintf(` | |||
CREATE TABLE %s ( | |||
id INT, | |||
full_name TEXT, | |||
description TEXT, | |||
html_url TEXT | |||
)`, args[0])) | |||
if err != nil { | |||
return nil, err | |||
} | |||
return &ghRepoTable{}, nil | |||
} | |||
func (m *githubModule) Connect(c *sqlite3.SQLiteConn, args []string) (sqlite3.VTab, error) { | |||
return m.Create(c, args) | |||
} | |||
func (m *githubModule) DestroyModule() {} | |||
type ghRepoTable struct { | |||
repos []githubRepo | |||
} | |||
func (v *ghRepoTable) Open() (sqlite3.VTabCursor, error) { | |||
resp, err := http.Get("https://api.github.com/repositories") | |||
if err != nil { | |||
return nil, err | |||
} | |||
defer resp.Body.Close() | |||
body, err := ioutil.ReadAll(resp.Body) | |||
if err != nil { | |||
return nil, err | |||
} | |||
var repos []githubRepo | |||
if err := json.Unmarshal(body, &repos); err != nil { | |||
return nil, err | |||
} | |||
return &ghRepoCursor{0, repos}, nil | |||
} | |||
func (v *ghRepoTable) BestIndex(cst []sqlite3.InfoConstraint, ob []sqlite3.InfoOrderBy) (*sqlite3.IndexResult, error) { | |||
used := make([]bool, len(csts)) | |||
return &sqlite3.IndexResult{ | |||
IdxNum: 0, | |||
IdxStr: "default", | |||
Used: used, | |||
}, nil | |||
} | |||
func (v *ghRepoTable) Disconnect() error { return nil } | |||
func (v *ghRepoTable) Destroy() error { return nil } | |||
type ghRepoCursor struct { | |||
index int | |||
repos []githubRepo | |||
} | |||
func (vc *ghRepoCursor) Column(c *sqlite3.SQLiteContext, col int) error { | |||
switch col { | |||
case 0: | |||
c.ResultInt(vc.repos[vc.index].ID) | |||
case 1: | |||
c.ResultText(vc.repos[vc.index].FullName) | |||
case 2: | |||
c.ResultText(vc.repos[vc.index].Description) | |||
case 3: | |||
c.ResultText(vc.repos[vc.index].HTMLURL) | |||
} | |||
return nil | |||
} | |||
func (vc *ghRepoCursor) Filter(idxNum int, idxStr string, vals []interface{}) error { | |||
vc.index = 0 | |||
return nil | |||
} | |||
func (vc *ghRepoCursor) Next() error { | |||
vc.index++ | |||
return nil | |||
} | |||
func (vc *ghRepoCursor) EOF() bool { | |||
return vc.index >= len(vc.repos) | |||
} | |||
func (vc *ghRepoCursor) Rowid() (int64, error) { | |||
return int64(vc.index), nil | |||
} | |||
func (vc *ghRepoCursor) Close() error { | |||
return nil | |||
} |
@ -0,0 +1,33 @@ | |||
package main | |||
import ( | |||
"database/sql" | |||
"fmt" | |||
"log" | |||
"github.com/mattn/go-sqlite3" | |||
) | |||
func main() { | |||
sql.Register("sqlite3_with_extensions", &sqlite3.SQLiteDriver{ | |||
ConnectHook: func(conn *sqlite3.SQLiteConn) error { | |||
return conn.CreateModule("series", &seriesModule{}) | |||
}, | |||
}) | |||
db, err := sql.Open("sqlite3_with_extensions", ":memory:") | |||
if err != nil { | |||
log.Fatal(err) | |||
} | |||
defer db.Close() | |||
rows, err := db.Query("select * from series") | |||
if err != nil { | |||
log.Fatal(err) | |||
} | |||
defer rows.Close() | |||
for rows.Next() { | |||
var value int | |||
rows.Scan(&value) | |||
fmt.Printf("value: %d\n", value) | |||
} | |||
} |
@ -0,0 +1,118 @@ | |||
package main | |||
import ( | |||
"fmt" | |||
"github.com/mattn/go-sqlite3" | |||
) | |||
type seriesModule struct{} | |||
func (m *seriesModule) EponymousOnlyModule() {} | |||
func (m *seriesModule) Create(c *sqlite3.SQLiteConn, args []string) (sqlite3.VTab, error) { | |||
err := c.DeclareVTab(fmt.Sprintf(` | |||
CREATE TABLE %s ( | |||
value INT, | |||
start HIDDEN, | |||
stop HIDDEN, | |||
step HIDDEN | |||
)`, args[0])) | |||
if err != nil { | |||
return nil, err | |||
} | |||
return &seriesTable{0, 0, 1}, nil | |||
} | |||
func (m *seriesModule) Connect(c *sqlite3.SQLiteConn, args []string) (sqlite3.VTab, error) { | |||
return m.Create(c, args) | |||
} | |||
func (m *seriesModule) DestroyModule() {} | |||
type seriesTable struct { | |||
start int64 | |||
stop int64 | |||
step int64 | |||
} | |||
func (v *seriesTable) Open() (sqlite3.VTabCursor, error) { | |||
return &seriesCursor{v, 0}, nil | |||
} | |||
func (v *seriesTable) BestIndex(csts []sqlite3.InfoConstraint, ob []sqlite3.InfoOrderBy) (*sqlite3.IndexResult, error) { | |||
used := make([]bool, len(csts)) | |||
for c, cst := range csts { | |||
if cst.Usable && cst.Op == sqlite3.OpEQ { | |||
used[c] = true | |||
} | |||
} | |||
return &sqlite3.IndexResult{ | |||
IdxNum: 0, | |||
IdxStr: "default", | |||
Used: used, | |||
}, nil | |||
} | |||
func (v *seriesTable) Disconnect() error { return nil } | |||
func (v *seriesTable) Destroy() error { return nil } | |||
type seriesCursor struct { | |||
*seriesTable | |||
value int64 | |||
} | |||
func (vc *seriesCursor) Column(c *sqlite3.SQLiteContext, col int) error { | |||
switch col { | |||
case 0: | |||
c.ResultInt64(vc.value) | |||
case 1: | |||
c.ResultInt64(vc.seriesTable.start) | |||
case 2: | |||
c.ResultInt64(vc.seriesTable.stop) | |||
case 3: | |||
c.ResultInt64(vc.seriesTable.step) | |||
} | |||
return nil | |||
} | |||
func (vc *seriesCursor) Filter(idxNum int, idxStr string, vals []interface{}) error { | |||
switch { | |||
case len(vals) < 1: | |||
vc.seriesTable.start = 0 | |||
vc.seriesTable.stop = 1000 | |||
vc.value = vc.seriesTable.start | |||
case len(vals) < 2: | |||
vc.seriesTable.start = vals[0].(int64) | |||
vc.seriesTable.stop = 1000 | |||
vc.value = vc.seriesTable.start | |||
case len(vals) < 3: | |||
vc.seriesTable.start = vals[0].(int64) | |||
vc.seriesTable.stop = vals[1].(int64) | |||
vc.value = vc.seriesTable.start | |||
case len(vals) < 4: | |||
vc.seriesTable.start = vals[0].(int64) | |||
vc.seriesTable.stop = vals[1].(int64) | |||
vc.seriesTable.step = vals[2].(int64) | |||
} | |||
return nil | |||
} | |||
func (vc *seriesCursor) Next() error { | |||
vc.value += vc.step | |||
return nil | |||
} | |||
func (vc *seriesCursor) EOF() bool { | |||
return vc.value > vc.stop | |||
} | |||
func (vc *seriesCursor) Rowid() (int64, error) { | |||
return int64(vc.value), nil | |||
} | |||
func (vc *seriesCursor) Close() error { | |||
return nil | |||
} |
@ -0,0 +1,85 @@ | |||
// 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. | |||
package sqlite3 | |||
/* | |||
#ifndef USE_LIBSQLITE3 | |||
#include "sqlite3-binding.h" | |||
#else | |||
#include <sqlite3.h> | |||
#endif | |||
#include <stdlib.h> | |||
*/ | |||
import "C" | |||
import ( | |||
"runtime" | |||
"unsafe" | |||
) | |||
// SQLiteBackup implement interface of Backup. | |||
type SQLiteBackup struct { | |||
b *C.sqlite3_backup | |||
} | |||
// Backup make backup from src to dest. | |||
func (destConn *SQLiteConn) Backup(dest string, srcConn *SQLiteConn, src string) (*SQLiteBackup, error) { | |||
destptr := C.CString(dest) | |||
defer C.free(unsafe.Pointer(destptr)) | |||
srcptr := C.CString(src) | |||
defer C.free(unsafe.Pointer(srcptr)) | |||
if b := C.sqlite3_backup_init(destConn.db, destptr, srcConn.db, srcptr); b != nil { | |||
bb := &SQLiteBackup{b: b} | |||
runtime.SetFinalizer(bb, (*SQLiteBackup).Finish) | |||
return bb, nil | |||
} | |||
return nil, destConn.lastError() | |||
} | |||
// Step to backs up for one step. Calls the underlying `sqlite3_backup_step` | |||
// function. This function returns a boolean indicating if the backup is done | |||
// and an error signalling any other error. Done is returned if the underlying | |||
// C function returns SQLITE_DONE (Code 101) | |||
func (b *SQLiteBackup) Step(p int) (bool, error) { | |||
ret := C.sqlite3_backup_step(b.b, C.int(p)) | |||
if ret == C.SQLITE_DONE { | |||
return true, nil | |||
} else if ret != 0 && ret != C.SQLITE_LOCKED && ret != C.SQLITE_BUSY { | |||
return false, Error{Code: ErrNo(ret)} | |||
} | |||
return false, nil | |||
} | |||
// Remaining return whether have the rest for backup. | |||
func (b *SQLiteBackup) Remaining() int { | |||
return int(C.sqlite3_backup_remaining(b.b)) | |||
} | |||
// PageCount return count of pages. | |||
func (b *SQLiteBackup) PageCount() int { | |||
return int(C.sqlite3_backup_pagecount(b.b)) | |||
} | |||
// Finish close backup. | |||
func (b *SQLiteBackup) Finish() error { | |||
return b.Close() | |||
} | |||
// Close close backup. | |||
func (b *SQLiteBackup) Close() error { | |||
ret := C.sqlite3_backup_finish(b.b) | |||
// sqlite3_backup_finish() never fails, it just returns the | |||
// error code from previous operations, so clean up before | |||
// checking and returning an error | |||
b.b = nil | |||
runtime.SetFinalizer(b, nil) | |||
if ret != 0 { | |||
return Error{Code: ErrNo(ret)} | |||
} | |||
return nil | |||
} |
@ -0,0 +1,294 @@ | |||
// 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 cgo | |||
package sqlite3 | |||
import ( | |||
"database/sql" | |||
"fmt" | |||
"os" | |||
"testing" | |||
"time" | |||
) | |||
// The number of rows of test data to create in the source database. | |||
// Can be used to control how many pages are available to be backed up. | |||
const testRowCount = 100 | |||
// The maximum number of seconds after which the page-by-page backup is considered to have taken too long. | |||
const usePagePerStepsTimeoutSeconds = 30 | |||
// Test the backup functionality. | |||
func testBackup(t *testing.T, testRowCount int, usePerPageSteps bool) { | |||
// This function will be called multiple times. | |||
// It uses sql.Register(), which requires the name parameter value to be unique. | |||
// There does not currently appear to be a way to unregister a registered driver, however. | |||
// So generate a database driver name that will likely be unique. | |||
var driverName = fmt.Sprintf("sqlite3_testBackup_%v_%v_%v", testRowCount, usePerPageSteps, time.Now().UnixNano()) | |||
// The driver's connection will be needed in order to perform the backup. | |||
driverConns := []*SQLiteConn{} | |||
sql.Register(driverName, &SQLiteDriver{ | |||
ConnectHook: func(conn *SQLiteConn) error { | |||
driverConns = append(driverConns, conn) | |||
return nil | |||
}, | |||
}) | |||
// Connect to the source database. | |||
srcTempFilename := TempFilename(t) | |||
defer os.Remove(srcTempFilename) | |||
srcDb, err := sql.Open(driverName, srcTempFilename) | |||
if err != nil { | |||
t.Fatal("Failed to open the source database:", err) | |||
} | |||
defer srcDb.Close() | |||
err = srcDb.Ping() | |||
if err != nil { | |||
t.Fatal("Failed to connect to the source database:", err) | |||
} | |||
// Connect to the destination database. | |||
destTempFilename := TempFilename(t) | |||
defer os.Remove(destTempFilename) | |||
destDb, err := sql.Open(driverName, destTempFilename) | |||
if err != nil { | |||
t.Fatal("Failed to open the destination database:", err) | |||
} | |||
defer destDb.Close() | |||
err = destDb.Ping() | |||
if err != nil { | |||
t.Fatal("Failed to connect to the destination database:", err) | |||
} | |||
// Check the driver connections. | |||
if len(driverConns) != 2 { | |||
t.Fatalf("Expected 2 driver connections, but found %v.", len(driverConns)) | |||
} | |||
srcDbDriverConn := driverConns[0] | |||
if srcDbDriverConn == nil { | |||
t.Fatal("The source database driver connection is nil.") | |||
} | |||
destDbDriverConn := driverConns[1] | |||
if destDbDriverConn == nil { | |||
t.Fatal("The destination database driver connection is nil.") | |||
} | |||
// Generate some test data for the given ID. | |||
var generateTestData = func(id int) string { | |||
return fmt.Sprintf("test-%v", id) | |||
} | |||
// Populate the source database with a test table containing some test data. | |||
tx, err := srcDb.Begin() | |||
if err != nil { | |||
t.Fatal("Failed to begin a transaction when populating the source database:", err) | |||
} | |||
_, err = srcDb.Exec("CREATE TABLE test (id INTEGER PRIMARY KEY, value TEXT)") | |||
if err != nil { | |||
tx.Rollback() | |||
t.Fatal("Failed to create the source database \"test\" table:", err) | |||
} | |||
for id := 0; id < testRowCount; id++ { | |||
_, err = srcDb.Exec("INSERT INTO test (id, value) VALUES (?, ?)", id, generateTestData(id)) | |||
if err != nil { | |||
tx.Rollback() | |||
t.Fatal("Failed to insert a row into the source database \"test\" table:", err) | |||
} | |||
} | |||
err = tx.Commit() | |||
if err != nil { | |||
t.Fatal("Failed to populate the source database:", err) | |||
} | |||
// Confirm that the destination database is initially empty. | |||
var destTableCount int | |||
err = destDb.QueryRow("SELECT COUNT(*) FROM sqlite_master WHERE type = 'table'").Scan(&destTableCount) | |||
if err != nil { | |||
t.Fatal("Failed to check the destination table count:", err) | |||
} | |||
if destTableCount != 0 { | |||
t.Fatalf("The destination database is not empty; %v table(s) found.", destTableCount) | |||
} | |||
// Prepare to perform the backup. | |||
backup, err := destDbDriverConn.Backup("main", srcDbDriverConn, "main") | |||
if err != nil { | |||
t.Fatal("Failed to initialize the backup:", err) | |||
} | |||
// Allow the initial page count and remaining values to be retrieved. | |||
// According to <https://www.sqlite.org/c3ref/backup_finish.html>, the page count and remaining values are "... only updated by sqlite3_backup_step()." | |||
isDone, err := backup.Step(0) | |||
if err != nil { | |||
t.Fatal("Unable to perform an initial 0-page backup step:", err) | |||
} | |||
if isDone { | |||
t.Fatal("Backup is unexpectedly done.") | |||
} | |||
// Check that the page count and remaining values are reasonable. | |||
initialPageCount := backup.PageCount() | |||
if initialPageCount <= 0 { | |||
t.Fatalf("Unexpected initial page count value: %v", initialPageCount) | |||
} | |||
initialRemaining := backup.Remaining() | |||
if initialRemaining <= 0 { | |||
t.Fatalf("Unexpected initial remaining value: %v", initialRemaining) | |||
} | |||
if initialRemaining != initialPageCount { | |||
t.Fatalf("Initial remaining value differs from the initial page count value; remaining: %v; page count: %v", initialRemaining, initialPageCount) | |||
} | |||
// Perform the backup. | |||
if usePerPageSteps { | |||
var startTime = time.Now().Unix() | |||
// Test backing-up using a page-by-page approach. | |||
var latestRemaining = initialRemaining | |||
for { | |||
// Perform the backup step. | |||
isDone, err = backup.Step(1) | |||
if err != nil { | |||
t.Fatal("Failed to perform a backup step:", err) | |||
} | |||
// The page count should remain unchanged from its initial value. | |||
currentPageCount := backup.PageCount() | |||
if currentPageCount != initialPageCount { | |||
t.Fatalf("Current page count differs from the initial page count; initial page count: %v; current page count: %v", initialPageCount, currentPageCount) | |||
} | |||
// There should now be one less page remaining. | |||
currentRemaining := backup.Remaining() | |||
expectedRemaining := latestRemaining - 1 | |||
if currentRemaining != expectedRemaining { | |||
t.Fatalf("Unexpected remaining value; expected remaining value: %v; actual remaining value: %v", expectedRemaining, currentRemaining) | |||
} | |||
latestRemaining = currentRemaining | |||
if isDone { | |||
break | |||
} | |||
// Limit the runtime of the backup attempt. | |||
if (time.Now().Unix() - startTime) > usePagePerStepsTimeoutSeconds { | |||
t.Fatal("Backup is taking longer than expected.") | |||
} | |||
} | |||
} else { | |||
// Test the copying of all remaining pages. | |||
isDone, err = backup.Step(-1) | |||
if err != nil { | |||
t.Fatal("Failed to perform a backup step:", err) | |||
} | |||
if !isDone { | |||
t.Fatal("Backup is unexpectedly not done.") | |||
} | |||
} | |||
// Check that the page count and remaining values are reasonable. | |||
finalPageCount := backup.PageCount() | |||
if finalPageCount != initialPageCount { | |||
t.Fatalf("Final page count differs from the initial page count; initial page count: %v; final page count: %v", initialPageCount, finalPageCount) | |||
} | |||
finalRemaining := backup.Remaining() | |||
if finalRemaining != 0 { | |||
t.Fatalf("Unexpected remaining value: %v", finalRemaining) | |||
} | |||
// Finish the backup. | |||
err = backup.Finish() | |||
if err != nil { | |||
t.Fatal("Failed to finish backup:", err) | |||
} | |||
// Confirm that the "test" table now exists in the destination database. | |||
var doesTestTableExist bool | |||
err = destDb.QueryRow("SELECT EXISTS (SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'test' LIMIT 1) AS test_table_exists").Scan(&doesTestTableExist) | |||
if err != nil { | |||
t.Fatal("Failed to check if the \"test\" table exists in the destination database:", err) | |||
} | |||
if !doesTestTableExist { | |||
t.Fatal("The \"test\" table could not be found in the destination database.") | |||
} | |||
// Confirm that the number of rows in the destination database's "test" table matches that of the source table. | |||
var actualTestTableRowCount int | |||
err = destDb.QueryRow("SELECT COUNT(*) FROM test").Scan(&actualTestTableRowCount) | |||
if err != nil { | |||
t.Fatal("Failed to determine the rowcount of the \"test\" table in the destination database:", err) | |||
} | |||
if testRowCount != actualTestTableRowCount { | |||
t.Fatalf("Unexpected destination \"test\" table row count; expected: %v; found: %v", testRowCount, actualTestTableRowCount) | |||
} | |||
// Check each of the rows in the destination database. | |||
for id := 0; id < testRowCount; id++ { | |||
var checkedValue string | |||
err = destDb.QueryRow("SELECT value FROM test WHERE id = ?", id).Scan(&checkedValue) | |||
if err != nil { | |||
t.Fatal("Failed to query the \"test\" table in the destination database:", err) | |||
} | |||
var expectedValue = generateTestData(id) | |||
if checkedValue != expectedValue { | |||
t.Fatalf("Unexpected value in the \"test\" table in the destination database; expected value: %v; actual value: %v", expectedValue, checkedValue) | |||
} | |||
} | |||
} | |||
func TestBackupStepByStep(t *testing.T) { | |||
testBackup(t, testRowCount, true) | |||
} | |||
func TestBackupAllRemainingPages(t *testing.T) { | |||
testBackup(t, testRowCount, false) | |||
} | |||
// Test the error reporting when preparing to perform a backup. | |||
func TestBackupError(t *testing.T) { | |||
const driverName = "sqlite3_TestBackupError" | |||
// The driver's connection will be needed in order to perform the backup. | |||
var dbDriverConn *SQLiteConn | |||
sql.Register(driverName, &SQLiteDriver{ | |||
ConnectHook: func(conn *SQLiteConn) error { | |||
dbDriverConn = conn | |||
return nil | |||
}, | |||
}) | |||
// Connect to the database. | |||
dbTempFilename := TempFilename(t) | |||
defer os.Remove(dbTempFilename) | |||
db, err := sql.Open(driverName, dbTempFilename) | |||
if err != nil { | |||
t.Fatal("Failed to open the database:", err) | |||
} | |||
defer db.Close() | |||
db.Ping() | |||
// Need the driver connection in order to perform the backup. | |||
if dbDriverConn == nil { | |||
t.Fatal("Failed to get the driver connection.") | |||
} | |||
// Prepare to perform the backup. | |||
// Intentionally using the same connection for both the source and destination databases, to trigger an error result. | |||
backup, err := dbDriverConn.Backup("main", dbDriverConn, "main") | |||
if err == nil { | |||
t.Fatal("Failed to get the expected error result.") | |||
} | |||
const expectedError = "source and destination must be distinct" | |||
if err.Error() != expectedError { | |||
t.Fatalf("Unexpected error message; expected value: \"%v\"; actual value: \"%v\"", expectedError, err.Error()) | |||
} | |||
if backup != nil { | |||
t.Fatal("Failed to get the expected nil backup result.") | |||
} | |||
} |
@ -0,0 +1,411 @@ | |||
// 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. | |||
package sqlite3 | |||
// You can't export a Go function to C and have definitions in the C | |||
// preamble in the same file, so we have to have callbackTrampoline in | |||
// its own file. Because we need a separate file anyway, the support | |||
// code for SQLite custom functions is in here. | |||
/* | |||
#ifndef USE_LIBSQLITE3 | |||
#include "sqlite3-binding.h" | |||
#else | |||
#include <sqlite3.h> | |||
#endif | |||
#include <stdlib.h> | |||
void _sqlite3_result_text(sqlite3_context* ctx, const char* s); | |||
void _sqlite3_result_blob(sqlite3_context* ctx, const void* b, int l); | |||
*/ | |||
import "C" | |||
import ( | |||
"errors" | |||
"fmt" | |||
"math" | |||
"reflect" | |||
"sync" | |||
"unsafe" | |||
) | |||
//export callbackTrampoline | |||
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] | |||
fi := lookupHandle(C.sqlite3_user_data(ctx)).(*functionInfo) | |||
fi.Call(ctx, args) | |||
} | |||
//export stepTrampoline | |||
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)] | |||
ai := lookupHandle(C.sqlite3_user_data(ctx)).(*aggInfo) | |||
ai.Step(ctx, args) | |||
} | |||
//export doneTrampoline | |||
func doneTrampoline(ctx *C.sqlite3_context) { | |||
ai := lookupHandle(C.sqlite3_user_data(ctx)).(*aggInfo) | |||
ai.Done(ctx) | |||
} | |||
//export compareTrampoline | |||
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) | |||
return C.int(cmp(C.GoStringN(a, la), C.GoStringN(b, lb))) | |||
} | |||
//export commitHookTrampoline | |||
func commitHookTrampoline(handle unsafe.Pointer) int { | |||
callback := lookupHandle(handle).(func() int) | |||
return callback() | |||
} | |||
//export rollbackHookTrampoline | |||
func rollbackHookTrampoline(handle unsafe.Pointer) { | |||
callback := lookupHandle(handle).(func()) | |||
callback() | |||
} | |||
//export updateHookTrampoline | |||
func updateHookTrampoline(handle unsafe.Pointer, op int, db *C.char, table *C.char, rowid int64) { | |||
callback := lookupHandle(handle).(func(int, string, string, int64)) | |||
callback(op, C.GoString(db), C.GoString(table), rowid) | |||
} | |||
//export authorizerTrampoline | |||
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) | |||
return callback(op, C.GoString(arg1), C.GoString(arg2), C.GoString(arg3)) | |||
} | |||
//export preUpdateHookTrampoline | |||
func preUpdateHookTrampoline(handle unsafe.Pointer, dbHandle uintptr, op int, db *C.char, table *C.char, oldrowid int64, newrowid int64) { | |||
hval := lookupHandleVal(handle) | |||
data := SQLitePreUpdateData{ | |||
Conn: hval.db, | |||
Op: op, | |||
DatabaseName: C.GoString(db), | |||
TableName: C.GoString(table), | |||
OldRowID: oldrowid, | |||
NewRowID: newrowid, | |||
} | |||
callback := hval.val.(func(SQLitePreUpdateData)) | |||
callback(data) | |||
} | |||
// Use handles to avoid passing Go pointers to C. | |||
type handleVal struct { | |||
db *SQLiteConn | |||
val interface{} | |||
} | |||
var handleLock sync.Mutex | |||
var handleVals = make(map[unsafe.Pointer]handleVal) | |||
func newHandle(db *SQLiteConn, v interface{}) unsafe.Pointer { | |||
handleLock.Lock() | |||
defer handleLock.Unlock() | |||
val := handleVal{db: db, val: v} | |||
var p unsafe.Pointer = C.malloc(C.size_t(1)) | |||
if p == nil { | |||
panic("can't allocate 'cgo-pointer hack index pointer': ptr == nil") | |||
} | |||
handleVals[p] = val | |||
return p | |||
} | |||
func lookupHandleVal(handle unsafe.Pointer) handleVal { | |||
handleLock.Lock() | |||
defer handleLock.Unlock() | |||
return handleVals[handle] | |||
} | |||
func lookupHandle(handle unsafe.Pointer) interface{} { | |||
return lookupHandleVal(handle).val | |||
} | |||
func deleteHandles(db *SQLiteConn) { | |||
handleLock.Lock() | |||
defer handleLock.Unlock() | |||
for handle, val := range handleVals { | |||
if val.db == db { | |||
delete(handleVals, handle) | |||
C.free(handle) | |||
} | |||
} | |||
} | |||
// This is only here so that tests can refer to it. | |||
type callbackArgRaw C.sqlite3_value | |||
type callbackArgConverter func(*C.sqlite3_value) (reflect.Value, error) | |||
type callbackArgCast struct { | |||
f callbackArgConverter | |||
typ reflect.Type | |||
} | |||
func (c callbackArgCast) Run(v *C.sqlite3_value) (reflect.Value, error) { | |||
val, err := c.f(v) | |||
if err != nil { | |||
return reflect.Value{}, err | |||
} | |||
if !val.Type().ConvertibleTo(c.typ) { | |||
return reflect.Value{}, fmt.Errorf("cannot convert %s to %s", val.Type(), c.typ) | |||
} | |||
return val.Convert(c.typ), nil | |||
} | |||
func callbackArgInt64(v *C.sqlite3_value) (reflect.Value, error) { | |||
if C.sqlite3_value_type(v) != C.SQLITE_INTEGER { | |||
return reflect.Value{}, fmt.Errorf("argument must be an INTEGER") | |||
} | |||
return reflect.ValueOf(int64(C.sqlite3_value_int64(v))), nil | |||
} | |||
func callbackArgBool(v *C.sqlite3_value) (reflect.Value, error) { | |||
if C.sqlite3_value_type(v) != C.SQLITE_INTEGER { | |||
return reflect.Value{}, fmt.Errorf("argument must be an INTEGER") | |||
} | |||
i := int64(C.sqlite3_value_int64(v)) | |||
val := false | |||
if i != 0 { | |||
val = true | |||
} | |||
return reflect.ValueOf(val), nil | |||
} | |||
func callbackArgFloat64(v *C.sqlite3_value) (reflect.Value, error) { | |||
if C.sqlite3_value_type(v) != C.SQLITE_FLOAT { | |||
return reflect.Value{}, fmt.Errorf("argument must be a FLOAT") | |||
} | |||
return reflect.ValueOf(float64(C.sqlite3_value_double(v))), nil | |||
} | |||
func callbackArgBytes(v *C.sqlite3_value) (reflect.Value, error) { | |||
switch C.sqlite3_value_type(v) { | |||
case C.SQLITE_BLOB: | |||
l := C.sqlite3_value_bytes(v) | |||
p := C.sqlite3_value_blob(v) | |||
return reflect.ValueOf(C.GoBytes(p, l)), nil | |||
case C.SQLITE_TEXT: | |||
l := C.sqlite3_value_bytes(v) | |||
c := unsafe.Pointer(C.sqlite3_value_text(v)) | |||
return reflect.ValueOf(C.GoBytes(c, l)), nil | |||
default: | |||
return reflect.Value{}, fmt.Errorf("argument must be BLOB or TEXT") | |||
} | |||
} | |||
func callbackArgString(v *C.sqlite3_value) (reflect.Value, error) { | |||
switch C.sqlite3_value_type(v) { | |||
case C.SQLITE_BLOB: | |||
l := C.sqlite3_value_bytes(v) | |||
p := (*C.char)(C.sqlite3_value_blob(v)) | |||
return reflect.ValueOf(C.GoStringN(p, l)), nil | |||
case C.SQLITE_TEXT: | |||
c := (*C.char)(unsafe.Pointer(C.sqlite3_value_text(v))) | |||
return reflect.ValueOf(C.GoString(c)), nil | |||
default: | |||
return reflect.Value{}, fmt.Errorf("argument must be BLOB or TEXT") | |||
} | |||
} | |||
func callbackArgGeneric(v *C.sqlite3_value) (reflect.Value, error) { | |||
switch C.sqlite3_value_type(v) { | |||
case C.SQLITE_INTEGER: | |||
return callbackArgInt64(v) | |||
case C.SQLITE_FLOAT: | |||
return callbackArgFloat64(v) | |||
case C.SQLITE_TEXT: | |||
return callbackArgString(v) | |||
case C.SQLITE_BLOB: | |||
return callbackArgBytes(v) | |||
case C.SQLITE_NULL: | |||
// Interpret NULL as a nil byte slice. | |||
var ret []byte | |||
return reflect.ValueOf(ret), nil | |||
default: | |||
panic("unreachable") | |||
} | |||
} | |||
func callbackArg(typ reflect.Type) (callbackArgConverter, error) { | |||
switch typ.Kind() { | |||
case reflect.Interface: | |||
if typ.NumMethod() != 0 { | |||
return nil, errors.New("the only supported interface type is interface{}") | |||
} | |||
return callbackArgGeneric, nil | |||
case reflect.Slice: | |||
if typ.Elem().Kind() != reflect.Uint8 { | |||
return nil, errors.New("the only supported slice type is []byte") | |||
} | |||
return callbackArgBytes, nil | |||
case reflect.String: | |||
return callbackArgString, nil | |||
case reflect.Bool: | |||
return callbackArgBool, nil | |||
case reflect.Int64: | |||
return callbackArgInt64, nil | |||
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Int, reflect.Uint: | |||
c := callbackArgCast{callbackArgInt64, typ} | |||
return c.Run, nil | |||
case reflect.Float64: | |||
return callbackArgFloat64, nil | |||
case reflect.Float32: | |||
c := callbackArgCast{callbackArgFloat64, typ} | |||
return c.Run, nil | |||
default: | |||
return nil, fmt.Errorf("don't know how to convert to %s", typ) | |||
} | |||
} | |||
func callbackConvertArgs(argv []*C.sqlite3_value, converters []callbackArgConverter, variadic callbackArgConverter) ([]reflect.Value, error) { | |||
var args []reflect.Value | |||
if len(argv) < len(converters) { | |||
return nil, fmt.Errorf("function requires at least %d arguments", len(converters)) | |||
} | |||
for i, arg := range argv[:len(converters)] { | |||
v, err := converters[i](arg) | |||
if err != nil { | |||
return nil, err | |||
} | |||
args = append(args, v) | |||
} | |||
if variadic != nil { | |||
for _, arg := range argv[len(converters):] { | |||
v, err := variadic(arg) | |||
if err != nil { | |||
return nil, err | |||
} | |||
args = append(args, v) | |||
} | |||
} | |||
return args, nil | |||
} | |||
type callbackRetConverter func(*C.sqlite3_context, reflect.Value) error | |||
func callbackRetInteger(ctx *C.sqlite3_context, v reflect.Value) error { | |||
switch v.Type().Kind() { | |||
case reflect.Int64: | |||
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Int, reflect.Uint: | |||
v = v.Convert(reflect.TypeOf(int64(0))) | |||
case reflect.Bool: | |||
b := v.Interface().(bool) | |||
if b { | |||
v = reflect.ValueOf(int64(1)) | |||
} else { | |||
v = reflect.ValueOf(int64(0)) | |||
} | |||
default: | |||
return fmt.Errorf("cannot convert %s to INTEGER", v.Type()) | |||
} | |||
C.sqlite3_result_int64(ctx, C.sqlite3_int64(v.Interface().(int64))) | |||
return nil | |||
} | |||
func callbackRetFloat(ctx *C.sqlite3_context, v reflect.Value) error { | |||
switch v.Type().Kind() { | |||
case reflect.Float64: | |||
case reflect.Float32: | |||
v = v.Convert(reflect.TypeOf(float64(0))) | |||
default: | |||
return fmt.Errorf("cannot convert %s to FLOAT", v.Type()) | |||
} | |||
C.sqlite3_result_double(ctx, C.double(v.Interface().(float64))) | |||
return nil | |||
} | |||
func callbackRetBlob(ctx *C.sqlite3_context, v reflect.Value) error { | |||
if v.Type().Kind() != reflect.Slice || v.Type().Elem().Kind() != reflect.Uint8 { | |||
return fmt.Errorf("cannot convert %s to BLOB", v.Type()) | |||
} | |||
i := v.Interface() | |||
if i == nil || len(i.([]byte)) == 0 { | |||
C.sqlite3_result_null(ctx) | |||
} else { | |||
bs := i.([]byte) | |||
C._sqlite3_result_blob(ctx, unsafe.Pointer(&bs[0]), C.int(len(bs))) | |||
} | |||
return nil | |||
} | |||
func callbackRetText(ctx *C.sqlite3_context, v reflect.Value) error { | |||
if v.Type().Kind() != reflect.String { | |||
return fmt.Errorf("cannot convert %s to TEXT", v.Type()) | |||
} | |||
C._sqlite3_result_text(ctx, C.CString(v.Interface().(string))) | |||
return nil | |||
} | |||
func callbackRetNil(ctx *C.sqlite3_context, v reflect.Value) error { | |||
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) { | |||
switch typ.Kind() { | |||
case reflect.Interface: | |||
errorInterface := reflect.TypeOf((*error)(nil)).Elem() | |||
if typ.Implements(errorInterface) { | |||
return callbackRetNil, nil | |||
} | |||
if typ.NumMethod() == 0 { | |||
return callbackRetGeneric, nil | |||
} | |||
fallthrough | |||
case reflect.Slice: | |||
if typ.Elem().Kind() != reflect.Uint8 { | |||
return nil, errors.New("the only supported slice type is []byte") | |||
} | |||
return callbackRetBlob, nil | |||
case reflect.String: | |||
return callbackRetText, nil | |||
case reflect.Bool, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Int, reflect.Uint: | |||
return callbackRetInteger, nil | |||
case reflect.Float32, reflect.Float64: | |||
return callbackRetFloat, nil | |||
default: | |||
return nil, fmt.Errorf("don't know how to convert to %s", typ) | |||
} | |||
} | |||
func callbackError(ctx *C.sqlite3_context, err error) { | |||
cstr := C.CString(err.Error()) | |||
defer C.free(unsafe.Pointer(cstr)) | |||
C.sqlite3_result_error(ctx, cstr, C.int(-1)) | |||
} | |||
// Test support code. Tests are not allowed to import "C", so we can't | |||
// declare any functions that use C.sqlite3_value. | |||
func callbackSyntheticForTests(v reflect.Value, err error) callbackArgConverter { | |||
return func(*C.sqlite3_value) (reflect.Value, error) { | |||
return v, err | |||
} | |||
} |
@ -0,0 +1,116 @@ | |||
// 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 cgo | |||
package sqlite3 | |||
import ( | |||
"errors" | |||
"math" | |||
"reflect" | |||
"testing" | |||
) | |||
func TestCallbackArgCast(t *testing.T) { | |||
intConv := callbackSyntheticForTests(reflect.ValueOf(int64(math.MaxInt64)), nil) | |||
floatConv := callbackSyntheticForTests(reflect.ValueOf(float64(math.MaxFloat64)), nil) | |||
errConv := callbackSyntheticForTests(reflect.Value{}, errors.New("test")) | |||
tests := []struct { | |||
f callbackArgConverter | |||
o reflect.Value | |||
}{ | |||
{intConv, reflect.ValueOf(int8(-1))}, | |||
{intConv, reflect.ValueOf(int16(-1))}, | |||
{intConv, reflect.ValueOf(int32(-1))}, | |||
{intConv, reflect.ValueOf(uint8(math.MaxUint8))}, | |||
{intConv, reflect.ValueOf(uint16(math.MaxUint16))}, | |||
{intConv, reflect.ValueOf(uint32(math.MaxUint32))}, | |||
// Special case, int64->uint64 is only 1<<63 - 1, not 1<<64 - 1 | |||
{intConv, reflect.ValueOf(uint64(math.MaxInt64))}, | |||
{floatConv, reflect.ValueOf(float32(math.Inf(1)))}, | |||
} | |||
for _, test := range tests { | |||
conv := callbackArgCast{test.f, test.o.Type()} | |||
val, err := conv.Run(nil) | |||
if err != nil { | |||
t.Errorf("Couldn't convert to %s: %s", test.o.Type(), err) | |||
} else if !reflect.DeepEqual(val.Interface(), test.o.Interface()) { | |||
t.Errorf("Unexpected result from converting to %s: got %v, want %v", test.o.Type(), val.Interface(), test.o.Interface()) | |||
} | |||
} | |||
conv := callbackArgCast{errConv, reflect.TypeOf(int8(0))} | |||
_, err := conv.Run(nil) | |||
if err == nil { | |||
t.Errorf("Expected error during callbackArgCast, but got none") | |||
} | |||
} | |||
func TestCallbackConverters(t *testing.T) { | |||
tests := []struct { | |||
v interface{} | |||
err bool | |||
}{ | |||
// Unfortunately, we can't tell which converter was returned, | |||
// but we can at least check which types can be converted. | |||
{[]byte{0}, false}, | |||
{"text", false}, | |||
{true, false}, | |||
{int8(0), false}, | |||
{int16(0), false}, | |||
{int32(0), false}, | |||
{int64(0), false}, | |||
{uint8(0), false}, | |||
{uint16(0), false}, | |||
{uint32(0), false}, | |||
{uint64(0), false}, | |||
{int(0), false}, | |||
{uint(0), false}, | |||
{float64(0), false}, | |||
{float32(0), false}, | |||
{func() {}, true}, | |||
{complex64(complex(0, 0)), true}, | |||
{complex128(complex(0, 0)), true}, | |||
{struct{}{}, true}, | |||
{map[string]string{}, true}, | |||
{[]string{}, true}, | |||
{(*int8)(nil), true}, | |||
{make(chan int), true}, | |||
} | |||
for _, test := range tests { | |||
_, err := callbackArg(reflect.TypeOf(test.v)) | |||
if test.err && err == nil { | |||
t.Errorf("Expected an error when converting %s, got no error", reflect.TypeOf(test.v)) | |||
} else if !test.err && err != nil { | |||
t.Errorf("Expected converter when converting %s, got error: %s", reflect.TypeOf(test.v), err) | |||
} | |||
} | |||
for _, test := range tests { | |||
_, err := callbackRet(reflect.TypeOf(test.v)) | |||
if test.err && err == nil { | |||
t.Errorf("Expected an error when converting %s, got no error", reflect.TypeOf(test.v)) | |||
} else if !test.err && err != nil { | |||
t.Errorf("Expected converter when converting %s, got error: %s", reflect.TypeOf(test.v), err) | |||
} | |||
} | |||
} | |||
func TestCallbackReturnAny(t *testing.T) { | |||
udf := func() interface{} { | |||
return 1 | |||
} | |||
typ := reflect.TypeOf(udf) | |||
_, err := callbackRet(typ.Out(0)) | |||
if err != nil { | |||
t.Errorf("Expected valid callback for any return type, got: %s", err) | |||
} | |||
} |
@ -0,0 +1,299 @@ | |||
// Extracted from Go database/sql source code | |||
// Copyright 2011 The Go Authors. All rights reserved. | |||
// Use of this source code is governed by a BSD-style | |||
// license that can be found in the LICENSE file. | |||
// Type conversions for Scan. | |||
package sqlite3 | |||
import ( | |||
"database/sql" | |||
"database/sql/driver" | |||
"errors" | |||
"fmt" | |||
"reflect" | |||
"strconv" | |||
"time" | |||
) | |||
var errNilPtr = errors.New("destination pointer is nil") // embedded in descriptive error | |||
// convertAssign copies to dest the value in src, converting it if possible. | |||
// An error is returned if the copy would result in loss of information. | |||
// dest should be a pointer type. | |||
func convertAssign(dest, src interface{}) error { | |||
// Common cases, without reflect. | |||
switch s := src.(type) { | |||
case string: | |||
switch d := dest.(type) { | |||
case *string: | |||
if d == nil { | |||
return errNilPtr | |||
} | |||
*d = s | |||
return nil | |||
case *[]byte: | |||
if d == nil { | |||
return errNilPtr | |||
} | |||
*d = []byte(s) | |||
return nil | |||
case *sql.RawBytes: | |||
if d == nil { | |||
return errNilPtr | |||
} | |||
*d = append((*d)[:0], s...) | |||
return nil | |||
} | |||
case []byte: | |||
switch d := dest.(type) { | |||
case *string: | |||
if d == nil { | |||
return errNilPtr | |||
} | |||
*d = string(s) | |||
return nil | |||
case *interface{}: | |||
if d == nil { | |||
return errNilPtr | |||
} | |||
*d = cloneBytes(s) | |||
return nil | |||
case *[]byte: | |||
if d == nil { | |||
return errNilPtr | |||
} | |||
*d = cloneBytes(s) | |||
return nil | |||
case *sql.RawBytes: | |||
if d == nil { | |||
return errNilPtr | |||
} | |||
*d = s | |||
return nil | |||
} | |||
case time.Time: | |||
switch d := dest.(type) { | |||
case *time.Time: | |||
*d = s | |||
return nil | |||
case *string: | |||
*d = s.Format(time.RFC3339Nano) | |||
return nil | |||
case *[]byte: | |||
if d == nil { | |||
return errNilPtr | |||
} | |||
*d = []byte(s.Format(time.RFC3339Nano)) | |||
return nil | |||
case *sql.RawBytes: | |||
if d == nil { | |||
return errNilPtr | |||
} | |||
*d = s.AppendFormat((*d)[:0], time.RFC3339Nano) | |||
return nil | |||
} | |||
case nil: | |||
switch d := dest.(type) { | |||
case *interface{}: | |||
if d == nil { | |||
return errNilPtr | |||
} | |||
*d = nil | |||
return nil | |||
case *[]byte: | |||
if d == nil { | |||
return errNilPtr | |||
} | |||
*d = nil | |||
return nil | |||
case *sql.RawBytes: | |||
if d == nil { | |||
return errNilPtr | |||
} | |||
*d = nil | |||
return nil | |||
} | |||
} | |||
var sv reflect.Value | |||
switch d := dest.(type) { | |||
case *string: | |||
sv = reflect.ValueOf(src) | |||
switch sv.Kind() { | |||
case reflect.Bool, | |||
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, | |||
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, | |||
reflect.Float32, reflect.Float64: | |||
*d = asString(src) | |||
return nil | |||
} | |||
case *[]byte: | |||
sv = reflect.ValueOf(src) | |||
if b, ok := asBytes(nil, sv); ok { | |||
*d = b | |||
return nil | |||
} | |||
case *sql.RawBytes: | |||
sv = reflect.ValueOf(src) | |||
if b, ok := asBytes([]byte(*d)[:0], sv); ok { | |||
*d = sql.RawBytes(b) | |||
return nil | |||
} | |||
case *bool: | |||
bv, err := driver.Bool.ConvertValue(src) | |||
if err == nil { | |||
*d = bv.(bool) | |||
} | |||
return err | |||
case *interface{}: | |||
*d = src | |||
return nil | |||
} | |||
if scanner, ok := dest.(sql.Scanner); ok { | |||
return scanner.Scan(src) | |||
} | |||
dpv := reflect.ValueOf(dest) | |||
if dpv.Kind() != reflect.Ptr { | |||
return errors.New("destination not a pointer") | |||
} | |||
if dpv.IsNil() { | |||
return errNilPtr | |||
} | |||
if !sv.IsValid() { | |||
sv = reflect.ValueOf(src) | |||
} | |||
dv := reflect.Indirect(dpv) | |||
if sv.IsValid() && sv.Type().AssignableTo(dv.Type()) { | |||
switch b := src.(type) { | |||
case []byte: | |||
dv.Set(reflect.ValueOf(cloneBytes(b))) | |||
default: | |||
dv.Set(sv) | |||
} | |||
return nil | |||
} | |||
if dv.Kind() == sv.Kind() && sv.Type().ConvertibleTo(dv.Type()) { | |||
dv.Set(sv.Convert(dv.Type())) | |||
return nil | |||
} | |||
// The following conversions use a string value as an intermediate representation | |||
// to convert between various numeric types. | |||
// | |||
// This also allows scanning into user defined types such as "type Int int64". | |||
// For symmetry, also check for string destination types. | |||
switch dv.Kind() { | |||
case reflect.Ptr: | |||
if src == nil { | |||
dv.Set(reflect.Zero(dv.Type())) | |||
return nil | |||
} | |||
dv.Set(reflect.New(dv.Type().Elem())) | |||
return convertAssign(dv.Interface(), src) | |||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: | |||
s := asString(src) | |||
i64, err := strconv.ParseInt(s, 10, dv.Type().Bits()) | |||
if err != nil { | |||
err = strconvErr(err) | |||
return fmt.Errorf("converting driver.Value type %T (%q) to a %s: %v", src, s, dv.Kind(), err) | |||
} | |||
dv.SetInt(i64) | |||
return nil | |||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: | |||
s := asString(src) | |||
u64, err := strconv.ParseUint(s, 10, dv.Type().Bits()) | |||
if err != nil { | |||
err = strconvErr(err) | |||
return fmt.Errorf("converting driver.Value type %T (%q) to a %s: %v", src, s, dv.Kind(), err) | |||
} | |||
dv.SetUint(u64) | |||
return nil | |||
case reflect.Float32, reflect.Float64: | |||
s := asString(src) | |||
f64, err := strconv.ParseFloat(s, dv.Type().Bits()) | |||
if err != nil { | |||
err = strconvErr(err) | |||
return fmt.Errorf("converting driver.Value type %T (%q) to a %s: %v", src, s, dv.Kind(), err) | |||
} | |||
dv.SetFloat(f64) | |||
return nil | |||
case reflect.String: | |||
switch v := src.(type) { | |||
case string: | |||
dv.SetString(v) | |||
return nil | |||
case []byte: | |||
dv.SetString(string(v)) | |||
return nil | |||
} | |||
} | |||
return fmt.Errorf("unsupported Scan, storing driver.Value type %T into type %T", src, dest) | |||
} | |||
func strconvErr(err error) error { | |||
if ne, ok := err.(*strconv.NumError); ok { | |||
return ne.Err | |||
} | |||
return err | |||
} | |||
func cloneBytes(b []byte) []byte { | |||
if b == nil { | |||
return nil | |||
} | |||
c := make([]byte, len(b)) | |||
copy(c, b) | |||
return c | |||
} | |||
func asString(src interface{}) string { | |||
switch v := src.(type) { | |||
case string: | |||
return v | |||
case []byte: | |||
return string(v) | |||
} | |||
rv := reflect.ValueOf(src) | |||
switch rv.Kind() { | |||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: | |||
return strconv.FormatInt(rv.Int(), 10) | |||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: | |||
return strconv.FormatUint(rv.Uint(), 10) | |||
case reflect.Float64: | |||
return strconv.FormatFloat(rv.Float(), 'g', -1, 64) | |||
case reflect.Float32: | |||
return strconv.FormatFloat(rv.Float(), 'g', -1, 32) | |||
case reflect.Bool: | |||
return strconv.FormatBool(rv.Bool()) | |||
} | |||
return fmt.Sprintf("%v", src) | |||
} | |||
func asBytes(buf []byte, rv reflect.Value) (b []byte, ok bool) { | |||
switch rv.Kind() { | |||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: | |||
return strconv.AppendInt(buf, rv.Int(), 10), true | |||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: | |||
return strconv.AppendUint(buf, rv.Uint(), 10), true | |||
case reflect.Float32: | |||
return strconv.AppendFloat(buf, rv.Float(), 'g', -1, 32), true | |||
case reflect.Float64: | |||
return strconv.AppendFloat(buf, rv.Float(), 'g', -1, 64), true | |||
case reflect.Bool: | |||
return strconv.AppendBool(buf, rv.Bool()), true | |||
case reflect.String: | |||
s := rv.String() | |||
return append(buf, s...), true | |||
} | |||
return | |||
} |
@ -0,0 +1,135 @@ | |||
/* | |||
Package sqlite3 provides interface to SQLite3 databases. | |||
This works as a driver for database/sql. | |||
Installation | |||
go get github.com/mattn/go-sqlite3 | |||
Supported Types | |||
Currently, go-sqlite3 supports the following data types. | |||
+------------------------------+ | |||
|go | sqlite3 | | |||
|----------|-------------------| | |||
|nil | null | | |||
|int | integer | | |||
|int64 | integer | | |||
|float64 | float | | |||
|bool | integer | | |||
|[]byte | blob | | |||
|string | text | | |||
|time.Time | timestamp/datetime| | |||
+------------------------------+ | |||
SQLite3 Extension | |||
You can write your own extension module for sqlite3. For example, below is an | |||
extension for a Regexp matcher operation. | |||
#include <pcre.h> | |||
#include <string.h> | |||
#include <stdio.h> | |||
#include <sqlite3ext.h> | |||
SQLITE_EXTENSION_INIT1 | |||
static void regexp_func(sqlite3_context *context, int argc, sqlite3_value **argv) { | |||
if (argc >= 2) { | |||
const char *target = (const char *)sqlite3_value_text(argv[1]); | |||
const char *pattern = (const char *)sqlite3_value_text(argv[0]); | |||
const char* errstr = NULL; | |||
int erroff = 0; | |||
int vec[500]; | |||
int n, rc; | |||
pcre* re = pcre_compile(pattern, 0, &errstr, &erroff, NULL); | |||
rc = pcre_exec(re, NULL, target, strlen(target), 0, 0, vec, 500); | |||
if (rc <= 0) { | |||
sqlite3_result_error(context, errstr, 0); | |||
return; | |||
} | |||
sqlite3_result_int(context, 1); | |||
} | |||
} | |||
#ifdef _WIN32 | |||
__declspec(dllexport) | |||
#endif | |||
int sqlite3_extension_init(sqlite3 *db, char **errmsg, | |||
const sqlite3_api_routines *api) { | |||
SQLITE_EXTENSION_INIT2(api); | |||
return sqlite3_create_function(db, "regexp", 2, SQLITE_UTF8, | |||
(void*)db, regexp_func, NULL, NULL); | |||
} | |||
It needs to be built as a so/dll shared library. And you need to register | |||
the extension module like below. | |||
sql.Register("sqlite3_with_extensions", | |||
&sqlite3.SQLiteDriver{ | |||
Extensions: []string{ | |||
"sqlite3_mod_regexp", | |||
}, | |||
}) | |||
Then, you can use this extension. | |||
rows, err := db.Query("select text from mytable where name regexp '^golang'") | |||
Connection Hook | |||
You can hook and inject your code when the connection is established by setting | |||
ConnectHook to get the SQLiteConn. | |||
sql.Register("sqlite3_with_hook_example", | |||
&sqlite3.SQLiteDriver{ | |||
ConnectHook: func(conn *sqlite3.SQLiteConn) error { | |||
sqlite3conn = append(sqlite3conn, conn) | |||
return nil | |||
}, | |||
}) | |||
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 | |||
If you want to register Go functions as SQLite extension functions | |||
you can make a custom driver by calling RegisterFunction from | |||
ConnectHook. | |||
regex = func(re, s string) (bool, error) { | |||
return regexp.MatchString(re, s) | |||
} | |||
sql.Register("sqlite3_extended", | |||
&sqlite3.SQLiteDriver{ | |||
ConnectHook: func(conn *sqlite3.SQLiteConn) error { | |||
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. | |||
*/ | |||
package sqlite3 |
@ -0,0 +1,150 @@ | |||
// 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. | |||
package sqlite3 | |||
/* | |||
#ifndef USE_LIBSQLITE3 | |||
#include "sqlite3-binding.h" | |||
#else | |||
#include <sqlite3.h> | |||
#endif | |||
*/ | |||
import "C" | |||
import "syscall" | |||
// ErrNo inherit errno. | |||
type ErrNo int | |||
// ErrNoMask is mask code. | |||
const ErrNoMask C.int = 0xff | |||
// ErrNoExtended is extended errno. | |||
type ErrNoExtended int | |||
// Error implement sqlite error code. | |||
type Error struct { | |||
Code ErrNo /* The error code returned by SQLite */ | |||
ExtendedCode ErrNoExtended /* The extended error code returned by SQLite */ | |||
SystemErrno syscall.Errno /* The system errno returned by the OS through SQLite, if applicable */ | |||
err string /* The error string returned by sqlite3_errmsg(), | |||
this usually contains more specific details. */ | |||
} | |||
// result codes from http://www.sqlite.org/c3ref/c_abort.html | |||
var ( | |||
ErrError = ErrNo(1) /* SQL error or missing database */ | |||
ErrInternal = ErrNo(2) /* Internal logic error in SQLite */ | |||
ErrPerm = ErrNo(3) /* Access permission denied */ | |||
ErrAbort = ErrNo(4) /* Callback routine requested an abort */ | |||
ErrBusy = ErrNo(5) /* The database file is locked */ | |||
ErrLocked = ErrNo(6) /* A table in the database is locked */ | |||
ErrNomem = ErrNo(7) /* A malloc() failed */ | |||
ErrReadonly = ErrNo(8) /* Attempt to write a readonly database */ | |||
ErrInterrupt = ErrNo(9) /* Operation terminated by sqlite3_interrupt() */ | |||
ErrIoErr = ErrNo(10) /* Some kind of disk I/O error occurred */ | |||
ErrCorrupt = ErrNo(11) /* The database disk image is malformed */ | |||
ErrNotFound = ErrNo(12) /* Unknown opcode in sqlite3_file_control() */ | |||
ErrFull = ErrNo(13) /* Insertion failed because database is full */ | |||
ErrCantOpen = ErrNo(14) /* Unable to open the database file */ | |||
ErrProtocol = ErrNo(15) /* Database lock protocol error */ | |||
ErrEmpty = ErrNo(16) /* Database is empty */ | |||
ErrSchema = ErrNo(17) /* The database schema changed */ | |||
ErrTooBig = ErrNo(18) /* String or BLOB exceeds size limit */ | |||
ErrConstraint = ErrNo(19) /* Abort due to constraint violation */ | |||
ErrMismatch = ErrNo(20) /* Data type mismatch */ | |||
ErrMisuse = ErrNo(21) /* Library used incorrectly */ | |||
ErrNoLFS = ErrNo(22) /* Uses OS features not supported on host */ | |||
ErrAuth = ErrNo(23) /* Authorization denied */ | |||
ErrFormat = ErrNo(24) /* Auxiliary database format error */ | |||
ErrRange = ErrNo(25) /* 2nd parameter to sqlite3_bind out of range */ | |||
ErrNotADB = ErrNo(26) /* File opened that is not a database file */ | |||
ErrNotice = ErrNo(27) /* Notifications from sqlite3_log() */ | |||
ErrWarning = ErrNo(28) /* Warnings from sqlite3_log() */ | |||
) | |||
// Error return error message from errno. | |||
func (err ErrNo) Error() string { | |||
return Error{Code: err}.Error() | |||
} | |||
// Extend return extended errno. | |||
func (err ErrNo) Extend(by int) ErrNoExtended { | |||
return ErrNoExtended(int(err) | (by << 8)) | |||
} | |||
// Error return error message that is extended code. | |||
func (err ErrNoExtended) Error() string { | |||
return Error{Code: ErrNo(C.int(err) & ErrNoMask), ExtendedCode: err}.Error() | |||
} | |||
func (err Error) Error() string { | |||
var str string | |||
if err.err != "" { | |||
str = err.err | |||
} else { | |||
str = C.GoString(C.sqlite3_errstr(C.int(err.Code))) | |||
} | |||
if err.SystemErrno != 0 { | |||
str += ": " + err.SystemErrno.Error() | |||
} | |||
return str | |||
} | |||
// result codes from http://www.sqlite.org/c3ref/c_abort_rollback.html | |||
var ( | |||
ErrIoErrRead = ErrIoErr.Extend(1) | |||
ErrIoErrShortRead = ErrIoErr.Extend(2) | |||
ErrIoErrWrite = ErrIoErr.Extend(3) | |||
ErrIoErrFsync = ErrIoErr.Extend(4) | |||
ErrIoErrDirFsync = ErrIoErr.Extend(5) | |||
ErrIoErrTruncate = ErrIoErr.Extend(6) | |||
ErrIoErrFstat = ErrIoErr.Extend(7) | |||
ErrIoErrUnlock = ErrIoErr.Extend(8) | |||
ErrIoErrRDlock = ErrIoErr.Extend(9) | |||
ErrIoErrDelete = ErrIoErr.Extend(10) | |||
ErrIoErrBlocked = ErrIoErr.Extend(11) | |||
ErrIoErrNoMem = ErrIoErr.Extend(12) | |||
ErrIoErrAccess = ErrIoErr.Extend(13) | |||
ErrIoErrCheckReservedLock = ErrIoErr.Extend(14) | |||
ErrIoErrLock = ErrIoErr.Extend(15) | |||
ErrIoErrClose = ErrIoErr.Extend(16) | |||
ErrIoErrDirClose = ErrIoErr.Extend(17) | |||
ErrIoErrSHMOpen = ErrIoErr.Extend(18) | |||
ErrIoErrSHMSize = ErrIoErr.Extend(19) | |||
ErrIoErrSHMLock = ErrIoErr.Extend(20) | |||
ErrIoErrSHMMap = ErrIoErr.Extend(21) | |||
ErrIoErrSeek = ErrIoErr.Extend(22) | |||
ErrIoErrDeleteNoent = ErrIoErr.Extend(23) | |||
ErrIoErrMMap = ErrIoErr.Extend(24) | |||
ErrIoErrGetTempPath = ErrIoErr.Extend(25) | |||
ErrIoErrConvPath = ErrIoErr.Extend(26) | |||
ErrLockedSharedCache = ErrLocked.Extend(1) | |||
ErrBusyRecovery = ErrBusy.Extend(1) | |||
ErrBusySnapshot = ErrBusy.Extend(2) | |||
ErrCantOpenNoTempDir = ErrCantOpen.Extend(1) | |||
ErrCantOpenIsDir = ErrCantOpen.Extend(2) | |||
ErrCantOpenFullPath = ErrCantOpen.Extend(3) | |||
ErrCantOpenConvPath = ErrCantOpen.Extend(4) | |||
ErrCorruptVTab = ErrCorrupt.Extend(1) | |||
ErrReadonlyRecovery = ErrReadonly.Extend(1) | |||
ErrReadonlyCantLock = ErrReadonly.Extend(2) | |||
ErrReadonlyRollback = ErrReadonly.Extend(3) | |||
ErrReadonlyDbMoved = ErrReadonly.Extend(4) | |||
ErrAbortRollback = ErrAbort.Extend(2) | |||
ErrConstraintCheck = ErrConstraint.Extend(1) | |||
ErrConstraintCommitHook = ErrConstraint.Extend(2) | |||
ErrConstraintForeignKey = ErrConstraint.Extend(3) | |||
ErrConstraintFunction = ErrConstraint.Extend(4) | |||
ErrConstraintNotNull = ErrConstraint.Extend(5) | |||
ErrConstraintPrimaryKey = ErrConstraint.Extend(6) | |||
ErrConstraintTrigger = ErrConstraint.Extend(7) | |||
ErrConstraintUnique = ErrConstraint.Extend(8) | |||
ErrConstraintVTab = ErrConstraint.Extend(9) | |||
ErrConstraintRowID = ErrConstraint.Extend(10) | |||
ErrNoticeRecoverWAL = ErrNotice.Extend(1) | |||
ErrNoticeRecoverRollback = ErrNotice.Extend(2) | |||
ErrWarningAutoIndex = ErrWarning.Extend(1) | |||
) |
@ -0,0 +1,275 @@ | |||
// 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 cgo | |||
package sqlite3 | |||
import ( | |||
"database/sql" | |||
"io/ioutil" | |||
"os" | |||
"path" | |||
"testing" | |||
) | |||
func TestSimpleError(t *testing.T) { | |||
e := ErrError.Error() | |||
if e != "SQL logic error or missing database" && e != "SQL logic error" { | |||
t.Error("wrong error code: " + e) | |||
} | |||
} | |||
func TestCorruptDbErrors(t *testing.T) { | |||
dirName, err := ioutil.TempDir("", "sqlite3") | |||
if err != nil { | |||
t.Fatal(err) | |||
} | |||
defer os.RemoveAll(dirName) | |||
dbFileName := path.Join(dirName, "test.db") | |||
f, err := os.Create(dbFileName) | |||
if err != nil { | |||
t.Error(err) | |||
} | |||
f.Write([]byte{1, 2, 3, 4, 5}) | |||
f.Close() | |||
db, err := sql.Open("sqlite3", dbFileName) | |||
if err == nil { | |||
_, err = db.Exec("drop table foo") | |||
} | |||
sqliteErr := err.(Error) | |||
if sqliteErr.Code != ErrNotADB { | |||
t.Error("wrong error code for corrupted DB") | |||
} | |||
if err.Error() == "" { | |||
t.Error("wrong error string for corrupted DB") | |||
} | |||
db.Close() | |||
} | |||
func TestSqlLogicErrors(t *testing.T) { | |||
dirName, err := ioutil.TempDir("", "sqlite3") | |||
if err != nil { | |||
t.Fatal(err) | |||
} | |||
defer os.RemoveAll(dirName) | |||
dbFileName := path.Join(dirName, "test.db") | |||
db, err := sql.Open("sqlite3", dbFileName) | |||
if err != nil { | |||
t.Error(err) | |||
} | |||
defer db.Close() | |||
_, err = db.Exec("CREATE TABLE Foo (id INTEGER PRIMARY KEY)") | |||
if err != nil { | |||
t.Error(err) | |||
} | |||
const expectedErr = "table Foo already exists" | |||
_, err = db.Exec("CREATE TABLE Foo (id INTEGER PRIMARY KEY)") | |||
if err.Error() != expectedErr { | |||
t.Errorf("Unexpected error: %s, expected %s", err.Error(), expectedErr) | |||
} | |||
} | |||
func TestExtendedErrorCodes_ForeignKey(t *testing.T) { | |||
dirName, err := ioutil.TempDir("", "sqlite3-err") | |||
if err != nil { | |||
t.Fatal(err) | |||
} | |||
defer os.RemoveAll(dirName) | |||
dbFileName := path.Join(dirName, "test.db") | |||
db, err := sql.Open("sqlite3", dbFileName) | |||
if err != nil { | |||
t.Error(err) | |||
} | |||
defer db.Close() | |||
_, err = db.Exec("PRAGMA foreign_keys=ON;") | |||
if err != nil { | |||
t.Errorf("PRAGMA foreign_keys=ON: %v", err) | |||
} | |||
_, err = db.Exec(`CREATE TABLE Foo ( | |||
id INTEGER PRIMARY KEY AUTOINCREMENT, | |||
value INTEGER NOT NULL, | |||
ref INTEGER NULL REFERENCES Foo (id), | |||
UNIQUE(value) | |||
);`) | |||
if err != nil { | |||
t.Error(err) | |||
} | |||
_, err = db.Exec("INSERT INTO Foo (ref, value) VALUES (100, 100);") | |||
if err == nil { | |||
t.Error("No error!") | |||
} else { | |||
sqliteErr := err.(Error) | |||
if sqliteErr.Code != ErrConstraint { | |||
t.Errorf("Wrong basic error code: %d != %d", | |||
sqliteErr.Code, ErrConstraint) | |||
} | |||
if sqliteErr.ExtendedCode != ErrConstraintForeignKey { | |||
t.Errorf("Wrong extended error code: %d != %d", | |||
sqliteErr.ExtendedCode, ErrConstraintForeignKey) | |||
} | |||
} | |||
} | |||
func TestExtendedErrorCodes_NotNull(t *testing.T) { | |||
dirName, err := ioutil.TempDir("", "sqlite3-err") | |||
if err != nil { | |||
t.Fatal(err) | |||
} | |||
defer os.RemoveAll(dirName) | |||
dbFileName := path.Join(dirName, "test.db") | |||
db, err := sql.Open("sqlite3", dbFileName) | |||
if err != nil { | |||
t.Error(err) | |||
} | |||
defer db.Close() | |||
_, err = db.Exec("PRAGMA foreign_keys=ON;") | |||
if err != nil { | |||
t.Errorf("PRAGMA foreign_keys=ON: %v", err) | |||
} | |||
_, err = db.Exec(`CREATE TABLE Foo ( | |||
id INTEGER PRIMARY KEY AUTOINCREMENT, | |||
value INTEGER NOT NULL, | |||
ref INTEGER NULL REFERENCES Foo (id), | |||
UNIQUE(value) | |||
);`) | |||
if err != nil { | |||
t.Error(err) | |||
} | |||
res, err := db.Exec("INSERT INTO Foo (value) VALUES (100);") | |||
if err != nil { | |||
t.Fatalf("Creating first row: %v", err) | |||
} | |||
id, err := res.LastInsertId() | |||
if err != nil { | |||
t.Fatalf("Retrieving last insert id: %v", err) | |||
} | |||
_, err = db.Exec("INSERT INTO Foo (ref) VALUES (?);", id) | |||
if err == nil { | |||
t.Error("No error!") | |||
} else { | |||
sqliteErr := err.(Error) | |||
if sqliteErr.Code != ErrConstraint { | |||
t.Errorf("Wrong basic error code: %d != %d", | |||
sqliteErr.Code, ErrConstraint) | |||
} | |||
if sqliteErr.ExtendedCode != ErrConstraintNotNull { | |||
t.Errorf("Wrong extended error code: %d != %d", | |||
sqliteErr.ExtendedCode, ErrConstraintNotNull) | |||
} | |||
} | |||
} | |||
func TestExtendedErrorCodes_Unique(t *testing.T) { | |||
dirName, err := ioutil.TempDir("", "sqlite3-err") | |||
if err != nil { | |||
t.Fatal(err) | |||
} | |||
defer os.RemoveAll(dirName) | |||
dbFileName := path.Join(dirName, "test.db") | |||
db, err := sql.Open("sqlite3", dbFileName) | |||
if err != nil { | |||
t.Error(err) | |||
} | |||
defer db.Close() | |||
_, err = db.Exec("PRAGMA foreign_keys=ON;") | |||
if err != nil { | |||
t.Errorf("PRAGMA foreign_keys=ON: %v", err) | |||
} | |||
_, err = db.Exec(`CREATE TABLE Foo ( | |||
id INTEGER PRIMARY KEY AUTOINCREMENT, | |||
value INTEGER NOT NULL, | |||
ref INTEGER NULL REFERENCES Foo (id), | |||
UNIQUE(value) | |||
);`) | |||
if err != nil { | |||
t.Error(err) | |||
} | |||
res, err := db.Exec("INSERT INTO Foo (value) VALUES (100);") | |||
if err != nil { | |||
t.Fatalf("Creating first row: %v", err) | |||
} | |||
id, err := res.LastInsertId() | |||
if err != nil { | |||
t.Fatalf("Retrieving last insert id: %v", err) | |||
} | |||
_, err = db.Exec("INSERT INTO Foo (ref, value) VALUES (?, 100);", id) | |||
if err == nil { | |||
t.Error("No error!") | |||
} else { | |||
sqliteErr := err.(Error) | |||
if sqliteErr.Code != ErrConstraint { | |||
t.Errorf("Wrong basic error code: %d != %d", | |||
sqliteErr.Code, ErrConstraint) | |||
} | |||
if sqliteErr.ExtendedCode != ErrConstraintUnique { | |||
t.Errorf("Wrong extended error code: %d != %d", | |||
sqliteErr.ExtendedCode, ErrConstraintUnique) | |||
} | |||
extended := sqliteErr.Code.Extend(3).Error() | |||
expected := "constraint failed" | |||
if extended != expected { | |||
t.Errorf("Wrong basic error code: %q != %q", | |||
extended, expected) | |||
} | |||
} | |||
} | |||
func TestError_SystemErrno(t *testing.T) { | |||
_, n, _ := Version() | |||
if n < 3012000 { | |||
t.Skip("sqlite3_system_errno requires sqlite3 >= 3.12.0") | |||
} | |||
// open a non-existent database in read-only mode so we get an IO error. | |||
db, err := sql.Open("sqlite3", "file:nonexistent.db?mode=ro") | |||
if err != nil { | |||
t.Fatal(err) | |||
} | |||
defer db.Close() | |||
err = db.Ping() | |||
if err == nil { | |||
t.Fatal("expected error pinging read-only non-existent database, but got nil") | |||
} | |||
serr, ok := err.(Error) | |||
if !ok { | |||
t.Fatalf("expected error to be of type Error, but got %[1]T %[1]v", err) | |||
} | |||
if serr.SystemErrno == 0 { | |||
t.Fatal("expected SystemErrno to be set") | |||
} | |||
if !os.IsNotExist(serr.SystemErrno) { | |||
t.Errorf("expected SystemErrno to be a not exists error, but got %v", serr.SystemErrno) | |||
} | |||
} |
@ -0,0 +1,7 @@ | |||
module github.com/mattn/go-sqlite3 | |||
go 1.16 | |||
retract ( | |||
[v2.0.0+incompatible, v2.0.6+incompatible] // Accidental; no major changes or features. | |||
) |
@ -0,0 +1,103 @@ | |||
// 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. | |||
package sqlite3 | |||
/* | |||
#ifndef USE_LIBSQLITE3 | |||
#include "sqlite3-binding.h" | |||
#else | |||
#include <sqlite3.h> | |||
#endif | |||
#include <stdlib.h> | |||
// These wrappers are necessary because SQLITE_TRANSIENT | |||
// is a pointer constant, and cgo doesn't translate them correctly. | |||
static inline void my_result_text(sqlite3_context *ctx, char *p, int np) { | |||
sqlite3_result_text(ctx, p, np, SQLITE_TRANSIENT); | |||
} | |||
static inline void my_result_blob(sqlite3_context *ctx, void *p, int np) { | |||
sqlite3_result_blob(ctx, p, np, SQLITE_TRANSIENT); | |||
} | |||
*/ | |||
import "C" | |||
import ( | |||
"math" | |||
"reflect" | |||
"unsafe" | |||
) | |||
const i64 = unsafe.Sizeof(int(0)) > 4 | |||
// SQLiteContext behave sqlite3_context | |||
type SQLiteContext C.sqlite3_context | |||
// ResultBool sets the result of an SQL function. | |||
func (c *SQLiteContext) ResultBool(b bool) { | |||
if b { | |||
c.ResultInt(1) | |||
} else { | |||
c.ResultInt(0) | |||
} | |||
} | |||
// ResultBlob sets the result of an SQL function. | |||
// See: sqlite3_result_blob, http://sqlite.org/c3ref/result_blob.html | |||
func (c *SQLiteContext) ResultBlob(b []byte) { | |||
if i64 && len(b) > math.MaxInt32 { | |||
C.sqlite3_result_error_toobig((*C.sqlite3_context)(c)) | |||
return | |||
} | |||
var p *byte | |||
if len(b) > 0 { | |||
p = &b[0] | |||
} | |||
C.my_result_blob((*C.sqlite3_context)(c), unsafe.Pointer(p), C.int(len(b))) | |||
} | |||
// ResultDouble sets the result of an SQL function. | |||
// See: sqlite3_result_double, http://sqlite.org/c3ref/result_blob.html | |||
func (c *SQLiteContext) ResultDouble(d float64) { | |||
C.sqlite3_result_double((*C.sqlite3_context)(c), C.double(d)) | |||
} | |||
// ResultInt sets the result of an SQL function. | |||
// See: sqlite3_result_int, http://sqlite.org/c3ref/result_blob.html | |||
func (c *SQLiteContext) ResultInt(i int) { | |||
if i64 && (i > math.MaxInt32 || i < math.MinInt32) { | |||
C.sqlite3_result_int64((*C.sqlite3_context)(c), C.sqlite3_int64(i)) | |||
} else { | |||
C.sqlite3_result_int((*C.sqlite3_context)(c), C.int(i)) | |||
} | |||
} | |||
// ResultInt64 sets the result of an SQL function. | |||
// See: sqlite3_result_int64, http://sqlite.org/c3ref/result_blob.html | |||
func (c *SQLiteContext) ResultInt64(i int64) { | |||
C.sqlite3_result_int64((*C.sqlite3_context)(c), C.sqlite3_int64(i)) | |||
} | |||
// ResultNull sets the result of an SQL function. | |||
// See: sqlite3_result_null, http://sqlite.org/c3ref/result_blob.html | |||
func (c *SQLiteContext) ResultNull() { | |||
C.sqlite3_result_null((*C.sqlite3_context)(c)) | |||
} | |||
// ResultText sets the result of an SQL function. | |||
// See: sqlite3_result_text, http://sqlite.org/c3ref/result_blob.html | |||
func (c *SQLiteContext) ResultText(s string) { | |||
h := (*reflect.StringHeader)(unsafe.Pointer(&s)) | |||
cs, l := (*C.char)(unsafe.Pointer(h.Data)), C.int(h.Len) | |||
C.my_result_text((*C.sqlite3_context)(c), cs, l) | |||
} | |||
// ResultZeroblob sets the result of an SQL function. | |||
// See: sqlite3_result_zeroblob, http://sqlite.org/c3ref/result_blob.html | |||
func (c *SQLiteContext) ResultZeroblob(n int) { | |||
C.sqlite3_result_zeroblob((*C.sqlite3_context)(c), C.int(n)) | |||
} |
@ -0,0 +1,120 @@ | |||
// Copyright (C) 2018 G.J.R. Timmer <gjr.timmer@gmail.com>. | |||
// | |||
// Use of this source code is governed by an MIT-style | |||
// license that can be found in the LICENSE file. | |||
package sqlite3 | |||
import ( | |||
"crypto/sha1" | |||
"crypto/sha256" | |||
"crypto/sha512" | |||
) | |||
// This file provides several different implementations for the | |||
// default embedded sqlite_crypt function. | |||
// This function is uses a caesar-cypher by default | |||
// and is used within the UserAuthentication module to encode | |||
// the password. | |||
// | |||
// The provided functions can be used as an overload to the sqlite_crypt | |||
// function through the use of the RegisterFunc on the connection. | |||
// | |||
// Because the functions can serv a purpose to an end-user | |||
// without using the UserAuthentication module | |||
// the functions are default compiled in. | |||
// | |||
// From SQLITE3 - user-auth.txt | |||
// The sqlite_user.pw field is encoded by a built-in SQL function | |||
// "sqlite_crypt(X,Y)". The two arguments are both BLOBs. The first argument | |||
// is the plaintext password supplied to the sqlite3_user_authenticate() | |||
// interface. The second argument is the sqlite_user.pw value and is supplied | |||
// so that the function can extract the "salt" used by the password encoder. | |||
// The result of sqlite_crypt(X,Y) is another blob which is the value that | |||
// ends up being stored in sqlite_user.pw. To verify credentials X supplied | |||
// by the sqlite3_user_authenticate() routine, SQLite runs: | |||
// | |||
// sqlite_user.pw == sqlite_crypt(X, sqlite_user.pw) | |||
// | |||
// To compute an appropriate sqlite_user.pw value from a new or modified | |||
// password X, sqlite_crypt(X,NULL) is run. A new random salt is selected | |||
// when the second argument is NULL. | |||
// | |||
// The built-in version of of sqlite_crypt() uses a simple Caesar-cypher | |||
// which prevents passwords from being revealed by searching the raw database | |||
// for ASCII text, but is otherwise trivally broken. For better password | |||
// security, the database should be encrypted using the SQLite Encryption | |||
// Extension or similar technology. Or, the application can use the | |||
// sqlite3_create_function() interface to provide an alternative | |||
// implementation of sqlite_crypt() that computes a stronger password hash, | |||
// perhaps using a cryptographic hash function like SHA1. | |||
// CryptEncoderSHA1 encodes a password with SHA1 | |||
func CryptEncoderSHA1(pass []byte, hash interface{}) []byte { | |||
h := sha1.Sum(pass) | |||
return h[:] | |||
} | |||
// CryptEncoderSSHA1 encodes a password with SHA1 with the | |||
// configured salt. | |||
func CryptEncoderSSHA1(salt string) func(pass []byte, hash interface{}) []byte { | |||
return func(pass []byte, hash interface{}) []byte { | |||
s := []byte(salt) | |||
p := append(pass, s...) | |||
h := sha1.Sum(p) | |||
return h[:] | |||
} | |||
} | |||
// CryptEncoderSHA256 encodes a password with SHA256 | |||
func CryptEncoderSHA256(pass []byte, hash interface{}) []byte { | |||
h := sha256.Sum256(pass) | |||
return h[:] | |||
} | |||
// CryptEncoderSSHA256 encodes a password with SHA256 | |||
// with the configured salt | |||
func CryptEncoderSSHA256(salt string) func(pass []byte, hash interface{}) []byte { | |||
return func(pass []byte, hash interface{}) []byte { | |||
s := []byte(salt) | |||
p := append(pass, s...) | |||
h := sha256.Sum256(p) | |||
return h[:] | |||
} | |||
} | |||
// CryptEncoderSHA384 encodes a password with SHA384 | |||
func CryptEncoderSHA384(pass []byte, hash interface{}) []byte { | |||
h := sha512.Sum384(pass) | |||
return h[:] | |||
} | |||
// CryptEncoderSSHA384 encodes a password with SHA384 | |||
// with the configured salt | |||
func CryptEncoderSSHA384(salt string) func(pass []byte, hash interface{}) []byte { | |||
return func(pass []byte, hash interface{}) []byte { | |||
s := []byte(salt) | |||
p := append(pass, s...) | |||
h := sha512.Sum384(p) | |||
return h[:] | |||
} | |||
} | |||
// CryptEncoderSHA512 encodes a password with SHA512 | |||
func CryptEncoderSHA512(pass []byte, hash interface{}) []byte { | |||
h := sha512.Sum512(pass) | |||
return h[:] | |||
} | |||
// CryptEncoderSSHA512 encodes a password with SHA512 | |||
// with the configured salt | |||
func CryptEncoderSSHA512(salt string) func(pass []byte, hash interface{}) []byte { | |||
return func(pass []byte, hash interface{}) []byte { | |||
s := []byte(salt) | |||
p := append(pass, s...) | |||
h := sha512.Sum512(p) | |||
return h[:] | |||
} | |||
} | |||
// EOF |
@ -0,0 +1,57 @@ | |||
// Copyright (C) 2018 G.J.R. Timmer <gjr.timmer@gmail.com>. | |||
// | |||
// Use of this source code is governed by an MIT-style | |||
// license that can be found in the LICENSE file. | |||
package sqlite3 | |||
import ( | |||
"fmt" | |||
"strings" | |||
"testing" | |||
) | |||
// TestCryptEncoders to increase coverage | |||
func TestCryptEncoders(t *testing.T) { | |||
tests := []struct { | |||
enc string | |||
salt string | |||
expected string | |||
}{ | |||
{"sha1", "", "d033e22ae348aeb5660fc2140aec35850c4da997"}, | |||
{"sha256", "", "8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918"}, | |||
{"sha384", "", "9ca694a90285c034432c9550421b7b9dbd5c0f4b6673f05f6dbce58052ba20e4248041956ee8c9a2ec9f10290cdc0782"}, | |||
{"sha512", "", "c7ad44cbad762a5da0a452f9e854fdc1e0e7a52a38015f23f3eab1d80b931dd472634dfac71cd34ebc35d16ab7fb8a90c81f975113d6c7538dc69dd8de9077ec"}, | |||
{"ssha1", "salt", "9bc7aa55f08fdad935c3f8362d3f48bcf70eb280"}, | |||
{"ssha256", "salt", "f9a81477552594c79f2abc3fc099daa896a6e3a3590a55ffa392b6000412e80b"}, | |||
{"ssha384", "salt", "9ed776b477fcfc1b5e584989e8d770f5e17d98a7643546a63c2b07d4ab00f1348f6b8e73103d3a23554f727136e8c215"}, | |||
{"ssha512", "salt", "3c4a79782143337be4492be072abcfe979dd703c00541a8c39a0f3df4bab2029c050cf46fddc47090b5b04ac537b3e78189e3de16e601e859f95c51ac9f6dafb"}, | |||
} | |||
for _, e := range tests { | |||
var fn func(pass []byte, hash interface{}) []byte | |||
switch e.enc { | |||
case "sha1": | |||
fn = CryptEncoderSHA1 | |||
case "ssha1": | |||
fn = CryptEncoderSSHA1(e.salt) | |||
case "sha256": | |||
fn = CryptEncoderSHA256 | |||
case "ssha256": | |||
fn = CryptEncoderSSHA256(e.salt) | |||
case "sha384": | |||
fn = CryptEncoderSHA384 | |||
case "ssha384": | |||
fn = CryptEncoderSSHA384(e.salt) | |||
case "sha512": | |||
fn = CryptEncoderSHA512 | |||
case "ssha512": | |||
fn = CryptEncoderSSHA512(e.salt) | |||
} | |||
h := fn([]byte("admin"), nil) | |||
if strings.Compare(fmt.Sprintf("%x", h), e.expected) != 0 { | |||
t.Fatalf("Invalid %s hash: expected: %s; got: %x", strings.ToUpper(e.enc), e.expected, h) | |||
} | |||
} | |||
} |
@ -0,0 +1,119 @@ | |||
// 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 go1.13,cgo | |||
package sqlite3 | |||
import ( | |||
"context" | |||
"database/sql" | |||
"database/sql/driver" | |||
"errors" | |||
"os" | |||
"testing" | |||
) | |||
func TestBeginTxCancel(t *testing.T) { | |||
srcTempFilename := TempFilename(t) | |||
defer os.Remove(srcTempFilename) | |||
db, err := sql.Open("sqlite3", srcTempFilename) | |||
if err != nil { | |||
t.Fatal(err) | |||
} | |||
db.SetMaxOpenConns(10) | |||
db.SetMaxIdleConns(5) | |||
defer db.Close() | |||
initDatabase(t, db, 100) | |||
// create several go-routines to expose racy issue | |||
for i := 0; i < 1000; i++ { | |||
func() { | |||
ctx, cancel := context.WithCancel(context.Background()) | |||
conn, err := db.Conn(ctx) | |||
if err != nil { | |||
t.Fatal(err) | |||
} | |||
defer func() { | |||
if err := conn.Close(); err != nil { | |||
t.Error(err) | |||
} | |||
}() | |||
err = conn.Raw(func(driverConn interface{}) error { | |||
d, ok := driverConn.(driver.ConnBeginTx) | |||
if !ok { | |||
t.Fatal("unexpected: wrong type") | |||
} | |||
// checks that conn.Raw can be used to get *SQLiteConn | |||
if _, ok = driverConn.(*SQLiteConn); !ok { | |||
t.Fatalf("conn.Raw() driverConn type=%T, expected *SQLiteConn", driverConn) | |||
} | |||
go cancel() // make it cancel concurrently with exec("BEGIN"); | |||
tx, err := d.BeginTx(ctx, driver.TxOptions{}) | |||
switch err { | |||
case nil: | |||
switch err := tx.Rollback(); err { | |||
case nil, sql.ErrTxDone: | |||
default: | |||
return err | |||
} | |||
case context.Canceled: | |||
default: | |||
// must not fail with "cannot start a transaction within a transaction" | |||
return err | |||
} | |||
return nil | |||
}) | |||
if err != nil { | |||
t.Fatal(err) | |||
} | |||
}() | |||
} | |||
} | |||
func TestStmtReadonly(t *testing.T) { | |||
db, err := sql.Open("sqlite3", ":memory:") | |||
if err != nil { | |||
t.Fatal(err) | |||
} | |||
_, err = db.Exec("CREATE TABLE t (count INT)") | |||
if err != nil { | |||
t.Fatal(err) | |||
} | |||
isRO := func(query string) bool { | |||
c, err := db.Conn(context.Background()) | |||
if err != nil { | |||
return false | |||
} | |||
var ro bool | |||
c.Raw(func(dc interface{}) error { | |||
stmt, err := dc.(*SQLiteConn).Prepare(query) | |||
if err != nil { | |||
return err | |||
} | |||
if stmt == nil { | |||
return errors.New("stmt is nil") | |||
} | |||
ro = stmt.(*SQLiteStmt).Readonly() | |||
return nil | |||
}) | |||
return ro // On errors ro will remain false. | |||
} | |||
if !isRO(`select * from t`) { | |||
t.Error("select not seen as read-only") | |||
} | |||
if isRO(`insert into t values (1), (2)`) { | |||
t.Error("insert seen as read-only") | |||
} | |||
} |
@ -0,0 +1,54 @@ | |||
// 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 cgo | |||
// +build go1.8 | |||
package sqlite3 | |||
import ( | |||
"database/sql/driver" | |||
"context" | |||
) | |||
// Ping implement Pinger. | |||
func (c *SQLiteConn) Ping(ctx context.Context) error { | |||
if c.db == nil { | |||
// must be ErrBadConn for sql to close the database | |||
return driver.ErrBadConn | |||
} | |||
return nil | |||
} | |||
// QueryContext implement QueryerContext. | |||
func (c *SQLiteConn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) { | |||
return c.query(ctx, query, args) | |||
} | |||
// ExecContext implement ExecerContext. | |||
func (c *SQLiteConn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) { | |||
return c.exec(ctx, query, args) | |||
} | |||
// PrepareContext implement ConnPrepareContext. | |||
func (c *SQLiteConn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) { | |||
return c.prepare(ctx, query) | |||
} | |||
// BeginTx implement ConnBeginTx. | |||
func (c *SQLiteConn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) { | |||
return c.begin(ctx) | |||
} | |||
// QueryContext implement QueryerContext. | |||
func (s *SQLiteStmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) { | |||
return s.query(ctx, args) | |||
} | |||
// ExecContext implement ExecerContext. | |||
func (s *SQLiteStmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) { | |||
return s.exec(ctx, args) | |||
} |
@ -0,0 +1,503 @@ | |||
// 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 go1.8,cgo | |||
package sqlite3 | |||
import ( | |||
"context" | |||
"database/sql" | |||
"fmt" | |||
"io/ioutil" | |||
"math/rand" | |||
"os" | |||
"sync" | |||
"testing" | |||
"time" | |||
) | |||
func TestNamedParams(t *testing.T) { | |||
tempFilename := TempFilename(t) | |||
defer os.Remove(tempFilename) | |||
db, err := sql.Open("sqlite3", tempFilename) | |||
if err != nil { | |||
t.Fatal("Failed to open database:", err) | |||
} | |||
defer db.Close() | |||
_, err = db.Exec(` | |||
create table foo (id integer, name text, extra text); | |||
`) | |||
if err != nil { | |||
t.Error("Failed to call db.Query:", err) | |||
} | |||
_, err = db.Exec(`insert into foo(id, name, extra) values(:id, :name, :name)`, sql.Named("name", "foo"), sql.Named("id", 1)) | |||
if err != nil { | |||
t.Error("Failed to call db.Exec:", err) | |||
} | |||
row := db.QueryRow(`select id, extra from foo where id = :id and extra = :extra`, sql.Named("id", 1), sql.Named("extra", "foo")) | |||
if row == nil { | |||
t.Error("Failed to call db.QueryRow") | |||
} | |||
var id int | |||
var extra string | |||
err = row.Scan(&id, &extra) | |||
if err != nil { | |||
t.Error("Failed to db.Scan:", err) | |||
} | |||
if id != 1 || extra != "foo" { | |||
t.Error("Failed to db.QueryRow: not matched results") | |||
} | |||
} | |||
var ( | |||
testTableStatements = []string{ | |||
`DROP TABLE IF EXISTS test_table`, | |||
` | |||
CREATE TABLE IF NOT EXISTS test_table ( | |||
key1 VARCHAR(64) PRIMARY KEY, | |||
key_id VARCHAR(64) NOT NULL, | |||
key2 VARCHAR(64) NOT NULL, | |||
key3 VARCHAR(64) NOT NULL, | |||
key4 VARCHAR(64) NOT NULL, | |||
key5 VARCHAR(64) NOT NULL, | |||
key6 VARCHAR(64) NOT NULL, | |||
data BLOB NOT NULL | |||
);`, | |||
} | |||
letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" | |||
) | |||
func randStringBytes(n int) string { | |||
b := make([]byte, n) | |||
for i := range b { | |||
b[i] = letterBytes[rand.Intn(len(letterBytes))] | |||
} | |||
return string(b) | |||
} | |||
func initDatabase(t *testing.T, db *sql.DB, rowCount int64) { | |||
for _, query := range testTableStatements { | |||
_, err := db.Exec(query) | |||
if err != nil { | |||
t.Fatal(err) | |||
} | |||
} | |||
for i := int64(0); i < rowCount; i++ { | |||
query := `INSERT INTO test_table | |||
(key1, key_id, key2, key3, key4, key5, key6, data) | |||
VALUES | |||
(?, ?, ?, ?, ?, ?, ?, ?);` | |||
args := []interface{}{ | |||
randStringBytes(50), | |||
fmt.Sprint(i), | |||
randStringBytes(50), | |||
randStringBytes(50), | |||
randStringBytes(50), | |||
randStringBytes(50), | |||
randStringBytes(50), | |||
randStringBytes(50), | |||
randStringBytes(2048), | |||
} | |||
_, err := db.Exec(query, args...) | |||
if err != nil { | |||
t.Fatal(err) | |||
} | |||
} | |||
} | |||
func TestShortTimeout(t *testing.T) { | |||
srcTempFilename := TempFilename(t) | |||
defer os.Remove(srcTempFilename) | |||
db, err := sql.Open("sqlite3", srcTempFilename) | |||
if err != nil { | |||
t.Fatal(err) | |||
} | |||
defer db.Close() | |||
initDatabase(t, db, 100) | |||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Microsecond) | |||
defer cancel() | |||
query := `SELECT key1, key_id, key2, key3, key4, key5, key6, data | |||
FROM test_table | |||
ORDER BY key2 ASC` | |||
_, err = db.QueryContext(ctx, query) | |||
if err != nil && err != context.DeadlineExceeded { | |||
t.Fatal(err) | |||
} | |||
if ctx.Err() != nil && ctx.Err() != context.DeadlineExceeded { | |||
t.Fatal(ctx.Err()) | |||
} | |||
} | |||
func TestExecContextCancel(t *testing.T) { | |||
srcTempFilename := TempFilename(t) | |||
defer os.Remove(srcTempFilename) | |||
db, err := sql.Open("sqlite3", srcTempFilename) | |||
if err != nil { | |||
t.Fatal(err) | |||
} | |||
defer db.Close() | |||
ts := time.Now() | |||
initDatabase(t, db, 1000) | |||
spent := time.Since(ts) | |||
const minTestTime = 100 * time.Millisecond | |||
if spent < minTestTime { | |||
t.Skipf("test will be too racy (spent=%s < min=%s) as ExecContext below will be too fast.", | |||
spent.String(), minTestTime.String(), | |||
) | |||
} | |||
// expected to be extremely slow query | |||
q := ` | |||
INSERT INTO test_table (key1, key_id, key2, key3, key4, key5, key6, data) | |||
SELECT t1.key1 || t2.key1, t1.key_id || t2.key_id, t1.key2 || t2.key2, t1.key3 || t2.key3, t1.key4 || t2.key4, t1.key5 || t2.key5, t1.key6 || t2.key6, t1.data || t2.data | |||
FROM test_table t1 LEFT OUTER JOIN test_table t2` | |||
// expect query above take ~ same time as setup above | |||
// This is racy: the context must be valid so sql/db.ExecContext calls the sqlite3 driver. | |||
// It starts the query, the context expires, then calls sqlite3_interrupt | |||
ctx, cancel := context.WithTimeout(context.Background(), minTestTime/2) | |||
defer cancel() | |||
ts = time.Now() | |||
r, err := db.ExecContext(ctx, q) | |||
// racy check | |||
if r != nil { | |||
n, err := r.RowsAffected() | |||
t.Logf("query should not have succeeded: rows=%d; err=%v; duration=%s", | |||
n, err, time.Since(ts).String()) | |||
} | |||
if err != context.DeadlineExceeded { | |||
t.Fatal(err, ctx.Err()) | |||
} | |||
} | |||
func TestQueryRowContextCancel(t *testing.T) { | |||
srcTempFilename := TempFilename(t) | |||
defer os.Remove(srcTempFilename) | |||
db, err := sql.Open("sqlite3", srcTempFilename) | |||
if err != nil { | |||
t.Fatal(err) | |||
} | |||
defer db.Close() | |||
initDatabase(t, db, 100) | |||
const query = `SELECT key_id FROM test_table ORDER BY key2 ASC` | |||
var keyID string | |||
unexpectedErrors := make(map[string]int) | |||
for i := 0; i < 10000; i++ { | |||
ctx, cancel := context.WithCancel(context.Background()) | |||
row := db.QueryRowContext(ctx, query) | |||
cancel() | |||
// it is fine to get "nil" as context cancellation can be handled with delay | |||
if err := row.Scan(&keyID); err != nil && err != context.Canceled { | |||
if err.Error() == "sql: Rows are closed" { | |||
// see https://github.com/golang/go/issues/24431 | |||
// fixed in 1.11.1 to properly return context error | |||
continue | |||
} | |||
unexpectedErrors[err.Error()]++ | |||
} | |||
} | |||
for errText, count := range unexpectedErrors { | |||
t.Error(errText, count) | |||
} | |||
} | |||
func TestQueryRowContextCancelParallel(t *testing.T) { | |||
srcTempFilename := TempFilename(t) | |||
defer os.Remove(srcTempFilename) | |||
db, err := sql.Open("sqlite3", srcTempFilename) | |||
if err != nil { | |||
t.Fatal(err) | |||
} | |||
db.SetMaxOpenConns(10) | |||
db.SetMaxIdleConns(5) | |||
defer db.Close() | |||
initDatabase(t, db, 100) | |||
const query = `SELECT key_id FROM test_table ORDER BY key2 ASC` | |||
wg := sync.WaitGroup{} | |||
defer wg.Wait() | |||
testCtx, cancel := context.WithCancel(context.Background()) | |||
defer cancel() | |||
for i := 0; i < 10; i++ { | |||
wg.Add(1) | |||
go func() { | |||
defer wg.Done() | |||
var keyID string | |||
for { | |||
select { | |||
case <-testCtx.Done(): | |||
return | |||
default: | |||
} | |||
ctx, cancel := context.WithCancel(context.Background()) | |||
row := db.QueryRowContext(ctx, query) | |||
cancel() | |||
_ = row.Scan(&keyID) // see TestQueryRowContextCancel | |||
} | |||
}() | |||
} | |||
var keyID string | |||
for i := 0; i < 10000; i++ { | |||
// note that testCtx is not cancelled during query execution | |||
row := db.QueryRowContext(testCtx, query) | |||
if err := row.Scan(&keyID); err != nil { | |||
t.Fatal(i, err) | |||
} | |||
} | |||
} | |||
func TestExecCancel(t *testing.T) { | |||
db, err := sql.Open("sqlite3", ":memory:") | |||
if err != nil { | |||
t.Fatal(err) | |||
} | |||
defer db.Close() | |||
if _, err = db.Exec("create table foo (id integer primary key)"); err != nil { | |||
t.Fatal(err) | |||
} | |||
for n := 0; n < 100; n++ { | |||
ctx, cancel := context.WithCancel(context.Background()) | |||
_, err = db.ExecContext(ctx, "insert into foo (id) values (?)", n) | |||
cancel() | |||
if err != nil { | |||
t.Fatal(err) | |||
} | |||
} | |||
} | |||
func doTestOpenContext(t *testing.T, option string) (string, error) { | |||
tempFilename := TempFilename(t) | |||
url := tempFilename + option | |||
defer func() { | |||
err := os.Remove(tempFilename) | |||
if err != nil { | |||
t.Error("temp file remove error:", err) | |||
} | |||
}() | |||
db, err := sql.Open("sqlite3", url) | |||
if err != nil { | |||
return "Failed to open database:", err | |||
} | |||
defer func() { | |||
err = db.Close() | |||
if err != nil { | |||
t.Error("db close error:", err) | |||
} | |||
}() | |||
ctx, cancel := context.WithTimeout(context.Background(), 55*time.Second) | |||
err = db.PingContext(ctx) | |||
cancel() | |||
if err != nil { | |||
return "ping error:", err | |||
} | |||
ctx, cancel = context.WithTimeout(context.Background(), 55*time.Second) | |||
_, err = db.ExecContext(ctx, "drop table foo") | |||
cancel() | |||
ctx, cancel = context.WithTimeout(context.Background(), 55*time.Second) | |||
_, err = db.ExecContext(ctx, "create table foo (id integer)") | |||
cancel() | |||
if err != nil { | |||
return "Failed to create table:", err | |||
} | |||
if stat, err := os.Stat(tempFilename); err != nil || stat.IsDir() { | |||
return "Failed to create ./foo.db", nil | |||
} | |||
return "", nil | |||
} | |||
func TestOpenContext(t *testing.T) { | |||
cases := map[string]bool{ | |||
"": true, | |||
"?_txlock=immediate": true, | |||
"?_txlock=deferred": true, | |||
"?_txlock=exclusive": true, | |||
"?_txlock=bogus": false, | |||
} | |||
for option, expectedPass := range cases { | |||
result, err := doTestOpenContext(t, option) | |||
if result == "" { | |||
if !expectedPass { | |||
errmsg := fmt.Sprintf("_txlock error not caught at dbOpen with option: %s", option) | |||
t.Fatal(errmsg) | |||
} | |||
} else if expectedPass { | |||
if err == nil { | |||
t.Fatal(result) | |||
} else { | |||
t.Fatal(result, err) | |||
} | |||
} | |||
} | |||
} | |||
func TestFileCopyTruncate(t *testing.T) { | |||
var err error | |||
tempFilename := TempFilename(t) | |||
defer func() { | |||
err = os.Remove(tempFilename) | |||
if err != nil { | |||
t.Error("temp file remove error:", err) | |||
} | |||
}() | |||
db, err := sql.Open("sqlite3", tempFilename) | |||
if err != nil { | |||
t.Fatal("open error:", err) | |||
} | |||
defer func() { | |||
err = db.Close() | |||
if err != nil { | |||
t.Error("db close error:", err) | |||
} | |||
}() | |||
ctx, cancel := context.WithTimeout(context.Background(), 55*time.Second) | |||
err = db.PingContext(ctx) | |||
cancel() | |||
if err != nil { | |||
t.Fatal("ping error:", err) | |||
} | |||
ctx, cancel = context.WithTimeout(context.Background(), 55*time.Second) | |||
_, err = db.ExecContext(ctx, "drop table foo") | |||
cancel() | |||
ctx, cancel = context.WithTimeout(context.Background(), 55*time.Second) | |||
_, err = db.ExecContext(ctx, "create table foo (id integer)") | |||
cancel() | |||
if err != nil { | |||
t.Fatal("create table error:", err) | |||
} | |||
// copy db to new file | |||
var data []byte | |||
data, err = ioutil.ReadFile(tempFilename) | |||
if err != nil { | |||
t.Fatal("read file error:", err) | |||
} | |||
var f *os.File | |||
f, err = os.Create(tempFilename + "-db-copy") | |||
if err != nil { | |||
t.Fatal("create file error:", err) | |||
} | |||
defer func() { | |||
err = os.Remove(tempFilename + "-db-copy") | |||
if err != nil { | |||
t.Error("temp file moved remove error:", err) | |||
} | |||
}() | |||
_, err = f.Write(data) | |||
if err != nil { | |||
f.Close() | |||
t.Fatal("write file error:", err) | |||
} | |||
err = f.Close() | |||
if err != nil { | |||
t.Fatal("close file error:", err) | |||
} | |||
// truncate current db file | |||
f, err = os.OpenFile(tempFilename, os.O_WRONLY|os.O_TRUNC, 0666) | |||
if err != nil { | |||
t.Fatal("open file error:", err) | |||
} | |||
err = f.Close() | |||
if err != nil { | |||
t.Fatal("close file error:", err) | |||
} | |||
// test db after file truncate | |||
ctx, cancel = context.WithTimeout(context.Background(), 55*time.Second) | |||
err = db.PingContext(ctx) | |||
cancel() | |||
if err != nil { | |||
t.Fatal("ping error:", err) | |||
} | |||
ctx, cancel = context.WithTimeout(context.Background(), 55*time.Second) | |||
_, err = db.ExecContext(ctx, "drop table foo") | |||
cancel() | |||
if err == nil { | |||
t.Fatal("drop table no error") | |||
} | |||
ctx, cancel = context.WithTimeout(context.Background(), 55*time.Second) | |||
_, err = db.ExecContext(ctx, "create table foo (id integer)") | |||
cancel() | |||
if err != nil { | |||
t.Fatal("create table error:", err) | |||
} | |||
err = db.Close() | |||
if err != nil { | |||
t.Error("db close error:", err) | |||
} | |||
// test copied file | |||
db, err = sql.Open("sqlite3", tempFilename+"-db-copy") | |||
if err != nil { | |||
t.Fatal("open error:", err) | |||
} | |||
defer func() { | |||
err = db.Close() | |||
if err != nil { | |||
t.Error("db close error:", err) | |||
} | |||
}() | |||
ctx, cancel = context.WithTimeout(context.Background(), 55*time.Second) | |||
err = db.PingContext(ctx) | |||
cancel() | |||
if err != nil { | |||
t.Fatal("ping error:", err) | |||
} | |||
ctx, cancel = context.WithTimeout(context.Background(), 55*time.Second) | |||
_, err = db.ExecContext(ctx, "drop table foo") | |||
cancel() | |||
if err != nil { | |||
t.Fatal("drop table error:", err) | |||
} | |||
ctx, cancel = context.WithTimeout(context.Background(), 55*time.Second) | |||
_, err = db.ExecContext(ctx, "create table foo (id integer)") | |||
cancel() | |||
if err != nil { | |||
t.Fatal("create table error:", err) | |||
} | |||
} |
@ -0,0 +1,21 @@ | |||
// 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 libsqlite3 | |||
package sqlite3 | |||
/* | |||
#cgo CFLAGS: -DUSE_LIBSQLITE3 | |||
#cgo linux LDFLAGS: -lsqlite3 | |||
#cgo darwin,amd64 LDFLAGS: -L/usr/local/opt/sqlite/lib -lsqlite3 | |||
#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 solaris LDFLAGS: -lsqlite3 | |||
#cgo windows LDFLAGS: -lsqlite3 | |||
*/ | |||
import "C" |
@ -0,0 +1,84 @@ | |||
// 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_omit_load_extension | |||
package sqlite3 | |||
/* | |||
#ifndef USE_LIBSQLITE3 | |||
#include "sqlite3-binding.h" | |||
#else | |||
#include <sqlite3.h> | |||
#endif | |||
#include <stdlib.h> | |||
*/ | |||
import "C" | |||
import ( | |||
"errors" | |||
"unsafe" | |||
) | |||
func (c *SQLiteConn) loadExtensions(extensions []string) error { | |||
rv := C.sqlite3_enable_load_extension(c.db, 1) | |||
if rv != C.SQLITE_OK { | |||
return errors.New(C.GoString(C.sqlite3_errmsg(c.db))) | |||
} | |||
for _, extension := range extensions { | |||
if err := c.loadExtension(extension, nil); err != nil { | |||
C.sqlite3_enable_load_extension(c.db, 0) | |||
return err | |||
} | |||
} | |||
rv = C.sqlite3_enable_load_extension(c.db, 0) | |||
if rv != C.SQLITE_OK { | |||
return errors.New(C.GoString(C.sqlite3_errmsg(c.db))) | |||
} | |||
return nil | |||
} | |||
// LoadExtension load the sqlite3 extension. | |||
func (c *SQLiteConn) LoadExtension(lib string, entry string) error { | |||
rv := C.sqlite3_enable_load_extension(c.db, 1) | |||
if rv != C.SQLITE_OK { | |||
return errors.New(C.GoString(C.sqlite3_errmsg(c.db))) | |||
} | |||
if err := c.loadExtension(lib, &entry); err != nil { | |||
C.sqlite3_enable_load_extension(c.db, 0) | |||
return err | |||
} | |||
rv = C.sqlite3_enable_load_extension(c.db, 0) | |||
if rv != C.SQLITE_OK { | |||
return errors.New(C.GoString(C.sqlite3_errmsg(c.db))) | |||
} | |||
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 | |||
} |
@ -0,0 +1,24 @@ | |||
// 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_omit_load_extension | |||
package sqlite3 | |||
/* | |||
#cgo CFLAGS: -DSQLITE_OMIT_LOAD_EXTENSION | |||
*/ | |||
import "C" | |||
import ( | |||
"errors" | |||
) | |||
func (c *SQLiteConn) loadExtensions(extensions []string) error { | |||
return errors.New("Extensions have been disabled for static builds") | |||
} | |||
func (c *SQLiteConn) LoadExtension(lib string, entry string) error { | |||
return errors.New("Extensions have been disabled for static builds") | |||
} |
@ -0,0 +1,63 @@ | |||
// 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_omit_load_extension | |||
package sqlite3 | |||
import ( | |||
"database/sql" | |||
"testing" | |||
) | |||
func TestExtensionsError(t *testing.T) { | |||
sql.Register("sqlite3_TestExtensionsError", | |||
&SQLiteDriver{ | |||
Extensions: []string{ | |||
"foobar", | |||
}, | |||
}, | |||
) | |||
db, err := sql.Open("sqlite3_TestExtensionsError", ":memory:") | |||
if err != nil { | |||
t.Fatal(err) | |||
} | |||
defer db.Close() | |||
err = db.Ping() | |||
if err == nil { | |||
t.Fatal("expected error loading non-existent extension") | |||
} | |||
if err.Error() == "not an error" { | |||
t.Fatal("expected error from sqlite3_enable_load_extension to be returned") | |||
} | |||
} | |||
func TestLoadExtensionError(t *testing.T) { | |||
sql.Register("sqlite3_TestLoadExtensionError", | |||
&SQLiteDriver{ | |||
ConnectHook: func(c *SQLiteConn) error { | |||
return c.LoadExtension("foobar", "") | |||
}, | |||
}, | |||
) | |||
db, err := sql.Open("sqlite3_TestLoadExtensionError", ":memory:") | |||
if err != nil { | |||
t.Fatal(err) | |||
} | |||
defer db.Close() | |||
err = db.Ping() | |||
if err == nil { | |||
t.Fatal("expected error loading non-existent extension") | |||
} | |||
if err.Error() == "not an error" { | |||
t.Fatal("expected error from sqlite3_enable_load_extension to be returned") | |||
} | |||
} |
@ -0,0 +1,15 @@ | |||
// Copyright (C) 2019 Yasuhiro Matsumoto <mattn.jp@gmail.com>. | |||
// Copyright (C) 2018 G.J.R. Timmer <gjr.timmer@gmail.com>. | |||
// | |||
// Use of this source code is governed by an MIT-style | |||
// license that can be found in the LICENSE file. | |||
// +build sqlite_allow_uri_authority | |||
package sqlite3 | |||
/* | |||
#cgo CFLAGS: -DSQLITE_ALLOW_URI_AUTHORITY | |||
#cgo LDFLAGS: -lm | |||
*/ | |||
import "C" |
@ -0,0 +1,16 @@ | |||
// Copyright (C) 2019 Yasuhiro Matsumoto <mattn.jp@gmail.com>. | |||
// Copyright (C) 2018 G.J.R. Timmer <gjr.timmer@gmail.com>. | |||
// | |||
// Use of this source code is governed by an MIT-style | |||
// license that can be found in the LICENSE file. | |||
// +build !windows | |||
// +build sqlite_app_armor | |||
package sqlite3 | |||
/* | |||
#cgo CFLAGS: -DSQLITE_ENABLE_API_ARMOR | |||
#cgo LDFLAGS: -lm | |||
*/ | |||
import "C" |
@ -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))) | |||
} |
@ -0,0 +1,39 @@ | |||
// +build sqlite_column_metadata | |||
package sqlite3 | |||
import "testing" | |||
func TestColumnTableName(t *testing.T) { | |||
d := SQLiteDriver{} | |||
conn, err := d.Open(":memory:") | |||
if err != nil { | |||
t.Fatal("failed to get database connection:", err) | |||
} | |||
defer conn.Close() | |||
sqlite3conn := conn.(*SQLiteConn) | |||
_, err = sqlite3conn.Exec(`CREATE TABLE foo (name string)`, nil) | |||
if err != nil { | |||
t.Fatal("Failed to create table:", err) | |||
} | |||
_, err = sqlite3conn.Exec(`CREATE TABLE bar (name string)`, nil) | |||
if err != nil { | |||
t.Fatal("Failed to create table:", err) | |||
} | |||
stmt, err := sqlite3conn.Prepare(`SELECT * FROM foo JOIN bar ON foo.name = bar.name`) | |||
if err != nil { | |||
t.Fatal(err) | |||
} | |||
if exp, got := "foo", stmt.(*SQLiteStmt).ColumnTableName(0); exp != got { | |||
t.Fatalf("Incorrect table name returned expected: %s, got: %s", exp, got) | |||
} | |||
if exp, got := "bar", stmt.(*SQLiteStmt).ColumnTableName(1); exp != got { | |||
t.Fatalf("Incorrect table name returned expected: %s, got: %s", exp, got) | |||
} | |||
if exp, got := "", stmt.(*SQLiteStmt).ColumnTableName(2); exp != got { | |||
t.Fatalf("Incorrect table name returned expected: %s, got: %s", exp, got) | |||
} | |||
} |
@ -0,0 +1,15 @@ | |||
// Copyright (C) 2019 Yasuhiro Matsumoto <mattn.jp@gmail.com>. | |||
// Copyright (C) 2018 G.J.R. Timmer <gjr.timmer@gmail.com>. | |||
// | |||
// Use of this source code is governed by an MIT-style | |||
// license that can be found in the LICENSE file. | |||
// +build sqlite_foreign_keys | |||
package sqlite3 | |||
/* | |||
#cgo CFLAGS: -DSQLITE_DEFAULT_FOREIGN_KEYS=1 | |||
#cgo LDFLAGS: -lm | |||
*/ | |||
import "C" |
@ -0,0 +1,132 @@ | |||
// 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 cgo | |||
package sqlite3 | |||
import ( | |||
"database/sql" | |||
"os" | |||
"testing" | |||
) | |||
func TestFTS3(t *testing.T) { | |||
tempFilename := TempFilename(t) | |||
defer os.Remove(tempFilename) | |||
db, err := sql.Open("sqlite3", tempFilename) | |||
if err != nil { | |||
t.Fatal("Failed to open database:", err) | |||
} | |||
defer db.Close() | |||
_, err = db.Exec("DROP TABLE foo") | |||
_, err = db.Exec("CREATE VIRTUAL TABLE foo USING fts3(id INTEGER PRIMARY KEY, value TEXT)") | |||
if err != nil { | |||
t.Fatal("Failed to create table:", err) | |||
} | |||
_, err = db.Exec("INSERT INTO foo(id, value) VALUES(?, ?)", 1, `今日の 晩御飯は 天麩羅よ`) | |||
if err != nil { | |||
t.Fatal("Failed to insert value:", err) | |||
} | |||
_, err = db.Exec("INSERT INTO foo(id, value) VALUES(?, ?)", 2, `今日は いい 天気だ`) | |||
if err != nil { | |||
t.Fatal("Failed to insert value:", err) | |||
} | |||
rows, err := db.Query("SELECT id, value FROM foo WHERE value MATCH '今日* 天*'") | |||
if err != nil { | |||
t.Fatal("Unable to query foo table:", err) | |||
} | |||
defer rows.Close() | |||
for rows.Next() { | |||
var id int | |||
var value string | |||
if err := rows.Scan(&id, &value); err != nil { | |||
t.Error("Unable to scan results:", err) | |||
continue | |||
} | |||
if id == 1 && value != `今日の 晩御飯は 天麩羅よ` { | |||
t.Error("Value for id 1 should be `今日の 晩御飯は 天麩羅よ`, but:", value) | |||
} else if id == 2 && value != `今日は いい 天気だ` { | |||
t.Error("Value for id 2 should be `今日は いい 天気だ`, but:", value) | |||
} | |||
} | |||
rows, err = db.Query("SELECT value FROM foo WHERE value MATCH '今日* 天麩羅*'") | |||
if err != nil { | |||
t.Fatal("Unable to query foo table:", err) | |||
} | |||
defer rows.Close() | |||
var value string | |||
if !rows.Next() { | |||
t.Fatal("Result should be only one") | |||
} | |||
if err := rows.Scan(&value); err != nil { | |||
t.Fatal("Unable to scan results:", err) | |||
} | |||
if value != `今日の 晩御飯は 天麩羅よ` { | |||
t.Fatal("Value should be `今日の 晩御飯は 天麩羅よ`, but:", value) | |||
} | |||
if rows.Next() { | |||
t.Fatal("Result should be only one") | |||
} | |||
} | |||
func TestFTS4(t *testing.T) { | |||
tempFilename := TempFilename(t) | |||
defer os.Remove(tempFilename) | |||
db, err := sql.Open("sqlite3", tempFilename) | |||
if err != nil { | |||
t.Fatal("Failed to open database:", err) | |||
} | |||
defer db.Close() | |||
_, err = db.Exec("DROP TABLE foo") | |||
_, err = db.Exec("CREATE VIRTUAL TABLE foo USING fts4(tokenize=unicode61, id INTEGER PRIMARY KEY, value TEXT)") | |||
switch { | |||
case err != nil && err.Error() == "unknown tokenizer: unicode61": | |||
t.Skip("FTS4 not supported") | |||
case err != nil: | |||
t.Fatal("Failed to create table:", err) | |||
} | |||
_, err = db.Exec("INSERT INTO foo(id, value) VALUES(?, ?)", 1, `février`) | |||
if err != nil { | |||
t.Fatal("Failed to insert value:", err) | |||
} | |||
rows, err := db.Query("SELECT value FROM foo WHERE value MATCH 'fevrier'") | |||
if err != nil { | |||
t.Fatal("Unable to query foo table:", err) | |||
} | |||
defer rows.Close() | |||
var value string | |||
if !rows.Next() { | |||
t.Fatal("Result should be only one") | |||
} | |||
if err := rows.Scan(&value); err != nil { | |||
t.Fatal("Unable to scan results:", err) | |||
} | |||
if value != `février` { | |||
t.Fatal("Value should be `février`, but:", value) | |||
} | |||
if rows.Next() { | |||
t.Fatal("Result should be only one") | |||
} | |||
} |
@ -0,0 +1,14 @@ | |||
// 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_fts5 fts5 | |||
package sqlite3 | |||
/* | |||
#cgo CFLAGS: -DSQLITE_ENABLE_FTS5 | |||
#cgo LDFLAGS: -lm | |||
*/ | |||
import "C" |
@ -0,0 +1,19 @@ | |||
// 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_icu icu | |||
package sqlite3 | |||
/* | |||
#cgo LDFLAGS: -licuuc -licui18n | |||
#cgo CFLAGS: -DSQLITE_ENABLE_ICU | |||
#cgo darwin,amd64 CFLAGS: -I/usr/local/opt/icu4c/include | |||
#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 | |||
*/ | |||
import "C" |
@ -0,0 +1,15 @@ | |||
// Copyright (C) 2019 Yasuhiro Matsumoto <mattn.jp@gmail.com>. | |||
// Copyright (C) 2018 G.J.R. Timmer <gjr.timmer@gmail.com>. | |||
// Use of this source code is governed by an MIT-style | |||
// license that can be found in the LICENSE file. | |||
// +build sqlite_introspect | |||
package sqlite3 | |||
/* | |||
#cgo CFLAGS: -DSQLITE_INTROSPECTION_PRAGMAS | |||
#cgo LDFLAGS: -lm | |||
*/ | |||
import "C" |
@ -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" |
@ -0,0 +1,29 @@ | |||
// +build sqlite_math_functions | |||
package sqlite3 | |||
import ( | |||
"database/sql" | |||
"testing" | |||
) | |||
func TestMathFunctions(t *testing.T) { | |||
db, err := sql.Open("sqlite3", ":memory:") | |||
if err != nil { | |||
t.Fatal("Failed to open database:", err) | |||
} | |||
defer db.Close() | |||
queries := []string{ | |||
`SELECT acos(1)`, | |||
`SELECT log(10, 100)`, | |||
`SELECT power(2, 2)`, | |||
} | |||
for _, query := range queries { | |||
var result float64 | |||
if err := db.QueryRow(query).Scan(&result); err != nil { | |||
t.Errorf("invoking math function query %q: %v", query, err) | |||
} | |||
} | |||
} |
@ -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" |
@ -0,0 +1,20 @@ | |||
// Copyright (C) 2019 G.J.R. Timmer <gjr.timmer@gmail.com>. | |||
// Copyright (C) 2018 segment.com <friends@segment.com> | |||
// | |||
// Use of this source code is governed by an MIT-style | |||
// license that can be found in the LICENSE file. | |||
// +build cgo | |||
package sqlite3 | |||
// SQLitePreUpdateData represents all of the data available during a | |||
// pre-update hook call. | |||
type SQLitePreUpdateData struct { | |||
Conn *SQLiteConn | |||
Op int | |||
DatabaseName string | |||
TableName string | |||
OldRowID int64 | |||
NewRowID int64 | |||
} |
@ -0,0 +1,112 @@ | |||
// Copyright (C) 2019 G.J.R. Timmer <gjr.timmer@gmail.com>. | |||
// Copyright (C) 2018 segment.com <friends@segment.com> | |||
// | |||
// Use of this source code is governed by an MIT-style | |||
// license that can be found in the LICENSE file. | |||
// +build sqlite_preupdate_hook | |||
package sqlite3 | |||
/* | |||
#cgo CFLAGS: -DSQLITE_ENABLE_PREUPDATE_HOOK | |||
#cgo LDFLAGS: -lm | |||
#ifndef USE_LIBSQLITE3 | |||
#include "sqlite3-binding.h" | |||
#else | |||
#include <sqlite3.h> | |||
#endif | |||
#include <stdlib.h> | |||
#include <string.h> | |||
void preUpdateHookTrampoline(void*, sqlite3 *, int, char *, char *, sqlite3_int64, sqlite3_int64); | |||
*/ | |||
import "C" | |||
import ( | |||
"errors" | |||
"unsafe" | |||
) | |||
// RegisterPreUpdateHook sets the pre-update hook for a connection. | |||
// | |||
// The callback is passed a SQLitePreUpdateData struct with the data for | |||
// the update, as well as methods for fetching copies of impacted data. | |||
// | |||
// 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 | |||
// without creating a new one. | |||
func (c *SQLiteConn) RegisterPreUpdateHook(callback func(SQLitePreUpdateData)) { | |||
if callback == nil { | |||
C.sqlite3_preupdate_hook(c.db, nil, nil) | |||
} else { | |||
C.sqlite3_preupdate_hook(c.db, (*[0]byte)(unsafe.Pointer(C.preUpdateHookTrampoline)), unsafe.Pointer(newHandle(c, callback))) | |||
} | |||
} | |||
// Depth returns the source path of the write, see sqlite3_preupdate_depth() | |||
func (d *SQLitePreUpdateData) Depth() int { | |||
return int(C.sqlite3_preupdate_depth(d.Conn.db)) | |||
} | |||
// Count returns the number of columns in the row | |||
func (d *SQLitePreUpdateData) Count() int { | |||
return int(C.sqlite3_preupdate_count(d.Conn.db)) | |||
} | |||
func (d *SQLitePreUpdateData) row(dest []interface{}, new bool) error { | |||
for i := 0; i < d.Count() && i < len(dest); i++ { | |||
var val *C.sqlite3_value | |||
var src interface{} | |||
// Initially I tried making this just a function pointer argument, but | |||
// it's absurdly complicated to pass C function pointers. | |||
if new { | |||
C.sqlite3_preupdate_new(d.Conn.db, C.int(i), &val) | |||
} else { | |||
C.sqlite3_preupdate_old(d.Conn.db, C.int(i), &val) | |||
} | |||
switch C.sqlite3_value_type(val) { | |||
case C.SQLITE_INTEGER: | |||
src = int64(C.sqlite3_value_int64(val)) | |||
case C.SQLITE_FLOAT: | |||
src = float64(C.sqlite3_value_double(val)) | |||
case C.SQLITE_BLOB: | |||
len := C.sqlite3_value_bytes(val) | |||
blobptr := C.sqlite3_value_blob(val) | |||
src = C.GoBytes(blobptr, len) | |||
case C.SQLITE_TEXT: | |||
len := C.sqlite3_value_bytes(val) | |||
cstrptr := unsafe.Pointer(C.sqlite3_value_text(val)) | |||
src = C.GoBytes(cstrptr, len) | |||
case C.SQLITE_NULL: | |||
src = nil | |||
} | |||
err := convertAssign(&dest[i], src) | |||
if err != nil { | |||
return err | |||
} | |||
} | |||
return nil | |||
} | |||
// Old populates dest with the row data to be replaced. This works similar to | |||
// database/sql's Rows.Scan() | |||
func (d *SQLitePreUpdateData) Old(dest ...interface{}) error { | |||
if d.Op == SQLITE_INSERT { | |||
return errors.New("There is no old row for INSERT operations") | |||
} | |||
return d.row(dest, false) | |||
} | |||
// New populates dest with the replacement row data. This works similar to | |||
// database/sql's Rows.Scan() | |||
func (d *SQLitePreUpdateData) New(dest ...interface{}) error { | |||
if d.Op == SQLITE_DELETE { | |||
return errors.New("There is no new row for DELETE operations") | |||
} | |||
return d.row(dest, true) | |||
} |
@ -0,0 +1,128 @@ | |||
// Copyright (C) 2019 G.J.R. Timmer <gjr.timmer@gmail.com>. | |||
// Copyright (C) 2018 segment.com <friends@segment.com> | |||
// | |||
// Use of this source code is governed by an MIT-style | |||
// license that can be found in the LICENSE file. | |||
// +build sqlite_preupdate_hook | |||
package sqlite3 | |||
import ( | |||
"database/sql" | |||
"testing" | |||
) | |||
type preUpdateHookDataForTest struct { | |||
databaseName string | |||
tableName string | |||
count int | |||
op int | |||
oldRow []interface{} | |||
newRow []interface{} | |||
} | |||
func TestPreUpdateHook(t *testing.T) { | |||
var events []preUpdateHookDataForTest | |||
sql.Register("sqlite3_PreUpdateHook", &SQLiteDriver{ | |||
ConnectHook: func(conn *SQLiteConn) error { | |||
conn.RegisterPreUpdateHook(func(data SQLitePreUpdateData) { | |||
eval := -1 | |||
oldRow := []interface{}{eval} | |||
if data.Op != SQLITE_INSERT { | |||
err := data.Old(oldRow...) | |||
if err != nil { | |||
t.Fatalf("Unexpected error calling SQLitePreUpdateData.Old: %v", err) | |||
} | |||
} | |||
eval2 := -1 | |||
newRow := []interface{}{eval2} | |||
if data.Op != SQLITE_DELETE { | |||
err := data.New(newRow...) | |||
if err != nil { | |||
t.Fatalf("Unexpected error calling SQLitePreUpdateData.New: %v", err) | |||
} | |||
} | |||
// tests dest bound checks in loop | |||
var tooSmallRow []interface{} | |||
if data.Op != SQLITE_INSERT { | |||
err := data.Old(tooSmallRow...) | |||
if err != nil { | |||
t.Fatalf("Unexpected error calling SQLitePreUpdateData.Old: %v", err) | |||
} | |||
if len(tooSmallRow) != 0 { | |||
t.Errorf("Expected tooSmallRow to be empty, got: %v", tooSmallRow) | |||
} | |||
} | |||
events = append(events, preUpdateHookDataForTest{ | |||
databaseName: data.DatabaseName, | |||
tableName: data.TableName, | |||
count: data.Count(), | |||
op: data.Op, | |||
oldRow: oldRow, | |||
newRow: newRow, | |||
}) | |||
}) | |||
return nil | |||
}, | |||
}) | |||
db, err := sql.Open("sqlite3_PreUpdateHook", ":memory:") | |||
if err != nil { | |||
t.Fatal("Failed to open database:", err) | |||
} | |||
defer db.Close() | |||
statements := []string{ | |||
"create table foo (id integer primary key)", | |||
"insert into foo values (9)", | |||
"update foo set id = 99 where id = 9", | |||
"delete from foo where id = 99", | |||
} | |||
for _, statement := range statements { | |||
_, err = db.Exec(statement) | |||
if err != nil { | |||
t.Fatalf("Unable to prepare test data [%v]: %v", statement, err) | |||
} | |||
} | |||
if len(events) != 3 { | |||
t.Errorf("Events should be 3 entries, got: %d", len(events)) | |||
} | |||
if events[0].op != SQLITE_INSERT { | |||
t.Errorf("Op isn't as expected: %v", events[0].op) | |||
} | |||
if events[1].op != SQLITE_UPDATE { | |||
t.Errorf("Op isn't as expected: %v", events[1].op) | |||
} | |||
if events[1].count != 1 { | |||
t.Errorf("Expected event row 1 to have 1 column, had: %v", events[1].count) | |||
} | |||
newRow_0_0 := events[0].newRow[0].(int64) | |||
if newRow_0_0 != 9 { | |||
t.Errorf("Expected event row 0 new column 0 to be == 9, got: %v", newRow_0_0) | |||
} | |||
oldRow_1_0 := events[1].oldRow[0].(int64) | |||
if oldRow_1_0 != 9 { | |||
t.Errorf("Expected event row 1 old column 0 to be == 9, got: %v", oldRow_1_0) | |||
} | |||
newRow_1_0 := events[1].newRow[0].(int64) | |||
if newRow_1_0 != 99 { | |||
t.Errorf("Expected event row 1 new column 0 to be == 99, got: %v", newRow_1_0) | |||
} | |||
oldRow_2_0 := events[2].oldRow[0].(int64) | |||
if oldRow_2_0 != 99 { | |||
t.Errorf("Expected event row 1 new column 0 to be == 99, got: %v", oldRow_2_0) | |||
} | |||
} |
@ -0,0 +1,21 @@ | |||
// Copyright (C) 2019 G.J.R. Timmer <gjr.timmer@gmail.com>. | |||
// Copyright (C) 2018 segment.com <friends@segment.com> | |||
// | |||
// Use of this source code is governed by an MIT-style | |||
// license that can be found in the LICENSE file. | |||
// +build !sqlite_preupdate_hook,cgo | |||
package sqlite3 | |||
// RegisterPreUpdateHook sets the pre-update hook for a connection. | |||
// | |||
// The callback is passed a SQLitePreUpdateData struct with the data for | |||
// the update, as well as methods for fetching copies of impacted data. | |||
// | |||
// 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 | |||
// without creating a new one. | |||
func (c *SQLiteConn) RegisterPreUpdateHook(callback func(SQLitePreUpdateData)) { | |||
// NOOP | |||
} |
@ -0,0 +1,15 @@ | |||
// Copyright (C) 2019 Yasuhiro Matsumoto <mattn.jp@gmail.com>. | |||
// Copyright (C) 2018 G.J.R. Timmer <gjr.timmer@gmail.com>. | |||
// | |||
// Use of this source code is governed by an MIT-style | |||
// license that can be found in the LICENSE file. | |||
// +build sqlite_secure_delete | |||
package sqlite3 | |||
/* | |||
#cgo CFLAGS: -DSQLITE_SECURE_DELETE=1 | |||
#cgo LDFLAGS: -lm | |||
*/ | |||
import "C" |
@ -0,0 +1,15 @@ | |||
// Copyright (C) 2019 Yasuhiro Matsumoto <mattn.jp@gmail.com>. | |||
// Copyright (C) 2018 G.J.R. Timmer <gjr.timmer@gmail.com>. | |||
// | |||
// Use of this source code is governed by an MIT-style | |||
// license that can be found in the LICENSE file. | |||
// +build sqlite_secure_delete_fast | |||
package sqlite3 | |||
/* | |||
#cgo CFLAGS: -DSQLITE_SECURE_DELETE=FAST | |||
#cgo LDFLAGS: -lm | |||
*/ | |||
import "C" |
@ -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.MaxUint32) { | |||
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 | |||
} |
@ -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") | |||
} |
@ -0,0 +1,99 @@ | |||
// +build !libsqlite3 sqlite_serialize | |||
package sqlite3 | |||
import ( | |||
"context" | |||
"database/sql" | |||
"os" | |||
"testing" | |||
) | |||
func TestSerializeDeserialize(t *testing.T) { | |||
// Connect to the source database. | |||
srcTempFilename := TempFilename(t) | |||
defer os.Remove(srcTempFilename) | |||
srcDb, err := sql.Open(driverName, srcTempFilename) | |||
if err != nil { | |||
t.Fatal("Failed to open the source database:", err) | |||
} | |||
defer srcDb.Close() | |||
err = srcDb.Ping() | |||
if err != nil { | |||
t.Fatal("Failed to connect to the source database:", err) | |||
} | |||
// Connect to the destination database. | |||
destTempFilename := TempFilename(t) | |||
defer os.Remove(destTempFilename) | |||
destDb, err := sql.Open(driverName, destTempFilename) | |||
if err != nil { | |||
t.Fatal("Failed to open the destination database:", err) | |||
} | |||
defer destDb.Close() | |||
err = destDb.Ping() | |||
if err != nil { | |||
t.Fatal("Failed to connect to the destination database:", err) | |||
} | |||
// Write data to source database. | |||
_, err = srcDb.Exec(`CREATE TABLE foo (name string)`) | |||
if err != nil { | |||
t.Fatal("Failed to create table in source database:", err) | |||
} | |||
_, err = srcDb.Exec(`INSERT INTO foo(name) VALUES("alice")`) | |||
if err != nil { | |||
t.Fatal("Failed to insert data into source database", err) | |||
} | |||
// Serialize the source database | |||
srcConn, err := srcDb.Conn(context.Background()) | |||
if err != nil { | |||
t.Fatal("Failed to get connection to source database:", err) | |||
} | |||
defer srcConn.Close() | |||
var serialized []byte | |||
if err := srcConn.Raw(func(raw interface{}) error { | |||
var err error | |||
serialized, err = raw.(*SQLiteConn).Serialize("") | |||
return err | |||
}); err != nil { | |||
t.Fatal("Failed to serialize source database:", err) | |||
} | |||
srcConn.Close() | |||
// Confirm that the destination database is initially empty. | |||
var destTableCount int | |||
err = destDb.QueryRow("SELECT COUNT(*) FROM sqlite_master WHERE type = 'table'").Scan(&destTableCount) | |||
if err != nil { | |||
t.Fatal("Failed to check the destination table count:", err) | |||
} | |||
if destTableCount != 0 { | |||
t.Fatalf("The destination database is not empty; %v table(s) found.", destTableCount) | |||
} | |||
// Deserialize to destination database | |||
destConn, err := destDb.Conn(context.Background()) | |||
if err != nil { | |||
t.Fatal("Failed to get connection to destination database:", err) | |||
} | |||
defer destConn.Close() | |||
if err := destConn.Raw(func(raw interface{}) error { | |||
return raw.(*SQLiteConn).Deserialize(serialized, "") | |||
}); err != nil { | |||
t.Fatal("Failed to deserialize source database:", err) | |||
} | |||
destConn.Close() | |||
// Confirm that destination database has been loaded correctly. | |||
var destRowCount int | |||
err = destDb.QueryRow(`SELECT COUNT(*) FROM foo`).Scan(&destRowCount) | |||
if err != nil { | |||
t.Fatal("Failed to count rows in destination database table", err) | |||
} | |||
if destRowCount != 1 { | |||
t.Fatalf("Destination table does not have the expected records") | |||
} | |||
} |
@ -0,0 +1,15 @@ | |||
// Copyright (C) 2019 Yasuhiro Matsumoto <mattn.jp@gmail.com>. | |||
// Copyright (C) 2018 G.J.R. Timmer <gjr.timmer@gmail.com>. | |||
// | |||
// Use of this source code is governed by an MIT-style | |||
// license that can be found in the LICENSE file. | |||
// +build sqlite_stat4 | |||
package sqlite3 | |||
/* | |||
#cgo CFLAGS: -DSQLITE_ENABLE_STAT4 | |||
#cgo LDFLAGS: -lm | |||
*/ | |||
import "C" |
@ -0,0 +1,85 @@ | |||
// Copyright (C) 2018 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. | |||
#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY | |||
#include <stdio.h> | |||
#include "sqlite3-binding.h" | |||
extern int unlock_notify_wait(sqlite3 *db); | |||
int | |||
_sqlite3_step_blocking(sqlite3_stmt *stmt) | |||
{ | |||
int rv; | |||
sqlite3* db; | |||
db = sqlite3_db_handle(stmt); | |||
for (;;) { | |||
rv = sqlite3_step(stmt); | |||
if (rv != SQLITE_LOCKED) { | |||
break; | |||
} | |||
if (sqlite3_extended_errcode(db) != SQLITE_LOCKED_SHAREDCACHE) { | |||
break; | |||
} | |||
rv = unlock_notify_wait(db); | |||
if (rv != SQLITE_OK) { | |||
break; | |||
} | |||
sqlite3_reset(stmt); | |||
} | |||
return rv; | |||
} | |||
int | |||
_sqlite3_step_row_blocking(sqlite3_stmt* stmt, long long* rowid, long long* changes) | |||
{ | |||
int rv; | |||
sqlite3* db; | |||
db = sqlite3_db_handle(stmt); | |||
for (;;) { | |||
rv = sqlite3_step(stmt); | |||
if (rv!=SQLITE_LOCKED) { | |||
break; | |||
} | |||
if (sqlite3_extended_errcode(db) != SQLITE_LOCKED_SHAREDCACHE) { | |||
break; | |||
} | |||
rv = unlock_notify_wait(db); | |||
if (rv != SQLITE_OK) { | |||
break; | |||
} | |||
sqlite3_reset(stmt); | |||
} | |||
*rowid = (long long) sqlite3_last_insert_rowid(db); | |||
*changes = (long long) sqlite3_changes(db); | |||
return rv; | |||
} | |||
int | |||
_sqlite3_prepare_v2_blocking(sqlite3 *db, const char *zSql, int nBytes, sqlite3_stmt **ppStmt, const char **pzTail) | |||
{ | |||
int rv; | |||
for (;;) { | |||
rv = sqlite3_prepare_v2(db, zSql, nBytes, ppStmt, pzTail); | |||
if (rv!=SQLITE_LOCKED) { | |||
break; | |||
} | |||
if (sqlite3_extended_errcode(db) != SQLITE_LOCKED_SHAREDCACHE) { | |||
break; | |||
} | |||
rv = unlock_notify_wait(db); | |||
if (rv != SQLITE_OK) { | |||
break; | |||
} | |||
} | |||
return rv; | |||
} | |||
#endif |
@ -0,0 +1,93 @@ | |||
// 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 cgo | |||
// +build sqlite_unlock_notify | |||
package sqlite3 | |||
/* | |||
#cgo CFLAGS: -DSQLITE_ENABLE_UNLOCK_NOTIFY | |||
#include <stdlib.h> | |||
#include "sqlite3-binding.h" | |||
extern void unlock_notify_callback(void *arg, int argc); | |||
*/ | |||
import "C" | |||
import ( | |||
"fmt" | |||
"math" | |||
"sync" | |||
"unsafe" | |||
) | |||
type unlock_notify_table struct { | |||
sync.Mutex | |||
seqnum uint | |||
table map[uint]chan struct{} | |||
} | |||
var unt unlock_notify_table = unlock_notify_table{table: make(map[uint]chan struct{})} | |||
func (t *unlock_notify_table) add(c chan struct{}) uint { | |||
t.Lock() | |||
defer t.Unlock() | |||
h := t.seqnum | |||
t.table[h] = c | |||
t.seqnum++ | |||
return h | |||
} | |||
func (t *unlock_notify_table) remove(h uint) { | |||
t.Lock() | |||
defer t.Unlock() | |||
delete(t.table, h) | |||
} | |||
func (t *unlock_notify_table) get(h uint) chan struct{} { | |||
t.Lock() | |||
defer t.Unlock() | |||
c, ok := t.table[h] | |||
if !ok { | |||
panic(fmt.Sprintf("Non-existent key for unlcok-notify channel: %d", h)) | |||
} | |||
return c | |||
} | |||
//export unlock_notify_callback | |||
func unlock_notify_callback(argv unsafe.Pointer, argc C.int) { | |||
for i := 0; i < int(argc); i++ { | |||
parg := ((*(*[(math.MaxInt32 - 1) / unsafe.Sizeof((*C.uint)(nil))]*[1]uint)(argv))[i]) | |||
arg := *parg | |||
h := arg[0] | |||
c := unt.get(h) | |||
c <- struct{}{} | |||
} | |||
} | |||
//export unlock_notify_wait | |||
func unlock_notify_wait(db *C.sqlite3) C.int { | |||
// It has to be a bufferred channel to not block in sqlite_unlock_notify | |||
// as sqlite_unlock_notify could invoke the callback before it returns. | |||
c := make(chan struct{}, 1) | |||
defer close(c) | |||
h := unt.add(c) | |||
defer unt.remove(h) | |||
pargv := C.malloc(C.sizeof_uint) | |||
defer C.free(pargv) | |||
argv := (*[1]uint)(pargv) | |||
argv[0] = h | |||
if rv := C.sqlite3_unlock_notify(db, (*[0]byte)(C.unlock_notify_callback), unsafe.Pointer(pargv)); rv != C.SQLITE_OK { | |||
return rv | |||
} | |||
<-c | |||
return C.SQLITE_OK | |||
} |
@ -0,0 +1,222 @@ | |||
// 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_unlock_notify | |||
package sqlite3 | |||
import ( | |||
"database/sql" | |||
"fmt" | |||
"os" | |||
"sync" | |||
"testing" | |||
"time" | |||
) | |||
func TestUnlockNotify(t *testing.T) { | |||
tempFilename := TempFilename(t) | |||
defer os.Remove(tempFilename) | |||
dsn := fmt.Sprintf("file:%s?cache=shared&mode=rwc&_busy_timeout=%d", tempFilename, 500) | |||
db, err := sql.Open("sqlite3", dsn) | |||
if err != nil { | |||
t.Fatal("Failed to open database:", err) | |||
} | |||
defer db.Close() | |||
_, err = db.Exec("CREATE TABLE foo(id INTEGER, status INTEGER)") | |||
if err != nil { | |||
t.Fatal("Failed to create table:", err) | |||
} | |||
tx, err := db.Begin() | |||
if err != nil { | |||
t.Fatal("Failed to begin transaction:", err) | |||
} | |||
_, err = tx.Exec("INSERT INTO foo(id, status) VALUES(1, 100)") | |||
if err != nil { | |||
t.Fatal("Failed to insert null:", err) | |||
} | |||
_, err = tx.Exec("UPDATE foo SET status = 200 WHERE id = 1") | |||
if err != nil { | |||
t.Fatal("Failed to update table:", err) | |||
} | |||
wg := sync.WaitGroup{} | |||
wg.Add(1) | |||
timer := time.NewTimer(500 * time.Millisecond) | |||
go func() { | |||
<-timer.C | |||
err := tx.Commit() | |||
if err != nil { | |||
t.Fatal("Failed to commit transaction:", err) | |||
} | |||
wg.Done() | |||
}() | |||
rows, err := db.Query("SELECT count(*) from foo") | |||
if err != nil { | |||
t.Fatal("Unable to query foo table:", err) | |||
} | |||
if rows.Next() { | |||
var count int | |||
if err := rows.Scan(&count); err != nil { | |||
t.Fatal("Failed to Scan rows", err) | |||
} | |||
} | |||
if err := rows.Err(); err != nil { | |||
t.Fatal("Failed at the call to Next:", err) | |||
} | |||
wg.Wait() | |||
} | |||
func TestUnlockNotifyMany(t *testing.T) { | |||
tempFilename := TempFilename(t) | |||
defer os.Remove(tempFilename) | |||
dsn := fmt.Sprintf("file:%s?cache=shared&mode=rwc&_busy_timeout=%d", tempFilename, 500) | |||
db, err := sql.Open("sqlite3", dsn) | |||
if err != nil { | |||
t.Fatal("Failed to open database:", err) | |||
} | |||
defer db.Close() | |||
_, err = db.Exec("CREATE TABLE foo(id INTEGER, status INTEGER)") | |||
if err != nil { | |||
t.Fatal("Failed to create table:", err) | |||
} | |||
tx, err := db.Begin() | |||
if err != nil { | |||
t.Fatal("Failed to begin transaction:", err) | |||
} | |||
_, err = tx.Exec("INSERT INTO foo(id, status) VALUES(1, 100)") | |||
if err != nil { | |||
t.Fatal("Failed to insert null:", err) | |||
} | |||
_, err = tx.Exec("UPDATE foo SET status = 200 WHERE id = 1") | |||
if err != nil { | |||
t.Fatal("Failed to update table:", err) | |||
} | |||
wg := sync.WaitGroup{} | |||
wg.Add(1) | |||
timer := time.NewTimer(500 * time.Millisecond) | |||
go func() { | |||
<-timer.C | |||
err := tx.Commit() | |||
if err != nil { | |||
t.Fatal("Failed to commit transaction:", err) | |||
} | |||
wg.Done() | |||
}() | |||
const concurrentQueries = 1000 | |||
wg.Add(concurrentQueries) | |||
for i := 0; i < concurrentQueries; i++ { | |||
go func() { | |||
rows, err := db.Query("SELECT count(*) from foo") | |||
if err != nil { | |||
t.Fatal("Unable to query foo table:", err) | |||
} | |||
if rows.Next() { | |||
var count int | |||
if err := rows.Scan(&count); err != nil { | |||
t.Fatal("Failed to Scan rows", err) | |||
} | |||
} | |||
if err := rows.Err(); err != nil { | |||
t.Fatal("Failed at the call to Next:", err) | |||
} | |||
wg.Done() | |||
}() | |||
} | |||
wg.Wait() | |||
} | |||
func TestUnlockNotifyDeadlock(t *testing.T) { | |||
tempFilename := TempFilename(t) | |||
defer os.Remove(tempFilename) | |||
dsn := fmt.Sprintf("file:%s?cache=shared&mode=rwc&_busy_timeout=%d", tempFilename, 500) | |||
db, err := sql.Open("sqlite3", dsn) | |||
if err != nil { | |||
t.Fatal("Failed to open database:", err) | |||
} | |||
defer db.Close() | |||
_, err = db.Exec("CREATE TABLE foo(id INTEGER, status INTEGER)") | |||
if err != nil { | |||
t.Fatal("Failed to create table:", err) | |||
} | |||
tx, err := db.Begin() | |||
if err != nil { | |||
t.Fatal("Failed to begin transaction:", err) | |||
} | |||
_, err = tx.Exec("INSERT INTO foo(id, status) VALUES(1, 100)") | |||
if err != nil { | |||
t.Fatal("Failed to insert null:", err) | |||
} | |||
_, err = tx.Exec("UPDATE foo SET status = 200 WHERE id = 1") | |||
if err != nil { | |||
t.Fatal("Failed to update table:", err) | |||
} | |||
wg := sync.WaitGroup{} | |||
wg.Add(1) | |||
timer := time.NewTimer(500 * time.Millisecond) | |||
go func() { | |||
<-timer.C | |||
err := tx.Commit() | |||
if err != nil { | |||
t.Fatal("Failed to commit transaction:", err) | |||
} | |||
wg.Done() | |||
}() | |||
wg.Add(1) | |||
go func() { | |||
tx2, err := db.Begin() | |||
if err != nil { | |||
t.Fatal("Failed to begin transaction:", err) | |||
} | |||
defer tx2.Rollback() | |||
_, err = tx2.Exec("DELETE FROM foo") | |||
if err != nil { | |||
t.Fatal("Failed to delete table:", err) | |||
} | |||
err = tx2.Commit() | |||
if err != nil { | |||
t.Fatal("Failed to commit transaction:", err) | |||
} | |||
wg.Done() | |||
}() | |||
rows, err := tx.Query("SELECT count(*) from foo") | |||
if err != nil { | |||
t.Fatal("Unable to query foo table:", err) | |||
} | |||
if rows.Next() { | |||
var count int | |||
if err := rows.Scan(&count); err != nil { | |||
t.Fatal("Failed to Scan rows", err) | |||
} | |||
} | |||
if err := rows.Err(); err != nil { | |||
t.Fatal("Failed at the call to Next:", err) | |||
} | |||
wg.Wait() | |||
} |
@ -0,0 +1,289 @@ | |||
// Copyright (C) 2018 G.J.R. Timmer <gjr.timmer@gmail.com>. | |||
// | |||
// Use of this source code is governed by an MIT-style | |||
// license that can be found in the LICENSE file. | |||
// +build sqlite_userauth | |||
package sqlite3 | |||
/* | |||
#cgo CFLAGS: -DSQLITE_USER_AUTHENTICATION | |||
#cgo LDFLAGS: -lm | |||
#ifndef USE_LIBSQLITE3 | |||
#include "sqlite3-binding.h" | |||
#else | |||
#include <sqlite3.h> | |||
#endif | |||
#include <stdlib.h> | |||
static int | |||
_sqlite3_user_authenticate(sqlite3* db, const char* zUsername, const char* aPW, int nPW) | |||
{ | |||
return sqlite3_user_authenticate(db, zUsername, aPW, nPW); | |||
} | |||
static int | |||
_sqlite3_user_add(sqlite3* db, const char* zUsername, const char* aPW, int nPW, int isAdmin) | |||
{ | |||
return sqlite3_user_add(db, zUsername, aPW, nPW, isAdmin); | |||
} | |||
static int | |||
_sqlite3_user_change(sqlite3* db, const char* zUsername, const char* aPW, int nPW, int isAdmin) | |||
{ | |||
return sqlite3_user_change(db, zUsername, aPW, nPW, isAdmin); | |||
} | |||
static int | |||
_sqlite3_user_delete(sqlite3* db, const char* zUsername) | |||
{ | |||
return sqlite3_user_delete(db, zUsername); | |||
} | |||
static int | |||
_sqlite3_auth_enabled(sqlite3* db) | |||
{ | |||
int exists = -1; | |||
sqlite3_stmt *stmt; | |||
sqlite3_prepare_v2(db, "select count(type) from sqlite_master WHERE type='table' and name='sqlite_user';", -1, &stmt, NULL); | |||
while ( sqlite3_step(stmt) == SQLITE_ROW) { | |||
exists = sqlite3_column_int(stmt, 0); | |||
} | |||
sqlite3_finalize(stmt); | |||
return exists; | |||
} | |||
*/ | |||
import "C" | |||
import ( | |||
"errors" | |||
"unsafe" | |||
) | |||
const ( | |||
SQLITE_AUTH = C.SQLITE_AUTH | |||
) | |||
var ( | |||
ErrUnauthorized = errors.New("SQLITE_AUTH: Unauthorized") | |||
ErrAdminRequired = errors.New("SQLITE_AUTH: Unauthorized; Admin Privileges Required") | |||
) | |||
// Authenticate will perform an authentication of the provided username | |||
// and password against the database. | |||
// | |||
// If a database contains the SQLITE_USER table, then the | |||
// call to Authenticate must be invoked with an | |||
// appropriate username and password prior to enable read and write | |||
//access to the database. | |||
// | |||
// Return SQLITE_OK on success or SQLITE_ERROR if the username/password | |||
// combination is incorrect or unknown. | |||
// | |||
// If the SQLITE_USER table is not present in the database file, then | |||
// this interface is a harmless no-op returnning SQLITE_OK. | |||
func (c *SQLiteConn) Authenticate(username, password string) error { | |||
rv := c.authenticate(username, password) | |||
switch rv { | |||
case C.SQLITE_ERROR, C.SQLITE_AUTH: | |||
return ErrUnauthorized | |||
case C.SQLITE_OK: | |||
return nil | |||
default: | |||
return c.lastError() | |||
} | |||
} | |||
// authenticate provides the actual authentication to SQLite. | |||
// This is not exported for usage in Go. | |||
// It is however exported for usage within SQL by the user. | |||
// | |||
// Returns: | |||
// C.SQLITE_OK (0) | |||
// C.SQLITE_ERROR (1) | |||
// C.SQLITE_AUTH (23) | |||
func (c *SQLiteConn) authenticate(username, password string) int { | |||
// Allocate C Variables | |||
cuser := C.CString(username) | |||
cpass := C.CString(password) | |||
// Free C Variables | |||
defer func() { | |||
C.free(unsafe.Pointer(cuser)) | |||
C.free(unsafe.Pointer(cpass)) | |||
}() | |||
return int(C._sqlite3_user_authenticate(c.db, cuser, cpass, C.int(len(password)))) | |||
} | |||
// AuthUserAdd can be used (by an admin user only) | |||
// to create a new user. When called on a no-authentication-required | |||
// database, this routine converts the database into an authentication- | |||
// required database, automatically makes the added user an | |||
// administrator, and logs in the current connection as that user. | |||
// The AuthUserAdd only works for the "main" database, not | |||
// for any ATTACH-ed databases. Any call to AuthUserAdd by a | |||
// non-admin user results in an error. | |||
func (c *SQLiteConn) AuthUserAdd(username, password string, admin bool) error { | |||
isAdmin := 0 | |||
if admin { | |||
isAdmin = 1 | |||
} | |||
rv := c.authUserAdd(username, password, isAdmin) | |||
switch rv { | |||
case C.SQLITE_ERROR, C.SQLITE_AUTH: | |||
return ErrAdminRequired | |||
case C.SQLITE_OK: | |||
return nil | |||
default: | |||
return c.lastError() | |||
} | |||
} | |||
// authUserAdd enables the User Authentication if not enabled. | |||
// Otherwise it will add a user. | |||
// | |||
// When user authentication is already enabled then this function | |||
// can only be called by an admin. | |||
// | |||
// This is not exported for usage in Go. | |||
// It is however exported for usage within SQL by the user. | |||
// | |||
// Returns: | |||
// C.SQLITE_OK (0) | |||
// C.SQLITE_ERROR (1) | |||
// C.SQLITE_AUTH (23) | |||
func (c *SQLiteConn) authUserAdd(username, password string, admin int) int { | |||
// Allocate C Variables | |||
cuser := C.CString(username) | |||
cpass := C.CString(password) | |||
// Free C Variables | |||
defer func() { | |||
C.free(unsafe.Pointer(cuser)) | |||
C.free(unsafe.Pointer(cpass)) | |||
}() | |||
return int(C._sqlite3_user_add(c.db, cuser, cpass, C.int(len(password)), C.int(admin))) | |||
} | |||
// AuthUserChange can be used to change a users | |||
// login credentials or admin privilege. Any user can change their own | |||
// login credentials. Only an admin user can change another users login | |||
// credentials or admin privilege setting. No user may change their own | |||
// admin privilege setting. | |||
func (c *SQLiteConn) AuthUserChange(username, password string, admin bool) error { | |||
isAdmin := 0 | |||
if admin { | |||
isAdmin = 1 | |||
} | |||
rv := c.authUserChange(username, password, isAdmin) | |||
switch rv { | |||
case C.SQLITE_ERROR, C.SQLITE_AUTH: | |||
return ErrAdminRequired | |||
case C.SQLITE_OK: | |||
return nil | |||
default: | |||
return c.lastError() | |||
} | |||
} | |||
// authUserChange allows to modify a user. | |||
// Users can change their own password. | |||
// | |||
// Only admins can change passwords for other users | |||
// and modify the admin flag. | |||
// | |||
// The admin flag of the current logged in user cannot be changed. | |||
// THis ensures that their is always an admin. | |||
// | |||
// This is not exported for usage in Go. | |||
// It is however exported for usage within SQL by the user. | |||
// | |||
// Returns: | |||
// C.SQLITE_OK (0) | |||
// C.SQLITE_ERROR (1) | |||
// C.SQLITE_AUTH (23) | |||
func (c *SQLiteConn) authUserChange(username, password string, admin int) int { | |||
// Allocate C Variables | |||
cuser := C.CString(username) | |||
cpass := C.CString(password) | |||
// Free C Variables | |||
defer func() { | |||
C.free(unsafe.Pointer(cuser)) | |||
C.free(unsafe.Pointer(cpass)) | |||
}() | |||
return int(C._sqlite3_user_change(c.db, cuser, cpass, C.int(len(password)), C.int(admin))) | |||
} | |||
// AuthUserDelete can be used (by an admin user only) | |||
// to delete a user. The currently logged-in user cannot be deleted, | |||
// which guarantees that there is always an admin user and hence that | |||
// the database cannot be converted into a no-authentication-required | |||
// database. | |||
func (c *SQLiteConn) AuthUserDelete(username string) error { | |||
rv := c.authUserDelete(username) | |||
switch rv { | |||
case C.SQLITE_ERROR, C.SQLITE_AUTH: | |||
return ErrAdminRequired | |||
case C.SQLITE_OK: | |||
return nil | |||
default: | |||
return c.lastError() | |||
} | |||
} | |||
// authUserDelete can be used to delete a user. | |||
// | |||
// This function can only be executed by an admin. | |||
// | |||
// This is not exported for usage in Go. | |||
// It is however exported for usage within SQL by the user. | |||
// | |||
// Returns: | |||
// C.SQLITE_OK (0) | |||
// C.SQLITE_ERROR (1) | |||
// C.SQLITE_AUTH (23) | |||
func (c *SQLiteConn) authUserDelete(username string) int { | |||
// Allocate C Variables | |||
cuser := C.CString(username) | |||
// Free C Variables | |||
defer func() { | |||
C.free(unsafe.Pointer(cuser)) | |||
}() | |||
return int(C._sqlite3_user_delete(c.db, cuser)) | |||
} | |||
// AuthEnabled checks if the database is protected by user authentication | |||
func (c *SQLiteConn) AuthEnabled() (exists bool) { | |||
rv := c.authEnabled() | |||
if rv == 1 { | |||
exists = true | |||
} | |||
return | |||
} | |||
// authEnabled perform the actual check for user authentication. | |||
// | |||
// This is not exported for usage in Go. | |||
// It is however exported for usage within SQL by the user. | |||
// | |||
// Returns: | |||
// 0 - Disabled | |||
// 1 - Enabled | |||
func (c *SQLiteConn) authEnabled() int { | |||
return int(C._sqlite3_auth_enabled(c.db)) | |||
} | |||
// EOF |
@ -0,0 +1,152 @@ | |||
// Copyright (C) 2018 G.J.R. Timmer <gjr.timmer@gmail.com>. | |||
// | |||
// Use of this source code is governed by an MIT-style | |||
// license that can be found in the LICENSE file. | |||
// +build !sqlite_userauth | |||
package sqlite3 | |||
import ( | |||
"C" | |||
) | |||
// Authenticate will perform an authentication of the provided username | |||
// and password against the database. | |||
// | |||
// If a database contains the SQLITE_USER table, then the | |||
// call to Authenticate must be invoked with an | |||
// appropriate username and password prior to enable read and write | |||
//access to the database. | |||
// | |||
// Return SQLITE_OK on success or SQLITE_ERROR if the username/password | |||
// combination is incorrect or unknown. | |||
// | |||
// If the SQLITE_USER table is not present in the database file, then | |||
// this interface is a harmless no-op returnning SQLITE_OK. | |||
func (c *SQLiteConn) Authenticate(username, password string) error { | |||
// NOOP | |||
return nil | |||
} | |||
// authenticate provides the actual authentication to SQLite. | |||
// This is not exported for usage in Go. | |||
// It is however exported for usage within SQL by the user. | |||
// | |||
// Returns: | |||
// C.SQLITE_OK (0) | |||
// C.SQLITE_ERROR (1) | |||
// C.SQLITE_AUTH (23) | |||
func (c *SQLiteConn) authenticate(username, password string) int { | |||
// NOOP | |||
return 0 | |||
} | |||
// AuthUserAdd can be used (by an admin user only) | |||
// to create a new user. When called on a no-authentication-required | |||
// database, this routine converts the database into an authentication- | |||
// required database, automatically makes the added user an | |||
// administrator, and logs in the current connection as that user. | |||
// The AuthUserAdd only works for the "main" database, not | |||
// for any ATTACH-ed databases. Any call to AuthUserAdd by a | |||
// non-admin user results in an error. | |||
func (c *SQLiteConn) AuthUserAdd(username, password string, admin bool) error { | |||
// NOOP | |||
return nil | |||
} | |||
// authUserAdd enables the User Authentication if not enabled. | |||
// Otherwise it will add a user. | |||
// | |||
// When user authentication is already enabled then this function | |||
// can only be called by an admin. | |||
// | |||
// This is not exported for usage in Go. | |||
// It is however exported for usage within SQL by the user. | |||
// | |||
// Returns: | |||
// C.SQLITE_OK (0) | |||
// C.SQLITE_ERROR (1) | |||
// C.SQLITE_AUTH (23) | |||
func (c *SQLiteConn) authUserAdd(username, password string, admin int) int { | |||
// NOOP | |||
return 0 | |||
} | |||
// AuthUserChange can be used to change a users | |||
// login credentials or admin privilege. Any user can change their own | |||
// login credentials. Only an admin user can change another users login | |||
// credentials or admin privilege setting. No user may change their own | |||
// admin privilege setting. | |||
func (c *SQLiteConn) AuthUserChange(username, password string, admin bool) error { | |||
// NOOP | |||
return nil | |||
} | |||
// authUserChange allows to modify a user. | |||
// Users can change their own password. | |||
// | |||
// Only admins can change passwords for other users | |||
// and modify the admin flag. | |||
// | |||
// The admin flag of the current logged in user cannot be changed. | |||
// THis ensures that their is always an admin. | |||
// | |||
// This is not exported for usage in Go. | |||
// It is however exported for usage within SQL by the user. | |||
// | |||
// Returns: | |||
// C.SQLITE_OK (0) | |||
// C.SQLITE_ERROR (1) | |||
// C.SQLITE_AUTH (23) | |||
func (c *SQLiteConn) authUserChange(username, password string, admin int) int { | |||
// NOOP | |||
return 0 | |||
} | |||
// AuthUserDelete can be used (by an admin user only) | |||
// to delete a user. The currently logged-in user cannot be deleted, | |||
// which guarantees that there is always an admin user and hence that | |||
// the database cannot be converted into a no-authentication-required | |||
// database. | |||
func (c *SQLiteConn) AuthUserDelete(username string) error { | |||
// NOOP | |||
return nil | |||
} | |||
// authUserDelete can be used to delete a user. | |||
// | |||
// This function can only be executed by an admin. | |||
// | |||
// This is not exported for usage in Go. | |||
// It is however exported for usage within SQL by the user. | |||
// | |||
// Returns: | |||
// C.SQLITE_OK (0) | |||
// C.SQLITE_ERROR (1) | |||
// C.SQLITE_AUTH (23) | |||
func (c *SQLiteConn) authUserDelete(username string) int { | |||
// NOOP | |||
return 0 | |||
} | |||
// AuthEnabled checks if the database is protected by user authentication | |||
func (c *SQLiteConn) AuthEnabled() (exists bool) { | |||
// NOOP | |||
return false | |||
} | |||
// authEnabled perform the actual check for user authentication. | |||
// | |||
// This is not exported for usage in Go. | |||
// It is however exported for usage within SQL by the user. | |||
// | |||
// Returns: | |||
// 0 - Disabled | |||
// 1 - Enabled | |||
func (c *SQLiteConn) authEnabled() int { | |||
// NOOP | |||
return 0 | |||
} | |||
// EOF |
@ -0,0 +1,643 @@ | |||
// Copyright (C) 2018 G.J.R. Timmer <gjr.timmer@gmail.com>. | |||
// | |||
// Use of this source code is governed by an MIT-style | |||
// license that can be found in the LICENSE file. | |||
// +build sqlite_userauth | |||
package sqlite3 | |||
import ( | |||
"database/sql" | |||
"fmt" | |||
"os" | |||
"testing" | |||
) | |||
var ( | |||
conn *SQLiteConn | |||
create func(t *testing.T, username, password string) (file string, err error) | |||
createWithCrypt func(t *testing.T, username, password, crypt, salt string) (file string, err error) | |||
connect func(t *testing.T, f string, username, password string) (file string, db *sql.DB, c *SQLiteConn, err error) | |||
connectWithCrypt func(t *testing.T, f string, username, password string, crypt string, salt string) (file string, db *sql.DB, c *SQLiteConn, err error) | |||
authEnabled func(db *sql.DB) (exists bool, err error) | |||
addUser func(db *sql.DB, username, password string, admin int) (rv int, err error) | |||
userExists func(db *sql.DB, username string) (rv int, err error) | |||
isAdmin func(db *sql.DB, username string) (rv bool, err error) | |||
modifyUser func(db *sql.DB, username, password string, admin int) (rv int, err error) | |||
deleteUser func(db *sql.DB, username string) (rv int, err error) | |||
) | |||
func init() { | |||
// Create database connection | |||
sql.Register("sqlite3_with_conn", | |||
&SQLiteDriver{ | |||
ConnectHook: func(c *SQLiteConn) error { | |||
conn = c | |||
return nil | |||
}, | |||
}) | |||
create = func(t *testing.T, username, password string) (file string, err error) { | |||
var db *sql.DB | |||
file, db, _, err = connect(t, "", username, password) | |||
db.Close() | |||
return | |||
} | |||
createWithCrypt = func(t *testing.T, username, password, crypt, salt string) (file string, err error) { | |||
var db *sql.DB | |||
file, db, _, err = connectWithCrypt(t, "", "admin", "admin", crypt, salt) | |||
db.Close() | |||
return | |||
} | |||
connect = func(t *testing.T, f string, username, password string) (file string, db *sql.DB, c *SQLiteConn, err error) { | |||
conn = nil // Clear connection | |||
file = f // Copy provided file (f) => file | |||
if file == "" { | |||
// Create dummy file | |||
file = TempFilename(t) | |||
} | |||
params := "?_auth" | |||
if len(username) > 0 { | |||
params = fmt.Sprintf("%s&_auth_user=%s", params, username) | |||
} | |||
if len(password) > 0 { | |||
params = fmt.Sprintf("%s&_auth_pass=%s", params, password) | |||
} | |||
db, err = sql.Open("sqlite3_with_conn", "file:"+file+params) | |||
if err != nil { | |||
defer os.Remove(file) | |||
return file, nil, nil, err | |||
} | |||
// Dummy query to force connection and database creation | |||
// Will return ErrUnauthorized (SQLITE_AUTH) if user authentication fails | |||
if _, err = db.Exec("SELECT 1;"); err != nil { | |||
defer os.Remove(file) | |||
defer db.Close() | |||
return file, nil, nil, err | |||
} | |||
c = conn | |||
return | |||
} | |||
connectWithCrypt = func(t *testing.T, f string, username, password string, crypt string, salt string) (file string, db *sql.DB, c *SQLiteConn, err error) { | |||
conn = nil // Clear connection | |||
file = f // Copy provided file (f) => file | |||
if file == "" { | |||
// Create dummy file | |||
file = TempFilename(t) | |||
} | |||
db, err = sql.Open("sqlite3_with_conn", "file:"+file+fmt.Sprintf("?_auth&_auth_user=%s&_auth_pass=%s&_auth_crypt=%s&_auth_salt=%s", username, password, crypt, salt)) | |||
if err != nil { | |||
defer os.Remove(file) | |||
return file, nil, nil, err | |||
} | |||
// Dummy query to force connection and database creation | |||
// Will return ErrUnauthorized (SQLITE_AUTH) if user authentication fails | |||
if _, err = db.Exec("SELECT 1;"); err != nil { | |||
defer os.Remove(file) | |||
defer db.Close() | |||
return file, nil, nil, err | |||
} | |||
c = conn | |||
return | |||
} | |||
authEnabled = func(db *sql.DB) (exists bool, err error) { | |||
err = db.QueryRow("select count(type) from sqlite_master WHERE type='table' and name='sqlite_user';").Scan(&exists) | |||
return | |||
} | |||
addUser = func(db *sql.DB, username, password string, admin int) (rv int, err error) { | |||
err = db.QueryRow("select auth_user_add(?, ?, ?);", username, password, admin).Scan(&rv) | |||
return | |||
} | |||
userExists = func(db *sql.DB, username string) (rv int, err error) { | |||
err = db.QueryRow("select count(uname) from sqlite_user where uname=?", username).Scan(&rv) | |||
return | |||
} | |||
isAdmin = func(db *sql.DB, username string) (rv bool, err error) { | |||
err = db.QueryRow("select isAdmin from sqlite_user where uname=?", username).Scan(&rv) | |||
return | |||
} | |||
modifyUser = func(db *sql.DB, username, password string, admin int) (rv int, err error) { | |||
err = db.QueryRow("select auth_user_change(?, ?, ?);", username, password, admin).Scan(&rv) | |||
return | |||
} | |||
deleteUser = func(db *sql.DB, username string) (rv int, err error) { | |||
err = db.QueryRow("select auth_user_delete(?);", username).Scan(&rv) | |||
return | |||
} | |||
} | |||
func TestUserAuthCreateDatabase(t *testing.T) { | |||
f, db, c, err := connect(t, "", "admin", "admin") | |||
if err != nil && c == nil && db == nil { | |||
t.Fatal(err) | |||
} | |||
defer db.Close() | |||
defer os.Remove(f) | |||
enabled, err := authEnabled(db) | |||
if err != nil || !enabled { | |||
t.Fatalf("UserAuth not enabled: %s", err) | |||
} | |||
e, err := userExists(db, "admin") | |||
if err != nil { | |||
t.Fatal(err) | |||
} | |||
if e != 1 { | |||
t.Fatal("UserAuth: admin does not exists") | |||
} | |||
a, err := isAdmin(db, "admin") | |||
if err != nil { | |||
t.Fatal(err) | |||
} | |||
if !a { | |||
t.Fatal("UserAuth: User is not administrator") | |||
} | |||
} | |||
func TestUserAuthCreateDatabaseWithoutArgs(t *testing.T) { | |||
_, db, c, err := connect(t, "", "", "") | |||
if err == nil && c == nil && db == nil { | |||
t.Fatal("Should have failed due to missing _auth_* parameters") | |||
} | |||
_, db, c, err = connect(t, "", "", "admin") | |||
if err == nil && c == nil && db == nil { | |||
t.Fatal("Should have failed due to missing _auth_user parameter") | |||
} | |||
_, db, c, err = connect(t, "", "admin", "") | |||
if err == nil && c == nil && db == nil { | |||
t.Fatal("Should have failed due to missing _auth_pass parameter") | |||
} | |||
} | |||
func TestUserAuthLogin(t *testing.T) { | |||
f1, err := create(t, "admin", "admin") | |||
if err != nil { | |||
t.Fatal(err) | |||
} | |||
defer os.Remove(f1) | |||
f2, db2, c2, err := connect(t, f1, "admin", "admin") | |||
if err != nil { | |||
t.Fatal(err) | |||
} | |||
defer db2.Close() | |||
if f1 != f2 { | |||
t.Fatal("UserAuth: Database file mismatch") | |||
} | |||
// Test lower level authentication | |||
err = c2.Authenticate("admin", "admin") | |||
if err != nil { | |||
t.Fatalf("UserAuth: *SQLiteConn.Authenticate() Failed: %s", err) | |||
} | |||
// Test Login Failed | |||
_, _, _, err = connect(t, f1, "admin", "invalid") | |||
if err == nil { | |||
t.Fatal("Login successful while expecting to fail") | |||
} | |||
if err != ErrUnauthorized { | |||
t.Fatal(err) | |||
} | |||
err = c2.Authenticate("admin", "invalid") | |||
if err == nil { | |||
t.Fatal("Login successful while expecting to fail") | |||
} | |||
if err != ErrUnauthorized { | |||
t.Fatal(err) | |||
} | |||
} | |||
func TestUserAuthAddAdmin(t *testing.T) { | |||
f, db, c, err := connect(t, "", "admin", "admin") | |||
if err != nil && c == nil && db == nil { | |||
t.Fatal(err) | |||
} | |||
defer db.Close() | |||
defer os.Remove(f) | |||
// Add Admin User through SQL call | |||
rv, err := addUser(db, "admin2", "admin2", 1) | |||
if err != nil { | |||
t.Fatal(err) | |||
} | |||
if rv != 0 { | |||
t.Fatal("Failed to add user") | |||
} | |||
// Check if user was created | |||
exists, err := userExists(db, "admin2") | |||
if err != nil { | |||
t.Fatal(err) | |||
} | |||
if exists != 1 { | |||
t.Fatal("UserAuth: 'admin2' does not exists") | |||
} | |||
// Check if user was created as an Administrator | |||
admin, err := isAdmin(db, "admin2") | |||
if err != nil { | |||
t.Fatal(err) | |||
} | |||
if !admin { | |||
t.Fatal("UserAuth: 'admin2' is not administrator") | |||
} | |||
// Test *SQLiteConn | |||
err = c.AuthUserAdd("admin3", "admin3", true) | |||
if err != nil { | |||
t.Fatal(err) | |||
} | |||
// Check if user was created | |||
exists, err = userExists(db, "admin2") | |||
if err != nil { | |||
t.Fatal(err) | |||
} | |||
if exists != 1 { | |||
t.Fatal("UserAuth: 'admin3' does not exists") | |||
} | |||
// Check if the user was created as an Administrator | |||
admin, err = isAdmin(db, "admin3") | |||
if err != nil { | |||
t.Fatal(err) | |||
} | |||
if !admin { | |||
t.Fatal("UserAuth: 'admin3' is not administrator") | |||
} | |||
} | |||
func TestUserAuthAddUser(t *testing.T) { | |||
f1, db1, c, err := connect(t, "", "admin", "admin") | |||
if err != nil && c == nil && db == nil { | |||
t.Fatal(err) | |||
} | |||
defer os.Remove(f1) | |||
// Add user through SQL call | |||
rv, err := addUser(db1, "user", "user", 0) | |||
if err != nil { | |||
t.Fatal(err) | |||
} | |||
if rv != 0 { | |||
t.Fatal("Failed to add user") | |||
} | |||
// Check if user was created | |||
exists, err := userExists(db1, "user") | |||
if err != nil { | |||
t.Fatal(err) | |||
} | |||
if exists != 1 { | |||
t.Fatal("UserAuth: 'user' does not exists") | |||
} | |||
// Check if user was created as an Administrator | |||
admin, err := isAdmin(db1, "user") | |||
if err != nil { | |||
t.Fatal(err) | |||
} | |||
if admin { | |||
t.Fatal("UserAuth: 'user' is administrator") | |||
} | |||
// Test *SQLiteConn | |||
err = c.AuthUserAdd("user2", "user2", false) | |||
if err != nil { | |||
t.Fatal(err) | |||
} | |||
// Check if user was created | |||
exists, err = userExists(db1, "user2") | |||
if err != nil { | |||
t.Fatal(err) | |||
} | |||
if exists != 1 { | |||
t.Fatal("UserAuth: 'user2' does not exists") | |||
} | |||
// Check if the user was created as an Administrator | |||
admin, err = isAdmin(db1, "user2") | |||
if err != nil { | |||
t.Fatal(err) | |||
} | |||
if admin { | |||
t.Fatal("UserAuth: 'user2' is administrator") | |||
} | |||
// Reconnect as normal user | |||
db1.Close() | |||
_, db2, c2, err := connect(t, f1, "user", "user") | |||
if err != nil { | |||
t.Fatal(err) | |||
} | |||
defer db2.Close() | |||
// Try to create admin user while logged in as normal user | |||
rv, err = addUser(db2, "admin2", "admin2", 1) | |||
if err != nil { | |||
t.Fatal(err) | |||
} | |||
if rv != SQLITE_AUTH { | |||
t.Fatal("Created admin user while not allowed") | |||
} | |||
err = c2.AuthUserAdd("admin3", "admin3", true) | |||
if err != ErrAdminRequired { | |||
t.Fatal("Created admin user while not allowed") | |||
} | |||
// Try to create normal user while logged in as normal user | |||
rv, err = addUser(db2, "user3", "user3", 0) | |||
if err != nil { | |||
t.Fatal(err) | |||
} | |||
if rv != SQLITE_AUTH { | |||
t.Fatal("Created user while not allowed") | |||
} | |||
err = c2.AuthUserAdd("user4", "user4", false) | |||
if err != ErrAdminRequired { | |||
t.Fatal("Created user while not allowed") | |||
} | |||
} | |||
func TestUserAuthModifyUser(t *testing.T) { | |||
f1, db1, c1, err := connect(t, "", "admin", "admin") | |||
if err != nil && c1 == nil && db == nil { | |||
t.Fatal(err) | |||
} | |||
defer os.Remove(f1) | |||
// Modify Password for current logged in admin | |||
// through SQL | |||
rv, err := modifyUser(db1, "admin", "admin2", 1) | |||
if err != nil { | |||
t.Fatal(err) | |||
} | |||
if rv != 0 { | |||
t.Fatal("Failed to modify password for admin") | |||
} | |||
// Modify password for current logged in admin | |||
// through *SQLiteConn | |||
err = c1.AuthUserChange("admin", "admin3", true) | |||
if err != nil { | |||
t.Fatal(err) | |||
} | |||
// Modify Administrator Flag | |||
// Because we are current logged in as 'admin' | |||
// Changing our own admin flag should fail. | |||
rv, err = modifyUser(db1, "admin", "admin3", 0) | |||
if err != nil { | |||
t.Fatal(err) | |||
} | |||
if rv != SQLITE_AUTH { | |||
t.Fatal("Successfully changed admin flag while not allowed") | |||
} | |||
// Modify admin flag through (*SQLiteConn) | |||
// Because we are current logged in as 'admin' | |||
// Changing our own admin flag should fail. | |||
err = c1.AuthUserChange("admin", "admin3", false) | |||
if err != ErrAdminRequired { | |||
t.Fatal("Successfully changed admin flag while not allowed") | |||
} | |||
// Add normal user | |||
rv, err = addUser(db1, "user", "password", 0) | |||
if err != nil { | |||
t.Fatal(err) | |||
} | |||
if rv != 0 { | |||
t.Fatal("Failed to add user") | |||
} | |||
rv, err = addUser(db1, "user2", "user2", 0) | |||
if err != nil { | |||
t.Fatal(err) | |||
} | |||
if rv != 0 { | |||
t.Fatal("Failed to add user") | |||
} | |||
// Modify other user password and flag through SQL | |||
rv, err = modifyUser(db1, "user", "pass", 1) | |||
if err != nil { | |||
t.Fatal(err) | |||
} | |||
if rv != 0 { | |||
t.Fatal("Failed to modify password for user") | |||
} | |||
// Modify other user password and flag through *SQLiteConn | |||
err = c1.AuthUserChange("user", "newpass", false) | |||
if err != nil { | |||
t.Fatal(err) | |||
} | |||
// Disconnect database for reconnect | |||
db1.Close() | |||
_, db2, c2, err := connect(t, f1, "user", "newpass") | |||
if err != nil { | |||
t.Fatal(err) | |||
} | |||
defer db2.Close() | |||
// Modify other user password through SQL | |||
rv, err = modifyUser(db2, "user2", "newpass", 0) | |||
if err != nil { | |||
t.Fatal(err) | |||
} | |||
if rv != SQLITE_AUTH { | |||
t.Fatal("Password change successful while not allowed") | |||
} | |||
// Modify other user password and flag through *SQLiteConn | |||
err = c2.AuthUserChange("user2", "invalid", false) | |||
if err != ErrAdminRequired { | |||
t.Fatal("Password change successful while not allowed") | |||
} | |||
} | |||
func TestUserAuthDeleteUser(t *testing.T) { | |||
f1, db1, c, err := connect(t, "", "admin", "admin") | |||
if err != nil && c == nil && db == nil { | |||
t.Fatal(err) | |||
} | |||
defer os.Remove(f1) | |||
// Add Admin User 2 | |||
rv, err := addUser(db1, "admin2", "admin2", 1) | |||
if err != nil { | |||
t.Fatal(err) | |||
} | |||
if rv != 0 { | |||
t.Fatal("Failed to add user") | |||
} | |||
rv, err = addUser(db1, "admin3", "admin3", 1) | |||
if err != nil { | |||
t.Fatal(err) | |||
} | |||
if rv != 0 { | |||
t.Fatal("Failed to add user") | |||
} | |||
// Check if user was created | |||
exists, err := userExists(db1, "admin2") | |||
if err != nil { | |||
t.Fatal(err) | |||
} | |||
if exists != 1 { | |||
t.Fatal("UserAuth: 'admin2' does not exists") | |||
} | |||
exists, err = userExists(db1, "admin3") | |||
if err != nil { | |||
t.Fatal(err) | |||
} | |||
if exists != 1 { | |||
t.Fatal("UserAuth: 'admin2' does not exists") | |||
} | |||
// Delete user through SQL | |||
rv, err = deleteUser(db1, "admin2") | |||
if err != nil { | |||
t.Fatal(err) | |||
} | |||
if rv != 0 { | |||
t.Fatal("Failed to delete admin2") | |||
} | |||
// Verify user admin2 deleted | |||
exists, err = userExists(db1, "admin2") | |||
if err != nil { | |||
t.Fatal(err) | |||
} | |||
if exists != 0 { | |||
t.Fatal("UserAuth: 'admin2' still exists") | |||
} | |||
// Delete user through *SQLiteConn | |||
rv, err = deleteUser(db1, "admin3") | |||
if err != nil { | |||
t.Fatal(err) | |||
} | |||
if rv != 0 { | |||
t.Fatal("Failed to delete admin3") | |||
} | |||
// Verify user admin3 deleted | |||
exists, err = userExists(db1, "admin3") | |||
if err != nil { | |||
t.Fatal(err) | |||
} | |||
if exists != 0 { | |||
t.Fatal("UserAuth: 'admin3' still exists") | |||
} | |||
// Add normal user for reconnect and privileges check | |||
rv, err = addUser(db1, "reconnect", "reconnect", 0) | |||
if err != nil { | |||
t.Fatal(err) | |||
} | |||
if rv != 0 { | |||
t.Fatal("Failed to add user") | |||
} | |||
// Add normal user for deletion through SQL | |||
rv, err = addUser(db1, "user", "user", 0) | |||
if err != nil { | |||
t.Fatal(err) | |||
} | |||
if rv != 0 { | |||
t.Fatal("Failed to add user") | |||
} | |||
rv, err = addUser(db1, "user2", "user2", 0) | |||
if err != nil { | |||
t.Fatal(err) | |||
} | |||
if rv != 0 { | |||
t.Fatal("Failed to add user") | |||
} | |||
// Close database for reconnect | |||
db1.Close() | |||
// Reconnect as normal user | |||
_, db2, c2, err := connect(t, f1, "reconnect", "reconnect") | |||
if err != nil { | |||
t.Fatal(err) | |||
} | |||
defer db2.Close() | |||
// Delete user while logged in as normal user | |||
// through SQL | |||
rv, err = deleteUser(db2, "user") | |||
if err != nil { | |||
t.Fatal(err) | |||
} | |||
if rv != SQLITE_AUTH { | |||
t.Fatal("Successfully deleted user wthout proper privileges") | |||
} | |||
// Delete user while logged in as normal user | |||
// through *SQLiteConn | |||
err = c2.AuthUserDelete("user2") | |||
if err != ErrAdminRequired { | |||
t.Fatal("Successfully deleted user wthout proper privileges") | |||
} | |||
} | |||
func TestUserAuthEncoders(t *testing.T) { | |||
cases := map[string]string{ | |||
"sha1": "", | |||
"ssha1": "salted", | |||
"sha256": "", | |||
"ssha256": "salted", | |||
"sha384": "", | |||
"ssha384": "salted", | |||
"sha512": "", | |||
"ssha512": "salted", | |||
} | |||
for enc, salt := range cases { | |||
f, err := createWithCrypt(t, "admin", "admin", enc, salt) | |||
if err != nil { | |||
t.Fatal(err) | |||
} | |||
defer os.Remove(f) | |||
_, db, _, err := connectWithCrypt(t, f, "admin", "admin", enc, salt) | |||
if err != nil { | |||
t.Fatal(err) | |||
} | |||
defer db.Close() | |||
if e, err := authEnabled(db); err != nil && !e { | |||
t.Fatalf("UserAuth (%s) not enabled %s", enc, err) | |||
} | |||
} | |||
} |
@ -0,0 +1,15 @@ | |||
// Copyright (C) 2019 Yasuhiro Matsumoto <mattn.jp@gmail.com>. | |||
// Copyright (C) 2018 G.J.R. Timmer <gjr.timmer@gmail.com>. | |||
// | |||
// Use of this source code is governed by an MIT-style | |||
// license that can be found in the LICENSE file. | |||
// +build sqlite_vacuum_full | |||
package sqlite3 | |||
/* | |||
#cgo CFLAGS: -DSQLITE_DEFAULT_AUTOVACUUM=1 | |||
#cgo LDFLAGS: -lm | |||
*/ | |||
import "C" |
@ -0,0 +1,15 @@ | |||
// Copyright (C) 2019 Yasuhiro Matsumoto <mattn.jp@gmail.com>. | |||
// Copyright (C) 2018 G.J.R. Timmer <gjr.timmer@gmail.com>. | |||
// | |||
// Use of this source code is governed by an MIT-style | |||
// license that can be found in the LICENSE file. | |||
// +build sqlite_vacuum_incr | |||
package sqlite3 | |||
/* | |||
#cgo CFLAGS: -DSQLITE_DEFAULT_AUTOVACUUM=2 | |||
#cgo LDFLAGS: -lm | |||
*/ | |||
import "C" |
@ -0,0 +1,720 @@ | |||
// 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_vtable vtable | |||
package sqlite3 | |||
/* | |||
#cgo CFLAGS: -std=gnu99 | |||
#cgo CFLAGS: -DSQLITE_ENABLE_RTREE | |||
#cgo CFLAGS: -DSQLITE_THREADSAFE | |||
#cgo CFLAGS: -DSQLITE_ENABLE_FTS3 | |||
#cgo CFLAGS: -DSQLITE_ENABLE_FTS3_PARENTHESIS | |||
#cgo CFLAGS: -DSQLITE_ENABLE_FTS4_UNICODE61 | |||
#cgo CFLAGS: -DSQLITE_TRACE_SIZE_LIMIT=15 | |||
#cgo CFLAGS: -DSQLITE_ENABLE_COLUMN_METADATA=1 | |||
#cgo CFLAGS: -Wno-deprecated-declarations | |||
#ifndef USE_LIBSQLITE3 | |||
#include "sqlite3-binding.h" | |||
#else | |||
#include <sqlite3.h> | |||
#endif | |||
#include <stdlib.h> | |||
#include <stdint.h> | |||
#include <memory.h> | |||
static inline char *_sqlite3_mprintf(char *zFormat, char *arg) { | |||
return sqlite3_mprintf(zFormat, arg); | |||
} | |||
typedef struct goVTab goVTab; | |||
struct goVTab { | |||
sqlite3_vtab base; | |||
void *vTab; | |||
}; | |||
uintptr_t goMInit(void *db, void *pAux, int argc, char **argv, char **pzErr, int isCreate); | |||
static int cXInit(sqlite3 *db, void *pAux, int argc, const char *const*argv, sqlite3_vtab **ppVTab, char **pzErr, int isCreate) { | |||
void *vTab = (void *)goMInit(db, pAux, argc, (char**)argv, pzErr, isCreate); | |||
if (!vTab || *pzErr) { | |||
return SQLITE_ERROR; | |||
} | |||
goVTab *pvTab = (goVTab *)sqlite3_malloc(sizeof(goVTab)); | |||
if (!pvTab) { | |||
*pzErr = sqlite3_mprintf("%s", "Out of memory"); | |||
return SQLITE_NOMEM; | |||
} | |||
memset(pvTab, 0, sizeof(goVTab)); | |||
pvTab->vTab = vTab; | |||
*ppVTab = (sqlite3_vtab *)pvTab; | |||
*pzErr = 0; | |||
return SQLITE_OK; | |||
} | |||
static inline int cXCreate(sqlite3 *db, void *pAux, int argc, const char *const*argv, sqlite3_vtab **ppVTab, char **pzErr) { | |||
return cXInit(db, pAux, argc, argv, ppVTab, pzErr, 1); | |||
} | |||
static inline int cXConnect(sqlite3 *db, void *pAux, int argc, const char *const*argv, sqlite3_vtab **ppVTab, char **pzErr) { | |||
return cXInit(db, pAux, argc, argv, ppVTab, pzErr, 0); | |||
} | |||
char* goVBestIndex(void *pVTab, void *icp); | |||
static inline int cXBestIndex(sqlite3_vtab *pVTab, sqlite3_index_info *info) { | |||
char *pzErr = goVBestIndex(((goVTab*)pVTab)->vTab, info); | |||
if (pzErr) { | |||
if (pVTab->zErrMsg) | |||
sqlite3_free(pVTab->zErrMsg); | |||
pVTab->zErrMsg = pzErr; | |||
return SQLITE_ERROR; | |||
} | |||
return SQLITE_OK; | |||
} | |||
char* goVRelease(void *pVTab, int isDestroy); | |||
static int cXRelease(sqlite3_vtab *pVTab, int isDestroy) { | |||
char *pzErr = goVRelease(((goVTab*)pVTab)->vTab, isDestroy); | |||
if (pzErr) { | |||
if (pVTab->zErrMsg) | |||
sqlite3_free(pVTab->zErrMsg); | |||
pVTab->zErrMsg = pzErr; | |||
return SQLITE_ERROR; | |||
} | |||
if (pVTab->zErrMsg) | |||
sqlite3_free(pVTab->zErrMsg); | |||
sqlite3_free(pVTab); | |||
return SQLITE_OK; | |||
} | |||
static inline int cXDisconnect(sqlite3_vtab *pVTab) { | |||
return cXRelease(pVTab, 0); | |||
} | |||
static inline int cXDestroy(sqlite3_vtab *pVTab) { | |||
return cXRelease(pVTab, 1); | |||
} | |||
typedef struct goVTabCursor goVTabCursor; | |||
struct goVTabCursor { | |||
sqlite3_vtab_cursor base; | |||
void *vTabCursor; | |||
}; | |||
uintptr_t goVOpen(void *pVTab, char **pzErr); | |||
static int cXOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor) { | |||
void *vTabCursor = (void *)goVOpen(((goVTab*)pVTab)->vTab, &(pVTab->zErrMsg)); | |||
goVTabCursor *pCursor = (goVTabCursor *)sqlite3_malloc(sizeof(goVTabCursor)); | |||
if (!pCursor) { | |||
return SQLITE_NOMEM; | |||
} | |||
memset(pCursor, 0, sizeof(goVTabCursor)); | |||
pCursor->vTabCursor = vTabCursor; | |||
*ppCursor = (sqlite3_vtab_cursor *)pCursor; | |||
return SQLITE_OK; | |||
} | |||
static int setErrMsg(sqlite3_vtab_cursor *pCursor, char *pzErr) { | |||
if (pCursor->pVtab->zErrMsg) | |||
sqlite3_free(pCursor->pVtab->zErrMsg); | |||
pCursor->pVtab->zErrMsg = pzErr; | |||
return SQLITE_ERROR; | |||
} | |||
char* goVClose(void *pCursor); | |||
static int cXClose(sqlite3_vtab_cursor *pCursor) { | |||
char *pzErr = goVClose(((goVTabCursor*)pCursor)->vTabCursor); | |||
if (pzErr) { | |||
return setErrMsg(pCursor, pzErr); | |||
} | |||
sqlite3_free(pCursor); | |||
return SQLITE_OK; | |||
} | |||
char* goVFilter(void *pCursor, int idxNum, char* idxName, int argc, sqlite3_value **argv); | |||
static int cXFilter(sqlite3_vtab_cursor *pCursor, int idxNum, const char *idxStr, int argc, sqlite3_value **argv) { | |||
char *pzErr = goVFilter(((goVTabCursor*)pCursor)->vTabCursor, idxNum, (char*)idxStr, argc, argv); | |||
if (pzErr) { | |||
return setErrMsg(pCursor, pzErr); | |||
} | |||
return SQLITE_OK; | |||
} | |||
char* goVNext(void *pCursor); | |||
static int cXNext(sqlite3_vtab_cursor *pCursor) { | |||
char *pzErr = goVNext(((goVTabCursor*)pCursor)->vTabCursor); | |||
if (pzErr) { | |||
return setErrMsg(pCursor, pzErr); | |||
} | |||
return SQLITE_OK; | |||
} | |||
int goVEof(void *pCursor); | |||
static inline int cXEof(sqlite3_vtab_cursor *pCursor) { | |||
return goVEof(((goVTabCursor*)pCursor)->vTabCursor); | |||
} | |||
char* goVColumn(void *pCursor, void *cp, int col); | |||
static int cXColumn(sqlite3_vtab_cursor *pCursor, sqlite3_context *ctx, int i) { | |||
char *pzErr = goVColumn(((goVTabCursor*)pCursor)->vTabCursor, ctx, i); | |||
if (pzErr) { | |||
return setErrMsg(pCursor, pzErr); | |||
} | |||
return SQLITE_OK; | |||
} | |||
char* goVRowid(void *pCursor, sqlite3_int64 *pRowid); | |||
static int cXRowid(sqlite3_vtab_cursor *pCursor, sqlite3_int64 *pRowid) { | |||
char *pzErr = goVRowid(((goVTabCursor*)pCursor)->vTabCursor, pRowid); | |||
if (pzErr) { | |||
return setErrMsg(pCursor, pzErr); | |||
} | |||
return SQLITE_OK; | |||
} | |||
char* goVUpdate(void *pVTab, int argc, sqlite3_value **argv, sqlite3_int64 *pRowid); | |||
static int cXUpdate(sqlite3_vtab *pVTab, int argc, sqlite3_value **argv, sqlite3_int64 *pRowid) { | |||
char *pzErr = goVUpdate(((goVTab*)pVTab)->vTab, argc, argv, pRowid); | |||
if (pzErr) { | |||
if (pVTab->zErrMsg) | |||
sqlite3_free(pVTab->zErrMsg); | |||
pVTab->zErrMsg = pzErr; | |||
return SQLITE_ERROR; | |||
} | |||
return SQLITE_OK; | |||
} | |||
static sqlite3_module goModule = { | |||
0, // iVersion | |||
cXCreate, // xCreate - create a table | |||
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 | |||
}; | |||
// 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*); | |||
static int _sqlite3_create_module(sqlite3 *db, const char *zName, uintptr_t pClientData) { | |||
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 ( | |||
"fmt" | |||
"math" | |||
"reflect" | |||
"unsafe" | |||
) | |||
type sqliteModule struct { | |||
c *SQLiteConn | |||
name string | |||
module Module | |||
} | |||
type sqliteVTab struct { | |||
module *sqliteModule | |||
vTab VTab | |||
} | |||
type sqliteVTabCursor struct { | |||
vTab *sqliteVTab | |||
vTabCursor VTabCursor | |||
} | |||
// Op is type of operations. | |||
type Op uint8 | |||
// Op mean identity of operations. | |||
const ( | |||
OpEQ Op = 2 | |||
OpGT = 4 | |||
OpLE = 8 | |||
OpLT = 16 | |||
OpGE = 32 | |||
OpMATCH = 64 | |||
OpLIKE = 65 /* 3.10.0 and later only */ | |||
OpGLOB = 66 /* 3.10.0 and later only */ | |||
OpREGEXP = 67 /* 3.10.0 and later only */ | |||
OpScanUnique = 1 /* Scan visits at most 1 row */ | |||
) | |||
// InfoConstraint give information of constraint. | |||
type InfoConstraint struct { | |||
Column int | |||
Op Op | |||
Usable bool | |||
} | |||
// InfoOrderBy give information of order-by. | |||
type InfoOrderBy struct { | |||
Column int | |||
Desc bool | |||
} | |||
func constraints(info *C.sqlite3_index_info) []InfoConstraint { | |||
slice := *(*[]C.struct_sqlite3_index_constraint)(unsafe.Pointer(&reflect.SliceHeader{ | |||
Data: uintptr(unsafe.Pointer(info.aConstraint)), | |||
Len: int(info.nConstraint), | |||
Cap: int(info.nConstraint), | |||
})) | |||
cst := make([]InfoConstraint, 0, len(slice)) | |||
for _, c := range slice { | |||
var usable bool | |||
if c.usable > 0 { | |||
usable = true | |||
} | |||
cst = append(cst, InfoConstraint{ | |||
Column: int(c.iColumn), | |||
Op: Op(c.op), | |||
Usable: usable, | |||
}) | |||
} | |||
return cst | |||
} | |||
func orderBys(info *C.sqlite3_index_info) []InfoOrderBy { | |||
slice := *(*[]C.struct_sqlite3_index_orderby)(unsafe.Pointer(&reflect.SliceHeader{ | |||
Data: uintptr(unsafe.Pointer(info.aOrderBy)), | |||
Len: int(info.nOrderBy), | |||
Cap: int(info.nOrderBy), | |||
})) | |||
ob := make([]InfoOrderBy, 0, len(slice)) | |||
for _, c := range slice { | |||
var desc bool | |||
if c.desc > 0 { | |||
desc = true | |||
} | |||
ob = append(ob, InfoOrderBy{ | |||
Column: int(c.iColumn), | |||
Desc: desc, | |||
}) | |||
} | |||
return ob | |||
} | |||
// IndexResult is a Go struct representation of what eventually ends up in the | |||
// output fields for `sqlite3_index_info` | |||
// See: https://www.sqlite.org/c3ref/index_info.html | |||
type IndexResult struct { | |||
Used []bool // aConstraintUsage | |||
IdxNum int | |||
IdxStr string | |||
AlreadyOrdered bool // orderByConsumed | |||
EstimatedCost float64 | |||
EstimatedRows float64 | |||
} | |||
// mPrintf is a utility wrapper around sqlite3_mprintf | |||
func mPrintf(format, arg string) *C.char { | |||
cf := C.CString(format) | |||
defer C.free(unsafe.Pointer(cf)) | |||
ca := C.CString(arg) | |||
defer C.free(unsafe.Pointer(ca)) | |||
return C._sqlite3_mprintf(cf, ca) | |||
} | |||
//export goMInit | |||
func goMInit(db, pClientData unsafe.Pointer, argc C.int, argv **C.char, pzErr **C.char, isCreate C.int) C.uintptr_t { | |||
m := lookupHandle(pClientData).(*sqliteModule) | |||
if m.c.db != (*C.sqlite3)(db) { | |||
*pzErr = mPrintf("%s", "Inconsistent db handles") | |||
return 0 | |||
} | |||
args := make([]string, argc) | |||
var A []*C.char | |||
slice := reflect.SliceHeader{Data: uintptr(unsafe.Pointer(argv)), Len: int(argc), Cap: int(argc)} | |||
a := reflect.NewAt(reflect.TypeOf(A), unsafe.Pointer(&slice)).Elem().Interface() | |||
for i, s := range a.([]*C.char) { | |||
args[i] = C.GoString(s) | |||
} | |||
var vTab VTab | |||
var err error | |||
if isCreate == 1 { | |||
vTab, err = m.module.Create(m.c, args) | |||
} else { | |||
vTab, err = m.module.Connect(m.c, args) | |||
} | |||
if err != nil { | |||
*pzErr = mPrintf("%s", err.Error()) | |||
return 0 | |||
} | |||
vt := sqliteVTab{m, vTab} | |||
*pzErr = nil | |||
return C.uintptr_t(uintptr(newHandle(m.c, &vt))) | |||
} | |||
//export goVRelease | |||
func goVRelease(pVTab unsafe.Pointer, isDestroy C.int) *C.char { | |||
vt := lookupHandle(pVTab).(*sqliteVTab) | |||
var err error | |||
if isDestroy == 1 { | |||
err = vt.vTab.Destroy() | |||
} else { | |||
err = vt.vTab.Disconnect() | |||
} | |||
if err != nil { | |||
return mPrintf("%s", err.Error()) | |||
} | |||
return nil | |||
} | |||
//export goVOpen | |||
func goVOpen(pVTab unsafe.Pointer, pzErr **C.char) C.uintptr_t { | |||
vt := lookupHandle(pVTab).(*sqliteVTab) | |||
vTabCursor, err := vt.vTab.Open() | |||
if err != nil { | |||
*pzErr = mPrintf("%s", err.Error()) | |||
return 0 | |||
} | |||
vtc := sqliteVTabCursor{vt, vTabCursor} | |||
*pzErr = nil | |||
return C.uintptr_t(uintptr(newHandle(vt.module.c, &vtc))) | |||
} | |||
//export goVBestIndex | |||
func goVBestIndex(pVTab unsafe.Pointer, icp unsafe.Pointer) *C.char { | |||
vt := lookupHandle(pVTab).(*sqliteVTab) | |||
info := (*C.sqlite3_index_info)(icp) | |||
csts := constraints(info) | |||
res, err := vt.vTab.BestIndex(csts, orderBys(info)) | |||
if err != nil { | |||
return mPrintf("%s", err.Error()) | |||
} | |||
if len(res.Used) != len(csts) { | |||
return mPrintf("Result.Used != expected value", "") | |||
} | |||
// Get a pointer to constraint_usage struct so we can update in place. | |||
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 | |||
for i := range slice { | |||
if res.Used[i] { | |||
slice[i].argvIndex = C.int(index) | |||
slice[i].omit = C.uchar(1) | |||
index++ | |||
} | |||
} | |||
info.idxNum = C.int(res.IdxNum) | |||
info.idxStr = (*C.char)(C.sqlite3_malloc(C.int(len(res.IdxStr) + 1))) | |||
if info.idxStr == nil { | |||
// C.malloc and C.CString ordinarily do this for you. See https://golang.org/cmd/cgo/ | |||
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 { | |||
info.orderByConsumed = C.int(1) | |||
} | |||
info.estimatedCost = C.double(res.EstimatedCost) | |||
info.estimatedRows = C.sqlite3_int64(res.EstimatedRows) | |||
return nil | |||
} | |||
//export goVClose | |||
func goVClose(pCursor unsafe.Pointer) *C.char { | |||
vtc := lookupHandle(pCursor).(*sqliteVTabCursor) | |||
err := vtc.vTabCursor.Close() | |||
if err != nil { | |||
return mPrintf("%s", err.Error()) | |||
} | |||
return nil | |||
} | |||
//export goMDestroy | |||
func goMDestroy(pClientData unsafe.Pointer) { | |||
m := lookupHandle(pClientData).(*sqliteModule) | |||
m.module.DestroyModule() | |||
} | |||
//export goVFilter | |||
func goVFilter(pCursor unsafe.Pointer, idxNum C.int, idxName *C.char, argc C.int, argv **C.sqlite3_value) *C.char { | |||
vtc := lookupHandle(pCursor).(*sqliteVTabCursor) | |||
args := (*[(math.MaxInt32 - 1) / unsafe.Sizeof((*C.sqlite3_value)(nil))]*C.sqlite3_value)(unsafe.Pointer(argv))[:argc:argc] | |||
vals := make([]interface{}, 0, argc) | |||
for _, v := range args { | |||
conv, err := callbackArgGeneric(v) | |||
if err != nil { | |||
return mPrintf("%s", err.Error()) | |||
} | |||
vals = append(vals, conv.Interface()) | |||
} | |||
err := vtc.vTabCursor.Filter(int(idxNum), C.GoString(idxName), vals) | |||
if err != nil { | |||
return mPrintf("%s", err.Error()) | |||
} | |||
return nil | |||
} | |||
//export goVNext | |||
func goVNext(pCursor unsafe.Pointer) *C.char { | |||
vtc := lookupHandle(pCursor).(*sqliteVTabCursor) | |||
err := vtc.vTabCursor.Next() | |||
if err != nil { | |||
return mPrintf("%s", err.Error()) | |||
} | |||
return nil | |||
} | |||
//export goVEof | |||
func goVEof(pCursor unsafe.Pointer) C.int { | |||
vtc := lookupHandle(pCursor).(*sqliteVTabCursor) | |||
err := vtc.vTabCursor.EOF() | |||
if err { | |||
return 1 | |||
} | |||
return 0 | |||
} | |||
//export goVColumn | |||
func goVColumn(pCursor, cp unsafe.Pointer, col C.int) *C.char { | |||
vtc := lookupHandle(pCursor).(*sqliteVTabCursor) | |||
c := (*SQLiteContext)(cp) | |||
err := vtc.vTabCursor.Column(c, int(col)) | |||
if err != nil { | |||
return mPrintf("%s", err.Error()) | |||
} | |||
return nil | |||
} | |||
//export goVRowid | |||
func goVRowid(pCursor unsafe.Pointer, pRowid *C.sqlite3_int64) *C.char { | |||
vtc := lookupHandle(pCursor).(*sqliteVTabCursor) | |||
rowid, err := vtc.vTabCursor.Rowid() | |||
if err != nil { | |||
return mPrintf("%s", err.Error()) | |||
} | |||
*pRowid = C.sqlite3_int64(rowid) | |||
return nil | |||
} | |||
//export goVUpdate | |||
func goVUpdate(pVTab unsafe.Pointer, argc C.int, argv **C.sqlite3_value, pRowid *C.sqlite3_int64) *C.char { | |||
vt := lookupHandle(pVTab).(*sqliteVTab) | |||
var tname string | |||
if n, ok := vt.vTab.(interface { | |||
TableName() string | |||
}); ok { | |||
tname = n.TableName() + " " | |||
} | |||
err := fmt.Errorf("virtual %s table %sis read-only", vt.module.name, tname) | |||
if v, ok := vt.vTab.(VTabUpdater); ok { | |||
// convert argv | |||
args := (*[(math.MaxInt32 - 1) / unsafe.Sizeof((*C.sqlite3_value)(nil))]*C.sqlite3_value)(unsafe.Pointer(argv))[:argc:argc] | |||
vals := make([]interface{}, 0, argc) | |||
for _, v := range args { | |||
conv, err := callbackArgGeneric(v) | |||
if err != nil { | |||
return mPrintf("%s", err.Error()) | |||
} | |||
// work around for SQLITE_NULL | |||
x := conv.Interface() | |||
if z, ok := x.([]byte); ok && z == nil { | |||
x = nil | |||
} | |||
vals = append(vals, x) | |||
} | |||
switch { | |||
case argc == 1: | |||
err = v.Delete(vals[0]) | |||
case argc > 1 && vals[0] == nil: | |||
var id int64 | |||
id, err = v.Insert(vals[1], vals[2:]) | |||
if err == nil { | |||
*pRowid = C.sqlite3_int64(id) | |||
} | |||
case argc > 1: | |||
err = v.Update(vals[1], vals[2:]) | |||
} | |||
} | |||
if err != nil { | |||
return mPrintf("%s", err.Error()) | |||
} | |||
return nil | |||
} | |||
// Module is a "virtual table module", it defines the implementation of a | |||
// virtual tables. See: http://sqlite.org/c3ref/module.html | |||
type Module interface { | |||
// http://sqlite.org/vtab.html#xcreate | |||
Create(c *SQLiteConn, args []string) (VTab, error) | |||
// http://sqlite.org/vtab.html#xconnect | |||
Connect(c *SQLiteConn, args []string) (VTab, error) | |||
// http://sqlite.org/c3ref/create_module.html | |||
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. | |||
// See: http://sqlite.org/c3ref/vtab.html | |||
type VTab interface { | |||
// http://sqlite.org/vtab.html#xbestindex | |||
BestIndex([]InfoConstraint, []InfoOrderBy) (*IndexResult, error) | |||
// http://sqlite.org/vtab.html#xdisconnect | |||
Disconnect() error | |||
// http://sqlite.org/vtab.html#sqlite3_module.xDestroy | |||
Destroy() error | |||
// http://sqlite.org/vtab.html#xopen | |||
Open() (VTabCursor, error) | |||
} | |||
// VTabUpdater is a type that allows a VTab to be inserted, updated, or | |||
// deleted. | |||
// See: https://sqlite.org/vtab.html#xupdate | |||
type VTabUpdater interface { | |||
Delete(interface{}) error | |||
Insert(interface{}, []interface{}) (int64, error) | |||
Update(interface{}, []interface{}) error | |||
} | |||
// VTabCursor describes cursors that point into the virtual table and are used | |||
// to loop through the virtual table. See: http://sqlite.org/c3ref/vtab_cursor.html | |||
type VTabCursor interface { | |||
// http://sqlite.org/vtab.html#xclose | |||
Close() error | |||
// http://sqlite.org/vtab.html#xfilter | |||
Filter(idxNum int, idxStr string, vals []interface{}) error | |||
// http://sqlite.org/vtab.html#xnext | |||
Next() error | |||
// http://sqlite.org/vtab.html#xeof | |||
EOF() bool | |||
// http://sqlite.org/vtab.html#xcolumn | |||
Column(c *SQLiteContext, col int) error | |||
// http://sqlite.org/vtab.html#xrowid | |||
Rowid() (int64, error) | |||
} | |||
// DeclareVTab declares the Schema of a virtual table. | |||
// See: http://sqlite.org/c3ref/declare_vtab.html | |||
func (c *SQLiteConn) DeclareVTab(sql string) error { | |||
zSQL := C.CString(sql) | |||
defer C.free(unsafe.Pointer(zSQL)) | |||
rv := C.sqlite3_declare_vtab(c.db, zSQL) | |||
if rv != C.SQLITE_OK { | |||
return c.lastError() | |||
} | |||
return nil | |||
} | |||
// CreateModule registers a virtual table implementation. | |||
// See: http://sqlite.org/c3ref/create_module.html | |||
func (c *SQLiteConn) CreateModule(moduleName string, module Module) error { | |||
mname := C.CString(moduleName) | |||
defer C.free(unsafe.Pointer(mname)) | |||
udm := sqliteModule{c, moduleName, module} | |||
switch module.(type) { | |||
case EponymousOnlyModule: | |||
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 | |||
} |
@ -0,0 +1,609 @@ | |||
// 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. | |||
//go:build sqlite_vtable || vtable | |||
// +build sqlite_vtable vtable | |||
package sqlite3 | |||
import ( | |||
"database/sql" | |||
"errors" | |||
"fmt" | |||
"os" | |||
"reflect" | |||
"strings" | |||
"testing" | |||
) | |||
type testModule struct { | |||
t *testing.T | |||
intarray []int | |||
} | |||
type testVTab struct { | |||
intarray []int | |||
} | |||
type testVTabCursor struct { | |||
vTab *testVTab | |||
index int | |||
} | |||
func (m testModule) Create(c *SQLiteConn, args []string) (VTab, error) { | |||
if len(args) != 6 { | |||
m.t.Fatal("six arguments expected") | |||
} | |||
if args[0] != "test" { | |||
m.t.Fatal("module name") | |||
} | |||
if args[1] != "main" { | |||
m.t.Fatal("db name") | |||
} | |||
if args[2] != "vtab" { | |||
m.t.Fatal("table name") | |||
} | |||
if args[3] != "'1'" { | |||
m.t.Fatal("first arg") | |||
} | |||
if args[4] != "2" { | |||
m.t.Fatal("second arg") | |||
} | |||
if args[5] != "three" { | |||
m.t.Fatal("third argsecond arg") | |||
} | |||
err := c.DeclareVTab("CREATE TABLE x(test TEXT)") | |||
if err != nil { | |||
return nil, err | |||
} | |||
return &testVTab{m.intarray}, nil | |||
} | |||
func (m testModule) Connect(c *SQLiteConn, args []string) (VTab, error) { | |||
return m.Create(c, args) | |||
} | |||
func (m testModule) DestroyModule() {} | |||
func (v *testVTab) BestIndex(cst []InfoConstraint, ob []InfoOrderBy) (*IndexResult, error) { | |||
used := make([]bool, 0, len(cst)) | |||
for range cst { | |||
used = append(used, false) | |||
} | |||
return &IndexResult{ | |||
Used: used, | |||
IdxNum: 0, | |||
IdxStr: "test-index", | |||
AlreadyOrdered: true, | |||
EstimatedCost: 100, | |||
EstimatedRows: 200, | |||
}, nil | |||
} | |||
func (v *testVTab) Disconnect() error { | |||
return nil | |||
} | |||
func (v *testVTab) Destroy() error { | |||
return nil | |||
} | |||
func (v *testVTab) Open() (VTabCursor, error) { | |||
return &testVTabCursor{v, 0}, nil | |||
} | |||
func (vc *testVTabCursor) Close() error { | |||
return nil | |||
} | |||
func (vc *testVTabCursor) Filter(idxNum int, idxStr string, vals []interface{}) error { | |||
vc.index = 0 | |||
return nil | |||
} | |||
func (vc *testVTabCursor) Next() error { | |||
vc.index++ | |||
return nil | |||
} | |||
func (vc *testVTabCursor) EOF() bool { | |||
return vc.index >= len(vc.vTab.intarray) | |||
} | |||
func (vc *testVTabCursor) Column(c *SQLiteContext, col int) error { | |||
if col != 0 { | |||
return fmt.Errorf("column index out of bounds: %d", col) | |||
} | |||
c.ResultInt(vc.vTab.intarray[vc.index]) | |||
return nil | |||
} | |||
func (vc *testVTabCursor) Rowid() (int64, error) { | |||
return int64(vc.index), nil | |||
} | |||
func TestCreateModule(t *testing.T) { | |||
tempFilename := TempFilename(t) | |||
defer os.Remove(tempFilename) | |||
intarray := []int{1, 2, 3} | |||
sql.Register("sqlite3_TestCreateModule", &SQLiteDriver{ | |||
ConnectHook: func(conn *SQLiteConn) error { | |||
return conn.CreateModule("test", testModule{t, intarray}) | |||
}, | |||
}) | |||
db, err := sql.Open("sqlite3_TestCreateModule", tempFilename) | |||
if err != nil { | |||
t.Fatalf("could not open db: %v", err) | |||
} | |||
_, err = db.Exec("CREATE VIRTUAL TABLE vtab USING test('1', 2, three)") | |||
if err != nil { | |||
t.Fatalf("could not create vtable: %v", err) | |||
} | |||
var i, value int | |||
rows, err := db.Query("SELECT rowid, * FROM vtab WHERE test = '3'") | |||
if err != nil { | |||
t.Fatalf("couldn't select from virtual table: %v", err) | |||
} | |||
for rows.Next() { | |||
rows.Scan(&i, &value) | |||
if intarray[i] != value { | |||
t.Fatalf("want %v but %v", intarray[i], value) | |||
} | |||
} | |||
_, err = db.Exec("DROP TABLE vtab") | |||
if err != nil { | |||
t.Fatalf("couldn't drop virtual table: %v", err) | |||
} | |||
} | |||
func TestVUpdate(t *testing.T) { | |||
tempFilename := TempFilename(t) | |||
defer os.Remove(tempFilename) | |||
// create module | |||
updateMod := &vtabUpdateModule{t, make(map[string]*vtabUpdateTable)} | |||
// register module | |||
sql.Register("sqlite3_TestVUpdate", &SQLiteDriver{ | |||
ConnectHook: func(conn *SQLiteConn) error { | |||
return conn.CreateModule("updatetest", updateMod) | |||
}, | |||
}) | |||
// connect | |||
db, err := sql.Open("sqlite3_TestVUpdate", tempFilename) | |||
if err != nil { | |||
t.Fatalf("could not open db: %v", err) | |||
} | |||
// create test table | |||
_, err = db.Exec(`CREATE VIRTUAL TABLE vt USING updatetest(f1 integer, f2 text, f3 text)`) | |||
if err != nil { | |||
t.Fatalf("could not create updatetest vtable vt, got: %v", err) | |||
} | |||
// check that table is defined properly | |||
if len(updateMod.tables) != 1 { | |||
t.Fatalf("expected exactly 1 table to exist, got: %d", len(updateMod.tables)) | |||
} | |||
if _, ok := updateMod.tables["vt"]; !ok { | |||
t.Fatalf("expected table `vt` to exist in tables") | |||
} | |||
// check nothing in updatetest | |||
rows, err := db.Query(`select * from vt`) | |||
if err != nil { | |||
t.Fatalf("could not query vt, got: %v", err) | |||
} | |||
i, err := getRowCount(rows) | |||
if err != nil { | |||
t.Fatalf("expected no error, got: %v", err) | |||
} | |||
if i != 0 { | |||
t.Fatalf("expected no rows in vt, got: %d", i) | |||
} | |||
_, err = db.Exec(`delete from vt where f1 = 'yes'`) | |||
if err != nil { | |||
t.Fatalf("expected error on delete, got nil") | |||
} | |||
// test bad column name | |||
_, err = db.Exec(`insert into vt (f4) values('a')`) | |||
if err == nil { | |||
t.Fatalf("expected error on insert, got nil") | |||
} | |||
// insert to vt | |||
res, err := db.Exec(`insert into vt (f1, f2, f3) values (115, 'b', 'c'), (116, 'd', 'e')`) | |||
if err != nil { | |||
t.Fatalf("expected no error on insert, got: %v", err) | |||
} | |||
n, err := res.RowsAffected() | |||
if err != nil { | |||
t.Fatalf("expected no error, got: %v", err) | |||
} | |||
if n != 2 { | |||
t.Fatalf("expected 1 row affected, got: %d", n) | |||
} | |||
// check vt table | |||
vt := updateMod.tables["vt"] | |||
if len(vt.data) != 2 { | |||
t.Fatalf("expected table vt to have exactly 2 rows, got: %d", len(vt.data)) | |||
} | |||
if !reflect.DeepEqual(vt.data[0], []interface{}{int64(115), "b", "c"}) { | |||
t.Fatalf("expected table vt entry 0 to be [115 b c], instead: %v", vt.data[0]) | |||
} | |||
if !reflect.DeepEqual(vt.data[1], []interface{}{int64(116), "d", "e"}) { | |||
t.Fatalf("expected table vt entry 1 to be [116 d e], instead: %v", vt.data[1]) | |||
} | |||
// query vt | |||
var f1 int | |||
var f2, f3 string | |||
err = db.QueryRow(`select * from vt where f1 = 115`).Scan(&f1, &f2, &f3) | |||
if err != nil { | |||
t.Fatalf("expected no error on vt query, got: %v", err) | |||
} | |||
// check column values | |||
if f1 != 115 || f2 != "b" || f3 != "c" { | |||
t.Errorf("expected f1==115, f2==b, f3==c, got: %d, %q, %q", f1, f2, f3) | |||
} | |||
// update vt | |||
res, err = db.Exec(`update vt set f1=117, f2='f' where f3='e'`) | |||
if err != nil { | |||
t.Fatalf("expected no error, got: %v", err) | |||
} | |||
n, err = res.RowsAffected() | |||
if err != nil { | |||
t.Fatalf("expected no error, got: %v", err) | |||
} | |||
if n != 1 { | |||
t.Fatalf("expected exactly one row updated, got: %d", n) | |||
} | |||
// check vt table | |||
if len(vt.data) != 2 { | |||
t.Fatalf("expected table vt to have exactly 2 rows, got: %d", len(vt.data)) | |||
} | |||
if !reflect.DeepEqual(vt.data[0], []interface{}{int64(115), "b", "c"}) { | |||
t.Fatalf("expected table vt entry 0 to be [115 b c], instead: %v", vt.data[0]) | |||
} | |||
if !reflect.DeepEqual(vt.data[1], []interface{}{int64(117), "f", "e"}) { | |||
t.Fatalf("expected table vt entry 1 to be [117 f e], instead: %v", vt.data[1]) | |||
} | |||
// delete from vt | |||
res, err = db.Exec(`delete from vt where f1 = 117`) | |||
if err != nil { | |||
t.Fatalf("expected no error, got: %v", err) | |||
} | |||
n, err = res.RowsAffected() | |||
if err != nil { | |||
t.Fatalf("expected no error, got: %v", err) | |||
} | |||
if n != 1 { | |||
t.Fatalf("expected exactly one row deleted, got: %d", n) | |||
} | |||
// check vt table | |||
if len(vt.data) != 1 { | |||
t.Fatalf("expected table vt to have exactly 1 row, got: %d", len(vt.data)) | |||
} | |||
if !reflect.DeepEqual(vt.data[0], []interface{}{int64(115), "b", "c"}) { | |||
t.Fatalf("expected table vt entry 0 to be [115 b c], instead: %v", vt.data[0]) | |||
} | |||
// check updatetest has 1 result | |||
rows, err = db.Query(`select * from vt`) | |||
if err != nil { | |||
t.Fatalf("could not query vt, got: %v", err) | |||
} | |||
i, err = getRowCount(rows) | |||
if err != nil { | |||
t.Fatalf("expected no error, got: %v", err) | |||
} | |||
if i != 1 { | |||
t.Fatalf("expected 1 row in vt, got: %d", i) | |||
} | |||
} | |||
func getRowCount(rows *sql.Rows) (int, error) { | |||
var i int | |||
for rows.Next() { | |||
i++ | |||
} | |||
return i, nil | |||
} | |||
type vtabUpdateModule struct { | |||
t *testing.T | |||
tables map[string]*vtabUpdateTable | |||
} | |||
func (m *vtabUpdateModule) Create(c *SQLiteConn, args []string) (VTab, error) { | |||
if len(args) < 2 { | |||
return nil, errors.New("must declare at least one column") | |||
} | |||
// get database name, table name, and column declarations ... | |||
dbname, tname, decls := args[1], args[2], args[3:] | |||
// extract column names + types from parameters declarations | |||
cols, typs := make([]string, len(decls)), make([]string, len(decls)) | |||
for i := 0; i < len(decls); i++ { | |||
n, typ := decls[i], "" | |||
if j := strings.IndexAny(n, " \t\n"); j != -1 { | |||
typ, n = strings.TrimSpace(n[j+1:]), n[:j] | |||
} | |||
cols[i], typs[i] = n, typ | |||
} | |||
// declare table | |||
err := c.DeclareVTab(fmt.Sprintf(`CREATE TABLE "%s"."%s" (%s)`, dbname, tname, strings.Join(decls, ","))) | |||
if err != nil { | |||
return nil, err | |||
} | |||
// create table | |||
vtab := &vtabUpdateTable{m.t, dbname, tname, cols, typs, make([][]interface{}, 0)} | |||
m.tables[tname] = vtab | |||
return vtab, nil | |||
} | |||
func (m *vtabUpdateModule) Connect(c *SQLiteConn, args []string) (VTab, error) { | |||
return m.Create(c, args) | |||
} | |||
func (m *vtabUpdateModule) DestroyModule() {} | |||
type vtabUpdateTable struct { | |||
t *testing.T | |||
db string | |||
name string | |||
cols []string | |||
typs []string | |||
data [][]interface{} | |||
} | |||
func (t *vtabUpdateTable) Open() (VTabCursor, error) { | |||
return &vtabUpdateCursor{t, 0}, nil | |||
} | |||
func (t *vtabUpdateTable) BestIndex(cst []InfoConstraint, ob []InfoOrderBy) (*IndexResult, error) { | |||
return &IndexResult{Used: make([]bool, len(cst))}, nil | |||
} | |||
func (t *vtabUpdateTable) Disconnect() error { | |||
return nil | |||
} | |||
func (t *vtabUpdateTable) Destroy() error { | |||
return nil | |||
} | |||
func (t *vtabUpdateTable) Insert(id interface{}, vals []interface{}) (int64, error) { | |||
var i int64 | |||
if id == nil { | |||
i, t.data = int64(len(t.data)), append(t.data, vals) | |||
return i, nil | |||
} | |||
var ok bool | |||
i, ok = id.(int64) | |||
if !ok { | |||
return 0, fmt.Errorf("id is invalid type: %T", id) | |||
} | |||
t.data[i] = vals | |||
return i, nil | |||
} | |||
func (t *vtabUpdateTable) Update(id interface{}, vals []interface{}) error { | |||
i, ok := id.(int64) | |||
if !ok { | |||
return fmt.Errorf("id is invalid type: %T", id) | |||
} | |||
if int(i) >= len(t.data) || i < 0 { | |||
return fmt.Errorf("invalid row id %d", i) | |||
} | |||
t.data[int(i)] = vals | |||
return nil | |||
} | |||
func (t *vtabUpdateTable) Delete(id interface{}) error { | |||
i, ok := id.(int64) | |||
if !ok { | |||
return fmt.Errorf("id is invalid type: %T", id) | |||
} | |||
if int(i) >= len(t.data) || i < 0 { | |||
return fmt.Errorf("invalid row id %d", i) | |||
} | |||
t.data = append(t.data[:i], t.data[i+1:]...) | |||
return nil | |||
} | |||
type vtabUpdateCursor struct { | |||
t *vtabUpdateTable | |||
i int | |||
} | |||
func (c *vtabUpdateCursor) Column(ctxt *SQLiteContext, col int) error { | |||
switch x := c.t.data[c.i][col].(type) { | |||
case []byte: | |||
ctxt.ResultBlob(x) | |||
case bool: | |||
ctxt.ResultBool(x) | |||
case float64: | |||
ctxt.ResultDouble(x) | |||
case int: | |||
ctxt.ResultInt(x) | |||
case int64: | |||
ctxt.ResultInt64(x) | |||
case nil: | |||
ctxt.ResultNull() | |||
case string: | |||
ctxt.ResultText(x) | |||
default: | |||
ctxt.ResultText(fmt.Sprintf("%v", x)) | |||
} | |||
return nil | |||
} | |||
func (c *vtabUpdateCursor) Filter(ixNum int, ixName string, vals []interface{}) error { | |||
return nil | |||
} | |||
func (c *vtabUpdateCursor) Next() error { | |||
c.i++ | |||
return nil | |||
} | |||
func (c *vtabUpdateCursor) EOF() bool { | |||
return c.i >= len(c.t.data) | |||
} | |||
func (c *vtabUpdateCursor) Rowid() (int64, error) { | |||
return int64(c.i), nil | |||
} | |||
func (c *vtabUpdateCursor) Close() error { | |||
return nil | |||
} | |||
type testModuleEponymousOnly struct { | |||
t *testing.T | |||
intarray []int | |||
} | |||
type testVTabEponymousOnly struct { | |||
intarray []int | |||
} | |||
type testVTabCursorEponymousOnly struct { | |||
vTab *testVTabEponymousOnly | |||
index int | |||
} | |||
func (m testModuleEponymousOnly) EponymousOnlyModule() {} | |||
func (m testModuleEponymousOnly) Create(c *SQLiteConn, args []string) (VTab, error) { | |||
err := c.DeclareVTab("CREATE TABLE x(test INT)") | |||
if err != nil { | |||
return nil, err | |||
} | |||
return &testVTabEponymousOnly{m.intarray}, nil | |||
} | |||
func (m testModuleEponymousOnly) Connect(c *SQLiteConn, args []string) (VTab, error) { | |||
return m.Create(c, args) | |||
} | |||
func (m testModuleEponymousOnly) DestroyModule() {} | |||
func (v *testVTabEponymousOnly) BestIndex(cst []InfoConstraint, ob []InfoOrderBy) (*IndexResult, error) { | |||
used := make([]bool, 0, len(cst)) | |||
for range cst { | |||
used = append(used, false) | |||
} | |||
return &IndexResult{ | |||
Used: used, | |||
IdxNum: 0, | |||
IdxStr: "test-index", | |||
AlreadyOrdered: true, | |||
EstimatedCost: 100, | |||
EstimatedRows: 200, | |||
}, nil | |||
} | |||
func (v *testVTabEponymousOnly) Disconnect() error { | |||
return nil | |||
} | |||
func (v *testVTabEponymousOnly) Destroy() error { | |||
return nil | |||
} | |||
func (v *testVTabEponymousOnly) Open() (VTabCursor, error) { | |||
return &testVTabCursorEponymousOnly{v, 0}, nil | |||
} | |||
func (vc *testVTabCursorEponymousOnly) Close() error { | |||
return nil | |||
} | |||
func (vc *testVTabCursorEponymousOnly) Filter(idxNum int, idxStr string, vals []interface{}) error { | |||
vc.index = 0 | |||
return nil | |||
} | |||
func (vc *testVTabCursorEponymousOnly) Next() error { | |||
vc.index++ | |||
return nil | |||
} | |||
func (vc *testVTabCursorEponymousOnly) EOF() bool { | |||
return vc.index >= len(vc.vTab.intarray) | |||
} | |||
func (vc *testVTabCursorEponymousOnly) Column(c *SQLiteContext, col int) error { | |||
if col != 0 { | |||
return fmt.Errorf("column index out of bounds: %d", col) | |||
} | |||
c.ResultInt(vc.vTab.intarray[vc.index]) | |||
return nil | |||
} | |||
func (vc *testVTabCursorEponymousOnly) Rowid() (int64, error) { | |||
return int64(vc.index), nil | |||
} | |||
func TestCreateModuleEponymousOnly(t *testing.T) { | |||
tempFilename := TempFilename(t) | |||
defer os.Remove(tempFilename) | |||
intarray := []int{1, 2, 3} | |||
sql.Register("sqlite3_TestCreateModuleEponymousOnly", &SQLiteDriver{ | |||
ConnectHook: func(conn *SQLiteConn) error { | |||
return conn.CreateModule("test", testModuleEponymousOnly{t, intarray}) | |||
}, | |||
}) | |||
db, err := sql.Open("sqlite3_TestCreateModuleEponymousOnly", tempFilename) | |||
if err != nil { | |||
t.Fatalf("could not open db: %v", err) | |||
} | |||
var i, value int | |||
rows, err := db.Query("SELECT rowid, * FROM test") | |||
if err != nil { | |||
t.Fatalf("couldn't select from virtual table: %v", err) | |||
} | |||
for rows.Next() { | |||
err := rows.Scan(&i, &value) | |||
if err != nil { | |||
t.Fatal(err) | |||
} | |||
if intarray[i] != value { | |||
t.Fatalf("want %v but %v", intarray[i], value) | |||
} | |||
} | |||
_, err = db.Exec("DROP TABLE test") | |||
if err != nil { | |||
t.Logf("couldn't drop virtual table: %v", err) | |||
} | |||
} |
@ -0,0 +1,17 @@ | |||
// 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 !windows | |||
package sqlite3 | |||
/* | |||
#cgo CFLAGS: -I. | |||
#cgo linux LDFLAGS: -ldl | |||
#cgo linux,ppc LDFLAGS: -lpthread | |||
#cgo linux,ppc64 LDFLAGS: -lpthread | |||
#cgo linux,ppc64le LDFLAGS: -lpthread | |||
*/ | |||
import "C" |
@ -0,0 +1,14 @@ | |||
// 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 solaris | |||
package sqlite3 | |||
/* | |||
#cgo CFLAGS: -D__EXTENSIONS__=1 | |||
#cgo LDFLAGS: -lc | |||
*/ | |||
import "C" |
@ -0,0 +1,287 @@ | |||
// 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_trace trace | |||
package sqlite3 | |||
/* | |||
#ifndef USE_LIBSQLITE3 | |||
#include "sqlite3-binding.h" | |||
#else | |||
#include <sqlite3.h> | |||
#endif | |||
#include <stdlib.h> | |||
int traceCallbackTrampoline(unsigned int traceEventCode, void *ctx, void *p, void *x); | |||
*/ | |||
import "C" | |||
import ( | |||
"fmt" | |||
"strings" | |||
"sync" | |||
"unsafe" | |||
) | |||
// Trace... constants identify the possible events causing callback invocation. | |||
// Values are same as the corresponding SQLite Trace Event Codes. | |||
const ( | |||
TraceStmt = uint32(C.SQLITE_TRACE_STMT) | |||
TraceProfile = uint32(C.SQLITE_TRACE_PROFILE) | |||
TraceRow = uint32(C.SQLITE_TRACE_ROW) | |||
TraceClose = uint32(C.SQLITE_TRACE_CLOSE) | |||
) | |||
type TraceInfo struct { | |||
// Pack together the shorter fields, to keep the struct smaller. | |||
// On a 64-bit machine there would be padding | |||
// between EventCode and ConnHandle; having AutoCommit here is "free": | |||
EventCode uint32 | |||
AutoCommit bool | |||
ConnHandle uintptr | |||
// Usually filled, unless EventCode = TraceClose = SQLITE_TRACE_CLOSE: | |||
// identifier for a prepared statement: | |||
StmtHandle uintptr | |||
// Two strings filled when EventCode = TraceStmt = SQLITE_TRACE_STMT: | |||
// (1) either the unexpanded SQL text of the prepared statement, or | |||
// an SQL comment that indicates the invocation of a trigger; | |||
// (2) expanded SQL, if requested and if (1) is not an SQL comment. | |||
StmtOrTrigger string | |||
ExpandedSQL string // only if requested (TraceConfig.WantExpandedSQL = true) | |||
// filled when EventCode = TraceProfile = SQLITE_TRACE_PROFILE: | |||
// estimated number of nanoseconds that the prepared statement took to run: | |||
RunTimeNanosec int64 | |||
DBError Error | |||
} | |||
// TraceUserCallback gives the signature for a trace function | |||
// provided by the user (Go application programmer). | |||
// SQLite 3.14 documentation (as of September 2, 2016) | |||
// for SQL Trace Hook = sqlite3_trace_v2(): | |||
// The integer return value from the callback is currently ignored, | |||
// though this may change in future releases. Callback implementations | |||
// should return zero to ensure future compatibility. | |||
type TraceUserCallback func(TraceInfo) int | |||
type TraceConfig struct { | |||
Callback TraceUserCallback | |||
EventMask uint32 | |||
WantExpandedSQL bool | |||
} | |||
func fillDBError(dbErr *Error, db *C.sqlite3) { | |||
// See SQLiteConn.lastError(), in file 'sqlite3.go' at the time of writing (Sept 5, 2016) | |||
dbErr.Code = ErrNo(C.sqlite3_errcode(db)) | |||
dbErr.ExtendedCode = ErrNoExtended(C.sqlite3_extended_errcode(db)) | |||
dbErr.err = C.GoString(C.sqlite3_errmsg(db)) | |||
} | |||
func fillExpandedSQL(info *TraceInfo, db *C.sqlite3, pStmt unsafe.Pointer) { | |||
if pStmt == nil { | |||
panic("No SQLite statement pointer in P arg of trace_v2 callback") | |||
} | |||
expSQLiteCStr := C.sqlite3_expanded_sql((*C.sqlite3_stmt)(pStmt)) | |||
defer C.sqlite3_free(unsafe.Pointer(expSQLiteCStr)) | |||
if expSQLiteCStr == nil { | |||
fillDBError(&info.DBError, db) | |||
return | |||
} | |||
info.ExpandedSQL = C.GoString(expSQLiteCStr) | |||
} | |||
//export traceCallbackTrampoline | |||
func traceCallbackTrampoline( | |||
traceEventCode C.uint, | |||
// Parameter named 'C' in SQLite docs = Context given at registration: | |||
ctx unsafe.Pointer, | |||
// Parameter named 'P' in SQLite docs (Primary event data?): | |||
p unsafe.Pointer, | |||
// Parameter named 'X' in SQLite docs (eXtra event data?): | |||
xValue unsafe.Pointer) C.int { | |||
eventCode := uint32(traceEventCode) | |||
if ctx == nil { | |||
panic(fmt.Sprintf("No context (ev 0x%x)", traceEventCode)) | |||
} | |||
contextDB := (*C.sqlite3)(ctx) | |||
connHandle := uintptr(ctx) | |||
var traceConf TraceConfig | |||
var found bool | |||
if eventCode == TraceClose { | |||
// clean up traceMap: 'pop' means get and delete | |||
traceConf, found = popTraceMapping(connHandle) | |||
} else { | |||
traceConf, found = lookupTraceMapping(connHandle) | |||
} | |||
if !found { | |||
panic(fmt.Sprintf("Mapping not found for handle 0x%x (ev 0x%x)", | |||
connHandle, eventCode)) | |||
} | |||
var info TraceInfo | |||
info.EventCode = eventCode | |||
info.AutoCommit = (int(C.sqlite3_get_autocommit(contextDB)) != 0) | |||
info.ConnHandle = connHandle | |||
switch eventCode { | |||
case TraceStmt: | |||
info.StmtHandle = uintptr(p) | |||
var xStr string | |||
if xValue != nil { | |||
xStr = C.GoString((*C.char)(xValue)) | |||
} | |||
info.StmtOrTrigger = xStr | |||
if !strings.HasPrefix(xStr, "--") { | |||
// Not SQL comment, therefore the current event | |||
// is not related to a trigger. | |||
// The user might want to receive the expanded SQL; | |||
// let's check: | |||
if traceConf.WantExpandedSQL { | |||
fillExpandedSQL(&info, contextDB, p) | |||
} | |||
} | |||
case TraceProfile: | |||
info.StmtHandle = uintptr(p) | |||
if xValue == nil { | |||
panic("NULL pointer in X arg of trace_v2 callback for SQLITE_TRACE_PROFILE event") | |||
} | |||
info.RunTimeNanosec = *(*int64)(xValue) | |||
// sample the error //TODO: is it safe? is it useful? | |||
fillDBError(&info.DBError, contextDB) | |||
case TraceRow: | |||
info.StmtHandle = uintptr(p) | |||
case TraceClose: | |||
handle := uintptr(p) | |||
if handle != info.ConnHandle { | |||
panic(fmt.Sprintf("Different conn handle 0x%x (expected 0x%x) in SQLITE_TRACE_CLOSE event.", | |||
handle, info.ConnHandle)) | |||
} | |||
default: | |||
// Pass unsupported events to the user callback (if configured); | |||
// let the user callback decide whether to panic or ignore them. | |||
} | |||
// Do not execute user callback when the event was not requested by user! | |||
// Remember that the Close event is always selected when | |||
// registering this callback trampoline with SQLite --- for cleanup. | |||
// In the future there may be more events forced to "selected" in SQLite | |||
// for the driver's needs. | |||
if traceConf.EventMask&eventCode == 0 { | |||
return 0 | |||
} | |||
r := 0 | |||
if traceConf.Callback != nil { | |||
r = traceConf.Callback(info) | |||
} | |||
return C.int(r) | |||
} | |||
type traceMapEntry struct { | |||
config TraceConfig | |||
} | |||
var traceMapLock sync.Mutex | |||
var traceMap = make(map[uintptr]traceMapEntry) | |||
func addTraceMapping(connHandle uintptr, traceConf TraceConfig) { | |||
traceMapLock.Lock() | |||
defer traceMapLock.Unlock() | |||
oldEntryCopy, found := traceMap[connHandle] | |||
if found { | |||
panic(fmt.Sprintf("Adding trace config %v: handle 0x%x already registered (%v).", | |||
traceConf, connHandle, oldEntryCopy.config)) | |||
} | |||
traceMap[connHandle] = traceMapEntry{config: traceConf} | |||
} | |||
func lookupTraceMapping(connHandle uintptr) (TraceConfig, bool) { | |||
traceMapLock.Lock() | |||
defer traceMapLock.Unlock() | |||
entryCopy, found := traceMap[connHandle] | |||
return entryCopy.config, found | |||
} | |||
// 'pop' = get and delete from map before returning the value to the caller | |||
func popTraceMapping(connHandle uintptr) (TraceConfig, bool) { | |||
traceMapLock.Lock() | |||
defer traceMapLock.Unlock() | |||
entryCopy, found := traceMap[connHandle] | |||
if found { | |||
delete(traceMap, connHandle) | |||
} | |||
return entryCopy.config, found | |||
} | |||
// SetTrace installs or removes the trace callback for the given database connection. | |||
// It's not named 'RegisterTrace' because only one callback can be kept and called. | |||
// Calling SetTrace a second time on same database connection | |||
// overrides (cancels) any prior callback and all its settings: | |||
// event mask, etc. | |||
func (c *SQLiteConn) SetTrace(requested *TraceConfig) error { | |||
connHandle := uintptr(unsafe.Pointer(c.db)) | |||
_, _ = popTraceMapping(connHandle) | |||
if requested == nil { | |||
// The traceMap entry was deleted already by popTraceMapping(): | |||
// can disable all events now, no need to watch for TraceClose. | |||
err := c.setSQLiteTrace(0) | |||
return err | |||
} | |||
reqCopy := *requested | |||
// Disable potentially expensive operations | |||
// if their result will not be used. We are doing this | |||
// just in case the caller provided nonsensical input. | |||
if reqCopy.EventMask&TraceStmt == 0 { | |||
reqCopy.WantExpandedSQL = false | |||
} | |||
addTraceMapping(connHandle, reqCopy) | |||
// The callback trampoline function does cleanup on Close event, | |||
// regardless of the presence or absence of the user callback. | |||
// Therefore it needs the Close event to be selected: | |||
actualEventMask := uint(reqCopy.EventMask | TraceClose) | |||
err := c.setSQLiteTrace(actualEventMask) | |||
return err | |||
} | |||
func (c *SQLiteConn) setSQLiteTrace(sqliteEventMask uint) error { | |||
rv := C.sqlite3_trace_v2(c.db, | |||
C.uint(sqliteEventMask), | |||
(*[0]byte)(unsafe.Pointer(C.traceCallbackTrampoline)), | |||
unsafe.Pointer(c.db)) // Fourth arg is same as first: we are | |||
// passing the database connection handle as callback context. | |||
if rv != C.SQLITE_OK { | |||
return c.lastError() | |||
} | |||
return nil | |||
} |
@ -0,0 +1,108 @@ | |||
// 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. | |||
package sqlite3 | |||
/* | |||
#ifndef USE_LIBSQLITE3 | |||
#include "sqlite3-binding.h" | |||
#else | |||
#include <sqlite3.h> | |||
#endif | |||
*/ | |||
import "C" | |||
import ( | |||
"database/sql" | |||
"reflect" | |||
"strings" | |||
) | |||
// ColumnTypeDatabaseTypeName implement RowsColumnTypeDatabaseTypeName. | |||
func (rc *SQLiteRows) ColumnTypeDatabaseTypeName(i int) string { | |||
return C.GoString(C.sqlite3_column_decltype(rc.s.s, C.int(i))) | |||
} | |||
/* | |||
func (rc *SQLiteRows) ColumnTypeLength(index int) (length int64, ok bool) { | |||
return 0, false | |||
} | |||
func (rc *SQLiteRows) ColumnTypePrecisionScale(index int) (precision, scale int64, ok bool) { | |||
return 0, 0, false | |||
} | |||
*/ | |||
// ColumnTypeNullable implement RowsColumnTypeNullable. | |||
func (rc *SQLiteRows) ColumnTypeNullable(i int) (nullable, ok bool) { | |||
return true, true | |||
} | |||
// ColumnTypeScanType implement RowsColumnTypeScanType. | |||
func (rc *SQLiteRows) ColumnTypeScanType(i int) reflect.Type { | |||
//ct := C.sqlite3_column_type(rc.s.s, C.int(i)) // Always returns 5 | |||
return scanType(C.GoString(C.sqlite3_column_decltype(rc.s.s, C.int(i)))) | |||
} | |||
const ( | |||
SQLITE_INTEGER = iota | |||
SQLITE_TEXT | |||
SQLITE_BLOB | |||
SQLITE_REAL | |||
SQLITE_NUMERIC | |||
SQLITE_TIME | |||
SQLITE_BOOL | |||
SQLITE_NULL | |||
) | |||
func scanType(cdt string) reflect.Type { | |||
t := strings.ToUpper(cdt) | |||
i := databaseTypeConvSqlite(t) | |||
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 | |||
} |
@ -0,0 +1,41 @@ | |||
// Copyright (C) 2018 G.J.R. Timmer <gjr.timmer@gmail.com>. | |||
// | |||
// Use of this source code is governed by an MIT-style | |||
// license that can be found in the LICENSE file. | |||
// +build cgo | |||
package sqlite3 | |||
// usleep is a function available on *nix based systems. | |||
// This function is not present in Windows. | |||
// Windows has a sleep function but this works with seconds | |||
// and not with microseconds as usleep. | |||
// | |||
// This code should improve performance on windows because | |||
// without the presence of usleep SQLite waits 1 second. | |||
// | |||
// 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> | |||
void usleep(__int64 usec) | |||
{ | |||
HANDLE timer; | |||
LARGE_INTEGER ft; | |||
// Convert to 100 nanosecond interval, negative value indicates relative time | |||
ft.QuadPart = -(10*usec); | |||
timer = CreateWaitableTimer(NULL, TRUE, NULL); | |||
SetWaitableTimer(timer, &ft, 0, NULL, NULL, 0); | |||
WaitForSingleObject(timer, INFINITE); | |||
CloseHandle(timer); | |||
} | |||
*/ | |||
import "C" | |||
// EOF |
@ -0,0 +1,17 @@ | |||
// 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 windows | |||
package sqlite3 | |||
/* | |||
#cgo CFLAGS: -I. | |||
#cgo CFLAGS: -fno-stack-check | |||
#cgo CFLAGS: -fno-stack-protector | |||
#cgo CFLAGS: -mno-stack-arg-probe | |||
#cgo windows,386 CFLAGS: -D_USE_32BIT_TIME_T | |||
*/ | |||
import "C" |
@ -0,0 +1,710 @@ | |||
#ifndef USE_LIBSQLITE3 | |||
/* | |||
** 2006 June 7 | |||
** | |||
** The author disclaims copyright to this source code. In place of | |||
** a legal notice, here is a blessing: | |||
** | |||
** May you do good and not evil. | |||
** May you find forgiveness for yourself and forgive others. | |||
** May you share freely, never taking more than you give. | |||
** | |||
************************************************************************* | |||
** This header file defines the SQLite interface for use by | |||
** shared libraries that want to be imported as extensions into | |||
** an SQLite instance. Shared libraries that intend to be loaded | |||
** as extensions by SQLite should #include this file instead of | |||
** sqlite3.h. | |||
*/ | |||
#ifndef SQLITE3EXT_H | |||
#define SQLITE3EXT_H | |||
#include "sqlite3-binding.h" | |||
#ifdef __clang__ | |||
#define assert(condition) ((void)0) | |||
#endif | |||
/* | |||
** The following structure holds pointers to all of the SQLite API | |||
** routines. | |||
** | |||
** WARNING: In order to maintain backwards compatibility, add new | |||
** interfaces to the end of this structure only. If you insert new | |||
** interfaces in the middle of this structure, then older different | |||
** versions of SQLite will not be able to load each other's shared | |||
** libraries! | |||
*/ | |||
struct sqlite3_api_routines { | |||
void * (*aggregate_context)(sqlite3_context*,int nBytes); | |||
int (*aggregate_count)(sqlite3_context*); | |||
int (*bind_blob)(sqlite3_stmt*,int,const void*,int n,void(*)(void*)); | |||
int (*bind_double)(sqlite3_stmt*,int,double); | |||
int (*bind_int)(sqlite3_stmt*,int,int); | |||
int (*bind_int64)(sqlite3_stmt*,int,sqlite_int64); | |||
int (*bind_null)(sqlite3_stmt*,int); | |||
int (*bind_parameter_count)(sqlite3_stmt*); | |||
int (*bind_parameter_index)(sqlite3_stmt*,const char*zName); | |||
const char * (*bind_parameter_name)(sqlite3_stmt*,int); | |||
int (*bind_text)(sqlite3_stmt*,int,const char*,int n,void(*)(void*)); | |||
int (*bind_text16)(sqlite3_stmt*,int,const void*,int,void(*)(void*)); | |||
int (*bind_value)(sqlite3_stmt*,int,const sqlite3_value*); | |||
int (*busy_handler)(sqlite3*,int(*)(void*,int),void*); | |||
int (*busy_timeout)(sqlite3*,int ms); | |||
int (*changes)(sqlite3*); | |||
int (*close)(sqlite3*); | |||
int (*collation_needed)(sqlite3*,void*,void(*)(void*,sqlite3*, | |||
int eTextRep,const char*)); | |||
int (*collation_needed16)(sqlite3*,void*,void(*)(void*,sqlite3*, | |||
int eTextRep,const void*)); | |||
const void * (*column_blob)(sqlite3_stmt*,int iCol); | |||
int (*column_bytes)(sqlite3_stmt*,int iCol); | |||
int (*column_bytes16)(sqlite3_stmt*,int iCol); | |||
int (*column_count)(sqlite3_stmt*pStmt); | |||
const char * (*column_database_name)(sqlite3_stmt*,int); | |||
const void * (*column_database_name16)(sqlite3_stmt*,int); | |||
const char * (*column_decltype)(sqlite3_stmt*,int i); | |||
const void * (*column_decltype16)(sqlite3_stmt*,int); | |||
double (*column_double)(sqlite3_stmt*,int iCol); | |||
int (*column_int)(sqlite3_stmt*,int iCol); | |||
sqlite_int64 (*column_int64)(sqlite3_stmt*,int iCol); | |||
const char * (*column_name)(sqlite3_stmt*,int); | |||
const void * (*column_name16)(sqlite3_stmt*,int); | |||
const char * (*column_origin_name)(sqlite3_stmt*,int); | |||
const void * (*column_origin_name16)(sqlite3_stmt*,int); | |||
const char * (*column_table_name)(sqlite3_stmt*,int); | |||
const void * (*column_table_name16)(sqlite3_stmt*,int); | |||
const unsigned char * (*column_text)(sqlite3_stmt*,int iCol); | |||
const void * (*column_text16)(sqlite3_stmt*,int iCol); | |||
int (*column_type)(sqlite3_stmt*,int iCol); | |||
sqlite3_value* (*column_value)(sqlite3_stmt*,int iCol); | |||
void * (*commit_hook)(sqlite3*,int(*)(void*),void*); | |||
int (*complete)(const char*sql); | |||
int (*complete16)(const void*sql); | |||
int (*create_collation)(sqlite3*,const char*,int,void*, | |||
int(*)(void*,int,const void*,int,const void*)); | |||
int (*create_collation16)(sqlite3*,const void*,int,void*, | |||
int(*)(void*,int,const void*,int,const void*)); | |||
int (*create_function)(sqlite3*,const char*,int,int,void*, | |||
void (*xFunc)(sqlite3_context*,int,sqlite3_value**), | |||
void (*xStep)(sqlite3_context*,int,sqlite3_value**), | |||
void (*xFinal)(sqlite3_context*)); | |||
int (*create_function16)(sqlite3*,const void*,int,int,void*, | |||
void (*xFunc)(sqlite3_context*,int,sqlite3_value**), | |||
void (*xStep)(sqlite3_context*,int,sqlite3_value**), | |||
void (*xFinal)(sqlite3_context*)); | |||
int (*create_module)(sqlite3*,const char*,const sqlite3_module*,void*); | |||
int (*data_count)(sqlite3_stmt*pStmt); | |||
sqlite3 * (*db_handle)(sqlite3_stmt*); | |||
int (*declare_vtab)(sqlite3*,const char*); | |||
int (*enable_shared_cache)(int); | |||
int (*errcode)(sqlite3*db); | |||
const char * (*errmsg)(sqlite3*); | |||
const void * (*errmsg16)(sqlite3*); | |||
int (*exec)(sqlite3*,const char*,sqlite3_callback,void*,char**); | |||
int (*expired)(sqlite3_stmt*); | |||
int (*finalize)(sqlite3_stmt*pStmt); | |||
void (*free)(void*); | |||
void (*free_table)(char**result); | |||
int (*get_autocommit)(sqlite3*); | |||
void * (*get_auxdata)(sqlite3_context*,int); | |||
int (*get_table)(sqlite3*,const char*,char***,int*,int*,char**); | |||
int (*global_recover)(void); | |||
void (*interruptx)(sqlite3*); | |||
sqlite_int64 (*last_insert_rowid)(sqlite3*); | |||
const char * (*libversion)(void); | |||
int (*libversion_number)(void); | |||
void *(*malloc)(int); | |||
char * (*mprintf)(const char*,...); | |||
int (*open)(const char*,sqlite3**); | |||
int (*open16)(const void*,sqlite3**); | |||
int (*prepare)(sqlite3*,const char*,int,sqlite3_stmt**,const char**); | |||
int (*prepare16)(sqlite3*,const void*,int,sqlite3_stmt**,const void**); | |||
void * (*profile)(sqlite3*,void(*)(void*,const char*,sqlite_uint64),void*); | |||
void (*progress_handler)(sqlite3*,int,int(*)(void*),void*); | |||
void *(*realloc)(void*,int); | |||
int (*reset)(sqlite3_stmt*pStmt); | |||
void (*result_blob)(sqlite3_context*,const void*,int,void(*)(void*)); | |||
void (*result_double)(sqlite3_context*,double); | |||
void (*result_error)(sqlite3_context*,const char*,int); | |||
void (*result_error16)(sqlite3_context*,const void*,int); | |||
void (*result_int)(sqlite3_context*,int); | |||
void (*result_int64)(sqlite3_context*,sqlite_int64); | |||
void (*result_null)(sqlite3_context*); | |||
void (*result_text)(sqlite3_context*,const char*,int,void(*)(void*)); | |||
void (*result_text16)(sqlite3_context*,const void*,int,void(*)(void*)); | |||
void (*result_text16be)(sqlite3_context*,const void*,int,void(*)(void*)); | |||
void (*result_text16le)(sqlite3_context*,const void*,int,void(*)(void*)); | |||
void (*result_value)(sqlite3_context*,sqlite3_value*); | |||
void * (*rollback_hook)(sqlite3*,void(*)(void*),void*); | |||
int (*set_authorizer)(sqlite3*,int(*)(void*,int,const char*,const char*, | |||
const char*,const char*),void*); | |||
void (*set_auxdata)(sqlite3_context*,int,void*,void (*)(void*)); | |||
char * (*xsnprintf)(int,char*,const char*,...); | |||
int (*step)(sqlite3_stmt*); | |||
int (*table_column_metadata)(sqlite3*,const char*,const char*,const char*, | |||
char const**,char const**,int*,int*,int*); | |||
void (*thread_cleanup)(void); | |||
int (*total_changes)(sqlite3*); | |||
void * (*trace)(sqlite3*,void(*xTrace)(void*,const char*),void*); | |||
int (*transfer_bindings)(sqlite3_stmt*,sqlite3_stmt*); | |||
void * (*update_hook)(sqlite3*,void(*)(void*,int ,char const*,char const*, | |||
sqlite_int64),void*); | |||
void * (*user_data)(sqlite3_context*); | |||
const void * (*value_blob)(sqlite3_value*); | |||
int (*value_bytes)(sqlite3_value*); | |||
int (*value_bytes16)(sqlite3_value*); | |||
double (*value_double)(sqlite3_value*); | |||
int (*value_int)(sqlite3_value*); | |||
sqlite_int64 (*value_int64)(sqlite3_value*); | |||
int (*value_numeric_type)(sqlite3_value*); | |||
const unsigned char * (*value_text)(sqlite3_value*); | |||
const void * (*value_text16)(sqlite3_value*); | |||
const void * (*value_text16be)(sqlite3_value*); | |||
const void * (*value_text16le)(sqlite3_value*); | |||
int (*value_type)(sqlite3_value*); | |||
char *(*vmprintf)(const char*,va_list); | |||
/* Added ??? */ | |||
int (*overload_function)(sqlite3*, const char *zFuncName, int nArg); | |||
/* Added by 3.3.13 */ | |||
int (*prepare_v2)(sqlite3*,const char*,int,sqlite3_stmt**,const char**); | |||
int (*prepare16_v2)(sqlite3*,const void*,int,sqlite3_stmt**,const void**); | |||
int (*clear_bindings)(sqlite3_stmt*); | |||
/* Added by 3.4.1 */ | |||
int (*create_module_v2)(sqlite3*,const char*,const sqlite3_module*,void*, | |||
void (*xDestroy)(void *)); | |||
/* Added by 3.5.0 */ | |||
int (*bind_zeroblob)(sqlite3_stmt*,int,int); | |||
int (*blob_bytes)(sqlite3_blob*); | |||
int (*blob_close)(sqlite3_blob*); | |||
int (*blob_open)(sqlite3*,const char*,const char*,const char*,sqlite3_int64, | |||
int,sqlite3_blob**); | |||
int (*blob_read)(sqlite3_blob*,void*,int,int); | |||
int (*blob_write)(sqlite3_blob*,const void*,int,int); | |||
int (*create_collation_v2)(sqlite3*,const char*,int,void*, | |||
int(*)(void*,int,const void*,int,const void*), | |||
void(*)(void*)); | |||
int (*file_control)(sqlite3*,const char*,int,void*); | |||
sqlite3_int64 (*memory_highwater)(int); | |||
sqlite3_int64 (*memory_used)(void); | |||
sqlite3_mutex *(*mutex_alloc)(int); | |||
void (*mutex_enter)(sqlite3_mutex*); | |||
void (*mutex_free)(sqlite3_mutex*); | |||
void (*mutex_leave)(sqlite3_mutex*); | |||
int (*mutex_try)(sqlite3_mutex*); | |||
int (*open_v2)(const char*,sqlite3**,int,const char*); | |||
int (*release_memory)(int); | |||
void (*result_error_nomem)(sqlite3_context*); | |||
void (*result_error_toobig)(sqlite3_context*); | |||
int (*sleep)(int); | |||
void (*soft_heap_limit)(int); | |||
sqlite3_vfs *(*vfs_find)(const char*); | |||
int (*vfs_register)(sqlite3_vfs*,int); | |||
int (*vfs_unregister)(sqlite3_vfs*); | |||
int (*xthreadsafe)(void); | |||
void (*result_zeroblob)(sqlite3_context*,int); | |||
void (*result_error_code)(sqlite3_context*,int); | |||
int (*test_control)(int, ...); | |||
void (*randomness)(int,void*); | |||
sqlite3 *(*context_db_handle)(sqlite3_context*); | |||
int (*extended_result_codes)(sqlite3*,int); | |||
int (*limit)(sqlite3*,int,int); | |||
sqlite3_stmt *(*next_stmt)(sqlite3*,sqlite3_stmt*); | |||
const char *(*sql)(sqlite3_stmt*); | |||
int (*status)(int,int*,int*,int); | |||
int (*backup_finish)(sqlite3_backup*); | |||
sqlite3_backup *(*backup_init)(sqlite3*,const char*,sqlite3*,const char*); | |||
int (*backup_pagecount)(sqlite3_backup*); | |||
int (*backup_remaining)(sqlite3_backup*); | |||
int (*backup_step)(sqlite3_backup*,int); | |||
const char *(*compileoption_get)(int); | |||
int (*compileoption_used)(const char*); | |||
int (*create_function_v2)(sqlite3*,const char*,int,int,void*, | |||
void (*xFunc)(sqlite3_context*,int,sqlite3_value**), | |||
void (*xStep)(sqlite3_context*,int,sqlite3_value**), | |||
void (*xFinal)(sqlite3_context*), | |||
void(*xDestroy)(void*)); | |||
int (*db_config)(sqlite3*,int,...); | |||
sqlite3_mutex *(*db_mutex)(sqlite3*); | |||
int (*db_status)(sqlite3*,int,int*,int*,int); | |||
int (*extended_errcode)(sqlite3*); | |||
void (*log)(int,const char*,...); | |||
sqlite3_int64 (*soft_heap_limit64)(sqlite3_int64); | |||
const char *(*sourceid)(void); | |||
int (*stmt_status)(sqlite3_stmt*,int,int); | |||
int (*strnicmp)(const char*,const char*,int); | |||
int (*unlock_notify)(sqlite3*,void(*)(void**,int),void*); | |||
int (*wal_autocheckpoint)(sqlite3*,int); | |||
int (*wal_checkpoint)(sqlite3*,const char*); | |||
void *(*wal_hook)(sqlite3*,int(*)(void*,sqlite3*,const char*,int),void*); | |||
int (*blob_reopen)(sqlite3_blob*,sqlite3_int64); | |||
int (*vtab_config)(sqlite3*,int op,...); | |||
int (*vtab_on_conflict)(sqlite3*); | |||
/* Version 3.7.16 and later */ | |||
int (*close_v2)(sqlite3*); | |||
const char *(*db_filename)(sqlite3*,const char*); | |||
int (*db_readonly)(sqlite3*,const char*); | |||
int (*db_release_memory)(sqlite3*); | |||
const char *(*errstr)(int); | |||
int (*stmt_busy)(sqlite3_stmt*); | |||
int (*stmt_readonly)(sqlite3_stmt*); | |||
int (*stricmp)(const char*,const char*); | |||
int (*uri_boolean)(const char*,const char*,int); | |||
sqlite3_int64 (*uri_int64)(const char*,const char*,sqlite3_int64); | |||
const char *(*uri_parameter)(const char*,const char*); | |||
char *(*xvsnprintf)(int,char*,const char*,va_list); | |||
int (*wal_checkpoint_v2)(sqlite3*,const char*,int,int*,int*); | |||
/* Version 3.8.7 and later */ | |||
int (*auto_extension)(void(*)(void)); | |||
int (*bind_blob64)(sqlite3_stmt*,int,const void*,sqlite3_uint64, | |||
void(*)(void*)); | |||
int (*bind_text64)(sqlite3_stmt*,int,const char*,sqlite3_uint64, | |||
void(*)(void*),unsigned char); | |||
int (*cancel_auto_extension)(void(*)(void)); | |||
int (*load_extension)(sqlite3*,const char*,const char*,char**); | |||
void *(*malloc64)(sqlite3_uint64); | |||
sqlite3_uint64 (*msize)(void*); | |||
void *(*realloc64)(void*,sqlite3_uint64); | |||
void (*reset_auto_extension)(void); | |||
void (*result_blob64)(sqlite3_context*,const void*,sqlite3_uint64, | |||
void(*)(void*)); | |||
void (*result_text64)(sqlite3_context*,const char*,sqlite3_uint64, | |||
void(*)(void*), unsigned char); | |||
int (*strglob)(const char*,const char*); | |||
/* Version 3.8.11 and later */ | |||
sqlite3_value *(*value_dup)(const sqlite3_value*); | |||
void (*value_free)(sqlite3_value*); | |||
int (*result_zeroblob64)(sqlite3_context*,sqlite3_uint64); | |||
int (*bind_zeroblob64)(sqlite3_stmt*, int, sqlite3_uint64); | |||
/* Version 3.9.0 and later */ | |||
unsigned int (*value_subtype)(sqlite3_value*); | |||
void (*result_subtype)(sqlite3_context*,unsigned int); | |||
/* Version 3.10.0 and later */ | |||
int (*status64)(int,sqlite3_int64*,sqlite3_int64*,int); | |||
int (*strlike)(const char*,const char*,unsigned int); | |||
int (*db_cacheflush)(sqlite3*); | |||
/* Version 3.12.0 and later */ | |||
int (*system_errno)(sqlite3*); | |||
/* Version 3.14.0 and later */ | |||
int (*trace_v2)(sqlite3*,unsigned,int(*)(unsigned,void*,void*,void*),void*); | |||
char *(*expanded_sql)(sqlite3_stmt*); | |||
/* Version 3.18.0 and later */ | |||
void (*set_last_insert_rowid)(sqlite3*,sqlite3_int64); | |||
/* Version 3.20.0 and later */ | |||
int (*prepare_v3)(sqlite3*,const char*,int,unsigned int, | |||
sqlite3_stmt**,const char**); | |||
int (*prepare16_v3)(sqlite3*,const void*,int,unsigned int, | |||
sqlite3_stmt**,const void**); | |||
int (*bind_pointer)(sqlite3_stmt*,int,void*,const char*,void(*)(void*)); | |||
void (*result_pointer)(sqlite3_context*,void*,const char*,void(*)(void*)); | |||
void *(*value_pointer)(sqlite3_value*,const char*); | |||
int (*vtab_nochange)(sqlite3_context*); | |||
int (*value_nochange)(sqlite3_value*); | |||
const char *(*vtab_collation)(sqlite3_index_info*,int); | |||
/* Version 3.24.0 and later */ | |||
int (*keyword_count)(void); | |||
int (*keyword_name)(int,const char**,int*); | |||
int (*keyword_check)(const char*,int); | |||
sqlite3_str *(*str_new)(sqlite3*); | |||
char *(*str_finish)(sqlite3_str*); | |||
void (*str_appendf)(sqlite3_str*, const char *zFormat, ...); | |||
void (*str_vappendf)(sqlite3_str*, const char *zFormat, va_list); | |||
void (*str_append)(sqlite3_str*, const char *zIn, int N); | |||
void (*str_appendall)(sqlite3_str*, const char *zIn); | |||
void (*str_appendchar)(sqlite3_str*, int N, char C); | |||
void (*str_reset)(sqlite3_str*); | |||
int (*str_errcode)(sqlite3_str*); | |||
int (*str_length)(sqlite3_str*); | |||
char *(*str_value)(sqlite3_str*); | |||
/* Version 3.25.0 and later */ | |||
int (*create_window_function)(sqlite3*,const char*,int,int,void*, | |||
void (*xStep)(sqlite3_context*,int,sqlite3_value**), | |||
void (*xFinal)(sqlite3_context*), | |||
void (*xValue)(sqlite3_context*), | |||
void (*xInv)(sqlite3_context*,int,sqlite3_value**), | |||
void(*xDestroy)(void*)); | |||
/* Version 3.26.0 and later */ | |||
const char *(*normalized_sql)(sqlite3_stmt*); | |||
/* Version 3.28.0 and later */ | |||
int (*stmt_isexplain)(sqlite3_stmt*); | |||
int (*value_frombind)(sqlite3_value*); | |||
/* Version 3.30.0 and later */ | |||
int (*drop_modules)(sqlite3*,const char**); | |||
/* Version 3.31.0 and later */ | |||
sqlite3_int64 (*hard_heap_limit64)(sqlite3_int64); | |||
const char *(*uri_key)(const char*,int); | |||
const char *(*filename_database)(const char*); | |||
const char *(*filename_journal)(const char*); | |||
const char *(*filename_wal)(const char*); | |||
/* Version 3.32.0 and later */ | |||
char *(*create_filename)(const char*,const char*,const char*, | |||
int,const char**); | |||
void (*free_filename)(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); | |||
}; | |||
/* | |||
** This is the function signature used for all extension entry points. It | |||
** is also defined in the file "loadext.c". | |||
*/ | |||
typedef int (*sqlite3_loadext_entry)( | |||
sqlite3 *db, /* Handle to the database. */ | |||
char **pzErrMsg, /* Used to set error string on failure. */ | |||
const sqlite3_api_routines *pThunk /* Extension API function pointers. */ | |||
); | |||
/* | |||
** The following macros redefine the API routines so that they are | |||
** redirected through the global sqlite3_api structure. | |||
** | |||
** This header file is also used by the loadext.c source file | |||
** (part of the main SQLite library - not an extension) so that | |||
** it can get access to the sqlite3_api_routines structure | |||
** definition. But the main library does not want to redefine | |||
** the API. So the redefinition macros are only valid if the | |||
** SQLITE_CORE macros is undefined. | |||
*/ | |||
#if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) | |||
#define sqlite3_aggregate_context sqlite3_api->aggregate_context | |||
#ifndef SQLITE_OMIT_DEPRECATED | |||
#define sqlite3_aggregate_count sqlite3_api->aggregate_count | |||
#endif | |||
#define sqlite3_bind_blob sqlite3_api->bind_blob | |||
#define sqlite3_bind_double sqlite3_api->bind_double | |||
#define sqlite3_bind_int sqlite3_api->bind_int | |||
#define sqlite3_bind_int64 sqlite3_api->bind_int64 | |||
#define sqlite3_bind_null sqlite3_api->bind_null | |||
#define sqlite3_bind_parameter_count sqlite3_api->bind_parameter_count | |||
#define sqlite3_bind_parameter_index sqlite3_api->bind_parameter_index | |||
#define sqlite3_bind_parameter_name sqlite3_api->bind_parameter_name | |||
#define sqlite3_bind_text sqlite3_api->bind_text | |||
#define sqlite3_bind_text16 sqlite3_api->bind_text16 | |||
#define sqlite3_bind_value sqlite3_api->bind_value | |||
#define sqlite3_busy_handler sqlite3_api->busy_handler | |||
#define sqlite3_busy_timeout sqlite3_api->busy_timeout | |||
#define sqlite3_changes sqlite3_api->changes | |||
#define sqlite3_close sqlite3_api->close | |||
#define sqlite3_collation_needed sqlite3_api->collation_needed | |||
#define sqlite3_collation_needed16 sqlite3_api->collation_needed16 | |||
#define sqlite3_column_blob sqlite3_api->column_blob | |||
#define sqlite3_column_bytes sqlite3_api->column_bytes | |||
#define sqlite3_column_bytes16 sqlite3_api->column_bytes16 | |||
#define sqlite3_column_count sqlite3_api->column_count | |||
#define sqlite3_column_database_name sqlite3_api->column_database_name | |||
#define sqlite3_column_database_name16 sqlite3_api->column_database_name16 | |||
#define sqlite3_column_decltype sqlite3_api->column_decltype | |||
#define sqlite3_column_decltype16 sqlite3_api->column_decltype16 | |||
#define sqlite3_column_double sqlite3_api->column_double | |||
#define sqlite3_column_int sqlite3_api->column_int | |||
#define sqlite3_column_int64 sqlite3_api->column_int64 | |||
#define sqlite3_column_name sqlite3_api->column_name | |||
#define sqlite3_column_name16 sqlite3_api->column_name16 | |||
#define sqlite3_column_origin_name sqlite3_api->column_origin_name | |||
#define sqlite3_column_origin_name16 sqlite3_api->column_origin_name16 | |||
#define sqlite3_column_table_name sqlite3_api->column_table_name | |||
#define sqlite3_column_table_name16 sqlite3_api->column_table_name16 | |||
#define sqlite3_column_text sqlite3_api->column_text | |||
#define sqlite3_column_text16 sqlite3_api->column_text16 | |||
#define sqlite3_column_type sqlite3_api->column_type | |||
#define sqlite3_column_value sqlite3_api->column_value | |||
#define sqlite3_commit_hook sqlite3_api->commit_hook | |||
#define sqlite3_complete sqlite3_api->complete | |||
#define sqlite3_complete16 sqlite3_api->complete16 | |||
#define sqlite3_create_collation sqlite3_api->create_collation | |||
#define sqlite3_create_collation16 sqlite3_api->create_collation16 | |||
#define sqlite3_create_function sqlite3_api->create_function | |||
#define sqlite3_create_function16 sqlite3_api->create_function16 | |||
#define sqlite3_create_module sqlite3_api->create_module | |||
#define sqlite3_create_module_v2 sqlite3_api->create_module_v2 | |||
#define sqlite3_data_count sqlite3_api->data_count | |||
#define sqlite3_db_handle sqlite3_api->db_handle | |||
#define sqlite3_declare_vtab sqlite3_api->declare_vtab | |||
#define sqlite3_enable_shared_cache sqlite3_api->enable_shared_cache | |||
#define sqlite3_errcode sqlite3_api->errcode | |||
#define sqlite3_errmsg sqlite3_api->errmsg | |||
#define sqlite3_errmsg16 sqlite3_api->errmsg16 | |||
#define sqlite3_exec sqlite3_api->exec | |||
#ifndef SQLITE_OMIT_DEPRECATED | |||
#define sqlite3_expired sqlite3_api->expired | |||
#endif | |||
#define sqlite3_finalize sqlite3_api->finalize | |||
#define sqlite3_free sqlite3_api->free | |||
#define sqlite3_free_table sqlite3_api->free_table | |||
#define sqlite3_get_autocommit sqlite3_api->get_autocommit | |||
#define sqlite3_get_auxdata sqlite3_api->get_auxdata | |||
#define sqlite3_get_table sqlite3_api->get_table | |||
#ifndef SQLITE_OMIT_DEPRECATED | |||
#define sqlite3_global_recover sqlite3_api->global_recover | |||
#endif | |||
#define sqlite3_interrupt sqlite3_api->interruptx | |||
#define sqlite3_last_insert_rowid sqlite3_api->last_insert_rowid | |||
#define sqlite3_libversion sqlite3_api->libversion | |||
#define sqlite3_libversion_number sqlite3_api->libversion_number | |||
#define sqlite3_malloc sqlite3_api->malloc | |||
#define sqlite3_mprintf sqlite3_api->mprintf | |||
#define sqlite3_open sqlite3_api->open | |||
#define sqlite3_open16 sqlite3_api->open16 | |||
#define sqlite3_prepare sqlite3_api->prepare | |||
#define sqlite3_prepare16 sqlite3_api->prepare16 | |||
#define sqlite3_prepare_v2 sqlite3_api->prepare_v2 | |||
#define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2 | |||
#define sqlite3_profile sqlite3_api->profile | |||
#define sqlite3_progress_handler sqlite3_api->progress_handler | |||
#define sqlite3_realloc sqlite3_api->realloc | |||
#define sqlite3_reset sqlite3_api->reset | |||
#define sqlite3_result_blob sqlite3_api->result_blob | |||
#define sqlite3_result_double sqlite3_api->result_double | |||
#define sqlite3_result_error sqlite3_api->result_error | |||
#define sqlite3_result_error16 sqlite3_api->result_error16 | |||
#define sqlite3_result_int sqlite3_api->result_int | |||
#define sqlite3_result_int64 sqlite3_api->result_int64 | |||
#define sqlite3_result_null sqlite3_api->result_null | |||
#define sqlite3_result_text sqlite3_api->result_text | |||
#define sqlite3_result_text16 sqlite3_api->result_text16 | |||
#define sqlite3_result_text16be sqlite3_api->result_text16be | |||
#define sqlite3_result_text16le sqlite3_api->result_text16le | |||
#define sqlite3_result_value sqlite3_api->result_value | |||
#define sqlite3_rollback_hook sqlite3_api->rollback_hook | |||
#define sqlite3_set_authorizer sqlite3_api->set_authorizer | |||
#define sqlite3_set_auxdata sqlite3_api->set_auxdata | |||
#define sqlite3_snprintf sqlite3_api->xsnprintf | |||
#define sqlite3_step sqlite3_api->step | |||
#define sqlite3_table_column_metadata sqlite3_api->table_column_metadata | |||
#define sqlite3_thread_cleanup sqlite3_api->thread_cleanup | |||
#define sqlite3_total_changes sqlite3_api->total_changes | |||
#define sqlite3_trace sqlite3_api->trace | |||
#ifndef SQLITE_OMIT_DEPRECATED | |||
#define sqlite3_transfer_bindings sqlite3_api->transfer_bindings | |||
#endif | |||
#define sqlite3_update_hook sqlite3_api->update_hook | |||
#define sqlite3_user_data sqlite3_api->user_data | |||
#define sqlite3_value_blob sqlite3_api->value_blob | |||
#define sqlite3_value_bytes sqlite3_api->value_bytes | |||
#define sqlite3_value_bytes16 sqlite3_api->value_bytes16 | |||
#define sqlite3_value_double sqlite3_api->value_double | |||
#define sqlite3_value_int sqlite3_api->value_int | |||
#define sqlite3_value_int64 sqlite3_api->value_int64 | |||
#define sqlite3_value_numeric_type sqlite3_api->value_numeric_type | |||
#define sqlite3_value_text sqlite3_api->value_text | |||
#define sqlite3_value_text16 sqlite3_api->value_text16 | |||
#define sqlite3_value_text16be sqlite3_api->value_text16be | |||
#define sqlite3_value_text16le sqlite3_api->value_text16le | |||
#define sqlite3_value_type sqlite3_api->value_type | |||
#define sqlite3_vmprintf sqlite3_api->vmprintf | |||
#define sqlite3_vsnprintf sqlite3_api->xvsnprintf | |||
#define sqlite3_overload_function sqlite3_api->overload_function | |||
#define sqlite3_prepare_v2 sqlite3_api->prepare_v2 | |||
#define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2 | |||
#define sqlite3_clear_bindings sqlite3_api->clear_bindings | |||
#define sqlite3_bind_zeroblob sqlite3_api->bind_zeroblob | |||
#define sqlite3_blob_bytes sqlite3_api->blob_bytes | |||
#define sqlite3_blob_close sqlite3_api->blob_close | |||
#define sqlite3_blob_open sqlite3_api->blob_open | |||
#define sqlite3_blob_read sqlite3_api->blob_read | |||
#define sqlite3_blob_write sqlite3_api->blob_write | |||
#define sqlite3_create_collation_v2 sqlite3_api->create_collation_v2 | |||
#define sqlite3_file_control sqlite3_api->file_control | |||
#define sqlite3_memory_highwater sqlite3_api->memory_highwater | |||
#define sqlite3_memory_used sqlite3_api->memory_used | |||
#define sqlite3_mutex_alloc sqlite3_api->mutex_alloc | |||
#define sqlite3_mutex_enter sqlite3_api->mutex_enter | |||
#define sqlite3_mutex_free sqlite3_api->mutex_free | |||
#define sqlite3_mutex_leave sqlite3_api->mutex_leave | |||
#define sqlite3_mutex_try sqlite3_api->mutex_try | |||
#define sqlite3_open_v2 sqlite3_api->open_v2 | |||
#define sqlite3_release_memory sqlite3_api->release_memory | |||
#define sqlite3_result_error_nomem sqlite3_api->result_error_nomem | |||
#define sqlite3_result_error_toobig sqlite3_api->result_error_toobig | |||
#define sqlite3_sleep sqlite3_api->sleep | |||
#define sqlite3_soft_heap_limit sqlite3_api->soft_heap_limit | |||
#define sqlite3_vfs_find sqlite3_api->vfs_find | |||
#define sqlite3_vfs_register sqlite3_api->vfs_register | |||
#define sqlite3_vfs_unregister sqlite3_api->vfs_unregister | |||
#define sqlite3_threadsafe sqlite3_api->xthreadsafe | |||
#define sqlite3_result_zeroblob sqlite3_api->result_zeroblob | |||
#define sqlite3_result_error_code sqlite3_api->result_error_code | |||
#define sqlite3_test_control sqlite3_api->test_control | |||
#define sqlite3_randomness sqlite3_api->randomness | |||
#define sqlite3_context_db_handle sqlite3_api->context_db_handle | |||
#define sqlite3_extended_result_codes sqlite3_api->extended_result_codes | |||
#define sqlite3_limit sqlite3_api->limit | |||
#define sqlite3_next_stmt sqlite3_api->next_stmt | |||
#define sqlite3_sql sqlite3_api->sql | |||
#define sqlite3_status sqlite3_api->status | |||
#define sqlite3_backup_finish sqlite3_api->backup_finish | |||
#define sqlite3_backup_init sqlite3_api->backup_init | |||
#define sqlite3_backup_pagecount sqlite3_api->backup_pagecount | |||
#define sqlite3_backup_remaining sqlite3_api->backup_remaining | |||
#define sqlite3_backup_step sqlite3_api->backup_step | |||
#define sqlite3_compileoption_get sqlite3_api->compileoption_get | |||
#define sqlite3_compileoption_used sqlite3_api->compileoption_used | |||
#define sqlite3_create_function_v2 sqlite3_api->create_function_v2 | |||
#define sqlite3_db_config sqlite3_api->db_config | |||
#define sqlite3_db_mutex sqlite3_api->db_mutex | |||
#define sqlite3_db_status sqlite3_api->db_status | |||
#define sqlite3_extended_errcode sqlite3_api->extended_errcode | |||
#define sqlite3_log sqlite3_api->log | |||
#define sqlite3_soft_heap_limit64 sqlite3_api->soft_heap_limit64 | |||
#define sqlite3_sourceid sqlite3_api->sourceid | |||
#define sqlite3_stmt_status sqlite3_api->stmt_status | |||
#define sqlite3_strnicmp sqlite3_api->strnicmp | |||
#define sqlite3_unlock_notify sqlite3_api->unlock_notify | |||
#define sqlite3_wal_autocheckpoint sqlite3_api->wal_autocheckpoint | |||
#define sqlite3_wal_checkpoint sqlite3_api->wal_checkpoint | |||
#define sqlite3_wal_hook sqlite3_api->wal_hook | |||
#define sqlite3_blob_reopen sqlite3_api->blob_reopen | |||
#define sqlite3_vtab_config sqlite3_api->vtab_config | |||
#define sqlite3_vtab_on_conflict sqlite3_api->vtab_on_conflict | |||
/* Version 3.7.16 and later */ | |||
#define sqlite3_close_v2 sqlite3_api->close_v2 | |||
#define sqlite3_db_filename sqlite3_api->db_filename | |||
#define sqlite3_db_readonly sqlite3_api->db_readonly | |||
#define sqlite3_db_release_memory sqlite3_api->db_release_memory | |||
#define sqlite3_errstr sqlite3_api->errstr | |||
#define sqlite3_stmt_busy sqlite3_api->stmt_busy | |||
#define sqlite3_stmt_readonly sqlite3_api->stmt_readonly | |||
#define sqlite3_stricmp sqlite3_api->stricmp | |||
#define sqlite3_uri_boolean sqlite3_api->uri_boolean | |||
#define sqlite3_uri_int64 sqlite3_api->uri_int64 | |||
#define sqlite3_uri_parameter sqlite3_api->uri_parameter | |||
#define sqlite3_uri_vsnprintf sqlite3_api->xvsnprintf | |||
#define sqlite3_wal_checkpoint_v2 sqlite3_api->wal_checkpoint_v2 | |||
/* Version 3.8.7 and later */ | |||
#define sqlite3_auto_extension sqlite3_api->auto_extension | |||
#define sqlite3_bind_blob64 sqlite3_api->bind_blob64 | |||
#define sqlite3_bind_text64 sqlite3_api->bind_text64 | |||
#define sqlite3_cancel_auto_extension sqlite3_api->cancel_auto_extension | |||
#define sqlite3_load_extension sqlite3_api->load_extension | |||
#define sqlite3_malloc64 sqlite3_api->malloc64 | |||
#define sqlite3_msize sqlite3_api->msize | |||
#define sqlite3_realloc64 sqlite3_api->realloc64 | |||
#define sqlite3_reset_auto_extension sqlite3_api->reset_auto_extension | |||
#define sqlite3_result_blob64 sqlite3_api->result_blob64 | |||
#define sqlite3_result_text64 sqlite3_api->result_text64 | |||
#define sqlite3_strglob sqlite3_api->strglob | |||
/* Version 3.8.11 and later */ | |||
#define sqlite3_value_dup sqlite3_api->value_dup | |||
#define sqlite3_value_free sqlite3_api->value_free | |||
#define sqlite3_result_zeroblob64 sqlite3_api->result_zeroblob64 | |||
#define sqlite3_bind_zeroblob64 sqlite3_api->bind_zeroblob64 | |||
/* Version 3.9.0 and later */ | |||
#define sqlite3_value_subtype sqlite3_api->value_subtype | |||
#define sqlite3_result_subtype sqlite3_api->result_subtype | |||
/* Version 3.10.0 and later */ | |||
#define sqlite3_status64 sqlite3_api->status64 | |||
#define sqlite3_strlike sqlite3_api->strlike | |||
#define sqlite3_db_cacheflush sqlite3_api->db_cacheflush | |||
/* Version 3.12.0 and later */ | |||
#define sqlite3_system_errno sqlite3_api->system_errno | |||
/* Version 3.14.0 and later */ | |||
#define sqlite3_trace_v2 sqlite3_api->trace_v2 | |||
#define sqlite3_expanded_sql sqlite3_api->expanded_sql | |||
/* Version 3.18.0 and later */ | |||
#define sqlite3_set_last_insert_rowid sqlite3_api->set_last_insert_rowid | |||
/* Version 3.20.0 and later */ | |||
#define sqlite3_prepare_v3 sqlite3_api->prepare_v3 | |||
#define sqlite3_prepare16_v3 sqlite3_api->prepare16_v3 | |||
#define sqlite3_bind_pointer sqlite3_api->bind_pointer | |||
#define sqlite3_result_pointer sqlite3_api->result_pointer | |||
#define sqlite3_value_pointer sqlite3_api->value_pointer | |||
/* Version 3.22.0 and later */ | |||
#define sqlite3_vtab_nochange sqlite3_api->vtab_nochange | |||
#define sqlite3_value_nochange sqlite3_api->value_nochange | |||
#define sqlite3_vtab_collation sqlite3_api->vtab_collation | |||
/* Version 3.24.0 and later */ | |||
#define sqlite3_keyword_count sqlite3_api->keyword_count | |||
#define sqlite3_keyword_name sqlite3_api->keyword_name | |||
#define sqlite3_keyword_check sqlite3_api->keyword_check | |||
#define sqlite3_str_new sqlite3_api->str_new | |||
#define sqlite3_str_finish sqlite3_api->str_finish | |||
#define sqlite3_str_appendf sqlite3_api->str_appendf | |||
#define sqlite3_str_vappendf sqlite3_api->str_vappendf | |||
#define sqlite3_str_append sqlite3_api->str_append | |||
#define sqlite3_str_appendall sqlite3_api->str_appendall | |||
#define sqlite3_str_appendchar sqlite3_api->str_appendchar | |||
#define sqlite3_str_reset sqlite3_api->str_reset | |||
#define sqlite3_str_errcode sqlite3_api->str_errcode | |||
#define sqlite3_str_length sqlite3_api->str_length | |||
#define sqlite3_str_value sqlite3_api->str_value | |||
/* Version 3.25.0 and later */ | |||
#define sqlite3_create_window_function sqlite3_api->create_window_function | |||
/* Version 3.26.0 and later */ | |||
#define sqlite3_normalized_sql sqlite3_api->normalized_sql | |||
/* Version 3.28.0 and later */ | |||
#define sqlite3_stmt_isexplain sqlite3_api->stmt_isexplain | |||
#define sqlite3_value_frombind sqlite3_api->value_frombind | |||
/* Version 3.30.0 and later */ | |||
#define sqlite3_drop_modules sqlite3_api->drop_modules | |||
/* Version 3.31.0 and later */ | |||
#define sqlite3_hard_heap_limit64 sqlite3_api->hard_heap_limit64 | |||
#define sqlite3_uri_key sqlite3_api->uri_key | |||
#define sqlite3_filename_database sqlite3_api->filename_database | |||
#define sqlite3_filename_journal sqlite3_api->filename_journal | |||
#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 | |||
#endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */ | |||
#if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) | |||
/* This case when the file really is being compiled as a loadable | |||
** extension */ | |||
# define SQLITE_EXTENSION_INIT1 const sqlite3_api_routines *sqlite3_api=0; | |||
# define SQLITE_EXTENSION_INIT2(v) sqlite3_api=v; | |||
# define SQLITE_EXTENSION_INIT3 \ | |||
extern const sqlite3_api_routines *sqlite3_api; | |||
#else | |||
/* This case when the file is being statically linked into the | |||
** application */ | |||
# define SQLITE_EXTENSION_INIT1 /*no-op*/ | |||
# define SQLITE_EXTENSION_INIT2(v) (void)v; /* unused parameter */ | |||
# define SQLITE_EXTENSION_INIT3 /*no-op*/ | |||
#endif | |||
#endif /* SQLITE3EXT_H */ | |||
#else // USE_LIBSQLITE3 | |||
// If users really want to link against the system sqlite3 we | |||
// need to make this file a noop. | |||
#endif |
@ -0,0 +1,37 @@ | |||
// 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 !cgo | |||
package sqlite3 | |||
import ( | |||
"database/sql" | |||
"database/sql/driver" | |||
"errors" | |||
) | |||
var errorMsg = errors.New("Binary was compiled with 'CGO_ENABLED=0', go-sqlite3 requires cgo to work. This is a stub") | |||
func init() { | |||
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)) {} |
@ -0,0 +1,5 @@ | |||
module github.com/mattn/go-sqlite3/upgrade | |||
go 1.16 | |||
require github.com/PuerkitoBio/goquery v1.7.1 // indirect |
@ -0,0 +1,12 @@ | |||
github.com/PuerkitoBio/goquery v1.7.1 h1:oE+T06D+1T7LNrn91B4aERsRIeCLJ/oPSa6xB9FPnz4= | |||
github.com/PuerkitoBio/goquery v1.7.1/go.mod h1:XY0pP4kfraEmmV1O7Uf6XyjoslwsneBbgeDjLYuN8xY= | |||
github.com/andybalholm/cascadia v1.2.0 h1:vuRCkM5Ozh/BfmsaTm26kbjm0mIOM3yS5Ek/F5h18aE= | |||
github.com/andybalholm/cascadia v1.2.0/go.mod h1:YCyR8vOZT9aZ1CHEd8ap0gMVm2aFgxBp0T0eFw1RUQY= | |||
golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= | |||
golang.org/x/net v0.0.0-20210614182718-04defd469f4e h1:XpT3nA5TvE525Ne3hInMh6+GETgn27Zfm9dxsThnX2Q= | |||
golang.org/x/net v0.0.0-20210614182718-04defd469f4e/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= | |||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | |||
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | |||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= | |||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= | |||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= |
@ -0,0 +1,5 @@ | |||
// Package upgrade is a dummy package to ensure package can be loaded | |||
// | |||
// This file is to avoid the following error: | |||
// can't load package: package go-sqlite3/upgrade: build constraints exclude all Go files in go-sqlite3\upgrade | |||
package upgrade |
@ -0,0 +1,231 @@ | |||
//go:build !cgo && upgrade && ignore | |||
// +build !cgo,upgrade,ignore | |||
package main | |||
import ( | |||
"archive/zip" | |||
"bufio" | |||
"bytes" | |||
"fmt" | |||
"io" | |||
"io/ioutil" | |||
"log" | |||
"net/http" | |||
"os" | |||
"path" | |||
"path/filepath" | |||
"strings" | |||
"time" | |||
"github.com/PuerkitoBio/goquery" | |||
) | |||
func download(prefix string) (url string, content []byte, err error) { | |||
year := time.Now().Year() | |||
site := "https://www.sqlite.org/download.html" | |||
//fmt.Printf("scraping %v\n", site) | |||
doc, err := goquery.NewDocument(site) | |||
if err != nil { | |||
log.Fatal(err) | |||
} | |||
doc.Find("a").Each(func(_ int, s *goquery.Selection) { | |||
if strings.HasPrefix(s.Text(), prefix) { | |||
url = fmt.Sprintf("https://www.sqlite.org/%d/", year) + s.Text() | |||
} | |||
}) | |||
if url == "" { | |||
return "", nil, fmt.Errorf("Unable to find prefix '%s' on sqlite.org", prefix) | |||
} | |||
fmt.Printf("Downloading %v\n", url) | |||
resp, err := http.Get(url) | |||
if err != nil { | |||
log.Fatal(err) | |||
} | |||
// Ready Body Content | |||
content, err = ioutil.ReadAll(resp.Body) | |||
defer resp.Body.Close() | |||
if err != nil { | |||
return "", nil, err | |||
} | |||
return url, content, nil | |||
} | |||
func mergeFile(src string, dst string) error { | |||
defer func() error { | |||
fmt.Printf("Removing: %s\n", src) | |||
err := os.Remove(src) | |||
if err != nil { | |||
return err | |||
} | |||
return nil | |||
}() | |||
// Open destination | |||
fdst, err := os.OpenFile(dst, os.O_APPEND|os.O_WRONLY, 0666) | |||
if err != nil { | |||
return err | |||
} | |||
defer fdst.Close() | |||
// Read source content | |||
content, err := ioutil.ReadFile(src) | |||
if err != nil { | |||
return err | |||
} | |||
// Add Additional newline | |||
if _, err := fdst.WriteString("\n"); err != nil { | |||
return err | |||
} | |||
fmt.Printf("Merging: %s into %s\n", src, dst) | |||
if _, err = fdst.Write(content); err != nil { | |||
return err | |||
} | |||
return nil | |||
} | |||
func main() { | |||
fmt.Println("Go-SQLite3 Upgrade Tool") | |||
wd, err := os.Getwd() | |||
if err != nil { | |||
log.Fatal(err) | |||
} | |||
if filepath.Base(wd) != "upgrade" { | |||
log.Printf("Current directory is %q but should run in upgrade directory", wd) | |||
os.Exit(1) | |||
} | |||
// Download Amalgamation | |||
_, amalgamation, err := download("sqlite-amalgamation-") | |||
if err != nil { | |||
log.Fatalf("Failed to download: sqlite-amalgamation; %s", err) | |||
} | |||
// Download Source | |||
_, source, err := download("sqlite-src-") | |||
if err != nil { | |||
log.Fatalf("Failed to download: sqlite-src; %s", err) | |||
} | |||
// Create Amalgamation Zip Reader | |||
rAmalgamation, err := zip.NewReader(bytes.NewReader(amalgamation), int64(len(amalgamation))) | |||
if err != nil { | |||
log.Fatal(err) | |||
} | |||
// Create Source Zip Reader | |||
rSource, err := zip.NewReader(bytes.NewReader(source), int64(len(source))) | |||
if err != nil { | |||
log.Fatal(err) | |||
} | |||
// Extract Amalgamation | |||
for _, zf := range rAmalgamation.File { | |||
var f *os.File | |||
switch path.Base(zf.Name) { | |||
case "sqlite3.c": | |||
f, err = os.Create("../sqlite3-binding.c") | |||
case "sqlite3.h": | |||
f, err = os.Create("../sqlite3-binding.h") | |||
case "sqlite3ext.h": | |||
f, err = os.Create("../sqlite3ext.h") | |||
default: | |||
continue | |||
} | |||
if err != nil { | |||
log.Fatal(err) | |||
} | |||
zr, err := zf.Open() | |||
if err != nil { | |||
log.Fatal(err) | |||
} | |||
_, err = io.WriteString(f, "#ifndef USE_LIBSQLITE3\n") | |||
if err != nil { | |||
zr.Close() | |||
f.Close() | |||
log.Fatal(err) | |||
} | |||
scanner := bufio.NewScanner(zr) | |||
for scanner.Scan() { | |||
text := scanner.Text() | |||
if text == `#include "sqlite3.h"` { | |||
text = `#include "sqlite3-binding.h" | |||
#ifdef __clang__ | |||
#define assert(condition) ((void)0) | |||
#endif | |||
` | |||
} | |||
_, err = fmt.Fprintln(f, text) | |||
if err != nil { | |||
break | |||
} | |||
} | |||
err = scanner.Err() | |||
if err != nil { | |||
zr.Close() | |||
f.Close() | |||
log.Fatal(err) | |||
} | |||
_, err = io.WriteString(f, "#else // USE_LIBSQLITE3\n // If users really want to link against the system sqlite3 we\n// need to make this file a noop.\n #endif") | |||
if err != nil { | |||
zr.Close() | |||
f.Close() | |||
log.Fatal(err) | |||
} | |||
zr.Close() | |||
f.Close() | |||
fmt.Printf("Extracted: %v\n", filepath.Base(f.Name())) | |||
} | |||
//Extract Source | |||
for _, zf := range rSource.File { | |||
var f *os.File | |||
switch path.Base(zf.Name) { | |||
case "userauth.c": | |||
f, err = os.Create("../userauth.c") | |||
case "sqlite3userauth.h": | |||
f, err = os.Create("../userauth.h") | |||
default: | |||
continue | |||
} | |||
if err != nil { | |||
log.Fatal(err) | |||
} | |||
zr, err := zf.Open() | |||
if err != nil { | |||
log.Fatal(err) | |||
} | |||
_, err = io.Copy(f, zr) | |||
if err != nil { | |||
log.Fatal(err) | |||
} | |||
zr.Close() | |||
f.Close() | |||
fmt.Printf("extracted %v\n", filepath.Base(f.Name())) | |||
} | |||
// Merge SQLite User Authentication into amalgamation | |||
if err := mergeFile("../userauth.c", "../sqlite3-binding.c"); err != nil { | |||
log.Fatal(err) | |||
} | |||
if err := mergeFile("../userauth.h", "../sqlite3-binding.h"); err != nil { | |||
log.Fatal(err) | |||
} | |||
os.Exit(0) | |||
} |