Skip to content

Commit

Permalink
Multiple db connection support
Browse files Browse the repository at this point in the history
  • Loading branch information
Turan Karatug authored and Turan Karatug committed Jul 10, 2019
1 parent 551650c commit 26f3b9a
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 44 deletions.
20 changes: 11 additions & 9 deletions App/Config/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
*************************************************/
return [

'db_driver' => 'mysql',
'db_host' => 'localhost',
'db_user' => '',
'db_pass' => '',
'db_name' => '',
'db_charset' => 'utf8',
'db_collation' => 'utf8_general_ci',
'db_prefix' => ''
'primary' => [
'db_driver' => 'mysql',
'db_host' => 'localhost',
'db_user' => '',
'db_pass' => '',
'db_name' => '',
'db_charset' => 'utf8',
'db_collation' => 'utf8_general_ci',
'db_prefix' => ''
],

];
];
99 changes: 64 additions & 35 deletions System/Libs/Database/DB.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

class DB
{
// Default connection
protected $con = 'primary';

// PDO instance
public $pdo = null;

Expand Down Expand Up @@ -72,46 +75,72 @@ class DB
/**
* Initializing
*
* @return object
*/
* @throws \Exception
*/
public function __construct()
{
// Getting db config items
$this->config = config('database');

$this->config['db_driver'] = ($this->config['db_driver']) ? $this->config['db_driver'] : 'mysql';
$this->config['db_host'] = ($this->config['db_host']) ? $this->config['db_host'] : 'localhost';
$this->config['db_charset'] = ($this->config['db_charset']) ? $this->config['db_charset'] : 'utf8';
$this->config['db_collation'] = ($this->config['db_collation']) ? $this->config['db_collation'] : 'utf8_general_ci';
$this->config['db_prefix'] = ($this->config['db_prefix']) ? $this->config['db_prefix'] : '';

// Setting prefix
$this->prefix = $this->config['db_prefix'];

$dsn = '';
// Setting connection string
if ($this->config['db_driver'] == 'mysql' || $this->config['db_driver'] == 'pgsql' || $this->config['db_driver'] == '') {
$dsn = $this->config['db_driver'] . ':host=' . $this->config['db_host'] . ';dbname=' . $this->config['db_name'];
} elseif ($this->config['db_driver'] == 'sqlite') {
$dsn = 'sqlite:' . $this->config['db_name'];
} elseif ($this->config['db_driver'] == 'oracle') {
$dsn = 'oci:dbname=' . $this->config['db_host'] . '/' . $this->config['db_name'];
}
$this->config = config('database.' . $this->con);

// Connecting to server
try
{
$this->pdo = new PDO($dsn, $this->config['db_user'], $this->config['db_pass']);
$this->pdo->exec("SET NAMES '" . $this->config['db_charset'] . "' COLLATE '" . $this->config['db_collation'] . "'");
$this->pdo->exec("SET CHARACTER SET '" . $this->config['db_charset'] . "'");
$this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
}
catch(PDOException $e)
{
throw new ExceptionHandler("DB error", "Can not connect to Database with PDO.<br><br>" . $e->getMessage());
}
$this->connect();
}

/**
* Select a connection
*
* @param string $connection
* @return $this
* @throws \Exception
*/
public function connection($connection)
{
$this->con = $connection;
$this->connect();

return $this;
}

return $this->pdo;
/**
* Connect
*
* @return PDO|null
* @throws \Exception
*/
public function connect()
{
$this->config['db_driver'] = ($this->config['db_driver']) ? $this->config['db_driver'] : 'mysql';
$this->config['db_host'] = ($this->config['db_host']) ? $this->config['db_host'] : 'localhost';
$this->config['db_charset'] = ($this->config['db_charset']) ? $this->config['db_charset'] : 'utf8';
$this->config['db_collation'] = ($this->config['db_collation']) ? $this->config['db_collation'] : 'utf8_general_ci';
$this->config['db_prefix'] = ($this->config['db_prefix']) ? $this->config['db_prefix'] : '';

// Setting prefix
$this->prefix = $this->config['db_prefix'];

$dsn = '';
// Setting connection string
if ($this->config['db_driver'] == 'mysql' || $this->config['db_driver'] == 'pgsql' || $this->config['db_driver'] == '') {
$dsn = $this->config['db_driver'] . ':host=' . $this->config['db_host'] . ';dbname=' . $this->config['db_name'];
} elseif ($this->config['db_driver'] == 'sqlite') {
$dsn = 'sqlite:' . $this->config['db_name'];
} elseif ($this->config['db_driver'] == 'oracle') {
$dsn = 'oci:dbname=' . $this->config['db_host'] . '/' . $this->config['db_name'];
}

// Connecting to server
try
{
$this->pdo = new PDO($dsn, $this->config['db_user'], $this->config['db_pass']);
$this->pdo->exec("SET NAMES '" . $this->config['db_charset'] . "' COLLATE '" . $this->config['db_collation'] . "'");
$this->pdo->exec("SET CHARACTER SET '" . $this->config['db_charset'] . "'");
$this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
}
catch(PDOException $e)
{
throw new ExceptionHandler("DB error", "Can not connect to Database with PDO.<br><br>" . $e->getMessage());
}

return $this->pdo;
}

/**
Expand Down

0 comments on commit 26f3b9a

Please sign in to comment.