Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

parseInt for datetimes

  • Loading branch information...
commit aacbd25a740b8bc908fa05729d0b7b04c77021a3 2 parents 1f91317 + 01a1142
@joeferner authored
View
14 lib/drivers/mysql.js
@@ -93,8 +93,18 @@ var MySqlDriver = Driver.extend({
conn = new MySqlConnection(this, opts.db, false, opts);
callback(null, conn);
} else {
- var db = new mysql.createClient(opts);
- conn = new MySqlConnection(this, db, true);
+ var db;
+ if (typeof(mysql.createConnection) === 'undefined') {
+ db = new mysql.createClient(opts);
+ } else {
+ db = new mysql.createConnection(opts);
+ db.connect(function(err) {
+ if (err) {
+ console.log(err);
+ }
+ });
+ }
+ conn = new MySqlConnection(this, db, true, opts);
callback(null, conn);
}
}
View
3  lib/sqltree.js
@@ -132,6 +132,9 @@ var SqlTree = Class.extend({
var key;
var trueFunc = function () { return true; };
for (key in row) {
+ if (key == 'parse' || key == '_typeCast') {
+ continue;
+ }
var column = this.getColumnByAlias(key);
if (!column) {
throw new Error('Could not find column from alias "' + key + '"');
View
138 test/update.js
@@ -1,4 +1,3 @@
-
var persist = require("../lib/persist");
var type = persist.type;
var nodeunit = require("nodeunit");
@@ -34,18 +33,30 @@ exports['Update'] = nodeunit.testCase({
var self = this;
var person1 = new this.Person({ name: "Bob O'Neill" });
person1.save(this.connection, function(err, p) {
- if(err) { console.error(err); return; }
+ if (err) {
+ console.error(err);
+ return;
+ }
self.Person.using(self.connection).all(function(err, rows) {
- if(err) { console.error(err); return; }
+ if (err) {
+ console.error(err);
+ return;
+ }
test.equal(1, rows.length);
test.equal("Bob O'Neill", rows[0].name);
// update
rows[0].name = 'tom';
rows[0].save(self.connection, function(err) {
- if(err) { console.error(err); return; }
+ if (err) {
+ console.error(err);
+ return;
+ }
self.Person.using(self.connection).all(function(err, rows) {
- if(err) { console.error(err); return; }
+ if (err) {
+ console.error(err);
+ return;
+ }
test.equal(1, rows.length);
test.equal('tom', rows[0].name);
@@ -60,17 +71,29 @@ exports['Update'] = nodeunit.testCase({
var self = this;
var person1 = new this.Person({ name: "Bob O'Neill" });
person1.save(this.connection, function(err, p) {
- if(err) { console.error(err); return; }
+ if (err) {
+ console.error(err);
+ return;
+ }
self.Person.using(self.connection).all(function(err, rows) {
- if(err) { console.error(err); return; }
+ if (err) {
+ console.error(err);
+ return;
+ }
test.equal(1, rows.length);
test.equal("Bob O'Neill", rows[0].name);
// update
rows[0].update(self.connection, {name: 'tom'}, function(err) {
- if(err) { console.error(err); return; }
+ if (err) {
+ console.error(err);
+ return;
+ }
self.Person.using(self.connection).all(function(err, rows) {
- if(err) { console.error(err); return; }
+ if (err) {
+ console.error(err);
+ return;
+ }
test.equal(1, rows.length);
test.equal('tom', rows[0].name);
@@ -85,14 +108,23 @@ exports['Update'] = nodeunit.testCase({
var self = this;
var person1 = new this.Person({ name: "Bob O'Neill" });
person1.save(this.connection, function(err, p) {
- if(err) { console.error(err); return; }
+ if (err) {
+ console.error(err);
+ return;
+ }
self.Person.update(self.connection, p.id, {
name: "Bob O'Neil"
}, function(err) {
- if(err) { console.error(err); return; }
+ if (err) {
+ console.error(err);
+ return;
+ }
self.Person.where("id = ?", p.id).first(self.connection, function(err, row) {
- if(err) { console.error(err); return; }
+ if (err) {
+ console.error(err);
+ return;
+ }
test.equal("Bob O'Neil", row.name);
test.done();
});
@@ -106,7 +138,10 @@ exports['Update'] = nodeunit.testCase({
var person2 = new this.Person({ name: "Tom Jones" });
var phone1 = new this.Phone({ number: "555-5555", person: person1 });
this.connection.save([person1, person2, phone1], function(err) {
- if(err) { console.error(err); return; }
+ if (err) {
+ console.error(err);
+ return;
+ }
test.ok(phone1.personId);
test.equal(phone1.personId, person1.id);
@@ -114,13 +149,22 @@ exports['Update'] = nodeunit.testCase({
self.Phone.update(self.connection, phone1.id, {
personId: person2.id
}, function(err) {
- if(err) { console.error(err); return; }
+ if (err) {
+ console.error(err);
+ return;
+ }
self.Person.include("phones").getById(self.connection, person1.id, function(err, row) {
- if(err) { console.error(err); return; }
+ if (err) {
+ console.error(err);
+ return;
+ }
test.equal(row.phones.length, 0);
self.Person.include("phones").getById(self.connection, person2.id, function(err, row) {
- if(err) { console.error(err); return; }
+ if (err) {
+ console.error(err);
+ return;
+ }
test.equal(row.phones.length, 1);
test.done();
});
@@ -135,7 +179,10 @@ exports['Update'] = nodeunit.testCase({
var person2 = new this.Person({ name: "Tom Jones" });
var phone1 = new this.Phone({ number: "555-5555", person: person1 });
this.connection.save([person1, person2, phone1], function(err) {
- if(err) { console.error(err); return; }
+ if (err) {
+ console.error(err);
+ return;
+ }
test.ok(phone1.personId);
test.equal(phone1.personId, person1.id);
@@ -143,13 +190,22 @@ exports['Update'] = nodeunit.testCase({
self.Phone.update(self.connection, phone1.id, {
person: person2
}, function(err) {
- if(err) { console.error(err); return; }
+ if (err) {
+ console.error(err);
+ return;
+ }
self.Person.include("phones").getById(self.connection, person1.id, function(err, row) {
- if(err) { console.error(err); return; }
+ if (err) {
+ console.error(err);
+ return;
+ }
test.equal(row.phones.length, 0);
self.Person.include("phones").getById(self.connection, person2.id, function(err, row) {
- if(err) { console.error(err); return; }
+ if (err) {
+ console.error(err);
+ return;
+ }
test.equal(row.phones.length, 1);
test.done();
});
@@ -165,28 +221,37 @@ exports['Update'] = nodeunit.testCase({
var person2 = new this.Person({ name: "Joe Blow", age: 35, lastUpdated: origDate });
var person3 = new this.Person({ name: "Joe Smith", age: 36, lastUpdated: origDate });
self.connection.save([person1, person2, person3], function(err) {
- if(err) { console.log(err); return; }
+ if (err) {
+ console.log(err);
+ return;
+ }
var lastUpdated = new Date(2012, 1, 2);
self.Person.where("name LIKE ?", "Joe%").updateAll(self.connection, { age: 19, lastUpdated: lastUpdated }, function(err) {
- if(err) { console.log(err); return; }
+ if (err) {
+ console.log(err);
+ return;
+ }
self.Person.orderBy("name").all(self.connection, function(err, results) {
- if(err) { console.log(err); return; }
+ if (err) {
+ console.log(err);
+ return;
+ }
test.equals(3, results.length);
test.equals("Bob Smith", results[0].name);
test.equals(20, results[0].age);
- test.equals(origDate.getTime(), results[0].lastUpdated);
+ test.equals(origDate.getTime(), new Date(parseInt(results[0].lastUpdated)).getTime());
test.equals("Joe Blow", results[1].name);
test.equals(19, results[1].age);
- test.equals(lastUpdated.getTime(), results[1].lastUpdated);
+ test.equals(lastUpdated.getTime(), new Date(parseInt(results[1].lastUpdated)).getTime());
test.equals("Joe Smith", results[2].name);
test.equals(19, results[2].age);
- test.equals(lastUpdated.getTime(), results[2].lastUpdated);
+ test.equals(lastUpdated.getTime(), new Date(parseInt(results[2].lastUpdated)).getTime());
test.done();
});
@@ -201,28 +266,37 @@ exports['Update'] = nodeunit.testCase({
var person2 = new this.Person({ name: "Joe Blow", age: 35, lastUpdated: origDate });
var person3 = new this.Person({ name: "Joe Smith", age: 36, lastUpdated: origDate });
self.connection.save([person1, person2, person3], function(err) {
- if(err) { console.log(err); return; }
+ if (err) {
+ console.log(err);
+ return;
+ }
var lastUpdated = new Date(2012, 1, 2);
self.Person.updateAll(self.connection, { age: 19, lastUpdated: lastUpdated }, function(err) {
- if(err) { console.log(err); return; }
+ if (err) {
+ console.log(err);
+ return;
+ }
self.Person.orderBy("name").all(self.connection, function(err, results) {
- if(err) { console.log(err); return; }
+ if (err) {
+ console.log(err);
+ return;
+ }
test.equals(3, results.length);
test.equals("Bob Smith", results[0].name);
test.equals(19, results[0].age);
- test.equals(lastUpdated.getTime(), results[0].lastUpdated);
+ test.equals(lastUpdated.getTime(), new Date(parseInt(results[0].lastUpdated)).getTime());
test.equals("Joe Blow", results[1].name);
test.equals(19, results[1].age);
- test.equals(lastUpdated.getTime(), results[1].lastUpdated);
+ test.equals(lastUpdated.getTime(), new Date(parseInt(results[1].lastUpdated)).getTime());
test.equals("Joe Smith", results[2].name);
test.equals(19, results[2].age);
- test.equals(lastUpdated.getTime(), results[2].lastUpdated);
+ test.equals(lastUpdated.getTime(), new Date(parseInt(results[2].lastUpdated)).getTime());
test.done();
});
View
1  test_helpers/test_utils.js
@@ -125,6 +125,7 @@ exports.connect = function(persist, opts, callback) {
} else {
opts.driver = opts.driver || 'mysql';
opts.database = opts.database || 'test';
+ opts.host = opts.host || 'localhost';
opts.user = opts.user || 'root';
opts.password = opts.password || 'root';
persist.connect(opts, mycallback);
Please sign in to comment.
Something went wrong with that request. Please try again.