refactoring

This commit is contained in:
xinatorus 2020-04-21 20:02:46 +02:00
parent b326596e16
commit a13c30a5c3

View File

@ -1,4 +1,5 @@
<?php <?php
class Db{ class Db{
private static $join; private static $join;
private static $commandDatabase = array ( private static $commandDatabase = array (
@ -57,44 +58,41 @@ class Db{
public static function add ($table, $values = array()) { public static function add ($table, $values = array()) {
return self::command ( return self::command (
"INSERT INTO `$table` (`" . "INSERT INTO `$table` (`" .
implode('`, `', array_keys($values)) . implode('`, `', array_keys($values)) .
"`) VALUES (" . "`) VALUES (" .
str_repeat('?,', (count($values) > 0 ? count($values)-1 : 0)) . str_repeat('?,', (count($values) > 0 ? count($values)-1 : 0)) .
"?)" "?)"
, array_values ($values)); , array_values ($values)
} );
// TODO: pokud vlozim prazdne pole tak chyba ?? }
public static function addAll ($table, $values = array ()) { // TODO: pokud vlozim prazdne pole tak chyba ??
try { public static function addAll ($table, $values = array ()) {
foreach ($values as $value) { try {
self::add ($table, $value); foreach ($values as $value) {
} self::add ($table, $value);
} catch (PDOException $ex) { }
throw new PDOException ($ex->getMessage()); } catch (PDOException $ex) {
} throw new PDOException ($ex->getMessage());
} }
}
public static function edit ( public static function edit ($table, $values = array(), $conditions, $values2 = array()) {
$table, return self::command (
$values = array(), "UPDATE `$table` SET `" .
$conditions, implode('` =?, `', array_keys($values)) .
$values2 = array() "` =? " .
) { $conditions
return self::command ( , array_merge (array_values ($values), $values2)
"UPDATE `$table` SET `" . );
implode('` =?, `', array_keys($values)) . }
"` =? " .
$conditions
, array_merge (array_values ($values), $values2));
}
public static function insertId () { public static function insertId () {
return self::$join->lastInsertId (); return self::$join->lastInsertId ();
} }
public static function addId ($lastTable, $lastIdName) { public static function addId ($lastTable, $lastIdName) {
$answer = self::$join->prepare ("SELECT `$lastIdName` FROM `$lastTable` ORDER BY `$lastIdName` DESC"); $answer = self::$join->prepare ("SELECT `$lastIdName` FROM `$lastTable` ORDER BY `$lastIdName` DESC");
$answer->execute (); $answer->execute ();
return $answer->fetch (PDO::FETCH_NUM)[0]; return $answer->fetch (PDO::FETCH_NUM)[0];
} }
} }