postgrejs 2.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (215) hide show
  1. package/CHANGELOG.md +814 -0
  2. package/LICENSE +21 -0
  3. package/README.md +200 -0
  4. package/cjs/connection/bind-param.js +10 -0
  5. package/cjs/connection/connection.js +279 -0
  6. package/cjs/connection/cursor.js +93 -0
  7. package/cjs/connection/intl-connection.js +243 -0
  8. package/cjs/connection/pool.js +226 -0
  9. package/cjs/connection/portal.js +148 -0
  10. package/cjs/connection/prepared-statement.js +217 -0
  11. package/cjs/constants.js +250 -0
  12. package/cjs/data-type-map.js +119 -0
  13. package/cjs/data-types/bool-type.js +27 -0
  14. package/cjs/data-types/box-type.js +52 -0
  15. package/cjs/data-types/bytea-type.js +29 -0
  16. package/cjs/data-types/char-type.js +27 -0
  17. package/cjs/data-types/circle-type.js +48 -0
  18. package/cjs/data-types/date-type.js +67 -0
  19. package/cjs/data-types/float4-type.js +25 -0
  20. package/cjs/data-types/float8-type.js +25 -0
  21. package/cjs/data-types/int2-type.js +26 -0
  22. package/cjs/data-types/int2-vector-type.js +38 -0
  23. package/cjs/data-types/int4-type.js +26 -0
  24. package/cjs/data-types/int8-type.js +31 -0
  25. package/cjs/data-types/json-type.js +38 -0
  26. package/cjs/data-types/jsonb-type.js +42 -0
  27. package/cjs/data-types/lseg-type.js +52 -0
  28. package/cjs/data-types/numeric-type.js +125 -0
  29. package/cjs/data-types/oid-type.js +32 -0
  30. package/cjs/data-types/point-type.js +39 -0
  31. package/cjs/data-types/time-type.js +54 -0
  32. package/cjs/data-types/timestamp-type.js +81 -0
  33. package/cjs/data-types/timestamptz-type.js +73 -0
  34. package/cjs/data-types/uuid-type.js +39 -0
  35. package/cjs/data-types/varchar-type.js +27 -0
  36. package/cjs/index.js +41 -0
  37. package/cjs/interfaces/command-result.js +2 -0
  38. package/cjs/interfaces/data-mapping-options.js +2 -0
  39. package/cjs/interfaces/data-type.js +2 -0
  40. package/cjs/interfaces/database-connection-params.js +2 -0
  41. package/cjs/interfaces/field-info.js +2 -0
  42. package/cjs/interfaces/query-options.js +2 -0
  43. package/cjs/interfaces/query-result.js +2 -0
  44. package/cjs/interfaces/script-execute-options.js +2 -0
  45. package/cjs/interfaces/script-result.js +2 -0
  46. package/cjs/interfaces/statement-prepare-options.js +2 -0
  47. package/cjs/package.json +3 -0
  48. package/cjs/protocol/backend.js +256 -0
  49. package/cjs/protocol/buffer-reader.js +78 -0
  50. package/cjs/protocol/database-error.js +17 -0
  51. package/cjs/protocol/frontend.js +218 -0
  52. package/cjs/protocol/pg-socket.js +345 -0
  53. package/cjs/protocol/protocol.js +71 -0
  54. package/cjs/protocol/sasl.js +108 -0
  55. package/cjs/protocol/smart-buffer.js +195 -0
  56. package/cjs/safe-event-emitter.js +17 -0
  57. package/cjs/types.js +2 -0
  58. package/cjs/util/array-calculatedim.js +19 -0
  59. package/cjs/util/bigint-methods.js +42 -0
  60. package/cjs/util/config-from-env.js +30 -0
  61. package/cjs/util/connection-config.js +65 -0
  62. package/cjs/util/convert-row-to-object.js +13 -0
  63. package/cjs/util/decode-binaryarray.js +39 -0
  64. package/cjs/util/encode-binaryarray.js +45 -0
  65. package/cjs/util/escape-literal.js +27 -0
  66. package/cjs/util/fast-parseint.js +32 -0
  67. package/cjs/util/get-parsers.js +42 -0
  68. package/cjs/util/parse-array.js +61 -0
  69. package/cjs/util/parse-datetime.js +38 -0
  70. package/cjs/util/parse-row.js +11 -0
  71. package/cjs/util/parse-time.js +29 -0
  72. package/cjs/util/stringify-arrayliteral.js +38 -0
  73. package/cjs/util/stringify-for-sql.js +30 -0
  74. package/cjs/util/wrap-row-description.js +35 -0
  75. package/esm/connection/bind-param.js +6 -0
  76. package/esm/connection/connection.js +275 -0
  77. package/esm/connection/cursor.js +86 -0
  78. package/esm/connection/intl-connection.js +238 -0
  79. package/esm/connection/pool.js +222 -0
  80. package/esm/connection/portal.js +144 -0
  81. package/esm/connection/prepared-statement.js +213 -0
  82. package/esm/constants.js +247 -0
  83. package/esm/data-type-map.js +115 -0
  84. package/esm/data-types/bool-type.js +24 -0
  85. package/esm/data-types/box-type.js +49 -0
  86. package/esm/data-types/bytea-type.js +23 -0
  87. package/esm/data-types/char-type.js +24 -0
  88. package/esm/data-types/circle-type.js +45 -0
  89. package/esm/data-types/date-type.js +64 -0
  90. package/esm/data-types/float4-type.js +22 -0
  91. package/esm/data-types/float8-type.js +22 -0
  92. package/esm/data-types/int2-type.js +23 -0
  93. package/esm/data-types/int2-vector-type.js +35 -0
  94. package/esm/data-types/int4-type.js +23 -0
  95. package/esm/data-types/int8-type.js +28 -0
  96. package/esm/data-types/json-type.js +35 -0
  97. package/esm/data-types/jsonb-type.js +39 -0
  98. package/esm/data-types/lseg-type.js +49 -0
  99. package/esm/data-types/numeric-type.js +121 -0
  100. package/esm/data-types/oid-type.js +29 -0
  101. package/esm/data-types/point-type.js +36 -0
  102. package/esm/data-types/time-type.js +51 -0
  103. package/esm/data-types/timestamp-type.js +78 -0
  104. package/esm/data-types/timestamptz-type.js +70 -0
  105. package/esm/data-types/uuid-type.js +36 -0
  106. package/esm/data-types/varchar-type.js +24 -0
  107. package/esm/index.js +25 -0
  108. package/esm/interfaces/command-result.js +1 -0
  109. package/esm/interfaces/data-mapping-options.js +1 -0
  110. package/esm/interfaces/data-type.js +1 -0
  111. package/esm/interfaces/database-connection-params.js +1 -0
  112. package/esm/interfaces/field-info.js +1 -0
  113. package/esm/interfaces/query-options.js +1 -0
  114. package/esm/interfaces/query-result.js +1 -0
  115. package/esm/interfaces/script-execute-options.js +1 -0
  116. package/esm/interfaces/script-result.js +1 -0
  117. package/esm/interfaces/statement-prepare-options.js +1 -0
  118. package/esm/protocol/backend.js +252 -0
  119. package/esm/protocol/buffer-reader.js +74 -0
  120. package/esm/protocol/database-error.js +13 -0
  121. package/esm/protocol/frontend.js +214 -0
  122. package/esm/protocol/pg-socket.js +338 -0
  123. package/esm/protocol/protocol.js +68 -0
  124. package/esm/protocol/sasl.js +102 -0
  125. package/esm/protocol/smart-buffer.js +168 -0
  126. package/esm/safe-event-emitter.js +13 -0
  127. package/esm/types.js +1 -0
  128. package/esm/util/array-calculatedim.js +15 -0
  129. package/esm/util/bigint-methods.js +37 -0
  130. package/esm/util/config-from-env.js +26 -0
  131. package/esm/util/connection-config.js +57 -0
  132. package/esm/util/convert-row-to-object.js +9 -0
  133. package/esm/util/decode-binaryarray.js +35 -0
  134. package/esm/util/encode-binaryarray.js +41 -0
  135. package/esm/util/escape-literal.js +23 -0
  136. package/esm/util/fast-parseint.js +28 -0
  137. package/esm/util/get-parsers.js +38 -0
  138. package/esm/util/parse-array.js +57 -0
  139. package/esm/util/parse-datetime.js +34 -0
  140. package/esm/util/parse-row.js +7 -0
  141. package/esm/util/parse-time.js +25 -0
  142. package/esm/util/stringify-arrayliteral.js +34 -0
  143. package/esm/util/stringify-for-sql.js +25 -0
  144. package/esm/util/wrap-row-description.js +31 -0
  145. package/package.json +54 -0
  146. package/typings/connection/bind-param.d.ts +6 -0
  147. package/typings/connection/connection.d.ts +110 -0
  148. package/typings/connection/cursor.d.ts +25 -0
  149. package/typings/connection/intl-connection.d.ts +40 -0
  150. package/typings/connection/pool.d.ts +52 -0
  151. package/typings/connection/portal.d.ts +23 -0
  152. package/typings/connection/prepared-statement.d.ts +27 -0
  153. package/typings/constants.d.ts +130 -0
  154. package/typings/data-type-map.d.ts +11 -0
  155. package/typings/data-types/bool-type.d.ts +3 -0
  156. package/typings/data-types/box-type.d.ts +3 -0
  157. package/typings/data-types/bytea-type.d.ts +3 -0
  158. package/typings/data-types/char-type.d.ts +3 -0
  159. package/typings/data-types/circle-type.d.ts +3 -0
  160. package/typings/data-types/date-type.d.ts +3 -0
  161. package/typings/data-types/float4-type.d.ts +3 -0
  162. package/typings/data-types/float8-type.d.ts +3 -0
  163. package/typings/data-types/int2-type.d.ts +3 -0
  164. package/typings/data-types/int2-vector-type.d.ts +3 -0
  165. package/typings/data-types/int4-type.d.ts +3 -0
  166. package/typings/data-types/int8-type.d.ts +3 -0
  167. package/typings/data-types/json-type.d.ts +3 -0
  168. package/typings/data-types/jsonb-type.d.ts +3 -0
  169. package/typings/data-types/lseg-type.d.ts +3 -0
  170. package/typings/data-types/numeric-type.d.ts +12 -0
  171. package/typings/data-types/oid-type.d.ts +4 -0
  172. package/typings/data-types/point-type.d.ts +3 -0
  173. package/typings/data-types/time-type.d.ts +3 -0
  174. package/typings/data-types/timestamp-type.d.ts +3 -0
  175. package/typings/data-types/timestamptz-type.d.ts +3 -0
  176. package/typings/data-types/uuid-type.d.ts +3 -0
  177. package/typings/data-types/varchar-type.d.ts +3 -0
  178. package/typings/index.d.ts +25 -0
  179. package/typings/interfaces/command-result.d.ts +28 -0
  180. package/typings/interfaces/data-mapping-options.d.ts +9 -0
  181. package/typings/interfaces/data-type.d.ts +31 -0
  182. package/typings/interfaces/database-connection-params.d.ts +38 -0
  183. package/typings/interfaces/field-info.d.ts +46 -0
  184. package/typings/interfaces/query-options.d.ts +46 -0
  185. package/typings/interfaces/query-result.d.ts +8 -0
  186. package/typings/interfaces/script-execute-options.d.ts +26 -0
  187. package/typings/interfaces/script-result.d.ts +15 -0
  188. package/typings/interfaces/statement-prepare-options.d.ts +13 -0
  189. package/typings/protocol/backend.d.ts +9 -0
  190. package/typings/protocol/buffer-reader.d.ts +18 -0
  191. package/typings/protocol/database-error.d.ts +20 -0
  192. package/typings/protocol/frontend.d.ts +59 -0
  193. package/typings/protocol/pg-socket.d.ts +51 -0
  194. package/typings/protocol/protocol.d.ts +180 -0
  195. package/typings/protocol/sasl.d.ts +13 -0
  196. package/typings/protocol/smart-buffer.d.ts +39 -0
  197. package/typings/safe-event-emitter.d.ts +5 -0
  198. package/typings/types.d.ts +15 -0
  199. package/typings/util/array-calculatedim.d.ts +1 -0
  200. package/typings/util/bigint-methods.d.ts +3 -0
  201. package/typings/util/config-from-env.d.ts +2 -0
  202. package/typings/util/connection-config.d.ts +3 -0
  203. package/typings/util/convert-row-to-object.d.ts +2 -0
  204. package/typings/util/decode-binaryarray.d.ts +4 -0
  205. package/typings/util/encode-binaryarray.d.ts +5 -0
  206. package/typings/util/escape-literal.d.ts +1 -0
  207. package/typings/util/fast-parseint.d.ts +1 -0
  208. package/typings/util/get-parsers.d.ts +4 -0
  209. package/typings/util/parse-array.d.ts +5 -0
  210. package/typings/util/parse-datetime.d.ts +1 -0
  211. package/typings/util/parse-row.d.ts +3 -0
  212. package/typings/util/parse-time.d.ts +3 -0
  213. package/typings/util/stringify-arrayliteral.d.ts +3 -0
  214. package/typings/util/stringify-for-sql.d.ts +4 -0
  215. package/typings/util/wrap-row-description.d.ts +5 -0
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.ArrayByteaType = exports.ByteaType = void 0;
7
+ const postgres_bytea_1 = __importDefault(require("postgres-bytea"));
8
+ const constants_js_1 = require("../constants.js");
9
+ exports.ByteaType = {
10
+ name: 'bytea',
11
+ oid: constants_js_1.DataTypeOIDs.bytea,
12
+ jsType: 'Buffer',
13
+ parseBinary(v) {
14
+ return v;
15
+ },
16
+ encodeBinary(buf, v) {
17
+ buf.writeBuffer(v);
18
+ },
19
+ parseText: postgres_bytea_1.default,
20
+ isType(v) {
21
+ return v instanceof Buffer;
22
+ },
23
+ };
24
+ exports.ArrayByteaType = {
25
+ ...exports.ByteaType,
26
+ name: '_bytea',
27
+ oid: constants_js_1.DataTypeOIDs._bytea,
28
+ elementsOID: constants_js_1.DataTypeOIDs.bytea,
29
+ };
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ArrayCharType = exports.CharType = void 0;
4
+ const constants_js_1 = require("../constants.js");
5
+ exports.CharType = {
6
+ name: 'char',
7
+ oid: constants_js_1.DataTypeOIDs.char,
8
+ jsType: 'string',
9
+ parseBinary(v) {
10
+ return v.toString('utf8');
11
+ },
12
+ encodeBinary(buf, v) {
13
+ buf.writeString((v ? '' + v : ' ')[0], 'utf8');
14
+ },
15
+ parseText(v) {
16
+ return '' + v;
17
+ },
18
+ isType(v) {
19
+ return typeof v === 'string' && v.length === 1;
20
+ },
21
+ };
22
+ exports.ArrayCharType = {
23
+ ...exports.CharType,
24
+ name: '_char',
25
+ oid: constants_js_1.DataTypeOIDs._char,
26
+ elementsOID: constants_js_1.DataTypeOIDs.char,
27
+ };
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ArrayCircleType = exports.CircleType = void 0;
4
+ const constants_js_1 = require("../constants.js");
5
+ const CIRCLE_PATTERN1 = /^< *\( *(-?\d+\.?\d*) *, *(-?\d+\.?\d*) *\) *, *(-?\d+\.?\d*) *>$/;
6
+ const CIRCLE_PATTERN2 = /^\( *\( *(-?\d+\.?\d*) *, *(-?\d+\.?\d*) *\) *, *(-?\d+\.?\d*) *\)$/;
7
+ const CIRCLE_PATTERN3 = /^\( *(-?\d+\.?\d*) *, *(-?\d+\.?\d*) *\) *, *(-?\d+\.?\d*)$/;
8
+ const CIRCLE_PATTERN4 = /^(-?\d+\.?\d*) *, *(-?\d+\.?\d*) *, *(-?\d+\.?\d*)$/;
9
+ exports.CircleType = {
10
+ name: 'circle',
11
+ oid: constants_js_1.DataTypeOIDs.circle,
12
+ jsType: 'object',
13
+ parseBinary(v) {
14
+ return {
15
+ x: v.readDoubleBE(0),
16
+ y: v.readDoubleBE(8),
17
+ r: v.readDoubleBE(16),
18
+ };
19
+ },
20
+ encodeBinary(buf, v) {
21
+ buf.writeDoubleBE(v.x);
22
+ buf.writeDoubleBE(v.y);
23
+ buf.writeDoubleBE(v.r);
24
+ },
25
+ parseText(v) {
26
+ const m = v.match(CIRCLE_PATTERN1) || v.match(CIRCLE_PATTERN2) || v.match(CIRCLE_PATTERN3) || v.match(CIRCLE_PATTERN4);
27
+ if (!m)
28
+ return undefined;
29
+ return {
30
+ x: parseFloat(m[1]),
31
+ y: parseFloat(m[2]),
32
+ r: parseFloat(m[3]),
33
+ };
34
+ },
35
+ isType(v) {
36
+ return (typeof v === 'object' &&
37
+ Object.keys(v).length === 3 &&
38
+ typeof v.x === 'number' &&
39
+ typeof v.y === 'number' &&
40
+ typeof v.r === 'number');
41
+ },
42
+ };
43
+ exports.ArrayCircleType = {
44
+ ...exports.CircleType,
45
+ name: '_circle',
46
+ oid: constants_js_1.DataTypeOIDs._circle,
47
+ elementsOID: constants_js_1.DataTypeOIDs.circle,
48
+ };
@@ -0,0 +1,67 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ArrayDateType = exports.DateType = void 0;
4
+ const constants_js_1 = require("../constants.js");
5
+ const parse_datetime_js_1 = require("../util/parse-datetime.js");
6
+ const timeShift = 946684800000;
7
+ exports.DateType = {
8
+ name: 'date',
9
+ oid: constants_js_1.DataTypeOIDs.date,
10
+ jsType: 'Date',
11
+ parseBinary(v, options) {
12
+ const fetchAsString = options.fetchAsString && options.fetchAsString.includes(constants_js_1.DataTypeOIDs.date);
13
+ const t = v.readInt32BE();
14
+ if (t === 0x7fffffff)
15
+ return fetchAsString ? 'infinity' : Infinity;
16
+ if (t === -0x80000000)
17
+ return fetchAsString ? '-infinity' : -Infinity;
18
+ // Shift from 2000 to 1970
19
+ let d = new Date(t * 1000 * 86400 + timeShift);
20
+ if (fetchAsString || !options.utcDates)
21
+ d = new Date(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate());
22
+ return fetchAsString ? dateToDateString(d) : d;
23
+ },
24
+ encodeBinary(buf, v, options) {
25
+ if (typeof v === 'string')
26
+ v = (0, parse_datetime_js_1.parseDateTime)(v, false, false, options.utcDates);
27
+ if (v === Infinity) {
28
+ buf.writeInt32BE(0x7fffffff);
29
+ return;
30
+ }
31
+ if (v === -Infinity) {
32
+ buf.writeInt32BE(-0x80000000);
33
+ return;
34
+ }
35
+ if (!(v instanceof Date))
36
+ v = new Date(v);
37
+ let n = options.utcDates ? v.getTime() : v.getTime() - v.getTimezoneOffset() * 60 * 1000;
38
+ n = (n - timeShift) / 1000 / 86400;
39
+ const t = Math.trunc(n + Number.EPSILON);
40
+ buf.writeInt32BE(t);
41
+ },
42
+ parseText(v, options) {
43
+ const fetchAsString = options.fetchAsString && options.fetchAsString.includes(constants_js_1.DataTypeOIDs.date);
44
+ if (fetchAsString)
45
+ return v;
46
+ return (0, parse_datetime_js_1.parseDateTime)(v, false, false, options.utcDates);
47
+ },
48
+ isType(v) {
49
+ return (v instanceof Date &&
50
+ v.getHours() === 0 &&
51
+ v.getMinutes() === 0 &&
52
+ v.getSeconds() === 0 &&
53
+ v.getMilliseconds() === 0);
54
+ },
55
+ };
56
+ function padZero(v) {
57
+ return v < 9 ? '0' + v : '' + v;
58
+ }
59
+ function dateToDateString(d) {
60
+ return d.getFullYear() + '-' + padZero(d.getMonth() + 1) + '-' + padZero(d.getDate());
61
+ }
62
+ exports.ArrayDateType = {
63
+ ...exports.DateType,
64
+ name: '_date',
65
+ oid: constants_js_1.DataTypeOIDs._date,
66
+ elementsOID: constants_js_1.DataTypeOIDs.date,
67
+ };
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ArrayFloat4Type = exports.Float4Type = void 0;
4
+ const constants_js_1 = require("../constants.js");
5
+ exports.Float4Type = {
6
+ name: 'float4',
7
+ oid: constants_js_1.DataTypeOIDs.float4,
8
+ jsType: 'number',
9
+ parseBinary(v) {
10
+ return Math.round((v.readFloatBE(0) + Number.EPSILON) * 100) / 100;
11
+ },
12
+ encodeBinary(buf, v) {
13
+ buf.writeFloatBE(typeof v === 'number' ? v : parseFloat(v));
14
+ },
15
+ parseText: parseFloat,
16
+ isType(v) {
17
+ return typeof v === 'number';
18
+ },
19
+ };
20
+ exports.ArrayFloat4Type = {
21
+ ...exports.Float4Type,
22
+ name: '_float4',
23
+ oid: constants_js_1.DataTypeOIDs._float4,
24
+ elementsOID: constants_js_1.DataTypeOIDs.float4,
25
+ };
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ArrayFloat8Type = exports.Float8Type = void 0;
4
+ const constants_js_1 = require("../constants.js");
5
+ exports.Float8Type = {
6
+ name: 'float8',
7
+ oid: constants_js_1.DataTypeOIDs.float8,
8
+ jsType: 'number',
9
+ parseBinary(v) {
10
+ return v.readDoubleBE(0);
11
+ },
12
+ encodeBinary(buf, v) {
13
+ buf.writeDoubleBE(typeof v === 'number' ? v : parseFloat(v));
14
+ },
15
+ parseText: parseFloat,
16
+ isType(v) {
17
+ return typeof v === 'number';
18
+ },
19
+ };
20
+ exports.ArrayFloat8Type = {
21
+ ...exports.Float8Type,
22
+ name: '_float8',
23
+ oid: constants_js_1.DataTypeOIDs._float8,
24
+ elementsOID: constants_js_1.DataTypeOIDs.float8,
25
+ };
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ArrayInt2Type = exports.Int2Type = void 0;
4
+ const constants_js_1 = require("../constants.js");
5
+ const fast_parseint_js_1 = require("../util/fast-parseint.js");
6
+ exports.Int2Type = {
7
+ name: 'int2',
8
+ oid: constants_js_1.DataTypeOIDs.int2,
9
+ jsType: 'number',
10
+ parseBinary(v) {
11
+ return v.readInt16BE(0);
12
+ },
13
+ encodeBinary(buf, v) {
14
+ buf.writeInt16BE((0, fast_parseint_js_1.fastParseInt)(v));
15
+ },
16
+ parseText: fast_parseint_js_1.fastParseInt,
17
+ isType(v) {
18
+ return typeof v === 'number' && Number.isInteger(v) && v >= -32768 && v <= 32767;
19
+ },
20
+ };
21
+ exports.ArrayInt2Type = {
22
+ ...exports.Int2Type,
23
+ name: '_int2',
24
+ oid: constants_js_1.DataTypeOIDs._int2,
25
+ elementsOID: constants_js_1.DataTypeOIDs.int2,
26
+ };
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ArrayInt2VectorType = exports.Int2VectorType = void 0;
4
+ const constants_js_1 = require("../constants.js");
5
+ const decode_binaryarray_js_1 = require("../util/decode-binaryarray.js");
6
+ const encode_binaryarray_js_1 = require("../util/encode-binaryarray.js");
7
+ const fast_parseint_js_1 = require("../util/fast-parseint.js");
8
+ exports.Int2VectorType = {
9
+ name: 'int2vector',
10
+ oid: constants_js_1.DataTypeOIDs.int2vector,
11
+ jsType: 'array',
12
+ parseBinary(v) {
13
+ return (0, decode_binaryarray_js_1.decodeBinaryArray)(v, b => b.readInt16BE()) || undefined;
14
+ },
15
+ encodeBinary(buf, v) {
16
+ (0, encode_binaryarray_js_1.encodeBinaryArray)(buf, v, constants_js_1.DataTypeOIDs.int2, {}, (io, x) => {
17
+ io.writeInt16BE(x);
18
+ });
19
+ },
20
+ encodeCalculateDim(v) {
21
+ return [v.length];
22
+ },
23
+ parseText(str) {
24
+ return str.split(' ').map(fast_parseint_js_1.fastParseInt);
25
+ },
26
+ encodeText(v) {
27
+ return v.join(' ');
28
+ },
29
+ isType(v) {
30
+ return (Array.isArray(v) && !v.find(x => !(typeof x === 'number' && Number.isInteger(x) && x >= -32768 && x <= 32767)));
31
+ },
32
+ };
33
+ exports.ArrayInt2VectorType = {
34
+ ...exports.Int2VectorType,
35
+ name: '_int2vector',
36
+ oid: constants_js_1.DataTypeOIDs._int2vector,
37
+ elementsOID: constants_js_1.DataTypeOIDs.int2vector,
38
+ };
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ArrayInt4Type = exports.Int4Type = void 0;
4
+ const constants_js_1 = require("../constants.js");
5
+ const fast_parseint_js_1 = require("../util/fast-parseint.js");
6
+ exports.Int4Type = {
7
+ name: 'int4',
8
+ oid: constants_js_1.DataTypeOIDs.int4,
9
+ jsType: 'number',
10
+ parseBinary(v) {
11
+ return v.readInt32BE(0);
12
+ },
13
+ encodeBinary(buf, v) {
14
+ buf.writeInt32BE((0, fast_parseint_js_1.fastParseInt)(v));
15
+ },
16
+ parseText: fast_parseint_js_1.fastParseInt,
17
+ isType(v) {
18
+ return typeof v === 'number' && Number.isInteger(v) && v <= Number.MAX_SAFE_INTEGER;
19
+ },
20
+ };
21
+ exports.ArrayInt4Type = {
22
+ ...exports.Int4Type,
23
+ name: '_int4',
24
+ oid: constants_js_1.DataTypeOIDs._int4,
25
+ elementsOID: constants_js_1.DataTypeOIDs.int4,
26
+ };
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ArrayInt8Type = exports.Int8Type = void 0;
4
+ const constants_js_1 = require("../constants.js");
5
+ const bigint_methods_js_1 = require("../util/bigint-methods.js");
6
+ const maxSafeInteger = BigInt(Number.MAX_SAFE_INTEGER);
7
+ exports.Int8Type = {
8
+ name: 'int8',
9
+ oid: constants_js_1.DataTypeOIDs.int8,
10
+ jsType: 'BigInt',
11
+ parseBinary(buf) {
12
+ const v = typeof buf.readBigInt64BE === 'function' ? buf.readBigInt64BE(0) : (0, bigint_methods_js_1.readBigInt64BE)(buf);
13
+ return v >= -maxSafeInteger && v <= maxSafeInteger ? Number(v) : v;
14
+ },
15
+ encodeBinary(buf, v) {
16
+ buf.writeBigInt64BE(v);
17
+ },
18
+ parseText(s) {
19
+ const v = BigInt(s);
20
+ return v >= -maxSafeInteger && v <= maxSafeInteger ? Number(v) : v;
21
+ },
22
+ isType(v) {
23
+ return typeof v === 'bigint' || (typeof v === 'number' && Number.isInteger(v) && v > Number.MAX_SAFE_INTEGER);
24
+ },
25
+ };
26
+ exports.ArrayInt8Type = {
27
+ ...exports.Int8Type,
28
+ name: '_int8',
29
+ oid: constants_js_1.DataTypeOIDs._int8,
30
+ elementsOID: constants_js_1.DataTypeOIDs.int8,
31
+ };
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ArrayJsonType = exports.JsonType = void 0;
4
+ const constants_js_1 = require("../constants.js");
5
+ exports.JsonType = {
6
+ name: 'json',
7
+ oid: constants_js_1.DataTypeOIDs.json,
8
+ jsType: 'string',
9
+ parseBinary(v, options) {
10
+ const content = v.toString('utf8');
11
+ const fetchAsString = options.fetchAsString && options.fetchAsString.includes(constants_js_1.DataTypeOIDs.jsonb);
12
+ if (fetchAsString)
13
+ return content;
14
+ return content ? JSON.parse(content) : undefined;
15
+ },
16
+ encodeText(v) {
17
+ if (typeof v === 'object' || typeof v === 'bigint')
18
+ return JSON.stringify(v);
19
+ if (typeof v === 'boolean')
20
+ return v ? 'true' : 'false';
21
+ return '' + v;
22
+ },
23
+ parseText(v, options) {
24
+ const fetchAsString = options.fetchAsString && options.fetchAsString.includes(constants_js_1.DataTypeOIDs.jsonb);
25
+ if (fetchAsString)
26
+ return v;
27
+ return v ? JSON.parse(v) : null;
28
+ },
29
+ isType(v) {
30
+ return v && typeof v === 'object';
31
+ },
32
+ };
33
+ exports.ArrayJsonType = {
34
+ ...exports.JsonType,
35
+ name: '_json',
36
+ oid: constants_js_1.DataTypeOIDs._json,
37
+ elementsOID: constants_js_1.DataTypeOIDs.json,
38
+ };
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ArrayJsonbType = exports.JsonbType = void 0;
4
+ const constants_js_1 = require("../constants.js");
5
+ const buffer_reader_js_1 = require("../protocol/buffer-reader.js");
6
+ exports.JsonbType = {
7
+ name: 'jsonb',
8
+ oid: constants_js_1.DataTypeOIDs.jsonb,
9
+ jsType: 'string',
10
+ parseBinary(v, options) {
11
+ const buf = new buffer_reader_js_1.BufferReader(v);
12
+ if (buf.readUInt8() !== 1)
13
+ throw new Error('Unexpected Jsonb version value in header');
14
+ const fetchAsString = options.fetchAsString && options.fetchAsString.includes(constants_js_1.DataTypeOIDs.json);
15
+ const content = buf.readLString(buf.length - buf.offset);
16
+ if (fetchAsString)
17
+ return content;
18
+ return content ? JSON.parse(content) : undefined;
19
+ },
20
+ encodeText(v) {
21
+ if (typeof v === 'object' || typeof v === 'bigint')
22
+ return JSON.stringify(v);
23
+ if (typeof v === 'boolean')
24
+ return v ? 'true' : 'false';
25
+ return '\x0001' + v;
26
+ },
27
+ parseText(v, options) {
28
+ const fetchAsString = options.fetchAsString && options.fetchAsString.includes(constants_js_1.DataTypeOIDs.json);
29
+ if (fetchAsString)
30
+ return v;
31
+ return v ? JSON.parse(v) : null;
32
+ },
33
+ isType(v) {
34
+ return v && typeof v === 'object';
35
+ },
36
+ };
37
+ exports.ArrayJsonbType = {
38
+ ...exports.JsonbType,
39
+ name: '_jsonb',
40
+ oid: constants_js_1.DataTypeOIDs._jsonb,
41
+ elementsOID: constants_js_1.DataTypeOIDs.jsonb,
42
+ };
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ArrayLsegType = exports.LsegType = void 0;
4
+ const constants_js_1 = require("../constants.js");
5
+ const LSEG_PATTERN1 = /^\[ *\( *(-?\d+\.?\d*) *, *(-?\d+\.?\d*) *\) *, *\( *(-?\d+\.?\d*) *, *(-?\d+\.?\d*) *\) *]$/;
6
+ const LSEG_PATTERN2 = /^\( *\( *(-?\d+\.?\d*) *, *(-?\d+\.?\d*) *\) *, *\( *(-?\d+\.?\d*) *, *(-?\d+\.?\d*) *\) *\)$/;
7
+ const LSEG_PATTERN3 = /^\( *(-?\d+\.?\d*) *, *(-?\d+\.?\d*) *\) *, *\( *(-?\d+\.?\d*) *, *(-?\d+\.?\d*) *\)$/;
8
+ const LSEG_PATTERN4 = /^(-?\d+\.?\d*) *, *(-?\d+\.?\d*) *, *(-?\d+\.?\d*) *, *(-?\d+\.?\d*)$/;
9
+ exports.LsegType = {
10
+ name: 'lseg',
11
+ oid: constants_js_1.DataTypeOIDs.lseg,
12
+ jsType: 'object',
13
+ parseBinary(v) {
14
+ return {
15
+ x1: v.readDoubleBE(0),
16
+ y1: v.readDoubleBE(8),
17
+ x2: v.readDoubleBE(16),
18
+ y2: v.readDoubleBE(24),
19
+ };
20
+ },
21
+ encodeBinary(buf, v) {
22
+ buf.writeDoubleBE(v.x1);
23
+ buf.writeDoubleBE(v.y1);
24
+ buf.writeDoubleBE(v.x2);
25
+ buf.writeDoubleBE(v.y2);
26
+ },
27
+ parseText(v) {
28
+ const m = v.match(LSEG_PATTERN1) || v.match(LSEG_PATTERN2) || v.match(LSEG_PATTERN3) || v.match(LSEG_PATTERN4);
29
+ if (!m)
30
+ return undefined;
31
+ return {
32
+ x1: parseFloat(m[1]),
33
+ y1: parseFloat(m[2]),
34
+ x2: parseFloat(m[3]),
35
+ y2: parseFloat(m[4]),
36
+ };
37
+ },
38
+ isType(v) {
39
+ return (typeof v === 'object' &&
40
+ Object.keys(v).length === 4 &&
41
+ typeof v.x1 === 'number' &&
42
+ typeof v.y1 === 'number' &&
43
+ typeof v.x2 === 'number' &&
44
+ typeof v.y2 === 'number');
45
+ },
46
+ };
47
+ exports.ArrayLsegType = {
48
+ ...exports.LsegType,
49
+ name: '_lseg',
50
+ oid: constants_js_1.DataTypeOIDs._lseg,
51
+ elementsOID: constants_js_1.DataTypeOIDs.lseg,
52
+ };
@@ -0,0 +1,125 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.numberBytesToString = exports.ArrayNumericType = exports.NumericType = void 0;
4
+ const constants_js_1 = require("../constants.js");
5
+ const NUMERIC_NEG = 0x4000;
6
+ const NUMERIC_NAN = 0xc000;
7
+ const DEC_DIGITS = 4;
8
+ const ROUND_POWERS = [0, 1000, 100, 10];
9
+ exports.NumericType = {
10
+ name: 'numeric',
11
+ oid: constants_js_1.DataTypeOIDs.numeric,
12
+ jsType: 'number',
13
+ parseBinary(v) {
14
+ const len = v.readInt16BE();
15
+ const weight = v.readInt16BE(2);
16
+ const sign = v.readInt16BE(4);
17
+ const scale = v.readInt16BE(6);
18
+ if (sign === NUMERIC_NAN)
19
+ return NaN;
20
+ const digits = [];
21
+ for (let i = 0; i < len; i++) {
22
+ digits[i] = v.readInt16BE(8 + i * 2);
23
+ }
24
+ const numString = numberBytesToString(digits, scale, weight, sign);
25
+ return parseFloat(numString);
26
+ },
27
+ encodeText(v) {
28
+ const n = typeof v === 'number' ? v : parseFloat(v);
29
+ return '' + n;
30
+ },
31
+ parseText: parseFloat,
32
+ isType(v) {
33
+ return typeof v === 'number';
34
+ },
35
+ };
36
+ exports.ArrayNumericType = {
37
+ ...exports.NumericType,
38
+ name: '_numeric',
39
+ oid: constants_js_1.DataTypeOIDs._numeric,
40
+ elementsOID: constants_js_1.DataTypeOIDs.numeric,
41
+ };
42
+ /* https://github.com/pgjdbc/pgjdbc/blob/3eca3a76aa4a04cb28cb960ed674cb67db30b5e3/pgjdbc/src/main/java/org/postgresql/util/ByteConverter.java */
43
+ /**
44
+ * Convert a number from binary representation to text representation.
45
+ * @param digits array of shorts that can be decoded as the number String
46
+ * @param scale the scale of the number binary representation
47
+ * @param weight the weight of the number binary representation
48
+ * @param sign the sign of the number
49
+ * @return String the number as String
50
+ */
51
+ function numberBytesToString(digits, scale, weight, sign) {
52
+ let i;
53
+ let d;
54
+ /*
55
+ * Allocate space for the result.
56
+ *
57
+ * i is set to the # of decimal digits before decimal point.
58
+ * dscale is the # of decimal digits we will print after decimal point.
59
+ * We may generate as many as DEC_DIGITS-1 excess digits at the end, and in addition we
60
+ * need room for sign, decimal point, null terminator.
61
+ */
62
+ i = (weight + 1) * DEC_DIGITS;
63
+ if (i <= 0)
64
+ i = 1;
65
+ /*
66
+ * Output a dash for negative values
67
+ */
68
+ let out = sign === NUMERIC_NEG ? '-' : '';
69
+ /*
70
+ * Output all digits before the decimal point
71
+ */
72
+ if (weight < 0) {
73
+ d = weight + 1;
74
+ out += '0';
75
+ }
76
+ else {
77
+ for (d = 0; d <= weight; d++) {
78
+ /* In the first digit, suppress extra leading decimal zeroes */
79
+ out += digitToString(d, digits, d !== 0);
80
+ }
81
+ }
82
+ /*
83
+ * If requested, output a decimal point and all the digits that follow it.
84
+ * We initially put out a multiple of DEC_DIGITS digits, then truncate if
85
+ * needed.
86
+ */
87
+ if (scale > 0) {
88
+ out += '.';
89
+ for (i = 0; i < scale; d++, i += DEC_DIGITS) {
90
+ out += digitToString(d, digits, true);
91
+ }
92
+ }
93
+ const extra = (i - scale) % DEC_DIGITS;
94
+ return out.substr(0, out.length - extra);
95
+ }
96
+ exports.numberBytesToString = numberBytesToString;
97
+ /* https://github.com/pgjdbc/pgjdbc/blob/3eca3a76aa4a04cb28cb960ed674cb67db30b5e3/pgjdbc/src/main/java/org/postgresql/util/ByteConverter.java */
98
+ /**
99
+ * Convert a number from binary representation to text representation.
100
+ * @param idx index of the digit to be converted in the digits array
101
+ * @param digits array of shorts that can be decoded as the number String
102
+ * @param alwaysPutIt a flag that indicate whether or not to put the digit char even if it is zero
103
+ * @return String the number as String
104
+ */
105
+ function digitToString(idx, digits, alwaysPutIt) {
106
+ let out = '';
107
+ let dig = idx >= 0 && idx < digits.length ? digits[idx] : 0;
108
+ // Each dig represents 4 decimal digits (e.g. 9999)
109
+ // If we continue the number, then we need to print 0 as 0000 (alwaysPutIt parameter is true)
110
+ for (let p = 1; p < ROUND_POWERS.length; p++) {
111
+ const pow = ROUND_POWERS[p];
112
+ const d1 = Math.trunc(dig / pow);
113
+ dig -= d1 * pow;
114
+ const putit = d1 > 0;
115
+ if (putit || alwaysPutIt) {
116
+ out += d1;
117
+ // We printed a character, so we need to print the rest of the current digits in dig
118
+ // For instance, we need to keep printing 000 from 1000 even if idx==0 (== it is the very
119
+ // beginning)
120
+ alwaysPutIt = true;
121
+ }
122
+ }
123
+ out += dig;
124
+ return out;
125
+ }
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.VectorOidType = exports.ArrayOidType = exports.OidType = void 0;
4
+ const constants_js_1 = require("../constants.js");
5
+ const fast_parseint_js_1 = require("../util/fast-parseint.js");
6
+ exports.OidType = {
7
+ name: 'oid',
8
+ oid: constants_js_1.DataTypeOIDs.oid,
9
+ jsType: 'number',
10
+ parseBinary(v) {
11
+ return v.readUInt32BE(0);
12
+ },
13
+ encodeBinary(buf, v) {
14
+ buf.writeUInt32BE((0, fast_parseint_js_1.fastParseInt)(v));
15
+ },
16
+ parseText: fast_parseint_js_1.fastParseInt,
17
+ isType(v) {
18
+ return typeof v === 'number' && Number.isInteger(v) && !!constants_js_1.DataTypeNames[v];
19
+ },
20
+ };
21
+ exports.ArrayOidType = {
22
+ ...exports.OidType,
23
+ name: '_oid',
24
+ oid: constants_js_1.DataTypeOIDs._oid,
25
+ elementsOID: constants_js_1.DataTypeOIDs.oid,
26
+ };
27
+ exports.VectorOidType = {
28
+ ...exports.OidType,
29
+ name: '_oidvector',
30
+ oid: constants_js_1.DataTypeOIDs.oidvector,
31
+ elementsOID: constants_js_1.DataTypeOIDs.oid,
32
+ };