Commit Graph

271 Commits

Author SHA1 Message Date
Mario Trangoni 847660225d fix all unconvert issues 2018-04-17 10:55:42 +02:00
Yasuhiro Matsumoto 57ca83473c
add _mutex flag to specify SQLITE_OPEN_NOMUTEX or SQLITE_OPEN_FULLMUTEX 2018-03-16 21:40:16 +09:00
mattn 696e2e43cb
Merge pull request #530 from navytux/y/no-go-if-notneeded
Don't spawn interrupt goroutine if we know that context cannot be canceled
2018-02-18 22:15:52 +09:00
Bas van Beek bd9ec0844c only enable pread/pwrite for linux. fixes #533 and fixes #532 2018-02-17 21:38:35 +01:00
Kirill Smelkov 00a23ba538 Don't spawn interrupt goroutine if we know that context cannot be canceled
For a Go-only project the following code pattern

	go func() {
		select {
		case <-ctx.Done():
			// call some cancel

		case <-done:
			// work finished ok
		}
	}()

	// do some work
	close(done)

works good and fast - without high scheduling overhead because scheduler
usually puts spawned goroutine into run queue on the same OS thread and so
after done is closed control is passed to spawned goroutine without OS context
switch.

However in the presence of Cgo calls in "do some work" the situation can
become different - Cgo calls are treated by go runtime similarly to
system calls with the effect that goroutines spawned on original OS
thread tend to be migrated by scheduler to be executed on another OS
thread.

This in turn can bring high overhead for communicating on "done", which
ultimately can result in full context switch: if the spawned goroutine
had chance to run, already checked done and ctx to be not ready, and went
into sleep via wait on futex - showing as something like below in strace for
one read query (note futex calls):

	27867 00:38:39.782146 stat(".../neo.sqlite-journal", 0x7f83809c4a20) = -1 ENOENT (No such file or directory)
	27867 00:38:39.782165 pread64(3, "\0\0\0\33\0\0\10\235\0\0\10]\0\0\0\27", 16, 24) = 16
	27871 00:38:39.782179 <... pselect6 resumed> ) = 0 (Timeout)
	27868 00:38:39.782187 <... pselect6 resumed> ) = 0 (Timeout)
	27871 00:38:39.782193 futex(0xc4200f8538, FUTEX_WAIT, 0, NULL <unfinished ...>
	27868 00:38:39.782199 futex(0xc420013138, FUTEX_WAIT, 0, NULL <unfinished ...>
	27867 00:38:39.782205 stat(".../neo.sqlite-wal", 0x7f83809c4a20) = -1 ENOENT (No such file or directory)
	27867 00:38:39.782224 fstat(3, {st_mode=S_IFREG|0644, st_size=9031680, ...}) = 0
	27867 00:38:39.782247 futex(0xc420013138, FUTEX_WAKE, 1 <unfinished ...>
	27868 00:38:39.782259 <... futex resumed> ) = 0
	27867 00:38:39.782265 <... futex resumed> ) = 1
	27868 00:38:39.782270 pselect6(0, NULL, NULL, NULL, {tv_sec=0, tv_nsec=3000}, NULL <unfinished ...>
	27867 00:38:39.782279 fcntl(3, F_SETLK, {l_type=F_UNLCK, l_whence=SEEK_SET, l_start=0, l_len=0}) = 0
	27867 00:38:39.782315 fcntl(3, F_SETLK, {l_type=F_RDLCK, l_whence=SEEK_SET, l_start=1073741824, l_len=1}) = 0
	27868 00:38:39.782336 <... pselect6 resumed> ) = 0 (Timeout)
	27867 00:38:39.782342 fcntl(3, F_SETLK, {l_type=F_RDLCK, l_whence=SEEK_SET, l_start=1073741826, l_len=510} <unfinished ...>
	27868 00:38:39.782348 futex(0xc4200f8538, FUTEX_WAKE, 1 <unfinished ...>
	27867 00:38:39.782355 <... fcntl resumed> ) = 0
	27871 00:38:39.782360 <... futex resumed> ) = 0
	27868 00:38:39.782367 <... futex resumed> ) = 1
	27871 00:38:39.782372 futex(0xc4200f8138, FUTEX_WAKE, 1 <unfinished ...>
	27868 00:38:39.782377 pselect6(0, NULL, NULL, NULL, {tv_sec=0, tv_nsec=3000}, NULL <unfinished ...>
	27871 00:38:39.782384 <... futex resumed> ) = 1
	27870 00:38:39.782389 <... futex resumed> ) = 0
	27867 00:38:39.782394 fcntl(3, F_SETLK, {l_type=F_UNLCK, l_whence=SEEK_SET, l_start=1073741824, l_len=1} <unfinished ...>
	27870 00:38:39.782400 pselect6(0, NULL, NULL, NULL, {tv_sec=0, tv_nsec=3000}, NULL <unfinished ...>
	27867 00:38:39.782408 <... fcntl resumed> ) = 0

Below link shows that go scheduler itself might be significantly improved for
cases when there are several Cgo calls made for a request in a server:

https://github.com/golang/go/issues/21827#issuecomment-329092317

in particular CGo-4 case should be closely related to this sqlite3 go package,
because for one query many CGo calls are made to SQLite.

However until there are proper scheduler fixes, let's make what could
be made to improve time to do queries:

If we know that the context under which a query is executed will never
be canceled - we know we can safely skip spawning the interrupt
goroutine and this was avoid ping-pong on done in between different OS
threads.

This brings the following speedup on my notebook with go1.10:

name               old req/s    new req/s    delta
Exec                 254k ± 1%    379k ± 1%  +48.89%  (p=0.000 n=10+10)
Query               90.6k ± 2%   96.4k ± 1%   +6.37%  (p=0.000 n=10+10)
Params              81.5k ± 1%   87.0k ± 1%   +6.83%  (p=0.000 n=10+10)
Stmt                 122k ± 2%    129k ± 1%   +6.07%  (p=0.000 n=10+9)
Rows                2.98k ± 1%   3.06k ± 1%   +2.77%  (p=0.000 n=9+10)
StmtRows            3.10k ± 1%   3.13k ± 1%   +1.12%  (p=0.000 n=9+10)

name               old time/op  new time/op  delta
CustomFunctions-4  10.6µs ± 1%  10.1µs ± 1%   -5.01%  (p=0.000 n=10+10)
2018-02-17 21:09:05 +03:00
Kirill Smelkov f675967c54 Let SQLite use pread/pwrite
With current settings SQLite was using lseek/read syscalls to read data, e.g.:

	20:43:17.640660 fcntl(3, F_SETLK, {l_type=F_UNLCK, l_whence=SEEK_SET, l_start=0, l_len=0}) = 0
	20:43:17.640683 fcntl(3, F_SETLK, {l_type=F_RDLCK, l_whence=SEEK_SET, l_start=1073741824, l_len=1}) = 0
	20:43:17.640705 fcntl(3, F_SETLK, {l_type=F_RDLCK, l_whence=SEEK_SET, l_start=1073741826, l_len=510}) = 0
	20:43:17.640725 fcntl(3, F_SETLK, {l_type=F_UNLCK, l_whence=SEEK_SET, l_start=1073741824, l_len=1}) = 0
	20:43:17.640744 stat(".../neo.sqlite-journal", 0x7ffef2c91080) = -1 ENOENT (No such file or directory)
	20:43:17.640764 lseek(3, 24, SEEK_SET)  = 24
	20:43:17.640779 read(3, "\0\0\0\33\0\0\10\235\0\0\10]\0\0\0\27", 16) = 16
	20:43:17.640795 stat(".../neo.sqlite-wal", 0x7ffef2c91080) = -1 ENOENT (No such file or directory)

but if we allow it to use pread it will be only 1 system call instead of 2 and
reading this way can also be done in parallel because there is no global to
file seeking:

	20:48:42.668466 fcntl(3, F_SETLK, {l_type=F_UNLCK, l_whence=SEEK_SET, l_start=0, l_len=0}) = 0
	20:48:42.668501 fcntl(3, F_SETLK, {l_type=F_RDLCK, l_whence=SEEK_SET, l_start=1073741824, l_len=1}) = 0
	20:48:42.668522 fcntl(3, F_SETLK, {l_type=F_RDLCK, l_whence=SEEK_SET, l_start=1073741826, l_len=510}) = 0
	20:48:42.668542 fcntl(3, F_SETLK, {l_type=F_UNLCK, l_whence=SEEK_SET, l_start=1073741824, l_len=1}) = 0
	20:48:42.668561 stat(".../neo.sqlite-journal", 0x7ffdbc1f22c0) = -1 ENOENT (No such file or directory)
	20:48:42.668580 pread64(3, "\0\0\0\33\0\0\10\235\0\0\10]\0\0\0\27", 16, 24) = 16
	20:48:42.668597 stat(".../neo.sqlite-wal", 0x7ffdbc1f22c0) = -1 ENOENT (No such file or directory)

(if needed this enablement can be done per OS)
2018-02-16 20:44:19 +03:00
mattn 534c0213e2
Merge pull request #525 from mattn/add-usleep
add -DHAVE_USLEEP=1
2018-02-07 18:32:28 +09:00
Yasuhiro Matsumoto f84a0f6cbe add -DHAVE_USLEEP=1
fixes #211
2018-02-07 17:16:12 +09:00
James C Kimble 614d7c1fda
Add static_mock.go to allow building with CGO_ENABLED=0 2018-01-31 22:24:37 -06:00
mattn 6c771bb988
Merge pull request #462 from faruzzy/master
Updated "context" import since it has become a standard library
2018-01-12 17:58:26 +09:00
Niklas Janlert 58004848f1 Fix race in ExecContext
When the context is cancelled, an interrupt should only be made if the
operation is still ongoing.
2017-11-21 13:40:00 +01:00
mattn ed69081a91
Merge pull request #479 from kenshaw/move-registeraggregator
Move RegisterAggregator implementation
2017-11-14 17:56:34 +09:00
Tetsuya Morimoto 9dddfd1328 fix to be able to build with GOTAGS=libsqlite3 2017-11-05 20:01:16 +09:00
Tetsuya Morimoto b07b06e15c update to call _sqlite3_limit as a wrapper instead of sqlite3_limit 2017-11-05 19:45:38 +09:00
Kenneth Shaw 7174000f77 Move RegisterAggregator implementation
The SQLiteConn.RegisterAggregator implementation was defined in
sqlite3_trace.go file, which is guarded with a build constraint. This
change simply moves RegisterAggregator to the main sqlite3.go file,
and moves accompanying unit tests.

The rationale for this move is that it was not possible for downstream
using packages to use RegisterAggregator without also specifying (and
notifying the user) the 'trace' build tag.
2017-11-05 09:18:06 +07:00
Tetsuya Morimoto d785b8f812 support sqlite3_limit to get/set run time limit 2017-11-05 08:19:06 +09:00
Roland Pangu 5df314a2dc Updated "context" import since it has become a standard library after go 1.7 https://golang.org/doc/go1.7#context 2017-09-05 19:54:16 -07:00
mattn 132eeedb4a Merge branch 'master' into master 2017-08-30 19:57:18 +09:00
Yasuhiro Matsumoto 8d81c2f1f8 fix race 2017-08-30 19:37:57 +09:00
Yasuhiro Matsumoto 58ed4a0810 fix race 2017-08-30 19:30:53 +09:00
Yasuhiro Matsumoto 911f1c4fa6 fix lock 2017-08-30 19:27:02 +09:00
Yasuhiro Matsumoto d40d490543 fixes #458 2017-08-28 18:58:02 +09:00
Greg Holt 85e456ef27 Fix to pass TestNilAndEmptyBytes 2017-08-21 13:30:07 -07:00
Yasuhiro Matsumoto 1828334c4a remove mutex 2017-08-02 01:43:14 +09:00
Yasuhiro Matsumoto 569232dc08 fix possibly double Close.
fixes #448
2017-08-02 00:06:18 +09:00
Ross Light 848386d7a2 Add connection option for recursive triggers
Similar to foreign keys, the recursive triggers PRAGMA affects the
interpretation of all statements on a connection.
2017-07-09 07:32:14 -07:00
Jason Abbott 0644414997 Merge pull request #2 from mattn/master
Merge lastest from mattn
2017-07-06 11:52:32 -06:00
Yasuhiro Matsumoto acfa601240 SQLITE_THREADSAFE=1
fixes #274
2017-07-05 17:25:24 +09:00
Jason Abbott 59bd281a89 Incorporate original PR 271 from https://github.com/brokensandals 2017-07-03 12:51:48 -06:00
deepilla 05123859be Don't convert Unix times to nanoseconds when querying datetime fields. Fixes #430. 2017-06-30 13:17:04 -05:00
Evgeniy Makeev ef9f773b24 Fix for cgo panic, issue #428: https://github.com/mattn/go-sqlite3/issues/428 2017-06-20 17:36:44 -07:00
Philip O'Toole cd1cbf523a Sync database-close and statement-close
Potential fix for issue #426.
2017-06-17 12:26:06 -07:00
Xu Xinran 3fa7ed2176 Use global variable for better performance. 2017-06-14 21:22:40 +08:00
Xu Xinran 308f5f1b2f Treat []byte{} as empty bytes instead of NULL. 2017-06-14 19:55:09 +08:00
David Anderson 0430b37250 Add support for collation sequences implemented in Go.
This allows Go programs to register custom comparison functions with
sqlite, and ORDER BY that comparator.
2017-06-08 19:14:07 -07:00
mattn 46e826d22a Merge pull request #407 from zombiezen/foreignkeys
Add _foreign_keys connection parameter
2017-04-02 23:13:39 +09:00
Ross Light c6d43c40e6 Add _foreign_keys connection parameter
Fixes #377
Updates #255
2017-04-01 11:06:59 -07:00
Ross Light 4c2dc8806a Avoid leaking db if setting busy timeout fails 2017-04-01 08:53:17 -07:00
Marko Kungla a1253acb6b
Removed ambitious conn.Close() 2017-03-24 01:06:55 +01:00
Yasuhiro Matsumoto fc03fa9989 close connection when got errors in Open 2017-03-24 08:48:29 +09:00
Yasuhiro Matsumoto 866c3293d9 fix breaking compatibility.
revert cf4bd560f1

close #394
2017-03-21 09:14:48 +09:00
Yasuhiro Matsumoto 0acd8f7aec fix build 2017-03-20 23:34:57 +09:00
Yasuhiro Matsumoto 997cab8094 fix build 2017-03-20 23:31:22 +09:00
Yasuhiro Matsumoto cf4bd560f1 fix build 2017-03-20 23:26:15 +09:00
Yasuhiro Matsumoto 53900fb4f0 return nil when last error is SQLITE_OK 2017-03-20 23:23:24 +09:00
Yasuhiro Matsumoto 32ab0d8730 refactoring 2017-03-05 22:16:51 +09:00
Conor Branagan bba480975b Add Go API for virtual tables
See https://www.sqlite.org/vtab.html for more details.

This work was started from
https://github.com/gwenn/gosqlite/blob/master/vtab.{c,go} and adds:

- Porting the API to go-sqlite3 APIs.
- Support for >= Go 1.6 without requiring the `cgocheck` flag to be changed.
- Filling out the unfinished callback functions for the `Vtable` struct.
- A simple `Context` API layer for ease of use when adding modules.

Tests are included.
2017-03-04 18:10:02 -05:00
Yasuhiro Matsumoto 253bbf431d rename function 2017-03-05 00:45:41 +09:00
Yasuhiro Matsumoto a7b54c9ddb workaround for a compiler
Apple LLVM version 7.0.2 (clang-700.1.81)

Close #386
2017-03-01 16:44:54 +09:00
Yasuhiro Matsumoto 2acfafad58 use variable db 2017-02-16 15:57:29 +09:00
Yasuhiro Matsumoto 588cd3292b fixes race 2017-02-11 21:48:33 +09:00
Yasuhiro Matsumoto a554cf198f close statement 2017-01-07 22:22:02 +09:00
Yasuhiro Matsumoto 6f2749a3ca fix race condition 2017-01-03 22:25:02 +09:00
Yasuhiro Matsumoto 056f235aa6 fix named args 2016-12-09 13:12:14 +09:00
Yasuhiro Matsumoto f043f8092b use prepare instead of Prepare 2016-11-09 01:13:34 +09:00
Yasuhiro Matsumoto d1009b4fa6 fix build 2016-11-08 17:12:03 +09:00
Yasuhiro Matsumoto bfeda3f4f6 fix build 2016-11-08 17:07:52 +09:00
Yasuhiro Matsumoto ea2afbe9e8 revert Multiple Result Set 2016-11-08 13:22:46 +09:00
Yasuhiro Matsumoto dc448a0cb6 fix trace callback. 2016-11-08 12:27:31 +09:00
Yasuhiro Matsumoto dd2c82226b fix trace callback.
Close #352
2016-11-08 12:19:51 +09:00
andrefsp 6c0a6bb9cd Fix data race introduced on 605d9d08
This commit will fix a data race introduced on 605d9d08 and also
make sure travis CI will now check for data races.
2016-11-07 11:57:52 +00:00
Yasuhiro Matsumoto deed33aec7 cancel 2016-11-06 20:46:27 +09:00
Yasuhiro Matsumoto 605d9d0851 cancel 2016-11-06 20:43:53 +09:00
Yasuhiro Matsumoto 0d1d1a644e go vet && golint 2016-11-05 00:40:06 +09:00
Yasuhiro Matsumoto 755d5be32c add BeginContext 2016-11-04 15:15:16 +09:00
Yasuhiro Matsumoto 025b917610 add PrepareContext 2016-11-04 15:11:24 +09:00
Yasuhiro Matsumoto b23526fb3c support named params 2016-11-04 15:00:29 +09:00
Yasuhiro Matsumoto c95a77965c context features 2016-11-04 14:24:22 +09:00
Yasuhiro Matsumoto 86681de00a remove -Wno-c99-extension
Close #343
2016-10-28 23:22:18 +09:00
Judson 61c9552d18
Added CFLAG config to ignore deprecations 2016-10-04 16:51:54 +01:00
Yasuhiro Matsumoto bf2cfc371a fix build 2016-09-08 00:59:25 +09:00
Yasuhiro Matsumoto 467e3720d8 fix build 2016-09-08 00:39:47 +09:00
A.N 0a2fcd29b6 Provide access to sqlite3_trace_v2(). 2016-09-07 23:48:06 +09:00
mattn e118d44513 Merge pull request #300 from sqweek/issue184
Fix inconsistent tx state with database/sql.
2016-07-15 22:38:49 +09:00
Zbigniew Mandziejewicz 3ebb821e65 Set SQLITE_DETERMINISTIC if undefined 2016-04-23 00:00:49 +08:00
sqweek 727ad200a2 Fix inconsistent tx state with database/sql.
The semantics of sql.Tx.Commit impose that the transaction is
finished and cleaned up by the time the driver's Commit function
returns. However sqlite3 leaves the transaction open if COMMIT
fails due to an SQLITE_BUSY error, so *we* must clean it up.

Closes #184.
2016-04-18 21:47:24 +08:00
David Bariod 3a5555302e Move sqlite3 amalgation files a directory up.
The purpose is to ease the use of vendoring files like godep.
The C sqlite3 files have been added a go compilation conditional flag
Fix #293
2016-04-02 12:48:26 +02:00
Zac Medico f544db98cc Add SQLiteRows.DeclTypes() method 2016-03-06 12:27:17 -08:00
Philip O Toole 1e280555b7 Fix minor typos in comments 2016-02-23 01:26:13 -05:00
mattn c5aee96497 Merge pull request #267 from ianlancetaylor/go16
bind: pass &v[0] in direct call to C
2016-02-01 14:34:37 +09:00
Ian Lance Taylor 8c66b9cf5e callback: use handles rather than passing Go pointers
The cgo pointer passing rules forbid passing a Go pointer to C if that
pointer points to memory containing other Go pointers.  This is true
even if the Go pointer is converted to uintptr.

This change fixes the code to use a handle instead, and to look up the
handle in the callback function.
2016-01-29 13:18:39 -08:00
Ian Lance Taylor b76c61051f bind: pass &v[0] in direct call to C
In Go 1.6, the cgo checking rules are more precise when they see an
address operation as an argument to the C function.  When you pass &v[0]
to a C function, the cgo check just verifies that v itself does not
contain any pointers.  When you write `p := &v[0]` and then pass p to
the C function, the cgo check is conservative: it verifies that the
entire memory block to which p points does not contain any pointers.
When the bind function is called by code that passes a slice that is
part of a larger struct, this means that the cgo check will look at the
entire larger struct, not just the slice.  This can cause a surprising
run time failure.

Avoid this problem by rewriting the code slightly to pass &v[0] in the
call to the C function itself.

In particular this fixes the tests of github.com/jmoiron/sqlx when using
Go 1.6.
2016-01-29 12:39:47 -08:00
Yasuhiro Matsumoto e969434e25 avoid cgoCheckPointer. ref https://github.com/golang/go/issues/12416 2015-12-30 23:29:15 +09:00
Yasuhiro Matsumoto a755cdc361 Merge branch 'master' of https://github.com/mattn/go-sqlite3 2015-11-02 11:56:57 +09:00
Yasuhiro Matsumoto 167173a31f Merge branch 'pr/207' 2015-11-02 11:53:42 +09:00
Augusto Roman 7b0d180ce9 Store/retrieve timezones for time.Time values.
Previously, the timezone information for a provided value was discarded
and the value always stored as in UTC.  However, sqlite allows specifying
the timezone offsets and handles those values appropriately.  This change
stores the timezone information and parses it out if present, otherwise
it defaults to UTC as before.

One additional bugfix:  Previously, a unix timestamp in seconds was
parsed in the local timezone (rather than UTC), in contrast to a unix
timestamp in milliseconds that was parsed in UTC.

While fixing that extra bug, I cleaned up the parsing code -- no need to
convert to a string and then parse it back again and risk a parse error,
just to check the number of digits.

The tests were extended to cover non-UTC timezones storage & retrieval,
meaningful unix timestamps, and correct handling of a trailing Z.
2015-10-09 22:59:25 -07:00
looi b808f01f66 Add FTS4 unicode61 tokenizer support 2015-09-21 19:53:44 +09:00
mattn 0bb7f1c676 Merge pull request #229 from danderson/master
Implement support for calling Go functions from SQLite
2015-09-16 10:46:17 +09:00
David Anderson 26917df7a6 Implement support for aggregation functions implemented in Go. 2015-09-15 18:05:49 -07:00
Jessica Frazelle e37121d4ea introduce ability to pass sqlite_omit_load_extension
sqlite_omit_load_extension is a go build tag which behaves much like its
C counterpart SQLITE_OMIT_LOAD_EXTENSION

Signed-off-by: Jessica Frazelle <acidburn@docker.com>
2015-09-04 14:46:16 -07:00
David Anderson b037a61690 Add support for interface{} arguments in Go SQLite functions.
This enabled support for functions like Foo(a interface{}) and
Bar(a ...interface{}).
2015-08-21 17:12:18 -07:00
David Anderson 566f63a43a Implement support for variadic functions.
Currently, the variadic part must all be the same type, because there's
no "generic" arg converter.
2015-08-21 16:38:23 -07:00
David Anderson 122ddb16de Move argument converters to callback.go, and optimize return value handling.
A call now doesn't have to do any reflection, it just blindly invokes
a bunch of argument and return value handlers to execute the translation,
and the safety of the translation is determined at registration time.
2015-08-21 16:37:45 -07:00
David Anderson cf8fa0af80 Implement support for passing Go functions as custom functions to SQLite.
Fixes #226.
2015-08-21 13:39:50 -07:00
kiwih 6b4ee3cb4f Add fix for go-sqlite3 truncating 64-bit integers when compiled by 32-bit mingw-gcc on windows by converting 'long' variable types to 'long long' in sqlite3.go 2015-08-07 15:13:52 +12:00
Lars Buitinck 90505effc7 Fix doc comment 2015-06-05 16:38:51 +02:00
Yasuhiro Matsumoto dee1a37fe1 Z suffix should be no-op 2015-04-15 16:26:27 +09:00
mattn 5f64400729 Merge pull request #194 from hallyn/tximm.2
Add a txlock option when opening databases
2015-04-14 09:12:54 +09:00
Serge Hallyn f91a09fb50 Add a txlock option when opening databases (v2)
When specified, changes the default locking at a tx.Begin.

Changelog (v2):
	Add a testcase to ensure _txlock is properly handled.

Closes #189

Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com>
2015-04-13 11:48:01 -05:00
Egon Elbre ac0129617f Fix NULs in text.
NUL character is a valid symbols in UTF8.

Fixes #195
2015-04-12 15:02:50 +03:00