Skip to content

Commit

Permalink
Improve support for nested SQL statements in the SQL builder
Browse files Browse the repository at this point in the history
  • Loading branch information
nicksagona committed Jan 2, 2025
1 parent c804788 commit c7f1438
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/Sql.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function select(mixed $columns = null): ?Sql\Select
}
foreach ($columns as $name => $value) {
if (!is_numeric($name)) {
$this->select->addNamedValue($name, $value);
$this->select->addValue($value, $name);
} else {
$this->select->addValue($value);
}
Expand Down
29 changes: 8 additions & 21 deletions src/Sql/AbstractClause.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,34 +119,21 @@ public function setValues(array $values): AbstractClause
/**
* Add a value
*
* @param mixed $value
* @return AbstractClause
*/
public function addValue(mixed $value): AbstractClause
{
if (!is_array($value) && !is_object($value)) {
if ($this->isParameter($value)) {
$value = $this->getParameter($value);
}
$this->values[] = $value;
}
return $this;
}

/**
* Add a named value
*
* @param string $name
* @param mixed $value
* @param ?string $name
* @return AbstractClause
*/
public function addNamedValue(string $name, mixed $value): AbstractClause
public function addValue(mixed $value, ?string $name = null): AbstractClause
{
if (!is_array($value) && !is_object($value)) {
if (!is_array($value) && (($value instanceof \Stringable) || !is_object($value))) {
if ($this->isParameter($value, $name)) {
$value = $this->getParameter($value, $name);
}
$this->values[$name] = $value;
if ($name !== null) {
$this->values[$name] = $value;
} else {
$this->values[] = $value;
}
}
return $this;
}
Expand Down
13 changes: 8 additions & 5 deletions src/Sql/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -426,12 +426,15 @@ public function render(): string
if (count($this->values) > 0) {
$cols = [];
foreach ($this->values as $as => $col) {
// If column is a SQL function, don't quote it
$c = self::isSupportedFunction($col) ? $col : $this->quoteId($col);
if (!is_numeric($as)) {
$cols[] = $c . ' AS ' . $this->quoteId($as);
// If column is a nested SQL query
if ($col instanceof AbstractSql) {
$cols[] = (!is_numeric($as)) ?
'(' . $col . ') AS ' . $this->quoteId($as) : '(' . $col . ')';
} else {
$cols[] = $c;
// If column is a SQL function, don't quote it
$c = self::isSupportedFunction($col) ? $col : $this->quoteId($col);
$cols[] = (!is_numeric($as)) ?
$c . ' AS ' . $this->quoteId($as) : $c;
}
}
$sql .= implode(', ', $cols) . ' ';
Expand Down
2 changes: 1 addition & 1 deletion src/Sql/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Update extends AbstractPredicateClause
*/
public function set(string $name, mixed $value): Update
{
$this->addNamedValue($name, $value);
$this->addValue($value, $name);
return $this;
}

Expand Down

0 comments on commit c7f1438

Please sign in to comment.