mysql-activerecord
Advanced tools
Comparing version 0.2.1 to 0.2.9
35
index.js
@@ -81,2 +81,6 @@ /** | ||
var escapeFieldName = function(str) { | ||
return '`' + str.replace('.','`.`') + '`'; | ||
}; | ||
var buildDataString = function(dataSet, separator, clause) { | ||
@@ -90,4 +94,5 @@ if (!clause) { | ||
} | ||
for (var key in dataSet) { | ||
queryString += key + "=" + connection.escape(dataSet[key]); | ||
queryString += escapeFieldName(key) + "=" + connection.escape(dataSet[key]); | ||
if (y < getObjectSize(dataSet)) { | ||
@@ -108,3 +113,3 @@ queryString += separator; | ||
for (var i = 0; i < joinClause.length; i++) { | ||
joinString += (joinClause[i].direction !== '' ? ' ' + joinClause[i].direction : '') + ' JOIN ' + joinClause[i].table + ' ON ' + joinClause[i].relation; | ||
joinString += (joinClause[i].direction !== '' ? ' ' + joinClause[i].direction : '') + ' JOIN ' + escapeFieldName(joinClause[i].table) + ' ON ' + joinClause[i].relation; | ||
} | ||
@@ -150,3 +155,2 @@ | ||
whereClause = mergeObjects(whereClause, whereSet); | ||
console.log(whereClause); | ||
} | ||
@@ -211,9 +215,16 @@ else if (typeof whereSet === 'string' || typeof whereSet === 'number') { | ||
this.insert = function(tableToInsert, dataSet, callbackHandler) { | ||
connection.query('INSERT into ' + tableToInsert + ' SET ' + buildQueryString(dataSet, ', '), callbackHandler); | ||
this.ping = function() { | ||
connection.ping(); | ||
return that; | ||
}; | ||
this.ping = function() { | ||
connection.ping(); | ||
this.insert = function(tableName, dataSet, responseCallback) { | ||
if (typeof tableName === 'string') { | ||
var combinedQueryString = 'INSERT into ' + escapeFieldName(tableName) | ||
+ buildDataString(dataSet, ', ', 'SET'); | ||
connection.query(combinedQueryString, responseCallback); | ||
resetQuery(combinedQueryString); | ||
} | ||
return that; | ||
@@ -225,3 +236,3 @@ }; | ||
var combinedQueryString = 'SELECT ' + (selectClause.length === 0 ? '*' : selectClause.join(',')) | ||
+ ' FROM ' + tableName | ||
+ ' FROM ' + escapeFieldName(tableName) | ||
+ buildJoinString() | ||
@@ -242,3 +253,3 @@ + buildDataString(whereClause, ' AND ', 'WHERE') | ||
if (typeof tableName === 'string') { | ||
var combinedQueryString = 'UPDATE ' + tableName | ||
var combinedQueryString = 'UPDATE ' + escapeFieldName(tableName) | ||
+ buildDataString(newData, ', ', 'SET') | ||
@@ -255,5 +266,9 @@ + buildDataString(whereClause, ' AND ', 'WHERE') | ||
this.escape = function(str) { | ||
return connection.escape(str); | ||
}; | ||
this.delete = function(tableName, responseCallback) { | ||
if (typeof tableName === 'string') { | ||
var combinedQueryString = 'DELETE FROM ' + tableName | ||
var combinedQueryString = 'DELETE FROM ' + escapeFieldName(tableName) | ||
+ buildDataString(whereClause, ' AND ', 'WHERE') | ||
@@ -260,0 +275,0 @@ + (limitClause !== -1 ? ' LIMIT ' + limitClause : ''); |
{ "name" : "mysql-activerecord" | ||
, "version": "0.2.1" | ||
, "version": "0.2.9" | ||
, "devDependencies": {"node-mysql": "0.9.1"} | ||
, "main" : "./" | ||
} |
MySQL ActiveRecord Adapter for Node.js | ||
====================================== | ||
Active Record Database Pattern implementation for use with node-mysql as MySQL connection driver. | ||
Active Record Database Pattern implementation for use with node-mysql (https://github.com/felixge/node-mysql) as MySQL connection driver. | ||
@@ -6,0 +6,0 @@ It enables similar database operations as in CodeIgniter (a PHP web applications framework). The main benefit and the reason I worked on this was the ability to direct JavaScript objects straight to MySQL queries without having to worry about constructing the query itself. Although Active Record is maybe a tiny step closer to ORM, I see a lot of value in the Active Record as it allows more control over database queries than traditional ORM. |
11354
248