{"size": 2691, "ext": "js", "max_stars_repo_path": "node_modules/db/types/mysql.js", "max_stars_repo_name": "ellennewellevans/newsScraper", "max_stars_repo_stars_event_min_datetime": "2019-12-07T03:21:50.000Z", "max_stars_repo_stars_event_max_datetime": "2019-12-07T03:21:50.000Z", "max_issues_repo_path": "node_modules/db/types/mysql.js", "max_issues_repo_name": "ellennewellevans/newsScraper", "max_issues_repo_issues_event_min_datetime": "2020-09-07T00:28:36.000Z", "max_issues_repo_issues_event_max_datetime": "2021-05-08T03:38:10.000Z", "max_forks_repo_path": "node_modules/db/types/mysql.js", "max_forks_repo_name": "ellennewellevans/newsScraper", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": 2, "max_forks_count": null, "avg_line_length": 20.0820895522, "max_line_length": 128, "alphanum_fraction": 0.5533259012}


let mysql = require("mysql");
let sleepless = require("sleepless");
let runq = require("runq");

exports.connect = function(cfg) {

	var db = {};
	db.cfg = cfg;

	cfg.connectionLimit = cfg.connectionLimit || 10;
	db.con = mysql.createPool( cfg );
	db.con.on('error', function(err) {
		console.warn(err);
		throw err;
	});

	db.end = function() {
		db.con.end();
	}

	db.read_all = function( sql, args, cb ) {
		db.con.query( sql, args, function( err, rows ) {
			cb( err, err ? [] : rows );
		});
	}

	db.read_one = function( sql, args, cb ) {
		db.con.query( sql, args, function( err, rows ) {
			var rec = null;
			if(!err && rows.length > 0) {
				rec = rows[0];
			}
			cb( err, rec );
		});
	}

	db.read_col = function( sql, args, fld, cb ) {
		db.read_one( sql, args, function( err, rec ) {
			var val = rec ? rec[fld] : null;
			cb( err, val );
		});
	}


	db.update = function(sql, args, cb) {
		db.con.query( sql, args, function( err, res ) {
			cb( err, err ? null : res.affectedRows, res );
		});
	}

	db.insert = function(sql, args, cb) {
		db.con.query( sql, args, function( err, res ) {
			cb( err, err ? null : res.insertId, res );
		});
	}

	db.insert_obj = function(obj, table, cb) {
		var sql = "select distinct(column_name), data_type from information_schema.columns where table_schema = ? and table_name = ?";
		db.con.query( sql, [cfg.database, table], function(err, res) {
			if(err) { cb(err, null); return; }

			var fields = [];
			var qmarks = [];
			var vals = [];

			for(var i = 0; i < res.length; i++) {
				var c = res[i].column_name;
				val = obj[c];
				if(val !== undefined) {
					fields.push("`"+c+"`");
					qmarks.push("?");
					var v = obj[c];
					if(res[i].data_type == "timestamp" && typeof v == "number") {
						v = ts2my(v);
					}
					else
					if(typeof v === "object") {
						v = o2j(v);
					}
					vals.push(v);
				}
			}

			var sql = "insert into "+table+" ("+fields.join(",")+") values ("+qmarks.join(",")+")";
			db.insert( sql, vals, cb);
		});
	}

	db.delete = function(sql, args, cb) {
		db.con.query( sql, args, function( err, res ) {
			cb( err, err ? null : res.affectedRows, res );
		});
	}


	db.start = function(cb) {
		db.con.query("start transaction", [], cb);
	}

	db.commit = function(cb) {
		db.con.query("commit", [], cb);
	}

	db.rollback = function(cb) {
		db.con.query("rollback", [], cb);
	}


	// hack to access query() call on felix's code
	db.query = function(sql) {
		return db.con.query(sql);
	}
	
	return db;
};


exports.queue = function(db) {
	let rq = runq();
	return {
		add: function(sql, args) {
			rq.push((fin)=>{
				db.con.query(sql, args, fin);
			});
			return o;
		},
		run: rq.run,
	};
};


