encode.go 15 KB

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