PHP-DB icon indicating copy to clipboard operation
PHP-DB copied to clipboard

Adding increment and decrement methods

Open AdamSEY opened this issue 5 years ago • 2 comments

While working with NodeJS I found some super popular package called knex.js using increment and decrement methods and thought about adding them to this great library

AdamSEY avatar May 20 '19 19:05 AdamSEY

Thank you very much!

The question is whether

$db->increment(
    'users',
    [ 'balance' => 25 ],
    [ 'username' => 'Adam' ]
);

is considerably shorter and more convenient than

$db->exec(
    'UPDATE users SET balance = balance + ? WHERE username = ?',
    [
        25,
        'Adam'
    ]
);

which is already supported today. The answer seems to be yes, it is. Not by a large margin, but still. So it would make sense to add it.

I’m not sure if we need separate increment and decrement methods when we can just use the sign of the numbers supplied in the second argument. The two methods could both simply be named addition. (We could even add multiplication as well.) But if we want a verb as the method name here, I can’t think of any better name (that is still unambiguous) than increment and decrement.

By the way, I also noticed that in the original Node.js package “knex.js”, this is part of a query builder, not a standalone method as it would be here. That should not really matter, although you cannot create complex conditions without a query builder.

ocram avatar May 23 '19 17:05 ocram

Yes I think with the query builder its much easier, maybe in the future we can work on a query builder, I don't think it's that hard with PHP.

actually I thought about using a variable to determine the sign, but I felt it's a little bit unreadable and ambiguous that's way I made a private function for that matter.

Those two methods might be some unneeded extras, but I found them fun and useful 👍 Feel free to improve the current code or close the pull request, that's totally fine :)

AdamSEY avatar May 23 '19 19:05 AdamSEY