-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
162 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
/** | ||
* Proxy class to node-mysql | ||
* Provides some convenient methods for simple queries | ||
* | ||
* @author: EgorKluch ([email protected]) | ||
* @date: 29.12.13 | ||
*/ | ||
|
||
var mysql = require('mysql'); | ||
var config = require('../config/config'); | ||
|
||
var Mysql = function (callback) { | ||
if (Mysql.instance) { | ||
return Mysql.instance; | ||
} | ||
Mysql.instance = this; | ||
|
||
this.connection = mysql.createConnection(config.mysql); | ||
this.connection.connect(function(err) { | ||
if (err) throw err; | ||
if (callback) callback(this); | ||
}.bind(this)); | ||
|
||
return this; | ||
}; | ||
|
||
/** | ||
* Select rows from {table} who match {where} | ||
* In result row inludes only {columns} fields | ||
* | ||
* @param table | ||
* @param columns | ||
* @param where | ||
* @param callback | ||
*/ | ||
Mysql.prototype.select = function (table, columns, where, callback) { | ||
table = mysql.escapeId(table); | ||
if (where) { | ||
where = ' where ' + this._getWhereString(where); | ||
} | ||
|
||
if (columns === null) { | ||
columns = '*' | ||
} else { | ||
columns = columns.forEach(function (column) { | ||
return mysql.escapeId(column); | ||
}).join(', '); | ||
} | ||
|
||
var query = 'select ' + columns + ' from ' + table + where; | ||
this.query(query, callback); | ||
}; | ||
|
||
/** | ||
* Return first result of this.select method result | ||
* | ||
* @param table | ||
* @param columns | ||
* @param where | ||
* @param callback | ||
*/ | ||
Mysql.prototype.one = function (table, columns, where, callback) { | ||
this.select(table, columns, where, function (rows) { | ||
callback(rows[0]); | ||
}); | ||
}; | ||
|
||
/** | ||
* Insert row into {table} | ||
* | ||
* @param table | ||
* @param fields | ||
* @param callback | ||
*/ | ||
Mysql.prototype.insert = function (table, fields, callback) { | ||
var query = 'insert into' + mysql.escapeId(table); | ||
query += '(' + Object.keys(fields).forEach(function (field) { | ||
return mysql.escapeId(field); | ||
}).join(', ') + ')'; | ||
query += ' values(' + Object.values(fields).forEach(function (value) { | ||
return mysql.escape(value); | ||
}); | ||
this.query(query, callback); | ||
}; | ||
|
||
/** | ||
* Update {table} from {values} source for rows, who match {where} | ||
* | ||
* @param table | ||
* @param where | ||
* @param values | ||
*/ | ||
Mysql.prototype.update = function (table, where, values) { | ||
var query = 'update ' + mysql.escapeId(table); | ||
query += ' set ' + this._getWhereString(values, ','); | ||
query += ' where ' + this._getWhereString(where); | ||
this.query(query, callback) | ||
}; | ||
|
||
/** | ||
* Delete rows from {table} who match {where} | ||
* | ||
* @param table | ||
* @param where | ||
*/ | ||
Mysql.prototype.delete = function (table, where) { | ||
var query = 'delete from ' + mysql.escapeId(table); | ||
query += ' where ' + this._getWhereString(where); | ||
this.query(query, callback); | ||
}; | ||
|
||
/** | ||
* Execute query | ||
* Data define source for escaping query values: | ||
* https://github.com/felixge/node-mysql#escaping-query-values | ||
* | ||
* Two different call: | ||
* - this.query(query, data, callback) | ||
* - this.query(query, callback) | ||
* | ||
* Return result rows in {callback} | ||
* | ||
* @param query | ||
* @param data | ||
* @param callback | ||
*/ | ||
Mysql.prototype.query = function (query, data, callback) { | ||
if (arguments.length === 2) { | ||
callback = data; | ||
data = []; | ||
} | ||
this.connection.query(query, data, function (err, rows) { | ||
if (err) throw err; | ||
callback(rows); | ||
}); | ||
}; | ||
|
||
/** | ||
* Build where string from where object | ||
* Example: { a:1, b:2, or: { c: 3, and: { d:4, e:5 } } } transform to | ||
* a=1 and b=2 and (c=3 or (d=4 and e=5)) | ||
* | ||
* @param where | ||
* @param operator | ||
* @returns {*} | ||
* @private | ||
*/ | ||
Mysql.prototype._getWhereString = function (where, operator) { | ||
if (where === null) return ''; | ||
if (_.isString(where)) return where; | ||
|
||
if (!operator) operator = 'and'; | ||
|
||
return _.forEach(where, function (field, value) { | ||
if (field === 'and' || field === 'or') { | ||
return ' (' + this._getWhereString(value, field) + ')'; | ||
} | ||
return mysql.escapeId(field) + '=' + mysql.escape(value); | ||
}.bind(this)).join(' ' + operator + ' '); | ||
}; | ||
|
||
exports = Mysql; |