Files
grocy/services/DatabaseService.php

72 lines
1.2 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-12 21:13:38 +02:00
use \Grocy\Services\ApplicationService;
2018-04-11 19:49:35 +02:00
2018-04-10 20:30:11 +02:00
class DatabaseService
{
2018-04-11 19:49:35 +02:00
private $DbConnectionRaw;
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
{
2018-04-11 19:49:35 +02:00
if ($this->DbConnectionRaw == null)
2018-04-10 20:30:11 +02:00
{
2018-07-24 19:41:35 +02:00
$pdo = new \PDO('sqlite:' . GROCY_DATAPATH . '/grocy.db');
2018-04-11 19:49:35 +02:00
$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
$this->DbConnectionRaw = $pdo;
2018-04-10 20:30:11 +02:00
}
2018-04-11 19:49:35 +02:00
return $this->DbConnectionRaw;
2018-04-10 20:30:11 +02:00
}
2018-04-11 19:49:35 +02:00
private $DbConnection;
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
{
2018-04-11 19:49:35 +02:00
if ($this->DbConnection == null)
2018-04-10 20:30:11 +02:00
{
2018-04-11 19:49:35 +02:00
$this->DbConnection = new \LessQL\Database($this->GetDbConnectionRaw());
2018-04-10 20:30:11 +02:00
}
2018-04-11 19:49:35 +02:00
return $this->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;
}
public function GetDbChangedTime()
{
return date('Y-m-d H:i:s', filemtime(GROCY_DATAPATH . '/grocy.db'));
}
2018-04-10 20:30:11 +02:00
}