Commit adbe176b authored by gwenn's avatar gwenn

Adds simple Exists method like in TCL api.

parent 4eef7ca9
...@@ -175,7 +175,7 @@ func Open(filename string, flags ...OpenFlag) (*Conn, os.Error) { ...@@ -175,7 +175,7 @@ func Open(filename string, flags ...OpenFlag) (*Conn, os.Error) {
var db *C.sqlite3 var db *C.sqlite3
name := C.CString(filename) name := C.CString(filename)
defer C.free(unsafe.Pointer(name)) 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 rv != C.SQLITE_OK {
if db != nil { if db != nil {
C.sqlite3_close(db) C.sqlite3_close(db)
...@@ -248,6 +248,15 @@ func (c *Conn) Exec(cmd string, args ...interface{}) os.Error { ...@@ -248,6 +248,15 @@ func (c *Conn) Exec(cmd string, args ...interface{}) os.Error {
return nil 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 // Calls http://sqlite.org/c3ref/changes.html
func (c *Conn) Changes() int { func (c *Conn) Changes() int {
return int(C.sqlite3_changes(c.db)) return int(C.sqlite3_changes(c.db))
...@@ -300,7 +309,7 @@ func (c *Conn) Prepare(cmd string, args ...interface{}) (*Stmt, os.Error) { ...@@ -300,7 +309,7 @@ func (c *Conn) Prepare(cmd string, args ...interface{}) (*Stmt, os.Error) {
} }
s := &Stmt{c: c, stmt: stmt, tail: t} s := &Stmt{c: c, stmt: stmt, tail: t}
if len(args) > 0 { if len(args) > 0 {
err := s.Bind(args) err := s.Bind(args...)
if err != nil { if err != nil {
return s, err return s, err
} }
...@@ -344,7 +353,7 @@ func (s *Stmt) Bind(args ...interface{}) os.Error { ...@@ -344,7 +353,7 @@ func (s *Stmt) Bind(args ...interface{}) os.Error {
} }
n := s.BindParameterCount() 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)) 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 { ...@@ -470,7 +479,7 @@ func (s *Stmt) NamedScan(args ...interface{}) os.Error {
// http://sqlite.org/c3ref/column_blob.html // http://sqlite.org/c3ref/column_blob.html
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) { // 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)) return os.NewError(fmt.Sprintf("incorrect argument count for Stmt.Scan: have %d want %d", len(args), n))
} }
......
...@@ -76,6 +76,26 @@ func TestCreateTable(t *testing.T) { ...@@ -76,6 +76,26 @@ func TestCreateTable(t *testing.T) {
createTable(db, 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) { func TestInsert(t *testing.T) {
db := open(t) db := open(t)
defer db.Close() defer db.Close()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment