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
7dc71ca7
Commit
7dc71ca7
authored
Jul 23, 2011
by
gwenn
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add methods to query/set FK support.
parent
c9dd3f26
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
47 additions
and
9 deletions
+47
-9
sqlite.go
sqlite.go
+29
-9
sqlite_test.go
sqlite_test.go
+18
-0
No files found.
sqlite.go
View file @
7dc71ca7
...
...
@@ -26,6 +26,11 @@ static int my_bind_text(sqlite3_stmt *stmt, int n, char *p, int np) {
static int my_bind_blob(sqlite3_stmt *stmt, int n, void *p, int np) {
return sqlite3_bind_blob(stmt, n, p, np, SQLITE_TRANSIENT);
}
// cgo doesn't support varargs
static int my_db_config(sqlite3 *db, int op, int v, int *ok) {
return sqlite3_db_config(db, op, v, ok);
}
*/
import
"C"
...
...
@@ -110,7 +115,7 @@ func (c *Conn) error(rv C.int) os.Error {
if
c
==
nil
||
c
.
db
==
nil
{
return
os
.
NewError
(
"nil sqlite database"
)
}
if
rv
==
0
{
if
rv
==
C
.
SQLITE_OK
{
return
nil
}
if
rv
==
21
{
// misuse
...
...
@@ -130,7 +135,7 @@ func Version() string {
func
EnableSharedCache
(
b
bool
)
os
.
Error
{
rv
:=
C
.
sqlite3_enable_shared_cache
(
btocint
(
b
))
if
rv
!=
0
{
if
rv
!=
C
.
SQLITE_OK
{
return
Errno
(
rv
)
}
return
nil
...
...
@@ -152,7 +157,7 @@ func Open(filename string) (*Conn, os.Error) {
C
.
SQLITE_OPEN_CREATE
|
C
.
SQLITE_OPEN_URI
,
nil
)
if
rv
!=
0
{
if
rv
!=
C
.
SQLITE_OK
{
if
db
!=
nil
{
C
.
sqlite3_close
(
db
)
}
...
...
@@ -166,12 +171,27 @@ func Open(filename string) (*Conn, os.Error) {
func
(
c
*
Conn
)
BusyTimeout
(
ms
int
)
os
.
Error
{
rv
:=
C
.
sqlite3_busy_timeout
(
c
.
db
,
C
.
int
(
ms
))
if
rv
==
0
{
if
rv
==
C
.
SQLITE_OK
{
return
nil
}
return
Errno
(
rv
)
}
func
(
c
*
Conn
)
EnableFKey
(
b
bool
)
(
bool
,
os
.
Error
)
{
return
c
.
queryOrSetEnableFKey
(
btocint
(
b
))
}
func
(
c
*
Conn
)
IsFKeyEnabled
()
(
bool
,
os
.
Error
)
{
return
c
.
queryOrSetEnableFKey
(
-
1
)
}
func
(
c
*
Conn
)
queryOrSetEnableFKey
(
i
C
.
int
)
(
bool
,
os
.
Error
)
{
var
ok
C
.
int
rv
:=
C
.
my_db_config
(
c
.
db
,
C
.
SQLITE_DBCONFIG_ENABLE_FKEY
,
i
,
&
ok
)
if
rv
==
C
.
SQLITE_OK
{
return
(
ok
==
1
),
nil
}
return
false
,
Errno
(
rv
)
}
// Don't use it with SELECT or anything that returns data.
func
(
c
*
Conn
)
Exec
(
cmd
string
,
args
...
interface
{})
os
.
Error
{
for
len
(
cmd
)
>
0
{
...
...
@@ -229,7 +249,7 @@ func (c *Conn) Prepare(cmd string, args ...interface{}) (*Stmt, os.Error) {
var
stmt
*
C
.
sqlite3_stmt
var
tail
*
C
.
char
rv
:=
C
.
sqlite3_prepare_v2
(
c
.
db
,
cmdstr
,
-
1
,
&
stmt
,
&
tail
)
if
rv
!=
0
{
if
rv
!=
C
.
SQLITE_OK
{
return
nil
,
c
.
error
(
rv
)
}
var
t
string
...
...
@@ -309,7 +329,7 @@ func (s *Stmt) Bind(args ...interface{}) os.Error {
}
rv
=
C
.
my_bind_blob
(
s
.
stmt
,
index
,
unsafe
.
Pointer
(
p
),
C
.
int
(
len
(
v
)))
}
if
rv
!=
0
{
if
rv
!=
C
.
SQLITE_OK
{
return
s
.
c
.
error
(
rv
)
}
}
...
...
@@ -330,7 +350,7 @@ func (s *Stmt) Next() (bool, os.Error) {
func
(
s
*
Stmt
)
Reset
()
os
.
Error
{
rv
:=
C
.
sqlite3_reset
(
s
.
stmt
)
if
rv
!=
0
{
if
rv
!=
C
.
SQLITE_OK
{
return
s
.
c
.
error
(
rv
)
}
return
nil
...
...
@@ -432,7 +452,7 @@ func (s *Stmt) scanField(index int, value interface{}) os.Error {
}
func
(
s
*
Stmt
)
Finalize
()
os
.
Error
{
rv
:=
C
.
sqlite3_finalize
(
s
.
stmt
)
if
rv
!=
0
{
if
rv
!=
C
.
SQLITE_OK
{
return
s
.
c
.
error
(
rv
)
}
s
.
stmt
=
nil
...
...
@@ -444,7 +464,7 @@ func (c *Conn) Close() os.Error {
return
os
.
NewError
(
"nil sqlite database"
)
}
rv
:=
C
.
sqlite3_close
(
c
.
db
)
if
rv
!=
0
{
if
rv
!=
C
.
SQLITE_OK
{
return
c
.
error
(
rv
)
}
c
.
db
=
nil
...
...
sqlite_test.go
View file @
7dc71ca7
...
...
@@ -38,6 +38,24 @@ func TestOpen(t *testing.T) {
db
.
Close
()
}
func
TestEnableFKey
(
t
*
testing
.
T
)
{
db
:=
open
(
t
)
defer
db
.
Close
()
b
,
err
:=
db
.
IsFKeyEnabled
()
if
err
!=
nil
{
t
.
Fatalf
(
"Error while checking if FK are enabled: %s"
,
err
)
}
if
!
b
{
b
,
err
=
db
.
EnableFKey
(
true
)
if
err
!=
nil
{
t
.
Fatalf
(
"Error while enabling FK: %s"
,
err
)
}
if
!
b
{
t
.
Error
(
"cannot enabled FK"
)
}
}
}
func
TestCreateTable
(
t
*
testing
.
T
)
{
db
:=
open
(
t
)
defer
db
.
Close
()
...
...
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