Skip to content

Commit

Permalink
QueryBuilder Bugs Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
steevenz committed Feb 27, 2018
1 parent 220b50c commit e10dae0
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 79 deletions.
2 changes: 2 additions & 0 deletions src/Sql/Abstracts/AbstractConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1032,6 +1032,8 @@ protected function replaceNamedBinds( $sqlStatement, array $sqlBinds )
}

$escapedValue = implode( ',', $escapedValue );
} elseif (strpos($bindReplace, ' AND ') !== false) {
$escapedValue = $bindReplace;
} else {
$escapedValue = preg_quote( trim( $escapedValue, $this->config[ 'escapeCharacter' ] ) );
}
Expand Down
161 changes: 82 additions & 79 deletions src/Sql/Abstracts/AbstractQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ abstract class AbstractQueryBuilder
*
* @var string
*/
protected $arrayObjectConversionMethod = 'json';
protected $arrayObjectConversionMethod = 'json_encode';

/**
* AbstractQueryBuilder::$SqlRandomKeywords
Expand Down Expand Up @@ -1242,6 +1242,57 @@ public function orHaving($field, $value = null, $escape = null)

//--------------------------------------------------------------------

/**
* AbstractQueryBuilder::whereBetween
*
* Add WHERE BETWEEN Sql statement portions into Query Builder
*
* @param string $field Input name
* @param array $values Array of between values
*
* @return static
*/
public function whereBetween($field, array $values = [], $escape = null)
{
return $this->prepareWhereStatement($field, implode(' AND ', $values), 'AND ', $escape, 'between');
}

//--------------------------------------------------------------------

/**
* AbstractQueryBuilder::orWhereBetween
*
* Add OR WHERE BETWEEN Sql statement portions into Query Builder
*
* @param string $field Input name
* @param array $values Array of between values
*
* @return static
*/
public function orWhereBetween($field, array $values = [], $escape = null)
{
return $this->prepareWhereStatement($field, implode(' AND ', $values), 'OR ', $escape, 'between');
}

//--------------------------------------------------------------------

/**
* AbstractQueryBuilder::whereNotBetween
*
* Add WHERE NOT BETWEEN Sql statement portions into Query Builder
*
* @param string $field Input name
* @param array $values Array of between values
*
* @return static
*/
public function whereNotBetween($field, array $values = [], $escape = null)
{
return $this->prepareWhereStatement($field, implode(' AND ', $values), 'OR ', $escape, 'notBetween');
}

//--------------------------------------------------------------------

/**
* AbstractQueryBuilder::prepareWhereHavingStatement
*
Expand Down Expand Up @@ -1270,17 +1321,13 @@ protected function prepareWhereStatement($field, $value = null, $type = 'AND ',
is_bool($escape) || $escape = $this->conn->isProtectIdentifiers;

foreach ($field as $fieldName => $fieldValue) {
$prefix = (count($this->builderCache->{$cacheKey}) === 0)
? $this->getBracketType('')
: $this->getBracketType($type);

if ($fieldValue !== null) {
$operator = $this->getOperator($fieldName);
$fieldName = trim(str_replace($operator, '', $fieldName));

$fieldBind = $this->bind($fieldName, $fieldValue);

if (empty($operator)) {
if (empty($operator) && !in_array($cacheKey, ['between', 'notBetween'])) {
$fieldName .= ' =';
} else {
$fieldName .= $operator;
Expand All @@ -1304,10 +1351,33 @@ protected function prepareWhereStatement($field, $value = null, $type = 'AND ',
? ' :' . $fieldBind
: $fieldValue;

$this->builderCache->{$cacheKey}[] = [
'condition' => $prefix . $fieldName . $fieldValue,
'escape' => $escape,
];
if($cacheKey === 'having') {
$prefix = (count($this->builderCache->having) === 0)
? $this->getBracketType('')
: $this->getBracketType($type);

$this->builderCache->having[] = [
'condition' => $prefix . $fieldName . $fieldValue,
'escape' => $escape,
];
} else {
$prefix = (count($this->builderCache->where) === 0)
? $this->getBracketType('')
: $this->getBracketType($type);

if($cacheKey === 'between') {
$condition = $prefix . $fieldName . ' BETWEEN' . $fieldValue;
} elseif($cacheKey === 'notBetween') {
$condition = $prefix . $fieldName . ' NOT BETWEEN' . $fieldValue;
} else {
$condition = $prefix . $fieldName . $fieldValue;
}

$this->builderCache->where[] = [
'condition' => $condition,
'escape' => $escape,
];
}
}

return $this;
Expand Down Expand Up @@ -1387,57 +1457,6 @@ public function orWhereNotIn($field, $values = [], $escape = null)

//--------------------------------------------------------------------

/**
* AbstractQueryBuilder::whereBetween
*
* Add WHERE BETWEEN Sql statement portions into Query Builder
*
* @param string $field Input name
* @param array $values Array of between values
*
* @return static
*/
public function whereBetween($field, array $values = [], $escape = null)
{
return $this->prepareWhereStatement($field, implode(' AND ', $values), 'AND ', $escape, 'between');
}

//--------------------------------------------------------------------

/**
* AbstractQueryBuilder::orWhereBetween
*
* Add OR WHERE BETWEEN Sql statement portions into Query Builder
*
* @param string $field Input name
* @param array $values Array of between values
*
* @return static
*/
public function orWhereBetween($field, array $values = [], $escape = null)
{
return $this->prepareWhereStatement($field, implode(' AND ', $values), 'OR ', $escape, 'between');
}

//--------------------------------------------------------------------

/**
* AbstractQueryBuilder::whereNotBetween
*
* Add WHERE NOT BETWEEN Sql statement portions into Query Builder
*
* @param string $field Input name
* @param array $values Array of between values
*
* @return static
*/
public function whereNotBetween($field, array $values = [], $escape = null)
{
return $this->prepareWhereStatement($field, implode(' AND ', $values), 'OR ', $escape, 'not_between');
}

//--------------------------------------------------------------------

/**
* AbstractQueryBuilder::orWhereNotBetween
*
Expand All @@ -1450,7 +1469,7 @@ public function whereNotBetween($field, array $values = [], $escape = null)
*/
public function orWhereNotBetween($field, array $values = [], $escape = null)
{
return $this->prepareWhereStatement($field, implode(' OR ', $values), 'OR ', $escape, 'not_between');
return $this->prepareWhereStatement($field, implode(' OR ', $values), 'OR ', $escape, 'notBetween');
}

//--------------------------------------------------------------------
Expand Down Expand Up @@ -2944,10 +2963,8 @@ public function getSqlStatement($reset = true)
'From',
'Join',
'Where',
'GroupBy',
'Having',
'Between',
'NotBetween',
'GroupBy',
'OrderBy',
'Limit',
];
Expand Down Expand Up @@ -3181,20 +3198,6 @@ protected function compileWhereHavingStatement($cacheKey)
);
}

if ($cacheKey === 'between') {
return "\n" . sprintf(
'BETWEEN %s',
implode("\n", $this->builderCache->between)
);
}

if ($cacheKey === 'notBetween') {
return "\n" . sprintf(
'NOT BETWEEN %s',
implode("\n", $this->builderCache->notBetween)
);
}

return "\n" . sprintf(
'WHERE %s',
implode("\n", $this->builderCache->{$cacheKey})
Expand Down

0 comments on commit e10dae0

Please sign in to comment.