encode.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  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 30-minute-offset timezone
  182. if (typ == oid.T_timestamptz || typ == oid.T_timetz) &&
  183. str[len(str)-3] == ':' {
  184. f += ":00"
  185. }
  186. // Special case for 24:00 time.
  187. // Unfortunately, golang does not parse 24:00 as a proper time.
  188. // In this case, we want to try "round to the next day", to differentiate.
  189. // As such, we find if the 24:00 time matches at the beginning; if so,
  190. // we default it back to 00:00 but add a day later.
  191. var is2400Time bool
  192. switch typ {
  193. case oid.T_timetz, oid.T_time:
  194. if matches := time2400Regex.FindStringSubmatch(str); matches != nil {
  195. // Concatenate timezone information at the back.
  196. str = "00:00:00" + str[len(matches[1]):]
  197. is2400Time = true
  198. }
  199. }
  200. t, err := time.Parse(f, str)
  201. if err != nil {
  202. errorf("decode: %s", err)
  203. }
  204. if is2400Time {
  205. t = t.Add(24 * time.Hour)
  206. }
  207. return t
  208. }
  209. var errInvalidTimestamp = errors.New("invalid timestamp")
  210. type timestampParser struct {
  211. err error
  212. }
  213. func (p *timestampParser) expect(str string, char byte, pos int) {
  214. if p.err != nil {
  215. return
  216. }
  217. if pos+1 > len(str) {
  218. p.err = errInvalidTimestamp
  219. return
  220. }
  221. if c := str[pos]; c != char && p.err == nil {
  222. p.err = fmt.Errorf("expected '%v' at position %v; got '%v'", char, pos, c)
  223. }
  224. }
  225. func (p *timestampParser) mustAtoi(str string, begin int, end int) int {
  226. if p.err != nil {
  227. return 0
  228. }
  229. if begin < 0 || end < 0 || begin > end || end > len(str) {
  230. p.err = errInvalidTimestamp
  231. return 0
  232. }
  233. result, err := strconv.Atoi(str[begin:end])
  234. if err != nil {
  235. if p.err == nil {
  236. p.err = fmt.Errorf("expected number; got '%v'", str)
  237. }
  238. return 0
  239. }
  240. return result
  241. }
  242. // The location cache caches the time zones typically used by the client.
  243. type locationCache struct {
  244. cache map[int]*time.Location
  245. lock sync.Mutex
  246. }
  247. // All connections share the same list of timezones. Benchmarking shows that
  248. // about 5% speed could be gained by putting the cache in the connection and
  249. // losing the mutex, at the cost of a small amount of memory and a somewhat
  250. // significant increase in code complexity.
  251. var globalLocationCache = newLocationCache()
  252. func newLocationCache() *locationCache {
  253. return &locationCache{cache: make(map[int]*time.Location)}
  254. }
  255. // Returns the cached timezone for the specified offset, creating and caching
  256. // it if necessary.
  257. func (c *locationCache) getLocation(offset int) *time.Location {
  258. c.lock.Lock()
  259. defer c.lock.Unlock()
  260. location, ok := c.cache[offset]
  261. if !ok {
  262. location = time.FixedZone("", offset)
  263. c.cache[offset] = location
  264. }
  265. return location
  266. }
  267. var infinityTsEnabled = false
  268. var infinityTsNegative time.Time
  269. var infinityTsPositive time.Time
  270. const (
  271. infinityTsEnabledAlready = "pq: infinity timestamp enabled already"
  272. infinityTsNegativeMustBeSmaller = "pq: infinity timestamp: negative value must be smaller (before) than positive"
  273. )
  274. // EnableInfinityTs controls the handling of Postgres' "-infinity" and
  275. // "infinity" "timestamp"s.
  276. //
  277. // If EnableInfinityTs is not called, "-infinity" and "infinity" will return
  278. // []byte("-infinity") and []byte("infinity") respectively, and potentially
  279. // cause error "sql: Scan error on column index 0: unsupported driver -> Scan
  280. // pair: []uint8 -> *time.Time", when scanning into a time.Time value.
  281. //
  282. // Once EnableInfinityTs has been called, all connections created using this
  283. // driver will decode Postgres' "-infinity" and "infinity" for "timestamp",
  284. // "timestamp with time zone" and "date" types to the predefined minimum and
  285. // maximum times, respectively. When encoding time.Time values, any time which
  286. // equals or precedes the predefined minimum time will be encoded to
  287. // "-infinity". Any values at or past the maximum time will similarly be
  288. // encoded to "infinity".
  289. //
  290. // If EnableInfinityTs is called with negative >= positive, it will panic.
  291. // Calling EnableInfinityTs after a connection has been established results in
  292. // undefined behavior. If EnableInfinityTs is called more than once, it will
  293. // panic.
  294. func EnableInfinityTs(negative time.Time, positive time.Time) {
  295. if infinityTsEnabled {
  296. panic(infinityTsEnabledAlready)
  297. }
  298. if !negative.Before(positive) {
  299. panic(infinityTsNegativeMustBeSmaller)
  300. }
  301. infinityTsEnabled = true
  302. infinityTsNegative = negative
  303. infinityTsPositive = positive
  304. }
  305. /*
  306. * Testing might want to toggle infinityTsEnabled
  307. */
  308. func disableInfinityTs() {
  309. infinityTsEnabled = false
  310. }
  311. // This is a time function specific to the Postgres default DateStyle
  312. // setting ("ISO, MDY"), the only one we currently support. This
  313. // accounts for the discrepancies between the parsing available with
  314. // time.Parse and the Postgres date formatting quirks.
  315. func parseTs(currentLocation *time.Location, str string) interface{} {
  316. switch str {
  317. case "-infinity":
  318. if infinityTsEnabled {
  319. return infinityTsNegative
  320. }
  321. return []byte(str)
  322. case "infinity":
  323. if infinityTsEnabled {
  324. return infinityTsPositive
  325. }
  326. return []byte(str)
  327. }
  328. t, err := ParseTimestamp(currentLocation, str)
  329. if err != nil {
  330. panic(err)
  331. }
  332. return t
  333. }
  334. // ParseTimestamp parses Postgres' text format. It returns a time.Time in
  335. // currentLocation iff that time's offset agrees with the offset sent from the
  336. // Postgres server. Otherwise, ParseTimestamp returns a time.Time with the
  337. // fixed offset offset provided by the Postgres server.
  338. func ParseTimestamp(currentLocation *time.Location, str string) (time.Time, error) {
  339. p := timestampParser{}
  340. monSep := strings.IndexRune(str, '-')
  341. // this is Gregorian year, not ISO Year
  342. // In Gregorian system, the year 1 BC is followed by AD 1
  343. year := p.mustAtoi(str, 0, monSep)
  344. daySep := monSep + 3
  345. month := p.mustAtoi(str, monSep+1, daySep)
  346. p.expect(str, '-', daySep)
  347. timeSep := daySep + 3
  348. day := p.mustAtoi(str, daySep+1, timeSep)
  349. minLen := monSep + len("01-01") + 1
  350. isBC := strings.HasSuffix(str, " BC")
  351. if isBC {
  352. minLen += 3
  353. }
  354. var hour, minute, second int
  355. if len(str) > minLen {
  356. p.expect(str, ' ', timeSep)
  357. minSep := timeSep + 3
  358. p.expect(str, ':', minSep)
  359. hour = p.mustAtoi(str, timeSep+1, minSep)
  360. secSep := minSep + 3
  361. p.expect(str, ':', secSep)
  362. minute = p.mustAtoi(str, minSep+1, secSep)
  363. secEnd := secSep + 3
  364. second = p.mustAtoi(str, secSep+1, secEnd)
  365. }
  366. remainderIdx := monSep + len("01-01 00:00:00") + 1
  367. // Three optional (but ordered) sections follow: the
  368. // fractional seconds, the time zone offset, and the BC
  369. // designation. We set them up here and adjust the other
  370. // offsets if the preceding sections exist.
  371. nanoSec := 0
  372. tzOff := 0
  373. if remainderIdx < len(str) && str[remainderIdx] == '.' {
  374. fracStart := remainderIdx + 1
  375. fracOff := strings.IndexAny(str[fracStart:], "-+ ")
  376. if fracOff < 0 {
  377. fracOff = len(str) - fracStart
  378. }
  379. fracSec := p.mustAtoi(str, fracStart, fracStart+fracOff)
  380. nanoSec = fracSec * (1000000000 / int(math.Pow(10, float64(fracOff))))
  381. remainderIdx += fracOff + 1
  382. }
  383. if tzStart := remainderIdx; tzStart < len(str) && (str[tzStart] == '-' || str[tzStart] == '+') {
  384. // time zone separator is always '-' or '+' (UTC is +00)
  385. var tzSign int
  386. switch c := str[tzStart]; c {
  387. case '-':
  388. tzSign = -1
  389. case '+':
  390. tzSign = +1
  391. default:
  392. return time.Time{}, fmt.Errorf("expected '-' or '+' at position %v; got %v", tzStart, c)
  393. }
  394. tzHours := p.mustAtoi(str, tzStart+1, tzStart+3)
  395. remainderIdx += 3
  396. var tzMin, tzSec int
  397. if remainderIdx < len(str) && str[remainderIdx] == ':' {
  398. tzMin = p.mustAtoi(str, remainderIdx+1, remainderIdx+3)
  399. remainderIdx += 3
  400. }
  401. if remainderIdx < len(str) && str[remainderIdx] == ':' {
  402. tzSec = p.mustAtoi(str, remainderIdx+1, remainderIdx+3)
  403. remainderIdx += 3
  404. }
  405. tzOff = tzSign * ((tzHours * 60 * 60) + (tzMin * 60) + tzSec)
  406. }
  407. var isoYear int
  408. if isBC {
  409. isoYear = 1 - year
  410. remainderIdx += 3
  411. } else {
  412. isoYear = year
  413. }
  414. if remainderIdx < len(str) {
  415. return time.Time{}, fmt.Errorf("expected end of input, got %v", str[remainderIdx:])
  416. }
  417. t := time.Date(isoYear, time.Month(month), day,
  418. hour, minute, second, nanoSec,
  419. globalLocationCache.getLocation(tzOff))
  420. if currentLocation != nil {
  421. // Set the location of the returned Time based on the session's
  422. // TimeZone value, but only if the local time zone database agrees with
  423. // the remote database on the offset.
  424. lt := t.In(currentLocation)
  425. _, newOff := lt.Zone()
  426. if newOff == tzOff {
  427. t = lt
  428. }
  429. }
  430. return t, p.err
  431. }
  432. // formatTs formats t into a format postgres understands.
  433. func formatTs(t time.Time) []byte {
  434. if infinityTsEnabled {
  435. // t <= -infinity : ! (t > -infinity)
  436. if !t.After(infinityTsNegative) {
  437. return []byte("-infinity")
  438. }
  439. // t >= infinity : ! (!t < infinity)
  440. if !t.Before(infinityTsPositive) {
  441. return []byte("infinity")
  442. }
  443. }
  444. return FormatTimestamp(t)
  445. }
  446. // FormatTimestamp formats t into Postgres' text format for timestamps.
  447. func FormatTimestamp(t time.Time) []byte {
  448. // Need to send dates before 0001 A.D. with " BC" suffix, instead of the
  449. // minus sign preferred by Go.
  450. // Beware, "0000" in ISO is "1 BC", "-0001" is "2 BC" and so on
  451. bc := false
  452. if t.Year() <= 0 {
  453. // flip year sign, and add 1, e.g: "0" will be "1", and "-10" will be "11"
  454. t = t.AddDate((-t.Year())*2+1, 0, 0)
  455. bc = true
  456. }
  457. b := []byte(t.Format("2006-01-02 15:04:05.999999999Z07:00"))
  458. _, offset := t.Zone()
  459. offset %= 60
  460. if offset != 0 {
  461. // RFC3339Nano already printed the minus sign
  462. if offset < 0 {
  463. offset = -offset
  464. }
  465. b = append(b, ':')
  466. if offset < 10 {
  467. b = append(b, '0')
  468. }
  469. b = strconv.AppendInt(b, int64(offset), 10)
  470. }
  471. if bc {
  472. b = append(b, " BC"...)
  473. }
  474. return b
  475. }
  476. // Parse a bytea value received from the server. Both "hex" and the legacy
  477. // "escape" format are supported.
  478. func parseBytea(s []byte) (result []byte, err error) {
  479. if len(s) >= 2 && bytes.Equal(s[:2], []byte("\\x")) {
  480. // bytea_output = hex
  481. s = s[2:] // trim off leading "\\x"
  482. result = make([]byte, hex.DecodedLen(len(s)))
  483. _, err := hex.Decode(result, s)
  484. if err != nil {
  485. return nil, err
  486. }
  487. } else {
  488. // bytea_output = escape
  489. for len(s) > 0 {
  490. if s[0] == '\\' {
  491. // escaped '\\'
  492. if len(s) >= 2 && s[1] == '\\' {
  493. result = append(result, '\\')
  494. s = s[2:]
  495. continue
  496. }
  497. // '\\' followed by an octal number
  498. if len(s) < 4 {
  499. return nil, fmt.Errorf("invalid bytea sequence %v", s)
  500. }
  501. r, err := strconv.ParseInt(string(s[1:4]), 8, 9)
  502. if err != nil {
  503. return nil, fmt.Errorf("could not parse bytea value: %s", err.Error())
  504. }
  505. result = append(result, byte(r))
  506. s = s[4:]
  507. } else {
  508. // We hit an unescaped, raw byte. Try to read in as many as
  509. // possible in one go.
  510. i := bytes.IndexByte(s, '\\')
  511. if i == -1 {
  512. result = append(result, s...)
  513. break
  514. }
  515. result = append(result, s[:i]...)
  516. s = s[i:]
  517. }
  518. }
  519. }
  520. return result, nil
  521. }
  522. func encodeBytea(serverVersion int, v []byte) (result []byte) {
  523. if serverVersion >= 90000 {
  524. // Use the hex format if we know that the server supports it
  525. result = make([]byte, 2+hex.EncodedLen(len(v)))
  526. result[0] = '\\'
  527. result[1] = 'x'
  528. hex.Encode(result[2:], v)
  529. } else {
  530. // .. or resort to "escape"
  531. for _, b := range v {
  532. if b == '\\' {
  533. result = append(result, '\\', '\\')
  534. } else if b < 0x20 || b > 0x7e {
  535. result = append(result, []byte(fmt.Sprintf("\\%03o", b))...)
  536. } else {
  537. result = append(result, b)
  538. }
  539. }
  540. }
  541. return result
  542. }
  543. // NullTime represents a time.Time that may be null. NullTime implements the
  544. // sql.Scanner interface so it can be used as a scan destination, similar to
  545. // sql.NullString.
  546. type NullTime struct {
  547. Time time.Time
  548. Valid bool // Valid is true if Time is not NULL
  549. }
  550. // Scan implements the Scanner interface.
  551. func (nt *NullTime) Scan(value interface{}) error {
  552. nt.Time, nt.Valid = value.(time.Time)
  553. return nil
  554. }
  555. // Value implements the driver Valuer interface.
  556. func (nt NullTime) Value() (driver.Value, error) {
  557. if !nt.Valid {
  558. return nil, nil
  559. }
  560. return nt.Time, nil
  561. }