2022-12-11 13:55:49 +05:00
|
|
|
|
<?php
|
|
|
|
|
class DB
|
|
|
|
|
{
|
|
|
|
|
public static $dsn = '';
|
|
|
|
|
public static $user = '';
|
|
|
|
|
public static $pass = '';
|
2022-12-11 15:29:37 +05:00
|
|
|
|
public static $type = '';
|
2022-12-17 18:15:09 +05:00
|
|
|
|
|
|
|
|
|
public static $path = '';
|
2022-12-11 13:55:49 +05:00
|
|
|
|
/**
|
|
|
|
|
* Объект PDO.
|
|
|
|
|
*/
|
|
|
|
|
public static $dbh = null;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Statement Handle.
|
|
|
|
|
*/
|
|
|
|
|
public static $sth = null;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Выполняемый SQL запрос.
|
|
|
|
|
*/
|
|
|
|
|
public static $query = '';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Подключение к БД.
|
|
|
|
|
* $this -> conn = new PDO( 'sqlite:' . $_SERVER['DOCUMENT_ROOT'] . '/bd/'. $bd );
|
|
|
|
|
*/
|
2022-12-17 18:15:09 +05:00
|
|
|
|
public static function getDbh(){
|
|
|
|
|
if (self::$type==''){
|
|
|
|
|
require 'set/' . $_SERVER['SERVER_NAME'] . '.php';
|
|
|
|
|
//self::$type=@$db['type'];
|
|
|
|
|
if (@$db['type']=='mysql'){
|
|
|
|
|
self::$dsn=$db['type'] . ':dbname=' . $db['db'] . ';host=' . $db['host'];
|
|
|
|
|
self::$user=$db['user'];
|
|
|
|
|
self::$pass=$db['password'];
|
|
|
|
|
if (!self::$dbh) {
|
|
|
|
|
try {
|
|
|
|
|
self::$dbh = new PDO(
|
2022-12-11 13:55:49 +05:00
|
|
|
|
self::$dsn,
|
|
|
|
|
self::$user,
|
|
|
|
|
self::$pass,
|
|
|
|
|
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'")
|
2022-12-17 18:15:09 +05:00
|
|
|
|
);
|
2022-12-11 13:55:49 +05:00
|
|
|
|
self::$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
|
2022-12-17 18:15:09 +05:00
|
|
|
|
} catch (PDOException $e) {exit('Error connecting to database: ' . $e->getMessage());}
|
2022-12-11 13:55:49 +05:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-17 18:15:09 +05:00
|
|
|
|
if (@$db['type']=='sqlite3'){
|
|
|
|
|
# echo "lite";
|
|
|
|
|
if (!self::$dbh) {
|
|
|
|
|
try {
|
|
|
|
|
self::$dbh = new PDO('sqlite:' . 'bd/'. $_SERVER['SERVER_NAME'] . '.db' );
|
|
|
|
|
//Делаем бекап БД
|
|
|
|
|
if (!file_exists('bd/' . date("Y-m-d") . '_'. $_SERVER['SERVER_NAME'] . '.zip'))exec ('zip bd/' . date("Y-m-d") . '_'. $_SERVER['SERVER_NAME'] . '.zip bd/'. $_SERVER['SERVER_NAME'] . '.db 9 &');
|
2023-01-02 21:33:36 +05:00
|
|
|
|
self::$dbh->exec('PRAGMA journal_mode=WAL;');
|
2022-12-17 18:15:09 +05:00
|
|
|
|
} catch (PDOException $e) {exit('Error connecting to database: ' . $e->getMessage());}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}else{
|
|
|
|
|
self::$dbh = new PDO('sqlite:' . self::$path );
|
2023-01-02 21:33:36 +05:00
|
|
|
|
self::$dbh->exec('PRAGMA journal_mode=WAL;');
|
2022-12-11 13:55:49 +05:00
|
|
|
|
}
|
|
|
|
|
return self::$dbh;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Закрытие соединения.
|
|
|
|
|
*/
|
2022-12-17 18:15:09 +05:00
|
|
|
|
public static function destroy(){
|
2022-12-11 13:55:49 +05:00
|
|
|
|
self::$dbh = null;
|
|
|
|
|
return self::$dbh;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Получение ошибки запроса.
|
|
|
|
|
*/
|
2022-12-17 18:15:09 +05:00
|
|
|
|
public static function getError(){
|
2022-12-11 13:55:49 +05:00
|
|
|
|
$info = self::$sth->errorInfo();
|
|
|
|
|
return (isset($info[2])) ? 'SQL: ' . $info[2] : null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Возвращает структуру таблицы в виде ассоциативного массива.
|
|
|
|
|
*/
|
2022-12-17 18:15:09 +05:00
|
|
|
|
public static function getStructure($table){
|
2022-12-11 13:55:49 +05:00
|
|
|
|
$res = array();
|
2022-12-17 18:15:09 +05:00
|
|
|
|
foreach (self::getAll("SHOW COLUMNS FROM {$table}") as $row) {$res[$row['Field']] = (is_null($row['Default'])) ? '' : $row['Default'];}
|
2022-12-11 13:55:49 +05:00
|
|
|
|
return $res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Добавление в таблицу, в случаи успеха вернет вставленный ID, иначе 0.
|
|
|
|
|
*/
|
2022-12-17 18:15:09 +05:00
|
|
|
|
public static function add($query, $param = array()){
|
2022-12-11 15:29:37 +05:00
|
|
|
|
self::$sth = self::getDbh()->prepare($query);
|
|
|
|
|
return (self::$sth->execute((array) $param)) ? self::getDbh()->lastInsertId() : 0;
|
2022-12-11 13:55:49 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Выполнение запроса.
|
|
|
|
|
*/
|
2022-12-17 18:15:09 +05:00
|
|
|
|
public static function set($query, $param = array()){
|
2022-12-11 13:55:49 +05:00
|
|
|
|
self::$sth = self::getDbh()->prepare($query);
|
|
|
|
|
return self::$sth->execute((array) $param);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-17 18:15:09 +05:00
|
|
|
|
|
2022-12-11 13:55:49 +05:00
|
|
|
|
/**
|
|
|
|
|
* Получение строки из таблицы.
|
|
|
|
|
*/
|
2022-12-17 18:15:09 +05:00
|
|
|
|
public static function getRow($query, $param = array()){
|
2022-12-11 13:55:49 +05:00
|
|
|
|
self::$sth = self::getDbh()->prepare($query);
|
|
|
|
|
self::$sth->execute((array) $param);
|
|
|
|
|
return self::$sth->fetch(PDO::FETCH_ASSOC);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Получение всех строк из таблицы.
|
|
|
|
|
*/
|
2022-12-17 18:15:09 +05:00
|
|
|
|
public static function getAll($query, $param = array()){
|
2022-12-11 13:55:49 +05:00
|
|
|
|
self::$sth = self::getDbh()->prepare($query);
|
|
|
|
|
self::$sth->execute((array) $param);
|
|
|
|
|
return self::$sth->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Получение значения.
|
|
|
|
|
*/
|
2022-12-17 18:15:09 +05:00
|
|
|
|
public static function getValue($query, $param = array(), $default = null){
|
2022-12-11 13:55:49 +05:00
|
|
|
|
$result = self::getRow($query, $param);
|
2022-12-17 18:15:09 +05:00
|
|
|
|
if (!empty($result)) {$result = array_shift($result);}
|
2022-12-11 13:55:49 +05:00
|
|
|
|
return (empty($result)) ? $default : $result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Получение столбца таблицы.
|
|
|
|
|
*/
|
2022-12-17 18:15:09 +05:00
|
|
|
|
public static function getColumn($query, $param = array()){
|
2022-12-11 13:55:49 +05:00
|
|
|
|
self::$sth = self::getDbh()->prepare($query);
|
|
|
|
|
self::$sth->execute((array) $param);
|
|
|
|
|
return self::$sth->fetchAll(PDO::FETCH_COLUMN);
|
|
|
|
|
}
|
2022-12-17 18:15:09 +05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static function alterTable($query, $param = array()){
|
|
|
|
|
self::$sth = self::getDbh()->prepare($query);
|
|
|
|
|
self::$dbh -> query($query);
|
|
|
|
|
}
|
2022-12-11 13:55:49 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
|
|
олучение одной записи из БД
|
|
|
|
|
|
|
|
|
|
$item = DB::getRow("SELECT * FROM `category` WHERE `id` = ?", 1);
|
|
|
|
|
|
|
|
|
|
// Или
|
|
|
|
|
|
|
|
|
|
$item = DB::getRow("SELECT * FROM `category` WHERE `id` = :id", array('id' => 1));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print_r($item);
|
|
|
|
|
|
|
|
|
|
PHP
|
|
|
|
|
Результат:
|
|
|
|
|
|
|
|
|
|
Array
|
|
|
|
|
|
|
|
|
|
(
|
|
|
|
|
|
|
|
|
|
[id] => 1
|
|
|
|
|
|
|
|
|
|
[parent] => 0
|
|
|
|
|
|
|
|
|
|
[name] => Мороженое
|
|
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
Получение нескольких записей из БД
|
|
|
|
|
|
|
|
|
|
$items = DB::getAll("SELECT * FROM `category` WHERE `id` > 2");
|
|
|
|
|
|
|
|
|
|
print_r($items);
|
|
|
|
|
|
|
|
|
|
PHP
|
|
|
|
|
Результат:
|
|
|
|
|
|
|
|
|
|
Array
|
|
|
|
|
|
|
|
|
|
(
|
|
|
|
|
|
|
|
|
|
[0] => Array
|
|
|
|
|
|
|
|
|
|
(
|
|
|
|
|
|
|
|
|
|
[id] => 3
|
|
|
|
|
|
|
|
|
|
[parent] => 0
|
|
|
|
|
|
|
|
|
|
[name] => Фрукты
|
|
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
[1] => Array
|
|
|
|
|
|
|
|
|
|
(
|
|
|
|
|
|
|
|
|
|
[id] => 4
|
|
|
|
|
|
|
|
|
|
[parent] => 0
|
|
|
|
|
|
|
|
|
|
[name] => Ягоды
|
|
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
[2] => Array
|
|
|
|
|
|
|
|
|
|
(
|
|
|
|
|
|
|
|
|
|
[id] => 5
|
|
|
|
|
|
|
|
|
|
[parent] => 2
|
|
|
|
|
|
|
|
|
|
[name] => Грибы
|
|
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
...
|
|
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
Получения значения
|
|
|
|
|
|
|
|
|
|
$value = DB::getValue("SELECT `name` FROM `category` WHERE `id` = 2");
|
|
|
|
|
|
|
|
|
|
print_r($value);
|
|
|
|
|
|
|
|
|
|
PHP
|
|
|
|
|
Результат:
|
|
|
|
|
|
|
|
|
|
Овощи
|
|
|
|
|
|
|
|
|
|
Получения значений колонки
|
|
|
|
|
|
|
|
|
|
$values = DB::getColumn("SELECT `name` FROM `category`");
|
|
|
|
|
|
|
|
|
|
print_r($values);
|
|
|
|
|
|
|
|
|
|
PHP
|
|
|
|
|
Результат:
|
|
|
|
|
|
|
|
|
|
Array
|
|
|
|
|
|
|
|
|
|
(
|
|
|
|
|
|
|
|
|
|
[0] => Мороженое
|
|
|
|
|
|
|
|
|
|
[1] => Овощи
|
|
|
|
|
|
|
|
|
|
[2] => Фрукты
|
|
|
|
|
|
|
|
|
|
[3] => Ягоды
|
|
|
|
|
|
|
|
|
|
[4] => Грибы
|
|
|
|
|
|
|
|
|
|
[5] => Морепродукты
|
|
|
|
|
|
|
|
|
|
...
|
|
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
Добавление в БД
|
|
|
|
|
|
|
|
|
|
Метод возвращает ID вставленной записи.
|
|
|
|
|
|
|
|
|
|
$insert_id = DB::add("INSERT INTO `category` SET `name` = ?", 'Яблоки');
|
|
|
|
|
|
|
|
|
|
PHP
|
|
|
|
|
Все остальные запросы
|
|
|
|
|
|
|
|
|
|
Выполняет запросы в БД, такие как DELETE, UPDATE, CREATE TABLE и т.д. В случаи успеха возвращает true.
|
|
|
|
|
|
|
|
|
|
DB::set("DELETE FROM `category` WHERE `id` > ? AND `parent` > ?", array(123, 0));
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
?>
|