Skip to content

Commit 513a40c

Browse files
compile statement correctly
1 parent bd855d2 commit 513a40c

File tree

1 file changed

+24
-17
lines changed

1 file changed

+24
-17
lines changed

machines/compile-statement.js

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
// Build up a SQLBuilder instance when the file is required rather than when the
2+
// function is run.
3+
var SQLBuilder = require('waterline-sql-builder')({
4+
dialect: 'mysql'
5+
});
6+
17
module.exports = {
28

39

@@ -70,33 +76,34 @@ module.exports = {
7076

7177

7278
fn: function compileStatement(inputs, exits) {
73-
var SQLBuilder = require('waterline-query-builder');
74-
75-
SQLBuilder.generateSql({
76-
dialect: 'mysql',
77-
query: inputs.statement
78-
}).exec({
79-
error: function error(err) {
79+
var compiledNativeQuery;
80+
try {
81+
compiledNativeQuery = SQLBuilder.generate(inputs.statement);
82+
} catch (err) {
83+
if (!err.code || err.code === 'error') {
8084
return exits.error(err);
81-
},
82-
malformed: function malformed(err) {
85+
}
86+
87+
if (err.code === 'malformed') {
8388
return exits.malformed({
8489
error: err,
8590
meta: inputs.meta
8691
});
87-
},
88-
notSupported: function notSupported(err) {
92+
}
93+
94+
if (err.code === 'notSupported') {
8995
return exits.notSupported({
9096
error: err,
9197
meta: inputs.meta
9298
});
93-
},
94-
success: function success(compiledNativeQuery) {
95-
return exits.success({
96-
nativeQuery: compiledNativeQuery,
97-
meta: inputs.meta
98-
});
9999
}
100+
101+
return exits.error(err);
102+
}
103+
104+
return exits.success({
105+
nativeQuery: compiledNativeQuery,
106+
meta: inputs.meta
100107
});
101108
}
102109

0 commit comments

Comments
 (0)