Skip to content

Add sql query builder #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/QueryBuilder/Column.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php


namespace TipsTricks\QueryBuilder;


class Column
{
private $expression;
private $alias;

public function __construct(string $expression, string $alias = null)
{
$this->expression = $expression;
$this->alias = $alias;
}

public function __toString()
{
if ($this->alias) {
return Query::INDENT . $this->expression . ' AS ' . $this->alias;
}
return Query::INDENT . $this->expression;
}
}
20 changes: 20 additions & 0 deletions src/QueryBuilder/From.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php


namespace TipsTricks\QueryBuilder;


class From
{
private $expression;

public function __construct(string $expression)
{
$this->expression = $expression;
}

public function __toString()
{
return "FROM " . $this->expression . PHP_EOL;
}
}
26 changes: 26 additions & 0 deletions src/QueryBuilder/Join.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php


namespace TipsTricks\QueryBuilder;


class Join
{
private $table;

private $condition;

public function __construct($table)
{
$this->table = $table;
}

private function conditions() {
return " ON order.customer_id = customer.id";
}

public function __toString()
{
return Query::INDENT . "JOIN " . $this->table . $this->conditions();
}
}
39 changes: 39 additions & 0 deletions src/QueryBuilder/Query.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace TipsTricks\QueryBuilder;

class Query
{
const INDENT = " ";

const FINAL_POINT = ";";

private $select;
private $from;
private $where;

public function __construct(Select $select, From $from, Where $where)
{
$this->select = $select;
$this->from = $from;
$this->where = $where;
}

public function __toString()
{
$parts = [
$this->select,
$this->from(),
$this->where,
Query::FINAL_POINT,
];

return implode("\n", $parts);
}

private function from()
{
return $this->from . (new Join('customer'));
}
}

19 changes: 19 additions & 0 deletions src/QueryBuilder/Select.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace TipsTricks\QueryBuilder;

class Select
{
private $columns;

public function __construct(array $columns)
{
$this->columns = $columns;
}

public function __toString()
{
$select = implode(",\n", $this->columns);
return PHP_EOL . "SELECT" . PHP_EOL . $select;
}
}
13 changes: 13 additions & 0 deletions src/QueryBuilder/Where.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php


namespace TipsTricks\QueryBuilder;


class Where
{
public function __toString()
{
return "WHERE " . new WhereEquals('customer.id', '14');
}
}
22 changes: 22 additions & 0 deletions src/QueryBuilder/WhereEquals.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php


namespace TipsTricks\QueryBuilder;


class WhereEquals
{
private $one;
private $two;

public function __construct($one, $two)
{
$this->one = $one;
$this->two = $two;
}

public function __toString()
{
return $this->one . ' = ' . $this->two;
}
}
37 changes: 37 additions & 0 deletions tests/QueryBuilder/QueryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

use TipsTricks\QueryBuilder\Column;
use TipsTricks\QueryBuilder\From;
use TipsTricks\QueryBuilder\Query;
use TipsTricks\QueryBuilder\Select;
use TipsTricks\QueryBuilder\Where;

class QueryTest extends \PHPUnit\Framework\TestCase
{
public function testQuery()
{
$selects = [
new Column("SUM(order.price)", "total"),
new Column("customer.name"),
];

$from = new From('order');

$select = new Select($selects);

$where = new Where();
$query = new Query($select, $from, $where);

$sql = <<<SQL

SELECT
SUM(order.price) AS total,
customer.name
FROM order
JOIN customer ON order.customer_id = customer.id
WHERE customer.id = 14
;
SQL;
self::assertSame($sql, (string) $query);
}
}