2022-12-21 10:14:44 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BancaDati;
|
|
|
|
|
2023-01-11 11:02:42 +01:00
|
|
|
use PDO;
|
|
|
|
use PDOException;
|
|
|
|
|
2022-12-21 10:14:44 +01:00
|
|
|
class BancaDati {
|
|
|
|
private $dbName = "BancaDati";
|
|
|
|
private $linkName = "localhost";
|
|
|
|
private $user = "root";
|
|
|
|
private $pw = "root";
|
|
|
|
|
|
|
|
public $pdo;
|
|
|
|
public function __construct() {
|
|
|
|
$this->linkDB();
|
|
|
|
}
|
|
|
|
private function linkDB() {
|
|
|
|
try {
|
2023-01-11 11:02:42 +01:00
|
|
|
$this->pdo = new PDO("mysql:dbname=$this->dbName;host=$this->linkName"
|
2022-12-21 10:14:44 +01:00
|
|
|
, $this->user
|
|
|
|
, $this->pw
|
2023-01-11 11:02:42 +01:00
|
|
|
, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
|
|
|
|
} catch (PDOException $e) {
|
2022-12-21 10:14:44 +01:00
|
|
|
die;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public function createUUID()
|
|
|
|
{
|
|
|
|
$data = openssl_random_pseudo_bytes(16);
|
|
|
|
$data[6] = chr(ord($data[6]) & 0x0f | 0x40);
|
|
|
|
$data[8] = chr(ord($data[8]) & 0x3f | 0x80);
|
|
|
|
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
|
|
|
|
}
|
2022-12-21 11:19:40 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Einheitliche Insert Funktion
|
|
|
|
* @param string $table
|
|
|
|
* @param array $values
|
|
|
|
* @return void
|
|
|
|
* author Simon Bock
|
|
|
|
*/
|
|
|
|
public function insert(string $table, array $values){
|
|
|
|
$value = "";
|
|
|
|
$column = "";
|
|
|
|
foreach ($values as $col => $v){
|
2023-01-18 09:09:39 +01:00
|
|
|
$value .= "'" . $v . "',";
|
2022-12-21 11:19:40 +01:00
|
|
|
$column .= $col . ",";
|
|
|
|
}
|
|
|
|
$value = trim($value, ",");
|
|
|
|
$column = trim($column, ",");
|
|
|
|
|
|
|
|
$sql = "INSERT INTO $table($column) VALUES ($value);";
|
|
|
|
try {
|
|
|
|
$sth = $this->pdo->prepare($sql);
|
|
|
|
$sth->execute();
|
2023-01-11 11:02:42 +01:00
|
|
|
}catch (PDOException $e){
|
2022-12-21 11:19:40 +01:00
|
|
|
die;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Einheitliche Update Funktion
|
|
|
|
* @param string $table
|
|
|
|
* @param string $id
|
|
|
|
* @param array $values
|
|
|
|
* @return void
|
|
|
|
* @author Malte Schulze Hobeling
|
|
|
|
*/
|
2022-12-21 10:57:49 +01:00
|
|
|
public function update(string $table, string $id, array $values){
|
|
|
|
$value = "";
|
|
|
|
foreach ($values as $col => $v){
|
2023-01-18 09:09:39 +01:00
|
|
|
$value .= $col . "='" . $v . "',";
|
2022-12-21 10:57:49 +01:00
|
|
|
}
|
|
|
|
$value = trim($value, ",");
|
|
|
|
|
2022-12-21 10:39:28 +01:00
|
|
|
$sql = "UPDATE " . $table . " SET " . $value . " WHERE `id` = " . $id . ";";
|
|
|
|
try {
|
|
|
|
$sth = $this->pdo->prepare($sql);
|
|
|
|
$sth->execute();
|
2023-01-11 11:02:42 +01:00
|
|
|
}catch (PDOException $e){
|
2022-12-21 10:39:28 +01:00
|
|
|
die;
|
|
|
|
}
|
|
|
|
}
|
2022-12-21 11:19:40 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Einheitliche Delete Funktion
|
|
|
|
* @param string $table
|
|
|
|
* @param string $id
|
|
|
|
* @return void
|
|
|
|
* @author Malte Schulze Hobeling
|
|
|
|
*/
|
|
|
|
public function delete(string $table, string $id){
|
2023-01-18 09:09:39 +01:00
|
|
|
$sql = "DELETE FROM " . $table . " WHERE `id` = '" . $id . "';";
|
2022-12-21 11:19:40 +01:00
|
|
|
try {
|
|
|
|
$sth = $this->pdo->prepare($sql);
|
|
|
|
$sth->execute();
|
2023-01-11 11:02:42 +01:00
|
|
|
}catch (PDOException $e){
|
2022-12-21 11:19:40 +01:00
|
|
|
die;
|
|
|
|
}
|
|
|
|
}
|
2023-01-11 11:02:42 +01:00
|
|
|
|
2023-01-12 14:05:19 +01:00
|
|
|
/**
|
|
|
|
* einheitliche Select Funktion
|
|
|
|
* @param string $table
|
|
|
|
* @param array $where ["column"]=>"value" es wird mit LIKE verglichen und mit AND verbunden
|
|
|
|
* @param array|null $order ["by"]=>"column"; ["order"]=>"ASC|DESC"
|
|
|
|
* @return void
|
|
|
|
* @author Malte Schulze Hobeling
|
|
|
|
*/
|
|
|
|
public function select(string $table, array $where, array $order = null){
|
|
|
|
$whereString = "";
|
|
|
|
foreach ($where as $col => $v) {
|
|
|
|
if($whereString != ""){
|
|
|
|
$whereString .= " AND ";
|
2023-01-11 14:40:12 +01:00
|
|
|
}
|
2023-01-18 09:09:39 +01:00
|
|
|
$whereString .= "`" . $col . "` = '" . $v . "'";
|
2023-01-11 14:40:12 +01:00
|
|
|
}
|
2023-01-18 09:09:39 +01:00
|
|
|
$sql = "SELECT * FROM ".$table." WHERE ".$whereString.";";
|
2023-01-11 14:40:12 +01:00
|
|
|
if(isset($order["by"])){
|
2023-01-11 14:41:30 +01:00
|
|
|
$sql .= " ORDER BY ".$order["by"];
|
2023-01-11 14:40:12 +01:00
|
|
|
}
|
|
|
|
if(isset($order["order"])){
|
|
|
|
$sql .= $order["order"];
|
|
|
|
}
|
|
|
|
try {
|
2023-01-18 09:09:39 +01:00
|
|
|
var_dump($sql);
|
2023-01-11 14:40:12 +01:00
|
|
|
return $this->pdo->query($sql);
|
|
|
|
}catch (PDOException $e){
|
|
|
|
die;
|
2023-01-11 11:02:42 +01:00
|
|
|
}
|
|
|
|
}
|
2022-12-21 10:14:44 +01:00
|
|
|
}
|