2017-04-15 23:16:20 +02:00
< ? php
class Grocy
{
private static $DbConnectionRaw ;
/**
* @ return PDO
*/
2017-04-21 11:52:24 +02:00
public static function GetDbConnectionRaw ( $doMigrations = false )
2017-04-15 23:16:20 +02:00
{
2017-04-21 11:52:24 +02:00
if ( $doMigrations === true )
{
self :: $DbConnectionRaw = null ;
}
2017-04-15 23:16:20 +02:00
if ( self :: $DbConnectionRaw == null )
{
2017-04-16 23:11:03 +02:00
$pdo = new PDO ( 'sqlite:' . __DIR__ . '/data/grocy.db' );
$pdo -> setAttribute ( PDO :: ATTR_ERRMODE , PDO :: ERRMODE_EXCEPTION );
2017-04-15 23:16:20 +02:00
2017-04-21 11:52:24 +02:00
if ( $doMigrations === true )
2017-04-15 23:16:20 +02:00
{
2017-04-21 11:52:24 +02:00
Grocy :: ExecuteDbStatement ( $pdo , " CREATE TABLE IF NOT EXISTS migrations (migration INTEGER NOT NULL UNIQUE, execution_time_timestamp DATETIME DEFAULT (datetime('now', 'localtime')), PRIMARY KEY(migration)) WITHOUT ROWID " );
2017-04-16 23:11:03 +02:00
GrocyDbMigrator :: MigrateDb ( $pdo );
2017-04-15 23:16:20 +02:00
if ( self :: IsDemoInstallation ())
{
2017-04-16 23:11:03 +02:00
GrocyDemoDataGenerator :: PopulateDemoData ( $pdo );
2017-04-15 23:16:20 +02:00
}
}
self :: $DbConnectionRaw = $pdo ;
}
return self :: $DbConnectionRaw ;
}
2017-04-17 16:51:49 +02:00
private static $DbConnection ;
2017-04-15 23:16:20 +02:00
/**
* @ return LessQL\Database
*/
2017-04-21 11:52:24 +02:00
public static function GetDbConnection ( $doMigrations = false )
2017-04-15 23:16:20 +02:00
{
2017-04-21 11:52:24 +02:00
if ( $doMigrations === true )
{
self :: $DbConnection = null ;
}
2017-04-15 23:16:20 +02:00
if ( self :: $DbConnection == null )
{
2017-04-21 11:52:24 +02:00
self :: $DbConnection = new LessQL\Database ( self :: GetDbConnectionRaw ( $doMigrations ));
2017-04-15 23:16:20 +02:00
}
return self :: $DbConnection ;
}
2017-04-21 12:30:08 +02:00
/**
* @ return boolean
*/
2017-04-21 11:52:24 +02:00
public static function ExecuteDbStatement ( PDO $pdo , string $sql )
{
if ( $pdo -> exec ( utf8_encode ( $sql )) === false )
{
throw new Exception ( $pdo -> errorInfo ());
}
return true ;
}
2017-04-21 12:30:08 +02:00
/**
* @ return boolean | PDOStatement
*/
2017-04-21 11:52:24 +02:00
public static function ExecuteDbQuery ( PDO $pdo , string $sql )
{
if ( self :: ExecuteDbStatement ( $pdo , $sql ) === true )
{
return $pdo -> query ( utf8_encode ( $sql ));
}
return false ;
}
2017-04-21 12:30:08 +02:00
/**
* @ return boolean
*/
2017-04-15 23:16:20 +02:00
public static function IsDemoInstallation ()
{
2017-04-16 23:11:03 +02:00
return file_exists ( __DIR__ . '/data/demo.txt' );
2017-04-15 23:16:20 +02:00
}
2017-04-17 16:51:49 +02:00
private static $InstalledVersion ;
2017-04-21 12:30:08 +02:00
/**
* @ return string
*/
2017-04-17 16:51:49 +02:00
public static function GetInstalledVersion ()
{
if ( self :: $InstalledVersion == null )
{
self :: $InstalledVersion = file_get_contents ( __DIR__ . '/version.txt' );
}
return self :: $InstalledVersion ;
}
2017-04-15 23:16:20 +02:00
}