Commit 6ed6151b authored by Patrick Crosby's avatar Patrick Crosby

testing file

parent 1be09629
...@@ -9,5 +9,6 @@ _obj ...@@ -9,5 +9,6 @@ _obj
_test _test
cgo_* cgo_*
_cgo_* _cgo_*
_testmain.go
...@@ -234,6 +234,68 @@ func (s *Stmt) Exec(args ...interface{}) os.Error { ...@@ -234,6 +234,68 @@ func (s *Stmt) Exec(args ...interface{}) os.Error {
return nil return nil
} }
func (s *Stmt) Exec2(args ...interface{}) os.Error {
s.args = fmt.Sprintf(" %v", []interface{}(args))
rv := C.sqlite3_reset(s.stmt)
if rv != 0 {
return s.c.error(rv)
}
n := int(C.sqlite3_bind_parameter_count(s.stmt))
if n != len(args) {
return os.NewError(fmt.Sprintf("incorrect argument count for Stmt.Exec: have %d want %d", len(args), n))
}
for i, v := range args {
var str string
switch v := v.(type) {
case int:
if rv := C.sqlite3_bind_int(s.stmt, C.int(i+1), C.int(v)); rv != 0 {
return s.c.error(rv)
}
continue
case int64:
if rv := C.sqlite3_bind_int64(s.stmt, C.int(i+1), C.sqlite3_int64(v)); rv != 0 {
return s.c.error(rv)
}
continue
case float64:
if rv := C.sqlite3_bind_double(s.stmt, C.int(i+1), C.double(v)); rv != 0 {
return s.c.error(rv)
}
continue
case []byte:
var p *byte
if len(v) > 0 {
p = &v[0]
}
if rv := C.my_bind_blob(s.stmt, C.int(i+1), unsafe.Pointer(p), C.int(len(v))); rv != 0 {
return s.c.error(rv)
}
continue
case bool:
if v {
str = "1"
} else {
str = "0"
}
default:
str = fmt.Sprint(v)
}
cstr := C.CString(str)
rv := C.my_bind_text(s.stmt, C.int(i+1), cstr, C.int(len(str)))
C.free(unsafe.Pointer(cstr))
if rv != 0 {
return s.c.error(rv)
}
}
return nil
}
func (s *Stmt) Error() os.Error { func (s *Stmt) Error() os.Error {
return s.err return s.err
} }
...@@ -305,32 +367,32 @@ func (s *Stmt) Scan2(args ...interface{}) os.Error { ...@@ -305,32 +367,32 @@ func (s *Stmt) Scan2(args ...interface{}) os.Error {
} }
for i, v := range args { for i, v := range args {
/* /*
n := C.sqlite3_column_bytes(s.stmt, C.int(i)) n := C.sqlite3_column_bytes(s.stmt, C.int(i))
p := C.sqlite3_column_blob(s.stmt, C.int(i)) p := C.sqlite3_column_blob(s.stmt, C.int(i))
if p == nil && n > 0 { if p == nil && n > 0 {
return os.NewError("got nil blob") return os.NewError("got nil blob")
}
var data []byte
if n > 0 {
data = (*[1 << 30]byte)(unsafe.Pointer(p))[0:n]
}
*/
switch v := v.(type) {
/*
case *[]byte:
*v = data
case *string:
*v = string(data)
case *bool:
*v = string(data) == "1"
case *int:
x, err := strconv.Atoi(string(data))
if err != nil {
return os.NewError("arg " + strconv.Itoa(i) + " as int: " + err.String())
} }
*v = x var data []byte
*/ if n > 0 {
data = (*[1 << 30]byte)(unsafe.Pointer(p))[0:n]
}
*/
switch v := v.(type) {
/*
case *[]byte:
*v = data
case *string:
*v = string(data)
case *bool:
*v = string(data) == "1"
case *int:
x, err := strconv.Atoi(string(data))
if err != nil {
return os.NewError("arg " + strconv.Itoa(i) + " as int: " + err.String())
}
*v = x
*/
case *int64: case *int64:
x := C.sqlite3_column_int64(s.stmt, C.int(i)) x := C.sqlite3_column_int64(s.stmt, C.int(i))
*v = int64(x) *v = int64(x)
......
package fsqlite
import (
"testing"
"fmt"
)
func TestOpen(t *testing.T) {
db, err := Open("/tmp/test.db")
if err != nil {
t.Errorf("couldn't open database file: %s", err)
}
if db == nil {
t.Error("opened database is nil")
}
db.Close()
}
func TestCreateTable(t *testing.T) {
db, err := Open("/tmp/test.db")
db.Exec("DROP TABLE test")
err = db.Exec("CREATE TABLE test (id INTEGER PRIMARY KEY AUTOINCREMENT, float_num REAL, int_num INTEGER, a_string TEXT)")
if err != nil {
t.Errorf("error creating table: %s", err)
}
}
type OutRow struct {
Key int64
FloatNum float64
IntNum int64
AString string
}
func TestInsert(t *testing.T) {
db, _ := Open("/tmp/test.db")
db.Exec("DROP TABLE test")
db.Exec("CREATE TABLE test (id INTEGER PRIMARY KEY AUTOINCREMENT, float_num REAL, int_num INTEGER, a_string TEXT)")
for i := 0; i < 1000; i++ {
ierr := db.Exec("INSERT INTO test (float_num, int_num, a_string) VALUES (?, ?, ?)", float64(i)*float64(3.14), i, "hello")
if ierr != nil {
t.Errorf("insert error: %s", ierr)
}
}
cs, _ := db.Prepare("SELECT COUNT(*) FROM test")
cs.Exec()
if !cs.Next() {
t.Error("no result for count")
}
var i int
err := cs.Scan(&i)
if err != nil {
t.Errorf("error scanning count: %s", err)
}
if i != 1000 {
t.Errorf("count should be 1000, but it is %d", i)
}
}
func TestInsertWithStatement(t *testing.T) {
db, _ := Open("/tmp/test_is.db")
db.Exec("DROP TABLE test")
db.Exec("CREATE TABLE test (id INTEGER PRIMARY KEY AUTOINCREMENT, float_num REAL, int_num INTEGER, a_string TEXT)")
s, serr := db.Prepare("INSERT INTO test (float_num, int_num, a_string) VALUES (?, ?, ?)")
if serr != nil {
t.Errorf("prepare error: %s", serr)
}
if s == nil {
t.Error("statement is nil")
}
for i := 0; i < 1000; i++ {
ierr := s.Exec(float64(i)*float64(3.14), i, "hello")
if ierr != nil {
t.Errorf("insert error: %s", ierr)
}
s.Next()
}
s.Finalize()
cs, _ := db.Prepare("SELECT COUNT(*) FROM test")
cs.Exec()
if !cs.Next() {
t.Error("no result for count")
}
var i int
err := cs.Scan(&i)
if err != nil {
t.Errorf("error scanning count: %s", err)
}
if i != 1000 {
t.Errorf("count should be 1000, but it is %d", i)
}
}
func TestInsertWithStatement2(t *testing.T) {
db, _ := Open("/tmp/test_is2.db")
db.Exec("DROP TABLE test")
db.Exec("CREATE TABLE test (id INTEGER PRIMARY KEY AUTOINCREMENT, float_num REAL, int_num INTEGER, a_string TEXT)")
s, serr := db.Prepare("INSERT INTO test (float_num, int_num, a_string) VALUES (?, ?, ?)")
if serr != nil {
t.Errorf("prepare error: %s", serr)
}
if s == nil {
t.Error("statement is nil")
}
for i := 0; i < 1000; i++ {
ierr := s.Exec2(float64(i)*float64(3.14), i, "hello")
if ierr != nil {
t.Errorf("insert error: %s", ierr)
}
s.Next()
}
s.Finalize()
cs, _ := db.Prepare("SELECT COUNT(*) FROM test")
cs.Exec()
if !cs.Next() {
t.Error("no result for count")
}
var i int
err := cs.Scan(&i)
if err != nil {
t.Errorf("error scanning count: %s", err)
}
if i != 1000 {
t.Errorf("count should be 1000, but it is %d", i)
}
rs, _ := db.Prepare("SELECT float_num, int_num, a_string FROM test ORDER BY int_num LIMIT 10")
var fnum float64
var inum int64
var sstr string
for rs.Next() {
rs.Scan(&fnum, &inum, &sstr)
fmt.Printf("fnum = %f, inum = %d, sstre = %s\n", fnum, inum, sstr)
}
}
func BenchmarkScan(b *testing.B) {
b.StopTimer()
db, _ := Open("/tmp/test_bs.db")
db.Exec("DROP TABLE test")
db.Exec("CREATE TABLE test (id INTEGER PRIMARY KEY AUTOINCREMENT, float_num REAL, int_num INTEGER, a_string TEXT)")
s, _ := db.Prepare("INSERT INTO test (float_num, int_num, a_string) VALUES (?, ?, ?)")
for i := 0; i < 1000; i++ {
s.Exec(float64(i)*float64(3.14), i, "hello")
s.Next()
}
s.Finalize()
b.StartTimer()
for i := 0; i < b.N; i++ {
cs, _ := db.Prepare("SELECT float_num, int_num, a_string FROM test")
cs.Exec()
var fnum float64
var inum int64
var sstr string
for cs.Next() {
cs.Scan(&fnum, &inum, &sstr)
}
}
}
func BenchmarkScan2(b *testing.B) {
b.StopTimer()
db, _ := Open("/tmp/test_bs2.db")
db.Exec("DROP TABLE test")
db.Exec("CREATE TABLE test (id INTEGER PRIMARY KEY AUTOINCREMENT, float_num REAL, int_num INTEGER, a_string TEXT)")
s, _ := db.Prepare("INSERT INTO test (float_num, int_num, a_string) VALUES (?, ?, ?)")
for i := 0; i < 1000; i++ {
s.Exec(float64(i)*float64(3.14), i, "hello")
s.Next()
}
s.Finalize()
b.StartTimer()
for i := 0; i < b.N; i++ {
cs, _ := db.Prepare("SELECT float_num, int_num, a_string FROM test")
cs.Exec()
var fnum float64
var inum int64
var sstr string
for cs.Next() {
cs.Scan2(&fnum, &inum, &sstr)
}
}
}
func BenchmarkInsert(b *testing.B) {
db, _ := Open("/tmp/test_bi.db")
db.Exec("DROP TABLE test")
db.Exec("CREATE TABLE test (id INTEGER PRIMARY KEY AUTOINCREMENT, float_num REAL, int_num INTEGER, a_string TEXT)")
s, _ := db.Prepare("INSERT INTO test (float_num, int_num, a_string) VALUES (?, ?, ?)")
// for x := 0; x < b.N; x++ {
for i := 0; i < b.N; i++ {
s.Exec(float64(i)*float64(3.14), i, "hello")
s.Next()
}
// }
s.Finalize()
}
func BenchmarkInsert2(b *testing.B) {
db, _ := Open("/tmp/test_bi2.db")
db.Exec("DROP TABLE test")
db.Exec("CREATE TABLE test (id INTEGER PRIMARY KEY AUTOINCREMENT, float_num REAL, int_num INTEGER, a_string TEXT)")
s, _ := db.Prepare("INSERT INTO test (float_num, int_num, a_string) VALUES (?, ?, ?)")
// for x := 0; x < b.N; x++ {
for i := 0; i < b.N; i++ {
s.Exec2(float64(i)*float64(3.14), i, "hello")
s.Next()
}
// }
s.Finalize()
}
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