Skip to content

Commit

Permalink
extended support for join
Browse files Browse the repository at this point in the history
added support for custom condtions as arrayo
  • Loading branch information
Sorin Valer Stanila committed Jun 13, 2016
1 parent 5f4d636 commit 74e9235
Show file tree
Hide file tree
Showing 13 changed files with 113 additions and 10 deletions.
8 changes: 5 additions & 3 deletions lib/Mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,14 @@ public function collectionClass()

/**
* Entity manager class for storing information and meta-data about entities
*
* @param string $entityName
* @return \Spot\Entity\Manager
*/
public function entityManager()
public function entityManager($entityName = null)
{
$entityName = $this->entity();
if (!$entityName) {
$entityName = $this->entity();
}
if (!isset(self::$entityManager[$entityName])) {
self::$entityManager[$entityName] = new Entity\Manager($entityName);
}
Expand Down
68 changes: 62 additions & 6 deletions lib/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -421,10 +421,11 @@ public function whereSql($sql)
*
* @param array $where Array of conditions for this clause
* @param bool $useAlias
* @param string $entityName
* @return array SQL fragment strings for WHERE clause
* @throws Exception
*/
private function parseWhereToSQLFragments(array $where, $useAlias = true)
private function parseWhereToSQLFragments(array $where, $useAlias = true, $entityName=null)
{
$builder = $this->builder();

Expand Down Expand Up @@ -457,9 +458,14 @@ private function parseWhereToSQLFragments(array $where, $useAlias = true)

// Prefix column name with alias
if ($useAlias === true) {
$col = $this->fieldWithAlias($col);
$col = $this->fieldWithAlias($col, true, $entityName);
}

if ( $this->stringIsExistingField($entityName, $value) ){
$value = function () use ($value){
return $value;
};
}
$sqlFragments[] = $operatorCallable($builder, $col, $value);
}

Expand Down Expand Up @@ -777,18 +783,25 @@ public function escapeIdentifier($identifier)
* Get field name with table alias appended
* @param string $field
* @param bool $escaped
* @param string $entityName
* @return string
*/
public function fieldWithAlias($field, $escaped = true)
public function fieldWithAlias($field, $escaped = true, $entityName = null)
{
$fieldInfo = $this->_mapper->entityManager()->fields();
$fieldInfo = $this->_mapper->entityManager($entityName)->fields();

//extract table alias if present
list($field, $table) = $this->extractTableAndFieldFromString($field);
// Determine real field name (column alias support)
if (isset($fieldInfo[$field])) {
$field = $fieldInfo[$field]['column'];
}

$field = $this->_tableName . '.' . $field;
if (!$table) {
$table = $this->_mapper->entityManager($entityName)->table();
}

$field = $table . '.' . $field;

return $escaped ? $this->escapeIdentifier($field) : $field;
}
Expand Down Expand Up @@ -903,7 +916,7 @@ private function addMapping($type, array $data)
public function makeJoin($type, $fromAlias, $entityName, $alias, $condition)
{
$joinTable = $this->mapper()->getMapper($entityName)->table();
$conditionString = (string)$condition;
// $conditionString = (string)$condition;

$this->addMapping(
'join',
Expand All @@ -916,6 +929,8 @@ public function makeJoin($type, $fromAlias, $entityName, $alias, $condition)
),
)
);
// $testCondition = explode('=', $condition);
$conditionString = implode(' AND ', $this->parseWhereToSQLFragments($condition, true, $entityName));
// $conditionString = implode(' =', $condition);
//@FIXME: now parameters are double escaped, because the initial are double escaped also :(
$this->builder()->$type(
Expand All @@ -927,4 +942,45 @@ public function makeJoin($type, $fromAlias, $entityName, $alias, $condition)

return $this;
}

/**
* Extract data from string, for strings which contains "point"
* @Example: table.field
* @param $string
* @return array
*/
public function extractTableAndFieldFromString($string)
{
$pointPosition = strpos($string, '.');
if ($pointPosition !== false) {
$table = substr($string, 0, $pointPosition);
$field = substr($string, $pointPosition + 1);
} else {
$table = null;
$field = $string;
}

return [$field, $table];

}

public function stringIsExistingField($entityName, $value)
{
$field = null;

if (is_array($value)) {
return $field;
}

$fieldInfo = array_merge($this->_mapper->entityManager($entityName)->fields(),
$this->_mapper->entityManager()->fields());
//extract table alias if present
list($extractedField, $table) = $this->extractTableAndFieldFromString($value);
// Determine real field name (column alias support)
if (isset($fieldInfo[$extractedField])) {
$field = $fieldInfo[$extractedField]['column'];
}

return $field;
}
}
4 changes: 4 additions & 0 deletions lib/Query/Operator/Equals.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ public function __invoke(QueryBuilder $builder, $column, $value)
return $column . ' IS NULL';
}

