src/db/execute.js
function execute_(ctx, statement, values) {
  return new Promise(function(resolve, reject) {
    if (/^begin/i.test(statement)) {
      // If this is a `BEGIN` statement; put us in a transaction
      ctx.inTransaction = true
    } else if (/^(commit|rollback)/i.test(statement)) {
      // Else if this is a `COMMIT` or `ROLLBACK` statement;
      // leave the transaction
      ctx.inTransaction = false
    }

    // Default values to an empty array
    values = values || []

    // TRACE: Get the current clock time
    let now = microtime.now()

    // Send the statement
    ctx.client.query(statement, values, function(err, result) {
      // Handle and reject if an error occurred
      if (err) return reject(err)

      // TRACE: Calculate the elasped time and log the SQL statement
      let elapsed = +((microtime.now() - now) / 1000).toFixed(2)
      log.trace({
        id: ctx.id,
        elapsed: `${elapsed}ms`,
        values: (values && values.length) ? values : undefined
      }, statement)

      // DEBUG: Increment the per-session counters
      ctx.elapsed += elapsed
      ctx.count += 1

      // Parse (and resolve) the result proxy
      if (!_.isFinite(result.rowCount)) {
        // This doesn't support a sane row count resolve with nothing
        resolve()
      } else if (result.fields == null) {
        // Resolve the row count
        resolve(result.rowCount)
      } else {
        // Resolve the rows
        resolve(result.rows)
      }
    })
  })
}
function execute_(ctx, statement, values) {
  return new Promise(function(resolve, reject) {
    if (/^begin/i.test(statement)) {
      // If this is a `BEGIN` statement; put us in a transaction
      ctx.inTransaction = true
    } else if (/^(commit|rollback)/i.test(statement)) {
      // Else if this is a `COMMIT` or `ROLLBACK` statement;
      // leave the transaction
      ctx.inTransaction = false
    }

    // Default values to an empty array
    values = values || []

    // TRACE: Get the current clock time
    let now = microtime.now()

    // Send the statement
    ctx.client.query(statement, values, function(err, result) {
      // Handle and reject if an error occurred
      if (err) return reject(err)

      // TRACE: Calculate the elasped time and log the SQL statement
      let elapsed = +((microtime.now() - now) / 1000).toFixed(2)
      log.trace({
        id: ctx.id,
        elapsed: `${elapsed}ms`,
        values: (values && values.length) ? values : undefined
      }, statement)

      // DEBUG: Increment the per-session counters
      ctx.elapsed += elapsed
      ctx.count += 1

      // Parse (and resolve) the result proxy
      if (!_.isFinite(result.rowCount)) {
        // This doesn't support a sane row count resolve with nothing
        resolve()
      } else if (result.fields == null) {
        // Resolve the row count
        resolve(result.rowCount)
      } else {
        // Resolve the rows
        resolve(result.rows)
      }
    })
  })
}
function execute_ ( ctx , statement , values ) { return new Promise ( function ( resolve , reject ) { if ( / ^begin / i . test ( statement ) ) { // If this is a `BEGIN` statement; put us in a transaction ctx . inTransaction = true } else if ( / ^(commit|rollback) / i . test ( statement ) ) { // Else if this is a `COMMIT` or `ROLLBACK` statement; // leave the transaction ctx . inTransaction = false } // Default values to an empty array values = values || [ ] // TRACE: Get the current clock time let now = microtime . now ( ) // Send the statement ctx . client . query ( statement , values , function ( err , result ) { // Handle and reject if an error occurred if ( err ) return reject ( err ) // TRACE: Calculate the elasped time and log the SQL statement let elapsed = + ( ( microtime . now ( ) - now ) / 1000 ) . toFixed ( 2 ) log . trace ( { id : ctx . id , elapsed : ` ${ elapsed } ` , values : ( values && values . length ) ? values : undefined } , statement ) // DEBUG: Increment the per-session counters ctx . elapsed += elapsed ctx . count += 1 // Parse (and resolve) the result proxy if ( ! _ . isFinite ( result . rowCount ) ) { // This doesn't support a sane row count resolve with nothing resolve ( ) } else if ( result . fields == null ) { // Resolve the row count resolve ( result . rowCount ) } else { // Resolve the rows resolve ( result . rows ) } } ) } ) }
Actually execute the statement
Actually execute the statement