Skip to content

Commit

Permalink
Micah - Hazzah! All tests pass. Some bullshit, though. MySQL doesn't …
Browse files Browse the repository at this point in the history
…have TRUNCATE CASCADE. CASCADE modifier is simply ignored. MySQL doesn't have OFFSET without LIMIT like PG does. So, if offset opt is present without limit, statement will return: LIMIT 100000000 OFFSET x. If both limit and offset opts are present, works as expected.
  • Loading branch information
Micah Silverman committed Oct 31, 2012
1 parent 57275ef commit 68b5ad4
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 150 deletions.
50 changes: 10 additions & 40 deletions lib/fast_legs/mysql/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,6 @@ Base.prototype.create = function(obj, callback) {
loadSchema(self, createQuery);
};

// postgres results look like:
// {
// ...
// rows: [
// ...
// ]
// }
//
// mysql results look like:
// [
// ...
// ]
['find', 'findOne'].forEach(function(finder) {
Base.prototype[finder] = function(selector, opts, callback) {
var self = this;
Expand All @@ -71,21 +59,21 @@ Base.prototype.create = function(obj, callback) {
var outValues = [];
var selectStatement =
self.client.statements.select(self, selector, opts, outValues);
/*
*console.log(selectStatement)
*console.log(outValues)
*/
self.client.emit('query', selectStatement, outValues, function(err, result) {
var results = null;

if (finder === 'find') {
var empty = []
, rowOrRows = (_.isUndefined(result)) ?
null :
(result.rows) ? result.rows : result;
, rowOrRows = (_.isUndefined(result)) ? null : result;
}

if (finder === 'findOne') {
var empty = null
, rowOrRows = (_.isUndefined(result)) ?
null :
(result.rows) ? result.rows[0] : result[0];
, rowOrRows = (_.isUndefined(result)) ? null : result[0];
}

if (err && err.message === 'column "invalid" does not exist') {
Expand All @@ -94,15 +82,10 @@ Base.prototype.create = function(obj, callback) {
if (_(opts.include).isUndefined()) {
if (_(selector).isArray())
results = _.isUndefined(result) ? empty : rowOrRows;
else if (_(selector).isNumber() || _(selector).isString()) {
if (result.rows)
results = _.isUndefined(result.rows[0]) ? null : result.rows[0];
else
results = _.isUndefined(result[0]) ? null : result[0]
}
else if (_(selector).isNumber() || _(selector).isString())
results = _.isUndefined(result[0]) ? null : result[0]
else
results = _.isUndefined(result) ? empty : rowOrRows;

callback(err, results);
} else {
results = _.isUndefined(result) ? empty : rowOrRows;
Expand Down Expand Up @@ -137,17 +120,6 @@ Base.prototype.update = function(selector, obj, callback) {
loadSchema(self, updateQuery);
};

// postgres results look like:
// {
// ...
// rowCount: x
// }
//
// mysql results look like:
// {
// ...
// affectedRows: x
// }
Base.prototype.destroy = function(selector, callback) {
var self = this;

Expand All @@ -163,9 +135,7 @@ Base.prototype.destroy = function(selector, callback) {
if (err && err.message === 'column "invalid" does not exist') {
callback(null, 0);
} else {
var deletedRows = _(results).isUndefined() ?
0 :
(results.rowCount) ? results.rowCount : results.affectedRows;
var deletedRows = _(results).isUndefined() ? 0 : results.affectedRows;
callback(err, deletedRows);
}
});
Expand Down Expand Up @@ -259,7 +229,7 @@ var loadSchema = function(model, query) {

if (_(model._fields).isUndefined()) {
model.client.emit('query', statement, function(err, result) {
model._fields = result.rows || result;
model._fields = result;
query();
});
} else {
Expand Down
15 changes: 13 additions & 2 deletions lib/fast_legs/mysql/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,20 @@ Client.prototype.connect = function() {
self.connParams :
{host: 'localhost', port: 3306}
self.client = mysql.createConnection(connParams)
self.client.on('error', function(err) {
self.lastError = err;
return true;
})
self.on('query', function(query, values, callback) {
self.connected || self.doConnect();
self.client.query(query, values, callback)
if(self.lastError !== null){
var error = self.lastError;
self.lastError = null;
callback(error,0);
} else {
if (!_.isUndefined(values[0])) values = _.flatten(values);
self.connected || self.doConnect();
self.client.query(query, values, callback)
}
})
}

Expand Down
16 changes: 10 additions & 6 deletions lib/fast_legs/mysql/statements.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,11 @@ exports.truncate = function(model, opts) {
var opts = opts === undefined ? {} : opts
, stmt = "TRUNCATE " + model.tableName

if (opts.cascade) {
stmt += " CASCADE";
}
/*
*if (opts.cascade) {
* stmt += " CASCADE";
*}
*/

return stmt + ";"
};
Expand Down Expand Up @@ -110,7 +112,7 @@ var buildOffsetClause = function(opts) {
if(_(opts.offset).isUndefined()) {
return "";
} else {
return " OFFSET " + opts.offset;
return (_(opts.limit).isUndefined()?" LIMIT 100000000":"") + " OFFSET " + opts.offset;
}
};

Expand Down Expand Up @@ -163,6 +165,7 @@ var buildOperator = function(key, value, outValues) {
break;
case 'ilike':
var operator = "ILIKE";
field = 'UPPER('+field+')';
break;
case 'nilike': case 'not_ilike':
var operator = "NOT ILIKE";
Expand All @@ -186,9 +189,10 @@ var buildOperator = function(key, value, outValues) {
outValues = _.flatten(outValues);
if (key.split('.')[1] == 'textsearch') {
return 'to_tsvector(\'english\', ' + field + ') ' + operator +
' to_tsquery(\'english\', ' + utils.quote(outValues, operator, true) + ')';
' to_tsquery(\'english\', ' + utils.quote(outValues, operator) + ')';
} else {
return field + ' ' + operator + ' ' + utils.quote(outValues, operator, true);
return field + ' ' +
((operator === 'ILIKE')?'LIKE':operator) + ' ' + utils.quote(outValues, operator);
}
};

Expand Down
9 changes: 5 additions & 4 deletions lib/fast_legs/mysql/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,19 @@ var nil = exports.nil = function(value) {
}
};

var quote = exports.quote = function(outValues, operator, placeholder) {
placeholder = (placeholder) ? placeholder : false;
var quote = exports.quote = function(outValues, operator) {
if (operator === 'IN' || operator === 'NOT IN') {
var valuePos = _.range(1, outValues.length+1);
var values = _.reduce(valuePos, function(memo, pos, i) {
memo += (placeholder) ? '?' : '$' + pos.toString();
memo += '?';
if (i+1 !== valuePos.length) memo += ',';
return memo;
}, '');
return '(' + values + ')';
} else if (operator === 'ILIKE') {
return 'UPPER(?)';
} else {
return (placeholder) ? '?' : '$' + outValues.length;
return '?';
}
};

Expand Down
4 changes: 1 addition & 3 deletions lib/fast_legs/pg/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ Client.prototype.connect = function() {
}else{
if (!_.isUndefined(values[0])) values = _.flatten(values);
self.connected || self.doConnect();
self.client.query(query, values, function(err, result) {
callback(err, result);
});
self.client.query(query, values, callback);
}
});
}
Expand Down
7 changes: 3 additions & 4 deletions lib/fast_legs/pg/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,17 @@ var nil = exports.nil = function(value) {
}
};

var quote = exports.quote = function(outValues, operator, placeholder) {
placeholder = (placeholder) ? placeholder : false;
var quote = exports.quote = function(outValues, operator) {
if (operator === 'IN' || operator === 'NOT IN') {
var valuePos = _.range(1, outValues.length+1);
var values = _.reduce(valuePos, function(memo, pos, i) {
memo += (placeholder) ? '?' : '$' + pos.toString();
memo += '$' + pos.toString();
if (i+1 !== valuePos.length) memo += ',';
return memo;
}, '');
return '(' + values + ')';
} else {
return (placeholder) ? '?' : '$' + outValues.length;
return '$' + outValues.length;
}
};

Expand Down
Loading

0 comments on commit 68b5ad4

Please sign in to comment.