<commit_before>var mysql = require('mysql');
var Query = require('./query');

/**
 * A mysql client
 *
 * @constructor
 * @param {Object<string,string>} config The mysql configuration
 */
var Client = function(config) {
	this.pool = mysql.createPool(config);
	this.table = config.table;
	// Directly extends Query object
	// This is risky but this keeps client and query object coherent
	// Moreover this is less code to maintain
	var self = this;
	for (var prop in Query) {
		(function(prop) {
			// This allows to call Query functions through the Client class
			// Client.<query_method>(connection, ..., callback);
			self[prop] = function() {
				var args = Array.prototype.slice.call(arguments);
				var connection = args.shift();
				var callback = args.pop();
				var query = Query[prop].apply(null, args);
				connection.query(query.statement, query.args, function(err, res) {
					if (err)
						throw err;
					callback(res);
				});
			};
			// Same methods with 'p' prefix (that stands for 'pool')
			// In this case, the connection is got from the pool
			// Client.p<query_method>(..., callback);
			self['p' + prop] = function() {
				var callback = arguments[arguments.length - 1];
				var query = Query[prop].apply(null, arguments);
				self.pool.getConnection(function(err, connection) {
					if (err)
						throw err;
					connection.query(query.statement, query.args, function(err, res) {
						connection.release();
						if (err)
							throw err;
						callback(res);
					});
				});
			};
		})(prop);
	}
	this.query = this.raw;
	this.pquery = this.praw;
};

/**
 * Run multiple queries at once
 * To use it, you have to explicitly enable multipleStatements in the config
 * However this is not always recommanded
 *
 * @param {mysql.Connection} connection
 * @param {Array.Query} queries
 * @param {Function} callback
 */
Client.prototype.multi = function(connection, queries, callback) {
	var statement = 'BEGIN';
	var args = [];
	for (var i = 0, len = queries.length; i < len; ++i) {
		var query = queries[i];
		statement += ';' + query.statement;<commit_msg>Add getConnection to retrieve a connection - handles exception<commit_after>var mysql = require('mysql');
var Query = require('./query');

/**
 * A mysql client
 *
 * @constructor
 * @param {Object<string,string>} config The mysql configuration
 */
var Client = function(config) {
	this.pool = mysql.createPool(config);
	this.table = config.table;
	// Directly extends Query object
	// This is risky but this keeps client and query object coherent
	// Moreover this is less code to maintain
	var self = this;
	for (var prop in Query) {
		(function(prop) {
			// This allows to call Query functions through the Client class
			// Client.<query_method>(connection, ..., callback);
			self[prop] = function() {
				var args = Array.prototype.slice.call(arguments);
				var connection = args.shift();
				var callback = args.pop();
				var query = Query[prop].apply(null, args);
				connection.query(query.statement, query.args, function(err, res) {
					if (err)
						throw err;
					callback(res);
				});
			};
			// Same methods with 'p' prefix (that stands for 'pool')
			// In this case, the connection is got from the pool
			// Client.p<query_method>(..., callback);
			self['p' + prop] = function() {
				var callback = arguments[arguments.length - 1];
				var query = Query[prop].apply(null, arguments);
				self.pool.getConnection(function(err, connection) {
					if (err)
						throw err;
					connection.query(query.statement, query.args, function(err, res) {
						connection.release();
						if (err)
							throw err;
						callback(res);
					});
				});
			};
		})(prop);
	}
	this.query = this.raw;
	this.pquery = this.praw;
};

/**
 * Get a connection from the pool
 *
 * @param {Function} callback
 */
Client.prototype.getConnection = function(callback) {
	this.pool.getConnection(function(err, connection) {
		if (err)
			throw err;
		// The callback should release the connection itself
		callback(connection);
	});
};

/**
 * Run multiple queries at once
 * To use it, you have to explicitly enable multipleStatements in the config
 * However this is not always recommanded
 *
 * @param {mysql.Connection} connection
 * @param {Array.Query} queries
 * @param {Function} callback
 */
Client.prototype.multi = function(connection, queries, callback) {
	var statement = 'BEGIN';
	var args = [];
	for (var i = 0, len = queries.length; i < len; ++i) {
		var query = queries[i];
		statement += ';' + query.statement;
{'fraction_non_alphanumeric': 0.13817537643932684, 'fraction_numerical': 0.0008857395925597874, 'mean_word_length': 3.26937618147448, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, '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': '2168821', 'n_tokens_mistral': 1545, 'n_tokens_neox': 1318, 'n_words': 427}