|
| 1 | +import { Oid } from "./oid.ts"; |
| 2 | +import { Column, Format } from "./connection.ts"; |
| 3 | + |
| 4 | + |
| 5 | +// Datetime parsing based on: |
| 6 | +// https://github.com/bendrucker/postgres-date/blob/master/index.js |
| 7 | +const DATETIME_RE = /^(\d{1,})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})(\.\d{1,})?/; |
| 8 | +const DATE_RE = /^(\d{1,})-(\d{2})-(\d{2})$/; |
| 9 | +const TIMEZONE_RE = /([Z+-])(\d{2})?:?(\d{2})?:?(\d{2})?/; |
| 10 | +const BC_RE = /BC$/; |
| 11 | + |
| 12 | +function decodeDate(dateStr: string): null | Date { |
| 13 | + const matches = DATE_RE.exec(dateStr); |
| 14 | + |
| 15 | + if (!matches) { |
| 16 | + return null; |
| 17 | + } |
| 18 | + |
| 19 | + const year = parseInt(matches[1], 10); |
| 20 | + // remember JS dates are 0-based |
| 21 | + const month = parseInt(matches[2], 10) - 1; |
| 22 | + const day = parseInt(matches[3], 10); |
| 23 | + const date = new Date(year, month, day); |
| 24 | + // use `setUTCFullYear` because if date is from first |
| 25 | + // century `Date`'s compatibility for millenium bug |
| 26 | + // would set it as 19XX |
| 27 | + date.setUTCFullYear(year); |
| 28 | + |
| 29 | + return date; |
| 30 | +} |
| 31 | +/** |
| 32 | + * Decode numerical timezone offset from provided date string. |
| 33 | + * |
| 34 | + * Matched these kinds: |
| 35 | + * - `Z (UTC)` |
| 36 | + * - `-05` |
| 37 | + * - `+06:30` |
| 38 | + * - `+06:30:10` |
| 39 | + * |
| 40 | + * Returns offset in miliseconds. |
| 41 | + */ |
| 42 | +function decodeTimezoneOffset(dateStr: string): null | number { |
| 43 | + // get rid of date part as TIMEZONE_RE would match '-MM` part |
| 44 | + const timeStr = dateStr.split(' ')[1]; |
| 45 | + const matches = TIMEZONE_RE.exec(timeStr); |
| 46 | + |
| 47 | + if (!matches) { |
| 48 | + return null; |
| 49 | + } |
| 50 | + |
| 51 | + const type = matches[1]; |
| 52 | + |
| 53 | + if (type === "Z") { |
| 54 | + // Zulu timezone === UTC === 0 |
| 55 | + return 0; |
| 56 | + } |
| 57 | + |
| 58 | + // in JS timezone offsets are reversed, ie. timezones |
| 59 | + // that are "positive" (+01:00) are represented as negative |
| 60 | + // offsets and vice-versa |
| 61 | + const sign = type === '-' ? 1 : -1; |
| 62 | + |
| 63 | + const hours = parseInt(matches[2], 10); |
| 64 | + const minutes = parseInt(matches[3] || "0", 10); |
| 65 | + const seconds = parseInt(matches[4] || "0", 10); |
| 66 | + |
| 67 | + const offset = (hours * 3600) + (minutes * 60) + seconds; |
| 68 | + |
| 69 | + return sign * offset * 1000; |
| 70 | +} |
| 71 | + |
| 72 | +function decodeDatetime(dateStr: string): null | number | Date { |
| 73 | + /** |
| 74 | + * Postgres uses ISO 8601 style date output by default: |
| 75 | + * 1997-12-17 07:37:16-08 |
| 76 | + */ |
| 77 | + |
| 78 | + // there are special `infinity` and `-infinity` |
| 79 | + // cases representing out-of-range dates |
| 80 | + if (dateStr === 'infinity') { |
| 81 | + return Number(Infinity); |
| 82 | + } else if (dateStr === "-infinity") { |
| 83 | + return Number(-Infinity); |
| 84 | + } |
| 85 | + |
| 86 | + const matches = DATETIME_RE.exec(dateStr); |
| 87 | + |
| 88 | + if (!matches) { |
| 89 | + return decodeDate(dateStr); |
| 90 | + } |
| 91 | + |
| 92 | + const isBC = BC_RE.test(dateStr); |
| 93 | + |
| 94 | + |
| 95 | + const year = parseInt(matches[1], 10) * (isBC ? -1 : 1); |
| 96 | + // remember JS dates are 0-based |
| 97 | + const month = parseInt(matches[2], 10) - 1; |
| 98 | + const day = parseInt(matches[3], 10); |
| 99 | + const hour = parseInt(matches[4], 10); |
| 100 | + const minute = parseInt(matches[5], 10); |
| 101 | + const second = parseInt(matches[6], 10); |
| 102 | + // ms are written as .007 |
| 103 | + const msMatch = matches[7]; |
| 104 | + const ms = msMatch ? 1000 * parseFloat(msMatch) : 0; |
| 105 | + |
| 106 | + |
| 107 | + let date: Date; |
| 108 | + |
| 109 | + const offset = decodeTimezoneOffset(dateStr); |
| 110 | + if (offset === null) { |
| 111 | + date = new Date(year, month, day, hour, minute, second, ms); |
| 112 | + } else { |
| 113 | + // This returns miliseconds from 1 January, 1970, 00:00:00, |
| 114 | + // adding decoded timezone offset will construct proper date object. |
| 115 | + const utc = Date.UTC(year, month, day, hour, minute, second, ms); |
| 116 | + date = new Date(utc + offset); |
| 117 | + } |
| 118 | + |
| 119 | + // use `setUTCFullYear` because if date is from first |
| 120 | + // century `Date`'s compatibility for millenium bug |
| 121 | + // would set it as 19XX |
| 122 | + date.setUTCFullYear(year); |
| 123 | + return date; |
| 124 | +} |
| 125 | + |
| 126 | +function decodeBinary() { |
| 127 | + throw new Error("Not implemented!") |
| 128 | +} |
| 129 | + |
| 130 | +const decoder = new TextDecoder(); |
| 131 | + |
| 132 | +function decodeText(value: Uint8Array, typeOid: number): any { |
| 133 | + const strValue = decoder.decode(value); |
| 134 | + |
| 135 | + switch (typeOid) { |
| 136 | + case Oid.char: |
| 137 | + case Oid.varchar: |
| 138 | + case Oid.text: |
| 139 | + case Oid.time: |
| 140 | + case Oid.timetz: |
| 141 | + return strValue; |
| 142 | + case Oid.bool: |
| 143 | + return strValue[0] === "t"; |
| 144 | + case Oid.int2: |
| 145 | + case Oid.int4: |
| 146 | + case Oid.int8: |
| 147 | + return parseInt(strValue, 10); |
| 148 | + case Oid.float4: |
| 149 | + case Oid.float8: |
| 150 | + return parseFloat(strValue); |
| 151 | + case Oid.timestamptz: |
| 152 | + case Oid.timestamp: |
| 153 | + return decodeDatetime(strValue); |
| 154 | + case Oid.date: |
| 155 | + return decodeDate(strValue); |
| 156 | + default: |
| 157 | + throw new Error(`Don't know how to parse column type: ${typeOid}`); |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +export function decode(value: Uint8Array, column: Column) { |
| 162 | + if (column.format === Format.BINARY) { |
| 163 | + return decodeBinary(); |
| 164 | + } else if (column.format === Format.TEXT) { |
| 165 | + return decodeText(value, column.typeOid); |
| 166 | + } else { |
| 167 | + throw new Error(`Unknown column format: ${column.format}`); |
| 168 | + } |
| 169 | +} |
0 commit comments