Some fixes / improvements

This commit is contained in:
Roman Hergenreder 2020-04-03 17:39:58 +02:00
parent a8fc52b42a
commit ad604a309e
38 changed files with 254 additions and 5993 deletions

@ -0,0 +1,5 @@
<component name="ProjectCodeStyleConfiguration">
<state>
<option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" />
</state>
</component>

@ -5,6 +5,7 @@ namespace Api\ApiKey;
use \Api\Request; use \Api\Request;
use DateTime; use DateTime;
use \Driver\SQL\Condition\Compare; use \Driver\SQL\Condition\Compare;
use Exception;
class Fetch extends Request { class Fetch extends Request {
@ -34,12 +35,12 @@ class Fetch extends Request {
foreach($res as $row) { foreach($res as $row) {
try { try {
$validUntil = (new DateTime($row["valid_until"]))->getTimestamp(); $validUntil = (new DateTime($row["valid_until"]))->getTimestamp();
} catch (\Exception $e) { } catch (Exception $e) {
$validUntil = $row["valid_until"]; $validUntil = $row["valid_until"];
} }
$this->result["api_keys"][] = array( $this->result["api_keys"][] = array(
"uid" => $row["uid"], "uid" => intval($row["uid"]),
"api_key" => $row["api_key"], "api_key" => $row["api_key"],
"valid_until" => $validUntil, "valid_until" => $validUntil,
); );

@ -9,5 +9,3 @@ class BoolColumn extends Column {
} }
} }
?>

@ -4,8 +4,8 @@ namespace Driver\SQL\Column;
class Column { class Column {
private $name; private string $name;
private $nullable; private bool $nullable;
private $defaultValue; private $defaultValue;
public function __construct($name, $nullable = false, $defaultValue = NULL) { public function __construct($name, $nullable = false, $defaultValue = NULL) {
@ -19,5 +19,3 @@ class Column {
public function getDefaultValue() { return $this->defaultValue; } public function getDefaultValue() { return $this->defaultValue; }
} }
?>

@ -8,5 +8,3 @@ class DateTimeColumn extends Column {
parent::__construct($name, $nullable, $defaultValue); parent::__construct($name, $nullable, $defaultValue);
} }
} }
?>

@ -4,7 +4,7 @@ namespace Driver\SQL\Column;
class EnumColumn extends Column { class EnumColumn extends Column {
private $values; private array $values;
public function __construct($name, $values, $nullable=false, $defaultValue=NULL) { public function __construct($name, $values, $nullable=false, $defaultValue=NULL) {
parent::__construct($name, $nullable, $defaultValue); parent::__construct($name, $nullable, $defaultValue);
@ -13,5 +13,3 @@ class EnumColumn extends Column {
public function getValues() { return $this->values; } public function getValues() { return $this->values; }
} }
?>

@ -9,5 +9,3 @@ class IntColumn extends Column {
} }
} }
?>

@ -9,5 +9,3 @@ class JsonColumn extends Column {
} }
} }
?>

@ -9,5 +9,3 @@ class SerialColumn extends Column {
} }
} }
?>

