Commit 2b3a2cc7 authored by gwenn's avatar gwenn

Refactor usages of Next().

parent 6c1f9b97
...@@ -52,6 +52,7 @@ type ZeroBlobLength int ...@@ -52,6 +52,7 @@ type ZeroBlobLength int
// // check err // // check err
// //
// Calls http://sqlite.org/c3ref/blob_open.html // Calls http://sqlite.org/c3ref/blob_open.html
// TODO A real 'incremental' example...
func (c *Conn) NewBlobReader(db, table, column string, row int64) (*BlobReader, error) { func (c *Conn) NewBlobReader(db, table, column string, row int64) (*BlobReader, error) {
bl, err := c.blob_open(db, table, column, row, false) bl, err := c.blob_open(db, table, column, row, false)
if err != nil { if err != nil {
......
...@@ -27,18 +27,19 @@ func (c *Conn) Databases() (map[string]string, error) { ...@@ -27,18 +27,19 @@ func (c *Conn) Databases() (map[string]string, error) {
} }
defer s.Finalize() defer s.Finalize()
var databases map[string]string = make(map[string]string) var databases map[string]string = make(map[string]string)
var ok bool
var name, file string var name, file string
for ok, err = s.Next(); ok; ok, err = s.Next() { for {
if ok, err := s.Next(); err != nil {
return nil, err
} else if !ok {
break
}
err = s.Scan(nil, &name, &file) err = s.Scan(nil, &name, &file)
if err != nil { if err != nil {
return nil, err return nil, err
} }
databases[name] = file databases[name] = file
} }
if err != nil {
return nil, err
}
return databases, nil return databases, nil
} }
...@@ -50,15 +51,19 @@ func (c *Conn) Tables() ([]string, error) { ...@@ -50,15 +51,19 @@ func (c *Conn) Tables() ([]string, error) {
} }
defer s.Finalize() defer s.Finalize()
var tables []string = make([]string, 0, 20) var tables []string = make([]string, 0, 20)
var ok bool
var name string var name string
for ok, err = s.Next(); ok; ok, err = s.Next() { for {
s.Scan(&name) if ok, err := s.Next(); err != nil {
tables = append(tables, name) return nil, err
} else if !ok {
break
} }
err = s.Scan(&name)
if err != nil { if err != nil {
return nil, err return nil, err
} }
tables = append(tables, name)
}
return tables, nil return tables, nil
} }
...@@ -82,8 +87,12 @@ func (c *Conn) Columns(table string) ([]Column, error) { ...@@ -82,8 +87,12 @@ func (c *Conn) Columns(table string) ([]Column, error) {
} }
defer s.Finalize() defer s.Finalize()
var columns []Column = make([]Column, 0, 20) var columns []Column = make([]Column, 0, 20)
var ok bool for {
for ok, err = s.Next(); ok; ok, err = s.Next() { if ok, err := s.Next(); err != nil {
return nil, err
} else if !ok {
break
}
c := Column{} c := Column{}
err = s.Scan(&c.Cid, &c.Name, &c.DataType, &c.NotNull, &c.DfltValue, &c.Pk) err = s.Scan(&c.Cid, &c.Name, &c.DataType, &c.NotNull, &c.DfltValue, &c.Pk)
if err != nil { if err != nil {
...@@ -91,9 +100,6 @@ func (c *Conn) Columns(table string) ([]Column, error) { ...@@ -91,9 +100,6 @@ func (c *Conn) Columns(table string) ([]Column, error) {
} }
columns = append(columns, c) columns = append(columns, c)
} }
if err != nil {
return nil, err
}
return columns, nil return columns, nil
} }
...@@ -113,10 +119,14 @@ func (c *Conn) ForeignKeys(table string) (map[int]*ForeignKey, error) { ...@@ -113,10 +119,14 @@ func (c *Conn) ForeignKeys(table string) (map[int]*ForeignKey, error) {
} }
defer s.Finalize() defer s.Finalize()
var fks = make(map[int]*ForeignKey) var fks = make(map[int]*ForeignKey)
var ok bool
var id, seq int var id, seq int
var ref, from, to string var ref, from, to string
for ok, err = s.Next(); ok; ok, err = s.Next() { for {
if ok, err := s.Next(); err != nil {
return nil, err
} else if !ok {
break
}
err = s.NamedScan("id", &id, "seq", &seq, "table", &ref, "from", &from, "to", &to) err = s.NamedScan("id", &id, "seq", &seq, "table", &ref, "from", &from, "to", &to)
if err != nil { if err != nil {
return nil, err return nil, err
...@@ -130,9 +140,6 @@ func (c *Conn) ForeignKeys(table string) (map[int]*ForeignKey, error) { ...@@ -130,9 +140,6 @@ func (c *Conn) ForeignKeys(table string) (map[int]*ForeignKey, error) {
fk.From = append(fk.From, from) fk.From = append(fk.From, from)
fk.To = append(fk.To, to) fk.To = append(fk.To, to)
} }
if err != nil {
return nil, err
}
return fks, nil return fks, nil
} }
...@@ -151,8 +158,12 @@ func (c *Conn) Indexes(table string) ([]Index, error) { ...@@ -151,8 +158,12 @@ func (c *Conn) Indexes(table string) ([]Index, error) {
} }
defer s.Finalize() defer s.Finalize()
var indexes []Index = make([]Index, 0, 5) var indexes []Index = make([]Index, 0, 5)
var ok bool for {
for ok, err = s.Next(); ok; ok, err = s.Next() { if ok, err := s.Next(); err != nil {
return nil, err
} else if !ok {
break
}
i := Index{} i := Index{}
err = s.Scan(nil, &i.Name, &i.Unique) err = s.Scan(nil, &i.Name, &i.Unique)
if err != nil { if err != nil {
...@@ -160,9 +171,6 @@ func (c *Conn) Indexes(table string) ([]Index, error) { ...@@ -160,9 +171,6 @@ func (c *Conn) Indexes(table string) ([]Index, error) {
} }
indexes = append(indexes, i) indexes = append(indexes, i)
} }
if err != nil {
return nil, err
}
return indexes, nil return indexes, nil
} }
...@@ -175,8 +183,12 @@ func (c *Conn) IndexColumns(index string) ([]Column, error) { ...@@ -175,8 +183,12 @@ func (c *Conn) IndexColumns(index string) ([]Column, error) {
} }
defer s.Finalize() defer s.Finalize()
var columns []Column = make([]Column, 0, 5) var columns []Column = make([]Column, 0, 5)
var ok bool for {
for ok, err = s.Next(); ok; ok, err = s.Next() { if ok, err := s.Next(); err != nil {
return nil, err
} else if !ok {
break
}
c := Column{} c := Column{}
err = s.Scan(nil, &c.Cid, &c.Name) err = s.Scan(nil, &c.Cid, &c.Name)
if err != nil { if err != nil {
...@@ -184,9 +196,6 @@ func (c *Conn) IndexColumns(index string) ([]Column, error) { ...@@ -184,9 +196,6 @@ func (c *Conn) IndexColumns(index string) ([]Column, error) {
} }
columns = append(columns, c) columns = append(columns, c)
} }
if err != nil {
return nil, err
}
return columns, nil return columns, nil
} }
......
...@@ -600,14 +600,15 @@ func (s *Stmt) BindByIndex(index int, value interface{}) error { ...@@ -600,14 +600,15 @@ func (s *Stmt) BindByIndex(index int, value interface{}) error {
} }
// Evaluate an SQL statement // Evaluate an SQL statement
//
// With custom error handling: // With custom error handling:
// var ok bool // for {
// var err os.Error // if ok, err := s.Next(); err != nil {
// for ok, err = s.Next(); ok; ok, err = s.Next() { // return nil, err
// err = s.Scan(&fnum, &inum, &sstr) // } else if !ok {
// break
// } // }
// if err != nil { // err = s.Scan(&fnum, &inum, &sstr)
// ...
// } // }
// With panic on error: // With panic on error:
// for Must(s.Next()) { // for Must(s.Next()) {
...@@ -712,6 +713,7 @@ func (s *Stmt) ColumnType(index int) Type { ...@@ -712,6 +713,7 @@ func (s *Stmt) ColumnType(index int) Type {
// Scan result values from a query by name (name1, value1, ...) // Scan result values from a query by name (name1, value1, ...)
// Example: // Example:
// stmt, err := db.Prepare("SELECT 1 as id, 'test' as name") // stmt, err := db.Prepare("SELECT 1 as id, 'test' as name")
// // TODO error handling
// defer stmt.Finalize() // defer stmt.Finalize()
// var id int // var id int
// var name string // var name string
...@@ -749,6 +751,7 @@ func (s *Stmt) NamedScan(args ...interface{}) error { ...@@ -749,6 +751,7 @@ func (s *Stmt) NamedScan(args ...interface{}) error {
// Scan result values from a query // Scan result values from a query
// Example: // Example:
// stmt, err := db.Prepare("SELECT 1, 'test'") // stmt, err := db.Prepare("SELECT 1, 'test'")
// // TODO error handling
// defer stmt.Finalize() // defer stmt.Finalize()
// var id int // var id int
// var name string // var name string
...@@ -1093,7 +1096,7 @@ func (s *Stmt) ReadOnly() bool { ...@@ -1093,7 +1096,7 @@ func (s *Stmt) ReadOnly() bool {
func (c *Conn) EnableLoadExtension(b bool) { func (c *Conn) EnableLoadExtension(b bool) {
C.sqlite3_enable_load_extension(c.db, btocint(b)) C.sqlite3_enable_load_extension(c.db, btocint(b))
} }
// Load an xxtension // Load an extension
// Calls http://sqlite.org/c3ref/load_extension.html // Calls http://sqlite.org/c3ref/load_extension.html
func (c *Conn) LoadExtension(file string, proc ...string) error { func (c *Conn) LoadExtension(file string, proc ...string) error {
cfile := C.CString(file) cfile := C.CString(file)
...@@ -1133,11 +1136,9 @@ func (c *Conn) IntegrityCheck(max int, quick bool) error { ...@@ -1133,11 +1136,9 @@ func (c *Conn) IntegrityCheck(max int, quick bool) error {
return err return err
} }
defer s.Finalize() defer s.Finalize()
ok, err := s.Next() if ok, err := s.Next(); err != nil {
if err != nil {
return err return err
} } else if !ok {
if !ok {
return errors.New("Integrity check failed (no result)") return errors.New("Integrity check failed (no result)")
} }
msg, null := s.ScanText(0) msg, null := s.ScanText(0)
......
...@@ -130,7 +130,7 @@ func TestInsert(t *testing.T) { ...@@ -130,7 +130,7 @@ func TestInsert(t *testing.T) {
t.Errorf("column count error: %d <> 1", columnCount) t.Errorf("column count error: %d <> 1", columnCount)
} }
if ok := Must(cs.Next()); !ok { if !Must(cs.Next()) {
t.Fatal("no result for count") t.Fatal("no result for count")
} }
var i int var i int
...@@ -193,7 +193,7 @@ func TestInsertWithStatement(t *testing.T) { ...@@ -193,7 +193,7 @@ func TestInsertWithStatement(t *testing.T) {
if !cs.ReadOnly() { if !cs.ReadOnly() {
t.Errorf("update statement is not readonly") t.Errorf("update statement is not readonly")
} }
if ok := Must(cs.Next()); !ok { if !Must(cs.Next()) {
t.Fatal("no result for count") t.Fatal("no result for count")
} }
var i int var i int
...@@ -216,7 +216,7 @@ func TestInsertWithStatement(t *testing.T) { ...@@ -216,7 +216,7 @@ func TestInsertWithStatement(t *testing.T) {
t.Errorf("column name error: %s <> 'int_num'", secondColumnName) t.Errorf("column name error: %s <> 'int_num'", secondColumnName)
} }
if ok := Must(rs.Next()); ok { if Must(rs.Next()) {
var fnum float64 var fnum float64
var inum int64 var inum int64
var sstr string var sstr string
...@@ -231,7 +231,7 @@ func TestInsertWithStatement(t *testing.T) { ...@@ -231,7 +231,7 @@ func TestInsertWithStatement(t *testing.T) {
t.Errorf("Expected 'hello' <> %s\n", sstr) t.Errorf("Expected 'hello' <> %s\n", sstr)
} }
} }
if ok := Must(rs.Next()); ok { if Must(rs.Next()) {
var fnum float64 var fnum float64
var inum int64 var inum int64
var sstr string var sstr string
...@@ -320,7 +320,7 @@ func TestScanColumn(t *testing.T) { ...@@ -320,7 +320,7 @@ func TestScanColumn(t *testing.T) {
t.Fatalf("prepare error: %s", err) t.Fatalf("prepare error: %s", err)
} }
defer s.Finalize() defer s.Finalize()
if ok := Must(s.Next()); !ok { if !Must(s.Next()) {
t.Fatal("no result") t.Fatal("no result")
} }
var i1, i2, i3 int var i1, i2, i3 int
...@@ -353,7 +353,7 @@ func TestNamedScanColumn(t *testing.T) { ...@@ -353,7 +353,7 @@ func TestNamedScanColumn(t *testing.T) {
t.Fatalf("prepare error: %s", err) t.Fatalf("prepare error: %s", err)
} }
defer s.Finalize() defer s.Finalize()
if ok := Must(s.Next()); !ok { if !Must(s.Next()) {
t.Fatal("no result") t.Fatal("no result")
} }
var i1, i2, i3 int var i1, i2, i3 int
......
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