grocy/services/DatabaseService.php
2020-08-31 20:40:31 +02:00

104 lines
1.8 KiB
PHP

<?php
namespace Grocy\Services;
#use \Grocy\Services\ApplicationService;
class DatabaseService
{
private static $DbConnection = null;
private static $DbConnectionRaw = null;
private static $instance = null;
/**
* @return boolean|\PDOStatement
*/
public function ExecuteDbQuery(string $sql)
{
$pdo = $this->GetDbConnectionRaw();
if ($this->ExecuteDbStatement($sql) === true)
{
return $pdo->query($sql);
}
return false;
}
/**
* @return boolean
*/
public function ExecuteDbStatement(string $sql)
{
$pdo = $this->GetDbConnectionRaw();
if ($pdo->exec($sql) === false)
{
throw new Exception($pdo->errorInfo());
}
return true;
}
public function GetDbChangedTime()
{
return date('Y-m-d H:i:s', filemtime($this->GetDbFilePath()));
}
/**
* @return \LessQL\Database
*/
public function GetDbConnection()
{
if (self::$DbConnection == null)
{
self::$DbConnection = new \LessQL\Database($this->GetDbConnectionRaw());
}
return self::$DbConnection;
}
/**
* @return \PDO
*/
public function GetDbConnectionRaw()
{
if (self::$DbConnectionRaw == null)
{
$pdo = new \PDO('sqlite:' . $this->GetDbFilePath());
$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
self::$DbConnectionRaw = $pdo;
}
return self::$DbConnectionRaw;
}
public function SetDbChangedTime($dateTime)
{
touch($this->GetDbFilePath(), strtotime($dateTime));
}
public static function getInstance()
{
if (self::$instance == null)
{
self::$instance = new self();
}
return self::$instance;
}
private function GetDbFilePath()
{
if (GROCY_MODE === 'demo' || GROCY_MODE === 'prerelease')
{
return GROCY_DATAPATH . '/grocy_' . GROCY_DEFAULT_LOCALE . '.db';
}
return GROCY_DATAPATH . '/grocy.db';
}
}