<commit_before>'use strict';

var Mixin = require('../../../util/mixin');
var BluebirdPromise = require('bluebird');

/**
 * Transaction support for queries.
 *
 * @mixin Transaction
 */
module.exports = Mixin.create(/** @lends Transaction */{

  /**
   * Documentation forthcoming.
   */
  _take: function(orig) {
    this._super(orig);
    this._transaction = orig._transaction;
  },

  /**
   * Documentation forthcoming.
   */
  client: function() {
    return (this._transaction && this._transaction._client) || this._super();
  },

  /**
   * Documentation forthcoming.
   */
  transaction: function(transaction) {
    // TODO: write some tests that use the getter.
    var result;
    if (arguments.length === 0) { // getter
      result = this._transaction;
    }
    else { // setter
      var dup = this._dup();
      dup._transaction = transaction;
      result = dup;
    }
    return result;
  },

  /**
   * Documentation forthcoming.
   */
  beforeExecution: BluebirdPromise.method(function() {
    if (!this._transaction) { return; }

    var transaction = this._transaction;
    if (this._transactionAction) {
      // don't throw error for any transaction action methods
    }
    else if (transaction._clientInvalid) {
      throw new Error('Cannot execute query using committed/resolved transaction.');
    }
    else if (transaction !== this && !transaction._executed) {
      throw new Error('Must execute `begin` query before using transaction.');
    }

    var result = transaction._client || transaction._clientPromise;
    if (!result) {
      transaction._clientPromise =
      result = transaction._adapter.pool.acquireAsync().then(function(client) {
        transaction._client = client;
      });
    }
    return result;
  }),

  /**
   * Documentation forthcoming.
   */
  afterExecution: BluebirdPromise.method(function() {
    if (!this._transaction) { return; }

    var transaction = this._transaction;
    if (transaction._clientReleasing) {
      transaction._adapter.pool.release(transaction._client);
      transaction._clientReleasing = false;
    }
  }),

  /**
   * Override of {@link RawSQL#execute}.
   *
   * @private
   * @see {@link RawSQL#execute}
   */
  execute: function() {
    var self = this;
    return this.beforeExecution()
      .then(this._super)
      .then(function() {
        return self.afterExecution();
      });
  },

  /**
   * Documentation forthcoming.
   */
  begin: function() {
    var dup = this._dup();
    dup._transactionAction = 'begin';
    dup._transaction = dup;
    return dup;
  },

  /**
   * Documentation forthcoming.
   */
  commit: function() {
    var dup = this._dup();
    dup._transactionAction = 'commit';
    dup._transaction._clientReleasing = true;
    dup._transaction._clientInvalid = true;
    // TODO: release the client
    return dup;
  },

  /**
   * Documentation forthcoming.
   */
  rollback: function() {
    var dup = this._dup();
    dup._transactionAction = 'rollback';
    dup._transaction._clientReleasing = true;
    dup._transaction._clientInvalid = true;
    // TODO: release the client
    return dup;
  },

  /**
   * Override of {@link RawSQL#sql}.
   *
   * @private
   * @see {@link RawSQL#sql}
   */
  sql: function() {
    return this._transactionAction ?<commit_msg>Variable renaming.<commit_after>'use strict';

var Mixin = require('../../../util/mixin');
var BluebirdPromise = require('bluebird');

/**
 * Transaction support for queries.
 *
 * @mixin Transaction
 */
module.exports = Mixin.create(/** @lends Transaction */{

  /**
   * Documentation forthcoming.
   */
  _take: function(orig) {
    this._super(orig);
    this._transaction = orig._transaction;
  },

  /**
   * Documentation forthcoming.
   */
  client: function() {
    return (this._transaction && this._transaction._client) || this._super();
  },

  /**
   * Documentation forthcoming.
   */
  transaction: function(transaction) {
    // TODO: write some tests that use the getter.
    var result;
    if (arguments.length === 0) { // getter
      result = this._transaction;
    }
    else { // setter
      var dup = this._dup();
      dup._transaction = transaction;
      result = dup;
    }
    return result;
  },

  /**
   * Documentation forthcoming.
   */
  beforeExecution: BluebirdPromise.method(function() {
    if (!this._transaction) { return; }

    var transaction = this._transaction;
    if (this._transactionAction) {
      // don't throw error for any transaction action methods
    }
    else if (transaction._transactionClosed) {
      throw new Error('Cannot execute query using committed/resolved transaction.');
    }
    else if (transaction !== this && !transaction._executed) {
      throw new Error('Must execute `begin` query before using transaction.');
    }

    var result = transaction._client || transaction._clientPromise;
    if (!result) {
      transaction._clientPromise =
      result = transaction._adapter.pool.acquireAsync().then(function(client) {
        transaction._client = client;
      });
    }
    return result;
  }),

  /**
   * Documentation forthcoming.
   */
  afterExecution: BluebirdPromise.method(function() {
    if (!this._transaction) { return; }

    var transaction = this._transaction;
    if (transaction._transactionReleasing) {
      transaction._adapter.pool.release(transaction._client);
      transaction._transactionReleasing = false;
    }
  }),

  /**
   * Override of {@link RawSQL#execute}.
   *
   * @private
   * @see {@link RawSQL#execute}
   */
  execute: function() {
    var self = this;
    return this.beforeExecution()
      .then(this._super)
      .then(function() {
        return self.afterExecution();
      });
  },

  /**
   * Documentation forthcoming.
   */
  begin: function() {
    var dup = this._dup();
    dup._transactionAction = 'begin';
    dup._transaction = dup;
    return dup;
  },

  /**
   * Documentation forthcoming.
   */
  commit: function() {
    var dup = this._dup();
    dup._transactionAction = 'commit';
    dup._transaction._transactionReleasing = true;
    dup._transaction._transactionClosed = true;
    return dup;
  },

  /**
   * Documentation forthcoming.
   */
  rollback: function() {
    var dup = this._dup();
    dup._transactionAction = 'rollback';
    dup._transaction._transactionReleasing = true;
    dup._transaction._transactionClosed = true;
    return dup;
  },

  /**
   * Override of {@link RawSQL#sql}.
   *
   * @private
   * @see {@link RawSQL#sql}
   */
  sql: function() {
    return this._transactionAction ?
{'fraction_non_alphanumeric': 0.13623188405797101, 'fraction_numerical': 0.00030511060259344014, 'mean_word_length': 2.6810780460415495, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6940529', 'n_tokens_mistral': 1882, 'n_tokens_neox': 1836, 'n_words': 431}