Skip to content

Commit f76e13a

Browse files
committed
Merge pull request brianc#141 from eugeneware/blob-support
BLOB support for postgres, mysql and sqlite.
2 parents 54caca1 + b1ede85 commit f76e13a

6 files changed

Lines changed: 120 additions & 1 deletion

File tree

lib/dialect/mysql.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ Mysql.prototype._getParameterPlaceholder = function() {
2222
return '?';
2323
};
2424

25+
Mysql.prototype._getParameterValue = function(value) {
26+
if (Buffer.isBuffer(value)) {
27+
value = 'x' + this._getParameterValue(value.toString('hex'));
28+
} else {
29+
value = Postgres.prototype._getParameterValue.call(this, value);
30+
}
31+
return value;
32+
};
33+
2534
Mysql.prototype.visitReturning = function() {
2635
throw new Error('MySQL does not allow returning clause.');
2736
};

lib/dialect/postgres.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ Postgres.prototype._getParameterValue = function(value) {
4646
// Date object's default toString format does not get parsed well
4747
// Handle date like objects using toISOString
4848
value = this._getParameterValue(value.toISOString());
49+
} else if (Buffer.isBuffer(value)) {
50+
value = this._getParameterValue('\\x' + value.toString('hex'));
4951
} else {
5052
// rich object represent with string
5153
value = this._getParameterValue(value.toString());

lib/dialect/sqlite.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ Sqlite.prototype._myClass = Sqlite;
1717

1818
Sqlite.prototype._arrayAggFunctionName = 'GROUP_CONCAT';
1919

20+
Sqlite.prototype._getParameterValue = function(value) {
21+
if (Buffer.isBuffer(value)) {
22+
value = 'x' + this._getParameterValue(value.toString('hex'));
23+
} else {
24+
value = Postgres.prototype._getParameterValue.call(this, value);
25+
}
26+
return value;
27+
};
28+
2029
Sqlite.prototype.visitDefault = function() {
2130
throw new Error('SQLite requires that all rows of a multi-row insert are for the same columns.');
2231
};

test/dialects/insert-tests.js

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ Harness.test({
2121
params: ['test', 1]
2222
});
2323

24-
2524
Harness.test({
2625
query: post.insert(post.content.value('whoah')),
2726
pg: {
@@ -325,3 +324,64 @@ Harness.test({
325324
},
326325
params: ['A%']
327326
});
327+
328+
// Binary inserts
329+
Harness.test({
330+
query: post.insert(post.content.value(new Buffer('test')), post.userId.value(2)),
331+
pg: {
332+
text : 'INSERT INTO "post" ("content", "userId") VALUES ($1, $2)',
333+
string: 'INSERT INTO "post" ("content", "userId") VALUES (\'\\x74657374\', 2)'
334+
},
335+
sqlite: {
336+
text : 'INSERT INTO "post" ("content", "userId") VALUES ($1, $2)',
337+
string: 'INSERT INTO "post" ("content", "userId") VALUES (x\'74657374\', 2)'
338+
},
339+
mysql: {
340+
text : 'INSERT INTO `post` (`content`, `userId`) VALUES (?, ?)',
341+
string: 'INSERT INTO `post` (`content`, `userId`) VALUES (x\'74657374\', 2)'
342+
},
343+
params: [new Buffer('test'), 2]
344+
});
345+
346+
Harness.test({
347+
query: post.insert({
348+
content: new Buffer('test'),
349+
userId: 2
350+
}),
351+
pg: {
352+
text : 'INSERT INTO "post" ("content", "userId") VALUES ($1, $2)',
353+
string: 'INSERT INTO "post" ("content", "userId") VALUES (\'\\x74657374\', 2)'
354+
},
355+
sqlite: {
356+
text : 'INSERT INTO "post" ("content", "userId") VALUES ($1, $2)',
357+
string: 'INSERT INTO "post" ("content", "userId") VALUES (x\'74657374\', 2)'
358+
},
359+
mysql: {
360+
text : 'INSERT INTO `post` (`content`, `userId`) VALUES (?, ?)',
361+
string: 'INSERT INTO `post` (`content`, `userId`) VALUES (x\'74657374\', 2)'
362+
},
363+
params: [new Buffer('test'), 2]
364+
});
365+
366+
Harness.test({
367+
query: post.insert([{
368+
content: new Buffer('whoah')
369+
}, {
370+
content: new Buffer('hey')
371+
}
372+
]),
373+
pg: {
374+
text : 'INSERT INTO "post" ("content") VALUES ($1), ($2)',
375+
string: 'INSERT INTO "post" ("content") ' +
376+
'VALUES (\'\\x77686f6168\'), (\'\\x686579\')'
377+
},
378+
sqlite: {
379+
text : 'INSERT INTO "post" ("content") VALUES ($1), ($2)',
380+
string: 'INSERT INTO "post" ("content") VALUES (x\'77686f6168\'), (x\'686579\')'
381+
},
382+
mysql: {
383+
text : 'INSERT INTO `post` (`content`) VALUES (?), (?)',
384+
string: 'INSERT INTO `post` (`content`) VALUES (x\'77686f6168\'), (x\'686579\')'
385+
},
386+
params: [new Buffer('whoah'), new Buffer('hey')]
387+
});

test/dialects/update-tests.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,23 @@ Harness.test({
121121
},
122122
params: []
123123
});
124+
125+
// Binary updates
126+
Harness.test({
127+
query: post.update({
128+
content: new Buffer('test')
129+
}),
130+
pg: {
131+
text : 'UPDATE "post" SET "content" = $1',
132+
string: 'UPDATE "post" SET "content" = \'\\x74657374\''
133+
},
134+
sqlite: {
135+
text : 'UPDATE "post" SET "content" = $1',
136+
string: 'UPDATE "post" SET "content" = x\'74657374\''
137+
},
138+
mysql: {
139+
text : 'UPDATE `post` SET `content` = ?',
140+
string: 'UPDATE `post` SET `content` = x\'74657374\''
141+
},
142+
params: [new Buffer('test')]
143+
});

test/dialects/value-expression-tests.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
var Harness = require('./support');
44
var customer = Harness.defineCustomerTable();
5+
var post = Harness.definePostTable();
56
var v = Harness.defineVariableTable();
67

78
// Test composition of binary methods +, *, -, =.
@@ -76,3 +77,21 @@ Harness.test({
7677
},
7778
params: []
7879
});
80+
81+
Harness.test({
82+
query: post.select(post.id).where(post.content.equals(new Buffer('test'))),
83+
pg: {
84+
text : 'SELECT "post"."id" FROM "post" WHERE ("post"."content" = $1)',
85+
string: 'SELECT "post"."id" FROM "post" WHERE ("post"."content" = \'\\x74657374\')',
86+
},
87+
sqlite: {
88+
text : 'SELECT "post"."id" FROM "post" WHERE ("post"."content" = $1)',
89+
string: 'SELECT "post"."id" FROM "post" WHERE ("post"."content" = x\'74657374\')',
90+
},
91+
mysql: {
92+
text : 'SELECT `post`.`id` FROM `post` WHERE (`post`.`content` = ?)',
93+
string: 'SELECT `post`.`id` FROM `post` WHERE (`post`.`content` = x\'74657374\')',
94+
},
95+
params: [new Buffer('test')]
96+
});
97+

0 commit comments

Comments
 (0)