Created
December 1, 2017 18:23
-
-
Save rudwolf/5a8ebe44157fad355df7981f7d9a399f to your computer and use it in GitHub Desktop.
app/Traits/DbTraits.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Trait to handle commom function used by DynamicController or anything that needs to access or run query in any Database | |
*/ | |
namespace App\Traits; | |
use Config; | |
use Db; | |
//use Illuminate\Support\Facades\DB; | |
trait DbTraits { | |
private $handleAs = 'default'; | |
private $config = []; | |
/** | |
* Define the connection settings | |
* | |
* @param $config array driver, host, database (database name), username and password | |
* of the target database | |
* | |
* @return array merged options with default parameters | |
*/ | |
private function setConfig($config) | |
{ | |
$defaultOptions = array( | |
'driver' => 'mysql', | |
'host'=> '127.0.0.1', | |
'database' => '', | |
'username' => NULL, | |
'password' => NULL | |
); | |
$options = array_merge($defaultOptions, $config); | |
$this->config = $options; | |
} | |
/** | |
* Sets the current connection to the database you want to connect to | |
* | |
* @param $nameKey string Connection name to define config details | |
* @param $config array driver, host, database (database name), username and password | |
* of the target database | |
* | |
*/ | |
private function DynDBConnect($nameKey, $config) | |
{ | |
//config(['database.connections.'.$nameKey => $this->setConfig($config)]); | |
$cfgset = Config::set('database.connections.'.$nameKey, $this->setConfig($config) ); | |
$current_config = [ | |
'config_name' => 'database.connections.'.$nameKey, | |
'set' => $cfgset, | |
'config' => Config::get('database.connections.'.$nameKey) | |
]; | |
dd( $current_config ); | |
// | |
$this->currentConnection = DB::connection($nameKey); | |
//return $this->currentConnection; | |
} | |
/** | |
* Closes connection | |
* | |
* @param $nameKey string Connection name to be closed | |
* | |
*/ | |
private function DynDBClose($nameKey) { | |
if (isset($this->currentConnection) || get_class($this->currentConnection->getPdo()) == 'PDO') | |
$this->currentConnection = NULL; | |
DB::disconnect($nameKey); | |
} | |
/** | |
* Closes a dynamic connection | |
* | |
* @param $nameKey DB id/name | |
*/ | |
public function endDbInstance($nameKey) { | |
$this->DynDBClose($nameKey); | |
} | |
/** | |
* Runs query and returns a dataset | |
* @param array $config Database configuration details | |
* @param string $qryString Database query to be ran | |
* @param boolean $cache True or false if query will be cached | |
* @param integer $timeout Time in minutes to cache query, defaults to 3 minutes | |
* @return object Dataset containing results for query | |
*/ | |
public function qryRunner($config, $qryString, $cache = true, $timeout = 3) | |
{ | |
$this->setConfig($config); | |
if ( empty($this->config['database']) ) | |
return false; | |
if ($cache) { | |
return $this->cacheQuery($qryString, $timeout); | |
} else { | |
return $this->qryRun($qryString); | |
} | |
} | |
private function cacheQuery($qryString, $timeout = 3) | |
{ | |
return \Cache::remember(md5($qryString), $timeout, function() use ($qryString) { | |
return $this->qryRun($qryString); | |
}); | |
} | |
/** | |
* Runs DB Query | |
* @param string $qryString Database query to be ran | |
* @return object Dataset containing results for query | |
*/ | |
private function qryRun($qryString) | |
{ | |
$connection_id = $this->config['database']; | |
$this->DynDBConnect($connection_id, $this->config); | |
// needs a active connection, if not found, returns null | |
if (!isset($this->currentConnection) || !get_class($this->currentConnection->getPdo()) == 'PDO') | |
return false; | |
$result = $this->currentConnection->select($qryString); | |
$this->DynDBClose($connection_id); | |
return $result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment