mirror of
https://github.com/glebarez/go-sqlite.git
synced 2025-10-04 15:32:46 +08:00
ColumnTypeScanType: return time.Time for date/time types
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
20
sqlite.go
20
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:
|
||||
|
Reference in New Issue
Block a user