Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
closes #10085
  • Loading branch information
thinkingmedia authored and markstory committed Nov 3, 2017
1 parent 5661606 commit d696802
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/Database/Query.php
Expand Up @@ -14,6 +14,7 @@
*/
namespace Cake\Database;

use Cake\Database\Expression\IdentifierExpression;
use Cake\Database\Expression\OrderByExpression;
use Cake\Database\Expression\OrderClauseExpression;
use Cake\Database\Expression\QueryExpression;
Expand Down Expand Up @@ -1461,6 +1462,27 @@ public function into($table)
return $this;
}

/**
* Creates an expression that refers to an identifier. Identifiers are used to refer to field names and allow
* the SQL compiler to apply quotes or escape the identifier.
*
* The value is used as is, and you might be required to use aliases or include the table reference in
* the identifier. Do not use this method to inject SQL methods or logical statements.
*
* ### Example
*
* ```
* $query->newExp()->lte('count', $query->identifier('total'));
* ```
*
* @param string $identifier The identifier for an expression
* @return ExpressionInterface
*/
public function identifier($identifier)
{
return new IdentifierExpression($identifier);
}

/**
* Set the values for an insert query.
*
Expand Down
14 changes: 14 additions & 0 deletions tests/TestCase/Database/QueryTest.php
Expand Up @@ -3369,6 +3369,20 @@ public function testInsertExpressionValues()
$this->assertEquals($expected, $result->fetch('assoc'));
}

/**
* Tests that the identifier method creates an expression object.
*
* @return void
*/
public function testIdentifierExpression()
{
$query = new Query($this->connection);
/** @var IdentifierExpression $expression */
$expression = $query->identifier('foo');
$this->assertInstanceOf(IdentifierExpression::class, $expression);
$this->assertEquals('foo', $expression->getIdentifier());
}

/**
* Tests that functions are correctly transformed and their parameters are bound
*
Expand Down

0 comments on commit d696802

Please sign in to comment.