encode.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. package pq
  2. import (
  3. "bytes"
  4. "database/sql/driver"
  5. "encoding/binary"
  6. "encoding/hex"
  7. "errors"
  8. "fmt"
  9. "math"
  10. "strconv"
  11. "strings"
  12. "sync"
  13. "time"
  14. "github.com/lib/pq/oid"
  15. )
  16. func binaryEncode(parameterStatus *parameterStatus, x interface{}) []byte {
  17. switch v := x.(type) {
  18. case []byte:
  19. return v
  20. default:
  21. return encode(parameterStatus, x, oid.T_unknown)
  22. }
  23. }
  24. func encode(parameterStatus *parameterStatus, x interface{}, pgtypOid oid.Oid) []byte {
  25. switch v := x.(type) {
  26. case int64:
  27. return strconv.AppendInt(nil, v, 10)
  28. case float64:
  29. return strconv.AppendFloat(nil, v, 'f', -1, 64)
  30. case []byte:
  31. if pgtypOid == oid.T_bytea {
  32. return encodeBytea(parameterStatus.serverVersion, v)
  33. }
  34. return v
  35. case string:
  36. if pgtypOid == oid.T_bytea {
  37. return encodeBytea(parameterStatus.serverVersion, []byte(v))
  38. }
  39. return []byte(v)
  40. case bool:
  41. return strconv.AppendBool(nil, v)
  42. case time.Time:
  43. return formatTs(v)
  44. default:
  45. errorf("encode: unknown type for %T", v)
  46. }
  47. panic("not reached")
  48. }
  49. func decode(parameterStatus *parameterStatus, s []byte, typ oid.Oid, f format) interface{} {
  50. if f == formatBinary {
  51. return binaryDecode(parameterStatus, s, typ)
  52. } else {
  53. return textDecode(parameterStatus, s, typ)
  54. }
  55. }
  56. func binaryDecode(parameterStatus *parameterStatus, s []byte, typ oid.Oid) interface{} {
  57. switch typ {
  58. case oid.T_bytea:
  59. return s
  60. case oid.T_int8:
  61. return int64(binary.BigEndian.Uint64(s))
  62. case oid.T_int4:
  63. return int64(int32(binary.BigEndian.Uint32(s)))
  64. case oid.T_int2:
  65. return int64(int16(binary.BigEndian.Uint16(s)))
  66. default:
  67. errorf("don't know how to decode binary parameter of type %d", uint32(typ))
  68. }
  69. panic("not reached")
  70. }
  71. func textDecode(parameterStatus *parameterStatus, s []byte, typ oid.Oid) interface{} {
  72. switch typ {
  73. case oid.T_bytea:
  74. return parseBytea(s)
  75. case oid.T_timestamptz:
  76. return parseTs(parameterStatus.currentLocation, string(s))
  77. case oid.T_timestamp, oid.T_date:
  78. return parseTs(nil, string(s))
  79. case oid.T_time:
  80. return mustParse("15:04:05", typ, s)
  81. case oid.T_timetz:
  82. return mustParse("15:04:05-07", typ, s)
  83. case oid.T_bool:
  84. return s[0] == 't'
  85. case oid.T_int8, oid.T_int4, oid.T_int2:
  86. i, err := strconv.ParseInt(string(s), 10, 64)
  87. if err != nil {
  88. errorf("%s", err)
  89. }
  90. return i
  91. case oid.T_float4, oid.T_float8:
  92. bits := 64
  93. if typ == oid.T_float4 {
  94. bits = 32
  95. }
  96. f, err := strconv.ParseFloat(string(s), bits)
  97. if err != nil {
  98. errorf("%s", err)
  99. }
  100. return f
  101. }
  102. return s
  103. }
  104. // appendEncodedText encodes item in text format as required by COPY
  105. // and appends to buf
  106. func appendEncodedText(parameterStatus *parameterStatus, buf []byte, x interface{}) []byte {
  107. switch v := x.(type) {
  108. case int64:
  109. return strconv.AppendInt(buf, v, 10)
  110. case float64:
  111. return strconv.AppendFloat(buf, v, 'f', -1, 64)
  112. case []byte:
  113. encodedBytea := encodeBytea(parameterStatus.serverVersion, v)
  114. return appendEscapedText(buf, string(encodedBytea))
  115. case string:
  116. return appendEscapedText(buf, v)
  117. case bool:
  118. return strconv.AppendBool(buf, v)
  119. case time.Time:
  120. return append(buf, formatTs(v)...)
  121. case nil:
  122. return append(buf, "\\N"...)
  123. default:
  124. errorf("encode: unknown type for %T", v)
  125. }
  126. panic("not reached")
  127. }
  128. func appendEscapedText(buf []byte, text string) []byte {
  129. escapeNeeded := false
  130. startPos := 0
  131. var c byte
  132. // check if we need to escape
  133. for i := 0; i < len(text); i++ {
  134. c = text[i]
  135. if c == '\\' || c == '\n' || c == '\r' || c == '\t' {
  136. escapeNeeded = true
  137. startPos = i
  138. break
  139. }
  140. }
  141. if !escapeNeeded {
  142. return append(buf, text...)
  143. }
  144. // copy till first char to escape, iterate the rest
  145. result := append(buf, text[:startPos]...)
  146. for i := startPos; i < len(text); i++ {
  147. c = text[i]
  148. switch c {
  149. case '\\':
  150. result = append(result, '\\', '\\')
  151. case '\n':
  152. result = append(result, '\\', 'n')
  153. case '\r':
  154. result = append(result, '\\', 'r')
  155. case '\t':
  156. result = append(result, '\\', 't')
  157. default:
  158. result = append(result, c)
  159. }
  160. }
  161. return result
  162. }
  163. func mustParse(f string, typ oid.Oid, s []byte) time.Time {
  164. str := string(s)
  165. // check for a 30-minute-offset timezone
  166. if (typ == oid.T_timestamptz || typ == oid.T_timetz) &&
  167. str[len(str)-3] == ':' {
  168. f += ":00"
  169. }
  170. t, err := time.Parse(f, str)
  171. if err != nil {
  172. errorf("decode: %s", err)
  173. }
  174. return t
  175. }
  176. var invalidTimestampErr = errors.New("invalid timestamp")
  177. type timestampParser struct {
  178. err error
  179. }
  180. func (p *timestampParser) expect(str, char string, pos int) {
  181. if p.err != nil {
  182. return
  183. }
  184. if pos+1 > len(str) {
  185. p.err = invalidTimestampErr
  186. return
  187. }
  188. if c := str[pos : pos+1]; c != char && p.err == nil {
  189. p.err = fmt.Errorf("expected '%v' at position %v; got '%v'", char, pos, c)
  190. }
  191. }
  192. func (p *timestampParser) mustAtoi(str string, begin int, end int) int {
  193. if p.err != nil {
  194. return 0
  195. }
  196. if begin < 0 || end < 0 || begin > end || end > len(str) {
  197. p.err = invalidTimestampErr
  198. return 0
  199. }
  200. result, err := strconv.Atoi(str[begin:end])
  201. if err != nil {
  202. if p.err == nil {
  203. p.err = fmt.Errorf("expected number; got '%v'", str)
  204. }
  205. return 0
  206. }
  207. return result
  208. }
  209. // The location cache caches the time zones typically used by the client.
  210. type locationCache struct {
  211. cache map[int]*time.Location
  212. lock sync.Mutex
  213. }
  214. // All connections share the same list of timezones. Benchmarking shows that
  215. // about 5% speed could be gained by putting the cache in the connection and
  216. // losing the mutex, at the cost of a small amount of memory and a somewhat
  217. // significant increase in code complexity.
  218. var globalLocationCache *locationCache = newLocationCache()
  219. func newLocationCache() *locationCache {
  220. return &locationCache{cache: make(map[int]*time.Location)}
  221. }
  222. // Returns the cached timezone for the specified offset, creating and caching
  223. // it if necessary.
  224. func (c *locationCache) getLocation(offset int) *time.Location {
  225. c.lock.Lock()
  226. defer c.lock.Unlock()
  227. location, ok := c.cache[offset]
  228. if !ok {
  229. location = time.FixedZone("", offset)
  230. c.cache[offset] = location
  231. }
  232. return location
  233. }
  234. var infinityTsEnabled = false
  235. var infinityTsNegative time.Time
  236. var infinityTsPositive time.Time
  237. const (
  238. infinityTsEnabledAlready = "pq: infinity timestamp enabled already"
  239. infinityTsNegativeMustBeSmaller = "pq: infinity timestamp: negative value must be smaller (before) than positive"
  240. )
  241. /*
  242. * If EnableInfinityTs is not called, "-infinity" and "infinity" will return
  243. * []byte("-infinity") and []byte("infinity") respectively, and potentially
  244. * cause error "sql: Scan error on column index 0: unsupported driver -> Scan pair: []uint8 -> *time.Time",
  245. * when scanning into a time.Time value.
  246. *
  247. * Once EnableInfinityTs has been called, all connections created using this
  248. * driver will decode Postgres' "-infinity" and "infinity" for "timestamp",
  249. * "timestamp with time zone" and "date" types to the predefined minimum and
  250. * maximum times, respectively. When encoding time.Time values, any time which
  251. * equals or precedes the predefined minimum time will be encoded to
  252. * "-infinity". Any values at or past the maximum time will similarly be
  253. * encoded to "infinity".
  254. *
  255. *
  256. * If EnableInfinityTs is called with negative >= positive, it will panic.
  257. * Calling EnableInfinityTs after a connection has been established results in
  258. * undefined behavior. If EnableInfinityTs is called more than once, it will
  259. * panic.
  260. */
  261. func EnableInfinityTs(negative time.Time, positive time.Time) {
  262. if infinityTsEnabled {
  263. panic(infinityTsEnabledAlready)
  264. }
  265. if !negative.Before(positive) {
  266. panic(infinityTsNegativeMustBeSmaller)
  267. }
  268. infinityTsEnabled = true
  269. infinityTsNegative = negative
  270. infinityTsPositive = positive
  271. }
  272. /*
  273. * Testing might want to toggle infinityTsEnabled
  274. */
  275. func disableInfinityTs() {
  276. infinityTsEnabled = false
  277. }
  278. // This is a time function specific to the Postgres default DateStyle
  279. // setting ("ISO, MDY"), the only one we currently support. This
  280. // accounts for the discrepancies between the parsing available with
  281. // time.Parse and the Postgres date formatting quirks.
  282. func parseTs(currentLocation *time.Location, str string) interface{} {
  283. switch str {
  284. case "-infinity":
  285. if infinityTsEnabled {
  286. return infinityTsNegative
  287. }
  288. return []byte(str)
  289. case "infinity":
  290. if infinityTsEnabled {
  291. return infinityTsPositive
  292. }
  293. return []byte(str)
  294. }
  295. t, err := ParseTimestamp(currentLocation, str)
  296. if err != nil {
  297. panic(err)
  298. }
  299. return t
  300. }
  301. func ParseTimestamp(currentLocation *time.Location, str string) (time.Time, error) {
  302. p := timestampParser{}
  303. monSep := strings.IndexRune(str, '-')
  304. // this is Gregorian year, not ISO Year
  305. // In Gregorian system, the year 1 BC is followed by AD 1
  306. year := p.mustAtoi(str, 0, monSep)
  307. daySep := monSep + 3
  308. month := p.mustAtoi(str, monSep+1, daySep)
  309. p.expect(str, "-", daySep)
  310. timeSep := daySep + 3
  311. day := p.mustAtoi(str, daySep+1, timeSep)
  312. var hour, minute, second int
  313. if len(str) > monSep+len("01-01")+1 {
  314. p.expect(str, " ", timeSep)
  315. minSep := timeSep + 3
  316. p.expect(str, ":", minSep)
  317. hour = p.mustAtoi(str, timeSep+1, minSep)
  318. secSep := minSep + 3
  319. p.expect(str, ":", secSep)
  320. minute = p.mustAtoi(str, minSep+1, secSep)
  321. secEnd := secSep + 3
  322. second = p.mustAtoi(str, secSep+1, secEnd)
  323. }
  324. remainderIdx := monSep + len("01-01 00:00:00") + 1
  325. // Three optional (but ordered) sections follow: the
  326. // fractional seconds, the time zone offset, and the BC
  327. // designation. We set them up here and adjust the other
  328. // offsets if the preceding sections exist.
  329. nanoSec := 0
  330. tzOff := 0
  331. if remainderIdx+1 <= len(str) && str[remainderIdx:remainderIdx+1] == "." {
  332. fracStart := remainderIdx + 1
  333. fracOff := strings.IndexAny(str[fracStart:], "-+ ")
  334. if fracOff < 0 {
  335. fracOff = len(str) - fracStart
  336. }
  337. fracSec := p.mustAtoi(str, fracStart, fracStart+fracOff)
  338. nanoSec = fracSec * (1000000000 / int(math.Pow(10, float64(fracOff))))
  339. remainderIdx += fracOff + 1
  340. }
  341. if tzStart := remainderIdx; tzStart+1 <= len(str) && (str[tzStart:tzStart+1] == "-" || str[tzStart:tzStart+1] == "+") {
  342. // time zone separator is always '-' or '+' (UTC is +00)
  343. var tzSign int
  344. if c := str[tzStart : tzStart+1]; c == "-" {
  345. tzSign = -1
  346. } else if c == "+" {
  347. tzSign = +1
  348. } else {
  349. return time.Time{}, fmt.Errorf("expected '-' or '+' at position %v; got %v", tzStart, c)
  350. }
  351. tzHours := p.mustAtoi(str, tzStart+1, tzStart+3)
  352. remainderIdx += 3
  353. var tzMin, tzSec int
  354. if tzStart+4 <= len(str) && str[tzStart+3:tzStart+4] == ":" {
  355. tzMin = p.mustAtoi(str, tzStart+4, tzStart+6)
  356. remainderIdx += 3
  357. }
  358. if tzStart+7 <= len(str) && str[tzStart+6:tzStart+7] == ":" {
  359. tzSec = p.mustAtoi(str, tzStart+7, tzStart+9)
  360. remainderIdx += 3
  361. }
  362. tzOff = tzSign * ((tzHours * 60 * 60) + (tzMin * 60) + tzSec)
  363. }
  364. var isoYear int
  365. if remainderIdx+3 <= len(str) && str[remainderIdx:remainderIdx+3] == " BC" {
  366. isoYear = 1 - year
  367. remainderIdx += 3
  368. } else {
  369. isoYear = year
  370. }
  371. if remainderIdx < len(str) {
  372. return time.Time{}, fmt.Errorf("expected end of input, got %v", str[remainderIdx:])
  373. }
  374. t := time.Date(isoYear, time.Month(month), day,
  375. hour, minute, second, nanoSec,
  376. globalLocationCache.getLocation(tzOff))
  377. if currentLocation != nil {
  378. // Set the location of the returned Time based on the session's
  379. // TimeZone value, but only if the local time zone database agrees with
  380. // the remote database on the offset.
  381. lt := t.In(currentLocation)
  382. _, newOff := lt.Zone()
  383. if newOff == tzOff {
  384. t = lt
  385. }
  386. }
  387. return t, p.err
  388. }
  389. // formatTs formats t into a format postgres understands.
  390. func formatTs(t time.Time) (b []byte) {
  391. if infinityTsEnabled {
  392. // t <= -infinity : ! (t > -infinity)
  393. if !t.After(infinityTsNegative) {
  394. return []byte("-infinity")
  395. }
  396. // t >= infinity : ! (!t < infinity)
  397. if !t.Before(infinityTsPositive) {
  398. return []byte("infinity")
  399. }
  400. }
  401. // Need to send dates before 0001 A.D. with " BC" suffix, instead of the
  402. // minus sign preferred by Go.
  403. // Beware, "0000" in ISO is "1 BC", "-0001" is "2 BC" and so on
  404. bc := false
  405. if t.Year() <= 0 {
  406. // flip year sign, and add 1, e.g: "0" will be "1", and "-10" will be "11"
  407. t = t.AddDate((-t.Year())*2+1, 0, 0)
  408. bc = true
  409. }
  410. b = []byte(t.Format(time.RFC3339Nano))
  411. _, offset := t.Zone()
  412. offset = offset % 60
  413. if offset != 0 {
  414. // RFC3339Nano already printed the minus sign
  415. if offset < 0 {
  416. offset = -offset
  417. }
  418. b = append(b, ':')
  419. if offset < 10 {
  420. b = append(b, '0')
  421. }
  422. b = strconv.AppendInt(b, int64(offset), 10)
  423. }
  424. if bc {
  425. b = append(b, " BC"...)
  426. }
  427. return b
  428. }
  429. // Parse a bytea value received from the server. Both "hex" and the legacy
  430. // "escape" format are supported.
  431. func parseBytea(s []byte) (result []byte) {
  432. if len(s) >= 2 && bytes.Equal(s[:2], []byte("\\x")) {
  433. // bytea_output = hex
  434. s = s[2:] // trim off leading "\\x"
  435. result = make([]byte, hex.DecodedLen(len(s)))
  436. _, err := hex.Decode(result, s)
  437. if err != nil {
  438. errorf("%s", err)
  439. }
  440. } else {
  441. // bytea_output = escape
  442. for len(s) > 0 {
  443. if s[0] == '\\' {
  444. // escaped '\\'
  445. if len(s) >= 2 && s[1] == '\\' {
  446. result = append(result, '\\')
  447. s = s[2:]
  448. continue
  449. }
  450. // '\\' followed by an octal number
  451. if len(s) < 4 {
  452. errorf("invalid bytea sequence %v", s)
  453. }
  454. r, err := strconv.ParseInt(string(s[1:4]), 8, 9)
  455. if err != nil {
  456. errorf("could not parse bytea value: %s", err.Error())
  457. }
  458. result = append(result, byte(r))
  459. s = s[4:]
  460. } else {
  461. // We hit an unescaped, raw byte. Try to read in as many as
  462. // possible in one go.
  463. i := bytes.IndexByte(s, '\\')
  464. if i == -1 {
  465. result = append(result, s...)
  466. break
  467. }
  468. result = append(result, s[:i]...)
  469. s = s[i:]
  470. }
  471. }
  472. }
  473. return result
  474. }
  475. func encodeBytea(serverVersion int, v []byte) (result []byte) {
  476. if serverVersion >= 90000 {
  477. // Use the hex format if we know that the server supports it
  478. result = make([]byte, 2+hex.EncodedLen(len(v)))
  479. result[0] = '\\'
  480. result[1] = 'x'
  481. hex.Encode(result[2:], v)
  482. } else {
  483. // .. or resort to "escape"
  484. for _, b := range v {
  485. if b == '\\' {
  486. result = append(result, '\\', '\\')
  487. } else if b < 0x20 || b > 0x7e {
  488. result = append(result, []byte(fmt.Sprintf("\\%03o", b))...)
  489. } else {
  490. result = append(result, b)
  491. }
  492. }
  493. }
  494. return result
  495. }
  496. // NullTime represents a time.Time that may be null. NullTime implements the
  497. // sql.Scanner interface so it can be used as a scan destination, similar to
  498. // sql.NullString.
  499. type NullTime struct {
  500. Time time.Time
  501. Valid bool // Valid is true if Time is not NULL
  502. }
  503. // Scan implements the Scanner interface.
  504. func (nt *NullTime) Scan(value interface{}) error {
  505. nt.Time, nt.Valid = value.(time.Time)
  506. return nil
  507. }
  508. // Value implements the driver Valuer interface.
  509. func (nt NullTime) Value() (driver.Value, error) {
  510. if !nt.Valid {
  511. return nil, nil
  512. }
  513. return nt.Time, nil
  514. }