Files
grocy/services/DatabaseService.php

131 lines
2.7 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;
use Grocy\Services\UsersService;
2023-05-13 14:43:51 +02:00
use LessQL\Database;
2018-04-10 20:30:11 +02:00
class DatabaseService
{
2020-08-31 20:40:31 +02:00
private static $DbConnection = null;
private static $DbConnectionRaw = null;
private static $instance = null;
Convert services to singletons and use lazy loading to improve performance (#479) * use singletons to reduce need to recreate the same objects * unable to make the constructor private * comment out debug printing to log file * correct typo of treating self() as a var instead of a function * utilise Localisation service as a singleton * fix errent line that should have been commented * remove phpinfo * correct mistake in stock controller * try storing app in apcu * serialise inside the app closures * get timings for db-changed-time * get timings for db-changed-time * store localisation service in apcu * stor translations in apcu instead of localisation service (due to database connection) * correct syntax error * forgot to uncomment instance map * correct indentation and variable out of scope * more timings for app execution time * try apc caching for views * correct scope for Pot variable * remove additional fopen * correct timings for app build time * correct timings for app object build time * correct timings for app route build time * get timings for routing timings * get more in depth timings for routing loading * fix more in depth timings for routing loading * start investigating session auth middleware creation * start investigating session auth middleware creation * start investigating Login controller time * start investigating Login controller time * in depth look at Logincontroller timings * comment out debug printing * lazily obtain valus for page rendering * correct syntax error * correct scope of variable * correct visibiity of methds inherited from BaseController * missing use for Userfieldsservice * lazy loading of open api spec * lazy loading of users service * lazy loading of batteries service * lazy loading of services in controllers * lazy loading of services in services * correct mistake * fix userservice * fix userservice * fix userfieldservice * fix chores service * fix calendar service * remove Dockerfile used for development * Remove docker compose file used for development * Clean up app.php * remove last diff * Clean up base controller * Clean up controllers * lean up middleware * Clean up and tuen all services into singletons * remove debug from routes.php * remove acpu from localisation * Complete removal of acpu from localisation * fixes for things broken * More fixes following merge * Fix for start up bug. Re factoring singleton code had brroken due to scope of clas var. * fix bug where getUsersService is declared twice * bug fixes following merge * bug fixes following merge * bug fixes following merge * bug fixes following merge * bug fixes following merge * Fix all the not working things... * Deleted off-topic files * Deleted off-topic files Co-authored-by: Bernd Bestel <bernd@berrnd.de>
2020-03-01 23:47:47 +07:00
2020-08-31 20:40:31 +02:00
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;
}
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 (GROCY_MODE === 'dev')
{
$logFilePath = GROCY_DATAPATH . '/sql.log';
if (file_exists($logFilePath))
{
file_put_contents($logFilePath, $sql . PHP_EOL, FILE_APPEND);
}
}
2020-08-31 20:40:31 +02:00
if ($pdo->exec($sql) === false)
2018-04-10 20:30:11 +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
public function GetDbConnection()
2018-04-10 20:30:11 +02:00
{
Convert services to singletons and use lazy loading to improve performance (#479) * use singletons to reduce need to recreate the same objects * unable to make the constructor private * comment out debug printing to log file * correct typo of treating self() as a var instead of a function * utilise Localisation service as a singleton * fix errent line that should have been commented * remove phpinfo * correct mistake in stock controller * try storing app in apcu * serialise inside the app closures * get timings for db-changed-time * get timings for db-changed-time * store localisation service in apcu * stor translations in apcu instead of localisation service (due to database connection) * correct syntax error * forgot to uncomment instance map * correct indentation and variable out of scope * more timings for app execution time * try apc caching for views * correct scope for Pot variable * remove additional fopen * correct timings for app build time * correct timings for app object build time * correct timings for app route build time * get timings for routing timings * get more in depth timings for routing loading * fix more in depth timings for routing loading * start investigating session auth middleware creation * start investigating session auth middleware creation * start investigating Login controller time * start investigating Login controller time * in depth look at Logincontroller timings * comment out debug printing * lazily obtain valus for page rendering * correct syntax error * correct scope of variable * correct visibiity of methds inherited from BaseController * missing use for Userfieldsservice * lazy loading of open api spec * lazy loading of users service * lazy loading of batteries service * lazy loading of services in controllers * lazy loading of services in services * correct mistake * fix userservice * fix userservice * fix userfieldservice * fix chores service * fix calendar service * remove Dockerfile used for development * Remove docker compose file used for development * Clean up app.php * remove last diff * Clean up base controller * Clean up controllers * lean up middleware * Clean up and tuen all services into singletons * remove debug from routes.php * remove acpu from localisation * Complete removal of acpu from localisation * fixes for things broken * More fixes following merge * Fix for start up bug. Re factoring singleton code had brroken due to scope of clas var. * fix bug where getUsersService is declared twice * bug fixes following merge * bug fixes following merge * bug fixes following merge * bug fixes following merge * bug fixes following merge * Fix all the not working things... * Deleted off-topic files * Deleted off-topic files Co-authored-by: Bernd Bestel <bernd@berrnd.de>
2020-03-01 23:47:47 +07:00
if (self::$DbConnection == null)
2018-04-10 20:30:11 +02:00
{
2023-05-13 14:43:51 +02:00
self::$DbConnection = new Database($this->GetDbConnectionRaw());
2018-04-10 20:30:11 +02:00
}
if (GROCY_MODE === 'dev')
{
$logFilePath = GROCY_DATAPATH . '/sql.log';
if (file_exists($logFilePath))
{
self::$DbConnection->setQueryCallback(function ($query, $params) use ($logFilePath)
{
file_put_contents($logFilePath, $query . ' #### ' . implode(';', $params) . PHP_EOL, FILE_APPEND);
});
}
}
Convert services to singletons and use lazy loading to improve performance (#479) * use singletons to reduce need to recreate the same objects * unable to make the constructor private * comment out debug printing to log file * correct typo of treating self() as a var instead of a function * utilise Localisation service as a singleton * fix errent line that should have been commented * remove phpinfo * correct mistake in stock controller * try storing app in apcu * serialise inside the app closures * get timings for db-changed-time * get timings for db-changed-time * store localisation service in apcu * stor translations in apcu instead of localisation service (due to database connection) * correct syntax error * forgot to uncomment instance map * correct indentation and variable out of scope * more timings for app execution time * try apc caching for views * correct scope for Pot variable * remove additional fopen * correct timings for app build time * correct timings for app object build time * correct timings for app route build time * get timings for routing timings * get more in depth timings for routing loading * fix more in depth timings for routing loading * start investigating session auth middleware creation * start investigating session auth middleware creation * start investigating Login controller time * start investigating Login controller time * in depth look at Logincontroller timings * comment out debug printing * lazily obtain valus for page rendering * correct syntax error * correct scope of variable * correct visibiity of methds inherited from BaseController * missing use for Userfieldsservice * lazy loading of open api spec * lazy loading of users service * lazy loading of batteries service * lazy loading of services in controllers * lazy loading of services in services * correct mistake * fix userservice * fix userservice * fix userfieldservice * fix chores service * fix calendar service * remove Dockerfile used for development * Remove docker compose file used for development * Clean up app.php * remove last diff * Clean up base controller * Clean up controllers * lean up middleware * Clean up and tuen all services into singletons * remove debug from routes.php * remove acpu from localisation * Complete removal of acpu from localisation * fixes for things broken * More fixes following merge * Fix for start up bug. Re factoring singleton code had brroken due to scope of clas var. * fix bug where getUsersService is declared twice * bug fixes following merge * bug fixes following merge * bug fixes following merge * bug fixes following merge * bug fixes following merge * Fix all the not working things... * Deleted off-topic files * Deleted off-topic files Co-authored-by: Bernd Bestel <bernd@berrnd.de>
2020-03-01 23:47:47 +07:00
return self::$DbConnection;
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->setAttribute(\PDO::ATTR_ORACLE_NULLS, \PDO::NULL_EMPTY_STRING);
$pdo->sqliteCreateFunction('regexp', function ($pattern, $value)
{
mb_regex_encoding('UTF-8');
return (false !== mb_ereg($pattern, $value)) ? 1 : 0;
});
$pdo->sqliteCreateFunction('grocy_user_setting', function ($value)
{
$usersService = new UsersService();
return $usersService->GetUserSetting(GROCY_USER_ID, $value);
});
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
}