You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: machines/compile-statement.js
+11-3Lines changed: 11 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -48,7 +48,8 @@ module.exports = {
48
48
outputDescription: 'The `nativeQuery` property is the compiled native query for the database. The `meta` property is reserved for custom driver-specific extensions.',
49
49
example: '==='
50
50
// example: {
51
-
// nativeQuery: '*',
51
+
// nativeQuery: 'SELECT * FROM foo',
52
+
// valuesToEscape: ['foo']
52
53
// meta: '==='
53
54
// }
54
55
},
@@ -105,9 +106,16 @@ module.exports = {
105
106
returnexits.error(err);
106
107
}
107
108
109
+
110
+
// Attach a flag to the meta object to denote that the query was generated
111
+
// with Knex and that it's valuesToEscape don't need to be processed any further.
Copy file name to clipboardExpand all lines: machines/send-native-query.js
+45-36Lines changed: 45 additions & 36 deletions
Original file line number
Diff line number
Diff line change
@@ -23,17 +23,23 @@ module.exports = {
23
23
},
24
24
25
25
nativeQuery: {
26
-
description: 'A SQL statement as a string (or to use built-in escaping, this should be provided as a dictionary).',
27
-
extendedDescription: 'If provided as a dictionary, this should contain `sql` (the SQL statement string; e.g. \'SELECT * FROM dogs WHERE name = ?\') as well as an array of `bindings` (e.g. [\'David\']).',
extendedDescription: 'If `valuesToEscape` is provided, this supports template syntax like `$1`, `$2`, etc.',
29
28
whereToGet: {
30
-
description: 'This is oftentimes compiled from Waterline query syntax using "Compile statement", however it could also originate from userland code.',
29
+
description: 'Write a native query for this database, or if this driver supports it, use `compileStatement()` to build a native query from Waterline syntax.',
30
+
extendedDescription: 'This might be compiled from a Waterline statement (stage 4 query) using "Compile statement", however it could also originate directly from userland code.'
31
31
},
32
-
example: '===',
33
-
// example: '*',
32
+
example: 'SELECT * FROM pets WHERE species=$1 AND nickname=$2',
34
33
required: true
35
34
},
36
35
36
+
valuesToEscape: {
37
+
description: 'An optional list of strings, numbers, or special literals (true, false, or null) to escape and include in the native query, in order.',
38
+
extendedDescription: 'The first value in the list will be used to replace `$1`, the second value to replace `$2`, and so on. Note that numbers, `true`, `false`, and `null` are interpreted _differently_ than if they were strings wrapped in quotes. This array must never contain any arrays or dictionaries.',
39
+
example: '===',
40
+
defaultsTo: []
41
+
},
42
+
37
43
meta: {
38
44
friendlyName: 'Meta (custom)',
39
45
description: 'Additional stuff to pass to the driver.',
@@ -95,49 +101,52 @@ module.exports = {
95
101
}
96
102
97
103
98
-
// Validate query
99
-
// (supports raw SQL string or dictionary consisting of `sql` and `bindings` properties)
returnexits.error(newError('Provided `nativeQuery` is invalid. Please specify either a string of raw SQL or a dictionary like `{sql: \'SELECT * FROM dogs WHERE name = $1\', bindings: [\'Rover\']}`.'));
112
-
}
104
+
// Validate provided native query.
105
+
varsql=inputs.nativeQuery;
106
+
varbindings=inputs.valuesToEscape||[];
107
+
varqueryInfo;
113
108
114
109
115
110
debug('Running SQL Query:');
116
111
debug('SQL: '+sql);
117
112
debug('Bindings: '+bindings);
118
113
debug('Connection Id: '+inputs.connection.id);
119
114
120
-
// Process SQL template, escaping bindings.
121
-
// This converts `$1`, `$2`, etc. into the escaped binding.
0 commit comments