Skip to content

Commit 8d21086

Browse files
author
Kyle Farris
committed
Fixed a bug causing inserts to fail due to paranthesis. Fixed undefined value in set object bug.
1 parent 463cfb1 commit 8d21086

File tree

5 files changed

+25
-23
lines changed

5 files changed

+25
-23
lines changed

drivers/mysql/query_builder.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ var QueryBuilder = function() {
453453
return '';
454454
}
455455

456-
return verb + 'INTO ' + qb.from_array[0] + ' (' + keys.join(', ') + ') VALUES (' + values.join(', ') + ')';// + suffix;
456+
return verb + 'INTO ' + qb.from_array[0] + ' (' + keys.join(', ') + ') VALUES (' + values.join(', ') + ')' + suffix;
457457
};
458458

459459
// ---------------------------- ACTUAL QUERY BUILDER ----------------------------//
@@ -1173,7 +1173,9 @@ var QueryBuilder = function() {
11731173
// Add each key:value pair to the set_array
11741174
for (var i in key) {
11751175
var v = key[i];
1176-
if (typeof v === 'undefined') continue;
1176+
if (typeof v === 'undefined') {
1177+
throw new Error("set(): Invalid value provided! (provided: " + v + " (type: " + (typeof v) + ")");
1178+
}
11771179

11781180
if ((typeof v).match(/^(number|string|boolean)$/) === null && v !== null) {
11791181
throw new Error("set(): Invalid value provided! (provided: " + v + " (type: " + (typeof v) + ")");
@@ -1344,7 +1346,7 @@ var QueryBuilder = function() {
13441346
}
13451347

13461348
var verb = 'INSERT ' + (ignore === true ? 'IGNORE ' : '');
1347-
return verb + 'INTO (' + this.from_array[0] + ') (' + columns.join(', ') + ') VALUES ' + map.join(', ') + suffix;
1349+
return verb + 'INTO ' + this.from_array[0] + ' (' + columns.join(', ') + ') VALUES ' + map.join(', ') + suffix;
13481350
},
13491351

13501352
get: function(table) {

test/mysql/tests-compilation_methods.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ describe('get_compiled_insert()', function() {
3636
it('should return a SQL string', function() {
3737
qb.reset_query();
3838
var sql = qb.set({foo:'bar'}).get_compiled_insert('galaxies');
39-
sql.should.eql("INSERT INTO (`galaxies`) (`foo`) VALUES ('bar')");
39+
sql.should.eql("INSERT INTO `galaxies` (`foo`) VALUES ('bar')");
4040
});
4141
});
4242

test/mysql/tests-insert.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,30 +68,30 @@ describe('insert()', function() {
6868
it('should allow for an empty data parameter', function() {
6969
qb.reset_query();
7070
var sql = qb.insert('galaxies');
71-
sql.should.eql("INSERT INTO (`galaxies`) () VALUES ()");
71+
sql.should.eql("INSERT INTO `galaxies` () VALUES ()");
7272
});
7373
it('should utilize pre-existing tables set in from_array', function() {
7474
qb.reset_query();
7575
qb.from('galaxies');
7676
var sql = qb.insert();
77-
sql.should.eql("INSERT INTO (`galaxies`) () VALUES ()");
77+
sql.should.eql("INSERT INTO `galaxies` () VALUES ()");
7878
});
7979
it('should utilize pre-existing values set in in set_array', function() {
8080
qb.reset_query();
8181
qb.set(test_data);
8282
var sql = qb.insert('galaxies');
83-
sql.should.eql("INSERT INTO (`galaxies`) (`id`, `name`, `type`) VALUES (3, 'Milky Way', 'spiral')");
83+
sql.should.eql("INSERT INTO `galaxies` (`id`, `name`, `type`) VALUES (3, 'Milky Way', 'spiral')");
8484
});
8585
it('should utilize pre-existing tables and values from from_aray and set_array, respectively', function() {
8686
qb.reset_query();
8787
qb.from('galaxies').set(test_data);
8888
var sql = qb.insert();
89-
sql.should.eql("INSERT INTO (`galaxies`) (`id`, `name`, `type`) VALUES (3, 'Milky Way', 'spiral')");
89+
sql.should.eql("INSERT INTO `galaxies` (`id`, `name`, `type`) VALUES (3, 'Milky Way', 'spiral')");
9090
});
9191
it('should accept a non-empty object for the data parameter', function() {
9292
qb.reset_query();
9393
var sql = qb.insert('galaxies', test_data);
94-
sql.should.eql("INSERT INTO (`galaxies`) (`id`, `name`, `type`) VALUES (3, 'Milky Way', 'spiral')");
94+
sql.should.eql("INSERT INTO `galaxies` (`id`, `name`, `type`) VALUES (3, 'Milky Way', 'spiral')");
9595
});
9696
it('should convert call to insert_batch() if an array of non-emtpy objects is passed in the data parameter', function() {
9797
qb.reset_query();
@@ -127,7 +127,7 @@ describe('insert_ignore()', function() {
127127
it('should create an INSERT IGNORE statement', function() {
128128
qb.reset_query();
129129
var sql = qb.insert_ignore('galaxies', test_data);
130-
sql.should.eql("INSERT IGNORE INTO (`galaxies`) (`id`, `name`, `type`) VALUES (3, 'Milky Way', 'spiral')");
130+
sql.should.eql("INSERT IGNORE INTO `galaxies` (`id`, `name`, `type`) VALUES (3, 'Milky Way', 'spiral')");
131131
});
132132
it('should be just a wrapper of insert() that passes true to the 3rd parameter', function() {
133133
qb.reset_query();
@@ -138,11 +138,11 @@ describe('insert_ignore()', function() {
138138
it('should convert to insert_batch() if an array of data is supplied to second parameter', function() {
139139
qb.reset_query();
140140
var sql = qb.insert_ignore('galaxies', test_data_set);
141-
sql.should.eql("INSERT IGNORE INTO (`galaxies`) (`id`, `name`, `type`) VALUES (3, 'Milky Way', 'spiral'), (4, 'Andromeda', 'spiral')");
141+
sql.should.eql("INSERT IGNORE INTO `galaxies` (`id`, `name`, `type`) VALUES (3, 'Milky Way', 'spiral'), (4, 'Andromeda', 'spiral')");
142142
});
143143
it('should support the "on_dupe" suffix parameter... effectively appending to the query anything supplied in this parameter', function() {
144144
qb.reset_query();
145145
var sql = qb.insert_ignore('galaxies', test_data, 'ON DUPLICATE KEY UPDATE last_update = NOW()');
146-
sql.should.eql("INSERT IGNORE INTO (`galaxies`) (`id`, `name`, `type`) VALUES (3, 'Milky Way', 'spiral') ON DUPLICATE KEY UPDATE last_update = NOW()");
146+
sql.should.eql("INSERT IGNORE INTO `galaxies` (`id`, `name`, `type`) VALUES (3, 'Milky Way', 'spiral') ON DUPLICATE KEY UPDATE last_update = NOW()");
147147
});
148148
});

test/mysql/tests-insert_batch.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ describe('insert_batch()', function() {
4242
it('should build a proper batch INSERT string', function() {
4343
qb.reset_query();
4444
var sql = qb.insert_batch('galaxies', test_data);
45-
sql.should.eql("INSERT INTO (`galaxies`) (`id`, `name`, `type`) VALUES (3, 'Milky Way', 'spiral'), (4, 'Andromeda', 'spiral')");
45+
sql.should.eql("INSERT INTO `galaxies` (`id`, `name`, `type`) VALUES (3, 'Milky Way', 'spiral'), (4, 'Andromeda', 'spiral')");
4646
});
4747
it('should only accept an array as the second parameter', function() {
4848
qb.reset_query();
@@ -72,13 +72,13 @@ describe('insert_batch()', function() {
7272
it('should allow for an empty data parameter', function() {
7373
qb.reset_query();
7474
var sql = qb.insert_batch('galaxies',[]);
75-
sql.should.eql("INSERT INTO (`galaxies`) () VALUES ()");
75+
sql.should.eql("INSERT INTO `galaxies` () VALUES ()");
7676
});
7777
it('should utilize pre-existing tables set in from_array', function() {
7878
qb.reset_query();
7979
qb.from('galaxies');
8080
var sql = qb.insert_batch(null,[]);
81-
sql.should.eql("INSERT INTO (`galaxies`) () VALUES ()");
81+
sql.should.eql("INSERT INTO `galaxies` () VALUES ()");
8282
});
8383
it('should fail if any invalid values are passed into one of the data objects in the dataset', function() {
8484
qb.reset_query();
@@ -98,6 +98,6 @@ describe('insert_batch()', function() {
9898
it('should support insert ignore statements', function() {
9999
qb.reset_query();
100100
var sql = qb.insert_batch('galaxies', test_data, true, 'ON DUPLICATE KEY UPDATE last_update = NOW()');
101-
sql.should.eql("INSERT IGNORE INTO (`galaxies`) (`id`, `name`, `type`) VALUES (3, 'Milky Way', 'spiral'), (4, 'Andromeda', 'spiral') ON DUPLICATE KEY UPDATE last_update = NOW()");
101+
sql.should.eql("INSERT IGNORE INTO `galaxies` (`id`, `name`, `type`) VALUES (3, 'Milky Way', 'spiral'), (4, 'Andromeda', 'spiral') ON DUPLICATE KEY UPDATE last_update = NOW()");
102102
});
103103
});

test/mysql/tests-update.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,13 @@ describe('update()', function() {
108108
var arr = [1,2,3];
109109
var obj = {foo: 'bar'};
110110

111-
qb.reset_query(); expect(function() { qb.insert('galaxies',{id: func}); }, 'function in data').to.throw(Error);
112-
qb.reset_query(); expect(function() { qb.insert('galaxies',{id: regex}); }, 'regex in data').to.throw(Error);
113-
qb.reset_query(); expect(function() { qb.insert('galaxies',{id: Infinity}); }, 'Infinity in data').to.throw(Error);
114-
qb.reset_query(); expect(function() { qb.insert('galaxies',{id: undefined}); }, 'undefined in data').to.throw(Error);
115-
qb.reset_query(); expect(function() { qb.insert('galaxies',{id: NaN}); }, 'NaN in data').to.throw(Error);
116-
qb.reset_query(); expect(function() { qb.insert('galaxies',{id: arr}); }, 'array in data').to.throw(Error);
117-
qb.reset_query(); expect(function() { qb.insert('galaxies',{id: obj}); }, 'object in data').to.throw(Error);
111+
qb.reset_query(); expect(function() { qb.update('galaxies',{id: func}); }, 'function in data').to.throw(Error);
112+
qb.reset_query(); expect(function() { qb.update('galaxies',{id: regex}); }, 'regex in data').to.throw(Error);
113+
qb.reset_query(); expect(function() { qb.update('galaxies',{id: Infinity}); }, 'Infinity in data').to.throw(Error);
114+
qb.reset_query(); expect(function() { qb.update('galaxies',{id: undefined}); }, 'undefined in data').to.throw(Error);
115+
qb.reset_query(); expect(function() { qb.update('galaxies',{id: NaN}); }, 'NaN in data').to.throw(Error);
116+
qb.reset_query(); expect(function() { qb.update('galaxies',{id: arr}); }, 'array in data').to.throw(Error);
117+
qb.reset_query(); expect(function() { qb.update('galaxies',{id: obj}); }, 'object in data').to.throw(Error);
118118

119119
});
120120
/*

0 commit comments

Comments
 (0)