Skip to content

[FEATURE, cool for readme/documentation!] Connect to multiple database  #702

Closed
@jjkirkpatrick

Description

Hi, i like the way databases are currently handled, however it is lacking the ability to connect to multiple databases, while some people deem this unnecessary as most people stick to one kind of connector(mysql, sqlsrv) and one database, it is a useful feature to be able to connect to multiple connectors and databases within the same script.

I am putting this here as it's not a necessary part and is more feature creep than anything. maybe some one will find it useful

class DatabaseFactory
{
private static $factory;
private $database;

public static function getFactory($fresh = false)
{
    ($fresh != true ?: self::$factory = '');

    (self::$factory ?: self::$factory = new DatabaseFactory());

    return self::$factory;
}

public function getConnection($Type, $database = null)
{
    if (!$this->database) {
        if ($Type == "MYSQL") {
            $options = array(PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING);
            (is_null($database) ? $db = Config::get('DB_NAME_MYSQL') : $db = $database);
            $this->database = new PDO(
                Config::get('DB_TYPE_MYSQL') . ':host=' . Config::get('DB_HOST_MYSQL') . ';dbname=' .
                $db . ';port=' . Config::get('DB_PORT_MYSQL') . ';charset=' . Config::get('DB_CHARSET_MYSQL'),
                Config::get('DB_USER_MYSQL'), Config::get('DB_PASS_MYSQL'), $options
            );

        } else if ($Type == "SQLSRV") {

            $options = array(PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING);
            (is_null($database) ? $db = Config::get('DB_NAME_SQLSRV') : $db = $database);
            $connectionString = 'sqlsrv:Server=' . Config::get('DB_HOST_SQLSRV') . ';Database=' . $db;
            $this->database = new PDO("$connectionString", Config::get('DB_USER_SQLSRV'), Config::get('DB_PASS_SQLSRV'), $options);
        }

    }
    return $this->database;
}
}

Usage

this is used pretty much exactly like the only way of doing it with a few extra parameters

$database = DatabaseFactory::getFactory()->getConnection('Connection type', 'Database');

$query = $database->prepare(...);
$query->execute();
$result = $query->fetchAll();

Example

class myClass
{
      public function myFunction1()
      {
        $database = DatabaseFactory::getFactory()->getConnection('MYSQL', 'Tabel1');

        $query = $database->prepare(...);
        $query->execute();
        $result = $query->fetchAll();
      }

      public function myFunction2()
      {
        $database = DatabaseFactory::getFactory(true)->getConnection('SQLSRV', 'myTable');

        $query = $database->prepare(...);
        $query->execute();
        $result = $query->fetchAll();

       // do things with data

        $database = DatabaseFactory::getFactory(true)->getConnection('Mysql', 'Tabel1');

        $query = $database->prepare(...);
        $query->execute();
        $result = $query->fetchAll();

      }


      public function myFunction3()
      {
        $database = DatabaseFactory::getFactory(true)->getConnection('MYSQL', 'Tabel3');

        $query = $database->prepare(...);
        $query->execute();
        $result = $query->fetchAll();
      }



}

DatabaseFactory::getFactory(true)

true needs to be set if you are changing the connector or database at any point, not doing so will result in the table/connector not being changed

->getConnection('MYSQL', 'Tabel3');

Connector type, table

leaving these blank will return the first database listed in the getConnection method. with the default database from config,

$database = DatabaseFactory::getFactory()->getConnection();

is completely valid and will return the exact same result as it currently does.

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions