Skip to content

Commit

Permalink
formatting and slight changes
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed May 26, 2016
1 parent b73ecfa commit 0908679
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 69 deletions.
4 changes: 4 additions & 0 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,10 @@ public function where($column, $operator = null, $value = null, $boolean = 'and'
// will be bound to each SQL statements when it is finally executed.
$type = 'Basic';

if (Str::contains($column, '->') && is_bool($value)) {
$value = new Expression($value ? 'true' : 'false');
}

$this->wheres[] = compact('type', 'column', 'operator', 'value', 'boolean');

if (! $value instanceof Expression) {
Expand Down
86 changes: 21 additions & 65 deletions src/Illuminate/Database/Query/Grammars/MySqlGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,16 @@ public function compileUpdate(Builder $query, $values)
{
$table = $this->wrapTable($query->from);

$columns = [];

// Each one of the columns in the update statements needs to be wrapped in the
// keyword identifiers, also a place-holder needs to be created for each of
// the values in the list of bindings so we can make the sets statements.
$columns = [];

foreach ($values as $key => $value) {
if ($this->isJsonSelector($key)) {
$columns[] = $this->prepareJsonUpdateColumn($key, new JsonExpression($value));
$columns[] = $this->compileJsonUpdateColumn(
$key, new JsonExpression($value)
);
} else {
$columns[] = $this->wrap($key).' = '.$this->parameter($value);
}
Expand Down Expand Up @@ -138,23 +140,21 @@ public function compileUpdate(Builder $query, $values)
}

/**
* Prepares the update column for JSON selectors using the JSON_SET MySQL function.
* Prepares a JSON column being updated using the JSON_SET function.
*
* @param string $key
* @param JsonExpression $value
* @param string $key
* @param \Illuminate\Database\JsonExpression $value
* @return string
*/
protected function prepareJsonUpdateColumn($key, JsonExpression $value)
protected function compileJsonUpdateColumn($key, JsonExpression $value)
{
$path = explode('->', $key);

$field = $this->wrapValue(array_shift($path));

$accessor = '"$.'.implode('.', $path).'"';

$sanitizedValue = $value->getValue();

return "{$field} = json_set({$field}, {$accessor}, {$sanitizedValue})";
return "{$field} = json_set({$field}, {$accessor}, {$value->getValue()})";
}

/**
Expand Down Expand Up @@ -188,61 +188,6 @@ public function compileDelete(Builder $query)
return $sql;
}

/**
* Check for a JSON selector.
*
* @param string $value
* @return bool
*/
protected function isJsonSelector($value)
{
return Str::contains($value, '->');
}

/**
* Compile a basic where clause.
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $where
* @return string
*/
protected function whereBasic(Builder $query, $where)
{
// If we have a JSON selector here we'll simply
// convert it to a JsonExpression which then
// sets the value correctly on the query
if ($this->isJsonSelector($where['column']) && is_bool($where['value'])) {
$this->removeWhereBindingFromQuery($query, $where);

$where['value'] = new JsonExpression($where['value']);
}

$value = $this->parameter($where['value']);

return $this->wrap($where['column']).' '.$where['operator'].' '.$value;
}

/**
* Removes one where binding from the query.
*
* @param Builder $query
* @param array $where
* @return void
*/
protected function removeWhereBindingFromQuery(Builder $query, $where)
{
$wheres = $query->wheres;
$offset = array_search($where, $wheres);

if ($offset !== false) {
$whereBindings = $query->getRawBindings()['where'];

unset($whereBindings[$offset]);

$query->setBindings($whereBindings, 'where');
}
}

/**
* Wrap a single string in keyword identifiers.
*
Expand Down Expand Up @@ -276,4 +221,15 @@ protected function wrapJsonSelector($value)

return $field.'->'.'"$.'.implode('.', $path).'"';
}

/**
* Determine if the given string is a JSON selector.
*
* @param string $value
* @return bool
*/
protected function isJsonSelector($value)
{
return Str::contains($value, '->');
}
}
10 changes: 6 additions & 4 deletions src/Illuminate/Database/Query/JsonExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Illuminate\Database\Query;

use InvalidArgumentException;

class JsonExpression extends Expression
{
/**
Expand All @@ -19,16 +21,16 @@ class JsonExpression extends Expression
*/
public function __construct($value)
{
$this->value = $this->getJsonValue($value);
$this->value = $this->getJsonBindingParameter($value);
}

/**
* Get the value of a JSON using the correct type.
* Translate the given value into the appropriate JSON binding parameter.
*
* @param mixed $value
* @return string
*/
protected function getJsonValue($value)
protected function getJsonBindingParameter($value)
{
switch ($type = gettype($value)) {
case 'boolean':
Expand All @@ -43,7 +45,7 @@ protected function getJsonValue($value)
return '?';
}

throw new \InvalidArgumentException('JSON value is of illegal type: '.$type);
throw new InvalidArgumentException('JSON value is of illegal type: '.$type);
}

/**
Expand Down

0 comments on commit 0908679

Please sign in to comment.