Skip to content

Commit

Permalink
v0.0.16: Use mysql pool + format
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Dobson authored and Matthew Dobson committed Jul 4, 2013
1 parent 5af2fa9 commit f33194c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 72 deletions.
96 changes: 28 additions & 68 deletions mysql.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,55 +9,14 @@ var db = function (settings, logging) {
return this;
};

db.prototype.getConnection = function () {
// Test connection health before returning it to caller.
if (
this.connection && this.connection._socket
&& this.connection._socket.readable
&& this.connection._socket.writable
) {
return this.connection;
}

if (this.connection) {
console.log("UNHEALTHY SQL CONNECTION; RECONNECTING TO SQL.");
} else {
console.log("CONNECTING TO SQL.");
}

var connection = mysql.createConnection(this.settings);

connection.connect(function(err) {
if (err) {
console.log("SQL CONNECT ERROR: " + err);
} else {
console.log("SQL CONNECT SUCCESSFUL.");
}
});

connection.on("close", function (err) {
console.log("SQL CONNECTION CLOSED.");
});

connection.on("error", function (err) {
console.log("SQL CONNECTION ERROR: " + err);
});

this.connection = connection;

return this.connection;
db.prototype.createPool = function () {
this.pool = mysql.createPool(this.settings);
};

db.prototype.addTicks = function (string) {
return "`" + string + "`";
};

db.prototype.format = function (sql, params) {
var connection = this.getConnection();

return connection.format(sql, params);
};

db.prototype.riddlify = function () {
return "?";
};
Expand Down Expand Up @@ -98,30 +57,29 @@ db.prototype.build = function (tableName, options, params) {
if (options.offset) sql += " OFFSET " + options.offset;
sql += ";";

if (sqlParams && sqlParams.length) sql = this.format(sql, sqlParams);

return sql;
return { sql: sql, params: sqlParams };
};

db.prototype.run = function (sql, _cb) {
if (this.logging) console.log(sql);

var connection = this.getConnection();
db.prototype.run = function (sql, params, _cb) {
var self = this;

connection.query(sql, _cb);
return this;
};
this.pool.getConnection(function (err, connection) {
if (err) return _cb(err);

db.prototype.useDatabase = function (dbName) {
var connection = this.getConnection();
if (self.logging) console.log(connection.format(sql, params));

connection.query(sql, params, function () {
connection.end();
if (_cb) _cb.apply(null, arguments);
});
});

connection.useDatabase(dbName);
return this;
};

db.prototype.listTables = function (_cb) {
var self = this;
return this.run("SHOW TABLES", function (err, rows) {
return this.run("SHOW TABLES", null, function (err, rows) {
if (err) return _cb(err);

rows = rows.map(function (row) {
Expand All @@ -132,7 +90,9 @@ db.prototype.listTables = function (_cb) {
};

db.prototype.listFields = function (tableName, _cb) {
return this.run("SHOW FULL COLUMNS FROM " + this.addTicks(tableName), function (err, rows) {
var sql = "SHOW FULL COLUMNS FROM " + this.addTicks(tableName);

return this.run(sql, null, function (err, rows) {
if (err) return _cb(err);

var o = {};
Expand All @@ -145,14 +105,14 @@ db.prototype.listFields = function (tableName, _cb) {

db.prototype.select = function (tableName, options, params, _cb) {
options.select = true;
var sql = this.build(tableName, options, params);
return this.run(sql, _cb);
var obj = this.build(tableName, options, params);
return this.run(obj.sql, obj.params, _cb);
};

db.prototype.update = function (tableName, options, params, _cb) {
options.update = true;
var sql = this.build(tableName, options, params);
return this.run(sql, _cb);
var obj = this.build(tableName, options, params);
return this.run(obj.sql, obj.params, _cb);
};

db.prototype.save = function (tableName, fields, obj, onDupeKey, _cb) {
Expand All @@ -177,8 +137,8 @@ db.prototype.save = function (tableName, fields, obj, onDupeKey, _cb) {
if (onDupeKey) sql += " " + onDupeKey;
else sql += fields.map(function (field) { return " `" + field + "` = VALUES(`" + field + "`)"; }).join(", ");
sql += ";";
var sqlReady = this.format(sql, params);
this.run(sqlReady, _cb);

this.run(sql, params, _cb);
};

db.prototype.delete = function (tableName, options, params, _cb) {
Expand All @@ -189,15 +149,15 @@ db.prototype.delete = function (tableName, options, params, _cb) {
if (options.offset) return _cb("Offset cannot be used for delete queries!");

options.delete = true;
var sql = this.build(tableName, options, params);
return this.run(sql, _cb);
var obj = this.build(tableName, options, params);
return this.run(obj.sql, obj.params, _cb);
};

db.prototype.count = function (tableName, options, params, _cb) {
options.select = true;
options.fields = ["COUNT(*)"];
var sql = this.build(tableName, options, params);
return this.run(sql, function (err, rows) {
var obj = this.build(tableName, options, params);
return this.run(obj.sql, obj.params, function (err, rows) {
if (err) return _cb(err);

_cb(null, rows[0]["COUNT(*)"]);
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "serious-sql",
"description": "Library that provides a cool way of interacting with existing MySQL databases. Use your own conventions, and it will automagically figure out associations. It has efficient recursive prefetching, as well as table aliases, chainable interfaces and other features.",
"version": "0.0.15",
"version": "0.0.16",
"author": "Matt Dobson <mjadobson@gmail.com>",
"dependencies": {
"lingo": ">= 0.0.4",
"mysql": "2.0.0-alpha7"
"mysql": "2.0.0-alpha8"
},
"keywords": ["mysql"],
"repository": "git://github.com/sugarstack/serious-sql",
Expand Down
6 changes: 4 additions & 2 deletions serious.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ Serious.prototype.set = function (setting, val) {
Serious.prototype.start = function (_cb) {
var self = this
, counter = 0;

this.client = new this.settings.client(this.settings.connection, this.settings.logging);

this.client.createPool();

this.client.listTables(function (err, tableNames) {
if (err) return _cb(err);
Expand Down Expand Up @@ -75,7 +77,7 @@ Serious.prototype.query = function () {
params = args;
}

this.client.run(this.client.format(sql, params), _cb);
this.client.run(sql, params, _cb);
return this;
};

Expand Down

0 comments on commit f33194c

Please sign in to comment.