<commit_before>'use strict';

var _ = require('lodash');
var BluebirdPromise = require('bluebird');
var Adapter = require('./base');
var mysql = require('mysql');

BluebirdPromise.promisifyAll(mysql);
BluebirdPromise.promisifyAll(require('mysql/lib/Connection').prototype);

var like = Adapter.Translator.like,
  contains = Adapter.Translator.contains,
  startsWith = Adapter.Translator.startsWith,
  endsWith = Adapter.Translator.endsWith,
  regex = Adapter.Translator.regex,
  wrapValue = Adapter.Translator.wrapValue;

var returning = require('./mixins/returning'),
  ExtractPseudoReturn = returning.ExtractPseudoReturn;

/**
 * MySQL Adapter
 *
 * Documentation forthcoming.
 *
 * @since 1.0
 * @public
 * @constructor
 * @extends Adapter
 */
var MySQLAdapter = Adapter.extend(/** @lends MySQLAdapter# */ {

  /**
   * Connect for MySQLAdapter.
   *
   * @method
   * @protected
   * @see {Adapter#_connect}
   */
  _connect: BluebirdPromise.method(function() {
    var connection = this._connection;
    connection = _.isString(connection) ? connection :
      _.pick(connection,
        'user', 'database', 'password', 'port', 'host', 'ssl',
        'localAddress', 'socketPath', 'charset', 'timezone', 'connectTimeout',
        'stringifyObjects', 'insecureAuth', 'typeCast', 'queryFormat',
        'supportBigNumbers', 'bigNumberStrings', 'dateStrings', 'debug', 'trace',
        'multipleStatements', 'flags');

    return mysql.createConnection(connection);
  }),

  /**
   * Disconnect for MySQLAdapter.
   *
   * @method
   * @protected
   * @see {Adapter#_disconnect}
   */
  _disconnect: BluebirdPromise.method(function(connection) {
    return connection.end();
  }),

  /**
   * Execute for MySQLAdapter.
   *
   * @method
   * @private
   * @see {Adapter#_execute}
   */
  _execute: BluebirdPromise.method(function(connection, sql, args, id) {
    return BluebirdPromise.bind({})
    .then(function() {
      return connection.queryAsync(sql, args);
    })
    .spread(function(rows, fields) {
      if (rows.insertId) { id(rows.insertId); }
      return {
        rows: fields ? rows : [],
        fields: _.map(fields, 'name')
      };
    });
  })

});

MySQLAdapter.reopenClass(/** @lends MySQLAdapter */ {

  Grammar: Adapter.Grammar.extend({
    quote: function(string) {
      return '`' + string + '`';
    }
  }, { __name__: 'MySQLGrammar' }),

  Translator: Adapter.Translator.extend({
    predicates: function(p) {
      this._super.apply(this, arguments);
      p('iexact', '%s LIKE %s');
      p('contains', '%s LIKE BINARY %s', wrapValue(like, contains));
      p('icontains', '%s LIKE %s', wrapValue(like, contains));
      p('startsWith', '%s LIKE BINARY %s', wrapValue(like, startsWith));
      p('istartsWith', '%s LIKE %s', wrapValue(like, startsWith));
      p('endsWith', '%s LIKE BINARY %s', wrapValue(like, endsWith));
      p('iendsWith', '%s LIKE %s', wrapValue(like, endsWith));
      p('regex', '%s REGEXP BINARY %s', wrapValue(regex));
      p('iregex', '%s REGEXP %s', wrapValue(regex));
    }
  }, { __name__: 'MySQLTranslator' })
});

MySQLAdapter.reopen(ExtractPseudoReturn);

module.exports = MySQLAdapter.reopenClass({ __name__: 'MySQLAdapter' });<commit_msg>Setting up for embedding pseudo return.<commit_after>'use strict';

var _ = require('lodash');
var BluebirdPromise = require('bluebird');
var Adapter = require('./base');
var mysql = require('mysql');

