2015-12-07 17:03:36 -05:00
|
|
|
'use strict';
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-07 11:16:04 -05:00
|
|
|
/**
|
|
|
|
* Driver for Sqlite databases
|
|
|
|
*
|
|
|
|
* @module drivers/sqlite
|
|
|
|
*/
|
2016-02-12 12:50:59 -05:00
|
|
|
module.exports = (() => {
|
2015-12-07 11:16:04 -05:00
|
|
|
delete require.cache[require.resolve('../Driver')];
|
2015-12-08 10:40:52 -05:00
|
|
|
let 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-07 11:16:04 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
// Get the data values to insert, so they can
|
|
|
|
// be parameterized
|
2015-12-07 17:03:36 -05:00
|
|
|
let sql = '',
|
2015-12-03 20:43:42 -05:00
|
|
|
vals = [],
|
|
|
|
cols = [],
|
|
|
|
fields = [],
|
2015-12-08 10:40:52 -05:00
|
|
|
first = data.shift();
|
2015-12-03 20:43:42 -05:00
|
|
|
|
2015-12-07 15:58:31 -05:00
|
|
|
data.forEach(obj => {
|
2015-12-07 12:03:42 -05:00
|
|
|
let 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
|
|
|
|
fields = Object.keys(first);
|
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 => {
|
2016-03-11 13:29:01 -05:00
|
|
|
let 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,
|
2015-12-07 17:03:36 -05:00
|
|
|
values: null,
|
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-02-12 12:50:59 -05:00
|
|
|
})();
|