123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- const {assert} = require('../../assert');
- const {TableName} = require('../table-name');
- const {ColumnSet} = require('../column-set');
- const npm = {
- formatting: require('../../formatting'),
- utils: require('../../utils')
- };
- function update(data, columns, table, options, capSQL) {
- if (!data || typeof data !== 'object') {
- throw new TypeError('Invalid parameter \'data\' specified.');
- }
- const isArray = Array.isArray(data);
- if (isArray && !data.length) {
- throw new TypeError('Cannot generate an UPDATE from an empty array.');
- }
- if (columns instanceof ColumnSet) {
- if (npm.utils.isNull(table)) {
- table = columns.table;
- }
- } else {
- if (isArray && npm.utils.isNull(columns)) {
- throw new TypeError('Parameter \'columns\' is required when updating multiple records.');
- }
- columns = new ColumnSet(columns || data);
- }
- options = assert(options, ['tableAlias', 'valueAlias', 'emptyUpdate']);
- const format = npm.formatting.as.format,
- useEmptyUpdate = 'emptyUpdate' in options,
- fmOptions = {capSQL};
- if (isArray) {
- const tableAlias = npm.formatting.as.alias(npm.utils.isNull(options.tableAlias) ? 't' : options.tableAlias);
- const valueAlias = npm.formatting.as.alias(npm.utils.isNull(options.valueAlias) ? 'v' : options.valueAlias);
- const q = capSQL ? sql.multi.capCase : sql.multi.lowCase;
- const actualColumns = columns.columns.filter(c => !c.cnd);
- if (checkColumns(actualColumns)) {
- return options.emptyUpdate;
- }
- checkTable();
- const targetCols = actualColumns.map(c => c.escapedName + '=' + valueAlias + '.' + c.escapedName).join();
- const values = data.map((d, index) => {
- if (!d || typeof d !== 'object') {
- throw new Error(`Invalid update object at index ${index}.`);
- }
- return '(' + format(columns.variables, columns.prepare(d), fmOptions) + ')';
- }).join();
- return format(q, [table.name, tableAlias, targetCols, values, valueAlias, columns.names], fmOptions);
- }
- const updates = columns.assign({source: data});
- if (checkColumns(updates)) {
- return options.emptyUpdate;
- }
- checkTable();
- const query = capSQL ? sql.single.capCase : sql.single.lowCase;
- return format(query, table.name) + format(updates, columns.prepare(data), fmOptions);
- function checkTable() {
- if (table && !(table instanceof TableName)) {
- table = new TableName(table);
- }
- if (!table) {
- throw new Error('Table name is unknown.');
- }
- }
- function checkColumns(cols) {
- if (!cols.length) {
- if (useEmptyUpdate) {
- return true;
- }
- throw new Error('Cannot generate an UPDATE without any columns.');
- }
- }
- }
- const sql = {
- single: {
- lowCase: 'update $1^ set ',
- capCase: 'UPDATE $1^ SET '
- },
- multi: {
- lowCase: 'update $1^ as $2^ set $3^ from (values$4^) as $5^($6^)',
- capCase: 'UPDATE $1^ AS $2^ SET $3^ FROM (VALUES$4^) AS $5^($6^)'
- }
- };
- module.exports = {update};
|