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,213 @@
1
+ import { coerceToBoolean } from 'putil-varhelpers';
2
+ import { DEFAULT_COLUMN_FORMAT } from '../constants.js';
3
+ import { GlobalTypeMap } from '../data-type-map.js';
4
+ import { Protocol } from '../protocol/protocol.js';
5
+ import { SafeEventEmitter } from '../safe-event-emitter.js';
6
+ import { convertRowToObject } from '../util/convert-row-to-object.js';
7
+ import { getParsers } from '../util/get-parsers.js';
8
+ import { parseRow } from '../util/parse-row.js';
9
+ import { wrapRowDescription } from '../util/wrap-row-description.js';
10
+ import { Cursor } from './cursor.js';
11
+ import { getIntlConnection } from './intl-connection.js';
12
+ import { Portal } from './portal.js';
13
+ let statementCounter = 0;
14
+ let portalCounter = 0;
15
+ export class PreparedStatement extends SafeEventEmitter {
16
+ constructor(connection, sql, paramTypes) {
17
+ super();
18
+ this._sql = '';
19
+ this._name = '';
20
+ this._refCount = 0;
21
+ this._connection = connection;
22
+ this._name = 'S_' + ++statementCounter;
23
+ this._sql = sql;
24
+ this._paramTypes = paramTypes;
25
+ this._onErrorSavePoint = 'SP_' + Math.round(Math.random() * 100000000);
26
+ }
27
+ static async prepare(connection, sql, options) {
28
+ const intoCon = getIntlConnection(connection);
29
+ intoCon.assertConnected();
30
+ const socket = intoCon.socket;
31
+ const statement = new PreparedStatement(connection, sql, options?.paramTypes);
32
+ await intoCon.statementQueue
33
+ .enqueue(async () => {
34
+ intoCon.ref();
35
+ try {
36
+ socket.sendParseMessage({
37
+ statement: statement.name,
38
+ sql: statement.sql,
39
+ paramTypes: statement.paramTypes,
40
+ });
41
+ socket.sendFlushMessage();
42
+ try {
43
+ await socket.capture(async (code, msg, done) => {
44
+ if (code === Protocol.BackendMessageCode.ParseComplete)
45
+ done();
46
+ // May be Protocol.BackendMessageCode.NoticeResponse
47
+ });
48
+ }
49
+ finally {
50
+ socket.sendSyncMessage();
51
+ await socket.capture(async (code, msg, done) => {
52
+ if (code === Protocol.BackendMessageCode.ReadyForQuery)
53
+ done();
54
+ // May be Protocol.BackendMessageCode.NoticeResponse
55
+ });
56
+ }
57
+ }
58
+ finally {
59
+ intoCon.unref();
60
+ }
61
+ })
62
+ .toPromise();
63
+ statement._refCount = 1;
64
+ return statement;
65
+ }
66
+ get connection() {
67
+ return this._connection;
68
+ }
69
+ get name() {
70
+ return this._name;
71
+ }
72
+ get sql() {
73
+ return this._sql;
74
+ }
75
+ get paramTypes() {
76
+ return this._paramTypes;
77
+ }
78
+ async execute(options = {}) {
79
+ const intlCon = getIntlConnection(this.connection);
80
+ const transactionCommand = this.sql.match(/^(\bBEGIN\b|\bCOMMIT\b|\bSTART\b|\bROLLBACK|SAVEPOINT|RELEASE\b)/i);
81
+ let beginFirst = false;
82
+ let commitLast = false;
83
+ if (!transactionCommand) {
84
+ if (!intlCon.inTransaction &&
85
+ (options?.autoCommit != null ? options?.autoCommit : intlCon.config.autoCommit) === false) {
86
+ beginFirst = true;
87
+ }
88
+ if (intlCon.inTransaction && options?.autoCommit)
89
+ commitLast = true;
90
+ }
91
+ if (beginFirst)
92
+ await intlCon.execute('BEGIN');
93
+ const rollbackOnError = !transactionCommand &&
94
+ (options?.rollbackOnError != null
95
+ ? options.rollbackOnError
96
+ : coerceToBoolean(intlCon.config.rollbackOnError, true));
97
+ if (intlCon.inTransaction && rollbackOnError)
98
+ await intlCon.execute('SAVEPOINT ' + this._onErrorSavePoint);
99
+ try {
100
+ const result = await intlCon.statementQueue.enqueue(() => this._execute(options)).toPromise();
101
+ if (commitLast)
102
+ await intlCon.execute('COMMIT');
103
+ else if (intlCon.inTransaction && rollbackOnError)
104
+ await intlCon.execute('RELEASE ' + this._onErrorSavePoint + ';');
105
+ return result;
106
+ }
107
+ catch (e) {
108
+ if (intlCon.inTransaction && rollbackOnError)
109
+ await intlCon.execute('ROLLBACK TO ' + this._onErrorSavePoint + ';');
110
+ throw e;
111
+ }
112
+ }
113
+ async close() {
114
+ --this._refCount;
115
+ if (this._refCount > 0)
116
+ return;
117
+ const intoCon = getIntlConnection(this.connection);
118
+ await intoCon.statementQueue.enqueue(() => this._close()).toPromise();
119
+ }
120
+ async cancel() {
121
+ throw new Error('Not implemented yet');
122
+ }
123
+ async _execute(options = {}) {
124
+ let portal;
125
+ const intlCon = getIntlConnection(this.connection);
126
+ intlCon.ref();
127
+ try {
128
+ const result = { command: undefined };
129
+ const startTime = Date.now();
130
+ const t = Date.now();
131
+ // Create portal
132
+ const portalName = 'P_' + ++portalCounter;
133
+ portal = new Portal(this, portalName);
134
+ await portal.bind(options.params, options);
135
+ const fields = await portal.retrieveFields();
136
+ const typeMap = options.typeMap || GlobalTypeMap;
137
+ let parsers;
138
+ let resultFields;
139
+ if (fields) {
140
+ parsers = getParsers(typeMap, fields);
141
+ resultFields = wrapRowDescription(typeMap, fields, options.columnFormat || DEFAULT_COLUMN_FORMAT);
142
+ result.fields = resultFields;
143
+ result.rowType = options.objectRows ? 'object' : 'array';
144
+ if (options.cursor) {
145
+ result.cursor = new Cursor(this, portal, resultFields, parsers, options);
146
+ this._refCount++;
147
+ portal = undefined;
148
+ return result;
149
+ }
150
+ }
151
+ const executeResult = await portal.execute(options.fetchCount);
152
+ result.executeTime = Date.now() - t;
153
+ if (executeResult.command)
154
+ result.command = executeResult.command;
155
+ if (resultFields && parsers && executeResult.rows) {
156
+ if (!result.command)
157
+ result.command = 'SELECT';
158
+ const rows = (result.rows = executeResult.rows);
159
+ const l = rows.length;
160
+ let row;
161
+ for (let i = 0; i < l; i++) {
162
+ row = rows[i];
163
+ parseRow(parsers, row, options);
164
+ if (options.objectRows) {
165
+ rows[i] = convertRowToObject(resultFields, row);
166
+ }
167
+ }
168
+ }
169
+ if (result.command === 'DELETE' || result.command === 'INSERT' || result.command === 'UPDATE')
170
+ result.rowsAffected = executeResult.rowCount;
171
+ result.executeTime = Date.now() - startTime;
172
+ return result;
173
+ }
174
+ finally {
175
+ intlCon.unref();
176
+ if (portal)
177
+ await portal.close();
178
+ }
179
+ }
180
+ async _close() {
181
+ if (--this._refCount > 0)
182
+ return;
183
+ const intoCon = getIntlConnection(this.connection);
184
+ intoCon.ref();
185
+ try {
186
+ const socket = intoCon.socket;
187
+ socket.sendCloseMessage({ type: 'S', name: this.name });
188
+ socket.sendSyncMessage();
189
+ await socket.capture(async (code, msg, done) => {
190
+ switch (code) {
191
+ case Protocol.BackendMessageCode.NoticeResponse:
192
+ this.emit('notice', msg);
193
+ break;
194
+ case Protocol.BackendMessageCode.CloseComplete:
195
+ break;
196
+ case Protocol.BackendMessageCode.ReadyForQuery:
197
+ intoCon.transactionStatus = msg.status;
198
+ done();
199
+ break;
200
+ default:
201
+ done(new Error(`Server returned unexpected response message (0x${code.toString(16)})`));
202
+ }
203
+ });
204
+ }
205
+ finally {
206
+ intoCon.unref();
207
+ }
208
+ this.emit('close');
209
+ }
210
+ [Symbol.asyncDispose]() {
211
+ return this.close();
212
+ }
213
+ }
@@ -0,0 +1,247 @@
1
+ import { Protocol } from './protocol/protocol.js';
2
+ var DataFormat = Protocol.DataFormat;
3
+ // @ts-ignore
4
+ Symbol.asyncDispose ?? (Symbol.asyncDispose = Symbol('Symbol.asyncDispose'));
5
+ export { DataFormat };
6
+ export const DEFAULT_COLUMN_FORMAT = DataFormat.binary;
7
+ export var ConnectionState;
8
+ (function (ConnectionState) {
9
+ ConnectionState[ConnectionState["CLOSED"] = 0] = "CLOSED";
10
+ ConnectionState[ConnectionState["CONNECTING"] = 1] = "CONNECTING";
11
+ ConnectionState[ConnectionState["AUTHORIZING"] = 3] = "AUTHORIZING";
12
+ ConnectionState[ConnectionState["READY"] = 2] = "READY";
13
+ ConnectionState[ConnectionState["CLOSING"] = 10] = "CLOSING";
14
+ })(ConnectionState || (ConnectionState = {}));
15
+ export const DataTypeOIDs = {
16
+ bool: 16,
17
+ bytea: 17,
18
+ char: 18,
19
+ name: 19,
20
+ int8: 20,
21
+ int2: 21,
22
+ int2vector: 22,
23
+ int4: 23,
24
+ regproc: 24,
25
+ text: 25,
26
+ oid: 26,
27
+ tid: 27,
28
+ xid: 28,
29
+ cid: 29,
30
+ oidvector: 30,
31
+ json: 114,
32
+ xml: 142,
33
+ point: 600,
34
+ lseg: 601,
35
+ path: 602,
36
+ box: 603,
37
+ polygon: 604,
38
+ line: 628,
39
+ cidr: 650,
40
+ float4: 700,
41
+ float8: 701,
42
+ unknown: 705,
43
+ circle: 718,
44
+ macaddr8: 774,
45
+ money: 790,
46
+ macaddr: 829,
47
+ inet: 869,
48
+ bpchar: 1042,
49
+ varchar: 1043,
50
+ date: 1082,
51
+ time: 1083,
52
+ timestamp: 1114,
53
+ timestamptz: 1184,
54
+ interval: 1186,
55
+ timetz: 1266,
56
+ bit: 1560,
57
+ varbit: 1562,
58
+ numeric: 1700,
59
+ refcursor: 1790,
60
+ regprocedure: 2202,
61
+ regoper: 2203,
62
+ regoperator: 2204,
63
+ regclass: 2205,
64
+ regtype: 2206,
65
+ record: 2249,
66
+ cstring: 2275,
67
+ any: 2276,
68
+ anyarray: 2277,
69
+ void: 2278,
70
+ trigger: 2279,
71
+ // eslint-disable-next-line camelcase
72
+ language_handler: 2280,
73
+ internal: 2281,
74
+ anyelement: 2283,
75
+ anynonarray: 2776,
76
+ uuid: 2950,
77
+ jsonb: 3802,
78
+ anyrange: 3831,
79
+ int4range: 3904,
80
+ numrange: 3906,
81
+ tsrange: 3908,
82
+ rstzrange: 3910,
83
+ daterange: 3912,
84
+ int8range: 3926,
85
+ _xml: 143,
86
+ _json: 199,
87
+ _xid8: 271,
88
+ _line: 629,
89
+ _cidr: 651,
90
+ _circle: 719,
91
+ _macaddr8: 775,
92
+ _money: 791,
93
+ _bool: 1000,
94
+ _bytea: 1001,
95
+ _char: 1002,
96
+ _name: 1003,
97
+ _int2: 1005,
98
+ _int2vector: 1006,
99
+ _int4: 1007,
100
+ _regproc: 1008,
101
+ _text: 1009,
102
+ _tid: 1010,
103
+ _xid: 1011,
104
+ _cid: 1012,
105
+ _oidvector: 1013,
106
+ _bpchar: 1014,
107
+ _varchar: 1015,
108
+ _int8: 1016,
109
+ _point: 1017,
110
+ _lseg: 1018,
111
+ _path: 1019,
112
+ _box: 1020,
113
+ _float4: 1021,
114
+ _float8: 1022,
115
+ _polygon: 1027,
116
+ _oid: 1028,
117
+ _macaddr: 1040,
118
+ _inet: 1041,
119
+ _timestamp: 1115,
120
+ _date: 1182,
121
+ _time: 1183,
122
+ _timestamptz: 1185,
123
+ _interval: 1187,
124
+ _numeric: 1231,
125
+ _cstring: 1263,
126
+ _timetz: 1270,
127
+ _bit: 1561,
128
+ _varbit: 1563,
129
+ _uuid: 2951,
130
+ _jsonb: 3807,
131
+ };
132
+ export const DataTypeNames = {
133
+ [DataTypeOIDs.bool]: 'bool',
134
+ [DataTypeOIDs.bytea]: 'bytea',
135
+ [DataTypeOIDs.char]: 'char',
136
+ [DataTypeOIDs.name]: 'name',
137
+ [DataTypeOIDs.int8]: 'int8',
138
+ [DataTypeOIDs.int2]: 'int2',
139
+ [DataTypeOIDs.int2vector]: 'int2vector',
140
+ [DataTypeOIDs.int4]: 'int4',
141
+ [DataTypeOIDs.regproc]: 'regproc',
142
+ [DataTypeOIDs.text]: 'text',
143
+ [DataTypeOIDs.oid]: 'oid',
144
+ [DataTypeOIDs.tid]: 'tid',
145
+ [DataTypeOIDs.xid]: 'xid',
146
+ [DataTypeOIDs.cid]: 'cid',
147
+ [DataTypeOIDs.oidvector]: 'oidvector',
148
+ [DataTypeOIDs.json]: 'json',
149
+ [DataTypeOIDs.xml]: 'xml',
150
+ [DataTypeOIDs.point]: 'point',
151
+ [DataTypeOIDs.lseg]: 'lseg',
152
+ [DataTypeOIDs.path]: 'path',
153
+ [DataTypeOIDs.box]: 'box',
154
+ [DataTypeOIDs.polygon]: 'polygon',
155
+ [DataTypeOIDs.line]: 'line',
156
+ [DataTypeOIDs.cidr]: 'cidr',
157
+ [DataTypeOIDs.float4]: 'float4',
158
+ [DataTypeOIDs.float8]: 'float8',
159
+ [DataTypeOIDs.unknown]: 'unknown',
160
+ [DataTypeOIDs.circle]: 'circle',
161
+ [DataTypeOIDs.macaddr8]: 'macaddr8',
162
+ [DataTypeOIDs.money]: 'money',
163
+ [DataTypeOIDs.macaddr]: 'macaddr',
164
+ [DataTypeOIDs.inet]: 'inet',
165
+ [DataTypeOIDs.bpchar]: 'bpchar',
166
+ [DataTypeOIDs.varchar]: 'varchar',
167
+ [DataTypeOIDs.date]: 'date',
168
+ [DataTypeOIDs.time]: 'time',
169
+ [DataTypeOIDs.timestamp]: 'timestamp',
170
+ [DataTypeOIDs.timestamptz]: 'timestamptz',
171
+ [DataTypeOIDs.interval]: 'interval',
172
+ [DataTypeOIDs.timetz]: 'timetz',
173
+ [DataTypeOIDs.bit]: 'bit',
174
+ [DataTypeOIDs.varbit]: 'varbit',
175
+ [DataTypeOIDs.numeric]: 'numeric',
176
+ [DataTypeOIDs.refcursor]: 'refcursor',
177
+ [DataTypeOIDs.regprocedure]: 'regprocedure',
178
+ [DataTypeOIDs.regoper]: 'regoper',
179
+ [DataTypeOIDs.regoperator]: 'regoperator',
180
+ [DataTypeOIDs.regclass]: 'regclass',
181
+ [DataTypeOIDs.regtype]: 'regtype',
182
+ [DataTypeOIDs.record]: 'record',
183
+ [DataTypeOIDs.cstring]: 'cstring',
184
+ [DataTypeOIDs.any]: 'any',
185
+ [DataTypeOIDs.anyarray]: 'anyarray',
186
+ [DataTypeOIDs.void]: 'void',
187
+ [DataTypeOIDs.trigger]: 'trigger',
188
+ [DataTypeOIDs.language_handler]: 'language_handler',
189
+ [DataTypeOIDs.internal]: 'internal',
190
+ [DataTypeOIDs.anyelement]: 'anyelement',
191
+ [DataTypeOIDs.anynonarray]: 'anynonarray',
192
+ [DataTypeOIDs.uuid]: 'uuid',
193
+ [DataTypeOIDs.jsonb]: 'jsonb',
194
+ [DataTypeOIDs.anyrange]: 'anyrange',
195
+ [DataTypeOIDs.int4range]: 'int4range',
196
+ [DataTypeOIDs.numrange]: 'numrange',
197
+ [DataTypeOIDs.tsrange]: 'tsrange',
198
+ [DataTypeOIDs.rstzrange]: 'rstzrange',
199
+ [DataTypeOIDs.daterange]: 'daterange',
200
+ [DataTypeOIDs.int8range]: 'int8range',
201
+ [DataTypeOIDs._xml]: '_xml',
202
+ [DataTypeOIDs._json]: '_json',
203
+ [DataTypeOIDs._xid8]: '_xid8',
204
+ [DataTypeOIDs._line]: '_line',
205
+ [DataTypeOIDs._cidr]: '_cidr',
206
+ [DataTypeOIDs._circle]: '_circle',
207
+ [DataTypeOIDs._macaddr8]: '_macaddr8',
208
+ [DataTypeOIDs._money]: '_money',
209
+ [DataTypeOIDs._bool]: '_bool',
210
+ [DataTypeOIDs._bytea]: '_bytea',
211
+ [DataTypeOIDs._char]: '_char',
212
+ [DataTypeOIDs._name]: '_name',
213
+ [DataTypeOIDs._int2]: '_int2',
214
+ [DataTypeOIDs._int2vector]: '_int2vector',
215
+ [DataTypeOIDs._int4]: '_int4',
216
+ [DataTypeOIDs._regproc]: '_regproc',
217
+ [DataTypeOIDs._text]: '_text',
218
+ [DataTypeOIDs._tid]: '_tid',
219
+ [DataTypeOIDs._xid]: '_xid',
220
+ [DataTypeOIDs._cid]: '_cid',
221
+ [DataTypeOIDs._oidvector]: '_oidvector',
222
+ [DataTypeOIDs._bpchar]: '_bpchar',
223
+ [DataTypeOIDs._varchar]: '_varchar',
224
+ [DataTypeOIDs._int8]: '_int8',
225
+ [DataTypeOIDs._point]: '_point',
226
+ [DataTypeOIDs._lseg]: '_lseg',
227
+ [DataTypeOIDs._path]: '_path',
228
+ [DataTypeOIDs._box]: '_box',
229
+ [DataTypeOIDs._float4]: '_float4',
230
+ [DataTypeOIDs._float8]: '_float8',
231
+ [DataTypeOIDs._polygon]: '_polygon',
232
+ [DataTypeOIDs._oid]: '_oid',
233
+ [DataTypeOIDs._macaddr]: '_macaddr',
234
+ [DataTypeOIDs._inet]: '_inet',
235
+ [DataTypeOIDs._timestamp]: '_timestamp',
236
+ [DataTypeOIDs._date]: '_date',
237
+ [DataTypeOIDs._time]: '_time',
238
+ [DataTypeOIDs._timestamptz]: '_timestamptz',
239
+ [DataTypeOIDs._interval]: '_interval',
240
+ [DataTypeOIDs._numeric]: '_numeric',
241
+ [DataTypeOIDs._cstring]: '_cstring',
242
+ [DataTypeOIDs._timetz]: '_timetz',
243
+ [DataTypeOIDs._bit]: '_bit',
244
+ [DataTypeOIDs._varbit]: '_varbit',
245
+ [DataTypeOIDs._uuid]: '_uuid',
246
+ [DataTypeOIDs._jsonb]: '_jsonb',
247
+ };
@@ -0,0 +1,115 @@
1
+ import { DataTypeOIDs } from './constants.js';
2
+ import { ArrayBoolType, BoolType } from './data-types/bool-type.js';
3
+ import { ArrayBoxType, BoxType } from './data-types/box-type.js';
4
+ import { ArrayByteaType, ByteaType } from './data-types/bytea-type.js';
5
+ import { ArrayCharType, CharType } from './data-types/char-type.js';
6
+ import { ArrayCircleType, CircleType } from './data-types/circle-type.js';
7
+ import { ArrayDateType, DateType } from './data-types/date-type.js';
8
+ import { ArrayFloat4Type, Float4Type } from './data-types/float4-type.js';
9
+ import { ArrayFloat8Type, Float8Type } from './data-types/float8-type.js';
10
+ import { ArrayInt2Type, Int2Type } from './data-types/int2-type.js';
11
+ import { ArrayInt2VectorType, Int2VectorType } from './data-types/int2-vector-type.js';
12
+ import { ArrayInt4Type, Int4Type } from './data-types/int4-type.js';
13
+ import { ArrayInt8Type, Int8Type } from './data-types/int8-type.js';
14
+ import { ArrayJsonType, JsonType } from './data-types/json-type.js';
15
+ import { ArrayJsonbType, JsonbType } from './data-types/jsonb-type.js';
16
+ import { ArrayLsegType, LsegType } from './data-types/lseg-type.js';
17
+ import { ArrayNumericType, NumericType } from './data-types/numeric-type.js';
18
+ import { ArrayOidType, OidType, VectorOidType } from './data-types/oid-type.js';
19
+ import { ArrayPointType, PointType } from './data-types/point-type.js';
20
+ import { ArrayTimeType, TimeType } from './data-types/time-type.js';
21
+ import { ArrayTimestampType, TimestampType } from './data-types/timestamp-type.js';
22
+ import { ArrayTimestamptzType, TimestamptzType } from './data-types/timestamptz-type.js';
23
+ import { ArrayUuidType, UuidType } from './data-types/uuid-type.js';
24
+ import { ArrayVarcharType, VarcharType } from './data-types/varchar-type.js';
25
+ export class DataTypeMap {
26
+ constructor(other) {
27
+ this._itemsByOID = {};
28
+ this._items = [];
29
+ if (other instanceof DataTypeMap)
30
+ Object.assign(this._items, other._items);
31
+ }
32
+ get(oid) {
33
+ return this._itemsByOID[oid];
34
+ }
35
+ register(dataTypes) {
36
+ dataTypes = Array.isArray(dataTypes) ? dataTypes : [dataTypes];
37
+ for (const t of dataTypes) {
38
+ this._itemsByOID[t.oid] = t;
39
+ const i = this._items.findIndex(tt => tt.oid === t.oid);
40
+ if (i >= 0)
41
+ this._items[i] = t;
42
+ else
43
+ this._items.push(t);
44
+ }
45
+ }
46
+ determine(value) {
47
+ if (value == null)
48
+ return DataTypeOIDs.unknown;
49
+ const valueIsArray = Array.isArray(value);
50
+ let i;
51
+ let t;
52
+ for (i = this._items.length - 1; i >= 0; i--) {
53
+ t = this._items[i];
54
+ if (valueIsArray) {
55
+ if (t.elementsOID && t.isType(value[0]))
56
+ return t.oid;
57
+ }
58
+ else if (!t.elementsOID && t.isType(value))
59
+ return t.oid;
60
+ }
61
+ return DataTypeOIDs.unknown;
62
+ }
63
+ }
64
+ export const GlobalTypeMap = new DataTypeMap();
65
+ GlobalTypeMap.register([OidType, VectorOidType, ArrayOidType]);
66
+ GlobalTypeMap.register([JsonbType, ArrayJsonbType]);
67
+ GlobalTypeMap.register([JsonType, ArrayJsonType]);
68
+ GlobalTypeMap.register([BoolType, ArrayBoolType]);
69
+ GlobalTypeMap.register([NumericType, ArrayNumericType]);
70
+ GlobalTypeMap.register([Float4Type, ArrayFloat4Type]);
71
+ GlobalTypeMap.register([Float8Type, ArrayFloat8Type]);
72
+ GlobalTypeMap.register([Int2Type, ArrayInt2Type]);
73
+ GlobalTypeMap.register([Int4Type, ArrayInt4Type]);
74
+ GlobalTypeMap.register([Int8Type, ArrayInt8Type]);
75
+ GlobalTypeMap.register([ByteaType, ArrayByteaType]);
76
+ GlobalTypeMap.register([CircleType, ArrayCircleType]);
77
+ GlobalTypeMap.register([PointType, ArrayPointType]);
78
+ GlobalTypeMap.register([LsegType, ArrayLsegType]);
79
+ GlobalTypeMap.register([BoxType, ArrayBoxType]);
80
+ GlobalTypeMap.register([Int2VectorType, ArrayInt2VectorType]);
81
+ GlobalTypeMap.register({ ...VarcharType, name: 'bpchar', oid: DataTypeOIDs.bpchar });
82
+ GlobalTypeMap.register({
83
+ ...ArrayVarcharType,
84
+ name: '_bpchar',
85
+ oid: DataTypeOIDs._bpchar,
86
+ elementsOID: DataTypeOIDs.bpchar,
87
+ });
88
+ GlobalTypeMap.register({ ...VarcharType, name: 'name', oid: DataTypeOIDs.name });
89
+ GlobalTypeMap.register({
90
+ ...ArrayVarcharType,
91
+ name: '_name',
92
+ oid: DataTypeOIDs._name,
93
+ elementsOID: DataTypeOIDs.name,
94
+ });
95
+ GlobalTypeMap.register({ ...VarcharType, name: 'text', oid: DataTypeOIDs.text });
96
+ GlobalTypeMap.register({
97
+ ...ArrayVarcharType,
98
+ name: '_text',
99
+ oid: DataTypeOIDs._text,
100
+ elementsOID: DataTypeOIDs.text,
101
+ });
102
+ GlobalTypeMap.register({ ...VarcharType, name: 'xml', oid: DataTypeOIDs.xml });
103
+ GlobalTypeMap.register({
104
+ ...ArrayVarcharType,
105
+ name: '_xml',
106
+ oid: DataTypeOIDs._xml,
107
+ elementsOID: DataTypeOIDs.xml,
108
+ });
109
+ GlobalTypeMap.register([VarcharType, ArrayVarcharType]);
110
+ GlobalTypeMap.register([UuidType, ArrayUuidType]);
111
+ GlobalTypeMap.register([CharType, ArrayCharType]);
112
+ GlobalTypeMap.register([TimestamptzType, ArrayTimestamptzType]);
113
+ GlobalTypeMap.register([TimeType, ArrayTimeType]);
114
+ GlobalTypeMap.register([DateType, ArrayDateType]);
115
+ GlobalTypeMap.register([TimestampType, ArrayTimestampType]);
@@ -0,0 +1,24 @@
1
+ import { DataTypeOIDs } from '../constants.js';
2
+ export const BoolType = {
3
+ name: 'bool',
4
+ oid: DataTypeOIDs.bool,
5
+ jsType: 'boolean',
6
+ parseBinary(v) {
7
+ return !!v.readUInt8();
8
+ },
9
+ encodeBinary(buf, v) {
10
+ buf.writeInt8(v ? 1 : 0);
11
+ },
12
+ parseText(v) {
13
+ return v === 'TRUE' || v === 't' || v === 'true' || v === 'y' || v === 'yes' || v === 'on' || v === '1';
14
+ },
15
+ isType(v) {
16
+ return typeof v === 'boolean';
17
+ },
18
+ };
19
+ export const ArrayBoolType = {
20
+ ...BoolType,
21
+ name: '_bool',
22
+ oid: DataTypeOIDs._bool,
23
+ elementsOID: DataTypeOIDs.bool,
24
+ };
@@ -0,0 +1,49 @@
1
+ import { DataTypeOIDs } from '../constants.js';
2
+ const BOX_PATTERN1 = /^\( *\( *(-?\d+\.?\d*) *, *(-?\d+\.?\d*) *\) *, *\( *(-?\d+\.?\d*) *, *(-?\d+\.?\d*) *\) *\)$/;
3
+ const BOX_PATTERN2 = /^\( *(-?\d+\.?\d*) *, *(-?\d+\.?\d*) *\) *, *\( *(-?\d+\.?\d*) *, *(-?\d+\.?\d*) *\)$/;
4
+ const BOX_PATTERN3 = /^(-?\d+\.?\d*) *, *(-?\d+\.?\d*) *, *(-?\d+\.?\d*) *, *(-?\d+\.?\d*)$/;
5
+ export const BoxType = {
6
+ name: 'box',
7
+ oid: DataTypeOIDs.box,
8
+ jsType: 'object',
9
+ arraySeparator: ';',
10
+ parseBinary(v) {
11
+ return {
12
+ x1: v.readDoubleBE(0),
13
+ y1: v.readDoubleBE(8),
14
+ x2: v.readDoubleBE(16),
15
+ y2: v.readDoubleBE(24),
16
+ };
17
+ },
18
+ encodeBinary(buf, v) {
19
+ buf.writeDoubleBE(v.x1);
20
+ buf.writeDoubleBE(v.y1);
21
+ buf.writeDoubleBE(v.x2);
22
+ buf.writeDoubleBE(v.y2);
23
+ },
24
+ parseText(v) {
25
+ const m = v.match(BOX_PATTERN1) || v.match(BOX_PATTERN2) || v.match(BOX_PATTERN3);
26
+ if (!m)
27
+ return undefined;
28
+ return {
29
+ x1: parseFloat(m[1]),
30
+ y1: parseFloat(m[2]),
31
+ x2: parseFloat(m[3]),
32
+ y2: parseFloat(m[4]),
33
+ };
34
+ },
35
+ isType(v) {
36
+ return (typeof v === 'object' &&
37
+ Object.keys(v).length === 4 &&
38
+ typeof v.x1 === 'number' &&
39
+ typeof v.y1 === 'number' &&
40
+ typeof v.x2 === 'number' &&
41
+ typeof v.y2 === 'number');
42
+ },
43
+ };
44
+ export const ArrayBoxType = {
45
+ ...BoxType,
46
+ name: '_box',
47
+ oid: DataTypeOIDs._box,
48
+ elementsOID: DataTypeOIDs.box,
49
+ };
@@ -0,0 +1,23 @@
1
+ import decodeBytea from 'postgres-bytea';
2
+ import { DataTypeOIDs } from '../constants.js';
3
+ export const ByteaType = {
4
+ name: 'bytea',
5
+ oid: DataTypeOIDs.bytea,
6
+ jsType: 'Buffer',
7
+ parseBinary(v) {
8
+ return v;
9
+ },
10
+ encodeBinary(buf, v) {
11
+ buf.writeBuffer(v);
12
+ },
13
+ parseText: decodeBytea,
14
+ isType(v) {
15
+ return v instanceof Buffer;
16
+ },
17
+ };
18
+ export const ArrayByteaType = {
19
+ ...ByteaType,
20
+ name: '_bytea',
21
+ oid: DataTypeOIDs._bytea,
22
+ elementsOID: DataTypeOIDs.bytea,
23
+ };
@@ -0,0 +1,24 @@
1
+ import { DataTypeOIDs } from '../constants.js';
2
+ export const CharType = {
3
+ name: 'char',
4
+ oid: DataTypeOIDs.char,
5
+ jsType: 'string',
6
+ parseBinary(v) {
7
+ return v.toString('utf8');
8
+ },
9
+ encodeBinary(buf, v) {
10
+ buf.writeString((v ? '' + v : ' ')[0], 'utf8');
11
+ },
12
+ parseText(v) {
13
+ return '' + v;
14
+ },
15
+ isType(v) {
16
+ return typeof v === 'string' && v.length === 1;
17
+ },
18
+ };
19
+ export const ArrayCharType = {
20
+ ...CharType,
21
+ name: '_char',
22
+ oid: DataTypeOIDs._char,
23
+ elementsOID: DataTypeOIDs.char,
24
+ };