mirror of
https://port.numenaute.org/aleajactaest/khanat-opennel-code.git
synced 2024-11-27 01:26:30 +00:00
Re-use the database connection
This commit is contained in:
parent
6acf67d03b
commit
6fed8bad5b
1 changed files with 261 additions and 268 deletions
|
@ -28,6 +28,9 @@
|
||||||
* @author Daan Janssens, mentored by Matthew Lagoe
|
* @author Daan Janssens, mentored by Matthew Lagoe
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
$PDOCache = array();
|
||||||
|
|
||||||
class DBLayer {
|
class DBLayer {
|
||||||
|
|
||||||
private $PDO;
|
private $PDO;
|
||||||
|
@ -42,10 +45,12 @@ class DBLayer {
|
||||||
* @param $db String, the name of the databases entry in the $cfg global var.
|
* @param $db String, the name of the databases entry in the $cfg global var.
|
||||||
* @param $dbn String, the name of the databases entry in the $cfg global var if $db referenced to an action(install etc).
|
* @param $dbn String, the name of the databases entry in the $cfg global var if $db referenced to an action(install etc).
|
||||||
*/
|
*/
|
||||||
function __construct( $db, $dbn = null )
|
function __construct($db, $dbn = null)
|
||||||
{
|
{
|
||||||
if ( $db != "install" ) {
|
global $PDOCache;
|
||||||
|
if (isset($PDOCache[$db])) {
|
||||||
|
$this->PDO = $PDOCache[$db];
|
||||||
|
} else {
|
||||||
global $cfg;
|
global $cfg;
|
||||||
$dsn = "mysql:";
|
$dsn = "mysql:";
|
||||||
$dsn .= "host=" . $cfg['db'][$db]['host'] . ";";
|
$dsn .= "host=" . $cfg['db'][$db]['host'] . ";";
|
||||||
|
@ -53,23 +58,12 @@ class DBLayer {
|
||||||
$dsn .= "port=" . $cfg['db'][$db]['port'] . ";";
|
$dsn .= "port=" . $cfg['db'][$db]['port'] . ";";
|
||||||
|
|
||||||
$opt = array(
|
$opt = array(
|
||||||
PDO :: ATTR_ERRMODE => PDO :: ERRMODE_EXCEPTION,
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
||||||
PDO :: ATTR_DEFAULT_FETCH_MODE => PDO :: FETCH_ASSOC
|
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
|
||||||
);
|
);
|
||||||
$this -> PDO = new PDO( $dsn, $cfg['db'][$db]['user'], $cfg['db'][$db]['pass'], $opt );
|
$this->PDO = new PDO($dsn, $cfg['db'][$db]['user'], $cfg['db'][$db]['pass'], $opt);
|
||||||
} else {
|
$PDOCache[$db] = $this->PDO;
|
||||||
global $cfg;
|
|
||||||
$dsn = "mysql:";
|
|
||||||
$dsn .= "host=" . $cfg['db'][$dbn]['host'] . ";";
|
|
||||||
$dsn .= "port=" . $cfg['db'][$dbn]['port'] . ";";
|
|
||||||
|
|
||||||
$opt = array(
|
|
||||||
PDO :: ATTR_ERRMODE => PDO :: ERRMODE_EXCEPTION,
|
|
||||||
PDO :: ATTR_DEFAULT_FETCH_MODE => PDO :: FETCH_ASSOC
|
|
||||||
);
|
|
||||||
$this -> PDO = new PDO( $dsn, $_POST['Username'], $_POST['Password'], $opt );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -78,9 +72,9 @@ class DBLayer {
|
||||||
* @param $query the mysql query.
|
* @param $query the mysql query.
|
||||||
* @return returns a PDOStatement object.
|
* @return returns a PDOStatement object.
|
||||||
*/
|
*/
|
||||||
public function executeWithoutParams( $query ) {
|
public function executeWithoutParams($query) {
|
||||||
$statement = $this -> PDO -> prepare( $query );
|
$statement = $this->PDO->prepare($query);
|
||||||
$statement -> execute();
|
$statement->execute();
|
||||||
return $statement;
|
return $statement;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -145,8 +139,7 @@ class DBLayer {
|
||||||
$sth -> execute( $data );
|
$sth -> execute( $data );
|
||||||
$this -> PDO -> commit();
|
$this -> PDO -> commit();
|
||||||
}
|
}
|
||||||
catch( Exception $e )
|
catch ( Exception $e ) {
|
||||||
{
|
|
||||||
$this -> PDO -> rollBack();
|
$this -> PDO -> rollBack();
|
||||||
throw new Exception( "error selection" );
|
throw new Exception( "error selection" );
|
||||||
return false;
|
return false;
|
||||||
|
|
Loading…
Reference in a new issue