<commit_before>var ErrorCodes = require("../../ErrorCodes");

exports.drop = function (driver, opts, cb) {
	var i, queries = [], pending;

	queries.push("DROP TABLE IF EXISTS " + driver.query.escapeId(opts.table));

	for (i = 0; i < opts.many_associations.length; i++) {
		queries.push("DROP TABLE IF EXISTS " + driver.query.escapeId(opts.many_associations[i].mergeTable));
	}

	pending = queries.length;

	if (driver.opts.pool) {
		return driver.db.pool.getConnection(function (err, db) {
			for (i = 0; i < queries.length; i++) {
				db.query(queries[i], function (err) {
					if (--pending === 0) {
						return cb(err);
					}
				});
			}
		});
	}

	for (i = 0; i < queries.length; i++) {
		driver.db.query(queries[i], function (err) {
			if (--pending === 0) {
				return cb(err);
			}
		});
	}
};

exports.sync = function (driver, opts, cb) {
	var queries = [];
	var definitions = [];
	var k, i, pending;
	var primary_keys = opts.id.map(function (k) { return driver.query.escapeId(k); });
	var keys = [];

	for (i = 0; i < opts.id.length; i++) {
		if (opts.properties.hasOwnProperty(opts.id[i])) continue;

		keys.push(driver.query.escapeId(opts.id[i]));
	}

	for (i = 0; i < keys.length; i++) {
		definitions.push(keys[i] + " INT(10) UNSIGNED NOT NULL");
	}
	if (opts.id.length == 1 && !opts.extension) {
		definitions[definitions.length - 1] += " AUTO_INCREMENT";
	}

	for (k in opts.properties) {
		definitions.push(buildColumnDefinition(driver, k, opts.properties[k]));
	}

	for (i = 0; i < opts.one_associations.length; i++) {
		if (opts.one_associations[i].extension) continue;
		if (opts.one_associations[i].reversed) continue;
		for (k in opts.one_associations[i].field) {
			definitions.push(buildColumnDefinition(driver, k, opts.one_associations[i].field[k]));
		}
	}

	for (k in opts.properties) {
		if (opts.properties[k].unique === true) {
			definitions.push("UNIQUE KEY " + driver.query.escapeId(k) + " (" + driver.query.escapeId(k) + ")");
		} else if (opts.properties[k].index) {
			definitions.push("INDEX (" + driver.query.escapeId(k) + ")");
		}
	}
	
	for (i = 0; i < opts.one_associations.length; i++) {
		if (opts.one_associations[i].extension) continue;
		if (opts.one_associations[i].reversed) continue;
		for (k in opts.one_associations[i].field) {
			definitions.push("INDEX (" + driver.query.escapeId(k) + ")");
		}
	}

	for (i = 0; i < opts.indexes.length; i++) {
		definitions.push("INDEX (" + opts.indexes[i].split(/[,;]+/).map(function (el) {
			return driver.query.escapeId(el);
		}).join(", ") + ")");
	}

	definitions.push("PRIMARY KEY (" + primary_keys.join(", ") + ")");

	queries.push(
		"CREATE TABLE IF NOT EXISTS " + driver.query.escapeId(opts.table) +
		" (" + definitions.join(", ") + ")"
	);

	for (i = 0; i < opts.many_associations.length; i++) {
		definitions = [];

		for (k in opts.many_associations[i].mergeId) {
			definitions.push(buildColumnDefinition(driver, k, opts.many_associations[i].mergeId[k]));
		}

		for (k in opts.many_associations[i].mergeAssocId) {
			definitions.push(buildColumnDefinition(driver, k, opts.many_associations[i].mergeAssocId[k]));
		}

		for (k in opts.many_associations[i].props) {
			definitions.push(buildColumnDefinition(driver, k, opts.many_associations[i].props[k]));
		}

		var index = null;
		for (k in opts.many_associations[i].mergeId) {
			if (index == null) index = driver.query.escapeId(k);
			else index += ", " + driver.query.escapeId(k);
		}

		for (k in opts.many_associations[i].mergeAssocId) {
			if (index == null) index = driver.query.escapeId(k);
			else index += ", " + driver.query.escapeId(k);
		}
		

		definitions.push("INDEX (" + index + ")");
		queries.push(
			"CREATE TABLE IF NOT EXISTS " + driver.query.escapeId(opts.many_associations[i].mergeTable) +
			" (" + definitions.join(", ") + ")"
		);
	}

	pending = queries.length;

	if (driver.opts.pool) {
		return driver.db.pool.getConnection(function (err, db) {
			for (i = 0; i < queries.length; i++) {
				db.query(queries[i], function (err) {
					if (--pending === 0) {
						return cb(err);
					}
				});
			}
		});
	}
	
	for (i = 0; i < queries.length; i++) {
		driver.db.query(queries[i], function (err) {
			if (--pending === 0) {
				return cb(err);
			}
		});
	}
};

