2018-04-10 20:30:11 +02:00
|
|
|
<?php
|
|
|
|
|
2018-04-11 19:49:35 +02:00
|
|
|
namespace Grocy\Services;
|
|
|
|
|
2020-03-01 23:47:47 +07:00
|
|
|
#use \Grocy\Services\ApplicationService;
|
2018-04-11 19:49:35 +02:00
|
|
|
|
2018-04-10 20:30:11 +02:00
|
|
|
class DatabaseService
|
|
|
|
{
|
2020-03-01 23:47:47 +07:00
|
|
|
private static $instance = null;
|
|
|
|
|
|
|
|
public static function getInstance()
|
|
|
|
{
|
|
|
|
if (self::$instance == null)
|
|
|
|
{
|
|
|
|
self::$instance = new self();
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::$instance;
|
|
|
|
}
|
|
|
|
|
2019-09-21 15:07:29 +02:00
|
|
|
private function GetDbFilePath()
|
|
|
|
{
|
|
|
|
if (GROCY_MODE === 'demo' || GROCY_MODE === 'prerelease')
|
|
|
|
{
|
2020-08-31 19:11:51 +02:00
|
|
|
return GROCY_DATAPATH . '/grocy_' . GROCY_DEFAULT_LOCALE . '.db';
|
2019-09-21 15:07:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return GROCY_DATAPATH . '/grocy.db';
|
|
|
|
}
|
|
|
|
|
2020-08-29 16:41:27 +02:00
|
|
|
private static $DbConnectionRaw = null;
|
2018-04-10 20:30:11 +02:00
|
|
|
/**
|
2018-04-11 19:49:35 +02:00
|
|
|
* @return \PDO
|
2018-04-10 20:30:11 +02:00
|
|
|
*/
|
2018-04-11 19:49:35 +02:00
|
|
|
public function GetDbConnectionRaw()
|
2018-04-10 20:30:11 +02:00
|
|
|
{
|
2020-03-01 23:47:47 +07:00
|
|
|
if (self::$DbConnectionRaw == null)
|
2018-04-10 20:30:11 +02:00
|
|
|
{
|
2019-09-21 15:07:29 +02:00
|
|
|
$pdo = new \PDO('sqlite:' . $this->GetDbFilePath());
|
2018-04-11 19:49:35 +02:00
|
|
|
$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
|
2020-03-01 23:47:47 +07:00
|
|
|
self::$DbConnectionRaw = $pdo;
|
2018-04-10 20:30:11 +02:00
|
|
|
}
|
|
|
|
|
2020-03-01 23:47:47 +07:00
|
|
|
return self::$DbConnectionRaw;
|
2018-04-10 20:30:11 +02:00
|
|
|
}
|
|
|
|
|
2020-03-01 23:47:47 +07:00
|
|
|
private static $DbConnection = null;
|
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
|
|
|
{
|
2020-03-01 23:47:47 +07:00
|
|
|
if (self::$DbConnection == null)
|
2018-04-10 20:30:11 +02:00
|
|
|
{
|
2020-03-01 23:47:47 +07:00
|
|
|
self::$DbConnection = new \LessQL\Database($this->GetDbConnectionRaw());
|
2018-04-10 20:30:11 +02:00
|
|
|
}
|
|
|
|
|
2020-03-01 23:47:47 +07:00
|
|
|
return self::$DbConnection;
|
2018-04-10 20:30:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2018-04-11 19:49:35 +02:00
|
|
|
public function ExecuteDbStatement(string $sql)
|
2018-04-10 20:30:11 +02:00
|
|
|
{
|
2018-04-11 19:49:35 +02:00
|
|
|
$pdo = $this->GetDbConnectionRaw();
|
2018-04-10 20:30:11 +02:00
|
|
|
if ($pdo->exec($sql) === false)
|
|
|
|
{
|
|
|
|
throw new Exception($pdo->errorInfo());
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-04-11 19:49:35 +02:00
|
|
|
* @return boolean|\PDOStatement
|
2018-04-10 20:30:11 +02:00
|
|
|
*/
|
2018-04-11 19:49:35 +02:00
|
|
|
public function ExecuteDbQuery(string $sql)
|
2018-04-10 20:30:11 +02:00
|
|
|
{
|
2018-04-11 19:49:35 +02:00
|
|
|
$pdo = $this->GetDbConnectionRaw();
|
|
|
|
if ($this->ExecuteDbStatement($sql) === true)
|
2018-04-10 20:30:11 +02:00
|
|
|
{
|
|
|
|
return $pdo->query($sql);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2018-09-24 13:53:18 +02:00
|
|
|
|
|
|
|
public function GetDbChangedTime()
|
|
|
|
{
|
2019-09-21 15:07:29 +02:00
|
|
|
return date('Y-m-d H:i:s', filemtime($this->GetDbFilePath()));
|
2018-09-24 13:53:18 +02:00
|
|
|
}
|
2019-09-17 17:49:58 +02:00
|
|
|
|
|
|
|
public function SetDbChangedTime($dateTime)
|
|
|
|
{
|
2019-09-21 15:07:29 +02:00
|
|
|
touch($this->GetDbFilePath(), strtotime($dateTime));
|
2019-09-17 17:49:58 +02:00
|
|
|
}
|
2018-04-10 20:30:11 +02:00
|
|
|
}
|