encode.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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. var hour, minute, second int
  332. if len(str) > monSep+len("01-01")+1 {
  333. p.expect(str, ' ', timeSep)
  334. minSep := timeSep + 3
  335. p.expect(str, ':', minSep)
  336. hour = p.mustAtoi(str, timeSep+1, minSep)
  337. secSep := minSep + 3
  338. p.expect(str, ':', secSep)
  339. minute = p.mustAtoi(str, minSep+1, secSep)
  340. secEnd := secSep + 3
  341. second = p.mustAtoi(str, secSep+1, secEnd)
  342. }
  343. remainderIdx := monSep + len("01-01 00:00:00") + 1
  344. // Three optional (but ordered) sections follow: the
  345. // fractional seconds, the time zone offset, and the BC
  346. // designation. We set them up here and adjust the other
  347. // offsets if the preceding sections exist.
  348. nanoSec := 0
  349. tzOff := 0
  350. if remainderIdx < len(str) && str[remainderIdx] == '.' {
  351. fracStart := remainderIdx + 1
  352. fracOff := strings.IndexAny(str[fracStart:], "-+ ")
  353. if fracOff < 0 {
  354. fracOff = len(str) - fracStart
  355. }
  356. fracSec := p.mustAtoi(str, fracStart, fracStart+fracOff)
  357. nanoSec = fracSec * (1000000000 / int(math.Pow(10, float64(fracOff))))
  358. remainderIdx += fracOff + 1
  359. }
  360. if tzStart := remainderIdx; tzStart < len(str) && (str[tzStart] == '-' || str[tzStart] == '+') {
  361. // time zone separator is always '-' or '+' (UTC is +00)
  362. var tzSign int
  363. switch c := str[tzStart]; c {
  364. case '-':
  365. tzSign = -1
  366. case '+':
  367. tzSign = +1
  368. default:
  369. return time.Time{}, fmt.Errorf("expected '-' or '+' at position %v; got %v", tzStart, c)
  370. }
  371. tzHours := p.mustAtoi(str, tzStart+1, tzStart+3)
  372. remainderIdx += 3
  373. var tzMin, tzSec int
  374. if remainderIdx < len(str) && str[remainderIdx] == ':' {
  375. tzMin = p.mustAtoi(str, remainderIdx+1, remainderIdx+3)
  376. remainderIdx += 3
  377. }
  378. if remainderIdx < len(str) && str[remainderIdx] == ':' {
  379. tzSec = p.mustAtoi(str, remainderIdx+1, remainderIdx+3)
  380. remainderIdx += 3
  381. }
  382. tzOff = tzSign * ((tzHours * 60 * 60) + (tzMin * 60) + tzSec)
  383. }
  384. var isoYear int
  385. if remainderIdx+3 <= len(str) && str[remainderIdx:remainderIdx+3] == " BC" {
  386. isoYear = 1 - year
  387. remainderIdx += 3
  388. } else {
  389. isoYear = year
  390. }
  391. if remainderIdx < len(str) {
  392. return time.Time{}, fmt.Errorf("expected end of input, got %v", str[remainderIdx:])
  393. }
  394. t := time.Date(isoYear, time.Month(month), day,
  395. hour, minute, second, nanoSec,
  396. globalLocationCache.getLocation(tzOff))
  397. if currentLocation != nil {
  398. // Set the location of the returned Time based on the session's
  399. // TimeZone value, but only if the local time zone database agrees with
  400. // the remote database on the offset.
  401. lt := t.In(currentLocation)
  402. _, newOff := lt.Zone()
  403. if newOff == tzOff {
  404. t = lt
  405. }
  406. }
  407. return t, p.err
  408. }
  409. // formatTs formats t into a format postgres understands.
  410. func formatTs(t time.Time) []byte {
  411. if infinityTsEnabled {
  412. // t <= -infinity : ! (t > -infinity)
  413. if !t.After(infinityTsNegative) {
  414. return []byte("-infinity")
  415. }
  416. // t >= infinity : ! (!t < infinity)
  417. if !t.Before(infinityTsPositive) {
  418. return []byte("infinity")
  419. }
  420. }
  421. return FormatTimestamp(t)
  422. }
  423. // FormatTimestamp formats t into Postgres' text format for timestamps.
  424. func FormatTimestamp(t time.Time) []byte {
  425. // Need to send dates before 0001 A.D. with " BC" suffix, instead of the
  426. // minus sign preferred by Go.
  427. // Beware, "0000" in ISO is "1 BC", "-0001" is "2 BC" and so on
  428. bc := false
  429. if t.Year() <= 0 {
  430. // flip year sign, and add 1, e.g: "0" will be "1", and "-10" will be "11"
  431. t = t.AddDate((-t.Year())*2+1, 0, 0)
  432. bc = true
  433. }
  434. b := []byte(t.Format("2006-01-02 15:04:05.999999999Z07:00"))
  435. _, offset := t.Zone()
  436. offset = offset % 60
  437. if offset != 0 {
  438. // RFC3339Nano already printed the minus sign
  439. if offset < 0 {
  440. offset = -offset
  441. }
  442. b = append(b, ':')
  443. if offset < 10 {
  444. b = append(b, '0')
  445. }
  446. b = strconv.AppendInt(b, int64(offset), 10)
  447. }
  448. if bc {
  449. b = append(b, " BC"...)
  450. }
  451. return b
  452. }
  453. // Parse a bytea value received from the server. Both "hex" and the legacy
  454. // "escape" format are supported.
  455. func parseBytea(s []byte) (result []byte, err error) {
  456. if len(s) >= 2 && bytes.Equal(s[:2], []byte("\\x")) {
  457. // bytea_output = hex
  458. s = s[2:] // trim off leading "\\x"
  459. result = make([]byte, hex.DecodedLen(len(s)))
  460. _, err := hex.Decode(result, s)
  461. if err != nil {
  462. return nil, err
  463. }
  464. } else {
  465. // bytea_output = escape
  466. for len(s) > 0 {
  467. if s[0] == '\\' {
  468. // escaped '\\'
  469. if len(s) >= 2 && s[1] == '\\' {
  470. result = append(result, '\\')
  471. s = s[2:]
  472. continue
  473. }
  474. // '\\' followed by an octal number
  475. if len(s) < 4 {
  476. return nil, fmt.Errorf("invalid bytea sequence %v", s)
  477. }
  478. r, err := strconv.ParseInt(string(s[1:4]), 8, 9)
  479. if err != nil {
  480. return nil, fmt.Errorf("could not parse bytea value: %s", err.Error())
  481. }
  482. result = append(result, byte(r))
  483. s = s[4:]
  484. } else {
  485. // We hit an unescaped, raw byte. Try to read in as many as
  486. // possible in one go.
  487. i := bytes.IndexByte(s, '\\')
  488. if i == -1 {
  489. result = append(result, s...)
  490. break
  491. }
  492. result = append(result, s[:i]...)
  493. s = s[i:]
  494. }
  495. }
  496. }
  497. return result, nil
  498. }
  499. func encodeBytea(serverVersion int, v []byte) (result []byte) {
  500. if serverVersion >= 90000 {
  501. // Use the hex format if we know that the server supports it
  502. result = make([]byte, 2+hex.EncodedLen(len(v)))
  503. result[0] = '\\'
  504. result[1] = 'x'
  505. hex.Encode(result[2:], v)
  506. } else {
  507. // .. or resort to "escape"
  508. for _, b := range v {
  509. if b == '\\' {
  510. result = append(result, '\\', '\\')
  511. } else if b < 0x20 || b > 0x7e {
  512. result = append(result, []byte(fmt.Sprintf("\\%03o", b))...)
  513. } else {
  514. result = append(result, b)
  515. }
  516. }
  517. }
  518. return result
  519. }
  520. // NullTime represents a time.Time that may be null. NullTime implements the
  521. // sql.Scanner interface so it can be used as a scan destination, similar to
  522. // sql.NullString.
  523. type NullTime struct {
  524. Time time.Time
  525. Valid bool // Valid is true if Time is not NULL
  526. }
  527. // Scan implements the Scanner interface.
  528. func (nt *NullTime) Scan(value interface{}) error {
  529. nt.Time, nt.Valid = value.(time.Time)
  530. return nil
  531. }
  532. // Value implements the driver Valuer interface.
  533. func (nt NullTime) Value() (driver.Value, error) {
  534. if !nt.Valid {
  535. return nil, nil
  536. }
  537. return nt.Time, nil
  538. }