210 lines
5.3 KiB
PHP
210 lines
5.3 KiB
PHP
<?php
|
|
namespace HermannTelser\Gutachten\DB;
|
|
|
|
// composer dump-autoload -o
|
|
|
|
use \HermannTelser\Gutachten\Utility\Message;
|
|
use \HermannTelser\Gutachten\Eid\TxGutachten;
|
|
use PDO;
|
|
|
|
class DbFunction
|
|
{
|
|
public static $debug = 0;
|
|
public static $projektid = 0;
|
|
|
|
/***************************************************
|
|
* print sql querries
|
|
***************************************************/
|
|
public static function debugSql($line)
|
|
{
|
|
if (self::$debug or TxGutachten::$debug) {
|
|
Message::note($line . " - " . str_replace('"', "", MariaDB::$db_handle->last()));
|
|
}
|
|
}
|
|
|
|
|
|
/***************************************************
|
|
* secure UPDATE
|
|
***************************************************/
|
|
|
|
public static function secureUpdate($table, $value_array, $where)
|
|
{
|
|
$max_tries = 3;
|
|
$tries = 0;
|
|
do {
|
|
$retry = false;
|
|
try {
|
|
$data = MariaDB::$db_handle->update($table, $value_array, $where);
|
|
|
|
self::debugSql(__line__);
|
|
|
|
return $data->rowCount();
|
|
|
|
} catch (PDOException $exc) {
|
|
if ($tries < $max_tries && $exc->errorInfo[0] == 40001 && $exc->errorInfo[1] == 1213) {
|
|
$retry = true;
|
|
sleep(1);
|
|
} else {
|
|
throw $exc;
|
|
}
|
|
}
|
|
} while ($retry);
|
|
|
|
}
|
|
|
|
/***************************************************
|
|
* secure INSERT
|
|
***************************************************/
|
|
|
|
public static function secureInsert($table, $insert_array)
|
|
{
|
|
$max_tries = 3;
|
|
$tries = 0;
|
|
do {
|
|
$retry = false;
|
|
try {
|
|
MariaDB::$db_handle->insert($table, $insert_array);
|
|
|
|
self::debugSql(__line__);
|
|
} catch (PDOException $exc) {
|
|
if ($tries < $max_tries && $exc->errorInfo[0] == 40001 && $exc->errorInfo[1] == 1213) {
|
|
$retry = true;
|
|
sleep(1);
|
|
} else {
|
|
throw $exc;
|
|
}
|
|
}
|
|
} while ($retry);
|
|
|
|
return MariaDB::$db_handle->id();
|
|
}
|
|
|
|
/***************************************************
|
|
* DELETE
|
|
***************************************************/
|
|
|
|
public static function delete($table, $where)
|
|
{
|
|
$handle = MariaDB::$db_handle->delete($table, $where);
|
|
|
|
self::debugSql(__line__);
|
|
|
|
return $handle;
|
|
}
|
|
|
|
/***************************************************
|
|
* SELECT data
|
|
***************************************************/
|
|
|
|
public static function select($table, $fields, $where=[])
|
|
{
|
|
$tmp = MariaDB::$db_handle->select($table, $fields, $where);
|
|
|
|
self::debugSql(__line__);
|
|
|
|
return $tmp;
|
|
}
|
|
|
|
/***************************************************
|
|
* SUM data
|
|
***************************************************/
|
|
|
|
public static function sum($table, $fields, $where=[])
|
|
{
|
|
$tmp = MariaDB::$db_handle->sum($table, $fields, $where);
|
|
|
|
self::debugSql(__line__);
|
|
|
|
return $tmp;
|
|
}
|
|
|
|
/***************************************************
|
|
* SELECT data
|
|
***************************************************/
|
|
|
|
public static function selectJoin($table, $join, $fields, $where=[])
|
|
{
|
|
$tmp = MariaDB::$db_handle->select($table, $join, $fields, $where);
|
|
|
|
self::debugSql(__line__);
|
|
|
|
return $tmp;
|
|
}
|
|
|
|
/***************************************************
|
|
* select SINGLE row
|
|
***************************************************/
|
|
|
|
public static function get($table, $fields, $where)
|
|
{
|
|
$tmp = MariaDB::$db_handle->get($table, $fields, $where);
|
|
|
|
self::debugSql(__line__);
|
|
|
|
return $tmp;
|
|
}
|
|
|
|
|
|
/***************************************************
|
|
* select SINGLE row
|
|
***************************************************/
|
|
|
|
public static function getJoin($table, $join, $fields, $where)
|
|
{
|
|
$tmp = MariaDB::$db_handle->get($table, $join, $fields, $where);
|
|
|
|
self::debugSql(__line__);
|
|
|
|
return $tmp;
|
|
}
|
|
|
|
/***************************************************
|
|
* sql query
|
|
***************************************************/
|
|
|
|
public static function query($sql)
|
|
{
|
|
$arr = MariaDB::$db_handle->query($sql)->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
self::debugSql(__line__);
|
|
|
|
return $arr;
|
|
}
|
|
|
|
/***************************************************
|
|
* SELECT COUNT
|
|
***************************************************/
|
|
|
|
public static function count($table, $where=[])
|
|
{
|
|
$tmp = MariaDB::$db_handle->count($table, $where);
|
|
|
|
self::debugSql(__line__);
|
|
|
|
return $tmp;
|
|
}
|
|
|
|
/***************************************************
|
|
* has
|
|
***************************************************/
|
|
|
|
public static function has($table, $where)
|
|
{
|
|
$tmp = MariaDB::$db_handle->has($table, $where);
|
|
|
|
self::debugSql(__line__);
|
|
|
|
return $tmp;
|
|
}
|
|
|
|
/***************************************************
|
|
* toggle
|
|
***************************************************/
|
|
|
|
public static function toggle($table, $field, $where)
|
|
{
|
|
self::query("UPDATE $table SET $field = 1 - $field WHERE $where");
|
|
}
|
|
|
|
}
|