ordersprinter/gastsystem/php/dbutils.php

71 lines
1.8 KiB
PHP

<?php
// Datenbank-Verbindungsparameter
require_once ('config.php');
class DbUtils {
public static $TYPE_LOGO = 1;
public static $TYPE_PRODIMG = 2;
public static function openDbAndReturnPdoStatic () {
$dsn = 'mysql:host=' . MYSQL_HOST . ';dbname=' . MYSQL_DB;
$user = MYSQL_USER;
$password = MYSQL_PASSWORD;
$pdo = null;
try {
$pdo = new PDO($dsn, $user, $password);
$pdo ->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
return $pdo;
}
public static function substTableAlias($sqlString) {
$out = str_replace("%ossystem%",TAB_PREFIX . 'ossystem',$sqlString);
$out = str_replace("%gueststatus%", TAB_PREFIX . 'gueststatus',$out);
$out = str_replace("%queue%", TAB_PREFIX . 'queue',$out);
$out = str_replace("%images%", TAB_PREFIX . 'images',$out);
return $out;
}
public static function getRowSqlObject($pdo,$sql,$params) {
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
if (is_null($params)) {
$stmt->execute();
} else {
$stmt->execute($params);
}
return ($stmt->fetchObject());
}
public static function fetchSqlAll($pdo,$sql,$params) {
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
if (is_null($params)) {
$stmt->execute();
} else {
$stmt->execute($params);
}
return ($stmt->fetchAll(PDO::FETCH_ASSOC));
}
public static function execSql($pdo,$sql,$params) {
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
if (is_null($params)) {
$stmt->execute();
} else {
$stmt->execute($params);
}
}
public static function getConfigItem($pdo,$item,$default) {
$sql = "SELECT value FROM %ossystem% WHERE item=?";
$result = self::fetchSqlAll($pdo, $sql, array($item));
if (count($result) == 0) {
return $default;
} else {
return $result[0]["value"];
}
}
}