ColumnTypeScanType: return time.Time for date/time types

This commit is contained in:
glebarez
2022-03-21 21:10:10 +03:00
parent 890e2cac01
commit 19d521f4e5
2 changed files with 15 additions and 7 deletions

View File

@@ -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)
}

View File

@@ -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:
// 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: