<commit_before>/**
 * @author <NAME>
 * @description MYSQL-JSON CONNECTION MODULE
 * @date 05/07/2017
 */

"use strict";

var Mysql = require('mysql');


module.exports = function(host) {

	var _self = this;

	/**
	 * CONNECT
	 * Used to open connection to host
	 * @param callback
	 */
	_self.connect = function(callback) {
		var connection = Mysql.createConnection(host);
		connection.connect(function (err) {
			callback(err, connection);
		});
	};


	/**
	 * QUERY
	 * Used to launch a Mysql query
	 * @param mysqlQuery
	 * @param callback
	 */
	_self.query = function(mysqlQuery, callback) {
		_self.connect(function(err, connection) {
			if (err) {
				callback(err, null);
			}
			else {
				connection.query(mysqlQuery, function(err, response) {
					connection.end();
					if (err) {
						callback(err, null);
					}
					else {
						callback(null, response);
					}
				});
			}
		});
	};


	/**
	 * INSERT
	 * Used to build and launch a Mysql insert query
	 * @param tableName
	 * @param dataToInsert
	 * @param callback
	 */
	_self.insert = function(tableName, dataToInsert, callback) {
		if (tableName && dataToInsert) {
			if (Object.size(dataToInsert) > 0) {
				var mysqlQuery = 'INSERT INTO `' + tableName + '` (';
				var i = 0;
				for (var index in dataToInsert) {
					mysqlQuery += index;
					mysqlQuery += (i + 1 == Object.size(dataToInsert) ? ') ' : ', ');
					i++;
				}
				i = 0;
				mysqlQuery += 'VALUES (';
				for (index in dataToInsert) {
					mysqlQuery += typeof(dataToInsert[index]) == 'string' ? '"' + dataToInsert[index].replace(/"/g, '\\"') + '"' : dataToInsert[index];
					mysqlQuery += (i + 1 == Object.size(dataToInsert) ? ') ' : ', ');
					i++;
				}
				_self.query(mysqlQuery, callback);
			}
			else {
				callback(new Error('mysql-json [insert]: data has to contain at least one field'), null);
			}
		}
		else {
			callback(new Error('mysql-json [insert]: Require at least a tableName and a data'), null);
		}
	};


	/**
	 * UPDATE
	 * Used to build and launch a Mysql update query
	 * @param tableName
	 * @param data
	 * @param conditions
	 * @param callback
	 */
	_self.update = function(tableName, data, conditions, callback) {
		if (tableName && data) {
			if (Object.size(data) > 0) {
				var mysqlQuery = 'UPDATE `' + tableName + '` SET ';
				var i = 0;
				for (var index in data) {
					mysqlQuery += index + '';
					mysqlQuery += '=';
					mysqlQuery += typeof(data[index]) == 'string' ? '"' + data[index].replace(/"/g, '\\"') + '"' : data[index];
					mysqlQuery += (i + 1 == Object.size(data) ? ' ' : ', ');
					i++;
				}
				i = 0;
				if (conditions) {
					mysqlQuery += 'WHERE ';
					for (index in conditions) {
						mysqlQuery += index;
						mysqlQuery += " ";
						mysqlQuery += (conditions[index].operator || '=');
						mysqlQuery += " ";
						mysqlQuery += typeof(conditions[index].value) == 'string' ? '"' + conditions[index].value.replace(/"/g, '\\"') + '"' : conditions[index].value;
						mysqlQuery += (i + 1 == Object.size(conditions) ? ' ' : ' AND ');
						i++;
					}
				}
				_self.query(mysqlQuery, callback);
			}
			else {
				callback(new Error('mysql-json [update]: data has to contain at least one field'), null);
			}
		}
		else {
			callback(new Error('mysql-json [update]: Require at least a tableName and a data'), null);
		}
	};


	/**
	 * DELETE
	 * Used to build and launch a delete Mysql query
	 * @param tableName
	 * @param conditions
	 * @param callback
	 */
	_self.delete = function(tableName, conditions, callback) {
		if (tableName) {
			var mysqlQuery = "DELETE FROM `" + tableName + "` ";
			if (conditions) {
				mysqlQuery += "WHERE ";
				var i = 0;
				for (var index in conditions) {
					mysqlQuery += index;
					mysqlQuery += " ";
					mysqlQuery += (conditions[index].operator || "=");
					mysqlQuery += " ";
					mysqlQuery += typeof(conditions[index].value) == 'string' ? '"' + conditions[index].value.replace(/"/g, '\\"') + '"' : conditions[index].value;
					mysqlQuery += (i + 1 == Object.size(conditions) ? " " : " AND ");
					i++;
				}
			}
			_self.query(mysqlQuery, callback);
		}
		else {
			callback(new Error('mysql-json [delete]: Require at least a tableName'), null);
		}
	};


};<commit_msg>Added find function to look up by Primary Key<commit_after>/**
 * @author <NAME>
 * @description MYSQL-JSON CONNECTION MODULE
 * @date 05/07/2017
 */

"use strict";

var Mysql = require('mysql');


module.exports = function(host) {

	var _self = this;

	/**
	 * CONNECT
	 * Used to open connection to host
	 * @param callback
	 */
	_self.connect = function(callback) {
		var connection = Mysql.createConnection(host);
		connection.connect(function (err) {
			callback(err, connection);
		});
	};


    /**
     * FIND
     * Used to query for a single record by primary key
     * @param tableName
     * @param id
     * @param callback
     */
    _self.find = function(tableName, id, callback) {
        if(id > 0) {

            var primaryKeyId = 'id';
            var primaryKeyQuery = 'SHOW KEYS FROM `' + tableName + '` WHERE Key_name = \'PRIMARY\'';
            _self.query(primaryKeyQuery, function(err, res) {
                if(err) {
                    console.log(err);
                    callback(new Error('mysql-json [find]: Cannot detect primary key field'), null);
                } else {
                    primaryKeyId =  res[0]['Column_name'];
                }

                var mysqlQuery = 'SELECT * FROM `' + tableName + '` WHERE ' + primaryKeyId + '=' + id;
                _self.query(mysqlQuery, callback);
            });

        }
        else {
            callback(new Error('mysql-json [find]: id missing or invalid'), null);
        }
    };

    
	/**
	 * QUERY
	 * Used to launch a Mysql query
	 * @param mysqlQuery
	 * @param callback
	 */
	_self.query = function(mysqlQuery, callback) {
		_self.connect(function(err, connection) {
			if (err) {
				callback(err, null);
			}
			else {
				connection.query(mysqlQuery, function(err, response) {
					connection.end();
					if (err) {
						callback(err, null);
					}
					else {
						callback(null, response);
					}
				});
			}
		});
	};


	/**
	 * INSERT
	 * Used to build and launch a Mysql insert query
	 * @param tableName
	 * @param dataToInsert
	 * @param callback
	 */
	_self.insert = function(tableName, dataToInsert, callback) {
		if (tableName && dataToInsert) {
			if (Object.size(dataToInsert) > 0) {
				var mysqlQuery = 'INSERT INTO `' + tableName + '` (';
				var i = 0;
				for (var index in dataToInsert) {
					mysqlQuery += index;
					mysqlQuery += (i + 1 == Object.size(dataToInsert) ? ') ' : ', ');
					i++;
				}
				i = 0;
				mysqlQuery += 'VALUES (';
				for (index in dataToInsert) {
					mysqlQuery += typeof(dataToInsert[index]) == 'string' ? '"' + dataToInsert[index].replace(/"/g, '\\"') + '"' : dataToInsert[index];
					mysqlQuery += (i + 1 == Object.size(dataToInsert) ? ') ' : ', ');
					i++;
				}
				_self.query(mysqlQuery, callback);
			}
			else {
				callback(new Error('mysql-json [insert]: data has to contain at least one field'), null);
			}
		}
		else {
			callback(new Error('mysql-json [insert]: Require at least a tableName and a data'), null);
		}
	};


	/**
	 * UPDATE
	 * Used to build and launch a Mysql update query
	 * @param tableName
	 * @param data
	 * @param conditions
	 * @param callback
	 */
	_self.update = function(tableName, data, conditions, callback) {
		if (tableName && data) {
			if (Object.size(data) > 0) {
				var mysqlQuery = 'UPDATE `' + tableName + '` SET ';
				var i = 0;
				for (var index in data) {
					mysqlQuery += index + '';
					mysqlQuery += '=';
					mysqlQuery += typeof(data[index]) == 'string' ? '"' + data[index].replace(/"/g, '\\"') + '"' : data[index];
					mysqlQuery += (i + 1 == Object.size(data) ? ' ' : ', ');
					i++;
				}
				i = 0;
				if (conditions) {
					mysqlQuery += 'WHERE ';
					for (index in conditions) {
						mysqlQuery += index;
						mysqlQuery += " ";
						mysqlQuery += (conditions[index].operator || '=');
						mysqlQuery += " ";
						mysqlQuery += typeof(conditions[index].value) == 'string' ? '"' + conditions[index].value.replace(/"/g, '\\"') + '"' : conditions[index].value;
						mysqlQuery += (i + 1 == Object.size(conditions) ? ' ' : ' AND ');
						i++;
					}
				}
				_self.query(mysqlQuery, callback);
			}
			else {
				callback(new Error('mysql-json [update]: data has to contain at least one field'), null);
			}
		}
		else {
			callback(new Error('mysql-json [update]: Require at least a tableName and a data'), null);
		}
	};


	/**
	 * DELETE
	 * Used to build and launch a delete Mysql query
	 * @param tableName
	 * @param conditions
	 * @param callback
	 */
	_self.delete = function(tableName, conditions, callback) {
		if (tableName) {
			var mysqlQuery = "DELETE FROM `" + tableName + "` ";
			if (conditions) {
				mysqlQuery += "WHERE ";
				var i = 0;
				for (var index in conditions) {
					mysqlQuery += index;
					mysqlQuery += " ";
					mysqlQuery += (conditions[index].operator || "=");
					mysqlQuery += " ";
					mysqlQuery += typeof(conditions[index].value) == 'string' ? '"' + conditions[index].value.replace(/"/g, '\\"') + '"' : conditions[index].value;
					mysqlQuery += (i + 1 == Object.size(conditions) ? " " : " AND ");
					i++;
				}
			}
			_self.query(mysqlQuery, callback);
		}
		else {
			callback(new Error('mysql-json [delete]: Require at least a tableName'), null);
		}
	};


};
{'fraction_non_alphanumeric': 0.16728115453492046, 'fraction_numerical': 0.0044243126514273675, 'mean_word_length': 2.558470764617691, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 10, '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': '5854880', 'n_tokens_mistral': 3736, 'n_tokens_neox': 2999, 'n_words': 758}