Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
jjkirkpatrick opened this issue Aug 19, 2015 · 2 comments
Closed

Comments

@jjkirkpatrick
Copy link
Contributor

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.

@panique
Copy link
Owner

panique commented Sep 7, 2015

I really like the idea and your code, but as this is only something for the really hardcore users I would like to add this to the upcoming large README, maybe under a section called "optional features (with working code examples)".

Gimme some weeks for this :)

@panique panique changed the title [Feature] Connect to multiple database [FEATURE, cool for readme/documentation!] Connect to multiple database Sep 7, 2015
@panique
Copy link
Owner

panique commented Oct 11, 2015

He, as this is a feature for very advanced users I'would kindly move this idea to the readme under "feature ideas" and link it from there, so people can still find it (and close this ticket as it's not really a ToDo). Hope that's okay for you :)

@panique panique closed this as completed Oct 11, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants