node-mysql2 icon indicating copy to clipboard operation
node-mysql2 copied to clipboard

Using prepared statements with bulk query() operations.

Open joejean opened this issue 9 years ago • 1 comments

Hi There,

From my understanding the following bulk insert does not use prepared statements:

var mysql = require('mysq2');
var conn = mysql.createConnection({
    ...
});

var sql = "INSERT INTO Test (name, email, n) VALUES ?";
var values = [
    ['demian', '[email protected]', 1],
    ['john', '[email protected]', 2],
    ['mark', '[email protected]', 3],
    ['pete', '[email protected]', 4]
];
conn.query(sql, [values], function(err) {
    if (err) throw err;
    conn.end();
});

I have two questions: 1- How safe are bulk operations with query()? Is there still room for sql injection attacks? 2- Would it be possible to make execute() support bulk operations just like query() while still using prepared statements?

joejean avatar Apr 16 '17 09:04 joejean

Would it be possible to make execute() support bulk operations just like query() while still using prepared statements?

it's possible, but I guess not very efficient - it would require preparing statement with as much parameters as values in input data

Have you tried load data local infile? Have a look at example at https://github.com/sidorares/node-mysql2/blob/master/documentation/Extras.md#sending-tabular-data-with-load-infile-and-local-stream I suspect it's more efficient and less prone to sql injection compared to large string constructed from INSERT INTO Test (name, email, n) VALUES + data.

sidorares avatar Apr 16 '17 09:04 sidorares