Commit 900f66f2 authored by gwenn's avatar gwenn

Panic if column index is out of range.

parent 4d9000a7
...@@ -331,9 +331,8 @@ func (s *Stmt) ExportToCSV(nullvalue string, headers bool, w *yacr.Writer) error ...@@ -331,9 +331,8 @@ func (s *Stmt) ExportToCSV(nullvalue string, headers bool, w *yacr.Writer) error
} }
s.Select(func(s *Stmt) error { s.Select(func(s *Stmt) error {
for i := 0; i < s.ColumnCount(); i++ { for i := 0; i < s.ColumnCount(); i++ {
if rb, null, err := s.ScanRawBytes(i); err != nil { rb, null := s.ScanRawBytes(i)
return err if null {
} else if null {
w.Write([]byte(nullvalue)) w.Write([]byte(nullvalue))
} else { } else {
w.Write(rb) w.Write(rb)
......
...@@ -206,20 +206,15 @@ func TestImportAffinity(t *testing.T) { ...@@ -206,20 +206,15 @@ func TestImportAffinity(t *testing.T) {
err := db.ImportCSV(r, ic, "", "test") err := db.ImportCSV(r, ic, "", "test")
checkNoError(t, err, "error while importing CSV file: %s") checkNoError(t, err, "error while importing CSV file: %s")
err = db.Select("SELECT typeof(t), typeof(i), typeof(r), typeof(b), typeof(n) from test", func(s *Stmt) error { err = db.Select("SELECT typeof(t), typeof(i), typeof(r), typeof(b), typeof(n) from test", func(s *Stmt) error {
tot, _, err := s.ScanText(0) tot, _ := s.ScanText(0)
checkNoError(t, err, "error while scanning: %s")
assert.Equal(t, "text", tot) assert.Equal(t, "text", tot)
toi, _, err := s.ScanText(1) toi, _ := s.ScanText(1)
checkNoError(t, err, "error while scanning: %s")
assert.Equal(t, "integer", toi) assert.Equal(t, "integer", toi)
tor, _, err := s.ScanText(2) tor, _ := s.ScanText(2)
checkNoError(t, err, "error while scanning: %s")
assert.Equal(t, "real", tor) assert.Equal(t, "real", tor)
tob, _, err := s.ScanText(3) tob, _ := s.ScanText(3)
checkNoError(t, err, "error while scanning: %s")
assert.Equal(t, "text", tob) assert.Equal(t, "text", tob)
ton, _, err := s.ScanText(4) ton, _ := s.ScanText(4)
checkNoError(t, err, "error while scanning: %s")
assert.Equal(t, "integer", ton) assert.Equal(t, "integer", ton)
return err return err
}) })
......
...@@ -229,9 +229,7 @@ func (r *rowsImpl) Next(dest []driver.Value) error { ...@@ -229,9 +229,7 @@ func (r *rowsImpl) Next(dest []driver.Value) error {
return io.EOF return io.EOF
} }
for i := range dest { for i := range dest {
if dest[i], _, err = r.s.s.ScanValue(i, true); err != nil { dest[i], _ = r.s.s.ScanValue(i, true)
return err
}
/*if !driver.IsScanValue(dest[i]) { /*if !driver.IsScanValue(dest[i]) {
panic("Invalid type returned by ScanValue") panic("Invalid type returned by ScanValue")
}*/ }*/
......
...@@ -55,13 +55,10 @@ func (c *Conn) Tables(dbName string) ([]string, error) { ...@@ -55,13 +55,10 @@ func (c *Conn) Tables(dbName string) ([]string, error) {
} }
defer s.finalize() defer s.finalize()
var tables = make([]string, 0, 20) var tables = make([]string, 0, 20)
err = s.Select(func(s *Stmt) (err error) { err = s.Select(func(s *Stmt) error {
if name, _, err := s.ScanText(0); err != nil { name, _ := s.ScanText(0)
return err
} else {
tables = append(tables, name) tables = append(tables, name)
} return nil
return
}) })
if err != nil { if err != nil {
return nil, err return nil, err
...@@ -86,13 +83,10 @@ func (c *Conn) Views(dbName string) ([]string, error) { ...@@ -86,13 +83,10 @@ func (c *Conn) Views(dbName string) ([]string, error) {
} }
defer s.finalize() defer s.finalize()
var views = make([]string, 0, 20) var views = make([]string, 0, 20)
err = s.Select(func(s *Stmt) (err error) { err = s.Select(func(s *Stmt) error {
if name, _, err := s.ScanText(0); err != nil { name, _ := s.ScanText(0)
return err
} else {
views = append(views, name) views = append(views, name)
} return nil
return
}) })
if err != nil { if err != nil {
return nil, err return nil, err
...@@ -178,11 +172,11 @@ func (c *Conn) Columns(dbName, table string) ([]Column, error) { ...@@ -178,11 +172,11 @@ func (c *Conn) Columns(dbName, table string) ([]Column, error) {
// If the result column is an expression or subquery, then an empty string is returned. // If the result column is an expression or subquery, then an empty string is returned.
// The left-most column is column 0. // The left-most column is column 0.
// (See http://www.sqlite.org/c3ref/column_decltype.html) // (See http://www.sqlite.org/c3ref/column_decltype.html)
func (s *Stmt) ColumnDeclaredType(index int) (string, error) { func (s *Stmt) ColumnDeclaredType(index int) string {
if index < 0 || index >= s.ColumnCount() { if index < 0 || index >= s.ColumnCount() {
return "", s.specificError("column index %d out of range [0,%d[.", index, s.ColumnCount()) panic(fmt.Sprintf("column index %d out of range [0,%d[.", index, s.ColumnCount()))
} }
return C.GoString(C.sqlite3_column_decltype(s.stmt, C.int(index))), nil return C.GoString(C.sqlite3_column_decltype(s.stmt, C.int(index)))
} }
// Affinity enumerates SQLite column type affinity // Affinity enumerates SQLite column type affinity
...@@ -201,13 +195,10 @@ const ( ...@@ -201,13 +195,10 @@ const (
// If the result column is an expression or subquery, then None is returned. // If the result column is an expression or subquery, then None is returned.
// The left-most column is column 0. // The left-most column is column 0.
// (See http://sqlite.org/datatype3.html) // (See http://sqlite.org/datatype3.html)
func (s *Stmt) ColumnTypeAffinity(index int) (Affinity, error) { func (s *Stmt) ColumnTypeAffinity(index int) Affinity {
if index < 0 || index >= s.ColumnCount() { if index < 0 || index >= s.ColumnCount() {
return None, s.specificError("column index %d out of range [0,%d[.", index, s.ColumnCount()) panic(fmt.Sprintf("column index %d out of range [0,%d[.", index, s.ColumnCount()))
} }
return s.columnTypeAffinity(index), nil
}
func (s *Stmt) columnTypeAffinity(index int) Affinity {
if s.affinities == nil { if s.affinities == nil {
count := s.ColumnCount() count := s.ColumnCount()
s.affinities = make([]Affinity, count) s.affinities = make([]Affinity, count)
...@@ -216,7 +207,7 @@ func (s *Stmt) columnTypeAffinity(index int) Affinity { ...@@ -216,7 +207,7 @@ func (s *Stmt) columnTypeAffinity(index int) Affinity {
return affinity return affinity
} }
} }
declType, _ := s.ColumnDeclaredType(index) declType := s.ColumnDeclaredType(index)
affinity := typeAffinity(declType) affinity := typeAffinity(declType)
s.affinities[index] = affinity s.affinities[index] = affinity
return affinity return affinity
......
...@@ -46,31 +46,31 @@ func (c *Conn) Column(dbName, tableName, columnName string) (*Column, error) { ...@@ -46,31 +46,31 @@ func (c *Conn) Column(dbName, tableName, columnName string) (*Column, error) {
// that is the origin of a particular result column in SELECT statement. // that is the origin of a particular result column in SELECT statement.
// The left-most column is column 0. // The left-most column is column 0.
// (See http://www.sqlite.org/c3ref/column_database_name.html) // (See http://www.sqlite.org/c3ref/column_database_name.html)
func (s *Stmt) ColumnDatabaseName(index int) (string, error) { func (s *Stmt) ColumnDatabaseName(index int) string {
if index < 0 || index >= s.ColumnCount() { if index < 0 || index >= s.ColumnCount() {
return "", s.specificError("column index %d out of range [0,%d[.", index, s.ColumnCount()) panic(fmt.Sprintf("column index %d out of range [0,%d[.", index, s.ColumnCount()))
} }
return C.GoString(C.sqlite3_column_database_name(s.stmt, C.int(index))), nil return C.GoString(C.sqlite3_column_database_name(s.stmt, C.int(index)))
} }
// ColumnTableName returns the original un-aliased table name // ColumnTableName returns the original un-aliased table name
// that is the origin of a particular result column in SELECT statement. // that is the origin of a particular result column in SELECT statement.
// The left-most column is column 0. // The left-most column is column 0.
// (See http://www.sqlite.org/c3ref/column_database_name.html) // (See http://www.sqlite.org/c3ref/column_database_name.html)
func (s *Stmt) ColumnTableName(index int) (string, error) { func (s *Stmt) ColumnTableName(index int) string {
if index < 0 || index >= s.ColumnCount() { if index < 0 || index >= s.ColumnCount() {
return "", s.specificError("column index %d out of range [0,%d[.", index, s.ColumnCount()) panic(fmt.Sprintf("column index %d out of range [0,%d[.", index, s.ColumnCount()))
} }
return C.GoString(C.sqlite3_column_table_name(s.stmt, C.int(index))), nil return C.GoString(C.sqlite3_column_table_name(s.stmt, C.int(index)))
} }
// ColumnOriginName returns the original un-aliased table column name // ColumnOriginName returns the original un-aliased table column name
// that is the origin of a particular result column in SELECT statement. // that is the origin of a particular result column in SELECT statement.
// The left-most column is column 0. // The left-most column is column 0.
// (See http://www.sqlite.org/c3ref/column_database_name.html) // (See http://www.sqlite.org/c3ref/column_database_name.html)
func (s *Stmt) ColumnOriginName(index int) (string, error) { func (s *Stmt) ColumnOriginName(index int) string {
if index < 0 || index >= s.ColumnCount() { if index < 0 || index >= s.ColumnCount() {
return "", s.specificError("column index %d out of range [0,%d[.", index, s.ColumnCount()) panic(fmt.Sprintf("column index %d out of range [0,%d[.", index, s.ColumnCount()))
} }
return C.GoString(C.sqlite3_column_origin_name(s.stmt, C.int(index))), nil return C.GoString(C.sqlite3_column_origin_name(s.stmt, C.int(index)))
} }
...@@ -40,20 +40,15 @@ func TestColumnMetadata(t *testing.T) { ...@@ -40,20 +40,15 @@ func TestColumnMetadata(t *testing.T) {
check(err) check(err)
defer checkFinalize(s, t) defer checkFinalize(s, t)
databaseName, err := s.ColumnDatabaseName(0) databaseName := s.ColumnDatabaseName(0)
check(err)
assert.Equal(t, "main", databaseName, "database name") assert.Equal(t, "main", databaseName, "database name")
tableName, err := s.ColumnTableName(0) tableName := s.ColumnTableName(0)
check(err)
assert.Equal(t, "sqlite_master", tableName, "table name") assert.Equal(t, "sqlite_master", tableName, "table name")
originName, err := s.ColumnOriginName(0) originName := s.ColumnOriginName(0)
check(err)
assert.Equal(t, "name", originName, "origin name") assert.Equal(t, "name", originName, "origin name")
declType, err := s.ColumnDeclaredType(0) declType := s.ColumnDeclaredType(0)
check(err)
assert.Equal(t, "text", declType, "declared type") assert.Equal(t, "text", declType, "declared type")
affinity, err := s.ColumnTypeAffinity(0) affinity := s.ColumnTypeAffinity(0)
check(err)
assert.Equal(t, Textual, affinity, "affinity") assert.Equal(t, Textual, affinity, "affinity")
} }
...@@ -68,20 +63,15 @@ func TestColumnMetadataOnView(t *testing.T) { ...@@ -68,20 +63,15 @@ func TestColumnMetadataOnView(t *testing.T) {
check(err) check(err)
defer checkFinalize(s, t) defer checkFinalize(s, t)
databaseName, err := s.ColumnDatabaseName(0) databaseName := s.ColumnDatabaseName(0)
check(err)
assert.Equal(t, "main", databaseName, "database name") assert.Equal(t, "main", databaseName, "database name")
tableName, err := s.ColumnTableName(0) tableName := s.ColumnTableName(0)
check(err)
assert.Equal(t, "test", tableName, "table name") assert.Equal(t, "test", tableName, "table name")
originName, err := s.ColumnOriginName(0) originName := s.ColumnOriginName(0)
check(err)
assert.Equal(t, "a_string", originName, "origin name") assert.Equal(t, "a_string", originName, "origin name")
declType, err := s.ColumnDeclaredType(0) declType := s.ColumnDeclaredType(0)
check(err)
assert.Equal(t, "TEXT", declType, "declared type") assert.Equal(t, "TEXT", declType, "declared type")
affinity, err := s.ColumnTypeAffinity(0) affinity := s.ColumnTypeAffinity(0)
check(err)
assert.Equal(t, Textual, affinity, "affinity") assert.Equal(t, Textual, affinity, "affinity")
} }
...@@ -95,19 +85,14 @@ func TestColumnMetadataOnExpr(t *testing.T) { ...@@ -95,19 +85,14 @@ func TestColumnMetadataOnExpr(t *testing.T) {
check(err) check(err)
defer checkFinalize(s, t) defer checkFinalize(s, t)
databaseName, err := s.ColumnDatabaseName(0) databaseName := s.ColumnDatabaseName(0)
check(err)
assert.Equal(t, "", databaseName, "database name") assert.Equal(t, "", databaseName, "database name")
tableName, err := s.ColumnTableName(0) tableName := s.ColumnTableName(0)
check(err)
assert.Equal(t, "", tableName, "table name") assert.Equal(t, "", tableName, "table name")
originName, err := s.ColumnOriginName(0) originName := s.ColumnOriginName(0)
check(err)
assert.Equal(t, "", originName, "origin name") assert.Equal(t, "", originName, "origin name")
declType, err := s.ColumnDeclaredType(0) declType := s.ColumnDeclaredType(0)
check(err)
assert.Equal(t, "", declType, "declared type") assert.Equal(t, "", declType, "declared type")
affinity, err := s.ColumnTypeAffinity(0) affinity := s.ColumnTypeAffinity(0)
check(err)
assert.Equal(t, None, affinity, "affinity") assert.Equal(t, None, affinity, "affinity")
} }
...@@ -198,24 +198,12 @@ func TestColumnTypeAffinity(t *testing.T) { ...@@ -198,24 +198,12 @@ func TestColumnTypeAffinity(t *testing.T) {
checkNoError(t, err, "%s") checkNoError(t, err, "%s")
defer checkFinalize(s, t) defer checkFinalize(s, t)
aff, err := s.ColumnTypeAffinity(0) assert.Equal(t, Integral, s.ColumnTypeAffinity(0), "affinity")
checkNoError(t, err, "%s") assert.Equal(t, Real, s.ColumnTypeAffinity(1), "affinity")
assert.Equal(t, Integral, aff, "affinity") assert.Equal(t, Numerical, s.ColumnTypeAffinity(2), "affinity")
aff, err = s.ColumnTypeAffinity(1) assert.Equal(t, None, s.ColumnTypeAffinity(3), "affinity")
checkNoError(t, err, "%s") assert.Equal(t, Textual, s.ColumnTypeAffinity(4), "affinity")
assert.Equal(t, Real, aff, "affinity") assert.Equal(t, None, s.ColumnTypeAffinity(5), "affinity")
aff, err = s.ColumnTypeAffinity(2)
checkNoError(t, err, "%s")
assert.Equal(t, Numerical, aff, "affinity")
aff, err = s.ColumnTypeAffinity(3)
checkNoError(t, err, "%s")
assert.Equal(t, None, aff, "affinity")
aff, err = s.ColumnTypeAffinity(4)
checkNoError(t, err, "%s")
assert.Equal(t, Textual, aff, "affinity")
aff, err = s.ColumnTypeAffinity(5)
checkNoError(t, err, "%s")
assert.Equal(t, None, aff, "affinity")
} }
func TestExpressionTypeAffinity(t *testing.T) { func TestExpressionTypeAffinity(t *testing.T) {
...@@ -226,16 +214,8 @@ func TestExpressionTypeAffinity(t *testing.T) { ...@@ -226,16 +214,8 @@ func TestExpressionTypeAffinity(t *testing.T) {
checkNoError(t, err, "%s") checkNoError(t, err, "%s")
defer checkFinalize(s, t) defer checkFinalize(s, t)
aff, err := s.ColumnTypeAffinity(0) assert.Equal(t, None, s.ColumnTypeAffinity(0), "affinity")
checkNoError(t, err, "%s") assert.Equal(t, None, s.ColumnTypeAffinity(1), "affinity")
assert.Equal(t, None, aff, "affinity") assert.Equal(t, None, s.ColumnTypeAffinity(2), "affinity")
aff, err = s.ColumnTypeAffinity(1) assert.Equal(t, None, s.ColumnTypeAffinity(3), "affinity")
checkNoError(t, err, "%s")
assert.Equal(t, None, aff, "affinity")
aff, err = s.ColumnTypeAffinity(2)
checkNoError(t, err, "%s")
assert.Equal(t, None, aff, "affinity")
aff, err = s.ColumnTypeAffinity(3)
checkNoError(t, err, "%s")
assert.Equal(t, None, aff, "affinity")
} }
This diff is collapsed.
...@@ -78,9 +78,7 @@ func TestInsertWithStatement(t *testing.T) { ...@@ -78,9 +78,7 @@ func TestInsertWithStatement(t *testing.T) {
defer checkFinalize(rs, t) defer checkFinalize(rs, t)
columnCount = rs.ColumnCount() columnCount = rs.ColumnCount()
assert.Equal(t, 3, columnCount, "column count") assert.Equal(t, 3, columnCount, "column count")
secondColumnName, err := rs.ColumnName(1) assert.Equal(t, "int_num", rs.ColumnName(1), "column name")
checkNoError(t, err, "error accessing column name: %s")
assert.Equal(t, "int_num", secondColumnName, "column name")
if checkStep(t, rs) { if checkStep(t, rs) {
var fnum float64 var fnum float64
...@@ -231,8 +229,7 @@ func TestScanNull(t *testing.T) { ...@@ -231,8 +229,7 @@ func TestScanNull(t *testing.T) {
assert.T(t, null, "expected null value") assert.T(t, null, "expected null value")
assert.Equal(t, false, bo, "expected false") assert.Equal(t, false, bo, "expected false")
rb, null, err := s.ScanRawBytes(0) rb, null := s.ScanRawBytes(0)
checkNoError(t, err, "scan error: %s")
assert.T(t, null, "expected null value") assert.T(t, null, "expected null value")
assert.Equal(t, 0, len(rb), "expected empty") assert.Equal(t, 0, len(rb), "expected empty")
} }
...@@ -256,8 +253,7 @@ func TestScanNotNull(t *testing.T) { ...@@ -256,8 +253,7 @@ func TestScanNotNull(t *testing.T) {
assert.T(t, !null, "expected not null value") assert.T(t, !null, "expected not null value")
assert.Equal(t, "1", *ps) assert.Equal(t, "1", *ps)
rb, null, err := s.ScanRawBytes(0) rb, null := s.ScanRawBytes(0)
checkNoError(t, err, "scan error: %s")
assert.T(t, !null, "expected not null value") assert.T(t, !null, "expected not null value")
assert.Equal(t, 1, len(rb), "expected not empty") assert.Equal(t, 1, len(rb), "expected not empty")
...@@ -512,8 +508,7 @@ func TestScanBytes(t *testing.T) { ...@@ -512,8 +508,7 @@ func TestScanBytes(t *testing.T) {
checkNoError(t, err, "prepare error: %s") checkNoError(t, err, "prepare error: %s")
defer checkFinalize(s, t) defer checkFinalize(s, t)
assert.T(t, checkStep(t, s)) assert.T(t, checkStep(t, s))
blob, _, err := s.ScanBlob(0) blob, _ := s.ScanBlob(0)
checkNoError(t, err, "scan error: %s")
assert.Equal(t, "test", string(blob)) assert.Equal(t, "test", string(blob))
} }
...@@ -532,11 +527,9 @@ func TestBindEmptyZero(t *testing.T) { ...@@ -532,11 +527,9 @@ func TestBindEmptyZero(t *testing.T) {
err = s.Scan(&ps, &zt) err = s.Scan(&ps, &zt)
checkNoError(t, err, "scan error: %s") checkNoError(t, err, "scan error: %s")
assert.T(t, ps == nil && zt.IsZero(), "null pointers expected") assert.T(t, ps == nil && zt.IsZero(), "null pointers expected")
_, null, err := s.ScanValue(0, false) _, null := s.ScanValue(0, false)
checkNoError(t, err, "scan error: %s")
assert.T(t, null, "null string expected") assert.T(t, null, "null string expected")
_, null, err = s.ScanValue(1, false) _, null = s.ScanValue(1, false)
checkNoError(t, err, "scan error: %s")
assert.T(t, null, "null time expected") assert.T(t, null, "null time expected")
} }
...@@ -562,11 +555,9 @@ func TestBindEmptyZeroNotTransformedToNull(t *testing.T) { ...@@ -562,11 +555,9 @@ func TestBindEmptyZeroNotTransformedToNull(t *testing.T) {
err = s.Scan(&st, &zt) err = s.Scan(&st, &zt)
checkNoError(t, err, "scan error: %s") checkNoError(t, err, "scan error: %s")
assert.T(t, len(st) == 0 && zt.IsZero(), "null pointers expected") assert.T(t, len(st) == 0 && zt.IsZero(), "null pointers expected")
_, null, err := s.ScanValue(0, false) _, null := s.ScanValue(0, false)
checkNoError(t, err, "scan error: %s")
assert.T(t, !null, "empty string expected") assert.T(t, !null, "empty string expected")
_, null, err = s.ScanValue(1, false) _, null = s.ScanValue(1, false)
checkNoError(t, err, "scan error: %s")
assert.T(t, !null, "zero time expected") assert.T(t, !null, "zero time expected")
} }
...@@ -582,12 +573,8 @@ func TestColumnType(t *testing.T) { ...@@ -582,12 +573,8 @@ func TestColumnType(t *testing.T) {
expectedAffinities := []Affinity{Integral, Real, Integral, Textual} expectedAffinities := []Affinity{Integral, Real, Integral, Textual}
for col := 0; col < s.ColumnCount(); col++ { for col := 0; col < s.ColumnCount(); col++ {
//println(col, s.ColumnName(col), s.ColumnOriginName(col), s.ColumnType(col), s.ColumnDeclaredType(col)) //println(col, s.ColumnName(col), s.ColumnOriginName(col), s.ColumnType(col), s.ColumnDeclaredType(col))
ct, err := s.ColumnType(col) assert.Equal(t, Null, s.ColumnType(col), "column type")
checkNoError(t, err, "column type error: %s") assert.Equal(t, expectedAffinities[col], s.ColumnTypeAffinity(col), "column type affinity")
assert.Equal(t, Null, ct, "column type")
cta, err := s.ColumnTypeAffinity(col)
checkNoError(t, err, "column type affinity error: %s")
assert.Equal(t, expectedAffinities[col], cta, "column type affinity")
} }
} }
......
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