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
adbe176b
Commit
adbe176b
authored
Aug 27, 2011
by
gwenn
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adds simple Exists method like in TCL api.
parent
4eef7ca9
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
33 additions
and
4 deletions
+33
-4
sqlite.go
sqlite.go
+13
-4
sqlite_test.go
sqlite_test.go
+20
-0
No files found.
sqlite.go
View file @
adbe176b
...
...
@@ -175,7 +175,7 @@ func Open(filename string, flags ...OpenFlag) (*Conn, os.Error) {
var
db
*
C
.
sqlite3
name
:=
C
.
CString
(
filename
)
defer
C
.
free
(
unsafe
.
Pointer
(
name
))
rv
:=
C
.
sqlite3_open_v2
(
name
,
&
db
,
C
.
int
(
openFlags
),
nil
)
rv
:=
C
.
sqlite3_open_v2
(
name
,
&
db
,
C
.
int
(
openFlags
),
nil
)
if
rv
!=
C
.
SQLITE_OK
{
if
db
!=
nil
{
C
.
sqlite3_close
(
db
)
...
...
@@ -248,6 +248,15 @@ func (c *Conn) Exec(cmd string, args ...interface{}) os.Error {
return
nil
}
// Returns true if the specified query returns at least one row.
func
(
c
*
Conn
)
Exists
(
query
string
,
args
...
interface
{})
(
bool
,
os
.
Error
)
{
s
,
err
:=
c
.
Prepare
(
query
,
args
...
)
if
err
!=
nil
{
return
false
,
err
}
return
s
.
Next
()
}
// Calls http://sqlite.org/c3ref/changes.html
func
(
c
*
Conn
)
Changes
()
int
{
return
int
(
C
.
sqlite3_changes
(
c
.
db
))
...
...
@@ -300,7 +309,7 @@ func (c *Conn) Prepare(cmd string, args ...interface{}) (*Stmt, os.Error) {
}
s
:=
&
Stmt
{
c
:
c
,
stmt
:
stmt
,
tail
:
t
}
if
len
(
args
)
>
0
{
err
:=
s
.
Bind
(
args
)
err
:=
s
.
Bind
(
args
...
)
if
err
!=
nil
{
return
s
,
err
}
...
...
@@ -344,7 +353,7 @@ func (s *Stmt) Bind(args ...interface{}) os.Error {
}
n
:=
s
.
BindParameterCount
()
if
n
!=
len
(
args
)
{
//
TODO
What happens when the number of arguments is less than the number of parameters?
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
))
}
...
...
@@ -470,7 +479,7 @@ func (s *Stmt) NamedScan(args ...interface{}) os.Error {
// http://sqlite.org/c3ref/column_blob.html
func
(
s
*
Stmt
)
Scan
(
args
...
interface
{})
os
.
Error
{
n
:=
s
.
ColumnCount
()
if
n
!=
len
(
args
)
{
//
TODO
What happens when the number of arguments is less than the number of columns?
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
))
}
...
...
sqlite_test.go
View file @
adbe176b
...
...
@@ -76,6 +76,26 @@ func TestCreateTable(t *testing.T) {
createTable
(
db
,
t
)
}
func
TestExists
(
t
*
testing
.
T
)
{
db
:=
open
(
t
)
defer
db
.
Close
()
createTable
(
db
,
t
)
b
,
err
:=
db
.
Exists
(
"SELECT * FROM test"
)
if
err
!=
nil
{
t
.
Fatalf
(
"Error: %s"
,
err
)
}
if
b
{
t
.
Error
(
"No row expected"
)
}
b
,
err
=
db
.
Exists
(
"SELECT count(1) FROM test"
)
if
err
!=
nil
{
t
.
Fatalf
(
"Error: %s"
,
err
)
}
if
!
b
{
t
.
Error
(
"One row expected"
)
}
}
func
TestInsert
(
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