Commit 1624e5b0 authored by gwenn's avatar gwenn

Introduce ScanValues.

parent 4e29d083
......@@ -5,20 +5,42 @@ import (
"testing"
)
func BenchmarkScan(b *testing.B) {
b.StopTimer()
db, _ := Open("")
defer db.Close()
func fill(db *Conn, n int) {
db.Exec("DROP TABLE IF EXISTS test")
db.Exec("CREATE TABLE test (id INTEGER PRIMARY KEY AUTOINCREMENT, float_num REAL, int_num INTEGER, a_string TEXT)")
db.Begin()
s, _ := db.Prepare("INSERT INTO test (float_num, int_num, a_string) VALUES (?, ?, ?)")
for i := 0; i < 1000; i++ {
db.Begin()
for i := 0; i < n; i++ {
s.Exec(float64(i)*float64(3.14), i, "hello")
}
s.Finalize()
db.Commit()
}
func BenchmarkValuesScan(b *testing.B) {
b.StopTimer()
db, _ := Open("")
defer db.Close()
fill(db, 1000)
b.StartTimer()
for i := 0; i < b.N; i++ {
cs, _ := db.Prepare("SELECT float_num, int_num, a_string FROM test")
values := make([]interface{}, 3)
for Must(cs.Next()) {
cs.ScanValues(values)
}
cs.Finalize()
}
}
func BenchmarkScan(b *testing.B) {
b.StopTimer()
db, _ := Open("")
defer db.Close()
fill(db, 1000)
b.StartTimer()
for i := 0; i < b.N; i++ {
......@@ -39,16 +61,7 @@ func BenchmarkNamedScan(b *testing.B) {
b.StopTimer()
db, _ := Open("")
defer db.Close()
db.Exec("DROP TABLE IF EXISTS test")
db.Exec("CREATE TABLE test (id INTEGER PRIMARY KEY AUTOINCREMENT, float_num REAL, int_num INTEGER, a_string TEXT)")
db.Begin()
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.Finalize()
db.Commit()
fill(db, 1000)
b.StartTimer()
for i := 0; i < b.N; i++ {
......@@ -66,18 +79,25 @@ func BenchmarkNamedScan(b *testing.B) {
}
func BenchmarkInsert(b *testing.B) {
db, _ := Open("")
defer db.Close()
fill(db, b.N)
}
func BenchmarkNamedInsert(b *testing.B) {
db, _ := Open("")
defer db.Close()
db.Exec("DROP TABLE IF EXISTS 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 (?, ?, ?)")
" VALUES (:f, :i, :s)")
defer s.Finalize()
db.Begin()
for i := 0; i < b.N; i++ {
s.Exec(float64(i)*float64(3.14), i, "hello")
s.NamedBind("f", float64(i)*float64(3.14), "i", i, "s", "hello")
s.Next()
}
db.Commit()
}
......@@ -121,8 +121,6 @@ func (s *stmtImpl) Next(dest []interface{}) error {
if !ok {
return io.EOF
}
for i := range dest {
dest[i] = s.s.ScanValue(i)
}
s.s.ScanValues(dest)
return nil
}
......@@ -477,7 +477,7 @@ func (s *Stmt) BindParameterCount() int {
return int(C.sqlite3_bind_parameter_count(s.stmt))
}
// Index of a parameter with a given name
// Index of a parameter with a given name (cached)
// Calls http://sqlite.org/c3ref/bind_parameter_index.html
func (s *Stmt) BindParameterIndex(name string) (int, error) {
if s.params == nil {
......@@ -511,7 +511,7 @@ func (s *Stmt) BindParameterName(i int) (string, error) {
// Bind parameters by their name (name1, value1, ...)
func (s *Stmt) NamedBind(args ...interface{}) error {
err := s.Reset() // TODO sqlite3_clear_bindings?
err := s.Reset()
if err != nil {
return err
}
......@@ -539,7 +539,7 @@ func (s *Stmt) NamedBind(args ...interface{}) error {
// 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{}) error {
err := s.Reset() // TODO sqlite3_clear_bindings?
err := s.Reset()
if err != nil {
return err
}
......@@ -890,6 +890,13 @@ func (s *Stmt) ScanValue(index int) (value interface{}) {
return
}
// Like ScanValue on several columns
func (s *Stmt) ScanValues(values []interface{}) {
for i := 0; i < len(values); i++ {
values[i] = s.ScanValue(i)
}
}
// The leftmost column/index is number 0.
// Returns true when column is null.
// Calls sqlite3_column_text.
......
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