PageRenderTime 27ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/ssi/vendor/github.com/lib/pq/encode.go

https://gitlab.com/nidhomriza/handbook-tes
Go | 628 lines | 480 code | 62 blank | 86 comment | 131 complexity | 640a476e0b3bb45317e71c4e3713910e MD5 | raw file
  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:], "-+ ")
  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 '+' (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. }
  412. var isoYear int
  413. if isBC {
  414. isoYear = 1 - year
  415. remainderIdx += 3
  416. } else {
  417. isoYear = year
  418. }
  419. if remainderIdx < len(str) {
  420. return time.Time{}, fmt.Errorf("expected end of input, got %v", str[remainderIdx:])
  421. }
  422. t := time.Date(isoYear, time.Month(month), day,
  423. hour, minute, second, nanoSec,
  424. globalLocationCache.getLocation(tzOff))
  425. if currentLocation != nil {
  426. // Set the location of the returned Time based on the session's
  427. // TimeZone value, but only if the local time zone database agrees with
  428. // the remote database on the offset.
  429. lt := t.In(currentLocation)
  430. _, newOff := lt.Zone()
  431. if newOff == tzOff {
  432. t = lt
  433. }
  434. }
  435. return t, p.err
  436. }
  437. // formatTs formats t into a format postgres understands.
  438. func formatTs(t time.Time) []byte {
  439. if infinityTsEnabled {
  440. // t <= -infinity : ! (t > -infinity)
  441. if !t.After(infinityTsNegative) {
  442. return []byte("-infinity")
  443. }
  444. // t >= infinity : ! (!t < infinity)
  445. if !t.Before(infinityTsPositive) {
  446. return []byte("infinity")
  447. }
  448. }
  449. return FormatTimestamp(t)
  450. }
  451. // FormatTimestamp formats t into Postgres' text format for timestamps.
  452. func FormatTimestamp(t time.Time) []byte {
  453. // Need to send dates before 0001 A.D. with " BC" suffix, instead of the
  454. // minus sign preferred by Go.
  455. // Beware, "0000" in ISO is "1 BC", "-0001" is "2 BC" and so on
  456. bc := false
  457. if t.Year() <= 0 {
  458. // flip year sign, and add 1, e.g: "0" will be "1", and "-10" will be "11"
  459. t = t.AddDate((-t.Year())*2+1, 0, 0)
  460. bc = true
  461. }
  462. b := []byte(t.Format("2006-01-02 15:04:05.999999999Z07:00"))
  463. _, offset := t.Zone()
  464. offset %= 60
  465. if offset != 0 {
  466. // RFC3339Nano already printed the minus sign
  467. if offset < 0 {
  468. offset = -offset
  469. }
  470. b = append(b, ':')
  471. if offset < 10 {
  472. b = append(b, '0')
  473. }
  474. b = strconv.AppendInt(b, int64(offset), 10)
  475. }
  476. if bc {
  477. b = append(b, " BC"...)
  478. }
  479. return b
  480. }
  481. // Parse a bytea value received from the server. Both "hex" and the legacy
  482. // "escape" format are supported.
  483. func parseBytea(s []byte) (result []byte, err error) {
  484. if len(s) >= 2 && bytes.Equal(s[:2], []byte("\\x")) {
  485. // bytea_output = hex
  486. s = s[2:] // trim off leading "\\x"
  487. result = make([]byte, hex.DecodedLen(len(s)))
  488. _, err := hex.Decode(result, s)
  489. if err != nil {
  490. return nil, err
  491. }
  492. } else {
  493. // bytea_output = escape
  494. for len(s) > 0 {
  495. if s[0] == '\\' {
  496. // escaped '\\'
  497. if len(s) >= 2 && s[1] == '\\' {
  498. result = append(result, '\\')
  499. s = s[2:]
  500. continue
  501. }
  502. // '\\' followed by an octal number
  503. if len(s) < 4 {
  504. return nil, fmt.Errorf("invalid bytea sequence %v", s)
  505. }
  506. r, err := strconv.ParseUint(string(s[1:4]), 8, 8)
  507. if err != nil {
  508. return nil, fmt.Errorf("could not parse bytea value: %s", err.Error())
  509. }
  510. result = append(result, byte(r))
  511. s = s[4:]
  512. } else {
  513. // We hit an unescaped, raw byte. Try to read in as many as
  514. // possible in one go.
  515. i := bytes.IndexByte(s, '\\')
  516. if i == -1 {
  517. result = append(result, s...)
  518. break
  519. }
  520. result = append(result, s[:i]...)
  521. s = s[i:]
  522. }
  523. }
  524. }
  525. return result, nil
  526. }
  527. func encodeBytea(serverVersion int, v []byte) (result []byte) {
  528. if serverVersion >= 90000 {
  529. // Use the hex format if we know that the server supports it
  530. result = make([]byte, 2+hex.EncodedLen(len(v)))
  531. result[0] = '\\'
  532. result[1] = 'x'
  533. hex.Encode(result[2:], v)
  534. } else {
  535. // .. or resort to "escape"
  536. for _, b := range v {
  537. if b == '\\' {
  538. result = append(result, '\\', '\\')
  539. } else if b < 0x20 || b > 0x7e {
  540. result = append(result, []byte(fmt.Sprintf("\\%03o", b))...)
  541. } else {
  542. result = append(result, b)
  543. }
  544. }
  545. }
  546. return result
  547. }
  548. // NullTime represents a time.Time that may be null. NullTime implements the
  549. // sql.Scanner interface so it can be used as a scan destination, similar to
  550. // sql.NullString.
  551. type NullTime struct {
  552. Time time.Time
  553. Valid bool // Valid is true if Time is not NULL
  554. }
  555. // Scan implements the Scanner interface.
  556. func (nt *NullTime) Scan(value interface{}) error {
  557. nt.Time, nt.Valid = value.(time.Time)
  558. return nil
  559. }
  560. // Value implements the driver Valuer interface.
  561. func (nt NullTime) Value() (driver.Value, error) {
  562. if !nt.Valid {
  563. return nil, nil
  564. }
  565. return nt.Time, nil
  566. }