-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
use sqlite3_column_type() for ColumnTypeScanType() #1327
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 2 commits
0a6a5dc
27e8f68
855d426
76882fe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,27 @@ import ( | |
"strings" | ||
) | ||
|
||
const ( | ||
SQLITE_INTEGER = iota | ||
SQLITE_TEXT | ||
SQLITE_BLOB | ||
SQLITE_REAL | ||
SQLITE_NUMERIC | ||
SQLITE_TIME | ||
SQLITE_BOOL | ||
SQLITE_NULL | ||
) | ||
|
||
var ( | ||
TYPE_NULLINT = reflect.TypeOf(sql.NullInt64{}) | ||
TYPE_NULLFLOAT = reflect.TypeOf(sql.NullFloat64{}) | ||
TYPE_NULLSTRING = reflect.TypeOf(sql.NullString{}) | ||
TYPE_RAWBYTES = reflect.TypeOf(sql.RawBytes{}) | ||
TYPE_NULLBOOL = reflect.TypeOf(sql.NullBool{}) | ||
TYPE_NULLTIME = reflect.TypeOf(sql.NullTime{}) | ||
TYPE_ANY = reflect.TypeOf(new(any)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well this is unfortunate - this means we are saying There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Relevant code:
In our case, this works:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's unrelated. For example, You'll also notice that all the other types being returned (e.g., In other words, you are supposed to create a variable of the type from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
) | ||
|
||
// ColumnTypeDatabaseTypeName implement RowsColumnTypeDatabaseTypeName. | ||
func (rc *SQLiteRows) ColumnTypeDatabaseTypeName(i int) string { | ||
return C.GoString(C.sqlite3_column_decltype(rc.s.s, C.int(i))) | ||
|
@@ -39,42 +60,50 @@ func (rc *SQLiteRows) ColumnTypeNullable(i int) (nullable, ok bool) { | |
} | ||
|
||
// ColumnTypeScanType implement RowsColumnTypeScanType. | ||
// In SQLite3, this method should be called after Next() has been called, as sqlite3_column_type() | ||
// returns the column type for a specific row. If Next() has not been called, fallback to | ||
// sqlite3_column_decltype() | ||
func (rc *SQLiteRows) ColumnTypeScanType(i int) reflect.Type { | ||
//ct := C.sqlite3_column_type(rc.s.s, C.int(i)) // Always returns 5 | ||
switch C.sqlite3_column_type(rc.s.s, C.int(i)) { | ||
alkemir marked this conversation as resolved.
Show resolved
Hide resolved
|
||
case C.SQLITE_INTEGER: | ||
return TYPE_NULLINT | ||
case C.SQLITE_FLOAT: | ||
return TYPE_NULLFLOAT | ||
case C.SQLITE_TEXT: | ||
return TYPE_NULLSTRING | ||
case C.SQLITE_BLOB: | ||
return TYPE_RAWBYTES | ||
// This case can signal that the value is NULL or that Next() has not been called yet. | ||
// Skip it and return the fallback behaviour as a best effort. This is safe as all types | ||
// returned are Nullable or any, which is the expected value for SQLite3. | ||
//case C.SQLITE_NULL: | ||
alkemir marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// return TYPE_ANY | ||
} | ||
|
||
// Fallback to schema declared to remain retro-compatible | ||
return scanType(C.GoString(C.sqlite3_column_decltype(rc.s.s, C.int(i)))) | ||
} | ||
|
||
const ( | ||
SQLITE_INTEGER = iota | ||
SQLITE_TEXT | ||
SQLITE_BLOB | ||
SQLITE_REAL | ||
SQLITE_NUMERIC | ||
SQLITE_TIME | ||
SQLITE_BOOL | ||
SQLITE_NULL | ||
) | ||
|
||
func scanType(cdt string) reflect.Type { | ||
t := strings.ToUpper(cdt) | ||
i := databaseTypeConvSqlite(t) | ||
switch i { | ||
case SQLITE_INTEGER: | ||
return reflect.TypeOf(sql.NullInt64{}) | ||
return TYPE_NULLINT | ||
case SQLITE_TEXT: | ||
return reflect.TypeOf(sql.NullString{}) | ||
return TYPE_NULLSTRING | ||
case SQLITE_BLOB: | ||
return reflect.TypeOf(sql.RawBytes{}) | ||
return TYPE_RAWBYTES | ||
case SQLITE_REAL: | ||
return reflect.TypeOf(sql.NullFloat64{}) | ||
return TYPE_NULLFLOAT | ||
case SQLITE_NUMERIC: | ||
return reflect.TypeOf(sql.NullFloat64{}) | ||
return TYPE_NULLFLOAT | ||
case SQLITE_BOOL: | ||
return reflect.TypeOf(sql.NullBool{}) | ||
return TYPE_NULLBOOL | ||
case SQLITE_TIME: | ||
return reflect.TypeOf(sql.NullTime{}) | ||
return TYPE_NULLTIME | ||
} | ||
return reflect.TypeOf(new(any)) | ||
return TYPE_ANY | ||
} | ||
|
||
func databaseTypeConvSqlite(t string) int { | ||
|
Uh oh!
There was an error while loading. Please reload this page.