core/api/php/db.php

158 lines
4.3 KiB
PHP
Raw Normal View History

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(){
2023-01-14 21:17:46 +05:00
2022-12-17 18:15:09 +05:00
require 'set/' . $_SERVER['SERVER_NAME'] . '.php';
2023-01-14 21:17:46 +05:00
if (self::$type==''){
if (@$db['type']=='mysql'){
if (!self::$dsn){
self::$dsn=$db['type'] . ':dbname=' . $db['db'] . ';host=' . $db['host'];
self::$user=$db['user'];
self::$pass=$db['password'];
}
2022-12-17 18:15:09 +05:00
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'){
2023-01-14 21:17:46 +05:00
// echo "lite";
2022-12-17 18:15:09 +05:00
if (!self::$dbh) {
try {
self::$dbh = new PDO('sqlite:' . 'bd/'. $_SERVER['SERVER_NAME'] . '.db' );
2023-01-14 21:17:46 +05:00
echo self::$dsn;
2022-12-17 18:15:09 +05:00
//Делаем бекап БД
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
}
2023-01-14 21:17:46 +05:00
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
}
?>