<commit_before>'use strict';

var BluebirdPromise = require('bluebird');
var Class = require('../util/class');
var Query = require('./query');

/**
 * @since 1.0
 * @public
 * @constructor
 */
var Database = Class.extend(/** @lends Database# */{

  /**
   * Create a new database.
   *
   * @alias Database
   * @param {Object} connection The connection information.
   * @param {String} connection.adapter The adapter to use. Possible choices
   * are:
   *   - pg
   *   - mysql
   *   - sqlite
   * @param {String} connection.database The database to connect to.
   * @param {String} connection.username The username to connect with.
   * @param {String} connection.password The password to connect with.
   */
  init: function(connection) {
    if (!connection) { throw new Error('Missing connection information.'); }

    var Adapter;
    if (connection.adapter && typeof connection.adapter.__class__ === 'function') {
      Adapter = connection.adapter;
    }
    if (!Adapter) {
      try { Adapter = require('./adapters/' + connection.adapter); }
      catch (e) {
        if (e.code === 'MODULE_NOT_FOUND') {
          throw new Error('No adapter found for ' + connection.adapter);
        }
        else { throw e; }
      }
    }
    this._adapter = Adapter.create();
    this._query = Query.create(this._adapter);

    this._ready = BluebirdPromise.bind(this)
    .then(function() {
      return this._adapter.connect(connection);
    })
    .then(function() { return this; });
  },

  /**
   * Ensure the database is ready.
   *
   * @return {Promise} A promise that resolves with the database instance
   * when the database is fully connected.
   */
  ready: function() {
    return this._ready;
  },

  /**
   * Disconnect from the database.
   *
   * @return {Promise} A promise indicating that the database has been
   * disconnected.
   */
  disconnect: function() {
    return this._adapter.disconnect();
  }
});

/**
 * Documentation forthcoming.
 *
 * @private
 * @function Database~query
 */
var query = function(name) {
  return function() {
    return this._query[name].apply(this._query, arguments);
  };
};

Database.reopen(/** @lends Database# */{
  /**
   * Documentation forthcoming.
   *
   * @since 1.0
   * @public
   * @method
   */
  select: query('select')
});

module.exports = Database.reopenClass({ __name__: 'Database' });<commit_msg>Added query accessor & documentation.<commit_after>'use strict';

var BluebirdPromise = require('bluebird');
var Class = require('../util/class');
var Query = require('./query');

/**
 * @since 1.0
 * @public
 * @constructor
 */
var Database = Class.extend(/** @lends Database# */{

  /**
   * Create a new database.
   *
   * @alias Database
   * @param {Object} connection The connection information.
   * @param {String} connection.adapter The adapter to use. Possible choices
   * are:
   *   - pg
   *   - mysql
   *   - sqlite
   * @param {String} connection.database The database to connect to.
   * @param {String} connection.username The username to connect with.
   * @param {String} connection.password The password to connect with.
   */
  init: function(connection) {
    if (!connection) { throw new Error('Missing connection information.'); }

    var Adapter;
    if (connection.adapter && typeof connection.adapter.__class__ === 'function') {
      Adapter = connection.adapter;
    }
    if (!Adapter) {
      try { Adapter = require('./adapters/' + connection.adapter); }
      catch (e) {
        if (e.code === 'MODULE_NOT_FOUND') {
          throw new Error('No adapter found for ' + connection.adapter);
        }
        else { throw e; }
      }
    }
    this._adapter = Adapter.create();
    this._query = Query.create(this._adapter);

    this._ready = BluebirdPromise.bind(this)
    .then(function() {
      return this._adapter.connect(connection);
    })
    .then(function() { return this; });
  },

  /**
   * Ensure the database is ready.
   *
   * @return {Promise} A promise that resolves with the database instance
   * when the database is fully connected.
   */
  ready: function() {
    return this._ready;
  },

  /**
   * Disconnect from the database.
   *
   * @return {Promise} A promise indicating that the database has been
   * disconnected.
   */
  disconnect: function() {
    return this._adapter.disconnect();
  }
});

/**
 * Documentation forthcoming.
 *
 * @private
 * @function Database~query
 */
var query = function(name) {
  return function() {
    return this._query[name].apply(this._query, arguments);
  };
};

/**
 * Access to a query object that is tied to this database.
 *
 * @name Database#query
 * @since 1.0
 * @public
 * @type {Query}
 * @readonly
 */
Database.defineAccessor('query');

Database.reopen(/** @lends Database# */ {

  /**
   * Shortcut for `db.query.select()`.
   *
   * @since 1.0
   * @public
   * @method
   * @see {@link Database#query}
   * @see {@link Query#select}
   */
  select: query('select')

});

module.exports = Database.reopenClass({ __name__: 'Database' });
{'fraction_non_alphanumeric': 0.14552683896620278, 'fraction_numerical': 0.0019880715705765406, 'mean_word_length': 2.8142532221379835, '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': '5351417', 'n_tokens_mistral': 1504, 'n_tokens_neox': 1477, 'n_words': 414}