Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Expression functionality #125

Merged
merged 1 commit into from
Dec 28, 2019

Conversation

peteraba
Copy link
Contributor

Feature to enable setting values via SQL expressions and not just simple values.

values in expressions

Strictly speaking Expression doesn't have to handle values, but it's probably nicer to set up queries with it.

With Expressions handling values:

$expr = new Expression('(val + ?) % ?', ['1', PDO::PARAM_INT], [2, PDO::PARAM_INT]);
$query = new UpdateQuery('users', 'u', ['name' => 'david']);
$query->addColumnValues(['email' => 'bar@foo.com', 'is_val_even' => $expr])
    ->where('u.id = ?', 'emails.userid = u.id', 'emails.email = ?')
    ->orWhere('u.name = ?')
    ->andWhere('subscriptions.userid = u.id', "subscriptions.type = 'customer'")
    ->addUnnamedPlaceholderValues([[18175, PDO::PARAM_INT], 'foo@bar.com', 'dave']);

Without this features it becomes:

$expr = new Expression('(val + ?) % ?');
$query = new UpdateQuery('users', 'u', ['name' => 'david']);
$query->addColumnValues(['email' => 'bar@foo.com', 'is_val_even' => $expr])
    ->where('u.id = ?', 'emails.userid = u.id', 'emails.email = ?')
    ->orWhere('u.name = ?')
    ->andWhere('subscriptions.userid = u.id', "subscriptions.type = 'customer'")
    ->addUnnamedPlaceholderValues([[1, \PDO::PARAM_INT], [2, PDO::PARAM_INT], [18175, PDO::PARAM_INT], 'foo@bar.com', 'dave']);

If you feel this feature is not needed, just let me know and I'll remove it.

@peteraba peteraba force-pushed the query-builder-expressions branch 2 times, most recently from ff5a7a7 to bb647e2 Compare December 28, 2019 19:52
Copy link
Member

@davidbyoung davidbyoung left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work. A few questions:

  • Could/should expressions ever allow sub-expressions?
  • Could/should DELETE queries support expressions?

src/Opulence/QueryBuilders/Expression/Expression.php Outdated Show resolved Hide resolved
@peteraba
Copy link
Contributor Author

Thanks, glad you like it.

Regarding the questions:

  1. Well, originally class Expression was called Upsert because I was planning to have another expression class called Column to support SELECT queries. It was not really obvious from the documentation that it was already supported, but then I saw that SelectQuery::__construct already takes expressions. Anyway I'll move Expression into QueryBuilders.
  2. Since WHERE and HAVING clauses already support expressions, I don't really see how they should be affected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants