The documentation for node-mysql on npm (https://npmjs.org/package/mysql) has an option for overlapping column names in table joins that looks like this:
var options = {sql: '...', nestTables: true};
connection.query(options, function(err, results) {
/* results will be an array like this now:
[{
table1: {
fieldA: '...',
fieldB: '...',
},
table2: {
fieldA: '...',
fieldB: '...',
},
}, ...]
*/
});
Escaping query identifiers format passes a second param to the connection.query()
that sanitizes the identifiers:
connection.query('SELECT * FROM users WHERE id = ?', [userId], function(err, results) {
// ...
});
I'm wondering how you can use these two together? Say you need to escape identifiers in a join, but want to declare the options for nested tables:
var sqlString: 'SELECT * FROM TableOne INNER JOIN TableTwo ON TableOne.id = TableTwo.tableone_id WHERE TableOne.id = ?';
var options = {sql: sqlString, nestTables: true};
connection.query(options, function(err, results) {
...
});
I attempted to pass the escape values as the second param in the connection.query()
like so:
var sqlString: 'SELECT * FROM TableOne INNER JOIN TableTwo ON TableOne.id = TableTwo.tableone_id WHERE TableOne.id = ?';
var options = {sql: sqlString, nestTables: true};
connection.query(options, 1, function(err, results) {
...
});
But to no avail. I'm wondering if there is anyway to do this?
If you read the code, line 66 there's an IF block
if (typeof sql === 'object') {
options = sql;
cb = values;
values = options.values;
}
meaning you would do
var sqlString: 'SELECT * FROM TableOne INNER JOIN TableTwo ON TableOne.id = TableTwo.tableone_id WHERE TableOne.id = ?';
var options = {sql: sqlString, nestTables: true, values: [1]};
connection.query(options, function(err, results) {
...
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.