DSN based database connecting.

This commit is contained in:
flash 2023-10-12 15:52:45 +00:00
parent 4c2add3861
commit ada73c23bb

View file

@ -1,14 +1,8 @@
<?php
namespace Makai;
use ErrorException;
use Index\Autoloader;
use Index\Environment;
use Index\Data\ConnectionFailedException;
use Index\Data\MariaDB\MariaDBBackend;
use Index\Data\MariaDB\MariaDBConnectionInfo;
use Index\Data\NullDb\NullDbBackend;
use Index\Data\NullDb\NullDbConnectionInfo;
use Index\Data\DbTools;
define('MKI_STARTUP', microtime(true));
define('MKI_ROOT', __DIR__);
@ -25,27 +19,17 @@ require_once MKI_ROOT . '/vendor/autoload.php';
Environment::setDebug(MKI_DEBUG);
try {
$dbConfig = parse_ini_file(MKI_DIR_CONFIG . DIRECTORY_SEPARATOR . 'database.ini');
} catch(ErrorException $ex) {
$dbConfig = null;
$cfg = parse_ini_file(MKI_DIR_CONFIG . DIRECTORY_SEPARATOR . 'database.ini');
} catch(\ErrorException $ex) {
$cfg = null;
}
if(!empty($dbConfig))
try {
$db = (new MariaDBBackend)->createConnection(MariaDBConnectionInfo::create(
$dbConfig['host'] ?? '',
$dbConfig['user'] ?? '',
$dbConfig['pass'] ?? '',
$dbConfig['name'] ?? '',
$dbConfig['char'] ?? 'utf8mb4',
'SET SESSION time_zone = \'+00:00\', sql_mode = \'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION\'',
));
} catch(ConnectionFailedException $ex) {
//echo '<h3>Unable to connect to database</h3>';
//die($ex->getMessage());
$db = DbTools::create($cfg['dsn'] ?? 'null:');
} catch(\Index\Data\ConnectionFailedException $ex) {
$db = DbTools::create('null:');
}
if(empty($db))
$db = (new NullDbBackend)->createConnection(new NullDbConnectionInfo);
$db->execute('SET SESSION time_zone = \'+00:00\', sql_mode = \'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION\'');
$ctx = new MakaiContext($db);