@ -4,7 +4,7 @@ namespace Driver\SQL\Column;
class StringColumn extends Column { class StringColumn extends Column {
private $maxSize; private ?int $maxSize;
public function __construct($name, $maxSize=null, $nullable=false, $defaultValue=null) { public function __construct($name, $maxSize=null, $nullable=false, $defaultValue=null) {
parent::__construct($name, $nullable, $defaultValue); parent::__construct($name, $nullable, $defaultValue);
@ -13,5 +13,3 @@ class StringColumn extends Column {
public function getMaxSize() { return $this->maxSize; } public function getMaxSize() { return $this->maxSize; }
} }
?>

@ -4,6 +4,10 @@ namespace Driver\SQL\Condition;
class Compare extends Condition { class Compare extends Condition {
private string $operator;
private string $column;
private $value;
public function __construct($col, $val, $operator='=') { public function __construct($col, $val, $operator='=') {
$this->operator = $operator; $this->operator = $operator;
$this->column = $col; $this->column = $col;
@ -15,5 +19,3 @@ class Compare extends Condition {
public function getOperator() { return $this->operator; } public function getOperator() { return $this->operator; }
} }
?>

@ -4,7 +4,7 @@ namespace Driver\SQL\Condition;
class CondAnd extends Condition { class CondAnd extends Condition {
private $conditions; private array $conditions;
public function __construct(...$conditions) { public function __construct(...$conditions) {
$this->conditions = $conditions; $this->conditions = $conditions;
@ -12,5 +12,3 @@ class CondAnd extends Condition {
public function getConditions() { return $this->conditions; } public function getConditions() { return $this->conditions; }
} }
?>

@ -4,6 +4,8 @@ namespace Driver\SQL\Condition;
class CondBool extends Condition { class CondBool extends Condition {
private $value;
public function __construct($val) { public function __construct($val) {
$this->value = $val; $this->value = $val;
} }
@ -11,5 +13,3 @@ class CondBool extends Condition {
public function getValue() { return $this->value; } public function getValue() { return $this->value; }
} }
?>

@ -4,7 +4,7 @@ namespace Driver\SQL\Condition;
class CondOr extends Condition { class CondOr extends Condition {
private $conditions; private array $conditions;
public function __construct(...$conditions) { public function __construct(...$conditions) {
$this->conditions = $conditions; $this->conditions = $conditions;
@ -12,5 +12,3 @@ class CondOr extends Condition {
public function getConditions() { return $this->conditions; } public function getConditions() { return $this->conditions; }
} }
?>

@ -5,5 +5,3 @@ namespace Driver\SQL\Condition;
abstract class Condition { abstract class Condition {
} }
?>

@ -4,13 +4,11 @@ namespace Driver\SQL\Constraint;
abstract class Constraint { abstract class Constraint {
private $columnName; private array $columnNames;
public function __construct($columnName) { public function __construct($columnNames) {
$this->columnName = $columnName; $this->columnNames = (!is_array($columnNames) ? array($columnNames) : $columnNames);
} }
public function getColumnName() { return $this->columnName; } public function getColumnNames() { return $this->columnNames; }
}; }
?>

@ -2,11 +2,13 @@
namespace Driver\SQL\Constraint; namespace Driver\SQL\Constraint;
use Driver\SQL\Strategy\Strategy;
class ForeignKey extends Constraint { class ForeignKey extends Constraint {
private $referencedTable; private string $referencedTable;
private $referencedColumn; private string $referencedColumn;
private $strategy; private ?Strategy $strategy;
public function __construct($name, $refTable, $refColumn, $strategy = NULL) { public function __construct($name, $refTable, $refColumn, $strategy = NULL) {
parent::__construct($name); parent::__construct($name);
@ -18,6 +20,4 @@ class ForeignKey extends Constraint {
public function getReferencedTable() { return $this->referencedTable; } public function getReferencedTable() { return $this->referencedTable; }
public function getReferencedColumn() { return $this->referencedColumn; } public function getReferencedColumn() { return $this->referencedColumn; }
public function onDelete() { return $this->strategy; } public function onDelete() { return $this->strategy; }
}; }
?>

@ -8,6 +8,4 @@ class PrimaryKey extends Constraint {
parent::__construct((!empty($names) && is_array($names[0])) ? $names[0] : $names); parent::__construct((!empty($names) && is_array($names[0])) ? $names[0] : $names);
} }
}; }
?>

@ -8,6 +8,4 @@ class Unique extends Constraint {
parent::__construct((!empty($names) && is_array($names[0])) ? $names[0] : $names); parent::__construct((!empty($names) && is_array($names[0])) ? $names[0] : $names);
} }
}; }
?>

@ -4,13 +4,13 @@ namespace Driver\SQL;
class Join { class Join {
private $type; private string $type;
private $table; private string $table;
private $columnA; private string $columnA;
private $columnB; private string $columnB;
public function __construct($type, $table, $columnA, $columnB) { public function __construct($type, $table, $columnA, $columnB) {
$this->tpye = $type; $this->type = $type;
$this->table = $table; $this->table = $table;
$this->columnA = $columnA; $this->columnA = $columnA;
$this->columnB = $columnB; $this->columnB = $columnB;
@ -22,5 +22,3 @@ class Join {
public function getColumnB() { return $this->columnB; } public function getColumnB() { return $this->columnB; }
} }
?>

@ -4,7 +4,7 @@ namespace Driver\SQL;
class Keyword { class Keyword {
private $value; private string $value;
public function __construct($value) { public function __construct($value) {
$this->value = $value; $this->value = $value;
@ -13,5 +13,3 @@ class Keyword {
public function getValue() { return $this->value; } public function getValue() { return $this->value; }
} }
?>

@ -13,9 +13,8 @@ use \Driver\SQL\Column\DateTimeColumn;
use Driver\SQL\Column\BoolColumn; use Driver\SQL\Column\BoolColumn;
use Driver\SQL\Column\JsonColumn; use Driver\SQL\Column\JsonColumn;
use \Driver\SQL\Strategy\CascadeStrategy; use Driver\SQL\Query\Insert;
use \Driver\SQL\Strategy\SetDefaultStrategy; use Driver\SQL\Strategy\Strategy;
use \Driver\SQL\Strategy\SetNullStrategy;
use \Driver\SQL\Strategy\UpdateStrategy; use \Driver\SQL\Strategy\UpdateStrategy;
class MySQL extends SQL { class MySQL extends SQL {
@ -47,7 +46,7 @@ class MySQL extends SQL {
$this->connectionData->getPort() $this->connectionData->getPort()
); );
if (mysqli_connect_errno($this->connection)) { if (mysqli_connect_errno()) {
$this->lastError = "Failed to connect to MySQL: " . mysqli_connect_error(); $this->lastError = "Failed to connect to MySQL: " . mysqli_connect_error();
$this->connection = NULL; $this->connection = NULL;
return false; return false;
@ -63,6 +62,7 @@ class MySQL extends SQL {
} }
mysqli_close($this->connection); mysqli_close($this->connection);
return true;
} }
public function getLastError() { public function getLastError() {
@ -81,6 +81,8 @@ class MySQL extends SQL {
switch($paramType) { switch($paramType) {
case Parameter::TYPE_BOOLEAN: case Parameter::TYPE_BOOLEAN:
$value = $value ? 1 : 0; $value = $value ? 1 : 0;
$sqlParams[0] .= 'i';
break;
case Parameter::TYPE_INT: case Parameter::TYPE_INT:
$sqlParams[0] .= 'i'; $sqlParams[0] .= 'i';
break; break;
@ -161,64 +163,34 @@ class MySQL extends SQL {
return ($success && $returnValues) ? $resultRows : $success; return ($success && $returnValues) ? $resultRows : $success;
} }
public function executeInsert($insert) { protected function getOnDuplicateStrategy(?Strategy $strategy, &$params) {
$tableName = $this->tableName($insert->getTableName()); if (is_null($strategy)) {
$columns = $insert->getColumns(); return "";
$rows = $insert->getRows(); } else if ($strategy instanceof UpdateStrategy) {
$onDuplicateKey = $insert->onDuplicateKey() ?? ""; $updateValues = array();
foreach($strategy->getValues() as $key => $value) {
$leftColumn = $this->columnName($key);
if ($value instanceof Column) {
$columnName = $this->columnName($value->getName());
$updateValues[] = "$leftColumn=$columnName";
} else {
$updateValues[] = "`$leftColumn=" . $this->addValue($value, $params);
}
}
if (empty($rows)) { return " ON DUPLICATE KEY UPDATE " . implode(",", $updateValues);
$this->lastError = "No rows to insert given."; } else {
$strategyClass = get_class($strategy);
$this->lastError = "ON DUPLICATE Strategy $strategyClass is not supported yet.";
return false; return false;
} }
if (is_null($columns) || empty($columns)) {
$columns = "";
$numColumns = count($rows[0]);
} else {
$numColumns = count($columns);
$columns = " (" . $this->columnName($columns) . ")";
}
$numRows = count($rows);
$parameters = array();
$values = implode(",", array_fill(0, $numRows, "(" . implode(",", array_fill(0, $numColumns, "?")) . ")"));
foreach($rows as $row) {
$parameters = array_merge($parameters, $row);
}
if ($onDuplicateKey) {
if ($onDuplicateKey instanceof UpdateStrategy) {
$updateValues = array();
foreach($onDuplicateKey->getValues() as $key => $value) {
if ($value instanceof Column) {
$columnName = $value->getName();
$updateValues[] = "`$key`=`$columnName`";
} else {
$updateValues[] = "`$key`=" . $this->addValue($value, $parameters);
}
}
$onDuplicateKey = " ON DUPLICATE KEY UPDATE " . implode(",", $updateValues);
} else {
$strategy = get_class($onDuplicateKey);
$this->lastError = "ON DUPLICATE Strategy $strategy is not supported yet.";
return false;
}
}
$query = "INSERT INTO $tableName$columns VALUES$values$onDuplicateKey";
$success = $this->execute($query, $parameters);
if($success) {
$this->lastInsertId = mysqli_insert_id($this->connection);
}
return $success;
} }
public function getColumnDefinition($column) { protected function fetchReturning($res, string $returningCol) {
$this->lastInsertId = mysqli_insert_id($this->connection);
}
public function getColumnDefinition(Column $column) {
$columnName = $this->columnName($column->getName()); $columnName = $this->columnName($column->getName());
$defaultValue = $column->getDefaultValue(); $defaultValue = $column->getDefaultValue();
@ -323,4 +295,4 @@ class MySQL extends SQL {
return new Keyword("NOW()"); return new Keyword("NOW()");
} }
}; }

@ -16,6 +16,7 @@ use Driver\SQL\Column\JsonColumn;
use \Driver\SQL\Strategy\CascadeStrategy; use \Driver\SQL\Strategy\CascadeStrategy;
use \Driver\SQL\Strategy\SetDefaultStrategy; use \Driver\SQL\Strategy\SetDefaultStrategy;
use \Driver\SQL\Strategy\SetNullStrategy; use \Driver\SQL\Strategy\SetNullStrategy;
use Driver\SQL\Strategy\Strategy;
use \Driver\SQL\Strategy\UpdateStrategy; use \Driver\SQL\Strategy\UpdateStrategy;
class PostgreSQL extends SQL { class PostgreSQL extends SQL {
@ -131,76 +132,41 @@ class PostgreSQL extends SQL {
} }
} }
// Querybuilder protected function getOnDuplicateStrategy(?Strategy $strategy, &$params) {
public function executeInsert($insert) { if (!is_null($strategy)) {
$tableName = $this->tableName($insert->getTableName()); /*if ($onDuplicateKey instanceof UpdateStrategy) {
$columns = $insert->getColumns(); $updateValues = array();
$rows = $insert->getRows(); foreach($onDuplicateKey->getValues() as $key => $value) {
$onDuplicateKey = $insert->onDuplicateKey() ?? ""; if ($value instanceof Column) {
$columnName = $value->getName();
$updateValues[] = "\"$key\"=\"$columnName\"";
} else {
$updateValues[] = "\"$key\"=" . $this->addValue($value, $parameters);
}
}
if (empty($rows)) { $onDuplicateKey = " ON CONFLICT DO UPDATE SET " . implode(",", $updateValues);
$this->lastError = "No rows to insert given."; } else*/ {
return false; $strategyClass = get_class($strategy);
} $this->lastError = "ON DUPLICATE Strategy $strategyClass is not supported yet.";
return false;
if (is_null($columns) || empty($columns)) {
$columnStr = "";
} else {
$columnStr = " (" . $this->columnName($columns) . ")";
}
$numRows = count($rows);
$parameters = array();
$values = array();
foreach($rows as $row) {
$rowPlaceHolder = array();
foreach($row as $val) {
$rowPlaceHolder[] = $this->addValue($val, $parameters);
}
$values[] = "(" . implode(",", $rowPlaceHolder) . ")";
}
$values = implode(",", $values);
if ($onDuplicateKey) {
/*if ($onDuplicateKey instanceof UpdateStrategy) {
$updateValues = array();
foreach($onDuplicateKey->getValues() as $key => $value) {
if ($value instanceof Column) {
$columnName = $value->getName();
$updateValues[] = "\"$key\"=\"$columnName\"";
} else {
$updateValues[] = "\"$key\"=" . $this->addValue($value, $parameters);
}
} }
} else {
$onDuplicateKey = " ON CONFLICT DO UPDATE SET " . implode(",", $updateValues); return "";
} else*/ {
$strategy = get_class($onDuplicateKey);
$this->lastError = "ON DUPLICATE Strategy $strategy is not supported yet.";
return false;
} }
} }
$returningCol = $insert->getReturning(); protected function getReturning(?string $columns) {
$returning = $returningCol ? (" RETURNING " . $this->columnName($returningCol)) : ""; return $columns ? (" RETURNING " . $this->columnName($columns)) : "";
}
$query = "INSERT INTO $tableName$columnStr VALUES$values$onDuplicateKey$returning"; protected function fetchReturning($res, string $returningCol) {
$res = $this->execute($query, $parameters, !empty($returning)); $this->lastInsertId = $res[0][$returningCol];
$success = ($res !== FALSE);
if($success && !empty($returning)) {
$this->lastInsertId = $res[0][$returningCol];
}
return $success;
} }
// UGLY but.. what should i do? // UGLY but.. what should i do?
private function createEnum($enumColumn) { private function createEnum(EnumColumn $enumColumn) {
$typeName = $enumColumn->getName(); $typeName = $enumColumn->getName();
if(!endsWith($typeName, "_type")) { if(!endsWith($typeName, "_type")) {
$typeName = "${typeName}_type"; $typeName = "${typeName}_type";
@ -320,4 +286,3 @@ class PostgreSQL extends SQL {
return new Keyword("CURRENT_TIMESTAMP"); return new Keyword("CURRENT_TIMESTAMP");
} }
} }
?>

@ -16,10 +16,10 @@ use Driver\SQL\Constraint\ForeignKey;
class CreateTable extends Query { class CreateTable extends Query {
private $tableName; private string $tableName;
private $columns; private array $columns;
private $constraints; private array $constraints;
private $ifNotExists; private bool $ifNotExists;
public function __construct($sql, $name) { public function __construct($sql, $name) {
parent::__construct($sql); parent::__construct($sql);
@ -92,6 +92,4 @@ class CreateTable extends Query {
public function getTableName() { return $this->tableName; } public function getTableName() { return $this->tableName; }
public function getColumns() { return $this->columns; } public function getColumns() { return $this->columns; }
public function getConstraints() { return $this->constraints; } public function getConstraints() { return $this->constraints; }
}; }
?>

@ -4,8 +4,8 @@ namespace Driver\SQL\Query;
class Delete extends Query { class Delete extends Query {
private $table; private string $table;
private $conditions; private array $conditions;
public function __construct($sql, $table) { public function __construct($sql, $table) {
parent::__construct($sql); parent::__construct($sql);
@ -24,6 +24,4 @@ class Delete extends Query {
public function getTable() { return $this->table; } public function getTable() { return $this->table; }
public function getConditions() { return $this->conditions; } public function getConditions() { return $this->conditions; }
}; }
?>

@ -2,13 +2,15 @@
namespace Driver\SQL\Query; namespace Driver\SQL\Query;
use Driver\SQL\Strategy\Strategy;
class Insert extends Query { class Insert extends Query {
private $tableName; private string $tableName;
private $columns; private array $columns;
private $rows; private array $rows;
private $onDuplicateKey; private ?Strategy $onDuplicateKey;
private $returning; private ?string $returning;
public function __construct($sql, $name, $columns=array()) { public function __construct($sql, $name, $columns=array()) {
parent::__construct($sql); parent::__construct($sql);
@ -43,6 +45,4 @@ class Insert extends Query {
public function getRows() { return $this->rows; } public function getRows() { return $this->rows; }
public function onDuplicateKey() { return $this->onDuplicateKey; } public function onDuplicateKey() { return $this->onDuplicateKey; }
public function getReturning() { return $this->returning; } public function getReturning() { return $this->returning; }
}; }
?>

@ -2,9 +2,11 @@
namespace Driver\SQL\Query; namespace Driver\SQL\Query;
use Driver\SQL\SQL;
abstract class Query { abstract class Query {
protected $sql; protected SQL $sql;
public function __construct($sql) { public function __construct($sql) {
$this->sql = $sql; $this->sql = $sql;
@ -12,6 +14,4 @@ abstract class Query {
public abstract function execute(); public abstract function execute();
}; }
?>

@ -2,16 +2,18 @@
namespace Driver\SQL\Query; namespace Driver\SQL\Query;
use Driver\SQL\Join;
class Select extends Query { class Select extends Query {
private $columns; private array $columns;
private $tables; private array $tables;
private $conditions; private array $conditions;
private $joins; private array $joins;
private $orderColumns; private array $orderColumns;
private $sortAscending; private bool $sortAscending;
private $limit; private int $limit;
private $offset; private int $offset;
public function __construct($sql, ...$columns) { public function __construct($sql, ...$columns) {
parent::__construct($sql); parent::__construct($sql);
@ -36,12 +38,12 @@ class Select extends Query {
} }
public function innerJoin($table, $columnA, $columnB) { public function innerJoin($table, $columnA, $columnB) {
$this->joins[] = new \Driver\SQL\Join("INNER", $table, $columnA, $columnB); $this->joins[] = new Join("INNER", $table, $columnA, $columnB);
return $this; return $this;
} }
public function leftJoin($table, $columnA, $columnB) { public function leftJoin($table, $columnA, $columnB) {
$this->joins[] = new \Driver\SQL\Join("LEFT", $table, $columnA, $columnB); $this->joins[] = new Join("LEFT", $table, $columnA, $columnB);
return $this; return $this;
} }
@ -51,12 +53,12 @@ class Select extends Query {
} }
public function ascending() { public function ascending() {
$this->ascending = true; $this->sortAscending = true;
return $this; return $this;
} }
public function descending() { public function descending() {
$this->ascending = false; $this->sortAscending = false;
return $this; return $this;
} }
@ -78,11 +80,9 @@ class Select extends Query {
public function getTables() { return $this->tables; } public function getTables() { return $this->tables; }
public function getConditions() { return $this->conditions; } public function getConditions() { return $this->conditions; }
public function getJoins() { return $this->joins; } public function getJoins() { return $this->joins; }
public function isOrderedAscending() { return $this->ascending; } public function isOrderedAscending() { return $this->sortAscending; }
public function getOrderBy() { return $this->orderColumns; } public function getOrderBy() { return $this->orderColumns; }
public function getLimit() { return $this->limit; } public function getLimit() { return $this->limit; }
public function getOffset() { return $this->offset; } public function getOffset() { return $this->offset; }
}; }
?>

@ -4,7 +4,7 @@ namespace Driver\SQL\Query;
class Truncate extends Query { class Truncate extends Query {
private $tableName; private string $tableName;
public function __construct($sql, $name) { public function __construct($sql, $name) {
parent::__construct($sql); parent::__construct($sql);
@ -15,7 +15,5 @@ class Truncate extends Query {
return $this->sql->executeTruncate($this); return $this->sql->executeTruncate($this);
} }
public function getTableName() { return $this->tableName; } public function getTable() { return $this->tableName; }
}; }
?>

@ -4,9 +4,9 @@ namespace Driver\SQL\Query;
class Update extends Query { class Update extends Query {
private $values; private array $values;
private $table; private string $table;
private $conditions; private array $conditions;
public function __construct($sql, $table) { public function __construct($sql, $table) {
parent::__construct($sql); parent::__construct($sql);
@ -32,6 +32,4 @@ class Update extends Query {
public function getTable() { return $this->table; } public function getTable() { return $this->table; }
public function getConditions() { return $this->conditions; } public function getConditions() { return $this->conditions; }
public function getValues() { return $this->values; } public function getValues() { return $this->values; }
}; }
?>

@ -2,16 +2,32 @@
namespace Driver\SQL; namespace Driver\SQL;
use Driver\SQL\Column\Column;
use Driver\SQL\Condition\Compare;
use Driver\SQL\Condition\CondBool;
use Driver\SQL\Condition\CondOr;
use Driver\SQL\Constraint\Constraint;
use \Driver\SQL\Constraint\Unique; use \Driver\SQL\Constraint\Unique;
use \Driver\SQL\Constraint\PrimaryKey; use \Driver\SQL\Constraint\PrimaryKey;
use \Driver\SQL\Constraint\ForeignKey; use \Driver\SQL\Constraint\ForeignKey;
use Driver\SQL\Query\CreateTable;
use Driver\SQL\Query\Delete;
use Driver\SQL\Query\Insert;
use Driver\SQL\Query\Select;
use Driver\SQL\Query\Truncate;
use Driver\SQL\Query\Update;
use Driver\SQL\Strategy\CascadeStrategy;
use Driver\SQL\Strategy\SetDefaultStrategy;
use Driver\SQL\Strategy\SetNullStrategy;
use Driver\SQL\Strategy\Strategy;
use Objects\ConnectionData;
abstract class SQL { abstract class SQL {
protected $lastError; protected string $lastError;
protected $connection; protected $connection;
protected $connectionData; protected ConnectionData $connectionData;
protected $lastInsertId; protected int $lastInsertId;
public function __construct($connectionData) { public function __construct($connectionData) {
$this->connection = NULL; $this->connection = NULL;
@ -65,7 +81,7 @@ abstract class SQL {
public abstract function disconnect(); public abstract function disconnect();
// Querybuilder // Querybuilder
public function executeCreateTable($createTable) { public function executeCreateTable(CreateTable $createTable) {
$tableName = $this->tableName($createTable->getTableName()); $tableName = $this->tableName($createTable->getTableName());
$ifNotExists = $createTable->ifNotExists() ? " IF NOT EXISTS": ""; $ifNotExists = $createTable->ifNotExists() ? " IF NOT EXISTS": "";
@ -89,10 +105,56 @@ abstract class SQL {
return $this->execute($query); return $this->execute($query);
} }
// TODO pull this function up public function executeInsert(Insert $insert) {
public abstract function executeInsert($query);
public function executeSelect($select) { $tableName = $this->tableName($insert->getTableName());
$columns = $insert->getColumns();
$rows = $insert->getRows();
if (empty($rows)) {
$this->lastError = "No rows to insert given.";
return false;
}
if (is_null($columns) || empty($columns)) {
$columnStr = "";
} else {
$columnStr = " (" . $this->columnName($columns) . ")";
}
$parameters = array();
$values = array();
foreach($rows as $row) {
$rowPlaceHolder = array();
foreach($row as $val) {
$rowPlaceHolder[] = $this->addValue($val, $parameters);
}
$values[] = "(" . implode(",", $rowPlaceHolder) . ")";
}
$values = implode(",", $values);
$onDuplicateKey = $this->getOnDuplicateStrategy($insert->onDuplicateKey(), $parameters);
if ($onDuplicateKey === FALSE) {
return false;
}
$returningCol = $insert->getReturning();
$returning = $this->getReturning($returningCol);
$query = "INSERT INTO $tableName$columnStr VALUES$values$onDuplicateKey$returning";
$res = $this->execute($query, $parameters, !empty($returning));
$success = ($res !== FALSE);
if($success && $returningCol) {
$this->fetchReturning($res, $returningCol);
}
return $success;
}
public function executeSelect(Select $select) {
$columns = $this->columnName($select->getColumns()); $columns = $this->columnName($select->getColumns());
$tables = $select->getTables(); $tables = $select->getTables();
@ -130,7 +192,7 @@ abstract class SQL {
return $this->execute($query, $params, true); return $this->execute($query, $params, true);
} }
public function executeDelete($delete) { public function executeDelete(Delete $delete) {
$table = $this->tableName($delete->getTable()); $table = $this->tableName($delete->getTable());
$where = $this->getWhereClause($delete->getConditions(), $params); $where = $this->getWhereClause($delete->getConditions(), $params);
@ -139,11 +201,11 @@ abstract class SQL {
return $this->execute($query); return $this->execute($query);
} }
public function executeTruncate($truncate) { public function executeTruncate(Truncate $truncate) {
return $this->execute("TRUNCATE " . $truncate->getTable()); return $this->execute("TRUNCATE " . $truncate->getTable());
} }
public function executeUpdate($update) { public function executeUpdate(Update $update) {
$params = array(); $params = array();
$table = $this->tableName($update->getTable()); $table = $this->tableName($update->getTable());
@ -167,10 +229,8 @@ abstract class SQL {
} }
} }
protected abstract function getColumnDefinition($column); public function getConstraintDefinition(Constraint $constraint) {
$columnName = $this->columnName($constraint->getColumnNames());
public function getConstraintDefinition($constraint) {
$columnName = $this->columnName($constraint->getColumnName());
if ($constraint instanceof PrimaryKey) { if ($constraint instanceof PrimaryKey) {
return "PRIMARY KEY ($columnName)"; return "PRIMARY KEY ($columnName)";
} else if ($constraint instanceof Unique) { } else if ($constraint instanceof Unique) {
@ -190,10 +250,19 @@ abstract class SQL {
return $code; return $code;
} else { } else {
$this->lastError = "Unsupported constraint type: " . get_class($strategy); $this->lastError = "Unsupported constraint type: " . get_class($constraint);
return false;
} }
} }
protected function getReturning(?string $columns) {
return "";
}
protected abstract function getColumnDefinition(Column $column);
protected abstract function fetchReturning($res, string $returningCol);
protected abstract function getOnDuplicateStrategy(?Strategy $strategy, &$params);
protected abstract function getValueDefinition($val); protected abstract function getValueDefinition($val);
protected abstract function addValue($val, &$params); protected abstract function addValue($val, &$params);
@ -221,18 +290,18 @@ abstract class SQL {
protected abstract function execute($query, $values=NULL, $returnValues=false); protected abstract function execute($query, $values=NULL, $returnValues=false);
protected function buildCondition($condition, &$params) { protected function buildCondition($condition, &$params) {
if ($condition instanceof \Driver\SQL\Condition\CondOr) { if ($condition instanceof CondOr) {
$conditions = array(); $conditions = array();
foreach($condition->getConditions() as $cond) { foreach($condition->getConditions() as $cond) {
$conditions[] = $this->buildCondition($cond, $params); $conditions[] = $this->buildCondition($cond, $params);
} }
return "(" . implode(" OR ", $conditions) . ")"; return "(" . implode(" OR ", $conditions) . ")";
} else if ($condition instanceof \Driver\SQL\Condition\Compare) { } else if ($condition instanceof Compare) {
$column = $this->columnName($condition->getColumn()); $column = $this->columnName($condition->getColumn());
$value = $condition->getValue(); $value = $condition->getValue();
$operator = $condition->getOperator(); $operator = $condition->getOperator();
return $column . $operator . $this->addValue($value, $params); return $column . $operator . $this->addValue($value, $params);
} else if ($condition instanceof \Driver\SQL\Condition\CondBool) { } else if ($condition instanceof CondBool) {
return $this->columnName($condition->getValue()); return $this->columnName($condition->getValue());
} else if (is_array($condition)) { } else if (is_array($condition)) {
if (count($condition) == 1) { if (count($condition) == 1) {
@ -244,6 +313,9 @@ abstract class SQL {
} }
return implode(" AND ", $conditions); return implode(" AND ", $conditions);
} }
} else {
$this->lastError = "Unsupported condition type: " . get_class($condition);
return false;
} }
} }
@ -260,7 +332,7 @@ abstract class SQL {
$this->connection = NULL; $this->connection = NULL;
} }
public static function createConnection($connectionData) { public static function createConnection(ConnectionData $connectionData) {
$type = $connectionData->getProperty("type"); $type = $connectionData->getProperty("type");
if ($type === "mysql") { if ($type === "mysql") {
$sql = new MySQL($connectionData); $sql = new MySQL($connectionData);
@ -280,5 +352,3 @@ abstract class SQL {
return $sql; return $sql;
} }
} }
?>

@ -7,6 +7,4 @@ class CascadeStrategy extends Strategy {
public function __construct() { public function __construct() {
} }
}; }
?>

@ -7,6 +7,4 @@ class SetDefaultStrategy extends Strategy {
public function __construct() { public function __construct() {
} }
}; }
?>

@ -7,6 +7,4 @@ class SetNullStrategy extends Strategy {
public function __construct() { public function __construct() {
} }
}; }
?>

@ -4,6 +4,4 @@ namespace Driver\SQL\Strategy;
abstract class Strategy { abstract class Strategy {
}; }
?>

@ -4,13 +4,11 @@ namespace Driver\SQL\Strategy;
class UpdateStrategy extends Strategy { class UpdateStrategy extends Strategy {
private $values; private array $values;
public function __construct($values) { public function __construct($values) {
$this->values = $values; $this->values = $values;
} }
public function getValues() { return $this->values; } public function getValues() { return $this->values; }
}; }
?>

@ -34,18 +34,16 @@ class Script extends \View {
private string $type; private string $type;
private string $content; private string $content;
private string $url; private string $src;
function __construct($type, $src, $content = "") { function __construct($type, $src, $content = "") {
$this->src = $src;
$this->type = $type; $this->type = $type;
$this->content = $content; $this->content = $content;
} }
function getCode() { function getCode() {
$src = (empty($this->url) ? "" : " src=\"$this->url\""); $src = (empty($this->src) ? "" : " src=\"$this->src\"");
$script = "<script type=\"$this->type\"$src>"; return "<script type=\"$this->type\"$src>$this->content</script>";
$script .= $this->content;
$script .= '</script>';
return $script;
} }
} }

File diff suppressed because it is too large Load Diff