var colTypes = {
	integer:  { 2: 'SMALLINT', 4: 'INTEGER', 8: 'BIGINT' },
	floating: {                4: 'FLOAT',   8: 'DOUBLE' }
};

function buildColumnDefinition(driver, name, prop) {
	var def;

	switch (prop.type) {
	    case "text":
			if (prop.big === true) {
				def = driver.query.escapeId(name) + " LONGTEXT";
			} else {
				def = driver.query.escapeId(name) + " VARCHAR(" + Math.min(Math.max(parseInt(prop.size, 10) || 255, 1), 65535) + ")";
			}			
			break;
		case "number":
			if (prop.rational === false) {
				def = driver.query.escapeId(name) + " " + colTypes.integer[prop.size || 4];
			} else {
				def = driver.query.escapeId(name) + " " + colTypes.floating[prop.size || 4];
			}
			if (prop.unsigned === true) {
				def += " UNSIGNED";
			}
			break;
		case "boolean":
			def = driver.query.escapeId(name) + " BOOLEAN";
			break;<commit_msg>Changes mysql DDL to use execQuery instead of checking for pool (#258)<commit_after>var ErrorCodes = require("../../ErrorCodes");

exports.drop = function (driver, opts, cb) {
	var i, queries = [], pending;

	queries.push("DROP TABLE IF EXISTS " + driver.query.escapeId(opts.table));

	for (i = 0; i < opts.many_associations.length; i++) {
		queries.push("DROP TABLE IF EXISTS " + driver.query.escapeId(opts.many_associations[i].mergeTable));
	}

	pending = queries.length;

	for (i = 0; i < queries.length; i++) {
		driver.execQuery(queries[i], function (err) {
			if (--pending === 0) {
				return cb(err);
			}
		});
	}
};

exports.sync = function (driver, opts, cb) {
	var queries = [];
	var definitions = [];
	var k, i, pending;
	var primary_keys = opts.id.map(function (k) { return driver.query.escapeId(k); });
	var keys = [];

	for (i = 0; i < opts.id.length; i++) {
		if (opts.properties.hasOwnProperty(opts.id[i])) continue;

		keys.push(driver.query.escapeId(opts.id[i]));
	}

	for (i = 0; i < keys.length; i++) {
		definitions.push(keys[i] + " INT(10) UNSIGNED NOT NULL");
	}
	if (opts.id.length == 1 && !opts.extension) {
		definitions[definitions.length - 1] += " AUTO_INCREMENT";
	}

	for (k in opts.properties) {
		definitions.push(buildColumnDefinition(driver, k, opts.properties[k]));
	}

	for (i = 0; i < opts.one_associations.length; i++) {
		if (opts.one_associations[i].extension) continue;
		if (opts.one_associations[i].reversed) continue;
		for (k in opts.one_associations[i].field) {
			definitions.push(buildColumnDefinition(driver, k, opts.one_associations[i].field[k]));
		}
	}

	for (k in opts.properties) {
		if (opts.properties[k].unique === true) {
			definitions.push("UNIQUE KEY " + driver.query.escapeId(k) + " (" + driver.query.escapeId(k) + ")");
		} else if (opts.properties[k].index) {
			definitions.push("INDEX (" + driver.query.escapeId(k) + ")");
		}
	}

	for (i = 0; i < opts.one_associations.length; i++) {
		if (opts.one_associations[i].extension) continue;
		if (opts.one_associations[i].reversed) continue;
		for (k in opts.one_associations[i].field) {
			definitions.push("INDEX (" + driver.query.escapeId(k) + ")");
		}
	}

	for (i = 0; i < opts.indexes.length; i++) {
		definitions.push("INDEX (" + opts.indexes[i].split(/[,;]+/).map(function (el) {
			return driver.query.escapeId(el);
		}).join(", ") + ")");
	}

	definitions.push("PRIMARY KEY (" + primary_keys.join(", ") + ")");

	queries.push(
		"CREATE TABLE IF NOT EXISTS " + driver.query.escapeId(opts.table) +
		" (" + definitions.join(", ") + ")"
	);

	for (i = 0; i < opts.many_associations.length; i++) {
		definitions = [];

		for (k in opts.many_associations[i].mergeId) {
			definitions.push(buildColumnDefinition(driver, k, opts.many_associations[i].mergeId[k]));
		}

		for (k in opts.many_associations[i].mergeAssocId) {
			definitions.push(buildColumnDefinition(driver, k, opts.many_associations[i].mergeAssocId[k]));
		}

		for (k in opts.many_associations[i].props) {
			definitions.push(buildColumnDefinition(driver, k, opts.many_associations[i].props[k]));
		}

		var index = null;
		for (k in opts.many_associations[i].mergeId) {
			if (index == null) index = driver.query.escapeId(k);
			else index += ", " + driver.query.escapeId(k);
		}

		for (k in opts.many_associations[i].mergeAssocId) {
			if (index == null) index = driver.query.escapeId(k);
			else index += ", " + driver.query.escapeId(k);
		}


		definitions.push("INDEX (" + index + ")");
		queries.push(
			"CREATE TABLE IF NOT EXISTS " + driver.query.escapeId(opts.many_associations[i].mergeTable) +
			" (" + definitions.join(", ") + ")"
		);
	}

	pending = queries.length;

	for (i = 0; i < queries.length; i++) {
		driver.execQuery(queries[i], function (err) {
			if (--pending === 0) {
				return cb(err);
			}
		});
	}
};

var colTypes = {
	integer:  { 2: 'SMALLINT', 4: 'INTEGER', 8: 'BIGINT' },
	floating: {                4: 'FLOAT',   8: 'DOUBLE' }
};

function buildColumnDefinition(driver, name, prop) {
	var def;

	switch (prop.type) {
	    case "text":
			if (prop.big === true) {
				def = driver.query.escapeId(name) + " LONGTEXT";
			} else {
				def = driver.query.escapeId(name) + " VARCHAR(" + Math.min(Math.max(parseInt(prop.size, 10) || 255, 1), 65535) + ")";
			}
			break;
		case "number":
			if (prop.rational === false) {
				def = driver.query.escapeId(name) + " " + colTypes.integer[prop.size || 4];
			} else {
				def = driver.query.escapeId(name) + " " + colTypes.floating[prop.size || 4];
			}
			if (prop.unsigned === true) {
				def += " UNSIGNED";
			}
			break;
		case "boolean":
			def = driver.query.escapeId(name) + " BOOLEAN";
			break;
{'fraction_non_alphanumeric': 0.19159165302782324, 'fraction_numerical': 0.007467266775777414, 'mean_word_length': 3.507607192254495, 'pattern_counts': {'":': 6, '<': 23, '<?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': '2725733', 'n_tokens_mistral': 4006, 'n_tokens_neox': 3578, 'n_words': 696}