2015-12-07 11:16:04 -05:00
|
|
|
/**
|
2016-09-14 16:50:32 -04:00
|
|
|
* Driver for SQLite databases
|
2015-12-07 11:16:04 -05:00
|
|
|
*
|
2016-09-14 16:50:32 -04:00
|
|
|
* @module drivers/Sqlite
|
2015-12-07 11:16:04 -05:00
|
|
|
*/
|
2016-02-12 12:50:59 -05:00
|
|
|
module.exports = (() => {
|
2015-12-07 11:16:04 -05:00
|
|
|
delete require.cache[require.resolve('../Driver')];
|
2018-02-09 17:29:26 -05:00
|
|
|
const driver = require('../Driver');
|
2015-12-03 20:43:42 -05:00
|
|
|
|
2015-12-07 11:16:04 -05:00
|
|
|
// Sqlite doesn't have a truncate command
|
|
|
|
driver.hasTruncate = false;
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
/**
|
|
|
|
* SQL to insert a group of rows
|
2015-12-07 11:16:04 -05:00
|
|
|
* Override default to have better compatibility
|
2015-12-03 20:43:42 -05:00
|
|
|
*
|
|
|
|
* @param {String} table - The table to insert to
|
|
|
|
* @param {Array} [data] - The array of object containing data to insert
|
2015-12-07 15:58:31 -05:00
|
|
|
* @return {String} - The generated sql statement
|
2015-12-03 20:43:42 -05:00
|
|
|
*/
|
2016-02-12 12:50:59 -05:00
|
|
|
driver.insertBatch = (table, data) => {
|
2015-12-03 20:43:42 -05:00
|
|
|
// Get the data values to insert, so they can
|
|
|
|
// be parameterized
|
2016-09-14 16:50:32 -04:00
|
|
|
let sql = '';
|
2018-02-09 17:29:26 -05:00
|
|
|
const first = data.shift();
|
2015-12-03 20:43:42 -05:00
|
|
|
|
2018-02-09 17:29:26 -05:00
|
|
|
const vals = [];
|
2015-12-07 15:58:31 -05:00
|
|
|
data.forEach(obj => {
|
2018-02-09 17:29:26 -05:00
|
|
|
const row = [];
|
2015-12-07 15:58:31 -05:00
|
|
|
Object.keys(obj).forEach(key => {
|
2015-12-03 20:43:42 -05:00
|
|
|
row.push(obj[key]);
|
2015-12-02 13:01:31 -05:00
|
|
|
});
|
2015-12-03 20:43:42 -05:00
|
|
|
vals.push(row);
|
|
|
|
});
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-07 15:58:31 -05:00
|
|
|
sql += `INSERT INTO ${driver.quoteTable(table)}\n`;
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
// Get the field names from the keys of the first
|
|
|
|
// object to be inserted
|
2018-02-09 17:29:26 -05:00
|
|
|
const fields = Object.keys(first);
|
|
|
|
const cols = [];
|
2015-12-08 10:40:52 -05:00
|
|
|
fields.forEach(key => {
|
2015-12-07 15:58:31 -05:00
|
|
|
cols.push(`'${driver._quote(first[key])}' AS ${driver.quoteIdentifiers(key)}`);
|
2015-12-03 20:43:42 -05:00
|
|
|
});
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-07 15:58:31 -05:00
|
|
|
sql += `SELECT ${cols.join(', ')}\n`;
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-07 17:03:36 -05:00
|
|
|
vals.forEach(rowValues => {
|
2018-02-09 17:29:26 -05:00
|
|
|
const quoted = rowValues.map(value => String(value).replace('\'', '\'\''));
|
2015-12-07 17:03:36 -05:00
|
|
|
sql += `UNION ALL SELECT '${quoted.join('\', \'')}'\n`;
|
2015-12-03 20:43:42 -05:00
|
|
|
});
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
return {
|
|
|
|
sql: sql,
|
2016-11-18 21:59:22 -05:00
|
|
|
values: undefined
|
2015-12-03 20:43:42 -05:00
|
|
|
};
|
2015-12-07 17:03:36 -05:00
|
|
|
};
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-07 11:16:04 -05:00
|
|
|
return driver;
|
2016-09-14 16:50:32 -04:00
|
|
|
})();
|