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 <?php
namespace Makai; namespace Makai;
use ErrorException;
use Index\Autoloader;
use Index\Environment; use Index\Environment;
use Index\Data\ConnectionFailedException; use Index\Data\DbTools;
use Index\Data\MariaDB\MariaDBBackend;
use Index\Data\MariaDB\MariaDBConnectionInfo;
use Index\Data\NullDb\NullDbBackend;
use Index\Data\NullDb\NullDbConnectionInfo;
define('MKI_STARTUP', microtime(true)); define('MKI_STARTUP', microtime(true));
define('MKI_ROOT', __DIR__); define('MKI_ROOT', __DIR__);
@ -25,27 +19,17 @@ require_once MKI_ROOT . '/vendor/autoload.php';
Environment::setDebug(MKI_DEBUG); Environment::setDebug(MKI_DEBUG);
try { try {
$dbConfig = parse_ini_file(MKI_DIR_CONFIG . DIRECTORY_SEPARATOR . 'database.ini'); $cfg = parse_ini_file(MKI_DIR_CONFIG . DIRECTORY_SEPARATOR . 'database.ini');
} catch(ErrorException $ex) { } catch(\ErrorException $ex) {
$dbConfig = null; $cfg = null;
} }
if(!empty($dbConfig)) try {
try { $db = DbTools::create($cfg['dsn'] ?? 'null:');
$db = (new MariaDBBackend)->createConnection(MariaDBConnectionInfo::create( } catch(\Index\Data\ConnectionFailedException $ex) {
$dbConfig['host'] ?? '', $db = DbTools::create('null:');
$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());
}
if(empty($db)) $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\'');
$db = (new NullDbBackend)->createConnection(new NullDbConnectionInfo);
$ctx = new MakaiContext($db); $ctx = new MakaiContext($db);