Files
grocy/services/DatabaseService.php
T

113 lines
2.0 KiB
PHP
Raw Normal View History

2018-04-10 20:30:11 +02:00
<?php
2018-04-11 19:49:35 +02:00
namespace Grocy\Services;
2018-04-10 20:30:11 +02:00
class DatabaseService
{
2020-08-31 20:40:31 +02:00
private static $DbConnection = null;
2021-07-03 17:46:47 +02:00
2020-08-31 20:40:31 +02:00
private static $DbConnectionRaw = null;
2021-07-03 17:46:47 +02:00
2020-08-31 20:40:31 +02:00
private static $instance = null;
2020-08-31 20:40:31 +02:00
/**
* @return boolean|\PDOStatement
*/
public function ExecuteDbQuery(string $sql)
{
2020-08-31 20:40:31 +02:00
$pdo = $this->GetDbConnectionRaw();
if ($this->ExecuteDbStatement($sql) === true)
{
2020-08-31 20:40:31 +02:00
return $pdo->query($sql);
}
2020-08-31 20:40:31 +02:00
return false;
}
2018-04-10 20:30:11 +02:00
/**
2020-08-31 20:40:31 +02:00
* @return boolean
2018-04-10 20:30:11 +02:00
*/
2020-08-31 20:40:31 +02:00
public function ExecuteDbStatement(string $sql)
2018-04-10 20:30:11 +02:00
{
2020-08-31 20:40:31 +02:00
$pdo = $this->GetDbConnectionRaw();
if ($pdo->exec($sql) === false)
2018-04-10 20:30:11 +02:00
{
2020-08-31 20:40:31 +02:00
throw new Exception($pdo->errorInfo());
2018-04-10 20:30:11 +02:00
}
2020-08-31 20:40:31 +02:00
return true;
}
public function GetDbChangedTime()
{
return date('Y-m-d H:i:s', filemtime($this->GetDbFilePath()));
2018-04-10 20:30:11 +02:00
}
/**
2018-04-11 19:49:35 +02:00
* @return \LessQL\Database
2018-04-10 20:30:11 +02:00
*/
2018-04-11 19:49:35 +02:00
public function GetDbConnection()
2018-04-10 20:30:11 +02:00
{
if (self::$DbConnection == null)
2018-04-10 20:30:11 +02:00
{
self::$DbConnection = new \LessQL\Database($this->GetDbConnectionRaw());
2018-04-10 20:30:11 +02:00
}
return self::$DbConnection;
2018-04-10 20:30:11 +02:00
}
/**
2020-08-31 20:40:31 +02:00
* @return \PDO
2018-04-10 20:30:11 +02:00
*/
2020-08-31 20:40:31 +02:00
public function GetDbConnectionRaw()
2018-04-10 20:30:11 +02:00
{
2020-08-31 20:40:31 +02:00
if (self::$DbConnectionRaw == null)
2018-04-10 20:30:11 +02:00
{
2020-08-31 20:40:31 +02:00
$pdo = new \PDO('sqlite:' . $this->GetDbFilePath());
$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
$pdo->sqliteCreateFunction('regexp', function ($pattern, $value) {
mb_regex_encoding('UTF-8');
return (false !== mb_ereg($pattern, $value)) ? 1 : 0;
});
2020-08-31 20:40:31 +02:00
self::$DbConnectionRaw = $pdo;
2018-04-10 20:30:11 +02:00
}
2020-08-31 20:40:31 +02:00
return self::$DbConnectionRaw;
2018-04-10 20:30:11 +02:00
}
2020-08-31 20:40:31 +02:00
public function SetDbChangedTime($dateTime)
2018-04-10 20:30:11 +02:00
{
2020-08-31 20:40:31 +02:00
touch($this->GetDbFilePath(), strtotime($dateTime));
}
public static function getInstance()
{
if (self::$instance == null)
2018-04-10 20:30:11 +02:00
{
2020-08-31 20:40:31 +02:00
self::$instance = new self();
2018-04-10 20:30:11 +02:00
}
2020-08-31 20:40:31 +02:00
return self::$instance;
2018-04-10 20:30:11 +02:00
}
2020-08-31 20:40:31 +02:00
private function GetDbFilePath()
{
2020-08-31 20:40:31 +02:00
if (GROCY_MODE === 'demo' || GROCY_MODE === 'prerelease')
{
$dbSuffix = GROCY_DEFAULT_LOCALE;
if (defined('GROCY_DEMO_DB_SUFFIX'))
{
$dbSuffix = GROCY_DEMO_DB_SUFFIX;
}
return GROCY_DATAPATH . '/grocy_' . $dbSuffix . '.db';
2020-08-31 20:40:31 +02:00
}
2020-08-31 20:40:31 +02:00
return GROCY_DATAPATH . '/grocy.db';
}
2018-04-10 20:30:11 +02:00
}