code cleanup

This commit is contained in:
2021-04-02 21:58:06 +02:00
parent 4a52ab2fd7
commit eea0aeacc6
67 changed files with 472 additions and 425 deletions

View File

@@ -13,6 +13,8 @@ use Driver\SQL\Column\JsonColumn;
use Driver\SQL\Constraint\PrimaryKey;
use Driver\SQL\Constraint\Unique;
use Driver\SQL\Constraint\ForeignKey;
use Driver\SQL\SQL;
use Driver\SQL\Strategy\Strategy;
class CreateTable extends Query {
@@ -21,7 +23,7 @@ class CreateTable extends Query {
private array $constraints;
private bool $ifNotExists;
public function __construct($sql, $name) {
public function __construct(SQL $sql, string $name) {
parent::__construct($sql);
$this->tableName = $name;
$this->columns = array();
@@ -29,67 +31,67 @@ class CreateTable extends Query {
$this->ifNotExists = false;
}
public function addSerial($name) {
public function addSerial(string $name): CreateTable {
$this->columns[$name] = new SerialColumn($name);
return $this;
}
public function addString($name, $maxSize=NULL, $nullable=false, $defaultValue=NULL) {
public function addString(string $name, ?int $maxSize = NULL, bool $nullable = false, $defaultValue = NULL): CreateTable {
$this->columns[$name] = new StringColumn($name, $maxSize, $nullable, $defaultValue);
return $this;
}
public function addDateTime($name, $nullable=false, $defaultValue=NULL) {
public function addDateTime(string $name, bool $nullable = false, $defaultValue = NULL): CreateTable {
$this->columns[$name] = new DateTimeColumn($name, $nullable, $defaultValue);
return $this;
}
public function addInt($name, $nullable=false, $defaultValue=NULL) {
public function addInt(string $name, bool $nullable = false, $defaultValue = NULL): CreateTable {
$this->columns[$name] = new IntColumn($name, $nullable, $defaultValue);
return $this;
}
public function addBool($name, $defaultValue=false) {
public function addBool(string $name, $defaultValue = false): CreateTable {
$this->columns[$name] = new BoolColumn($name, $defaultValue);
return $this;
}
public function addJson($name, $nullable=false, $defaultValue=NULL) {
public function addJson(string $name, bool $nullable = false, $defaultValue = NULL): CreateTable {
$this->columns[$name] = new JsonColumn($name, $nullable, $defaultValue);
return $this;
}
public function addEnum($name, $values, $nullable=false, $defaultValue=NULL) {
public function addEnum(string $name, array $values, bool $nullable = false, $defaultValue = NULL): CreateTable {
$this->columns[$name] = new EnumColumn($name, $values, $nullable, $defaultValue);
return $this;
}
public function primaryKey(...$names) {
public function primaryKey(...$names): CreateTable {
$this->constraints[] = new PrimaryKey($names);
return $this;
}
public function unique(...$names) {
public function unique(...$names): CreateTable {
$this->constraints[] = new Unique($names);
return $this;
}
public function foreignKey($name, $refTable, $refColumn, $strategy = NULL) {
public function foreignKey(string $name, string $refTable, string $refColumn, ?Strategy $strategy = NULL): CreateTable {
$this->constraints[] = new ForeignKey($name, $refTable, $refColumn, $strategy);
return $this;
}
public function onlyIfNotExists() {
public function onlyIfNotExists(): CreateTable {
$this->ifNotExists = true;
return $this;
}
public function execute() {
public function execute(): bool {
return $this->sql->executeCreateTable($this);
}
public function ifNotExists() { return $this->ifNotExists; }
public function getTableName() { return $this->tableName; }
public function getColumns() { return $this->columns; }
public function getConstraints() { return $this->constraints; }
public function ifNotExists(): bool { return $this->ifNotExists; }
public function getTableName(): string { return $this->tableName; }
public function getColumns(): array { return $this->columns; }
public function getConstraints(): array { return $this->constraints; }
}

View File

@@ -3,19 +3,20 @@
namespace Driver\SQL\Query;
use Driver\SQL\Condition\CondOr;
use Driver\SQL\SQL;
class Delete extends Query {
private string $table;
private array $conditions;
public function __construct($sql, $table) {
public function __construct(SQL $sql, string $table) {
parent::__construct($sql);
$this->table = $table;
$this->conditions = array();
}
public function where(...$conditions) {
public function where(...$conditions): Delete {
$this->conditions[] = (count($conditions) === 1 ? $conditions : new CondOr($conditions));
return $this;
}
@@ -24,6 +25,6 @@ class Delete extends Query {
return $this->sql->executeDelete($this);
}
public function getTable() { return $this->table; }
public function getConditions() { return $this->conditions; }
public function getTable(): string { return $this->table; }
public function getConditions(): array { return $this->conditions; }
}

View File

@@ -14,16 +14,16 @@ class Drop extends Query {
* @param SQL $sql
* @param string $table
*/
public function __construct(\Driver\SQL\SQL $sql, string $table) {
public function __construct(SQL $sql, string $table) {
parent::__construct($sql);
$this->table = $table;
}
public function execute() {
$this->sql->executeDrop($this);
public function execute(): bool {
return $this->sql->executeDrop($this);
}
public function getTable() {
public function getTable(): string {
return $this->table;
}
}

View File

@@ -2,6 +2,7 @@
namespace Driver\SQL\Query;
use Driver\SQL\SQL;
use Driver\SQL\Strategy\Strategy;
class Insert extends Query {
@@ -12,7 +13,7 @@ class Insert extends Query {
private ?Strategy $onDuplicateKey;
private ?string $returning;
public function __construct($sql, $name, $columns=array()) {
public function __construct(SQL $sql, string $name, array $columns = array()) {
parent::__construct($sql);
$this->tableName = $name;
$this->columns = $columns;
@@ -21,28 +22,28 @@ class Insert extends Query {
$this->returning = NULL;
}
public function addRow(...$values) {
public function addRow(...$values): Insert {
$this->rows[] = $values;
return $this;
}
public function onDuplicateKeyStrategy($strategy) {
public function onDuplicateKeyStrategy(Strategy $strategy): Insert {
$this->onDuplicateKey = $strategy;
return $this;
}
public function returning($column) {
public function returning(string $column): Insert {
$this->returning = $column;
return $this;
}
public function execute() {
public function execute(): bool {
return $this->sql->executeInsert($this);
}
public function getTableName() { return $this->tableName; }
public function getColumns() { return $this->columns; }
public function getRows() { return $this->rows; }
public function onDuplicateKey() { return $this->onDuplicateKey; }
public function getReturning() { return $this->returning; }
public function getTableName(): string { return $this->tableName; }
public function getColumns(): array { return $this->columns; }
public function getRows(): array { return $this->rows; }
public function onDuplicateKey(): ?Strategy { return $this->onDuplicateKey; }
public function getReturning(): ?string { return $this->returning; }
}

View File

@@ -9,16 +9,16 @@ abstract class Query {
protected SQL $sql;
public bool $dump;
public function __construct($sql) {
public function __construct(SQL $sql) {
$this->sql = $sql;
$this->dump = false;
}
public function dump() {
public function dump(): Query {
$this->dump = true;
return $this;
}
public abstract function execute();
public abstract function execute(): bool;
}

View File

@@ -30,68 +30,68 @@ class Select extends Query {
$this->sortAscending = true;
}
public function from(...$tables) {
public function from(...$tables): Select {
$this->tables = array_merge($this->tables, $tables);
return $this;
}
public function where(...$conditions) {
public function where(...$conditions): Select {
$this->conditions[] = (count($conditions) === 1 ? $conditions : new CondOr($conditions));
return $this;
}
public function innerJoin($table, $columnA, $columnB, $tableAlias=null) {
public function innerJoin(string $table, string $columnA, string $columnB, ?string $tableAlias = null): Select {
$this->joins[] = new Join("INNER", $table, $columnA, $columnB, $tableAlias);
return $this;
}
public function leftJoin($table, $columnA, $columnB, $tableAlias=null) {
public function leftJoin(string $table, string $columnA, string $columnB, ?string $tableAlias = null): Select {
$this->joins[] = new Join("LEFT", $table, $columnA, $columnB, $tableAlias);
return $this;
}
public function groupBy(...$columns) {
public function groupBy(...$columns): Select {
$this->groupColumns = $columns;
return $this;
}
public function orderBy(...$columns) {
public function orderBy(...$columns): Select {
$this->orderColumns = $columns;
return $this;
}
public function ascending() {
public function ascending(): Select {
$this->sortAscending = true;
return $this;
}
public function descending() {
public function descending(): Select {
$this->sortAscending = false;
return $this;
}
public function limit($limit) {
public function limit(int $limit): Select {
$this->limit = $limit;
return $this;
}
public function offset($offset) {
public function offset(int $offset): Select {
$this->offset = $offset;
return $this;
}
public function execute() {
public function execute(): bool {
return $this->sql->executeSelect($this);
}
public function getColumns() { return $this->columns; }
public function getTables() { return $this->tables; }
public function getConditions() { return $this->conditions; }
public function getJoins() { return $this->joins; }
public function isOrderedAscending() { return $this->sortAscending; }
public function getOrderBy() { return $this->orderColumns; }
public function getLimit() { return $this->limit; }
public function getOffset() { return $this->offset; }
public function getGroupBy() { return $this->groupColumns; }
public function getColumns(): array { return $this->columns; }
public function getTables(): array { return $this->tables; }
public function getConditions(): array { return $this->conditions; }
public function getJoins(): array { return $this->joins; }
public function isOrderedAscending(): bool { return $this->sortAscending; }
public function getOrderBy(): array { return $this->orderColumns; }
public function getLimit(): int { return $this->limit; }
public function getOffset(): int { return $this->offset; }
public function getGroupBy(): array { return $this->groupColumns; }
}

View File

@@ -2,18 +2,20 @@
namespace Driver\SQL\Query;
use Driver\SQL\SQL;
class Truncate extends Query {
private string $tableName;
public function __construct($sql, $name) {
public function __construct(SQL $sql, string $name) {
parent::__construct($sql);
$this->tableName = $name;
}
public function execute() {
public function execute(): bool {
return $this->sql->executeTruncate($this);
}
public function getTable() { return $this->tableName; }
public function getTable(): string { return $this->tableName; }
}

View File

@@ -3,6 +3,7 @@
namespace Driver\SQL\Query;
use Driver\SQL\Condition\CondOr;
use Driver\SQL\SQL;
class Update extends Query {
@@ -10,28 +11,28 @@ class Update extends Query {
private string $table;
private array $conditions;
public function __construct($sql, $table) {
public function __construct(SQL $sql, string $table) {
parent::__construct($sql);
$this->values = array();
$this->table = $table;
$this->conditions = array();
}
public function where(...$conditions) {
public function where(...$conditions): Update {
$this->conditions[] = (count($conditions) === 1 ? $conditions : new CondOr($conditions));
return $this;
}
public function set($key, $val) {
public function set(string $key, $val): Update {
$this->values[$key] = $val;
return $this;
}
public function execute() {
public function execute(): bool {
return $this->sql->executeUpdate($this);
}
public function getTable() { return $this->table; }
public function getConditions() { return $this->conditions; }
public function getValues() { return $this->values; }
public function getTable(): string { return $this->table; }
public function getConditions(): array { return $this->conditions; }
public function getValues(): array { return $this->values; }
}