From c72b10f06af07cb9954000f7399e6ec7311c8d57 Mon Sep 17 00:00:00 2001 From: Georgi Hristov Date: Mon, 10 May 2021 13:18:52 +0200 Subject: [PATCH] Expression implements Expressionable MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Michael Voříšek --- docs/connection.rst | 8 ++++---- src/Expression.php | 13 ++++++++++--- src/Query.php | 10 +++++----- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/docs/connection.rst b/docs/connection.rst index 8bcbfac0..7d5c53af 100644 --- a/docs/connection.rst +++ b/docs/connection.rst @@ -72,13 +72,13 @@ class. Connection class is also responsible for executing queries. This is only used if you connect to vendor that does not use PDO. -.. php:method:: execute(Expression $expr) +.. php:method:: execute(Expression $expr): \Doctrine\DBAL\Result Creates new Expression class and sets :php:attr:`Expression::connection`. :param Expression $expr: Expression (or query) to execute :returns: `Doctrine\DBAL\Result` - + .. php:method:: registerConnectionClass($connectionClass = null, $connectionType = null) Adds connection class to the registry for resolving in Connection::resolveConnectionClass method. @@ -93,6 +93,6 @@ Developers can register custom classes to handle driver types using the `Conneci .. php:method:: connectDbalConnection(array $dsn) - The method should establish connection with DB and return the underlying connection object used by - the `Connection` class. By default PDO is used but the method can be overriden to return custom object to be + The method should establish connection with DB and return the underlying connection object used by + the `Connection` class. By default PDO is used but the method can be overriden to return custom object to be used for connection to DB. diff --git a/src/Expression.php b/src/Expression.php index 9dc0f997..e29ce9c0 100644 --- a/src/Expression.php +++ b/src/Expression.php @@ -9,7 +9,7 @@ use Doctrine\DBAL\Platforms\PostgreSQL94Platform; use Doctrine\DBAL\Result as DbalResult; -class Expression implements \ArrayAccess +class Expression implements Expressionable, \ArrayAccess { /** @const string "[]" in template, escape as parameter */ protected const ESCAPE_PARAM = 'param'; @@ -114,6 +114,14 @@ public function __toString() return $this->getOne(); } + /** + * @return $this + */ + public function getDsqlExpression(self $expression): self + { + return $this; + } + /** * Whether or not an offset exists. * @@ -252,13 +260,12 @@ protected function consume($expression, string $escapeMode = self::ESCAPE_PARAM) ->addMoreInfo('escapeMode', $escapeMode); } - // User may add Expressionable trait to any class, then pass it's objects if ($expression instanceof Expressionable) { $expression = $expression->getDsqlExpression($this); } if (!$expression instanceof self) { - throw (new Exception('Only Expressions or Expressionable objects may be used in Expression')) + throw (new Exception('Only Expressionable object type may be used in Expression')) ->addMoreInfo('object', $expression); } diff --git a/src/Query.php b/src/Query.php index bdb104da..d7ddf228 100644 --- a/src/Query.php +++ b/src/Query.php @@ -601,7 +601,7 @@ public function where($field, $cond = null, $value = null, $kind = 'where', $num break; case 2: - if (is_object($cond) && !$cond instanceof Expressionable && !$cond instanceof Expression) { + if (is_object($cond) && !$cond instanceof Expressionable) { throw (new Exception('Value cannot be converted to SQL-compatible expression')) ->addMoreInfo('field', $field) ->addMoreInfo('value', $cond); @@ -611,7 +611,7 @@ public function where($field, $cond = null, $value = null, $kind = 'where', $num break; case 3: - if (is_object($value) && !$value instanceof Expressionable && !$value instanceof Expression) { + if (is_object($value) && !$value instanceof Expressionable) { throw (new Exception('Value cannot be converted to SQL-compatible expression')) ->addMoreInfo('field', $field) ->addMoreInfo('cond', $cond) @@ -859,7 +859,7 @@ public function set($field, $value = null) return $this; } - if (is_string($field) || $field instanceof Expression || $field instanceof Expressionable) { + if (is_string($field) || $field instanceof Expressionable) { $this->args['set'][] = [$field, $value]; } else { throw (new Exception('Field name should be string or Expressionable')) @@ -1068,8 +1068,8 @@ public function _render_limit() * $q->order('name desc, id asc') * $q->order('name',true); * - * @param string|Expression|Expressionable|array $order Order by - * @param string|bool $desc true to sort descending + * @param string|Expressionable|array $order order by + * @param string|bool $desc true to sort descending * * @return $this */