Skip to content

Commit 63772aa

Browse files
committed
SQL module can output named prepared statements
1 parent 9940bd0 commit 63772aa

File tree

2 files changed

+85
-17
lines changed

2 files changed

+85
-17
lines changed

src/plugins/sql-support/plugin.js

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,53 @@ QueryBuilder.defaults({
2121
is_not_empty: '!= ""',
2222
is_null: 'IS NULL',
2323
is_not_null: 'IS NOT NULL'
24+
},
25+
26+
sqlStatements: {
27+
'question_mark': function() {
28+
var bind_params = [];
29+
return {
30+
add: function(rule, value) {
31+
bind_params.push(value);
32+
return '?';
33+
},
34+
run: function() {
35+
return bind_params;
36+
}
37+
};
38+
},
39+
40+
'numbered': function() {
41+
var bind_index = 0;
42+
var bind_params = [];
43+
return {
44+
add: function(rule, value) {
45+
bind_params.push(value);
46+
bind_index++;
47+
return '$' + bind_index;
48+
},
49+
run: function() {
50+
return bind_params;
51+
}
52+
};
53+
},
54+
55+
'named': function() {
56+
var bind_index = {};
57+
var bind_params = {};
58+
return {
59+
add: function(rule, value) {
60+
if (!bind_index[rule.field]) bind_index[rule.field] = 0;
61+
bind_index[rule.field]++;
62+
var key = rule.field + '_' + bind_index[rule.field];
63+
bind_params[key] = value;
64+
return ':' + key;
65+
},
66+
run: function() {
67+
return bind_params;
68+
}
69+
};
70+
}
2471
}
2572
});
2673

@@ -37,8 +84,10 @@ QueryBuilder.extend({
3784
*/
3885
getSQL: function(stmt, nl, data) {
3986
data = (data===undefined) ? this.getRules() : data;
40-
stmt = (stmt===true || stmt===undefined) ? 'question_mark' : stmt;
41-
nl = (nl || nl===undefined) ? '\n' : ' ';
87+
nl = (nl===true) ? '\n' : ' ';
88+
89+
if (stmt===true || stmt===undefined) stmt = 'question_mark';
90+
if (typeof stmt == 'string') stmt = this.settings.sqlStatements[stmt]();
4291

4392
var that = this,
4493
bind_index = 1,
@@ -93,15 +142,7 @@ QueryBuilder.extend({
93142
}
94143

95144
if (stmt) {
96-
if (stmt == 'question_mark') {
97-
value+= '?';
98-
}
99-
else {
100-
value+= '$'+bind_index;
101-
}
102-
103-
bind_params.push(v);
104-
bind_index++;
145+
value+= stmt.add(rule, v);
105146
}
106147
else {
107148
if (typeof v === 'string') {
@@ -123,7 +164,7 @@ QueryBuilder.extend({
123164
if (stmt) {
124165
return {
125166
sql: sql,
126-
params: bind_params
167+
params: stmt.run()
127168
};
128169
}
129170
else {

tests/plugins.module.js

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,27 @@ $(function(){
7676
});
7777

7878
assert.deepEqual(
79-
$b.queryBuilder('getSQL', false, false),
79+
$b.queryBuilder('getSQL', false),
8080
basic_rules_sql_raw,
8181
'Should create SQL query'
8282
);
8383

8484
assert.deepEqual(
85-
$b.queryBuilder('getSQL', true, false),
85+
$b.queryBuilder('getSQL', 'question_mark'),
8686
basic_rules_sql_stmt,
87-
'Should create SQL query with statements'
87+
'Should create SQL query with statements (?)'
88+
);
89+
90+
assert.deepEqual(
91+
$b.queryBuilder('getSQL', 'numbered'),
92+
basic_rules_sql_stmt_num,
93+
'Should create SQL query with statements (numbered)'
94+
);
95+
96+
assert.deepEqual(
97+
$b.queryBuilder('getSQL', 'named'),
98+
basic_rules_sql_stmt_named,
99+
'Should create SQL query with statements (named)'
88100
);
89101

90102
assert.deepEqual(
@@ -306,13 +318,28 @@ $(function(){
306318
});
307319

308320

321+
var basic_rules_sql_raw = {
322+
sql: 'price < 10.25 AND name IS NULL AND ( category IN(\'mo\', \'mu\') OR id != \'1234-azer-5678\' ) '
323+
};
324+
309325
var basic_rules_sql_stmt = {
310326
sql: 'price < ? AND name IS NULL AND ( category IN(?, ?) OR id != ? ) ',
311327
params: [10.25, 'mo', 'mu', '1234-azer-5678']
312328
};
329+
330+
var basic_rules_sql_stmt_num = {
331+
sql: 'price < $1 AND name IS NULL AND ( category IN($2, $3) OR id != $4 ) ',
332+
params: [10.25, 'mo', 'mu', '1234-azer-5678']
333+
};
313334

314-
var basic_rules_sql_raw = {
315-
sql: 'price < 10.25 AND name IS NULL AND ( category IN(\'mo\', \'mu\') OR id != \'1234-azer-5678\' ) '
335+
var basic_rules_sql_stmt_named = {
336+
sql: 'price < :price_1 AND name IS NULL AND ( category IN(:category_1, :category_2) OR id != :id_1 ) ',
337+
params: {
338+
price_1: 10.25,
339+
category_1: 'mo',
340+
category_2: 'mu',
341+
id_1: '1234-azer-5678'
342+
}
316343
};
317344

318345
var basic_rules_mongodb = {'$and': [

0 commit comments

Comments
 (0)