Database abstraction
This commit is contained in:
@@ -1,133 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Driver;
|
||||
|
||||
class SQL {
|
||||
|
||||
public $connection;
|
||||
public $lastError;
|
||||
private $connectionData;
|
||||
|
||||
public function __construct($connectionData) {
|
||||
$this->connection = NULL;
|
||||
$this->lastError = 'Not connected';
|
||||
$this->connectionData = $connectionData;
|
||||
}
|
||||
|
||||
public function connect() {
|
||||
if(!is_null($this->connection))
|
||||
return true;
|
||||
|
||||
@$this->connection = mysqli_connect(
|
||||
$this->connectionData->getHost(),
|
||||
$this->connectionData->getLogin(),
|
||||
$this->connectionData->getPassword(),
|
||||
$this->connectionData->getProperty('database'),
|
||||
$this->connectionData->getPort()
|
||||
);
|
||||
|
||||
if (mysqli_connect_errno($this->connection)) {
|
||||
$this->lastError = "Failed to connect to MySQL: " . mysqli_connect_error();
|
||||
$this->connection = NULL;
|
||||
return false;
|
||||
}
|
||||
|
||||
mysqli_set_charset($this->connection, $this->connectionData->getProperty('encoding'));
|
||||
return true;
|
||||
}
|
||||
|
||||
public function disconnect() {
|
||||
if(is_null($this->connection))
|
||||
return;
|
||||
|
||||
mysqli_close($this->connection);
|
||||
$this->connection = NULL;
|
||||
}
|
||||
|
||||
public function isConnected() {
|
||||
return !is_null($this->connection);
|
||||
}
|
||||
|
||||
public function getLastError() {
|
||||
return empty(trim($this->lastError)) ? mysqli_error($this->connection) . " " . $this->getLastErrorNumber() : trim($this->lastError);
|
||||
}
|
||||
|
||||
public function setLastError($str) {
|
||||
$this->lastError = $str;
|
||||
}
|
||||
|
||||
public function getLastErrorNumber() {
|
||||
return mysqli_errno($this->connection);
|
||||
}
|
||||
|
||||
public function getLastInsertId() {
|
||||
return $this->connection->insert_id;
|
||||
}
|
||||
|
||||
public function close() {
|
||||
if(!is_null($this->connection)) {
|
||||
$this->connection->close();
|
||||
}
|
||||
}
|
||||
|
||||
public function getAffectedRows() {
|
||||
return $this->connection->affected_rows;
|
||||
}
|
||||
|
||||
public function execute($query) {
|
||||
if(!$this->isConnected()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!mysqli_query($this->connection, $query)) {
|
||||
$this->lastError = mysqli_error($this->connection);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function executeMulti($queries) {
|
||||
if(!$this->isConnected()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!$this->connection->multi_query($queries)) {
|
||||
$this->lastError = mysqli_error($this->connection);
|
||||
return false;
|
||||
}
|
||||
|
||||
while (($success = $this->connection->next_result())) {
|
||||
if (!$this->connection->more_results()) break;
|
||||
}
|
||||
|
||||
if(!$success) {
|
||||
$this->lastError = mysqli_error($this->connection);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function query($query) {
|
||||
if(!$this->isConnected()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$res = mysqli_query($this->connection, $query);
|
||||
if(!$res) {
|
||||
$this->lastError = mysqli_error($this->connection);
|
||||
return false;
|
||||
}
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
public static function createConnection($connectionData) {
|
||||
$sql = new SQL($connectionData);
|
||||
$sql->connect();
|
||||
return $sql;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
13
core/Driver/SQL/Column/BoolColumn.class.php
Normal file
13
core/Driver/SQL/Column/BoolColumn.class.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace Driver\SQL\Column;
|
||||
|
||||
class BoolColumn extends Column {
|
||||
|
||||
public function __construct($name, $defaultValue=false) {
|
||||
parent::__construct($name, false, $defaultValue);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
23
core/Driver/SQL/Column/Column.class.php
Normal file
23
core/Driver/SQL/Column/Column.class.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace Driver\SQL\Column;
|
||||
|
||||
class Column {
|
||||
|
||||
private $name;
|
||||
private $nullable;
|
||||
private $defaultValue;
|
||||
|
||||
public function __construct($name, $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 getDefaultValue() { return $this->defaultValue; }
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
12
core/Driver/SQL/Column/DateTimeColumn.class.php
Normal file
12
core/Driver/SQL/Column/DateTimeColumn.class.php
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace Driver\SQL\Column;
|
||||
|
||||
class DateTimeColumn extends Column {
|
||||
|
||||
public function __construct($name, $nullable=false, $defaultValue=NULL) {
|
||||
parent::__construct($name, $nullable, $defaultValue);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
17
core/Driver/SQL/Column/EnumColumn.class.php
Normal file
17
core/Driver/SQL/Column/EnumColumn.class.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace Driver\SQL\Column;
|
||||
|
||||
class EnumColumn extends Column {
|
||||
|
||||
private $values;
|
||||
|
||||
public function __construct($name, $values, $nullable=false, $defaultValue=NULL) {
|
||||
parent::__construct($name, $nullable, $defaultValue);
|
||||
$this->values = $values;
|
||||
}
|
||||
|
||||
public function getValues() { return $this->values; }
|
||||
}
|
||||
|
||||
?>
|
||||
13
core/Driver/SQL/Column/IntColumn.class.php
Normal file
13
core/Driver/SQL/Column/IntColumn.class.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace Driver\SQL\Column;
|
||||
|
||||
class IntColumn extends Column {
|
||||
|
||||
public function __construct($name, $nullable=false, $defaultValue=NULL) {
|
||||
parent::__construct($name, $nullable, $defaultValue);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
13
core/Driver/SQL/Column/JsonColumn.class.php
Normal file
13
core/Driver/SQL/Column/JsonColumn.class.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace Driver\SQL\Column;
|
||||
|
||||
class JsonColumn extends Column {
|
||||
|
||||
public function __construct($name, $nullable=false, $defaultValue=null) {
|
||||
parent::__construct($name, $nullable, $defaultValue);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
13
core/Driver/SQL/Column/SerialColumn.class.php
Normal file
13
core/Driver/SQL/Column/SerialColumn.class.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace Driver\SQL\Column;
|
||||
|
||||
class SerialColumn extends Column {
|
||||
|
||||
public function __construct($name, $defaultValue=NULL) {
|
||||
parent::__construct($name, false, $defaultValue); # not nullable
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
17
core/Driver/SQL/Column/StringColumn.class.php
Normal file
17
core/Driver/SQL/Column/StringColumn.class.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace Driver\SQL\Column;
|
||||
|
||||
class StringColumn extends Column {
|
||||
|
||||
private $maxSize;
|
||||
|
||||
public function __construct($name, $maxSize=null, $nullable=false, $defaultValue=null) {
|
||||
parent::__construct($name, $nullable, $defaultValue);
|
||||
$this->maxSize = $maxSize;
|
||||
}
|
||||
|
||||
public function getMaxSize() { return $this->maxSize; }
|
||||
}
|
||||
|
||||
?>
|
||||
19
core/Driver/SQL/Condition/Compare.class.php
Normal file
19
core/Driver/SQL/Condition/Compare.class.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace Driver\SQL\Condition;
|
||||
|
||||
class Compare extends Condition {
|
||||
|
||||
public function __construct($col, $val, $operator='=') {
|
||||
$this->operator = $operator;
|
||||
$this->column = $col;
|
||||
$this->value = $val;
|
||||
}
|
||||
|
||||
public function getColumn() { return $this->column; }
|
||||
public function getValue() { return $this->value; }
|
||||
public function getOperator() { return $this->operator; }
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
16
core/Driver/SQL/Condition/CondAnd.class.php
Normal file
16
core/Driver/SQL/Condition/CondAnd.class.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace Driver\SQL\Condition;
|
||||
|
||||
class CondAnd extends Condition {
|
||||
|
||||
private $conditions;
|
||||
|
||||
public function __construct(...$conditions) {
|
||||
$this->conditions = $conditions;
|
||||
}
|
||||
|
||||
public function getConditions() { return $this->conditions; }
|
||||
}
|
||||
|
||||
?>
|
||||
15
core/Driver/SQL/Condition/CondBool.class.php
Normal file
15
core/Driver/SQL/Condition/CondBool.class.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace Driver\SQL\Condition;
|
||||
|
||||
class CondBool extends Condition {
|
||||
|
||||
public function __construct($val) {
|
||||
$this->value = $val;
|
||||
}
|
||||
|
||||
public function getValue() { return $this->value; }
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
16
core/Driver/SQL/Condition/CondOr.class.php
Normal file
16
core/Driver/SQL/Condition/CondOr.class.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace Driver\SQL\Condition;
|
||||
|
||||
class CondOr extends Condition {
|
||||
|
||||
private $conditions;
|
||||
|
||||
public function __construct(...$conditions) {
|
||||
$this->conditions = $conditions;
|
||||
}
|
||||
|
||||
public function getConditions() { return $this->conditions; }
|
||||
}
|
||||
|
||||
?>
|
||||
9
core/Driver/SQL/Condition/Condition.class.php
Normal file
9
core/Driver/SQL/Condition/Condition.class.php
Normal file
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace Driver\SQL\Condition;
|
||||
|
||||
abstract class Condition {
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
16
core/Driver/SQL/Constraint/Constraint.class.php
Normal file
16
core/Driver/SQL/Constraint/Constraint.class.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace Driver\SQL\Constraint;
|
||||
|
||||
abstract class Constraint {
|
||||
|
||||
private $columnName;
|
||||
|
||||
public function __construct($columnName) {
|
||||
$this->columnName = $columnName;
|
||||
}
|
||||
|
||||
public function getColumnName() { return $this->columnName; }
|
||||
};
|
||||
|
||||
?>
|
||||
23
core/Driver/SQL/Constraint/ForeignKey.class.php
Normal file
23
core/Driver/SQL/Constraint/ForeignKey.class.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace Driver\SQL\Constraint;
|
||||
|
||||
class ForeignKey extends Constraint {
|
||||
|
||||
private $referencedTable;
|
||||
private $referencedColumn;
|
||||
private $strategy;
|
||||
|
||||
public function __construct($name, $refTable, $refColumn, $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; }
|
||||
};
|
||||
|
||||
?>
|
||||
13
core/Driver/SQL/Constraint/PrimaryKey.class.php
Normal file
13
core/Driver/SQL/Constraint/PrimaryKey.class.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace Driver\SQL\Constraint;
|
||||
|
||||
class PrimaryKey extends Constraint {
|
||||
|
||||
public function __construct(...$names) {
|
||||
parent::__construct((!empty($names) && is_array($names[0])) ? $names[0] : $names);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
?>
|
||||
13
core/Driver/SQL/Constraint/Unique.class.php
Normal file
13
core/Driver/SQL/Constraint/Unique.class.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace Driver\SQL\Constraint;
|
||||
|
||||
class Unique extends Constraint {
|
||||
|
||||
public function __construct(...$names) {
|
||||
parent::__construct((!empty($names) && is_array($names[0])) ? $names[0] : $names);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
?>
|
||||
26
core/Driver/SQL/Join.class.php
Normal file
26
core/Driver/SQL/Join.class.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace Driver\SQL;
|
||||
|
||||
class Join {
|
||||
|
||||
private $type;
|
||||
private $table;
|
||||
private $columnA;
|
||||
private $columnB;
|
||||
|
||||
public function __construct($type, $table, $columnA, $columnB) {
|
||||
$this->tpye = $type;
|
||||
$this->table = $table;
|
||||
$this->columnA = $columnA;
|
||||
$this->columnB = $columnB;
|
||||
}
|
||||
|
||||
public function getType() { return $this->type; }
|
||||
public function getTable() { return $this->table; }
|
||||
public function getColumnA() { return $this->columnA; }
|
||||
public function getColumnB() { return $this->columnB; }
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
17
core/Driver/SQL/Keyword.class.php
Normal file
17
core/Driver/SQL/Keyword.class.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace Driver\SQL;
|
||||
|
||||
class Keyword {
|
||||
|
||||
private $value;
|
||||
|
||||
public function __construct($value) {
|
||||
$this->value = $value;
|
||||
}
|
||||
|
||||
public function getValue() { return $this->value; }
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
430
core/Driver/SQL/MySQL.class.php
Normal file
430
core/Driver/SQL/MySQL.class.php
Normal file
@@ -0,0 +1,430 @@
|
||||
<?php
|
||||
|
||||
namespace Driver\SQL;
|
||||
|
||||
use \Api\Parameter\Parameter;
|
||||
|
||||
use \Driver\SQL\Column\Column;
|
||||
use \Driver\SQL\Column\IntColumn;
|
||||
use \Driver\SQL\Column\SerialColumn;
|
||||
use \Driver\SQL\Column\StringColumn;
|
||||
use \Driver\SQL\Column\EnumColumn;
|
||||
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\Strategy\UpdateStrategy;
|
||||
|
||||
use \Driver\SQL\Constraint\Unique;
|
||||
use \Driver\SQL\Constraint\PrimaryKey;
|
||||
use \Driver\SQL\Constraint\ForeignKey;
|
||||
|
||||
class MySQL extends SQL {
|
||||
|
||||
public function __construct($connectionData) {
|
||||
parent::__construct("mysql", $connectionData);
|
||||
}
|
||||
|
||||
public function connect() {
|
||||
|
||||
if(!is_null($this->connection)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@$this->connection = mysqli_connect(
|
||||
$this->connectionData->getHost(),
|
||||
$this->connectionData->getLogin(),
|
||||
$this->connectionData->getPassword(),
|
||||
$this->connectionData->getProperty('database'),
|
||||
$this->connectionData->getPort()
|
||||
);
|
||||
|
||||
if (mysqli_connect_errno($this->connection)) {
|
||||
$this->lastError = "Failed to connect to MySQL: " . mysqli_connect_error();
|
||||
$this->connection = NULL;
|
||||
return false;
|
||||
}
|
||||
|
||||
mysqli_set_charset($this->connection, $this->connectionData->getProperty('encoding'));
|
||||
return true;
|
||||
}
|
||||
|
||||
public function disconnect() {
|
||||
if(is_null($this->connection)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
mysqli_close($this->connection);
|
||||
$this->connection = NULL;
|
||||
}
|
||||
|
||||
public function getLastError() {
|
||||
$lastError = parent::getLastError();
|
||||
if (empty($lastError)) {
|
||||
$lastError = mysqli_error($this->connection);
|
||||
}
|
||||
|
||||
return $lastError;
|
||||
}
|
||||
|
||||
private function getPreparedParams($values) {
|
||||
$sqlParams = array('');
|
||||
foreach($values as $value) {
|
||||
$paramType = Parameter::parseType($value);
|
||||
switch($paramType) {
|
||||
case Parameter::TYPE_BOOLEAN:
|
||||
$value = $value ? 1 : 0;
|
||||
case Parameter::TYPE_INT:
|
||||
$sqlParams[0] .= 'i';
|
||||
break;
|
||||
case Parameter::TYPE_FLOAT:
|
||||
$sqlParams[0] .= 'd';
|
||||
break;
|
||||
case Parameter::TYPE_DATE:
|
||||
$value = $value->format('Y-m-d');
|
||||
$sqlParams[0] .= 's';
|
||||
break;
|
||||
case Parameter::TYPE_TIME:
|
||||
$value = $value->format('H:i:s');
|
||||
$sqlParams[0] .= 's';
|
||||
break;
|
||||
case Parameter::TYPE_DATE_TIME:
|
||||
$value = $value->format('Y-m-d H:i:s');
|
||||
$sqlParams[0] .= 's';
|
||||
break;
|
||||
case Parameter::TYPE_EMAIL:
|
||||
default:
|
||||
$sqlParams[0] .= 's';
|
||||
}
|
||||
|
||||
$sqlParams[] = $value;
|
||||
}
|
||||
|
||||
return $sqlParams;
|
||||
}
|
||||
|
||||
protected function execute($query, $values = NULL, $returnValues = false) {
|
||||
|
||||
$resultRows = array();
|
||||
$this->lastError = "";
|
||||
|
||||
if (is_null($values) || empty($values)) {
|
||||
$res = mysqli_query($this->connection, $query);
|
||||
$success = $res !== FALSE;
|
||||
if ($success && $returnValues) {
|
||||
while($row = $res->fetch_assoc()) {
|
||||
$resultRows[] = $row;
|
||||
}
|
||||
$res->close();
|
||||
}
|
||||
} else if($stmt = $this->connection->prepare($query)) {
|
||||
|
||||
$success = false;
|
||||
$sqlParams = $this->getPreparedParams($values);
|
||||
$tmp = array();
|
||||
foreach($sqlParams as $key => $value) $tmp[$key] = &$sqlParams[$key];
|
||||
if(call_user_func_array(array($stmt, "bind_param"), $tmp)) {
|
||||
if($stmt->execute()) {
|
||||
if ($returnValues) {
|
||||
$res = $stmt->get_result();
|
||||
if($res) {
|
||||
while($row = $res->fetch_assoc()) {
|
||||
$resultRows[] = $row;
|
||||
}
|
||||
$res->close();
|
||||
$success = true;
|
||||
} else {
|
||||
$this->lastError = "PreparedStatement::get_result failed: $stmt->error ($stmt->errno)";
|
||||
}
|
||||
} else {
|
||||
$success = true;
|
||||
}
|
||||
} else {
|
||||
$this->lastError = "PreparedStatement::execute failed: $stmt->error ($stmt->errno)";
|
||||
}
|
||||
} else {
|
||||
$this->lastError = "PreparedStatement::prepare failed: $stmt->error ($stmt->errno)";
|
||||
}
|
||||
|
||||
$stmt->close();
|
||||
} else {
|
||||
$success = false;
|
||||
}
|
||||
|
||||
return ($success && $returnValues) ? $resultRows : $success;
|
||||
}
|
||||
|
||||
public function executeCreateTable($createTable) {
|
||||
$tableName = $createTable->getTableName();
|
||||
$ifNotExists = $createTable->ifNotExists() ? " IF NOT EXISTS": "";
|
||||
|
||||
$entries = array();
|
||||
foreach($createTable->getColumns() as $column) {
|
||||
$entries[] = ($tmp = $this->getColumnDefinition($column));
|
||||
if (is_null($tmp)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
foreach($createTable->getConstraints() as $constraint) {
|
||||
$entries[] = ($tmp = $this->getConstraintDefinition($constraint));
|
||||
if (is_null($tmp)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
$entries = implode(",", $entries);
|
||||
$query = "CREATE TABLE$ifNotExists `$tableName` ($entries)";
|
||||
return $this->execute($query);
|
||||
}
|
||||
|
||||
public function executeInsert($insert) {
|
||||
$tableName = $insert->getTableName();
|
||||
$columns = $insert->getColumns();
|
||||
$rows = $insert->getRows();
|
||||
$onDuplicateKey = $insert->onDuplicateKey() ?? "";
|
||||
|
||||
if (empty($rows)) {
|
||||
$this->lastError = "No rows to insert given.";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (is_null($columns) || empty($columns)) {
|
||||
$columns = "";
|
||||
$numColumns = count($rows[0]);
|
||||
} else {
|
||||
$numColumns = count($columns);
|
||||
$columns = " (`" . implode("`, `", $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`=?";
|
||||
$parameters[] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
$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 executeSelect($select) {
|
||||
|
||||
$columns = implode(",", $select->getColumns());
|
||||
$tables = $select->getTables();
|
||||
$params = array();
|
||||
|
||||
if (is_null($tables) || empty($tables)) {
|
||||
return "SELECT $columns";
|
||||
} else {
|
||||
$tables = implode(",", $tables);
|
||||
}
|
||||
|
||||
$conditions = $select->getConditions();
|
||||
if (!empty($conditions)) {
|
||||
$condition = " WHERE " . $this->buildCondition($conditions, $params);
|
||||
} else {
|
||||
$condition = "";
|
||||
}
|
||||
|
||||
$joinStr = "";
|
||||
$joins = $select->getJoins();
|
||||
if (!empty($joins)) {
|
||||
$joinStr = "";
|
||||
foreach($joins as $join) {
|
||||
$type = $join->getType();
|
||||
$joinTable = $join->getTable();
|
||||
$columnA = $join->getColumnA();
|
||||
$columnB = $join->getColumnB();
|
||||
$joinStr .= " $type JOIN $joinTable ON $columnA=$columnB";
|
||||
}
|
||||
}
|
||||
|
||||
$orderBy = "";
|
||||
$limit = "";
|
||||
$offset = "";
|
||||
|
||||
$query = "SELECT $columns FROM $tables$joinStr$condition$orderBy$limit$offset";
|
||||
return $this->execute($query, $params, true);
|
||||
}
|
||||
|
||||
public function executeDelete($delete) {
|
||||
|
||||
$table = $delete->getTable();
|
||||
$conditions = $delete->getConditions();
|
||||
if (!empty($conditions)) {
|
||||
$condition = " WHERE " . $this->buildCondition($conditions, $params);
|
||||
} else {
|
||||
$condition = "";
|
||||
}
|
||||
|
||||
$query = "DELETE FROM $table$condition";
|
||||
return $this->execute($query);
|
||||
}
|
||||
|
||||
public function executeTruncate($truncate) {
|
||||
return $this->execute("TRUNCATE " . $truncate->getTable());
|
||||
}
|
||||
|
||||
public function executeUpdate($update) {
|
||||
|
||||
$params = array();
|
||||
$table = $update->getTable();
|
||||
|
||||
$valueStr = array();
|
||||
foreach($update->getValues() as $key => $val) {
|
||||
$valueStr[] = "$key=" . $this->addValue($val, $params);
|
||||
}
|
||||
$valueStr = implode(",", $valueStr);
|
||||
|
||||
$conditions = $update->getConditions();
|
||||
if (!empty($conditions)) {
|
||||
$condition = " WHERE " . $this->buildCondition($conditions, $params);
|
||||
} else {
|
||||
$condition = "";
|
||||
}
|
||||
|
||||
$query = "UPDATE $table SET $valueStr$condition";
|
||||
return $this->execute($query, $params);
|
||||
}
|
||||
|
||||
protected function buildCondition($condition, &$params) {
|
||||
if ($condition instanceof \Driver\SQL\Condition\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) {
|
||||
$column = $condition->getColumn();
|
||||
$value = $condition->getValue();
|
||||
$operator = $condition->getOperator();
|
||||
return $column . $operator . $this->addValue($value, $params);
|
||||
} else if ($condition instanceof \Driver\SQL\Condition\CondBool) {
|
||||
return $condition->getValue();
|
||||
} else if (is_array($condition)) {
|
||||
if (count($condition) == 1) {
|
||||
return $this->buildCondition($condition[0], $params);
|
||||
} else {
|
||||
$conditions = array();
|
||||
foreach($condition as $cond) {
|
||||
$conditions[] = $this->buildCondition($cond, $params);
|
||||
}
|
||||
return implode(" AND ", $conditions);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function getColumnDefinition($column) {
|
||||
$columnName = $column->getName();
|
||||
|
||||
if ($column instanceof StringColumn) {
|
||||
$maxSize = $column->getMaxSize();
|
||||
if ($maxSize) {
|
||||
$type = "VARCHAR($maxSize)";
|
||||
} else {
|
||||
$type = "TEXT";
|
||||
}
|
||||
} else if($column instanceof SerialColumn) {
|
||||
$type = "INTEGER AUTO_INCREMENT";
|
||||
} else if($column instanceof IntColumn) {
|
||||
$type = "INTEGER";
|
||||
} else if($column instanceof DateTimeColumn) {
|
||||
$type = "DATETIME";
|
||||
} else if($column instanceof EnumColumn) {
|
||||
$values = array();
|
||||
foreach($column->getValues() as $value) {
|
||||
$values[] = $this->getValueDefinition($value);
|
||||
}
|
||||
|
||||
$values = implode(",", $values);
|
||||
$type = "ENUM($values)";
|
||||
} else if($column instanceof BoolColumn) {
|
||||
$type = "BOOLEAN";
|
||||
} else if($column instanceof JsonColumn) {
|
||||
$type = "JSON";
|
||||
} else {
|
||||
$this->lastError = "Unsupported Column Type: " . get_class($column);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
$notNull = $column->notNull() ? " NOT NULL" : "";
|
||||
$defaultValue = (!is_null($column->getDefaultValue()) || !$column->notNull()) ? " DEFAULT " . $this->getValueDefinition($column->getDefaultValue()) : "";
|
||||
return "`$columnName` $type$notNull$defaultValue";
|
||||
}
|
||||
|
||||
public function getConstraintDefinition($constraint) {
|
||||
$columnName = $constraint->getColumnName();
|
||||
if ($constraint instanceof PrimaryKey) {
|
||||
if (is_array($columnName)) $columnName = implode('`,`', $columnName);
|
||||
return "PRIMARY KEY (`$columnName`)";
|
||||
} else if ($constraint instanceof Unique) {
|
||||
if (is_array($columnName)) $columnName = implode('`,`', $columnName);
|
||||
return "UNIQUE (`$columnName`)";
|
||||
} else if ($constraint instanceof ForeignKey) {
|
||||
$refTable = $constraint->getReferencedTable();
|
||||
$refColumn = $constraint->getReferencedColumn();
|
||||
$strategy = $constraint->onDelete();
|
||||
$code = "FOREIGN KEY (`$columnName`) REFERENCES `$refTable` (`$refColumn`)";
|
||||
if ($strategy instanceof SetDefaultStrategy) {
|
||||
$code .= " ON DELETE SET DEFAULT";
|
||||
} else if($strategy instanceof SetNullStrategy) {
|
||||
$code .= " ON DELETE SET NULL";
|
||||
} else if($strategy instanceof CascadeStrategy) {
|
||||
$code .= " ON DELETE CASCADE";
|
||||
}
|
||||
|
||||
return $code;
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: check this please..
|
||||
// TODO: Constants??
|
||||
public function getValueDefinition($value) {
|
||||
if (is_numeric($value) || is_bool($value)) {
|
||||
return $value;
|
||||
} else if(is_null($value)) {
|
||||
return "NULL";
|
||||
} else if($value instanceof Keyword) {
|
||||
return $value->getValue();
|
||||
} else {
|
||||
$str = addslashes($value);
|
||||
return "'$str'";
|
||||
}
|
||||
}
|
||||
|
||||
public function currentTimestamp() {
|
||||
return "NOW()";
|
||||
}
|
||||
};
|
||||
97
core/Driver/SQL/Query/CreateTable.class.php
Normal file
97
core/Driver/SQL/Query/CreateTable.class.php
Normal file
@@ -0,0 +1,97 @@
|
||||
<?php
|
||||
|
||||
namespace Driver\SQL\Query;
|
||||
|
||||
use Driver\SQL\Column\SerialColumn;
|
||||
use Driver\SQL\Column\StringColumn;
|
||||
use Driver\SQL\Column\IntColumn;
|
||||
use Driver\SQL\Column\DateTimeColumn;
|
||||
use Driver\SQL\Column\EnumColumn;
|
||||
use Driver\SQL\Column\BoolColumn;
|
||||
use Driver\SQL\Column\JsonColumn;
|
||||
|
||||
use Driver\SQL\Constraint\PrimaryKey;
|
||||
use Driver\SQL\Constraint\Unique;
|
||||
use Driver\SQL\Constraint\ForeignKey;
|
||||
|
||||
class CreateTable extends Query {
|
||||
|
||||
private $tableName;
|
||||
private $columns;
|
||||
private $constraints;
|
||||
private $ifNotExists;
|
||||
|
||||
public function __construct($sql, $name) {
|
||||
parent::__construct($sql);
|
||||
$this->tableName = $name;
|
||||
$this->columns = array();
|
||||
$this->constraints = array();
|
||||
$this->ifNotExists = false;
|
||||
}
|
||||
|
||||
public function addSerial($name) {
|
||||
$this->columns[$name] = new SerialColumn($name);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addString($name, $maxSize=NULL, $nullable=false, $defaultValue=NULL) {
|
||||
$this->columns[$name] = new StringColumn($name, $maxSize, $nullable, $defaultValue);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addDateTime($name, $nullable=false, $defaultNow=false) {
|
||||
$this->columns[$name] = new DateTimeColumn($name, $nullable, $defaultNow);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addInt($name, $nullable=false, $defaultValue=NULL) {
|
||||
$this->columns[$name] = new IntColumn($name, $nullable, $defaultValue);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addBool($name, $defaultValue=false) {
|
||||
$this->columns[$name] = new BoolColumn($name, $defaultValue);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addJson($name, $nullable=false, $defaultValue=NULL) {
|
||||
$this->columns[$name] = new JsonColumn($name, $nullable, $defaultValue);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addEnum($name, $values, $nullable=false, $defaultValue=NULL) {
|
||||
$this->columns[$name] = new EnumColumn($name, $values, $nullable, $defaultValue);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function primaryKey(...$names) {
|
||||
$this->constraints[] = new PrimaryKey($names);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function unique(...$names) {
|
||||
$this->constraints[] = new Unique($names);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function foreignKey($name, $refTable, $refColumn, $strategy = NULL) {
|
||||
$this->constraints[] = new ForeignKey($name, $refTable, $refColumn, $strategy);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function onlyIfNotExists() {
|
||||
$this->ifNotExists = true;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function execute() {
|
||||
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; }
|
||||
};
|
||||
|
||||
?>
|
||||
29
core/Driver/SQL/Query/Delete.class.php
Normal file
29
core/Driver/SQL/Query/Delete.class.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace Driver\SQL\Query;
|
||||
|
||||
class Delete extends Query {
|
||||
|
||||
private $table;
|
||||
private $conditions;
|
||||
|
||||
public function __construct($sql, $table) {
|
||||
parent::__construct($sql);
|
||||
$this->table = $table;
|
||||
$this->conditions = array();
|
||||
}
|
||||
|
||||
public function where(...$conditions) {
|
||||
$this->conditions = array_merge($this->conditions, $conditions);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function execute() {
|
||||
return $this->sql->executeDelete($this);
|
||||
}
|
||||
|
||||
public function getTable() { return $this->table; }
|
||||
public function getConditions() { return $this->conditions; }
|
||||
};
|
||||
|
||||
?>
|
||||
40
core/Driver/SQL/Query/Insert.class.php
Normal file
40
core/Driver/SQL/Query/Insert.class.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace Driver\SQL\Query;
|
||||
|
||||
class Insert extends Query {
|
||||
|
||||
private $tableName;
|
||||
private $columns;
|
||||
private $rows;
|
||||
private $onDuplicateKey;
|
||||
|
||||
public function __construct($sql, $name, $columns=array()) {
|
||||
parent::__construct($sql);
|
||||
$this->tableName = $name;
|
||||
$this->columns = $columns;
|
||||
$this->rows = array();
|
||||
$this->onDuplicateKey = NULL;
|
||||
}
|
||||
|
||||
public function addRow(...$values) {
|
||||
$this->rows[] = $values;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function onDuplicateKeyStrategy($strategy) {
|
||||
$this->onDuplicateKey = $strategy;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function execute() {
|
||||
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; }
|
||||
};
|
||||
|
||||
?>
|
||||
17
core/Driver/SQL/Query/Query.class.php
Normal file
17
core/Driver/SQL/Query/Query.class.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace Driver\SQL\Query;
|
||||
|
||||
abstract class Query {
|
||||
|
||||
protected $sql;
|
||||
|
||||
public function __construct($sql) {
|
||||
$this->sql = $sql;
|
||||
}
|
||||
|
||||
public abstract function execute();
|
||||
|
||||
};
|
||||
|
||||
?>
|
||||
50
core/Driver/SQL/Query/Select.class.php
Normal file
50
core/Driver/SQL/Query/Select.class.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace Driver\SQL\Query;
|
||||
|
||||
class Select extends Query {
|
||||
|
||||
private $columns;
|
||||
private $tables;
|
||||
private $conditions;
|
||||
private $joins;
|
||||
|
||||
public function __construct($sql, ...$columns) {
|
||||
parent::__construct($sql);
|
||||
$this->columns = (!empty($columns) && is_array($columns[0])) ? $columns[0] : $columns;
|
||||
$this->tables = array();
|
||||
$this->conditions = array();
|
||||
$this->joins = array();
|
||||
}
|
||||
|
||||
public function from(...$tables) {
|
||||
$this->tables = array_merge($this->tables, $tables);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function where(...$conditions) {
|
||||
$this->conditions = array_merge($this->conditions, $conditions);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function innerJoin($table, $columnA, $columnB) {
|
||||
$this->joins[] = new \Driver\SQL\Join("INNER", $table, $columnA, $columnB);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function leftJoin($table, $columnA, $columnB) {
|
||||
$this->joins[] = new \Driver\SQL\Join("LEFT", $table, $columnA, $columnB);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function execute() {
|
||||
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; }
|
||||
};
|
||||
|
||||
?>
|
||||
21
core/Driver/SQL/Query/Truncate.class.php
Normal file
21
core/Driver/SQL/Query/Truncate.class.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace Driver\SQL\Query;
|
||||
|
||||
class Truncate extends Query {
|
||||
|
||||
private $tableName;
|
||||
|
||||
public function __construct($sql, $name) {
|
||||
parent::__construct($sql);
|
||||
$this->tableName = $name;
|
||||
}
|
||||
|
||||
public function execute() {
|
||||
return $this->sql->executeTruncate($this);
|
||||
}
|
||||
|
||||
public function getTableName() { return $this->tableName; }
|
||||
};
|
||||
|
||||
?>
|
||||
37
core/Driver/SQL/Query/Update.class.php
Normal file
37
core/Driver/SQL/Query/Update.class.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace Driver\SQL\Query;
|
||||
|
||||
class Update extends Query {
|
||||
|
||||
private $values;
|
||||
private $table;
|
||||
private $conditions;
|
||||
|
||||
public function __construct($sql, $table) {
|
||||
parent::__construct($sql);
|
||||
$this->values = array();
|
||||
$this->table = $table;
|
||||
$this->conditions = array();
|
||||
}
|
||||
|
||||
public function where(...$conditions) {
|
||||
$this->conditions = array_merge($this->conditions, $conditions);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function set($key, $val) {
|
||||
$this->values[$key] = $val;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function execute() {
|
||||
return $this->sql->executeUpdate($this);
|
||||
}
|
||||
|
||||
public function getTable() { return $this->table; }
|
||||
public function getConditions() { return $this->conditions; }
|
||||
public function getValues() { return $this->values; }
|
||||
};
|
||||
|
||||
?>
|
||||
186
core/Driver/SQL/SQL.class.php
Normal file
186
core/Driver/SQL/SQL.class.php
Normal file
@@ -0,0 +1,186 @@
|
||||
<?php
|
||||
|
||||
namespace Driver\SQL;
|
||||
|
||||
abstract class SQL {
|
||||
|
||||
protected $lastError;
|
||||
protected $connection;
|
||||
protected $connectionData;
|
||||
protected $lastInsertId;
|
||||
private $type;
|
||||
|
||||
public function __construct($type, $connectionData) {
|
||||
$this->type = $type;
|
||||
$this->connection = NULL;
|
||||
$this->lastError = 'Not connected';
|
||||
$this->connectionData = $connectionData;
|
||||
$this->lastInsertId = 0;
|
||||
}
|
||||
|
||||
public abstract function connect();
|
||||
public abstract function disconnect();
|
||||
|
||||
public function isConnected() {
|
||||
return !is_null($this->connection);
|
||||
}
|
||||
|
||||
public function getLastError() {
|
||||
return trim($this->lastError);
|
||||
}
|
||||
|
||||
// public function executeQuery($query) {
|
||||
// if(!$this->isConnected()) {
|
||||
// $this->lastError = "Database is not connected yet.";
|
||||
// return false;
|
||||
// }
|
||||
//
|
||||
// return $query->execute($this);
|
||||
// // var_dump($generatedQuery);
|
||||
// // return $this->execute($generatedQuery);
|
||||
// }
|
||||
|
||||
public function createTable($tableName) {
|
||||
return new Query\CreateTable($this, $tableName);
|
||||
}
|
||||
|
||||
public function insert($tableName, $columns=array()) {
|
||||
return new Query\Insert($this, $tableName, $columns);
|
||||
}
|
||||
|
||||
public function select(...$columNames) {
|
||||
return new Query\Select($this, $columNames);
|
||||
}
|
||||
|
||||
public function truncate($table) {
|
||||
return new Query\Truncate($this, $table);
|
||||
}
|
||||
|
||||
public function delete($table) {
|
||||
return new Query\Delete($this, $table);
|
||||
}
|
||||
|
||||
public function update($table) {
|
||||
return new Query\Update($this, $table);
|
||||
}
|
||||
|
||||
// Querybuilder
|
||||
public abstract function executeCreateTable($query);
|
||||
public abstract function executeInsert($query);
|
||||
public abstract function executeSelect($query);
|
||||
public abstract function executeDelete($query);
|
||||
public abstract function executeTruncate($query);
|
||||
public abstract function executeUpdate($query);
|
||||
|
||||
//
|
||||
public abstract function currentTimestamp();
|
||||
|
||||
protected abstract function getColumnDefinition($column);
|
||||
protected abstract function getConstraintDefinition($constraint);
|
||||
protected abstract function getValueDefinition($val);
|
||||
protected abstract function buildCondition($conditions, &$params);
|
||||
|
||||
// Execute
|
||||
protected abstract function execute($query, $values=NULL, $returnValues=false);
|
||||
|
||||
public function setLastError($str) {
|
||||
$this->lastError = $str;
|
||||
}
|
||||
|
||||
protected function addValue($val, &$params) {
|
||||
if ($val instanceof Keyword) {
|
||||
return $val->getValue();
|
||||
} else {
|
||||
$params[] = $val;
|
||||
return "?";
|
||||
}
|
||||
}
|
||||
|
||||
/*public function getLastErrorNumber() {
|
||||
return mysqli_errno($this->connection);
|
||||
}*/
|
||||
|
||||
public function getLastInsertId() {
|
||||
return $this->lastInsertId;
|
||||
}
|
||||
|
||||
public function close() {
|
||||
if(!is_null($this->connection)) {
|
||||
$this->connection->close();
|
||||
}
|
||||
}
|
||||
|
||||
/*public function getAffectedRows() {
|
||||
return $this->connection->affected_rows;
|
||||
}*/
|
||||
|
||||
/*
|
||||
public function execute($query) {
|
||||
if(!$this->isConnected()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!mysqli_query($this->connection, $query)) {
|
||||
$this->lastError = mysqli_error($this->connection);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function executeMulti($queries) {
|
||||
if(!$this->isConnected()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!$this->connection->multi_query($queries)) {
|
||||
$this->lastError = mysqli_error($this->connection);
|
||||
return false;
|
||||
}
|
||||
|
||||
while (($success = $this->connection->next_result())) {
|
||||
if (!$this->connection->more_results()) break;
|
||||
}
|
||||
|
||||
if(!$success) {
|
||||
$this->lastError = mysqli_error($this->connection);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function query($query) {
|
||||
if(!$this->isConnected()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$res = mysqli_query($this->connection, $query);
|
||||
if(!$res) {
|
||||
$this->lastError = mysqli_error($this->connection);
|
||||
return false;
|
||||
}
|
||||
|
||||
return $res;
|
||||
}
|
||||
*/
|
||||
|
||||
public static function createConnection($connectionData) {
|
||||
$type = $connectionData->getProperty("type");
|
||||
if ($type === "mysql") {
|
||||
$sql = new MySQL($connectionData);
|
||||
/*} else if ($type === "postgres") {
|
||||
// $sql = new PostgreSQL($connectionData);
|
||||
} else if ($type === "oracle") {
|
||||
// $sql = new OracleSQL($connectionData);
|
||||
*/
|
||||
} else {
|
||||
return "Unknown database type";
|
||||
}
|
||||
|
||||
$sql->connect();
|
||||
return $sql;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
12
core/Driver/SQL/Strategy/CascadeStrategy.class.php
Normal file
12
core/Driver/SQL/Strategy/CascadeStrategy.class.php
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace Driver\SQL\Strategy;
|
||||
|
||||
class CascadeStrategy extends Strategy {
|
||||
|
||||
public function __construct() {
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
?>
|
||||
12
core/Driver/SQL/Strategy/SetDefaultStrategy.class.php
Normal file
12
core/Driver/SQL/Strategy/SetDefaultStrategy.class.php
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace Driver\SQL\Strategy;
|
||||
|
||||
class SetDefaultStrategy extends Strategy {
|
||||
|
||||
public function __construct() {
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
?>
|
||||
12
core/Driver/SQL/Strategy/SetNullStrategy.class.php
Normal file
12
core/Driver/SQL/Strategy/SetNullStrategy.class.php
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace Driver\SQL\Strategy;
|
||||
|
||||
class SetNullStrategy extends Strategy {
|
||||
|
||||
public function __construct() {
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
?>
|
||||
9
core/Driver/SQL/Strategy/Strategy.class.php
Normal file
9
core/Driver/SQL/Strategy/Strategy.class.php
Normal file
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace Driver\SQL\Strategy;
|
||||
|
||||
abstract class Strategy {
|
||||
|
||||
};
|
||||
|
||||
?>
|
||||
16
core/Driver/SQL/Strategy/UpdateStrategy.class.php
Normal file
16
core/Driver/SQL/Strategy/UpdateStrategy.class.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace Driver\SQL\Strategy;
|
||||
|
||||
class UpdateStrategy extends Strategy {
|
||||
|
||||
private $values;
|
||||
|
||||
public function __construct($values) {
|
||||
$this->values = $values;
|
||||
}
|
||||
|
||||
public function getValues() { return $this->values; }
|
||||
};
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user