diff --git a/all_test.go b/all_test.go index cf0aff8..0a34668 100644 --- a/all_test.go +++ b/all_test.go @@ -1038,7 +1038,7 @@ func TestColumnTypes(t *testing.T) { if g, e := b.String(), `Col 0: DatabaseTypeName "INTEGER", DecimalSize 0 0 false, Length 0 false, Name "uid", Nullable true true, ScanType "int64" Col 1: DatabaseTypeName "VARCHAR(64)", DecimalSize 0 0 false, Length 9223372036854775807 true, Name "username", Nullable true true, ScanType "string" Col 2: DatabaseTypeName "VARCHAR(64)", DecimalSize 0 0 false, Length 9223372036854775807 true, Name "departname", Nullable true true, ScanType "string" -Col 3: DatabaseTypeName "DATE", DecimalSize 0 0 false, Length 9223372036854775807 true, Name "created", Nullable true true, ScanType "string" +Col 3: DatabaseTypeName "DATE", DecimalSize 0 0 false, Length 9223372036854775807 true, Name "created", Nullable true true, ScanType "time.Time" `; g != e { t.Fatalf("---- got\n%s\n----expected\n%s", g, e) } diff --git a/sqlite.go b/sqlite.go index 146583e..304167b 100644 --- a/sqlite.go +++ b/sqlite.go @@ -429,20 +429,28 @@ func (r *rows) ColumnTypeScanType(index int) reflect.Type { return reflect.TypeOf("") } + declType := strings.ToLower(r.c.columnDeclType(r.pstmt, index)) + switch t { case sqlite3.SQLITE_INTEGER: - switch strings.ToLower(r.c.columnDeclType(r.pstmt, index)) { - case "boolean": + if declType == "boolean" { + // SQLite does not have a separate Boolean storage class. Instead, Boolean values are stored as integers 0 (false) and 1 (true). return reflect.TypeOf(false) - case "date", "datetime", "time", "timestamp": - return reflect.TypeOf(time.Time{}) - default: + } else { return reflect.TypeOf(int64(0)) } case sqlite3.SQLITE_FLOAT: return reflect.TypeOf(float64(0)) case sqlite3.SQLITE_TEXT: - return reflect.TypeOf("") + // SQLite does not have a storage class set aside for storing dates and/or times. + // Instead, the built-in Date And Time Functions of SQLite are capable of storing + // dates and times as TEXT, REAL, or INTEGER values + switch declType { + case "date", "datetime", "time", "timestamp": + return reflect.TypeOf(time.Time{}) + default: + return reflect.TypeOf("") + } case sqlite3.SQLITE_BLOB: return reflect.SliceOf(reflect.TypeOf([]byte{})) case sqlite3.SQLITE_NULL: