160 lines
4.6 KiB
PHP
160 lines
4.6 KiB
PHP
|
|
<?php
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
class TaskCertDb
|
||
|
|
{
|
||
|
|
public static function conn(): mysqli
|
||
|
|
{
|
||
|
|
if (!isset($GLOBALS['mysql_con']) || !($GLOBALS['mysql_con'] instanceof mysqli)) {
|
||
|
|
throw new RuntimeException('task_cert: Missing mysqli connection in $GLOBALS["mysql_con"].');
|
||
|
|
}
|
||
|
|
|
||
|
|
return $GLOBALS['mysql_con'];
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function beginTransaction(): void
|
||
|
|
{
|
||
|
|
mysqli_begin_transaction(self::conn());
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function commit(): void
|
||
|
|
{
|
||
|
|
mysqli_commit(self::conn());
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function rollback(): void
|
||
|
|
{
|
||
|
|
mysqli_rollback(self::conn());
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function fetchAll(string $sql, string $types = '', array $params = []): array
|
||
|
|
{
|
||
|
|
if ($types === '' && $params === []) {
|
||
|
|
$result = mysqli_query(self::conn(), $sql);
|
||
|
|
if ($result === false) {
|
||
|
|
throw new RuntimeException('task_cert query failed: ' . mysqli_error(self::conn()));
|
||
|
|
}
|
||
|
|
|
||
|
|
if ($result instanceof mysqli_result) {
|
||
|
|
$rows = mysqli_fetch_all($result, MYSQLI_ASSOC);
|
||
|
|
mysqli_free_result($result);
|
||
|
|
return $rows;
|
||
|
|
}
|
||
|
|
|
||
|
|
return [];
|
||
|
|
}
|
||
|
|
|
||
|
|
$stmt = self::prepareAndBind($sql, $types, $params);
|
||
|
|
mysqli_stmt_execute($stmt);
|
||
|
|
$result = mysqli_stmt_get_result($stmt);
|
||
|
|
|
||
|
|
if ($result === false) {
|
||
|
|
$error = mysqli_stmt_error($stmt);
|
||
|
|
mysqli_stmt_close($stmt);
|
||
|
|
throw new RuntimeException('task_cert fetchAll failed: ' . $error);
|
||
|
|
}
|
||
|
|
|
||
|
|
$rows = mysqli_fetch_all($result, MYSQLI_ASSOC);
|
||
|
|
mysqli_free_result($result);
|
||
|
|
mysqli_stmt_close($stmt);
|
||
|
|
|
||
|
|
return $rows;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function fetchOne(string $sql, string $types = '', array $params = []): ?array
|
||
|
|
{
|
||
|
|
$rows = self::fetchAll($sql, $types, $params);
|
||
|
|
if ($rows === []) {
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
return $rows[0];
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function scalar(string $sql, string $types = '', array $params = [])
|
||
|
|
{
|
||
|
|
$row = self::fetchOne($sql, $types, $params);
|
||
|
|
if ($row === null) {
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
foreach ($row as $value) {
|
||
|
|
return $value;
|
||
|
|
}
|
||
|
|
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function execute(string $sql, string $types = '', array $params = []): int
|
||
|
|
{
|
||
|
|
if ($types === '' && $params === []) {
|
||
|
|
$ok = mysqli_query(self::conn(), $sql);
|
||
|
|
if ($ok === false) {
|
||
|
|
throw new RuntimeException('task_cert execute failed: ' . mysqli_error(self::conn()));
|
||
|
|
}
|
||
|
|
|
||
|
|
return mysqli_affected_rows(self::conn());
|
||
|
|
}
|
||
|
|
|
||
|
|
$stmt = self::prepareAndBind($sql, $types, $params);
|
||
|
|
mysqli_stmt_execute($stmt);
|
||
|
|
$affected = mysqli_stmt_affected_rows($stmt);
|
||
|
|
$error = mysqli_stmt_error($stmt);
|
||
|
|
mysqli_stmt_close($stmt);
|
||
|
|
|
||
|
|
if ($error !== '') {
|
||
|
|
throw new RuntimeException('task_cert execute failed: ' . $error);
|
||
|
|
}
|
||
|
|
|
||
|
|
return $affected;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function insert(string $sql, string $types, array $params): int
|
||
|
|
{
|
||
|
|
$stmt = self::prepareAndBind($sql, $types, $params);
|
||
|
|
mysqli_stmt_execute($stmt);
|
||
|
|
$error = mysqli_stmt_error($stmt);
|
||
|
|
mysqli_stmt_close($stmt);
|
||
|
|
|
||
|
|
if ($error !== '') {
|
||
|
|
throw new RuntimeException('task_cert insert failed: ' . $error);
|
||
|
|
}
|
||
|
|
|
||
|
|
return (int) mysqli_insert_id(self::conn());
|
||
|
|
}
|
||
|
|
|
||
|
|
private static function prepareAndBind(string $sql, string $types, array $params): mysqli_stmt
|
||
|
|
{
|
||
|
|
$stmt = mysqli_prepare(self::conn(), $sql);
|
||
|
|
if (!$stmt) {
|
||
|
|
throw new RuntimeException('task_cert prepare failed: ' . mysqli_error(self::conn()));
|
||
|
|
}
|
||
|
|
|
||
|
|
self::bindParams($stmt, $types, $params);
|
||
|
|
|
||
|
|
return $stmt;
|
||
|
|
}
|
||
|
|
|
||
|
|
private static function bindParams(mysqli_stmt $stmt, string $types, array $params): void
|
||
|
|
{
|
||
|
|
if ($types === '') {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
$params = array_values($params);
|
||
|
|
if (strlen($types) !== count($params)) {
|
||
|
|
throw new InvalidArgumentException('task_cert bind mismatch: number of types != number of params');
|
||
|
|
}
|
||
|
|
|
||
|
|
$bindValues = [$types];
|
||
|
|
foreach ($params as $index => $value) {
|
||
|
|
$bindValues[] = &$params[$index];
|
||
|
|
}
|
||
|
|
|
||
|
|
$ok = call_user_func_array([$stmt, 'bind_param'], $bindValues);
|
||
|
|
if ($ok === false) {
|
||
|
|
throw new RuntimeException('task_cert bind_param failed: ' . mysqli_stmt_error($stmt));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|