Some fixes / improvements
This commit is contained in:
parent
a8fc52b42a
commit
ad604a309e
5
.idea/codeStyles/codeStyleConfig.xml
Normal file
5
.idea/codeStyles/codeStyleConfig.xml
Normal file
@ -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 DateTime;
|
||||
use \Driver\SQL\Condition\Compare;
|
||||
use Exception;
|
||||
|
||||
class Fetch extends Request {
|
||||
|
||||
@ -34,12 +35,12 @@ class Fetch extends Request {
|
||||
foreach($res as $row) {
|
||||
try {
|
||||
$validUntil = (new DateTime($row["valid_until"]))->getTimestamp();
|
||||
} catch (\Exception $e) {
|
||||
} catch (Exception $e) {
|
||||
$validUntil = $row["valid_until"];
|
||||
}
|
||||
|
||||
$this->result["api_keys"][] = array(
|
||||
"uid" => $row["uid"],
|
||||
"uid" => intval($row["uid"]),
|
||||
"api_key" => $row["api_key"],
|
||||
"valid_until" => $validUntil,
|
||||
);
|
||||
|
@ -9,5 +9,3 @@ class BoolColumn extends Column {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
|
@ -4,8 +4,8 @@ namespace Driver\SQL\Column;
|
||||
|
||||
class Column {
|
||||
|
||||
private $name;
|
||||
private $nullable;
|
||||
private string $name;
|
||||
private bool $nullable;
|
||||
private $defaultValue;
|
||||
|
||||
public function __construct($name, $nullable = false, $defaultValue = NULL) {
|
||||
@ -18,6 +18,4 @@ class Column {
|
||||
public function notNull() { return !$this->nullable; }
|
||||
public function getDefaultValue() { return $this->defaultValue; }
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
}
|
@ -7,6 +7,4 @@ class DateTimeColumn extends Column {
|
||||
public function __construct($name, $nullable=false, $defaultValue=NULL) {
|
||||
parent::__construct($name, $nullable, $defaultValue);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
}
|
@ -4,7 +4,7 @@ namespace Driver\SQL\Column;
|
||||
|
||||
class EnumColumn extends Column {
|
||||
|
||||
private $values;
|
||||
private array $values;
|
||||
|
||||
public function __construct($name, $values, $nullable=false, $defaultValue=NULL) {
|
||||
parent::__construct($name, $nullable, $defaultValue);
|
||||
@ -13,5 +13,3 @@ class EnumColumn extends Column {
|
||||
|
||||
public function getValues() { return $this->values; }
|
||||
}
|
||||
|
||||
?>
|
||||
|
@ -9,5 +9,3 @@ class IntColumn extends Column {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
|
@ -8,6 +8,4 @@ class JsonColumn extends Column {
|
||||
parent::__construct($name, $nullable, $defaultValue);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
}
|
@ -8,6 +8,4 @@ class SerialColumn extends Column {
|
||||
parent::__construct($name, false, $defaultValue); # not nullable
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
}
|
@ -4,7 +4,7 @@ namespace Driver\SQL\Column;
|
||||
|
||||
class StringColumn extends Column {
|
||||
|
||||
private $maxSize;
|
||||
private ?int $maxSize;
|
||||
|
||||
public function __construct($name, $maxSize=null, $nullable=false, $defaultValue=null) {
|
||||
parent::__construct($name, $nullable, $defaultValue);
|
||||
@ -12,6 +12,4 @@ class StringColumn extends Column {
|
||||
}
|
||||
|
||||
public function getMaxSize() { return $this->maxSize; }
|
||||
}
|
||||
|
||||
?>
|
||||
}
|
@ -4,6 +4,10 @@ namespace Driver\SQL\Condition;
|
||||
|
||||
class Compare extends Condition {
|
||||
|
||||
private string $operator;
|
||||
private string $column;
|
||||
private $value;
|
||||
|
||||
public function __construct($col, $val, $operator='=') {
|
||||
$this->operator = $operator;
|
||||
$this->column = $col;
|
||||
@ -14,6 +18,4 @@ class Compare extends Condition {
|
||||
public function getValue() { return $this->value; }
|
||||
public function getOperator() { return $this->operator; }
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
}
|
@ -4,13 +4,11 @@ namespace Driver\SQL\Condition;
|
||||
|
||||
class CondAnd extends Condition {
|
||||
|
||||
private $conditions;
|
||||
private array $conditions;
|
||||
|
||||
public function __construct(...$conditions) {
|
||||
$this->conditions = $conditions;
|
||||
}
|
||||
|
||||
public function getConditions() { return $this->conditions; }
|
||||
}
|
||||
|
||||
?>
|
||||
}
|
@ -4,12 +4,12 @@ namespace Driver\SQL\Condition;
|
||||
|
||||
class CondBool extends Condition {
|
||||
|
||||
private $value;
|
||||
|
||||
public function __construct($val) {
|
||||
$this->value = $val;
|
||||
}
|
||||
|
||||
public function getValue() { return $this->value; }
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
}
|
@ -4,13 +4,11 @@ namespace Driver\SQL\Condition;
|
||||
|
||||
class CondOr extends Condition {
|
||||
|
||||
private $conditions;
|
||||
private array $conditions;
|
||||
|
||||
public function __construct(...$conditions) {
|
||||
$this->conditions = $conditions;
|
||||
}
|
||||
|
||||
public function getConditions() { return $this->conditions; }
|
||||
}
|
||||
|
||||
?>
|
||||
}
|
@ -4,6 +4,4 @@ namespace Driver\SQL\Condition;
|
||||
|
||||
abstract class Condition {
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
}
|
@ -4,13 +4,11 @@ namespace Driver\SQL\Constraint;
|
||||
|
||||
abstract class Constraint {
|
||||
|
||||
private $columnName;
|
||||
private array $columnNames;
|
||||
|
||||
public function __construct($columnName) {
|
||||
$this->columnName = $columnName;
|
||||
public function __construct($columnNames) {
|
||||
$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;
|
||||
|
||||
use Driver\SQL\Strategy\Strategy;
|
||||
|
||||
class ForeignKey extends Constraint {
|
||||
|
||||
private $referencedTable;
|
||||
private $referencedColumn;
|
||||
private $strategy;
|
||||
private string $referencedTable;
|
||||
private string $referencedColumn;
|
||||
private ?Strategy $strategy;
|
||||
|
||||
public function __construct($name, $refTable, $refColumn, $strategy = NULL) {
|
||||
parent::__construct($name);
|
||||
@ -18,6 +20,4 @@ class ForeignKey extends Constraint {
|
||||
public function getReferencedTable() { return $this->referencedTable; }
|
||||
public function getReferencedColumn() { return $this->referencedColumn; }
|
||||
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);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
?>
|
||||
}
|
||||
|
@ -8,6 +8,4 @@ class Unique extends Constraint {
|
||||
parent::__construct((!empty($names) && is_array($names[0])) ? $names[0] : $names);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
?>
|
||||
}
|
@ -4,13 +4,13 @@ namespace Driver\SQL;
|
||||
|
||||
class Join {
|
||||
|
||||
private $type;
|
||||
private $table;
|
||||
private $columnA;
|
||||
private $columnB;
|
||||
private string $type;
|
||||
private string $table;
|
||||
private string $columnA;
|
||||
private string $columnB;
|
||||
|
||||
public function __construct($type, $table, $columnA, $columnB) {
|
||||
$this->tpye = $type;
|
||||
$this->type = $type;
|
||||
$this->table = $table;
|
||||
$this->columnA = $columnA;
|
||||
$this->columnB = $columnB;
|
||||
@ -21,6 +21,4 @@ class Join {
|
||||
public function getColumnA() { return $this->columnA; }
|
||||
public function getColumnB() { return $this->columnB; }
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
}
|
@ -4,7 +4,7 @@ namespace Driver\SQL;
|
||||
|
||||
class Keyword {
|
||||
|
||||
private $value;
|
||||
private string $value;
|
||||
|
||||
public function __construct($value) {
|
||||
$this->value = $value;
|
||||
@ -12,6 +12,4 @@ class Keyword {
|
||||
|
||||
public function getValue() { return $this->value; }
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
}
|
@ -13,9 +13,8 @@ use \Driver\SQL\Column\DateTimeColumn;
|
||||
use Driver\SQL\Column\BoolColumn;
|
||||
use Driver\SQL\Column\JsonColumn;
|
||||
|
||||
use \Driver\SQL\Strategy\CascadeStrategy;
|
||||
use \Driver\SQL\Strategy\SetDefaultStrategy;
|
||||
use \Driver\SQL\Strategy\SetNullStrategy;
|
||||
use Driver\SQL\Query\Insert;
|
||||
use Driver\SQL\Strategy\Strategy;
|
||||
use \Driver\SQL\Strategy\UpdateStrategy;
|
||||
|
||||
class MySQL extends SQL {
|
||||
@ -47,7 +46,7 @@ class MySQL extends SQL {
|
||||
$this->connectionData->getPort()
|
||||
);
|
||||
|
||||
if (mysqli_connect_errno($this->connection)) {
|
||||
if (mysqli_connect_errno()) {
|
||||
$this->lastError = "Failed to connect to MySQL: " . mysqli_connect_error();
|
||||
$this->connection = NULL;
|
||||
return false;
|
||||
@ -63,6 +62,7 @@ class MySQL extends SQL {
|
||||
}
|
||||
|
||||
mysqli_close($this->connection);
|
||||
return true;
|
||||
}
|
||||
|
||||
public function getLastError() {
|
||||
@ -81,6 +81,8 @@ class MySQL extends SQL {
|
||||
switch($paramType) {
|
||||
case Parameter::TYPE_BOOLEAN:
|
||||
$value = $value ? 1 : 0;
|
||||
$sqlParams[0] .= 'i';
|
||||
break;
|
||||
case Parameter::TYPE_INT:
|
||||
$sqlParams[0] .= 'i';
|
||||
break;
|
||||
@ -161,64 +163,34 @@ class MySQL extends SQL {
|
||||
return ($success && $returnValues) ? $resultRows : $success;
|
||||
}
|
||||
|
||||
public function executeInsert($insert) {
|
||||
$tableName = $this->tableName($insert->getTableName());
|
||||
$columns = $insert->getColumns();
|
||||
$rows = $insert->getRows();
|
||||
$onDuplicateKey = $insert->onDuplicateKey() ?? "";
|
||||
protected function getOnDuplicateStrategy(?Strategy $strategy, &$params) {
|
||||
if (is_null($strategy)) {
|
||||
return "";
|
||||
} else if ($strategy instanceof UpdateStrategy) {
|
||||
$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)) {
|
||||
$this->lastError = "No rows to insert given.";
|
||||
return " ON DUPLICATE KEY UPDATE " . implode(",", $updateValues);
|
||||
} else {
|
||||
$strategyClass = get_class($strategy);
|
||||
$this->lastError = "ON DUPLICATE Strategy $strategyClass is not supported yet.";
|
||||
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());
|
||||
$defaultValue = $column->getDefaultValue();
|
||||
|
||||
@ -323,4 +295,4 @@ class MySQL extends SQL {
|
||||
return new Keyword("NOW()");
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
|
@ -16,6 +16,7 @@ use Driver\SQL\Column\JsonColumn;
|
||||
use \Driver\SQL\Strategy\CascadeStrategy;
|
||||
use \Driver\SQL\Strategy\SetDefaultStrategy;
|
||||
use \Driver\SQL\Strategy\SetNullStrategy;
|
||||
use Driver\SQL\Strategy\Strategy;
|
||||
use \Driver\SQL\Strategy\UpdateStrategy;
|
||||
|
||||
class PostgreSQL extends SQL {
|
||||
@ -131,76 +132,41 @@ class PostgreSQL extends SQL {
|
||||
}
|
||||
}
|
||||
|
||||
// Querybuilder
|
||||
public function executeInsert($insert) {
|
||||
protected function getOnDuplicateStrategy(?Strategy $strategy, &$params) {
|
||||
if (!is_null($strategy)) {
|
||||
|
||||
$tableName = $this->tableName($insert->getTableName());
|
||||
$columns = $insert->getColumns();
|
||||
$rows = $insert->getRows();
|
||||
$onDuplicateKey = $insert->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);
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($rows)) {
|
||||
$this->lastError = "No rows to insert given.";
|
||||
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);
|
||||
}
|
||||
$onDuplicateKey = " ON CONFLICT DO UPDATE SET " . implode(",", $updateValues);
|
||||
} else*/ {
|
||||
$strategyClass = get_class($strategy);
|
||||
$this->lastError = "ON DUPLICATE Strategy $strategyClass is not supported yet.";
|
||||
return false;
|
||||
}
|
||||
|
||||
$onDuplicateKey = " ON CONFLICT DO UPDATE SET " . implode(",", $updateValues);
|
||||
} else*/ {
|
||||
$strategy = get_class($onDuplicateKey);
|
||||
$this->lastError = "ON DUPLICATE Strategy $strategy is not supported yet.";
|
||||
return false;
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$returningCol = $insert->getReturning();
|
||||
$returning = $returningCol ? (" RETURNING " . $this->columnName($returningCol)) : "";
|
||||
protected function getReturning(?string $columns) {
|
||||
return $columns ? (" RETURNING " . $this->columnName($columns)) : "";
|
||||
}
|
||||
|
||||
$query = "INSERT INTO $tableName$columnStr VALUES$values$onDuplicateKey$returning";
|
||||
$res = $this->execute($query, $parameters, !empty($returning));
|
||||
$success = ($res !== FALSE);
|
||||
|
||||
if($success && !empty($returning)) {
|
||||
$this->lastInsertId = $res[0][$returningCol];
|
||||
}
|
||||
|
||||
return $success;
|
||||
protected function fetchReturning($res, string $returningCol) {
|
||||
$this->lastInsertId = $res[0][$returningCol];
|
||||
}
|
||||
|
||||
// UGLY but.. what should i do?
|
||||
private function createEnum($enumColumn) {
|
||||
private function createEnum(EnumColumn $enumColumn) {
|
||||
$typeName = $enumColumn->getName();
|
||||
if(!endsWith($typeName, "_type")) {
|
||||
$typeName = "${typeName}_type";
|
||||
@ -319,5 +285,4 @@ class PostgreSQL extends SQL {
|
||||
public function currentTimestamp() {
|
||||
return new Keyword("CURRENT_TIMESTAMP");
|
||||
}
|
||||
}
|
||||
?>
|
||||
}
|
@ -16,10 +16,10 @@ use Driver\SQL\Constraint\ForeignKey;
|
||||
|
||||
class CreateTable extends Query {
|
||||
|
||||
private $tableName;
|
||||
private $columns;
|
||||
private $constraints;
|
||||
private $ifNotExists;
|
||||
private string $tableName;
|
||||
private array $columns;
|
||||
private array $constraints;
|
||||
private bool $ifNotExists;
|
||||
|
||||
public function __construct($sql, $name) {
|
||||
parent::__construct($sql);
|
||||
@ -92,6 +92,4 @@ class CreateTable extends Query {
|
||||
public function getTableName() { return $this->tableName; }
|
||||
public function getColumns() { return $this->columns; }
|
||||
public function getConstraints() { return $this->constraints; }
|
||||
};
|
||||
|
||||
?>
|
||||
}
|
||||
|
@ -4,8 +4,8 @@ namespace Driver\SQL\Query;
|
||||
|
||||
class Delete extends Query {
|
||||
|
||||
private $table;
|
||||
private $conditions;
|
||||
private string $table;
|
||||
private array $conditions;
|
||||
|
||||
public function __construct($sql, $table) {
|
||||
parent::__construct($sql);
|
||||
@ -24,6 +24,4 @@ class Delete extends Query {
|
||||
|
||||
public function getTable() { return $this->table; }
|
||||
public function getConditions() { return $this->conditions; }
|
||||
};
|
||||
|
||||
?>
|
||||
}
|
||||
|
@ -2,13 +2,15 @@
|
||||
|
||||
namespace Driver\SQL\Query;
|
||||
|
||||
use Driver\SQL\Strategy\Strategy;
|
||||
|
||||
class Insert extends Query {
|
||||
|
||||
private $tableName;
|
||||
private $columns;
|
||||
private $rows;
|
||||
private $onDuplicateKey;
|
||||
private $returning;
|
||||
private string $tableName;
|
||||
private array $columns;
|
||||
private array $rows;
|
||||
private ?Strategy $onDuplicateKey;
|
||||
private ?string $returning;
|
||||
|
||||
public function __construct($sql, $name, $columns=array()) {
|
||||
parent::__construct($sql);
|
||||
@ -43,6 +45,4 @@ class Insert extends Query {
|
||||
public function getRows() { return $this->rows; }
|
||||
public function onDuplicateKey() { return $this->onDuplicateKey; }
|
||||
public function getReturning() { return $this->returning; }
|
||||
};
|
||||
|
||||
?>
|
||||
}
|
@ -2,9 +2,11 @@
|
||||
|
||||
namespace Driver\SQL\Query;
|
||||
|
||||
use Driver\SQL\SQL;
|
||||
|
||||
abstract class Query {
|
||||
|
||||
protected $sql;
|
||||
protected SQL $sql;
|
||||
|
||||
public function __construct($sql) {
|
||||
$this->sql = $sql;
|
||||
@ -12,6 +14,4 @@ abstract class Query {
|
||||
|
||||
public abstract function execute();
|
||||
|
||||
};
|
||||
|
||||
?>
|
||||
}
|
@ -2,16 +2,18 @@
|
||||
|
||||
namespace Driver\SQL\Query;
|
||||
|
||||
use Driver\SQL\Join;
|
||||
|
||||
class Select extends Query {
|
||||
|
||||
private $columns;
|
||||
private $tables;
|
||||
private $conditions;
|
||||
private $joins;
|
||||
private $orderColumns;
|
||||
private $sortAscending;
|
||||
private $limit;
|
||||
private $offset;
|
||||
private array $columns;
|
||||
private array $tables;
|
||||
private array $conditions;
|
||||
private array $joins;
|
||||
private array $orderColumns;
|
||||
private bool $sortAscending;
|
||||
private int $limit;
|
||||
private int $offset;
|
||||
|
||||
public function __construct($sql, ...$columns) {
|
||||
parent::__construct($sql);
|
||||
@ -36,12 +38,12 @@ class Select extends Query {
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@ -51,12 +53,12 @@ class Select extends Query {
|
||||
}
|
||||
|
||||
public function ascending() {
|
||||
$this->ascending = true;
|
||||
$this->sortAscending = true;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function descending() {
|
||||
$this->ascending = false;
|
||||
$this->sortAscending = false;
|
||||
return $this;
|
||||
}
|
||||
|
||||
@ -78,11 +80,9 @@ class Select extends Query {
|
||||
public function getTables() { return $this->tables; }
|
||||
public function getConditions() { return $this->conditions; }
|
||||
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 getLimit() { return $this->limit; }
|
||||
public function getOffset() { return $this->offset; }
|
||||
|
||||
};
|
||||
|
||||
?>
|
||||
}
|
@ -4,7 +4,7 @@ namespace Driver\SQL\Query;
|
||||
|
||||
class Truncate extends Query {
|
||||
|
||||
private $tableName;
|
||||
private string $tableName;
|
||||
|
||||
public function __construct($sql, $name) {
|
||||
parent::__construct($sql);
|
||||
@ -15,7 +15,5 @@ class Truncate extends Query {
|
||||
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 {
|
||||
|
||||
private $values;
|
||||
private $table;
|
||||
private $conditions;
|
||||
private array $values;
|
||||
private string $table;
|
||||
private array $conditions;
|
||||
|
||||
public function __construct($sql, $table) {
|
||||
parent::__construct($sql);
|
||||
@ -32,6 +32,4 @@ class Update extends Query {
|
||||
public function getTable() { return $this->table; }
|
||||
public function getConditions() { return $this->conditions; }
|
||||
public function getValues() { return $this->values; }
|
||||
};
|
||||
|
||||
?>
|
||||
}
|
@ -2,16 +2,32 @@
|
||||
|
||||
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\PrimaryKey;
|
||||
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 {
|
||||
|
||||
protected $lastError;
|
||||
protected string $lastError;
|
||||
protected $connection;
|
||||
protected $connectionData;
|
||||
protected $lastInsertId;
|
||||
protected ConnectionData $connectionData;
|
||||
protected int $lastInsertId;
|
||||
|
||||
public function __construct($connectionData) {
|
||||
$this->connection = NULL;
|
||||
@ -65,7 +81,7 @@ abstract class SQL {
|
||||
public abstract function disconnect();
|
||||
|
||||
// Querybuilder
|
||||
public function executeCreateTable($createTable) {
|
||||
public function executeCreateTable(CreateTable $createTable) {
|
||||
$tableName = $this->tableName($createTable->getTableName());
|
||||
$ifNotExists = $createTable->ifNotExists() ? " IF NOT EXISTS": "";
|
||||
|
||||
@ -89,10 +105,56 @@ abstract class SQL {
|
||||
return $this->execute($query);
|
||||
}
|
||||
|
||||
// TODO pull this function up
|
||||
public abstract function executeInsert($query);
|
||||
public function executeInsert(Insert $insert) {
|
||||
|
||||
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());
|
||||
$tables = $select->getTables();
|
||||
@ -130,7 +192,7 @@ abstract class SQL {
|
||||
return $this->execute($query, $params, true);
|
||||
}
|
||||
|
||||
public function executeDelete($delete) {
|
||||
public function executeDelete(Delete $delete) {
|
||||
|
||||
$table = $this->tableName($delete->getTable());
|
||||
$where = $this->getWhereClause($delete->getConditions(), $params);
|
||||
@ -139,11 +201,11 @@ abstract class SQL {
|
||||
return $this->execute($query);
|
||||
}
|
||||
|
||||
public function executeTruncate($truncate) {
|
||||
public function executeTruncate(Truncate $truncate) {
|
||||
return $this->execute("TRUNCATE " . $truncate->getTable());
|
||||
}
|
||||
|
||||
public function executeUpdate($update) {
|
||||
public function executeUpdate(Update $update) {
|
||||
|
||||
$params = array();
|
||||
$table = $this->tableName($update->getTable());
|
||||
@ -167,10 +229,8 @@ abstract class SQL {
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract function getColumnDefinition($column);
|
||||
|
||||
public function getConstraintDefinition($constraint) {
|
||||
$columnName = $this->columnName($constraint->getColumnName());
|
||||
public function getConstraintDefinition(Constraint $constraint) {
|
||||
$columnName = $this->columnName($constraint->getColumnNames());
|
||||
if ($constraint instanceof PrimaryKey) {
|
||||
return "PRIMARY KEY ($columnName)";
|
||||
} else if ($constraint instanceof Unique) {
|
||||
@ -190,10 +250,19 @@ abstract class SQL {
|
||||
|
||||
return $code;
|
||||
} 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 addValue($val, &$params);
|
||||
|
||||
@ -221,18 +290,18 @@ abstract class SQL {
|
||||
protected abstract function execute($query, $values=NULL, $returnValues=false);
|
||||
|
||||
protected function buildCondition($condition, &$params) {
|
||||
if ($condition instanceof \Driver\SQL\Condition\CondOr) {
|
||||
if ($condition instanceof CondOr) {
|
||||
$conditions = array();
|
||||
foreach($condition->getConditions() as $cond) {
|
||||
$conditions[] = $this->buildCondition($cond, $params);
|
||||
}
|
||||
return "(" . implode(" OR ", $conditions) . ")";
|
||||
} else if ($condition instanceof \Driver\SQL\Condition\Compare) {
|
||||
} else if ($condition instanceof Compare) {
|
||||
$column = $this->columnName($condition->getColumn());
|
||||
$value = $condition->getValue();
|
||||
$operator = $condition->getOperator();
|
||||
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());
|
||||
} else if (is_array($condition)) {
|
||||
if (count($condition) == 1) {
|
||||
@ -244,6 +313,9 @@ abstract class SQL {
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
public static function createConnection($connectionData) {
|
||||
public static function createConnection(ConnectionData $connectionData) {
|
||||
$type = $connectionData->getProperty("type");
|
||||
if ($type === "mysql") {
|
||||
$sql = new MySQL($connectionData);
|
||||
@ -279,6 +351,4 @@ abstract class SQL {
|
||||
|
||||
return $sql;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
}
|
@ -7,6 +7,4 @@ class CascadeStrategy extends Strategy {
|
||||
public function __construct() {
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
?>
|
||||
}
|
@ -7,6 +7,4 @@ class SetDefaultStrategy extends Strategy {
|
||||
public function __construct() {
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
?>
|
||||
}
|
@ -7,6 +7,4 @@ class SetNullStrategy extends Strategy {
|
||||
public function __construct() {
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
?>
|
||||
}
|
@ -4,6 +4,4 @@ namespace Driver\SQL\Strategy;
|
||||
|
||||
abstract class Strategy {
|
||||
|
||||
};
|
||||
|
||||
?>
|
||||
}
|
@ -4,13 +4,11 @@ namespace Driver\SQL\Strategy;
|
||||
|
||||
class UpdateStrategy extends Strategy {
|
||||
|
||||
private $values;
|
||||
private array $values;
|
||||
|
||||
public function __construct($values) {
|
||||
$this->values = $values;
|
||||
}
|
||||
|
||||
public function getValues() { return $this->values; }
|
||||
};
|
||||
|
||||
?>
|
||||
}
|
@ -34,18 +34,16 @@ class Script extends \View {
|
||||
|
||||
private string $type;
|
||||
private string $content;
|
||||
private string $url;
|
||||
private string $src;
|
||||
|
||||
function __construct($type, $src, $content = "") {
|
||||
$this->src = $src;
|
||||
$this->type = $type;
|
||||
$this->content = $content;
|
||||
}
|
||||
|
||||
function getCode() {
|
||||
$src = (empty($this->url) ? "" : " src=\"$this->url\"");
|
||||
$script = "<script type=\"$this->type\"$src>";
|
||||
$script .= $this->content;
|
||||
$script .= '</script>';
|
||||
return $script;
|
||||
$src = (empty($this->src) ? "" : " src=\"$this->src\"");
|
||||
return "<script type=\"$this->type\"$src>$this->content</script>";
|
||||
}
|
||||
}
|
5702
core/External/phpQuery.php
vendored
5702
core/External/phpQuery.php
vendored
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user