Support multiple databases at same time.

This commit is contained in:
KJ 2024-05-12 03:47:09 -04:00
parent e294eaa7af
commit f9b2c678f7
2 changed files with 32 additions and 23 deletions

View File

@ -3,11 +3,6 @@
* Database - DuckBrain * Database - DuckBrain
* *
* Clase diseñada para crear y devolver una única instancia PDO (database). * Clase diseñada para crear y devolver una única instancia PDO (database).
* Hace uso de las siguientes constantes:
* DB_TYPE, DB_NAME, DB_HOST, DB_USER, DB_PASS
*
* Si DB_TYPE es sqlite, usará DB_NAME como el nombre del archivo sqlite.
* Además DB_USER y DB_PASS, no será necesariop que estén definidos.
* *
* @author KJ * @author KJ
* @website https://kj2.me * @website https://kj2.me
@ -21,7 +16,7 @@ use PDOException;
use Exception; use Exception;
class Database extends PDO { class Database extends PDO {
static private ?PDO $db = null; static private array $databases = [];
private function __construct() {} private function __construct() {}
@ -30,29 +25,34 @@ class Database extends PDO {
* *
* @return PDO * @return PDO
*/ */
static public function getInstance(): PDO static public function getInstance(
string $type = 'mysql',
string $host = 'localhost',
string $name = '',
string $user = '',
string $pass = '',
): PDO
{ {
if (is_null(self::$db)) { $key = $type.'/'.$host.'/'.$name.'/'.$user;
if (empty(static::$databases[$key])) {
if (DB_TYPE == 'sqlite') { if ($type == 'sqlite') {
$dsn = DB_TYPE .':'. DB_NAME; $dsn = $type .':'. $name;
!defined('DB_USER') && define('DB_USER', '');
!defined('DB_PASS') && define('DB_PASS', '');
} else } else
$dsn = DB_TYPE.':dbname='.DB_NAME.';host='.DB_HOST; $dsn = $type.':dbname='.$name.';host='.$host;
try { try {
self::$db = new PDO($dsn, DB_USER, DB_PASS); static::$databases[$key] = new PDO($dsn, $user, $pass);
} catch (PDOException $e) { } catch (PDOException $e) {
throw new Exception( throw new Exception(
'Error at connect to database: ' . $e->getMessage() 'Error at connect to database: ' . $e->getMessage()
); );
} }
self::$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); static::$databases[$key]->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
self::$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); static::$databases[$key]->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
} }
return self::$db; return static::$databases[$key];
} }
} }
?> ?>

View File

@ -3,7 +3,8 @@
* Model - DuckBrain * Model - DuckBrain
* *
* Modelo ORM para objetos que hagan uso de una base de datos. * Modelo ORM para objetos que hagan uso de una base de datos.
* Depende de Libs\Database. * Depende de Libs\Database y hace uso de las constantes
* DB_TYPE, DB_HOST, DB_NAME, DB_USER y DB_PASS.
* *
* @author KJ * @author KJ
* @website https://kj2.me * @website https://kj2.me
@ -30,7 +31,6 @@ class Model {
static protected array $forceSave = []; static protected array $forceSave = [];
static protected string $table; static protected string $table;
static protected string $tableSufix = 's'; static protected string $tableSufix = 's';
static protected ?PDO $db = null;
static protected array $queryVars = []; static protected array $queryVars = [];
static protected array $querySelect = [ static protected array $querySelect = [
'select' => ['*'], 'select' => ['*'],
@ -51,10 +51,19 @@ class Model {
*/ */
protected static function db(): PDO protected static function db(): PDO
{ {
if (is_null(static::$db)) if (DB_TYPE == 'sqlite')
static::$db = Database::getInstance(); return Database::getInstance(
type: DB_TYPE,
return static::$db; name: DB_NAME
);
else
return Database::getInstance(
DB_TYPE,
DB_HOST,
DB_NAME,
DB_USER,
DB_PASS
);
} }
/** /**