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
e8891f64
Commit
e8891f64
authored
Nov 02, 2011
by
gwenn
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix error type.
parent
ecae65ef
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
108 additions
and
114 deletions
+108
-114
backup.go
backup.go
+5
-5
blob.go
blob.go
+10
-10
meta.go
meta.go
+7
-10
sqlite.go
sqlite.go
+83
-83
trace.go
trace.go
+3
-6
No files found.
backup.go
View file @
e8891f64
...
...
@@ -19,7 +19,7 @@ import (
)
// Calls http://sqlite.org/c3ref/backup_finish.html#sqlite3backupinit
func
NewBackup
(
dst
*
Conn
,
dstTable
string
,
src
*
Conn
,
srcTable
string
)
(
*
Backup
,
os
.
E
rror
)
{
func
NewBackup
(
dst
*
Conn
,
dstTable
string
,
src
*
Conn
,
srcTable
string
)
(
*
Backup
,
e
rror
)
{
dname
:=
C
.
CString
(
dstTable
)
sname
:=
C
.
CString
(
srcTable
)
defer
C
.
free
(
unsafe
.
Pointer
(
dname
))
...
...
@@ -38,7 +38,7 @@ type Backup struct {
}
// Calls http://sqlite.org/c3ref/backup_finish.html#sqlite3backupstep
func
(
b
*
Backup
)
Step
(
npage
int
)
os
.
E
rror
{
func
(
b
*
Backup
)
Step
(
npage
int
)
e
rror
{
rv
:=
C
.
sqlite3_backup_step
(
b
.
sb
,
C
.
int
(
npage
))
if
rv
==
C
.
SQLITE_OK
||
Errno
(
rv
)
==
ErrBusy
||
Errno
(
rv
)
==
ErrLocked
{
return
nil
...
...
@@ -57,8 +57,8 @@ func (b *Backup) Status() BackupStatus {
}
// Calls http://sqlite.org/c3ref/backup_finish.html#sqlite3backupstep, sqlite3_backup_remaining and sqlite3_backup_pagecount
func
(
b
*
Backup
)
Run
(
npage
int
,
sleepNs
int64
,
c
chan
<-
BackupStatus
)
os
.
E
rror
{
var
err
os
.
E
rror
func
(
b
*
Backup
)
Run
(
npage
int
,
sleepNs
int64
,
c
chan
<-
BackupStatus
)
e
rror
{
var
err
e
rror
for
{
err
=
b
.
Step
(
npage
)
if
err
!=
nil
{
...
...
@@ -73,7 +73,7 @@ func (b *Backup) Run(npage int, sleepNs int64, c chan<- BackupStatus) os.Error {
}
// Calls http://sqlite.org/c3ref/backup_finish.html#sqlite3backupfinish
func
(
b
*
Backup
)
Close
()
os
.
E
rror
{
func
(
b
*
Backup
)
Close
()
e
rror
{
if
b
.
sb
==
nil
{
return
os
.
EINVAL
}
...
...
blob.go
View file @
e8891f64
...
...
@@ -13,7 +13,7 @@ package sqlite
import
"C"
import
(
"
o
s"
"
error
s"
"unsafe"
)
...
...
@@ -29,7 +29,7 @@ type BlobReadWriter struct {
type
ZeroBlobLength
int
// Calls http://sqlite.org/c3ref/blob_open.html
func
(
c
*
Conn
)
NewBlobReader
(
db
,
table
,
column
string
,
row
int64
)
(
*
BlobReader
,
os
.
E
rror
)
{
func
(
c
*
Conn
)
NewBlobReader
(
db
,
table
,
column
string
,
row
int64
)
(
*
BlobReader
,
e
rror
)
{
bl
,
err
:=
c
.
blob_open
(
db
,
table
,
column
,
row
,
false
)
if
err
!=
nil
{
return
nil
,
err
...
...
@@ -38,7 +38,7 @@ func (c *Conn) NewBlobReader(db, table, column string, row int64) (*BlobReader,
}
// Calls http://sqlite.org/c3ref/blob_open.html
func
(
c
*
Conn
)
NewBlobReadWriter
(
db
,
table
,
column
string
,
row
int64
)
(
*
BlobReadWriter
,
os
.
E
rror
)
{
func
(
c
*
Conn
)
NewBlobReadWriter
(
db
,
table
,
column
string
,
row
int64
)
(
*
BlobReadWriter
,
e
rror
)
{
bl
,
err
:=
c
.
blob_open
(
db
,
table
,
column
,
row
,
true
)
if
err
!=
nil
{
return
nil
,
err
...
...
@@ -46,7 +46,7 @@ func (c *Conn) NewBlobReadWriter(db, table, column string, row int64) (*BlobRead
return
&
BlobReadWriter
{
BlobReader
{
c
,
bl
}},
nil
}
func
(
c
*
Conn
)
blob_open
(
db
,
table
,
column
string
,
row
int64
,
write
bool
)
(
*
C
.
sqlite3_blob
,
os
.
E
rror
)
{
func
(
c
*
Conn
)
blob_open
(
db
,
table
,
column
string
,
row
int64
,
write
bool
)
(
*
C
.
sqlite3_blob
,
e
rror
)
{
zDb
:=
C
.
CString
(
db
)
defer
C
.
free
(
unsafe
.
Pointer
(
zDb
))
zTable
:=
C
.
CString
(
table
)
...
...
@@ -62,13 +62,13 @@ func (c *Conn) blob_open(db, table, column string, row int64, write bool) (*C.sq
return
nil
,
c
.
error
(
rv
)
}
if
bl
==
nil
{
return
nil
,
os
.
NewError
(
"sqlite succeeded without returning a blob"
)
return
nil
,
errors
.
New
(
"sqlite succeeded without returning a blob"
)
}
return
bl
,
nil
}
// Calls http://sqlite.org/c3ref/blob_close.html
func
(
r
*
BlobReader
)
Close
()
os
.
E
rror
{
func
(
r
*
BlobReader
)
Close
()
e
rror
{
rv
:=
C
.
sqlite3_blob_close
(
r
.
bl
)
if
rv
!=
C
.
SQLITE_OK
{
return
r
.
c
.
error
(
rv
)
...
...
@@ -78,7 +78,7 @@ func (r *BlobReader) Close() os.Error {
}
// Calls http://sqlite.org/c3ref/blob_read.html
func
(
r
*
BlobReader
)
Read
(
v
[]
byte
)
(
int
,
os
.
E
rror
)
{
func
(
r
*
BlobReader
)
Read
(
v
[]
byte
)
(
int
,
e
rror
)
{
var
p
*
byte
if
len
(
v
)
>
0
{
p
=
&
v
[
0
]
...
...
@@ -91,13 +91,13 @@ func (r *BlobReader) Read(v []byte) (int, os.Error) {
}
// Calls http://sqlite.org/c3ref/blob_bytes.html
func
(
r
*
BlobReader
)
Size
()
(
int
,
os
.
E
rror
)
{
func
(
r
*
BlobReader
)
Size
()
(
int
,
e
rror
)
{
s
:=
C
.
sqlite3_blob_bytes
(
r
.
bl
)
return
int
(
s
),
nil
}
// Calls http://sqlite.org/c3ref/blob_write.html
func
(
w
*
BlobReadWriter
)
Write
(
v
[]
byte
)
(
int
,
os
.
E
rror
)
{
func
(
w
*
BlobReadWriter
)
Write
(
v
[]
byte
)
(
int
,
e
rror
)
{
var
p
*
byte
if
len
(
v
)
>
0
{
p
=
&
v
[
0
]
...
...
@@ -110,7 +110,7 @@ func (w *BlobReadWriter) Write(v []byte) (int, os.Error) {
}
// Calls http://sqlite.org/c3ref/blob_reopen.html
func
(
r
*
BlobReader
)
Reopen
(
rowid
int64
)
os
.
E
rror
{
func
(
r
*
BlobReader
)
Reopen
(
rowid
int64
)
e
rror
{
rv
:=
C
.
sqlite3_blob_reopen
(
r
.
bl
,
C
.
sqlite3_int64
(
rowid
))
if
rv
!=
C
.
SQLITE_OK
{
return
r
.
c
.
error
(
rv
)
...
...
meta.go
View file @
e8891f64
...
...
@@ -17,13 +17,10 @@ static char *my_mprintf(char *zFormat, char *arg) {
*/
import
"C"
import
(
"os"
"unsafe"
)
import
"unsafe"
// Executes pragma 'database_list'
func
(
c
*
Conn
)
Databases
()
(
map
[
string
]
string
,
os
.
E
rror
)
{
func
(
c
*
Conn
)
Databases
()
(
map
[
string
]
string
,
e
rror
)
{
s
,
err
:=
c
.
Prepare
(
"PRAGMA database_list"
)
if
err
!=
nil
{
return
nil
,
err
...
...
@@ -46,7 +43,7 @@ func (c *Conn) Databases() (map[string]string, os.Error) {
}
// Selects tables (no view) from 'sqlite_master' and filters system tables out.
func
(
c
*
Conn
)
Tables
()
([]
string
,
os
.
E
rror
)
{
func
(
c
*
Conn
)
Tables
()
([]
string
,
e
rror
)
{
s
,
err
:=
c
.
Prepare
(
"SELECT name FROM sqlite_master WHERE type IN ('table') AND name NOT LIKE 'sqlite_%'"
)
if
err
!=
nil
{
return
nil
,
err
...
...
@@ -77,7 +74,7 @@ type Column struct {
// Executes pragma 'table_info'
// TODO How to specify a database-name?
// TODO sqlite3_table_column_metadata?
func
(
c
*
Conn
)
Columns
(
table
string
)
([]
Column
,
os
.
E
rror
)
{
func
(
c
*
Conn
)
Columns
(
table
string
)
([]
Column
,
e
rror
)
{
s
,
err
:=
c
.
Prepare
(
Mprintf
(
"PRAGMA table_info(%Q)"
,
table
))
if
err
!=
nil
{
return
nil
,
err
...
...
@@ -107,7 +104,7 @@ type ForeignKey struct {
// Executes pragma 'foreign_key_list'
// TODO How to specify a database-name?
func
(
c
*
Conn
)
ForeignKeys
(
table
string
)
(
map
[
int
]
*
ForeignKey
,
os
.
E
rror
)
{
func
(
c
*
Conn
)
ForeignKeys
(
table
string
)
(
map
[
int
]
*
ForeignKey
,
e
rror
)
{
s
,
err
:=
c
.
Prepare
(
Mprintf
(
"PRAGMA foreign_key_list(%Q)"
,
table
))
if
err
!=
nil
{
return
nil
,
err
...
...
@@ -144,7 +141,7 @@ type Index struct {
// Executes pragma 'index_list'
// TODO How to specify a database-name?
func
(
c
*
Conn
)
Indexes
(
table
string
)
([]
Index
,
os
.
E
rror
)
{
func
(
c
*
Conn
)
Indexes
(
table
string
)
([]
Index
,
e
rror
)
{
s
,
err
:=
c
.
Prepare
(
Mprintf
(
"PRAGMA index_list(%Q)"
,
table
))
if
err
!=
nil
{
return
nil
,
err
...
...
@@ -168,7 +165,7 @@ func (c *Conn) Indexes(table string) ([]Index, os.Error) {
// Executes pragma 'index_info'
// Only Column.Cid and Column.Name are specified. All other fields are unspecifed.
func
(
c
*
Conn
)
IndexColumns
(
index
string
)
([]
Column
,
os
.
E
rror
)
{
func
(
c
*
Conn
)
IndexColumns
(
index
string
)
([]
Column
,
e
rror
)
{
s
,
err
:=
c
.
Prepare
(
Mprintf
(
"PRAGMA index_info(%Q)"
,
index
))
if
err
!=
nil
{
return
nil
,
err
...
...
sqlite.go
View file @
e8891f64
...
...
@@ -35,15 +35,15 @@ static int my_db_config(sqlite3 *db, int op, int v, int *ok) {
import
"C"
import
(
"errors"
"fmt"
"os"
"reflect"
"unsafe"
)
type
Errno
int
func
(
e
Errno
)
String
()
string
{
func
(
e
Errno
)
Error
()
string
{
s
:=
errText
[
e
]
if
s
==
""
{
return
fmt
.
Sprintf
(
"errno %d"
,
int
(
e
))
...
...
@@ -52,30 +52,30 @@ func (e Errno) String() string {
}
var
(
ErrError
os
.
E
rror
=
Errno
(
1
)
// /* SQL error or missing database */
ErrInternal
os
.
E
rror
=
Errno
(
2
)
// /* Internal logic error in SQLite */
ErrPerm
os
.
E
rror
=
Errno
(
3
)
// /* Access permission denied */
ErrAbort
os
.
E
rror
=
Errno
(
4
)
// /* Callback routine requested an abort */
ErrBusy
os
.
E
rror
=
Errno
(
5
)
// /* The database file is locked */
ErrLocked
os
.
E
rror
=
Errno
(
6
)
// /* A table in the database is locked */
ErrNoMem
os
.
E
rror
=
Errno
(
7
)
// /* A malloc() failed */
ErrReadOnly
os
.
E
rror
=
Errno
(
8
)
// /* Attempt to write a readonly database */
ErrInterrupt
os
.
E
rror
=
Errno
(
9
)
// /* Operation terminated by sqlite3_interrupt()*/
ErrIOErr
os
.
E
rror
=
Errno
(
10
)
// /* Some kind of disk I/O error occurred */
ErrCorrupt
os
.
E
rror
=
Errno
(
11
)
// /* The database disk image is malformed */
ErrFull
os
.
E
rror
=
Errno
(
13
)
// /* Insertion failed because database is full */
ErrCantOpen
os
.
E
rror
=
Errno
(
14
)
// /* Unable to open the database file */
ErrEmpty
os
.
E
rror
=
Errno
(
16
)
// /* Database is empty */
ErrSchema
os
.
E
rror
=
Errno
(
17
)
// /* The database schema changed */
ErrTooBig
os
.
E
rror
=
Errno
(
18
)
// /* String or BLOB exceeds size limit */
ErrConstraint
os
.
E
rror
=
Errno
(
19
)
// /* Abort due to constraint violation */
ErrMismatch
os
.
E
rror
=
Errno
(
20
)
// /* Data type mismatch */
ErrMisuse
os
.
E
rror
=
Errno
(
21
)
// /* Library used incorrectly */
ErrNolfs
os
.
E
rror
=
Errno
(
22
)
// /* Uses OS features not supported on host */
ErrAuth
os
.
E
rror
=
Errno
(
23
)
// /* Authorization denied */
ErrFormat
os
.
E
rror
=
Errno
(
24
)
// /* Auxiliary database format error */
ErrRange
os
.
E
rror
=
Errno
(
25
)
// /* 2nd parameter to sqlite3_bind out of range */
ErrNotDB
os
.
E
rror
=
Errno
(
26
)
// /* File opened that is not a database file */
ErrError
e
rror
=
Errno
(
1
)
// /* SQL error or missing database */
ErrInternal
e
rror
=
Errno
(
2
)
// /* Internal logic error in SQLite */
ErrPerm
e
rror
=
Errno
(
3
)
// /* Access permission denied */
ErrAbort
e
rror
=
Errno
(
4
)
// /* Callback routine requested an abort */
ErrBusy
e
rror
=
Errno
(
5
)
// /* The database file is locked */
ErrLocked
e
rror
=
Errno
(
6
)
// /* A table in the database is locked */
ErrNoMem
e
rror
=
Errno
(
7
)
// /* A malloc() failed */
ErrReadOnly
e
rror
=
Errno
(
8
)
// /* Attempt to write a readonly database */
ErrInterrupt
e
rror
=
Errno
(
9
)
// /* Operation terminated by sqlite3_interrupt()*/
ErrIOErr
e
rror
=
Errno
(
10
)
// /* Some kind of disk I/O error occurred */
ErrCorrupt
e
rror
=
Errno
(
11
)
// /* The database disk image is malformed */
ErrFull
e
rror
=
Errno
(
13
)
// /* Insertion failed because database is full */
ErrCantOpen
e
rror
=
Errno
(
14
)
// /* Unable to open the database file */
ErrEmpty
e
rror
=
Errno
(
16
)
// /* Database is empty */
ErrSchema
e
rror
=
Errno
(
17
)
// /* The database schema changed */
ErrTooBig
e
rror
=
Errno
(
18
)
// /* String or BLOB exceeds size limit */
ErrConstraint
e
rror
=
Errno
(
19
)
// /* Abort due to constraint violation */
ErrMismatch
e
rror
=
Errno
(
20
)
// /* Data type mismatch */
ErrMisuse
e
rror
=
Errno
(
21
)
// /* Library used incorrectly */
ErrNolfs
e
rror
=
Errno
(
22
)
// /* Uses OS features not supported on host */
ErrAuth
e
rror
=
Errno
(
23
)
// /* Authorization denied */
ErrFormat
e
rror
=
Errno
(
24
)
// /* Auxiliary database format error */
ErrRange
e
rror
=
Errno
(
25
)
// /* 2nd parameter to sqlite3_bind out of range */
ErrNotDB
e
rror
=
Errno
(
26
)
// /* File opened that is not a database file */
Row
=
Errno
(
100
)
// /* sqlite3_step() has another row ready */
Done
=
Errno
(
101
)
// /* sqlite3_step() has finished executing */
)
...
...
@@ -111,9 +111,9 @@ var errText = map[Errno]string{
101
:
"sqlite3_step() has finished executing"
,
}
func
(
c
*
Conn
)
error
(
rv
C
.
int
)
os
.
E
rror
{
func
(
c
*
Conn
)
error
(
rv
C
.
int
)
e
rror
{
if
c
==
nil
||
c
.
db
==
nil
{
return
os
.
NewError
(
"nil sqlite database"
)
return
errors
.
New
(
"nil sqlite database"
)
}
if
rv
==
C
.
SQLITE_OK
{
return
nil
...
...
@@ -121,13 +121,13 @@ func (c *Conn) error(rv C.int) os.Error {
if
rv
==
21
{
// misuse
return
Errno
(
rv
)
}
return
os
.
NewError
(
Errno
(
rv
)
.
String
()
+
": "
+
C
.
GoString
(
C
.
sqlite3_errmsg
(
c
.
db
)))
return
errors
.
New
(
Errno
(
rv
)
.
Error
()
+
": "
+
C
.
GoString
(
C
.
sqlite3_errmsg
(
c
.
db
)))
}
// Calls http://sqlite.org/c3ref/errcode.html
func
(
c
*
Conn
)
Error
()
os
.
E
rror
{
func
(
c
*
Conn
)
Error
()
e
rror
{
if
c
==
nil
||
c
.
db
==
nil
{
return
os
.
NewError
(
"nil sqlite database"
)
return
errors
.
New
(
"nil sqlite database"
)
}
return
c
.
error
(
C
.
sqlite3_errcode
(
c
.
db
))
}
...
...
@@ -167,9 +167,9 @@ const (
// ":memory:" for memory db
// "" for temp file db
// Calls sqlite3_open_v2: http://sqlite.org/c3ref/open.html
func
Open
(
filename
string
,
flags
...
OpenFlag
)
(
*
Conn
,
os
.
E
rror
)
{
func
Open
(
filename
string
,
flags
...
OpenFlag
)
(
*
Conn
,
e
rror
)
{
if
C
.
sqlite3_threadsafe
()
==
0
{
return
nil
,
os
.
NewError
(
"sqlite library was not compiled for thread-safe operation"
)
return
nil
,
errors
.
New
(
"sqlite library was not compiled for thread-safe operation"
)
}
var
openFlags
int
if
len
(
flags
)
>
0
{
...
...
@@ -191,13 +191,13 @@ func Open(filename string, flags ...OpenFlag) (*Conn, os.Error) {
return
nil
,
Errno
(
rv
)
}
if
db
==
nil
{
return
nil
,
os
.
NewError
(
"sqlite succeeded without returning a database"
)
return
nil
,
errors
.
New
(
"sqlite succeeded without returning a database"
)
}
return
&
Conn
{
db
:
db
},
nil
}
// Calls http://sqlite.org/c3ref/busy_timeout.html
func
(
c
*
Conn
)
BusyTimeout
(
ms
int
)
os
.
E
rror
{
func
(
c
*
Conn
)
BusyTimeout
(
ms
int
)
e
rror
{
rv
:=
C
.
sqlite3_busy_timeout
(
c
.
db
,
C
.
int
(
ms
))
if
rv
==
C
.
SQLITE_OK
{
return
nil
...
...
@@ -208,16 +208,16 @@ func (c *Conn) BusyTimeout(ms int) os.Error {
// Calls sqlite3_db_config(db, SQLITE_DBCONFIG_ENABLE_FKEY, b)
// Another way is PRAGMA foreign_keys = boolean;
// http://sqlite.org/c3ref/c_dbconfig_enable_fkey.html
func
(
c
*
Conn
)
EnableFKey
(
b
bool
)
(
bool
,
os
.
E
rror
)
{
func
(
c
*
Conn
)
EnableFKey
(
b
bool
)
(
bool
,
e
rror
)
{
return
c
.
queryOrSetEnableFKey
(
btocint
(
b
))
}
// Calls sqlite3_db_config(db, SQLITE_DBCONFIG_ENABLE_FKEY, -1)
// Another way is PRAGMA foreign_keys;
// http://sqlite.org/c3ref/c_dbconfig_enable_fkey.html
func
(
c
*
Conn
)
IsFKeyEnabled
()
(
bool
,
os
.
E
rror
)
{
func
(
c
*
Conn
)
IsFKeyEnabled
()
(
bool
,
e
rror
)
{
return
c
.
queryOrSetEnableFKey
(
-
1
)
}
func
(
c
*
Conn
)
queryOrSetEnableFKey
(
i
C
.
int
)
(
bool
,
os
.
E
rror
)
{
func
(
c
*
Conn
)
queryOrSetEnableFKey
(
i
C
.
int
)
(
bool
,
e
rror
)
{
var
ok
C
.
int
rv
:=
C
.
my_db_config
(
c
.
db
,
C
.
SQLITE_DBCONFIG_ENABLE_FKEY
,
i
,
&
ok
)
if
rv
==
C
.
SQLITE_OK
{
...
...
@@ -230,7 +230,7 @@ func (c *Conn) queryOrSetEnableFKey(i C.int) (bool, os.Error) {
// Calls sqlite3_prepare_v2, sqlite3_bind_*, sqlite3_step and sqlite3_finalize
// http://sqlite.org/c3ref/prepare.html, http://sqlite.org/c3ref/bind_blob.html,
// http://sqlite.org/c3ref/step.html and http://sqlite.org/c3ref/finalize.html
func
(
c
*
Conn
)
Exec
(
cmd
string
,
args
...
interface
{})
os
.
E
rror
{
func
(
c
*
Conn
)
Exec
(
cmd
string
,
args
...
interface
{})
e
rror
{
for
len
(
cmd
)
>
0
{
s
,
err
:=
c
.
Prepare
(
cmd
)
if
err
!=
nil
{
...
...
@@ -251,7 +251,7 @@ func (c *Conn) Exec(cmd string, args ...interface{}) os.Error {
if
len
(
s
.
tail
)
>
0
{
if
len
(
args
)
>
0
{
s
.
Finalize
()
return
os
.
NewError
(
"Cannot execute multiple statements when args are specified"
)
return
errors
.
New
(
"Cannot execute multiple statements when args are specified"
)
}
}
cmd
=
s
.
tail
...
...
@@ -263,7 +263,7 @@ func (c *Conn) Exec(cmd string, args ...interface{}) os.Error {
}
// Returns true if the specified query returns at least one row.
func
(
c
*
Conn
)
Exists
(
query
string
,
args
...
interface
{})
(
bool
,
os
.
E
rror
)
{
func
(
c
*
Conn
)
Exists
(
query
string
,
args
...
interface
{})
(
bool
,
e
rror
)
{
s
,
err
:=
c
.
Prepare
(
query
,
args
...
)
if
err
!=
nil
{
return
false
,
err
...
...
@@ -304,11 +304,11 @@ const (
EXCLUSIVE
TransactionType
=
2
)
func
(
c
*
Conn
)
Begin
()
os
.
E
rror
{
func
(
c
*
Conn
)
Begin
()
e
rror
{
return
c
.
BeginTransaction
(
DEFERRED
)
}
func
(
c
*
Conn
)
BeginTransaction
(
t
TransactionType
)
os
.
E
rror
{
func
(
c
*
Conn
)
BeginTransaction
(
t
TransactionType
)
e
rror
{
if
t
==
DEFERRED
{
return
c
.
exec
(
"BEGIN"
)
}
else
if
t
==
IMMEDIATE
{
...
...
@@ -320,16 +320,16 @@ func (c *Conn) BeginTransaction(t TransactionType) os.Error {
return
nil
}
func
(
c
*
Conn
)
Commit
()
os
.
E
rror
{
func
(
c
*
Conn
)
Commit
()
e
rror
{
// TODO Check autocommit?
return
c
.
exec
(
"COMMIT"
)
}
func
(
c
*
Conn
)
Rollback
()
os
.
E
rror
{
func
(
c
*
Conn
)
Rollback
()
e
rror
{
// TODO Check autocommit?
return
c
.
exec
(
"ROLLBACK"
)
}
func
(
c
*
Conn
)
exec
(
cmd
string
)
os
.
E
rror
{
func
(
c
*
Conn
)
exec
(
cmd
string
)
e
rror
{
cmdstr
:=
C
.
CString
(
cmd
)
defer
C
.
free
(
unsafe
.
Pointer
(
cmdstr
))
rv
:=
C
.
sqlite3_exec
(
c
.
db
,
cmdstr
,
nil
,
nil
,
nil
)
...
...
@@ -353,9 +353,9 @@ type Stmt struct {
// Calls sqlite3_prepare_v2 and sqlite3_bind_*
// http://sqlite.org/c3ref/prepare.html, http://sqlite.org/c3ref/bind_blob.html,
func
(
c
*
Conn
)
Prepare
(
cmd
string
,
args
...
interface
{})
(
*
Stmt
,
os
.
E
rror
)
{
func
(
c
*
Conn
)
Prepare
(
cmd
string
,
args
...
interface
{})
(
*
Stmt
,
e
rror
)
{
if
c
==
nil
||
c
.
db
==
nil
{
return
nil
,
os
.
NewError
(
"nil sqlite database"
)
return
nil
,
errors
.
New
(
"nil sqlite database"
)
}
cmdstr
:=
C
.
CString
(
cmd
)
defer
C
.
free
(
unsafe
.
Pointer
(
cmdstr
))
...
...
@@ -382,7 +382,7 @@ func (c *Conn) Prepare(cmd string, args ...interface{}) (*Stmt, os.Error) {
// Don't use it with SELECT or anything that returns data.
// Calls sqlite3_bind_* and sqlite3_step
// http://sqlite.org/c3ref/bind_blob.html, http://sqlite.org/c3ref/step.html
func
(
s
*
Stmt
)
Exec
(
args
...
interface
{})
os
.
E
rror
{
func
(
s
*
Stmt
)
Exec
(
args
...
interface
{})
e
rror
{
err
:=
s
.
Bind
(
args
...
)
if
err
!=
nil
{
return
err
...
...
@@ -396,7 +396,7 @@ func (s *Stmt) Exec(args ...interface{}) os.Error {
// Don't use it with SELECT or anything that returns data.
// Like Exec but returns the number of rows that were changed or inserted or deleted.
func
(
s
*
Stmt
)
ExecUpdate
(
args
...
interface
{})
(
int
,
os
.
E
rror
)
{
func
(
s
*
Stmt
)
ExecUpdate
(
args
...
interface
{})
(
int
,
e
rror
)
{
err
:=
s
.
Exec
(
args
...
)
if
err
!=
nil
{
return
-
1
,
err
...
...
@@ -424,7 +424,7 @@ func (s *Stmt) BindParameterName(i int) string {
// Calls sqlite3_bind_parameter_count and sqlite3_bind_(blob|double|int|int64|null|text) depending on args type.
// http://sqlite.org/c3ref/bind_blob.html
func
(
s
*
Stmt
)
Bind
(
args
...
interface
{})
os
.
E
rror
{
func
(
s
*
Stmt
)
Bind
(
args
...
interface
{})
e
rror
{
err
:=
s
.
Reset
()
// TODO sqlite3_clear_bindings?
if
err
!=
nil
{
return
err
...
...
@@ -432,7 +432,7 @@ func (s *Stmt) Bind(args ...interface{}) os.Error {
n
:=
s
.
BindParameterCount
()
if
n
!=
len
(
args
)
{
// What happens when the number of arguments is less than the number of parameters?
return
os
.
NewError
(
fmt
.
Sprintf
(
"incorrect argument count for Stmt.Bind: have %d want %d"
,
len
(
args
),
n
))
return
errors
.
New
(
fmt
.
Sprintf
(
"incorrect argument count for Stmt.Bind: have %d want %d"
,
len
(
args
),
n
))
}
for
i
,
v
:=
range
args
{
...
...
@@ -466,7 +466,7 @@ func (s *Stmt) Bind(args ...interface{}) os.Error {
case
ZeroBlobLength
:
rv
=
C
.
sqlite3_bind_zeroblob
(
s
.
stmt
,
index
,
C
.
int
(
v
))
default
:
return
os
.
NewError
(
"unsupported type in Bind: "
+
reflect
.
TypeOf
(
v
)
.
String
())
return
errors
.
New
(
"unsupported type in Bind: "
+
reflect
.
TypeOf
(
v
)
.
String
())
}
if
rv
!=
C
.
SQLITE_OK
{
return
s
.
c
.
error
(
rv
)
...
...
@@ -490,7 +490,7 @@ func (s *Stmt) Bind(args ...interface{}) os.Error {
// }
// Calls sqlite3_step
// http://sqlite.org/c3ref/step.html
func
(
s
*
Stmt
)
Next
()
(
bool
,
os
.
E
rror
)
{
func
(
s
*
Stmt
)
Next
()
(
bool
,
e
rror
)
{
rv
:=
C
.
sqlite3_step
(
s
.
stmt
)
err
:=
Errno
(
rv
)
if
err
==
Row
{
...
...
@@ -503,7 +503,7 @@ func (s *Stmt) Next() (bool, os.Error) {
}
// Calls http://sqlite.org/c3ref/reset.html
func
(
s
*
Stmt
)
Reset
()
os
.
E
rror
{
func
(
s
*
Stmt
)
Reset
()
e
rror
{
rv
:=
C
.
sqlite3_reset
(
s
.
stmt
)
if
rv
!=
C
.
SQLITE_OK
{
return
s
.
c
.
error
(
rv
)
...
...
@@ -512,7 +512,7 @@ func (s *Stmt) Reset() os.Error {
}
// Calls http://sqlite.org/c3ref/clear_bindings.html
func
(
s
*
Stmt
)
ClearBindings
()
os
.
E
rror
{
func
(
s
*
Stmt
)
ClearBindings
()
e
rror
{
rv
:=
C
.
sqlite3_clear_bindings
(
s
.
stmt
)
if
rv
!=
C
.
SQLITE_OK
{
return
s
.
c
.
error
(
rv
)
...
...
@@ -569,14 +569,14 @@ func (s *Stmt) ColumnType(index int) Type {
// NULL value is converted to 0 if arg type is *int,*int64,*float,*float64, to "" for *string, to []byte{} for *[]byte and to false for *bool.
// Calls sqlite3_column_count, sqlite3_column_name and sqlite3_column_(blob|double|int|int64|text) depending on args type.
// http://sqlite.org/c3ref/column_blob.html
func
(
s
*
Stmt
)
NamedScan
(
args
...
interface
{})
os
.
E
rror
{
func
(
s
*
Stmt
)
NamedScan
(
args
...
interface
{})
e
rror
{
if
len
(
args
)
%
2
!=
0
{
return
os
.
NewError
(
"Expected an even number of arguments"
)
return
errors
.
New
(
"Expected an even number of arguments"
)
}
for
i
:=
0
;
i
<
len
(
args
);
i
+=
2
{
name
,
ok
:=
args
[
i
]
.
(
string
)
if
!
ok
{
return
os
.
NewError
(
"non-string field name field"
)
return
errors
.
New
(
"non-string field name field"
)
}
index
,
err
:=
s
.
ColumnIndex
(
name
)
// How to look up only once for one statement ?
if
err
!=
nil
{
...
...
@@ -595,10 +595,10 @@ func (s *Stmt) NamedScan(args ...interface{}) os.Error {
// TODO How to avoid NULL conversion?
// Calls sqlite3_column_count and sqlite3_column_(blob|double|int|int64|text) depending on args type.
// http://sqlite.org/c3ref/column_blob.html
func
(
s
*
Stmt
)
Scan
(
args
...
interface
{})
os
.
E
rror
{
func
(
s
*
Stmt
)
Scan
(
args
...
interface
{})
e
rror
{
n
:=
s
.
ColumnCount
()
if
n
!=
len
(
args
)
{
// What happens when the number of arguments is less than the number of columns?
return
os
.
NewError
(
fmt
.
Sprintf
(
"incorrect argument count for Stmt.Scan: have %d want %d"
,
len
(
args
),
n
))
return
errors
.
New
(
fmt
.
Sprintf
(
"incorrect argument count for Stmt.Scan: have %d want %d"
,
len
(
args
),
n
))
}
for
i
,
v
:=
range
args
{
...
...
@@ -618,7 +618,7 @@ func (s *Stmt) SQL() string {
// Must scan all columns (but result is cached).
// Calls sqlite3_column_count, sqlite3_column_name
// http://sqlite.org/c3ref/column_name.html
func
(
s
*
Stmt
)
ColumnIndex
(
name
string
)
(
int
,
os
.
E
rror
)
{
func
(
s
*
Stmt
)
ColumnIndex
(
name
string
)
(
int
,
e
rror
)
{
if
s
.
cols
==
nil
{
count
:=
s
.
ColumnCount
()
s
.
cols
=
make
(
map
[
string
]
int
,
count
)
...
...
@@ -630,14 +630,14 @@ func (s *Stmt) ColumnIndex(name string) (int, os.Error) {
if
ok
{
return
index
,
nil
}
return
0
,
os
.
NewError
(
"invalid column name: "
+
name
)
return
0
,
errors
.
New
(
"invalid column name: "
+
name
)
}
// Set nullable to false to skip NULL type test.
// Returns true when column is null and Stmt.CheckNull is activated.
// Calls sqlite3_column_count, sqlite3_column_name and sqlite3_column_(blob|double|int|int64|text) depending on args type.
// http://sqlite.org/c3ref/column_blob.html
func
(
s
*
Stmt
)
NamedScanColumn
(
name
string
,
value
interface
{})
(
bool
,
os
.
E
rror
)
{
func
(
s
*
Stmt
)
NamedScanColumn
(
name
string
,
value
interface
{})
(
bool
,
e
rror
)
{
index
,
err
:=
s
.
ColumnIndex
(
name
)
if
err
!=
nil
{
return
false
,
err
...
...
@@ -649,9 +649,9 @@ func (s *Stmt) NamedScanColumn(name string, value interface{}) (bool, os.Error)
// Returns true when column is null and Stmt.CheckNull is activated.
// Calls sqlite3_column_(blob|double|int|int64|text) depending on args type.
// http://sqlite.org/c3ref/column_blob.html
func
(
s
*
Stmt
)
ScanColumn
(
index
int
,
value
interface
{})
(
bool
,
os
.
E
rror
)
{
func
(
s
*
Stmt
)
ScanColumn
(
index
int
,
value
interface
{})
(
bool
,
e
rror
)
{
var
isNull
bool
var
err
os
.
E
rror
var
err
e
rror
switch
value
:=
value
.
(
type
)
{
case
nil
:
case
*
string
:
...
...
@@ -669,7 +669,7 @@ func (s *Stmt) ScanColumn(index int, value interface{}) (bool, os.Error) {
case
*
[]
byte
:
*
value
,
isNull
,
err
=
s
.
ScanBlob
(
index
)
default
:
return
false
,
os
.
NewError
(
"unsupported type in Scan: "
+
reflect
.
TypeOf
(
value
)
.
String
())
return
false
,
errors
.
New
(
"unsupported type in Scan: "
+
reflect
.
TypeOf
(
value
)
.
String
())
}
return
isNull
,
err
}
...
...
@@ -703,7 +703,7 @@ func (s *Stmt) ScanValue(index int) (value interface{}) {
// Returns true when column is null.
// Calls sqlite3_column_text.
// http://sqlite.org/c3ref/column_blob.html
func
(
s
*
Stmt
)
ScanText
(
index
int
)
(
value
string
,
isNull
bool
,
err
os
.
E
rror
)
{
func
(
s
*
Stmt
)
ScanText
(
index
int
)
(
value
string
,
isNull
bool
,
err
e
rror
)
{
p
:=
C
.
sqlite3_column_text
(
s
.
stmt
,
C
.
int
(
index
))
if
p
==
nil
{
isNull
=
true
...
...
@@ -718,7 +718,7 @@ func (s *Stmt) ScanText(index int) (value string, isNull bool, err os.Error) {
// Returns true when column is null and Stmt.CheckNull is activated.
// Calls sqlite3_column_int.
// http://sqlite.org/c3ref/column_blob.html
func
(
s
*
Stmt
)
ScanInt
(
index
int
)
(
value
int
,
isNull
bool
,
err
os
.
E
rror
)
{
func
(
s
*
Stmt
)
ScanInt
(
index
int
)
(
value
int
,
isNull
bool
,
err
e
rror
)
{
var
ctype
Type
if
s
.
CheckNull
||
s
.
CheckTypeMismatch
{
ctype
=
s
.
ColumnType
(
index
)
...
...
@@ -738,7 +738,7 @@ func (s *Stmt) ScanInt(index int) (value int, isNull bool, err os.Error) {
// Returns true when column is null and Stmt.CheckNull is activated.
// Calls sqlite3_column_int64.
// http://sqlite.org/c3ref/column_blob.html
func
(
s
*
Stmt
)
ScanInt64
(
index
int
)
(
value
int64
,
isNull
bool
,
err
os
.
E
rror
)
{
func
(
s
*
Stmt
)
ScanInt64
(
index
int
)
(
value
int64
,
isNull
bool
,
err
e
rror
)
{
var
ctype
Type
if
s
.
CheckNull
||
s
.
CheckTypeMismatch
{
ctype
=
s
.
ColumnType
(
index
)
...
...
@@ -758,7 +758,7 @@ func (s *Stmt) ScanInt64(index int) (value int64, isNull bool, err os.Error) {
// Returns true when column is null and Stmt.CheckNull is activated.
// Calls sqlite3_column_int.
// http://sqlite.org/c3ref/column_blob.html
func
(
s
*
Stmt
)
ScanByte
(
index
int
)
(
value
byte
,
isNull
bool
,
err
os
.
E
rror
)
{
func
(
s
*
Stmt
)
ScanByte
(
index
int
)
(
value
byte
,
isNull
bool
,
err
e
rror
)
{
var
ctype
Type
if
s
.
CheckNull
||
s
.
CheckTypeMismatch
{
ctype
=
s
.
ColumnType
(
index
)
...
...
@@ -778,7 +778,7 @@ func (s *Stmt) ScanByte(index int) (value byte, isNull bool, err os.Error) {
// Returns true when column is null and Stmt.CheckNull is activated.
// Calls sqlite3_column_int.
// http://sqlite.org/c3ref/column_blob.html
func
(
s
*
Stmt
)
ScanBool
(
index
int
)
(
value
bool
,
isNull
bool
,
err
os
.
E
rror
)
{
func
(
s
*
Stmt
)
ScanBool
(
index
int
)
(
value
bool
,
isNull
bool
,
err
e
rror
)
{
var
ctype
Type
if
s
.
CheckNull
||
s
.
CheckTypeMismatch
{
ctype
=
s
.
ColumnType
(
index
)
...
...
@@ -798,7 +798,7 @@ func (s *Stmt) ScanBool(index int) (value bool, isNull bool, err os.Error) {
// Returns true when column is null and Stmt.CheckNull is activated.
// Calls sqlite3_column_double.
// http://sqlite.org/c3ref/column_blob.html
func
(
s
*
Stmt
)
ScanFloat64
(
index
int
)
(
value
float64
,
isNull
bool
,
err
os
.
E
rror
)
{
func
(
s
*
Stmt
)
ScanFloat64
(
index
int
)
(
value
float64
,
isNull
bool
,
err
e
rror
)
{
var
ctype
Type
if
s
.
CheckNull
||
s
.
CheckTypeMismatch
{
ctype
=
s
.
ColumnType
(
index
)
...
...
@@ -818,7 +818,7 @@ func (s *Stmt) ScanFloat64(index int) (value float64, isNull bool, err os.Error)
// Returns true when column is null.
// Calls sqlite3_column_bytes.
// http://sqlite.org/c3ref/column_blob.html
func
(
s
*
Stmt
)
ScanBlob
(
index
int
)
(
value
[]
byte
,
isNull
bool
,
err
os
.
E
rror
)
{
func
(
s
*
Stmt
)
ScanBlob
(
index
int
)
(
value
[]
byte
,
isNull
bool
,
err
e
rror
)
{
p
:=
C
.
sqlite3_column_blob
(
s
.
stmt
,
C
.
int
(
index
))
if
p
==
nil
{
isNull
=
true
...
...
@@ -830,7 +830,7 @@ func (s *Stmt) ScanBlob(index int) (value []byte, isNull bool, err os.Error) {
}
// Only lossy conversion is reported as error.
func
(
s
*
Stmt
)
checkTypeMismatch
(
source
,
target
Type
)
os
.
E
rror
{
func
(
s
*
Stmt
)
checkTypeMismatch
(
source
,
target
Type
)
e
rror
{
switch
target
{
case
Integer
:
switch
source
{
...
...
@@ -853,7 +853,7 @@ func (s *Stmt) checkTypeMismatch(source, target Type) os.Error {
}
// Calls http://sqlite.org/c3ref/finalize.html
func
(
s
*
Stmt
)
Finalize
()
os
.
E
rror
{
func
(
s
*
Stmt
)
Finalize
()
e
rror
{
rv
:=
C
.
sqlite3_finalize
(
s
.
stmt
)
if
rv
!=
C
.
SQLITE_OK
{
return
s
.
c
.
error
(
rv
)
...
...
@@ -868,9 +868,9 @@ func (s *Stmt) Conn() *Conn {
}
// Calls http://sqlite.org/c3ref/close.html
func
(
c
*
Conn
)
Close
()
os
.
E
rror
{
func
(
c
*
Conn
)
Close
()
e
rror
{
if
c
==
nil
{
return
os
.
NewError
(
"nil sqlite database"
)
return
errors
.
New
(
"nil sqlite database"
)
}
// Dangling statements
stmt
:=
C
.
sqlite3_next_stmt
(
c
.
db
,
nil
)
...
...
@@ -898,7 +898,7 @@ func (c *Conn) EnableLoadExtension(b bool) {
C
.
sqlite3_enable_load_extension
(
c
.
db
,
btocint
(
b
))
}
// Calls http://sqlite.org/c3ref/load_extension.html
func
(
c
*
Conn
)
LoadExtension
(
file
string
,
proc
...
string
)
os
.
E
rror
{
func
(
c
*
Conn
)
LoadExtension
(
file
string
,
proc
...
string
)
e
rror
{
cfile
:=
C
.
CString
(
file
)
defer
C
.
free
(
unsafe
.
Pointer
(
cfile
))
var
cproc
*
C
.
char
...
...
@@ -910,7 +910,7 @@ func (c *Conn) LoadExtension(file string, proc ...string) os.Error {
rv
:=
C
.
sqlite3_load_extension
(
c
.
db
,
cfile
,
cproc
,
&
errMsg
)
if
rv
!=
C
.
SQLITE_OK
{
defer
C
.
sqlite3_free
(
unsafe
.
Pointer
(
errMsg
))
return
os
.
NewError
(
Errno
(
rv
)
.
String
()
+
": "
+
C
.
GoString
(
errMsg
))
return
errors
.
New
(
Errno
(
rv
)
.
Error
()
+
": "
+
C
.
GoString
(
errMsg
))
}
return
nil
}
...
...
@@ -922,7 +922,7 @@ func EnableSharedCache(b bool) {
// Must is a helper that wraps a call to a function returning (bool, os.Error)
// and panics if the error is non-nil.
func
Must
(
b
bool
,
err
os
.
E
rror
)
bool
{
func
Must
(
b
bool
,
err
e
rror
)
bool
{
if
err
!=
nil
{
panic
(
err
)
}
...
...
trace.go
View file @
e8891f64
...
...
@@ -46,10 +46,7 @@ static void my_log(int iErrCode, char *msg) {
*/
import
"C"
import
(
"os"
"unsafe"
)
import
"unsafe"
type
Tracer
func
(
udp
interface
{},
sql
string
)
...
...
@@ -162,7 +159,7 @@ func goXAuth(udp unsafe.Pointer, action C.int, arg1, arg2, dbName, triggerName *
}
// Calls http://sqlite.org/c3ref/set_authorizer.html
func
(
c
*
Conn
)
SetAuthorizer
(
f
Authorizer
,
udp
interface
{})
os
.
E
rror
{
func
(
c
*
Conn
)
SetAuthorizer
(
f
Authorizer
,
udp
interface
{})
e
rror
{
if
f
==
nil
{
c
.
authorizer
=
nil
return
c
.
error
(
C
.
sqlite3_set_authorizer
(
c
.
db
,
nil
,
nil
))
...
...
@@ -188,7 +185,7 @@ func goXBusy(udp unsafe.Pointer, count C.int) C.int {
// TODO NOT TESTED
// Calls http://sqlite.org/c3ref/busy_handler.html
func
(
c
*
Conn
)
BusyHandler
(
f
BusyHandler
,
udp
interface
{})
os
.
E
rror
{
func
(
c
*
Conn
)
BusyHandler
(
f
BusyHandler
,
udp
interface
{})
e
rror
{
if
f
==
nil
{
c
.
busyHandler
=
nil
return
c
.
error
(
C
.
sqlite3_busy_handler
(
c
.
db
,
nil
,
nil
))
...
...
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