diff options
author | mattn <mattn.jp@gmail.com> | 2017-09-01 17:40:05 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-09-01 17:40:05 +0900 |
commit | 05548ff55570cdb9ac72ff4a25a3b5e77a6fb7e5 (patch) | |
tree | 45438e41d445e4496b5c80066b74ba760ac07fb1 /sqlite3.go | |
parent | Merge pull request #461 from mattn/solaris (diff) | |
parent | Merge branch 'master' into master (diff) | |
download | golite-05548ff55570cdb9ac72ff4a25a3b5e77a6fb7e5.tar.gz golite-05548ff55570cdb9ac72ff4a25a3b5e77a6fb7e5.tar.xz |
Merge pull request #423 from danderson/master
Add support for collation sequences implemented in Go.
Diffstat (limited to 'sqlite3.go')
-rw-r--r-- | sqlite3.go | 25 |
1 files changed, 25 insertions, 0 deletions
@@ -100,6 +100,8 @@ int _sqlite3_create_function( } void callbackTrampoline(sqlite3_context*, int, sqlite3_value**); + +int compareTrampoline(void*, int, char*, int, char*); int commitHookTrampoline(void*); void rollbackHookTrampoline(void*); void updateHookTrampoline(void*, int, char*, char*, sqlite3_int64); @@ -326,6 +328,29 @@ func (tx *SQLiteTx) Rollback() error { return err } +// RegisterCollation makes a Go function available as a collation. +// +// cmp receives two UTF-8 strings, a and b. The result should be 0 if +// a==b, -1 if a < b, and +1 if a > b. +// +// cmp must always return the same result given the same +// inputs. Additionally, it must have the following properties for all +// strings A, B and C: if A==B then B==A; if A==B and B==C then A==C; +// if A<B then B>A; if A<B and B<C then A<C. +// +// If cmp does not obey these constraints, sqlite3's behavior is +// undefined when the collation is used. +func (c *SQLiteConn) RegisterCollation(name string, cmp func(string, string) int) error { + handle := newHandle(c, cmp) + cname := C.CString(name) + defer C.free(unsafe.Pointer(cname)) + rv := C.sqlite3_create_collation(c.db, cname, C.SQLITE_UTF8, unsafe.Pointer(handle), (*[0]byte)(unsafe.Pointer(C.compareTrampoline))) + if rv != C.SQLITE_OK { + return c.lastError() + } + return nil +} + // RegisterCommitHook sets the commit hook for a connection. // // If the callback returns non-zero the transaction will become a rollback. |