sqlite3_type.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package sqlite3
  2. /*
  3. #ifndef USE_LIBSQLITE3
  4. #include <sqlite3-binding.h>
  5. #else
  6. #include <sqlite3.h>
  7. #endif
  8. */
  9. import "C"
  10. import (
  11. "reflect"
  12. "time"
  13. )
  14. // ColumnTypeDatabaseTypeName implement RowsColumnTypeDatabaseTypeName.
  15. func (rc *SQLiteRows) ColumnTypeDatabaseTypeName(i int) string {
  16. return C.GoString(C.sqlite3_column_decltype(rc.s.s, C.int(i)))
  17. }
  18. /*
  19. func (rc *SQLiteRows) ColumnTypeLength(index int) (length int64, ok bool) {
  20. return 0, false
  21. }
  22. func (rc *SQLiteRows) ColumnTypePrecisionScale(index int) (precision, scale int64, ok bool) {
  23. return 0, 0, false
  24. }
  25. */
  26. // ColumnTypeNullable implement RowsColumnTypeNullable.
  27. func (rc *SQLiteRows) ColumnTypeNullable(i int) (nullable, ok bool) {
  28. return true, true
  29. }
  30. // ColumnTypeScanType implement RowsColumnTypeScanType.
  31. func (rc *SQLiteRows) ColumnTypeScanType(i int) reflect.Type {
  32. switch C.sqlite3_column_type(rc.s.s, C.int(i)) {
  33. case C.SQLITE_INTEGER:
  34. switch C.GoString(C.sqlite3_column_decltype(rc.s.s, C.int(i))) {
  35. case "timestamp", "datetime", "date":
  36. return reflect.TypeOf(time.Time{})
  37. case "boolean":
  38. return reflect.TypeOf(false)
  39. }
  40. return reflect.TypeOf(int64(0))
  41. case C.SQLITE_FLOAT:
  42. return reflect.TypeOf(float64(0))
  43. case C.SQLITE_BLOB:
  44. return reflect.SliceOf(reflect.TypeOf(byte(0)))
  45. case C.SQLITE_NULL:
  46. return reflect.TypeOf(nil)
  47. case C.SQLITE_TEXT:
  48. return reflect.TypeOf("")
  49. }
  50. return reflect.SliceOf(reflect.TypeOf(byte(0)))
  51. }