BluebirdPromise.promisifyAll(mysql);
BluebirdPromise.promisifyAll(require('mysql/lib/Connection').prototype);

var like = Adapter.Translator.like,
  contains = Adapter.Translator.contains,
  startsWith = Adapter.Translator.startsWith,
  endsWith = Adapter.Translator.endsWith,
  regex = Adapter.Translator.regex,
  wrapValue = Adapter.Translator.wrapValue;

var returning = require('./mixins/returning'),
  EmbedPseudoReturn = returning.EmbedPseudoReturn,
  ExtractPseudoReturn = returning.ExtractPseudoReturn;

/**
 * MySQL Adapter
 *
 * Documentation forthcoming.
 *
 * @since 1.0
 * @public
 * @constructor
 * @extends Adapter
 */
var MySQLAdapter = Adapter.extend(/** @lends MySQLAdapter# */ {

  /**
   * Connect for MySQLAdapter.
   *
   * @method
   * @protected
   * @see {Adapter#_connect}
   */
  _connect: BluebirdPromise.method(function() {
    var connection = this._connection;
    connection = _.isString(connection) ? connection :
      _.pick(connection,
        'user', 'database', 'password', 'port', 'host', 'ssl',
        'localAddress', 'socketPath', 'charset', 'timezone', 'connectTimeout',
        'stringifyObjects', 'insecureAuth', 'typeCast', 'queryFormat',
        'supportBigNumbers', 'bigNumberStrings', 'dateStrings', 'debug', 'trace',
        'multipleStatements', 'flags');

    return mysql.createConnection(connection);
  }),

  /**
   * Disconnect for MySQLAdapter.
   *
   * @method
   * @protected
   * @see {Adapter#_disconnect}
   */
  _disconnect: BluebirdPromise.method(function(connection) {
    return connection.end();
  }),

  /**
   * Execute for MySQLAdapter.
   *
   * @method
   * @private
   * @see {Adapter#_execute}
   */
  _execute: BluebirdPromise.method(function(connection, sql, args, id) {
    return BluebirdPromise.bind({})
    .then(function() {
      return connection.queryAsync(sql, args);
    })
    .spread(function(rows, fields) {
      if (rows.insertId) { id(rows.insertId); }
      return {
        rows: fields ? rows : [],
        fields: _.map(fields, 'name')
      };
    });
  })

});

MySQLAdapter.reopenClass(/** @lends MySQLAdapter */ {

  Phrasing: Adapter.Phrasing.extend(),
  Grammar: Adapter.Grammar.extend({
    quote: function(string) {
      return '`' + string + '`';
    }
  }, { __name__: 'MySQLGrammar' }),

  Translator: Adapter.Translator.extend({
    predicates: function(p) {
      this._super.apply(this, arguments);
      p('iexact', '%s LIKE %s');
      p('contains', '%s LIKE BINARY %s', wrapValue(like, contains));
      p('icontains', '%s LIKE %s', wrapValue(like, contains));
      p('startsWith', '%s LIKE BINARY %s', wrapValue(like, startsWith));
      p('istartsWith', '%s LIKE %s', wrapValue(like, startsWith));
      p('endsWith', '%s LIKE BINARY %s', wrapValue(like, endsWith));
      p('iendsWith', '%s LIKE %s', wrapValue(like, endsWith));
      p('regex', '%s REGEXP BINARY %s', wrapValue(regex));
      p('iregex', '%s REGEXP %s', wrapValue(regex));
    }
  }, { __name__: 'MySQLTranslator' })
});

MySQLAdapter.Phrasing.reopen(EmbedPseudoReturn);
MySQLAdapter.reopen(ExtractPseudoReturn);

module.exports = MySQLAdapter.reopenClass({ __name__: 'MySQLAdapter' });
{'fraction_non_alphanumeric': 0.1641609719058466, 'fraction_numerical': 0.0006074411541381929, 'mean_word_length': 3.7655571635311142, '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': '1969696', 'n_tokens_mistral': 2161, 'n_tokens_neox': 2125, 'n_words': 424}