Skip to content
This repository was archived by the owner on May 11, 2021. It is now read-only.

Commit

Permalink
Expression implements Expressionable
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Voříšek <[email protected]>
  • Loading branch information
georgehristov and mvorisek authored May 10, 2021
1 parent d4cc87e commit c72b10f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
8 changes: 4 additions & 4 deletions docs/connection.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
13 changes: 10 additions & 3 deletions src/Expression.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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);
}

Expand Down
10 changes: 5 additions & 5 deletions src/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)
Expand Down Expand Up @@ -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'))
Expand Down Expand Up @@ -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
*/
Expand Down

0 comments on commit c72b10f

Please sign in to comment.