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

Filter expressions #30

Open
nilmerg opened this issue Jan 28, 2021 · 0 comments
Open

Filter expressions #30

nilmerg opened this issue Jan 28, 2021 · 0 comments
Labels
enhancement New feature or request

Comments

@nilmerg
Copy link
Member

nilmerg commented Jan 28, 2021

With 8ce7d5c, 12a46ca and 0a68c04 I've added classes that resemble specific SQL where expressions. But these are currently implemented in a way they play fine with other code handling filters. (e.g. ipl\Orm\Compat\FilterProcessor) This leads to unnecessary cases like this and of course bogus interface implementations. (All of them have either no value or no column)

What I'd like to see is a more generic filter object that can handle all of these cases (plus more) and plays fine with other code handling filter objects. Heck, it should even be possible to still make use of things like RewriteFilter objects so that the columns and values of a expression are processed.

My first concept of this object looks like this:

namespace ipl\Sql\Filter;

use ipl\Sql\ExpressionInterface;
use ipl\Stdlib\Filter;

class Expression implements ExpressionInterface, Filter\Rule
{
    protected $statement;

    protected $columns;

    public function __construct($statement, array $columns)
    {
        $this->statement = $statement;
        $this->columns = $columns;
    }

    public function getStatement()
    {
        return sprintf($this->statement, ...$this->getColumns());
    }

    public function getColumns()
    {
        return array_keys($this->columns);
    }

    public function setColumns(array $columns)
    {
        $this->columns = $columns;

        return $this;
    }

    public function getValues()
    {
        return array_values($this->columns);
    }
}

Usage would look this way:

use ipl\Sql\Filter\Expression;

// ipl\Sql\Select objects would need to be handled just like columns, because objects can't be keys.....
$exists = new Expression('EXISTS(%s)', [ipl\Sql\Select]);
$notExists = new Expression('NOT EXISTS(%s)', [ipl\Sql\Select]);

// Values are processed just as usual, columns will be inserted into the statement by the object itself.
// Separating them anyway allows pre-processing to happen.
$isNull = new Expression('%s IS NULL', ['id' => null]);
$case = new Expression('CASE WHEN %s = ? OR %s = ? THEN 1 ELSE 0 END', ['name' => 'foo', 'name' => 'bar']);
@nilmerg nilmerg added the enhancement New feature or request label Jan 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant