Skip to content

Commit

Permalink
Merge df721bc into 1c782cf
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Lidén committed Oct 5, 2018
2 parents 1c782cf + df721bc commit f70b293
Show file tree
Hide file tree
Showing 3 changed files with 351 additions and 60 deletions.
47 changes: 35 additions & 12 deletions src/query/builder.js
Expand Up @@ -844,13 +844,36 @@ assign(Builder.prototype, {
},

// Increments a column's value by the specified amount.
increment(column, amount) {
increment(column, amount = 1) {
if (isObject(column)) {
for (const key in column) {
this._counter(key, column[key]);
}

return this;
}

return this._counter(column, amount);
},

// Decrements a column's value by the specified amount.
decrement(column, amount) {
return this._counter(column, amount, '-');
decrement(column, amount = 1) {
if (isObject(column)) {
for (const key in column) {
this._counter(key, -column[key]);
}

return this;
}

return this._counter(column, -amount);
},

// Clears increments/decrements
clearCounters() {
this._single.counter = {};

return this;
},

// Sets the values for a `select` query, informing that only the first
Expand Down Expand Up @@ -1018,15 +1041,15 @@ assign(Builder.prototype, {
// ----------------------------------------------------------------------

// Helper for the incrementing/decrementing queries.
_counter(column, amount, symbol) {
let amt = parseFloat(amount);
if (isNaN(amt)) amt = 1;
this._method = 'counter';
this._single.counter = {
column,
amount: amt,
symbol: symbol || '+',
};
_counter(column, amount) {
amount = parseFloat(amount);

this._method = 'update';

this._single.counter = this._single.counter || {};

this._single.counter[column] = amount;

return this;
},

Expand Down
44 changes: 28 additions & 16 deletions src/query/compiler.js
Expand Up @@ -16,6 +16,8 @@ import {
map,
omitBy,
reduce,
has,
keys,
} from 'lodash';
import uuid from 'uuid';

Expand Down Expand Up @@ -540,21 +542,6 @@ assign(QueryCompiler.prototype, {
}
},

// Compile the "counter".
counter() {
const { counter } = this.single;
const toUpdate = {};
toUpdate[counter.column] = this.client.raw(
this.formatter.wrap(counter.column) +
' ' +
(counter.symbol || '+') +
' ' +
counter.amount
);
this.single.update = toUpdate;
return this.update();
},

// On Clause
// ------

Expand Down Expand Up @@ -750,11 +737,36 @@ assign(QueryCompiler.prototype, {
},

// "Preps" the update.
_prepUpdate(data) {
_prepUpdate(data = {}) {
const { counter = {} } = this.single;

for (const column of keys(counter)) {
//Skip?
if (has(data, column)) {
//Needed?
this.client.logger.warn(
`increment/decrement called for a column that has already been specified in main .update() call. Ignoring increment/decrement and using value from .update() call.`
);
continue;
}

let value = counter[column];

const symbol = value < 0 ? '-' : '+';

if (symbol === '-') {
value = -value;
}

data[column] = this.client.raw(`?? ${symbol} ?`, [column, value]);
}

data = omitBy(data, isUndefined);

const vals = [];
const columns = Object.keys(data);
let i = -1;

while (++i < columns.length) {
vals.push(
this.formatter.wrap(columns[i]) +
Expand Down

0 comments on commit f70b293

Please sign in to comment.