本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 satır
2.4KB

  1. package pq
  2. import (
  3. "math"
  4. "reflect"
  5. "time"
  6. "github.com/lib/pq/oid"
  7. )
  8. const headerSize = 4
  9. type fieldDesc struct {
  10. // The object ID of the data type.
  11. OID oid.Oid
  12. // The data type size (see pg_type.typlen).
  13. // Note that negative values denote variable-width types.
  14. Len int
  15. // The type modifier (see pg_attribute.atttypmod).
  16. // The meaning of the modifier is type-specific.
  17. Mod int
  18. }
  19. func (fd fieldDesc) Type() reflect.Type {
  20. switch fd.OID {
  21. case oid.T_int8:
  22. return reflect.TypeOf(int64(0))
  23. case oid.T_int4:
  24. return reflect.TypeOf(int32(0))
  25. case oid.T_int2:
  26. return reflect.TypeOf(int16(0))
  27. case oid.T_varchar, oid.T_text:
  28. return reflect.TypeOf("")
  29. case oid.T_bool:
  30. return reflect.TypeOf(false)
  31. case oid.T_date, oid.T_time, oid.T_timetz, oid.T_timestamp, oid.T_timestamptz:
  32. return reflect.TypeOf(time.Time{})
  33. case oid.T_bytea:
  34. return reflect.TypeOf([]byte(nil))
  35. default:
  36. return reflect.TypeOf(new(interface{})).Elem()
  37. }
  38. }
  39. func (fd fieldDesc) Name() string {
  40. return oid.TypeName[fd.OID]
  41. }
  42. func (fd fieldDesc) Length() (length int64, ok bool) {
  43. switch fd.OID {
  44. case oid.T_text, oid.T_bytea:
  45. return math.MaxInt64, true
  46. case oid.T_varchar, oid.T_bpchar:
  47. return int64(fd.Mod - headerSize), true
  48. default:
  49. return 0, false
  50. }
  51. }
  52. func (fd fieldDesc) PrecisionScale() (precision, scale int64, ok bool) {
  53. switch fd.OID {
  54. case oid.T_numeric, oid.T__numeric:
  55. mod := fd.Mod - headerSize
  56. precision = int64((mod >> 16) & 0xffff)
  57. scale = int64(mod & 0xffff)
  58. return precision, scale, true
  59. default:
  60. return 0, 0, false
  61. }
  62. }
  63. // ColumnTypeScanType returns the value type that can be used to scan types into.
  64. func (rs *rows) ColumnTypeScanType(index int) reflect.Type {
  65. return rs.colTyps[index].Type()
  66. }
  67. // ColumnTypeDatabaseTypeName return the database system type name.
  68. func (rs *rows) ColumnTypeDatabaseTypeName(index int) string {
  69. return rs.colTyps[index].Name()
  70. }
  71. // ColumnTypeLength returns the length of the column type if the column is a
  72. // variable length type. If the column is not a variable length type ok
  73. // should return false.
  74. func (rs *rows) ColumnTypeLength(index int) (length int64, ok bool) {
  75. return rs.colTyps[index].Length()
  76. }
  77. // ColumnTypePrecisionScale should return the precision and scale for decimal
  78. // types. If not applicable, ok should be false.
  79. func (rs *rows) ColumnTypePrecisionScale(index int) (precision, scale int64, ok bool) {
  80. return rs.colTyps[index].PrecisionScale()
  81. }
上海开阖软件有限公司 沪ICP备12045867号-1