Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gosqlite
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
gosqlite
Commits
c22f0a7c
Commit
c22f0a7c
authored
Aug 17, 2011
by
gwenn
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fails to add support to sqlite3_set_authorizer.
parent
89b245d6
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
95 additions
and
2 deletions
+95
-2
Makefile
Makefile
+2
-1
backup.go
backup.go
+5
-0
meta.go
meta.go
+4
-0
sqlite.go
sqlite.go
+27
-1
sqlite_test.go
sqlite_test.go
+15
-0
trace.go
trace.go
+42
-0
No files found.
Makefile
View file @
c22f0a7c
...
@@ -9,6 +9,7 @@ TARG=github.com/gwenn/sqlite
...
@@ -9,6 +9,7 @@ TARG=github.com/gwenn/sqlite
CGOFILES
=
\
CGOFILES
=
\
sqlite.go
\
sqlite.go
\
backup.go
\
backup.go
\
meta.go
meta.go
\
trace.go
include
$(GOROOT)/src/Make.pkg
include
$(GOROOT)/src/Make.pkg
backup.go
View file @
c22f0a7c
...
@@ -17,6 +17,7 @@ import (
...
@@ -17,6 +17,7 @@ import (
"unsafe"
"unsafe"
)
)
// Calls sqlite3_backup_init
func
NewBackup
(
dst
*
Conn
,
dstTable
string
,
src
*
Conn
,
srcTable
string
)
(
*
Backup
,
os
.
Error
)
{
func
NewBackup
(
dst
*
Conn
,
dstTable
string
,
src
*
Conn
,
srcTable
string
)
(
*
Backup
,
os
.
Error
)
{
dname
:=
C
.
CString
(
dstTable
)
dname
:=
C
.
CString
(
dstTable
)
sname
:=
C
.
CString
(
srcTable
)
sname
:=
C
.
CString
(
srcTable
)
...
@@ -35,6 +36,7 @@ type Backup struct {
...
@@ -35,6 +36,7 @@ type Backup struct {
dst
,
src
*
Conn
dst
,
src
*
Conn
}
}
// Calls sqlite3_backup_step
func
(
b
*
Backup
)
Step
(
npage
int
)
os
.
Error
{
func
(
b
*
Backup
)
Step
(
npage
int
)
os
.
Error
{
rv
:=
C
.
sqlite3_backup_step
(
b
.
sb
,
C
.
int
(
npage
))
rv
:=
C
.
sqlite3_backup_step
(
b
.
sb
,
C
.
int
(
npage
))
if
rv
==
C
.
SQLITE_OK
||
Errno
(
rv
)
==
ErrBusy
||
Errno
(
rv
)
==
ErrLocked
{
if
rv
==
C
.
SQLITE_OK
||
Errno
(
rv
)
==
ErrBusy
||
Errno
(
rv
)
==
ErrLocked
{
...
@@ -48,10 +50,12 @@ type BackupStatus struct {
...
@@ -48,10 +50,12 @@ type BackupStatus struct {
PageCount
int
PageCount
int
}
}
// Calls sqlite3_backup_remaining and sqlite3_backup_pagecount
func
(
b
*
Backup
)
Status
()
BackupStatus
{
func
(
b
*
Backup
)
Status
()
BackupStatus
{
return
BackupStatus
{
int
(
C
.
sqlite3_backup_remaining
(
b
.
sb
)),
int
(
C
.
sqlite3_backup_pagecount
(
b
.
sb
))}
return
BackupStatus
{
int
(
C
.
sqlite3_backup_remaining
(
b
.
sb
)),
int
(
C
.
sqlite3_backup_pagecount
(
b
.
sb
))}
}
}
// Calls sqlite3_backup_step, sqlite3_backup_remaining and sqlite3_backup_pagecount
func
(
b
*
Backup
)
Run
(
npage
int
,
sleepNs
int64
,
c
chan
<-
BackupStatus
)
os
.
Error
{
func
(
b
*
Backup
)
Run
(
npage
int
,
sleepNs
int64
,
c
chan
<-
BackupStatus
)
os
.
Error
{
var
err
os
.
Error
var
err
os
.
Error
for
{
for
{
...
@@ -67,6 +71,7 @@ func (b *Backup) Run(npage int, sleepNs int64, c chan<- BackupStatus) os.Error {
...
@@ -67,6 +71,7 @@ func (b *Backup) Run(npage int, sleepNs int64, c chan<- BackupStatus) os.Error {
return
b
.
dst
.
error
(
C
.
sqlite3_errcode
(
b
.
dst
.
db
))
return
b
.
dst
.
error
(
C
.
sqlite3_errcode
(
b
.
dst
.
db
))
}
}
// Calls sqlite3_backup_finish
func
(
b
*
Backup
)
Close
()
os
.
Error
{
func
(
b
*
Backup
)
Close
()
os
.
Error
{
if
b
.
sb
==
nil
{
if
b
.
sb
==
nil
{
return
os
.
EINVAL
return
os
.
EINVAL
...
...
meta.go
View file @
c22f0a7c
...
@@ -21,6 +21,7 @@ import (
...
@@ -21,6 +21,7 @@ import (
"unsafe"
"unsafe"
)
)
// Selects tables (no view) from 'sqlite_master' and filters system tables out.
func
(
c
*
Conn
)
Tables
()
([]
string
,
os
.
Error
)
{
func
(
c
*
Conn
)
Tables
()
([]
string
,
os
.
Error
)
{
s
,
err
:=
c
.
Prepare
(
"SELECT name FROM sqlite_master WHERE type IN ('table') AND name NOT LIKE 'sqlite_%'"
)
s
,
err
:=
c
.
Prepare
(
"SELECT name FROM sqlite_master WHERE type IN ('table') AND name NOT LIKE 'sqlite_%'"
)
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -48,6 +49,7 @@ type Column struct {
...
@@ -48,6 +49,7 @@ type Column struct {
Pk
bool
Pk
bool
}
}
// Executes pragma 'table_info'
func
(
c
*
Conn
)
Columns
(
table
string
)
([]
Column
,
os
.
Error
)
{
func
(
c
*
Conn
)
Columns
(
table
string
)
([]
Column
,
os
.
Error
)
{
s
,
err
:=
c
.
Prepare
(
Mprintf
(
"pragma table_info(%Q)"
,
table
))
s
,
err
:=
c
.
Prepare
(
Mprintf
(
"pragma table_info(%Q)"
,
table
))
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -75,6 +77,7 @@ type ForeignKey struct {
...
@@ -75,6 +77,7 @@ type ForeignKey struct {
To
[]
string
To
[]
string
}
}
// Executes pragma 'foreign_key_list'
func
(
c
*
Conn
)
ForeignKeys
(
table
string
)
(
map
[
int
]
*
ForeignKey
,
os
.
Error
)
{
func
(
c
*
Conn
)
ForeignKeys
(
table
string
)
(
map
[
int
]
*
ForeignKey
,
os
.
Error
)
{
s
,
err
:=
c
.
Prepare
(
Mprintf
(
"pragma foreign_key_list(%Q)"
,
table
))
s
,
err
:=
c
.
Prepare
(
Mprintf
(
"pragma foreign_key_list(%Q)"
,
table
))
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -104,6 +107,7 @@ func (c *Conn) ForeignKeys(table string) (map[int]*ForeignKey, os.Error) {
...
@@ -104,6 +107,7 @@ func (c *Conn) ForeignKeys(table string) (map[int]*ForeignKey, os.Error) {
return
fks
,
nil
return
fks
,
nil
}
}
// Calls 'sqlite3_mprintf'
func
Mprintf
(
format
string
,
arg
string
)
string
{
func
Mprintf
(
format
string
,
arg
string
)
string
{
cf
:=
C
.
CString
(
format
)
cf
:=
C
.
CString
(
format
)
defer
C
.
free
(
unsafe
.
Pointer
(
cf
))
defer
C
.
free
(
unsafe
.
Pointer
(
cf
))
...
...
sqlite.go
View file @
c22f0a7c
...
@@ -124,15 +124,18 @@ func (c *Conn) error(rv C.int) os.Error {
...
@@ -124,15 +124,18 @@ func (c *Conn) error(rv C.int) os.Error {
return
os
.
NewError
(
Errno
(
rv
)
.
String
()
+
": "
+
C
.
GoString
(
C
.
sqlite3_errmsg
(
c
.
db
)))
return
os
.
NewError
(
Errno
(
rv
)
.
String
()
+
": "
+
C
.
GoString
(
C
.
sqlite3_errmsg
(
c
.
db
)))
}
}
// Connection
type
Conn
struct
{
type
Conn
struct
{
db
*
C
.
sqlite3
db
*
C
.
sqlite3
}
}
// Calls sqlite3_libversion
func
Version
()
string
{
func
Version
()
string
{
p
:=
C
.
sqlite3_libversion
()
p
:=
C
.
sqlite3_libversion
()
return
C
.
GoString
(
p
)
return
C
.
GoString
(
p
)
}
}
// Calls sqlite3_enable_shared_cache
func
EnableSharedCache
(
b
bool
)
os
.
Error
{
func
EnableSharedCache
(
b
bool
)
os
.
Error
{
rv
:=
C
.
sqlite3_enable_shared_cache
(
btocint
(
b
))
rv
:=
C
.
sqlite3_enable_shared_cache
(
btocint
(
b
))
if
rv
!=
C
.
SQLITE_OK
{
if
rv
!=
C
.
SQLITE_OK
{
...
@@ -143,6 +146,7 @@ func EnableSharedCache(b bool) os.Error {
...
@@ -143,6 +146,7 @@ func EnableSharedCache(b bool) os.Error {
// ':memory:' for memory db
// ':memory:' for memory db
// '' for temp db
// '' for temp db
// Calls sqlite3_open_v2
func
Open
(
filename
string
)
(
*
Conn
,
os
.
Error
)
{
func
Open
(
filename
string
)
(
*
Conn
,
os
.
Error
)
{
if
C
.
sqlite3_threadsafe
()
==
0
{
if
C
.
sqlite3_threadsafe
()
==
0
{
return
nil
,
os
.
NewError
(
"sqlite library was not compiled for thread-safe operation"
)
return
nil
,
os
.
NewError
(
"sqlite library was not compiled for thread-safe operation"
)
...
@@ -166,9 +170,10 @@ func Open(filename string) (*Conn, os.Error) {
...
@@ -166,9 +170,10 @@ func Open(filename string) (*Conn, os.Error) {
if
db
==
nil
{
if
db
==
nil
{
return
nil
,
os
.
NewError
(
"sqlite succeeded without returning a database"
)
return
nil
,
os
.
NewError
(
"sqlite succeeded without returning a database"
)
}
}
return
&
Conn
{
db
},
nil
return
&
Conn
{
db
:
db
},
nil
}
}
// Calls sqlite3_busy_timeout
func
(
c
*
Conn
)
BusyTimeout
(
ms
int
)
os
.
Error
{
func
(
c
*
Conn
)
BusyTimeout
(
ms
int
)
os
.
Error
{
rv
:=
C
.
sqlite3_busy_timeout
(
c
.
db
,
C
.
int
(
ms
))
rv
:=
C
.
sqlite3_busy_timeout
(
c
.
db
,
C
.
int
(
ms
))
if
rv
==
C
.
SQLITE_OK
{
if
rv
==
C
.
SQLITE_OK
{
...
@@ -177,9 +182,11 @@ func (c *Conn) BusyTimeout(ms int) os.Error {
...
@@ -177,9 +182,11 @@ func (c *Conn) BusyTimeout(ms int) os.Error {
return
Errno
(
rv
)
return
Errno
(
rv
)
}
}
// Calls sqlite3_db_config(db, SQLITE_DBCONFIG_ENABLE_FKEY, b)
func
(
c
*
Conn
)
EnableFKey
(
b
bool
)
(
bool
,
os
.
Error
)
{
func
(
c
*
Conn
)
EnableFKey
(
b
bool
)
(
bool
,
os
.
Error
)
{
return
c
.
queryOrSetEnableFKey
(
btocint
(
b
))
return
c
.
queryOrSetEnableFKey
(
btocint
(
b
))
}
}
// Calls sqlite3_db_config(db, SQLITE_DBCONFIG_ENABLE_FKEY, -1)
func
(
c
*
Conn
)
IsFKeyEnabled
()
(
bool
,
os
.
Error
)
{
func
(
c
*
Conn
)
IsFKeyEnabled
()
(
bool
,
os
.
Error
)
{
return
c
.
queryOrSetEnableFKey
(
-
1
)
return
c
.
queryOrSetEnableFKey
(
-
1
)
}
}
...
@@ -193,6 +200,7 @@ func (c *Conn) queryOrSetEnableFKey(i C.int) (bool, os.Error) {
...
@@ -193,6 +200,7 @@ func (c *Conn) queryOrSetEnableFKey(i C.int) (bool, os.Error) {
}
}
// Don't use it with SELECT or anything that returns data.
// Don't use it with SELECT or anything that returns data.
// Calls sqlite3_prepare_v2, sqlite3_bind_*, sqlite3_step and sqlite3_finalize
func
(
c
*
Conn
)
Exec
(
cmd
string
,
args
...
interface
{})
os
.
Error
{
func
(
c
*
Conn
)
Exec
(
cmd
string
,
args
...
interface
{})
os
.
Error
{
for
len
(
cmd
)
>
0
{
for
len
(
cmd
)
>
0
{
s
,
err
:=
c
.
Prepare
(
cmd
)
s
,
err
:=
c
.
Prepare
(
cmd
)
...
@@ -221,18 +229,22 @@ func (c *Conn) Exec(cmd string, args ...interface{}) os.Error {
...
@@ -221,18 +229,22 @@ func (c *Conn) Exec(cmd string, args ...interface{}) os.Error {
return
nil
return
nil
}
}
// Calls sqlite3_changes
func
(
c
*
Conn
)
Changes
()
int
{
func
(
c
*
Conn
)
Changes
()
int
{
return
int
(
C
.
sqlite3_changes
(
c
.
db
))
return
int
(
C
.
sqlite3_changes
(
c
.
db
))
}
}
// Calls sqlite3_last_insert_rowid
func
(
c
*
Conn
)
LastInsertRowid
()
int64
{
func
(
c
*
Conn
)
LastInsertRowid
()
int64
{
return
int64
(
C
.
sqlite3_last_insert_rowid
(
c
.
db
))
return
int64
(
C
.
sqlite3_last_insert_rowid
(
c
.
db
))
}
}
// Calls sqlite3_interrupt
func
(
c
*
Conn
)
Interrupt
()
{
func
(
c
*
Conn
)
Interrupt
()
{
C
.
sqlite3_interrupt
(
c
.
db
)
C
.
sqlite3_interrupt
(
c
.
db
)
}
}
// Prepared Statement (sqlite3_stmt)
type
Stmt
struct
{
type
Stmt
struct
{
c
*
Conn
c
*
Conn
stmt
*
C
.
sqlite3_stmt
stmt
*
C
.
sqlite3_stmt
...
@@ -240,6 +252,7 @@ type Stmt struct {
...
@@ -240,6 +252,7 @@ type Stmt struct {
cols
map
[
string
]
int
// cached columns index by name
cols
map
[
string
]
int
// cached columns index by name
}
}
// Calls sqlite3_prepare_v2 and sqlite3_bind_*
func
(
c
*
Conn
)
Prepare
(
cmd
string
,
args
...
interface
{})
(
*
Stmt
,
os
.
Error
)
{
func
(
c
*
Conn
)
Prepare
(
cmd
string
,
args
...
interface
{})
(
*
Stmt
,
os
.
Error
)
{
if
c
==
nil
||
c
.
db
==
nil
{
if
c
==
nil
||
c
.
db
==
nil
{
return
nil
,
os
.
NewError
(
"nil sqlite database"
)
return
nil
,
os
.
NewError
(
"nil sqlite database"
)
...
@@ -267,6 +280,7 @@ func (c *Conn) Prepare(cmd string, args ...interface{}) (*Stmt, os.Error) {
...
@@ -267,6 +280,7 @@ func (c *Conn) Prepare(cmd string, args ...interface{}) (*Stmt, os.Error) {
}
}
// Don't use it with SELECT or anything that returns data.
// Don't use it with SELECT or anything that returns data.
// Calls sqlite3_bind_* and sqlite3_step
func
(
s
*
Stmt
)
Exec
(
args
...
interface
{})
os
.
Error
{
func
(
s
*
Stmt
)
Exec
(
args
...
interface
{})
os
.
Error
{
err
:=
s
.
Bind
(
args
...
)
err
:=
s
.
Bind
(
args
...
)
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -279,16 +293,19 @@ func (s *Stmt) Exec(args ...interface{}) os.Error {
...
@@ -279,16 +293,19 @@ func (s *Stmt) Exec(args ...interface{}) os.Error {
return
nil
return
nil
}
}
// Calls sqlite3_bind_parameter_count
func
(
s
*
Stmt
)
BindParameterCount
()
int
{
func
(
s
*
Stmt
)
BindParameterCount
()
int
{
return
int
(
C
.
sqlite3_bind_parameter_count
(
s
.
stmt
))
return
int
(
C
.
sqlite3_bind_parameter_count
(
s
.
stmt
))
}
}
// Calls sqlite3_bind_parameter_index
func
(
s
*
Stmt
)
BindParameterIndex
(
name
string
)
int
{
func
(
s
*
Stmt
)
BindParameterIndex
(
name
string
)
int
{
cname
:=
C
.
CString
(
name
)
cname
:=
C
.
CString
(
name
)
defer
C
.
free
(
unsafe
.
Pointer
(
cname
))
defer
C
.
free
(
unsafe
.
Pointer
(
cname
))
return
int
(
C
.
sqlite3_bind_parameter_index
(
s
.
stmt
,
cname
))
return
int
(
C
.
sqlite3_bind_parameter_index
(
s
.
stmt
,
cname
))
}
}
// Calls sqlite3_bind_parameter_count and sqlite3_bind_(blob|double|int|int64|null|text) depending on args type.
func
(
s
*
Stmt
)
Bind
(
args
...
interface
{})
os
.
Error
{
func
(
s
*
Stmt
)
Bind
(
args
...
interface
{})
os
.
Error
{
err
:=
s
.
Reset
()
err
:=
s
.
Reset
()
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -336,6 +353,7 @@ func (s *Stmt) Bind(args ...interface{}) os.Error {
...
@@ -336,6 +353,7 @@ func (s *Stmt) Bind(args ...interface{}) os.Error {
return
nil
return
nil
}
}
// Calls sqlite3_step
func
(
s
*
Stmt
)
Next
()
(
bool
,
os
.
Error
)
{
func
(
s
*
Stmt
)
Next
()
(
bool
,
os
.
Error
)
{
rv
:=
C
.
sqlite3_step
(
s
.
stmt
)
rv
:=
C
.
sqlite3_step
(
s
.
stmt
)
err
:=
Errno
(
rv
)
err
:=
Errno
(
rv
)
...
@@ -348,6 +366,7 @@ func (s *Stmt) Next() (bool, os.Error) {
...
@@ -348,6 +366,7 @@ func (s *Stmt) Next() (bool, os.Error) {
return
false
,
nil
return
false
,
nil
}
}
// Calls sqlite3_reset
func
(
s
*
Stmt
)
Reset
()
os
.
Error
{
func
(
s
*
Stmt
)
Reset
()
os
.
Error
{
rv
:=
C
.
sqlite3_reset
(
s
.
stmt
)
rv
:=
C
.
sqlite3_reset
(
s
.
stmt
)
if
rv
!=
C
.
SQLITE_OK
{
if
rv
!=
C
.
SQLITE_OK
{
...
@@ -356,15 +375,18 @@ func (s *Stmt) Reset() os.Error {
...
@@ -356,15 +375,18 @@ func (s *Stmt) Reset() os.Error {
return
nil
return
nil
}
}
// Calls sqlite3_column_count
func
(
s
*
Stmt
)
ColumnCount
()
int
{
func
(
s
*
Stmt
)
ColumnCount
()
int
{
return
int
(
C
.
sqlite3_column_count
(
s
.
stmt
))
return
int
(
C
.
sqlite3_column_count
(
s
.
stmt
))
}
}
// The leftmost column is number 0.
// The leftmost column is number 0.
// Calls sqlite3_column_name
func
(
s
*
Stmt
)
ColumnName
(
index
int
)
string
{
func
(
s
*
Stmt
)
ColumnName
(
index
int
)
string
{
return
C
.
GoString
(
C
.
sqlite3_column_name
(
s
.
stmt
,
C
.
int
(
index
)))
return
C
.
GoString
(
C
.
sqlite3_column_name
(
s
.
stmt
,
C
.
int
(
index
)))
}
}
// Calls sqlite3_column_count, sqlite3_column_name and sqlite3_column_(blob|double|int|int64|text) depending on args type.
func
(
s
*
Stmt
)
NamedScan
(
args
...
interface
{})
os
.
Error
{
func
(
s
*
Stmt
)
NamedScan
(
args
...
interface
{})
os
.
Error
{
if
len
(
args
)
%
2
!=
0
{
if
len
(
args
)
%
2
!=
0
{
return
os
.
NewError
(
"Expected an even number of arguments"
)
return
os
.
NewError
(
"Expected an even number of arguments"
)
...
@@ -387,6 +409,7 @@ func (s *Stmt) NamedScan(args ...interface{}) os.Error {
...
@@ -387,6 +409,7 @@ func (s *Stmt) NamedScan(args ...interface{}) os.Error {
return
nil
return
nil
}
}
// Calls sqlite3_column_count and sqlite3_column_(blob|double|int|int64|text) depending on args type.
func
(
s
*
Stmt
)
Scan
(
args
...
interface
{})
os
.
Error
{
func
(
s
*
Stmt
)
Scan
(
args
...
interface
{})
os
.
Error
{
n
:=
s
.
ColumnCount
()
n
:=
s
.
ColumnCount
()
if
n
!=
len
(
args
)
{
if
n
!=
len
(
args
)
{
...
@@ -450,6 +473,8 @@ func (s *Stmt) scanField(index int, value interface{}) os.Error {
...
@@ -450,6 +473,8 @@ func (s *Stmt) scanField(index int, value interface{}) os.Error {
}
}
return
nil
return
nil
}
}
// Calls sqlite3_finalize
func
(
s
*
Stmt
)
Finalize
()
os
.
Error
{
func
(
s
*
Stmt
)
Finalize
()
os
.
Error
{
rv
:=
C
.
sqlite3_finalize
(
s
.
stmt
)
rv
:=
C
.
sqlite3_finalize
(
s
.
stmt
)
if
rv
!=
C
.
SQLITE_OK
{
if
rv
!=
C
.
SQLITE_OK
{
...
@@ -459,6 +484,7 @@ func (s *Stmt) Finalize() os.Error {
...
@@ -459,6 +484,7 @@ func (s *Stmt) Finalize() os.Error {
return
nil
return
nil
}
}
// Calls sqlite3_close
func
(
c
*
Conn
)
Close
()
os
.
Error
{
func
(
c
*
Conn
)
Close
()
os
.
Error
{
if
c
==
nil
||
c
.
db
==
nil
{
if
c
==
nil
||
c
.
db
==
nil
{
return
os
.
NewError
(
"nil sqlite database"
)
return
os
.
NewError
(
"nil sqlite database"
)
...
...
sqlite_test.go
View file @
c22f0a7c
package
sqlite
package
sqlite
import
(
import
(
"fmt"
"strings"
"strings"
"testing"
"testing"
"os"
"os"
)
)
func
trace
(
d
interface
{},
t
string
)
{
fmt
.
Printf
(
"%s: %s
\n
"
,
d
,
t
)
}
func
authorizer
(
d
interface
{},
action
int
,
arg1
,
arg2
,
arg3
,
arg4
string
)
int
{
fmt
.
Printf
(
"%s: %d, %s, %s, %s, %s
\n
"
,
d
,
action
,
arg1
,
arg2
,
arg3
,
arg4
)
return
0
}
func
open
(
t
*
testing
.
T
)
*
Conn
{
func
open
(
t
*
testing
.
T
)
*
Conn
{
db
,
err
:=
Open
(
""
)
db
,
err
:=
Open
(
""
)
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -14,6 +24,8 @@ func open(t *testing.T) *Conn {
...
@@ -14,6 +24,8 @@ func open(t *testing.T) *Conn {
if
db
==
nil
{
if
db
==
nil
{
t
.
Fatal
(
"opened database is nil"
)
t
.
Fatal
(
"opened database is nil"
)
}
}
//db.Trace(trace, "TRACE")
//db.SetAuthorizer(authorizer, "AUTH") FIXME panic
return
db
return
db
}
}
...
@@ -35,6 +47,8 @@ func TestVersion(t *testing.T) {
...
@@ -35,6 +47,8 @@ func TestVersion(t *testing.T) {
func
TestOpen
(
t
*
testing
.
T
)
{
func
TestOpen
(
t
*
testing
.
T
)
{
db
:=
open
(
t
)
db
:=
open
(
t
)
db
.
Trace
(
nil
,
nil
)
db
.
SetAuthorizer
(
nil
,
nil
)
db
.
Close
()
db
.
Close
()
}
}
...
@@ -138,6 +152,7 @@ func TestInsertWithStatement(t *testing.T) {
...
@@ -138,6 +152,7 @@ func TestInsertWithStatement(t *testing.T) {
t
.
Errorf
(
"insert error: %d <> 1"
,
c
)
t
.
Errorf
(
"insert error: %d <> 1"
,
c
)
}
}
}
}
s
.
Finalize
()
cs
,
_
:=
db
.
Prepare
(
"SELECT COUNT(*) FROM test"
)
cs
,
_
:=
db
.
Prepare
(
"SELECT COUNT(*) FROM test"
)
defer
cs
.
Finalize
()
defer
cs
.
Finalize
()
...
...
trace.go
View file @
c22f0a7c
...
@@ -14,10 +14,17 @@ extern void goXTrace(void *pArg, const char *t);
...
@@ -14,10 +14,17 @@ extern void goXTrace(void *pArg, const char *t);
static void goSqlite3Trace(sqlite3 *db, void *pArg) {
static void goSqlite3Trace(sqlite3 *db, void *pArg) {
sqlite3_trace(db, goXTrace, pArg);
sqlite3_trace(db, goXTrace, pArg);
}
}
extern int goXAuth(void *pUserData, int action, const char *arg1, const char *arg2, const char *arg3, const char *arg4);
static int goSqlite3SetAuthorizer(sqlite3 *db, void *pUserData) {
return sqlite3_set_authorizer(db, goXAuth, pUserData);
}
*/
*/
import
"C"
import
"C"
import
(
import
(
"fmt"
"unsafe"
"unsafe"
)
)
...
@@ -34,7 +41,42 @@ func goXTrace(pArg unsafe.Pointer, t *C.char) {
...
@@ -34,7 +41,42 @@ func goXTrace(pArg unsafe.Pointer, t *C.char) {
arg
.
f
(
arg
.
d
,
C
.
GoString
(
t
))
arg
.
f
(
arg
.
d
,
C
.
GoString
(
t
))
}
}
// Calls sqlite3_trace
func
(
c
*
Conn
)
Trace
(
f
SqliteTrace
,
arg
interface
{})
{
func
(
c
*
Conn
)
Trace
(
f
SqliteTrace
,
arg
interface
{})
{
if
f
==
nil
{
C
.
sqlite3_trace
(
c
.
db
,
nil
,
nil
)
return
}
pArg
:=
unsafe
.
Pointer
(
&
sqliteTrace
{
f
,
arg
})
pArg
:=
unsafe
.
Pointer
(
&
sqliteTrace
{
f
,
arg
})
C
.
goSqlite3Trace
(
c
.
db
,
pArg
)
C
.
goSqlite3Trace
(
c
.
db
,
pArg
)
}
}
type
SqliteAuthorizer
func
(
d
interface
{},
action
int
,
arg1
,
arg2
,
arg3
,
arg4
string
)
int
type
sqliteAuthorizer
struct
{
f
SqliteAuthorizer
d
interface
{}
}
//export goXAuth
func
goXAuth
(
pUserData
unsafe
.
Pointer
,
action
C
.
int
,
arg1
,
arg2
,
arg3
,
arg4
*
C
.
char
)
C
.
int
{
var
result
int
if
pUserData
!=
nil
{
arg
:=
(
*
sqliteAuthorizer
)(
pUserData
)
result
=
arg
.
f
(
arg
.
d
,
int
(
action
),
C
.
GoString
(
arg1
),
C
.
GoString
(
arg2
),
C
.
GoString
(
arg3
),
C
.
GoString
(
arg4
))
}
else
{
fmt
.
Printf
(
"ERROR - %v
\n
"
,
pUserData
)
result
=
0
}
return
C
.
int
(
result
)
}
// Calls sqlite3_set_authorizer
func
(
c
*
Conn
)
SetAuthorizer
(
f
SqliteAuthorizer
,
arg
interface
{})
int
{
// c.authorizer = f
if
f
==
nil
{
return
int
(
C
.
sqlite3_set_authorizer
(
c
.
db
,
nil
,
nil
))
}
pArg
:=
unsafe
.
Pointer
(
&
sqliteAuthorizer
{
f
,
arg
})
return
int
(
C
.
goSqlite3SetAuthorizer
(
c
.
db
,
pArg
))
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment