Skip to content

Commit

Permalink
Merge pull request #12 from PhpGt/select
Browse files Browse the repository at this point in the history
Basic functionality and tests
  • Loading branch information
g105b authored Sep 22, 2020
2 parents 7482cac + e36a6ee commit 8779480
Show file tree
Hide file tree
Showing 33 changed files with 805 additions and 22 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Object oriented representation of SQL queries.",

"require": {
"php": "^7.2"
"php": "^7.4"
},

"require-dev": {
Expand Down
4 changes: 2 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions src/Query/DeleteQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
namespace Gt\SqlBuilder\Query;

class DeleteQuery extends SqlQuery {
public function __toString():string {
return $this->processClauseList([
self::PRE_QUERY_COMMENT => $this->preQuery(),
"delete from" => $this->from(),
"partition" => $this->partition(),
"where" => $this->where(),
"order by" => $this->orderBy(),
"limit" => $this->limit(),
self::POST_QUERY_COMMENT => $this->postQuery(),
]);
}

public function from():array {
return [];
}

public function partition():array {
return [];
}

public function where():array {
return [];
}

public function orderBy():array {
return [];
}

public function limit():array {
return [];
}
}
31 changes: 31 additions & 0 deletions src/Query/InsertQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
namespace Gt\SqlBuilder\Query;

class InsertQuery extends ReplaceQuery {
public function __toString():string {
$query = $this->processClauseList([
self::PRE_QUERY_COMMENT => $this->preQuery(),
"insert into" => $this->into(),
"partition" => $this->partition(),
"set" => $this->normaliseSet($this->set()),
"on duplicate key update" => $this->normaliseSet($this->onDuplicate()),
self::POST_QUERY_COMMENT => $this->postQuery(),
]);

if($this->subQuery) {
$query = "( $query )";
}

return $query;
}

/**
* Return an assignment list that matches the set() rules. It is often
* useful to return a call to set() directly, as it is usual to list the
* same assignments as part of the "on duplicate key update" section as
* in the "set" section.
*/
public function onDuplicate():array {
return [];
}
}
52 changes: 52 additions & 0 deletions src/Query/ReplaceQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
namespace Gt\SqlBuilder\Query;

class ReplaceQuery extends SqlQuery {
public function __toString():string {
$query = $this->processClauseList([
self::PRE_QUERY_COMMENT => $this->preQuery(),
"replace into" => $this->into(),
"partition" => $this->partition(),
"set" => $this->normaliseSet($this->set()),
self::POST_QUERY_COMMENT => $this->postQuery(),
]);

if($this->subQuery) {
$query = "( $query )";
}

return $query;
}

public function into():array {
return [];
}

public function partition():array {
return [];
}

/**
* Return either an associative array where the keys are the column
* names and the values are the assignment values, or an indexed array
* where the values are the column names where the values will be
* inferred as the column name prefixed with the colon character.
*/
public function set():array {
return [];
}

protected function normaliseSet(array $setData):array {
$normalised = [];
foreach($setData as $i => $name) {
if(is_int($i)) {
$normalised[$name] = ":$name";
}
else {
$normalised[$i] = $name;
}
}

return $normalised;
}
}
64 changes: 57 additions & 7 deletions src/Query/SelectQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,99 @@

class SelectQuery extends SqlQuery {
public function __toString():string {
return $this->processClauseList([
$query = $this->processClauseList([
self::PRE_QUERY_COMMENT => $this->preQuery(),
"select" => $this->select(),
"from" => $this->from(),
"inner join" => $this->innerJoin(),
"cross join" => $this->crossJoin(),
// The underscore is not a typo (https://dev.mysql.com/doc/refman/8.0/en/join.html)
"straight_join" => $this->straightJoin(),
"left join" => $this->leftJoin(),
"left outer join" => $this->leftOuterJoin(),
"right join" => $this->rightJoin(),
"right outer join" => $this->rightOuterJoin(),
"where" => $this->where(),
"group by" => $this->groupBy(),
"having" => $this->having(),
"window" => $this->window(),
"order by" => $this->orderBy(),
"limit" => $this->limit(),
self::POST_QUERY_COMMENT => $this->postQuery(),
]);

if($this->subQuery) {
$query = "( $query )";
}

return $query;
}

/** @return string[]|SqlQuery[] */
/** @return string[] */
public function select():array {
return [];
}

/** @return string[]|SqlQuery[] */
public function from():array {
return [];
}

public function where():array {
/** @return string[] */
public function innerJoin():array {
return [];
}

public function groupBy():array {
/** @return string[] */
public function crossJoin():array {
return [];
}

public function having():array {
/** @return string[] */
public function straightJoin():array {
return [];
}

/** @return string[] */
public function leftJoin():array {
return [];
}

/** @return string[] */
public function leftOuterJoin():array {
return [];
}

/** @return string[] */
public function rightJoin():array {
return [];
}

public function window():array {
/** @return string[] */
public function rightOuterJoin():array {
return [];
}

/** @return string[]|SqlQuery[] */
public function where():array {
return [];
}

/** @return string[]|SqlQuery[] */
public function groupBy():array {
return [];
}

/** @return string[]|SqlQuery[] */
public function having():array {
return [];
}

/** @return string[] */
public function orderBy():array {
return [];
}

/** @return string[] */
public function limit():array {
return [];
}
Expand Down
70 changes: 69 additions & 1 deletion src/Query/SqlQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ abstract class SqlQuery {
const POST_QUERY_COMMENT = "/* postQuery */";
const WHERE_CLAUSES = ["where", "having"];

protected bool $subQuery;

public function __construct(bool $subQuery = false) {
$this->subQuery = $subQuery;
}

abstract public function __toString():string;

public function preQuery():string {
Expand All @@ -33,6 +39,21 @@ protected function processClauseList(array $clauses):string {
$parts
);
}
elseif($name === "set") {
$query .= $this->processSetClause($parts);
}
elseif($name === "on duplicate key update") {
$query .= $this->processSetClause($parts, $name);
}
elseif($name === "partition") {
$query .= $this->processPartitionClause($parts);
}
elseif(strstr($name, "join")) {
$query .= $this->processJoinClause(
$name,
$parts
);
}
else {
$query .= $this->processClause(
$name,
Expand All @@ -49,7 +70,7 @@ protected function processClause(
array $parts,
string $listChar = ","
):string {
if(empty($parts) || $parts[0] === "") {
if(empty($parts) || (isset($parts[0]) && $parts[0] === "")) {
return "";
}

Expand Down Expand Up @@ -92,4 +113,51 @@ protected function processWhereClause(
$query .= "\n\n";
return $query;
}

private function processJoinClause(
string $name,
array $parts
):string {
$query = "";

foreach($parts as $i => $part) {
$query .= $name . " ";
$part = str_replace(["\n", "\t"], " ", $part);
$query .= $part . PHP_EOL;
}

return $query;
}

private function processSetClause(
array $parts,
string $prefix = "set"
):string {
$query = "";

foreach($parts as $key => $value) {
if(strlen($query) === 0) {
$query .= "$prefix " . PHP_EOL;
}
else {
$query .= ", " . PHP_EOL;
}

$query .= "$key = $value";
}

return $query . PHP_EOL;
}

private function processPartitionClause(array $parts):string {
if(empty($parts)) {
return "";
}

return "partition ( "
. PHP_EOL
. implode(", " . PHP_EOL, $parts)
. " )"
. PHP_EOL;
}
}
Loading

0 comments on commit 8779480

Please sign in to comment.