encode.go 16 KB

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