UNPKG

postgrejs

Version:

Professional PostgreSQL client NodeJS

158 lines (157 loc) 6.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Portal = void 0; const constants_js_1 = require("../constants.js"); const data_type_map_js_1 = require("../data-type-map.js"); const protocol_js_1 = require("../protocol/protocol.js"); const intl_connection_js_1 = require("./intl-connection.js"); class Portal { constructor(statement, name) { this._columnFormat = constants_js_1.DEFAULT_COLUMN_FORMAT; this._statement = statement; this._name = name; } get connection() { return this._statement.connection; } get name() { return this._name; } async bind(params, queryOptions) { const intoCon = (0, intl_connection_js_1.getIntlConnection)(this.connection); intoCon.ref(); try { const socket = intoCon.socket; this._columnFormat = queryOptions.columnFormat != null ? queryOptions.columnFormat : protocol_js_1.Protocol.DataFormat.binary; socket.sendBindMessage({ typeMap: queryOptions.typeMap || data_type_map_js_1.GlobalTypeMap, statement: this._statement.name, portal: this.name, paramTypes: this._statement.paramTypes, params, queryOptions, }); socket.sendFlushMessage(); return await socket.capture(async (code, msg, done) => { switch (code) { case protocol_js_1.Protocol.BackendMessageCode.BindComplete: done(); break; case protocol_js_1.Protocol.BackendMessageCode.NoticeResponse: break; default: done(new Error(`Server returned unexpected response message (${String.fromCharCode(code)})`)); } }); } finally { intoCon.unref(); } } async retrieveFields() { const intoCon = (0, intl_connection_js_1.getIntlConnection)(this.connection); intoCon.ref(); try { const socket = intoCon.socket; socket.sendDescribeMessage({ type: 'P', name: this.name }); socket.sendFlushMessage(); return await socket.capture(async (code, msg, done) => { switch (code) { case protocol_js_1.Protocol.BackendMessageCode.NoticeResponse: break; case protocol_js_1.Protocol.BackendMessageCode.NoData: done(); break; case protocol_js_1.Protocol.BackendMessageCode.RowDescription: done(undefined, msg.fields); break; default: done(new Error(`Server returned unexpected response message (${String.fromCharCode(code)})`)); } }); } finally { intoCon.unref(); } } async execute(fetchCount) { const intoCon = (0, intl_connection_js_1.getIntlConnection)(this.connection); intoCon.ref(); try { const socket = intoCon.socket; socket.sendExecuteMessage({ portal: this.name, fetchCount: fetchCount || 100, }); socket.sendFlushMessage(); const rows = []; return await socket.capture(async (code, msg, done) => { switch (code) { case protocol_js_1.Protocol.BackendMessageCode.NoticeResponse: break; case protocol_js_1.Protocol.BackendMessageCode.NoData: done(undefined, { code }); break; case protocol_js_1.Protocol.BackendMessageCode.DataRow: if (Array.isArray(this._columnFormat)) { rows.push(msg.columns.map((buf, i) => this._columnFormat[i] === protocol_js_1.Protocol.DataFormat.text ? buf.toString('utf8') : buf)); } else if (this._columnFormat === protocol_js_1.Protocol.DataFormat.binary) rows.push(msg.columns); else rows.push(msg.columns.map((buf) => buf.toString('utf8'))); break; case protocol_js_1.Protocol.BackendMessageCode.PortalSuspended: done(undefined, { code, rows }); break; case protocol_js_1.Protocol.BackendMessageCode.CommandComplete: done(undefined, { code, rows, command: msg.command, rowCount: msg.rowCount, }); break; default: done(new Error(`Server returned unexpected response message (${String.fromCharCode(code)})`)); } }); } finally { intoCon.unref(); } } async close() { const intoCon = (0, intl_connection_js_1.getIntlConnection)(this.connection); intoCon.ref(); try { const socket = intoCon.socket; socket.sendCloseMessage({ type: 'P', name: this.name }); socket.sendSyncMessage(); return await socket.capture(async (code, msg, done) => { switch (code) { case protocol_js_1.Protocol.BackendMessageCode.NoticeResponse: break; case protocol_js_1.Protocol.BackendMessageCode.CloseComplete: break; case protocol_js_1.Protocol.BackendMessageCode.ReadyForQuery: intoCon.transactionStatus = msg.status; done(); break; default: done(new Error(`Server returned unexpected response message (${String.fromCharCode(code)})`)); } }); } finally { intoCon.unref(); } } } exports.Portal = Portal;