Commit 4b894bab authored by gwenn's avatar gwenn

Change Conn#OneValue method signature.

parent 0c9e9523
...@@ -22,7 +22,8 @@ func TestScalarFunction(t *testing.T) { ...@@ -22,7 +22,8 @@ func TestScalarFunction(t *testing.T) {
defer db.Close() defer db.Close()
err = db.CreateScalarFunction("half", 1, nil, half, nil) err = db.CreateScalarFunction("half", 1, nil, half, nil)
checkNoError(t, err, "couldn't create function: %s") checkNoError(t, err, "couldn't create function: %s")
d, err := db.OneValue("select half(6)") var d float64
err = db.OneValue("select half(6)", &d)
checkNoError(t, err, "couldn't retrieve result: %s") checkNoError(t, err, "couldn't retrieve result: %s")
if d != 3.0 { if d != 3.0 {
t.Errorf("Expected %f but got %f", 3.0, d) t.Errorf("Expected %f but got %f", 3.0, d)
...@@ -115,9 +116,10 @@ func TestSumFunction(t *testing.T) { ...@@ -115,9 +116,10 @@ func TestSumFunction(t *testing.T) {
defer db.Close() defer db.Close()
err = db.CreateAggregateFunction("mysum", 1, nil, sumStep, sumFinal, nil) err = db.CreateAggregateFunction("mysum", 1, nil, sumStep, sumFinal, nil)
checkNoError(t, err, "couldn't create function: %s") checkNoError(t, err, "couldn't create function: %s")
i, err := db.OneValue("select mysum(i) from (select 2 as i union all select 2)") var i int
err = db.OneValue("select mysum(i) from (select 2 as i union all select 2)", &i)
checkNoError(t, err, "couldn't execute statement: %s") checkNoError(t, err, "couldn't execute statement: %s")
if i != int64(4) { if i != 4 {
t.Errorf("Expected %d but got %v", 4, i) t.Errorf("Expected %d but got %v", 4, i)
} }
} }
......
...@@ -19,7 +19,8 @@ func (c *Conn) IntegrityCheck(max int, quick bool) error { ...@@ -19,7 +19,8 @@ func (c *Conn) IntegrityCheck(max int, quick bool) error {
} else { } else {
pragma = "integrity" pragma = "integrity"
} }
msg, err := c.OneValue(fmt.Sprintf("PRAGMA %s_check(%d)", pragma, max)) var msg string
err := c.OneValue(fmt.Sprintf("PRAGMA %s_check(%d)", pragma, max), &msg)
if err != nil { if err != nil {
return err return err
} }
...@@ -33,25 +34,21 @@ func (c *Conn) IntegrityCheck(max int, quick bool) error { ...@@ -33,25 +34,21 @@ func (c *Conn) IntegrityCheck(max int, quick bool) error {
// (See http://sqlite.org/pragma.html#pragma_encoding) // (See http://sqlite.org/pragma.html#pragma_encoding)
// TODO Make possible to specify the database-name (PRAGMA %Q.encoding) // TODO Make possible to specify the database-name (PRAGMA %Q.encoding)
func (c *Conn) Encoding() (string, error) { func (c *Conn) Encoding() (string, error) {
value, err := c.OneValue("PRAGMA encoding") var encoding string
err := c.OneValue("PRAGMA encoding", &encoding)
if err != nil { if err != nil {
return "", err return "", err
} }
if encoding, ok := value.(string); ok {
return encoding, nil return encoding, nil
}
return "", c.specificError("Unexpected encoding (%v)", value)
} }
// (See http://sqlite.org/pragma.html#pragma_schema_version) // (See http://sqlite.org/pragma.html#pragma_schema_version)
// TODO Make possible to specify the database-name (PRAGMA %Q.schema_version) // TODO Make possible to specify the database-name (PRAGMA %Q.schema_version)
func (c *Conn) SchemaVersion() (int64, error) { func (c *Conn) SchemaVersion() (int, error) {
value, err := c.OneValue("PRAGMA schema_version") var version int
err := c.OneValue("PRAGMA schema_version", &version)
if err != nil { if err != nil {
return -1, err return -1, err
} }
if version, ok := value.(int64); ok {
return version, nil return version, nil
}
return -1, c.specificError("Unexpected version (%v)", value)
} }
package sqlite_test package sqlite_test
import ( import (
// . "github.com/gwenn/gosqlite"
"testing" "testing"
) )
......
...@@ -378,19 +378,19 @@ func (c *Conn) Exists(query string, args ...interface{}) (bool, error) { ...@@ -378,19 +378,19 @@ func (c *Conn) Exists(query string, args ...interface{}) (bool, error) {
// Use it with SELECT that returns only one row with only one column. // Use it with SELECT that returns only one row with only one column.
// Returns io.EOF when there is no row. // Returns io.EOF when there is no row.
func (c *Conn) OneValue(query string, args ...interface{}) (interface{}, error) { func (c *Conn) OneValue(query string, value interface{}, args ...interface{}) error {
s, err := c.Prepare(query, args...) s, err := c.Prepare(query, args...)
if err != nil { if err != nil {
return nil, err return err
} }
defer s.Finalize() defer s.Finalize()
b, err := s.Next() b, err := s.Next()
if err != nil { if err != nil {
return nil, err return err
} else if !b { } else if !b {
return nil, io.EOF return io.EOF
} }
return s.ScanValue(0), nil return s.Scan(value)
} }
// Count the number of rows modified // Count the number of rows modified
......
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