database api start

This commit is contained in:
2023-01-09 20:27:01 +01:00
parent a8af7fa700
commit f14a7a4762
9 changed files with 174 additions and 31 deletions

View File

@@ -2,18 +2,21 @@
namespace Core\Driver\SQL\Condition;
use Core\Driver\SQL\MySQL;
use Core\Driver\SQL\SQL;
class Compare extends Condition {
private string $operator;
private string $column;
private $value;
private mixed $value;
private bool $binary;
public function __construct(string $col, $val, string $operator = '=') {
public function __construct(string $col, $val, string $operator = '=', bool $binary = false) {
$this->operator = $operator;
$this->column = $col;
$this->value = $val;
$this->binary = $binary;
}
public function getColumn(): string { return $this->column; }
@@ -30,6 +33,11 @@ class Compare extends Condition {
}
}
return $sql->columnName($this->column) . $this->operator . $sql->addValue($this->value, $params);
$condition = $sql->columnName($this->column) . $this->operator . $sql->addValue($this->value, $params);
if ($this->binary && $sql instanceof MySQL) {
$condition = "BINARY $condition";
}
return $condition;
}
}

View File

@@ -4,6 +4,9 @@ namespace Core\Driver\SQL;
use Core\API\Parameter\Parameter;
use Core\Driver\SQL\Condition\Compare;
use Core\Driver\SQL\Condition\CondLike;
use Core\Driver\SQL\Expression\Count;
use DateTime;
use Core\Driver\SQL\Column\Column;
use Core\Driver\SQL\Column\IntColumn;
@@ -453,6 +456,18 @@ class MySQL extends SQL {
return $query;
}
public function tableExists(string $tableName): bool {
$tableSchema = $this->connectionData->getProperty("database");
$res = $this->select(new Count())
->from("information_schema.TABLES")
->where(new Compare("TABLE_NAME", $tableName, "=", true))
->where(new Compare("TABLE_SCHEMA", $tableSchema, "=", true))
->where(new CondLike(new Column("TABLE_TYPE"), "BASE TABLE"))
->execute();
return $res && $res[0]["count"] > 0;
}
}
class RowIteratorMySQL extends RowIterator {

View File

@@ -14,8 +14,11 @@ use Core\Driver\SQL\Column\DateTimeColumn;
use Core\Driver\SQL\Column\BoolColumn;
use Core\Driver\SQL\Column\JsonColumn;
use Core\Driver\SQL\Condition\Compare;
use Core\Driver\SQL\Condition\CondLike;
use Core\Driver\SQL\Condition\CondRegex;
use Core\Driver\SQL\Expression\Add;
use Core\Driver\SQL\Expression\Count;
use Core\Driver\SQL\Expression\CurrentTimeStamp;
use Core\Driver\SQL\Expression\Expression;
use Core\Driver\SQL\Query\CreateProcedure;
@@ -444,6 +447,17 @@ class PostgreSQL extends SQL {
return $query;
}
public function tableExists(string $tableName): bool {
$tableSchema = $this->connectionData->getProperty("database");
$res = $this->select(new Count())
->from("pg_tables")
->whereEq("tablename", $tableName)
->whereEq("schemaname", $tableSchema)
->execute();
return $res && $res[0]["count"] > 0;
}
}
class RowIteratorPostgreSQL extends RowIterator {

View File

@@ -127,6 +127,9 @@ abstract class SQL {
public abstract function connect();
public abstract function disconnect();
// Schema
public abstract function tableExists(string $tableName): bool;
/**
* @param Query $query
* @param int $fetchType