frontend & backend update
This commit is contained in:
@@ -7,6 +7,7 @@ use Core\Driver\SQL\SQL;
|
||||
class Logger {
|
||||
|
||||
public const LOG_FILE_DATE_FORMAT = "Y-m-d_H-i-s_v";
|
||||
public const LOG_LEVEL_NONE = -1;
|
||||
public const LOG_LEVEL_DEBUG = 0;
|
||||
public const LOG_LEVEL_INFO = 1;
|
||||
public const LOG_LEVEL_WARNING = 2;
|
||||
|
||||
@@ -7,8 +7,8 @@ use Core\Driver\SQL\SQL;
|
||||
|
||||
class CondIn extends Condition {
|
||||
|
||||
private $needle;
|
||||
private $haystack;
|
||||
private mixed $needle;
|
||||
private mixed $haystack;
|
||||
|
||||
public function __construct($needle, $haystack) {
|
||||
$this->needle = $needle;
|
||||
@@ -20,22 +20,28 @@ class CondIn extends Condition {
|
||||
|
||||
function getExpression(SQL $sql, array &$params): string {
|
||||
|
||||
$haystack = $this->getHaystack();
|
||||
if (is_array($haystack)) {
|
||||
$values = array();
|
||||
foreach ($haystack as $value) {
|
||||
$values[] = $sql->addValue($value, $params);
|
||||
}
|
||||
$needle = $sql->addValue($this->needle, $params);
|
||||
|
||||
$values = implode(",", $values);
|
||||
$values = "($values)";
|
||||
} else if($haystack instanceof Select) {
|
||||
$values = $haystack->getExpression($sql, $params);
|
||||
if (is_array($this->haystack)) {
|
||||
if (!empty($this->haystack)) {
|
||||
$values = array();
|
||||
foreach ($this->haystack as $value) {
|
||||
$values[] = $sql->addValue($value, $params);
|
||||
}
|
||||
|
||||
$values = implode(",", $values);
|
||||
$values = "($values)";
|
||||
} else {
|
||||
$sql->getLogger()->error("Empty haystack for in-expression with needle: " . $needle);
|
||||
return false;
|
||||
}
|
||||
} else if ($this->haystack instanceof Select) {
|
||||
$values = $this->haystack->getExpression($sql, $params);
|
||||
} else {
|
||||
$sql->getLogger()->error("Unsupported in-expression value: " . get_class($haystack));
|
||||
$sql->getLogger()->error("Unsupported in-expression value: " . get_class($this->haystack));
|
||||
return false;
|
||||
}
|
||||
|
||||
return $sql->addValue($this->needle, $params) . " IN $values";
|
||||
return "$needle IN $values";
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ namespace Core\Driver\SQL;
|
||||
|
||||
use Core\API\Parameter\Parameter;
|
||||
|
||||
use Core\Driver\Logger\Logger;
|
||||
use Core\Driver\SQL\Condition\Compare;
|
||||
use Core\Driver\SQL\Condition\CondLike;
|
||||
use Core\Driver\SQL\Expression\Count;
|
||||
@@ -138,7 +139,7 @@ class MySQL extends SQL {
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
protected function execute($query, $values = NULL, int $fetchType = self::FETCH_NONE) {
|
||||
protected function execute($query, $values = NULL, int $fetchType = self::FETCH_NONE, int $logLevel = Logger::LOG_LEVEL_ERROR) {
|
||||
|
||||
$result = null;
|
||||
$this->lastError = "";
|
||||
@@ -146,6 +147,10 @@ class MySQL extends SQL {
|
||||
$res = null;
|
||||
$success = false;
|
||||
|
||||
if ($logLevel === Logger::LOG_LEVEL_DEBUG) {
|
||||
$this->logger->debug("query: " . $query . ", args: " . json_encode($values), false);
|
||||
}
|
||||
|
||||
try {
|
||||
if (empty($values)) {
|
||||
$res = mysqli_query($this->connection, $query);
|
||||
@@ -167,7 +172,6 @@ class MySQL extends SQL {
|
||||
}
|
||||
}
|
||||
} else if ($stmt = $this->connection->prepare($query)) {
|
||||
|
||||
$sqlParams = $this->getPreparedParams($values);
|
||||
if ($stmt->bind_param(...$sqlParams)) {
|
||||
if ($stmt->execute()) {
|
||||
@@ -201,10 +205,12 @@ class MySQL extends SQL {
|
||||
}
|
||||
}
|
||||
} catch (\mysqli_sql_exception $exception) {
|
||||
$this->lastError = $this->logger->error("MySQL::execute failed: " .
|
||||
($stmt !== null
|
||||
? "$stmt->error ($stmt->errno)"
|
||||
: $exception->getMessage()));
|
||||
if ($logLevel >= Logger::LOG_LEVEL_ERROR) {
|
||||
$this->lastError = $this->logger->error("MySQL::execute failed: " .
|
||||
($stmt !== null
|
||||
? "$stmt->error ($stmt->errno)"
|
||||
: $exception->getMessage()));
|
||||
}
|
||||
} finally {
|
||||
|
||||
if ($res !== null && !is_bool($res) && $fetchType !== self::FETCH_ITERATIVE) {
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace Core\Driver\SQL;
|
||||
|
||||
use Core\API\Parameter\Parameter;
|
||||
|
||||
use Core\Driver\Logger\Logger;
|
||||
use Core\Driver\SQL\Column\Column;
|
||||
use Core\Driver\SQL\Column\IntColumn;
|
||||
use Core\Driver\SQL\Column\NumericColumn;
|
||||
@@ -96,12 +97,16 @@ class PostgreSQL extends SQL {
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
protected function execute($query, $values = NULL, int $fetchType = self::FETCH_NONE) {
|
||||
protected function execute($query, $values = NULL, int $fetchType = self::FETCH_NONE, int $logLevel = Logger::LOG_LEVEL_ERROR) {
|
||||
|
||||
$this->lastError = "";
|
||||
$stmt_name = uniqid();
|
||||
$pgParams = array();
|
||||
|
||||
if ($logLevel === Logger::LOG_LEVEL_DEBUG) {
|
||||
$this->logger->debug("query: " . $query . ", args: " . json_encode($values), false);
|
||||
}
|
||||
|
||||
if (!is_null($values)) {
|
||||
foreach ($values as $value) {
|
||||
$paramType = Parameter::parseType($value);
|
||||
|
||||
@@ -40,6 +40,7 @@ class Insert extends Query {
|
||||
public function getTableName(): string { return $this->tableName; }
|
||||
public function getColumns(): array { return $this->columns; }
|
||||
public function getRows(): array { return $this->rows; }
|
||||
public function hasRows(): bool { return !empty($this->rows); }
|
||||
public function onDuplicateKey(): ?Strategy { return $this->onDuplicateKey; }
|
||||
public function getReturning(): ?string { return $this->returning; }
|
||||
|
||||
|
||||
@@ -149,7 +149,12 @@ abstract class SQL {
|
||||
return false;
|
||||
}
|
||||
|
||||
$res = $this->execute($queryStr, $parameters, $fetchType);
|
||||
$logLevel = Logger::LOG_LEVEL_DEBUG;
|
||||
if ($query instanceof Insert && $query->getTableName() === "SystemLog") {
|
||||
$logLevel = Logger::LOG_LEVEL_NONE;
|
||||
}
|
||||
|
||||
$res = $this->execute($queryStr, $parameters, $fetchType, $logLevel);
|
||||
$success = ($res !== FALSE);
|
||||
|
||||
// fetch generated serial ids for Insert statements
|
||||
@@ -273,7 +278,7 @@ abstract class SQL {
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
protected abstract function execute($query, $values = NULL, int $fetchType = self::FETCH_NONE);
|
||||
protected abstract function execute($query, $values = NULL, int $fetchType = self::FETCH_NONE, int $logLevel = Logger::LOG_LEVEL_ERROR);
|
||||
|
||||
public function buildCondition(Condition|array $condition, &$params): string {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user