Skip to content

Commit

Permalink
Replace Sql constant with expanded strings to improve readability
Browse files Browse the repository at this point in the history
Signed-off-by: pine3ree <[email protected]>
  • Loading branch information
pine3ree committed Oct 18, 2023
1 parent 86e23bb commit 3c30440
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 31 deletions.
21 changes: 7 additions & 14 deletions src/Sql/Predicate/In.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,29 +111,22 @@ public function getSQL(DriverInterface $driver = null, Params $params = null): s
}

$values = [];
$hasNull = false;
$has_null = false;
foreach ($this->valueList as $value) {
if (null === $value) {
$hasNull = true;
$has_null = true;
continue;
}
$values[] = $this->getValueSQL($params, $value, null, 'in');
}

$ivl_sql = "(" . (empty($values) ? Sql::NULL : implode(", ", $values)) . ")";
$in_value_list = empty($values) ? Sql::NULL : implode(", ", $values);

$null_sql = "";
if ($hasNull) {
$null_sql = " " . (
static::$not
? Sql::AND . " {$identifier} " . Sql::IS_NOT . " " . Sql::NULL
: Sql::OR . " {$identifier} " . Sql::IS . " " . Sql::NULL
);
}
$sql = "{$identifier} {$operator} ({$in_value_list})";

$sql = "{$identifier} {$operator} {$ivl_sql}{$null_sql}";
if ($hasNull) {
$sql = "({$sql})";
if ($has_null) {
$null_sql = static::$not ? "AND {$identifier} IS NOT NULL" : "OR {$identifier} IS NULL";
$sql = "({$sql} {$null_sql})";
}

return $this->sql = $sql;
Expand Down
3 changes: 1 addition & 2 deletions src/Sql/Statement/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ public function getSQL(DriverInterface $driver = null, Params $params = null): s
);
}

$this->sql = Sql::DELETE . " " . Sql::FROM . " {$table} {$where_sql}";
return $this->sql;
return $this->sql = "DELETE FROM {$table} {$where_sql}";
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Sql/Statement/Insert.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ public function getSQL(DriverInterface $driver = null, Params $params = null): s

protected function generateSQL(DriverInterface $driver, Params $params): string
{
$insert = $this->ignore ? Sql::INSERT_IGNORE : Sql::INSERT;
$INSERT = $this->ignore ? Sql::INSERT_IGNORE : Sql::INSERT;
$table = $driver->quoteIdentifier($this->table);
$columns = $this->getColumnsSQL($driver);
$values = $this->getValuesSQL($driver, $params);
Expand All @@ -351,10 +351,10 @@ protected function generateSQL(DriverInterface $driver, Params $params): string
$column_list = empty($columns) ? "" : "{$columns} ";

if ($this->select instanceof Select) {
return "{$insert} " . Sql::INTO . " {$table} {$column_list}{$values}";
return "{$INSERT} INTO {$table} {$column_list}{$values}";
}

return "{$insert} " . Sql::INTO . " {$table} {$column_list}" . Sql::VALUES . " {$values}";
return "{$INSERT} INTO {$table} {$column_list}VALUES {$values}";
}

private function getColumnsSQL(DriverInterface $driver): string
Expand Down
23 changes: 14 additions & 9 deletions src/Sql/Statement/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -572,10 +572,11 @@ private function getFromSQL(DriverInterface $driver, Params $params, bool $prett
}

if (!empty($this->alias)) {
$from = trim("{$from} " . $driver->quoteAlias($this->alias));
$from = "{$from} {$driver->quoteAlias($this->alias)}";
$from = trim($from);
}

return Sql::FROM . " {$from}";
return "FROM {$from}";
}

/**
Expand Down Expand Up @@ -794,7 +795,9 @@ private function getGroupBySQL(DriverInterface $driver): string
$groupBy[$key] = $this->getIdentifierSQL($identifier, $driver);
}

$this->sqls['group'] = $sql = Sql::GROUP_BY . " " . implode(", ", $groupBy);
$grouping_element_list = implode(", ", $groupBy);

$this->sqls['group'] = $sql = "GROUP BY {$grouping_element_list}";
return $sql;
}

Expand Down Expand Up @@ -902,7 +905,9 @@ private function getOrderBySQL(DriverInterface $driver): string
$sqls[] = $this->getIdentifierSQL($identifier, $driver) . " {$direction}";
}

$this->sqls['order'] = $sql = Sql::ORDER_BY . " " . implode(", ", $sqls);
$sort_specification_list = implode(", ", $sqls);

$this->sqls['order'] = $sql = "ORDER BY {$sort_specification_list}";
return $sql;
}

Expand Down Expand Up @@ -955,15 +960,15 @@ private function getLimitSQL(DriverInterface $driver, Params $params): string
// PostgreSQL also supports OFFSET without LIMIT
if (isset($this->limit)) {
$limit = $params->create($this->limit, PDO::PARAM_INT, 'limit');
$sql = Sql::LIMIT . " {$limit}";
$sql = "LIMIT {$limit}";
}

if (isset($this->offset) && $this->offset > 0) {
if (!isset($sql)) {
$sql = Sql::LIMIT . " " . PHP_INT_MAX;
$sql = "LIMIT " . PHP_INT_MAX;
}
$offset = $params->create($this->offset, PDO::PARAM_INT, 'offset');
$sql .= " " . Sql::OFFSET . " {$offset}";
$sql .= " OFFSET {$offset}";
}

return $sql ?? '';
Expand Down Expand Up @@ -1195,7 +1200,7 @@ public function __get(string $name)
}

if ('where' === $name) {
if (!isset($this->where)) {
if ($this->where === null) {
$this->where = new Where();
$this->where->setParent($this);
}
Expand All @@ -1207,7 +1212,7 @@ public function __get(string $name)
}

if ('having' === $name) {
if (!isset($this->having)) {
if ($this->having === null) {
$this->having = new Having();
$this->having->setParent($this);
}
Expand Down
8 changes: 5 additions & 3 deletions src/Sql/Statement/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,9 @@ private function getBaseSQL(DriverInterface $driver, Params $params): string
$set[] = "{$column} = {$marker}";
}

return Sql::UPDATE . " {$table} " . Sql::SET . " " . implode(", ", $set);
$set_clause_list = implode(", ", $set);

return "UPDATE {$table} SET {$set_clause_list}";
}

/**
Expand All @@ -178,7 +180,7 @@ public function __get(string $name)
}

if ('where' === $name) {
if (!isset($this->where)) {
if ($this->where === null) {
$this->where = new Where();
$this->where->setParent($this);
}
Expand All @@ -191,7 +193,7 @@ public function __get(string $name)
public function __clone()
{
parent::__clone();
if (isset($this->where)) {
if ($this->where instanceof Where) {
$this->where = clone $this->where;
$this->where->setParent($this);
}
Expand Down

0 comments on commit 3c30440

Please sign in to comment.