if ($value instanceof \Closure) {
return $column . ' = ' . $value();
}

return $column . ' = ' . $builder->createPositionalParameter($value);
}
}
4 changes: 4 additions & 0 deletions lib/Query/Operator/FullText.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ class FullText
*/
public function __invoke(QueryBuilder $builder, $column, $value)
{
if ($value instanceof \Closure) {
return $column . ' = ' . $value();
}

return 'MATCH(' . $column . ') AGAINST (' . $builder->createPositionalParameter($value) . ')';
}
}
4 changes: 4 additions & 0 deletions lib/Query/Operator/FullTextBoolean.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ class FullTextBoolean
*/
public function __invoke(QueryBuilder $builder, $column, $value)
{
if ($value instanceof \Closure) {
return $column . ' = ' . $value();
}

return 'MATCH(' . $column . ') AGAINST (' . $builder->createPositionalParameter($value) . ' IN BOOLEAN MODE)';
}
}
4 changes: 4 additions & 0 deletions lib/Query/Operator/GreaterThan.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ class GreaterThan
*/
public function __invoke(QueryBuilder $builder, $column, $value)
{
if ($value instanceof \Closure) {
return $column . ' = ' . $value();
}

return $column . ' > ' . $builder->createPositionalParameter($value);
}
}
4 changes: 4 additions & 0 deletions lib/Query/Operator/GreaterThanOrEqual.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ class GreaterThanOrEqual
*/
public function __invoke(QueryBuilder $builder, $column, $value)
{
if ($value instanceof \Closure) {
return $column . ' = ' . $value();
}

return $column . ' >= ' . $builder->createPositionalParameter($value);
}
}
4 changes: 4 additions & 0 deletions lib/Query/Operator/In.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public function __invoke(QueryBuilder $builder, $column, $value)
throw new Exception("Use of IN operator expects value to be array. Got " . gettype($value) . ".");
}

if ($value instanceof \Closure) {
return $column . ' = ' . $value();
}

return $column . ' IN (' . $builder->createPositionalParameter($value, Connection::PARAM_STR_ARRAY) . ')';
}
}
4 changes: 4 additions & 0 deletions lib/Query/Operator/LessThan.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ class LessThan
*/
public function __invoke(QueryBuilder $builder, $column, $value)
{
if ($value instanceof \Closure) {
return $column . ' < ' . $value();
}

return $column . ' < ' . $builder->createPositionalParameter($value);
}
}
4 changes: 4 additions & 0 deletions lib/Query/Operator/LessThanOrEqual.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ class LessThanOrEqual
*/
public function __invoke(QueryBuilder $builder, $column, $value)
{
if ($value instanceof \Closure) {
return $column . ' <= ' . $value();
}

return $column . ' <= ' . $builder->createPositionalParameter($value);
}
}
4 changes: 4 additions & 0 deletions lib/Query/Operator/Like.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ class Like
*/
public function __invoke(QueryBuilder $builder, $column, $value)
{
if ($value instanceof \Closure) {
return $column . ' = ' . $value();
}

return $column . ' LIKE ' . $builder->createPositionalParameter($value);
}
}
7 changes: 6 additions & 1 deletion lib/Query/Operator/Not.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,18 @@ class Not
public function __invoke(QueryBuilder $builder, $column, $value)
{
if (is_array($value) && !empty($value)) {
return $column . ' NOT IN (' . $builder->createPositionalParameter($value, Connection::PARAM_STR_ARRAY) . ')';
return $column . ' NOT IN (' . $builder->createPositionalParameter($value,
Connection::PARAM_STR_ARRAY) . ')';
}

if ($value === null || (is_array($value) && empty($value))) {
return $column . ' IS NOT NULL';
}

if ($value instanceof \Closure) {
return $column . ' = ' . $value();
}

return $column . ' != ' . $builder->createPositionalParameter($value);
}
}
4 changes: 4 additions & 0 deletions lib/Query/Operator/RegExp.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ class RegExp
*/
public function __invoke(QueryBuilder $builder, $column, $value)
{
if ($value instanceof \Closure) {
return $column . ' = ' . $value();
}

return $column . ' REGEXP ' . $builder->createPositionalParameter($value);
}
}

0 comments on commit 74e9235

Please sign in to comment.