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

@@ -4,7 +4,7 @@ namespace Driver\SQL\Column;
class BoolColumn extends Column {
public function __construct($name, $defaultValue=false) {
public function __construct(string $name, bool $defaultValue = false) {
parent::__construct($name, false, $defaultValue);
}

View File

@@ -8,14 +8,14 @@ class Column {
private bool $nullable;
private $defaultValue;
public function __construct($name, $nullable = false, $defaultValue = NULL) {
public function __construct(string $name, bool $nullable = false, $defaultValue = NULL) {
$this->name = $name;
$this->nullable = $nullable;
$this->defaultValue = $defaultValue;
}
public function getName() { return $this->name; }
public function notNull() { return !$this->nullable; }
public function getName(): string { return $this->name; }
public function notNull(): bool { return !$this->nullable; }
public function getDefaultValue() { return $this->defaultValue; }
}

View File

@@ -4,7 +4,7 @@ namespace Driver\SQL\Column;
class DateTimeColumn extends Column {
public function __construct($name, $nullable=false, $defaultValue=NULL) {
public function __construct(string $name, bool $nullable = false, $defaultValue = NULL) {
parent::__construct($name, $nullable, $defaultValue);
}
}

View File

@@ -6,10 +6,10 @@ class EnumColumn extends Column {
private array $values;
public function __construct($name, $values, $nullable=false, $defaultValue=NULL) {
public function __construct(string $name, array $values, bool $nullable = false, $defaultValue = NULL) {
parent::__construct($name, $nullable, $defaultValue);
$this->values = $values;
}
public function getValues() { return $this->values; }
public function getValues(): array { return $this->values; }
}

View File

@@ -4,7 +4,7 @@ namespace Driver\SQL\Column;
class IntColumn extends Column {
public function __construct($name, $nullable=false, $defaultValue=NULL) {
public function __construct(string $name, bool $nullable = false, $defaultValue = NULL) {
parent::__construct($name, $nullable, $defaultValue);
}

View File

@@ -4,7 +4,7 @@ namespace Driver\SQL\Column;
class JsonColumn extends Column {
public function __construct($name, $nullable=false, $defaultValue=null) {
public function __construct(string $name, bool $nullable = false, $defaultValue = null) {
parent::__construct($name, $nullable, $defaultValue);
}

View File

@@ -4,7 +4,7 @@ namespace Driver\SQL\Column;
class SerialColumn extends Column {
public function __construct($name, $defaultValue=NULL) {
public function __construct(string $name, $defaultValue = NULL) {
parent::__construct($name, false, $defaultValue); # not nullable
}

View File

@@ -6,10 +6,10 @@ class StringColumn extends Column {
private ?int $maxSize;
public function __construct($name, $maxSize=null, $nullable=false, $defaultValue=null) {
public function __construct(string $name, ?int $maxSize = null, bool $nullable = false, $defaultValue = null) {
parent::__construct($name, $nullable, $defaultValue);
$this->maxSize = $maxSize;
}
public function getMaxSize() { return $this->maxSize; }
public function getMaxSize(): ?int { return $this->maxSize; }
}

View File

@@ -8,14 +8,14 @@ class Compare extends Condition {
private string $column;
private $value;
public function __construct($col, $val, $operator='=') {
public function __construct(string $col, $val, string $operator = '=') {
$this->operator = $operator;
$this->column = $col;
$this->value = $val;
}
public function getColumn() { return $this->column; }
public function getColumn(): string { return $this->column; }
public function getValue() { return $this->value; }
public function getOperator() { return $this->operator; }
public function getOperator(): string { return $this->operator; }
}

View File

@@ -10,5 +10,5 @@ class CondAnd extends Condition {
$this->conditions = $conditions;
}
public function getConditions() { return $this->conditions; }
public function getConditions(): array { return $this->conditions; }
}

View File

@@ -12,6 +12,6 @@ class CondIn extends Condition {
$this->expression = $expression;
}
public function getColumn() { return $this->column; }
public function getColumn(): string { return $this->column; }
public function getExpression() { return $this->expression; }
}

View File

@@ -8,7 +8,7 @@ abstract class CondKeyword extends Condition {
private $rightExpression;
private string $keyword;
public function __construct($keyword, $leftExpression, $rightExpression) {
public function __construct(string $keyword, $leftExpression, $rightExpression) {
$this->leftExpression = $leftExpression;
$this->rightExpression = $rightExpression;
$this->keyword = $keyword;
@@ -16,5 +16,5 @@ abstract class CondKeyword extends Condition {
public function getLeftExp() { return $this->leftExpression; }
public function getRightExp() { return $this->rightExpression; }
public function getKeyword() { return $this->keyword; }
public function getKeyword(): string { return $this->keyword; }
}

View File

@@ -10,5 +10,5 @@ class CondNull extends Condition {
$this->column = $col;
}
public function getColumn() { return $this->column; }
public function getColumn(): string { return $this->column; }
}

View File

@@ -10,5 +10,5 @@ class CondOr extends Condition {
$this->conditions = (!empty($conditions) && is_array($conditions[0])) ? $conditions[0] : $conditions;
}
public function getConditions() { return $this->conditions; }
public function getConditions(): array { return $this->conditions; }
}

View File

@@ -10,5 +10,5 @@ abstract class Constraint {
$this->columnNames = (!is_array($columnNames) ? array($columnNames) : $columnNames);
}
public function getColumnNames() { return $this->columnNames; }
public function getColumnNames(): array { return $this->columnNames; }
}

View File

@@ -10,14 +10,14 @@ class ForeignKey extends Constraint {
private string $referencedColumn;
private ?Strategy $strategy;
public function __construct($name, $refTable, $refColumn, $strategy = NULL) {
public function __construct(string $name, string $refTable, string $refColumn, ?Strategy $strategy = NULL) {
parent::__construct($name);
$this->referencedTable = $refTable;
$this->referencedColumn = $refColumn;
$this->strategy = $strategy;
}
public function getReferencedTable() { return $this->referencedTable; }
public function getReferencedColumn() { return $this->referencedColumn; }
public function onDelete() { return $this->strategy; }
public function getReferencedTable(): string { return $this->referencedTable; }
public function getReferencedColumn(): string { return $this->referencedColumn; }
public function onDelete(): ?Strategy { return $this->strategy; }
}

View File

@@ -4,9 +4,10 @@ namespace Driver\SQL\Expression;
use Driver\SQL\Condition\Compare;
# TODO: change confusing class inheritance here
class Add extends Compare {
public function __construct($col, $val) {
public function __construct(string $col, $val) {
parent::__construct($col, $val, "+");
}

View File

@@ -8,9 +8,9 @@ class Join {
private string $table;
private string $columnA;
private string $columnB;
private $tableAlias;
private ?string $tableAlias;
public function __construct($type, $table, $columnA, $columnB, $tableAlias=null) {
public function __construct(string $type, string $table, string $columnA, string $columnB, ?string $tableAlias = null) {
$this->type = $type;
$this->table = $table;
$this->columnA = $columnA;
@@ -18,10 +18,10 @@ class Join {
$this->tableAlias = $tableAlias;
}
public function getType() { return $this->type; }
public function getTable() { return $this->table; }
public function getColumnA() { return $this->columnA; }
public function getColumnB() { return $this->columnB; }
public function getTableAlias() { return $this->tableAlias; }
public function getType(): string { return $this->type; }
public function getTable(): string { return $this->table; }
public function getColumnA(): string { return $this->columnA; }
public function getColumnB(): string { return $this->columnB; }
public function getTableAlias(): ?string { return $this->tableAlias; }
}

View File

@@ -6,10 +6,10 @@ class Keyword {
private string $value;
public function __construct($value) {
public function __construct(string $value) {
$this->value = $value;
}
public function getValue() { return $this->value; }
public function getValue(): string { return $this->value; }
}

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; }
}

View File

@@ -12,9 +12,11 @@ class UpdateStrategy extends Strategy {
$this->values = $values;
}
public function getConflictingColumns() {
public function getConflictingColumns(): array {
return $this->conflictingColumns;
}
public function getValues() { return $this->values; }
public function getValues(): array {
return $this->values;
}
}