Commit 2b3a2cc7 authored by gwenn's avatar gwenn

Refactor usages of Next().

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