123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- 'use strict'
- const { EventEmitter } = require('events')
- const Result = require('./result')
- const utils = require('./utils')
- class Query extends EventEmitter {
- constructor(config, values, callback) {
- super()
- config = utils.normalizeQueryConfig(config, values, callback)
- this.text = config.text
- this.values = config.values
- this.rows = config.rows
- this.types = config.types
- this.name = config.name
- this.queryMode = config.queryMode
- this.binary = config.binary
-
- this.portal = config.portal || ''
- this.callback = config.callback
- this._rowMode = config.rowMode
- if (process.domain && config.callback) {
- this.callback = process.domain.bind(config.callback)
- }
- this._result = new Result(this._rowMode, this.types)
-
- this._results = this._result
- this._canceledDueToError = false
- }
- requiresPreparation() {
- if (this.queryMode === 'extended') {
- return true
- }
-
- if (this.name) {
- return true
- }
-
-
- if (this.rows) {
- return true
- }
-
- if (!this.text) {
- return false
- }
-
- if (!this.values) {
- return false
- }
- return this.values.length > 0
- }
- _checkForMultirow() {
-
-
-
- if (this._result.command) {
- if (!Array.isArray(this._results)) {
- this._results = [this._result]
- }
- this._result = new Result(this._rowMode, this.types)
- this._results.push(this._result)
- }
- }
-
-
-
- handleRowDescription(msg) {
- this._checkForMultirow()
- this._result.addFields(msg.fields)
- this._accumulateRows = this.callback || !this.listeners('row').length
- }
- handleDataRow(msg) {
- let row
- if (this._canceledDueToError) {
- return
- }
- try {
- row = this._result.parseRow(msg.fields)
- } catch (err) {
- this._canceledDueToError = err
- return
- }
- this.emit('row', row, this._result)
- if (this._accumulateRows) {
- this._result.addRow(row)
- }
- }
- handleCommandComplete(msg, connection) {
- this._checkForMultirow()
- this._result.addCommandComplete(msg)
-
-
- if (this.rows) {
- connection.sync()
- }
- }
-
-
-
-
- handleEmptyQuery(connection) {
- if (this.rows) {
- connection.sync()
- }
- }
- handleError(err, connection) {
-
- if (this._canceledDueToError) {
- err = this._canceledDueToError
- this._canceledDueToError = false
- }
-
-
- if (this.callback) {
- return this.callback(err)
- }
- this.emit('error', err)
- }
- handleReadyForQuery(con) {
- if (this._canceledDueToError) {
- return this.handleError(this._canceledDueToError, con)
- }
- if (this.callback) {
- try {
- this.callback(null, this._results)
- } catch (err) {
- process.nextTick(() => {
- throw err
- })
- }
- }
- this.emit('end', this._results)
- }
- submit(connection) {
- if (typeof this.text !== 'string' && typeof this.name !== 'string') {
- return new Error('A query must have either text or a name. Supplying neither is unsupported.')
- }
- const previous = connection.parsedStatements[this.name]
- if (this.text && previous && this.text !== previous) {
- return new Error(`Prepared statements must be unique - '${this.name}' was used for a different statement`)
- }
- if (this.values && !Array.isArray(this.values)) {
- return new Error('Query values must be an array')
- }
- if (this.requiresPreparation()) {
- this.prepare(connection)
- } else {
- connection.query(this.text)
- }
- return null
- }
- hasBeenParsed(connection) {
- return this.name && connection.parsedStatements[this.name]
- }
- handlePortalSuspended(connection) {
- this._getRows(connection, this.rows)
- }
- _getRows(connection, rows) {
- connection.execute({
- portal: this.portal,
- rows: rows,
- })
-
-
- if (!rows) {
- connection.sync()
- } else {
-
- connection.flush()
- }
- }
-
- prepare(connection) {
-
- if (!this.hasBeenParsed(connection)) {
- connection.parse({
- text: this.text,
- name: this.name,
- types: this.types,
- })
- }
-
-
-
- try {
- connection.bind({
- portal: this.portal,
- statement: this.name,
- values: this.values,
- binary: this.binary,
- valueMapper: utils.prepareValue,
- })
- } catch (err) {
- this.handleError(err, connection)
- return
- }
- connection.describe({
- type: 'P',
- name: this.portal || '',
- })
- this._getRows(connection, this.rows)
- }
- handleCopyInResponse(connection) {
- connection.sendCopyFail('No source stream defined')
- }
-
- handleCopyData(msg, connection) {
-
- }
- }
- module.exports = Query
|