Re-use the database connection

This commit is contained in:
kaetemi 2014-09-02 20:39:07 +02:00
parent 6acf67d03b
commit 6fed8bad5b

View file

@ -5,270 +5,263 @@
* *
* --> First create an object of dblayer --> $db = new DBLayer('short database name used in config') * --> First create an object of dblayer --> $db = new DBLayer('short database name used in config')
* *
* --> Insert --> $db->insert( $tb_name, $data ) * --> Insert --> $db->insert( $tb_name, $data )
* $tb_name = table name in which we want to insert data * $tb_name = table name in which we want to insert data
* $data = array of data that needs to be inserted in format('fieldname' => $value) where fieldname must be a field in that table. * $data = array of data that needs to be inserted in format('fieldname' => $value) where fieldname must be a field in that table.
* *
* --> select --> $db->select( $tb_name, $data, $where ) * --> select --> $db->select( $tb_name, $data, $where )
* $tb_name = table name which we want to select * $tb_name = table name which we want to select
* $data = array of data which is then required in WHERE clause in format array('fieldname'=>$value) fieldname must be a field in that table. * $data = array of data which is then required in WHERE clause in format array('fieldname'=>$value) fieldname must be a field in that table.
* $where = string in format ('fieldname=:fieldname') where :fieldname takes it's value from $data array. * $where = string in format ('fieldname=:fieldname') where :fieldname takes it's value from $data array.
* *
* --> update --> $db->update( $tb_name, $data, $where ) * --> update --> $db->update( $tb_name, $data, $where )
* $tb_name = table name which we want to update * $tb_name = table name which we want to update
* $data = array of data which contains the filelds that need to be updated with their values in the format('fieldname' => $value,...) where fieldname must be a field in that table. * $data = array of data which contains the filelds that need to be updated with their values in the format('fieldname' => $value,...) where fieldname must be a field in that table.
* $where = string contains the filename with a value at that field in the format ('fieldname = $value') where fieldname must be a field in that table and $value is value respect to that field. * $where = string contains the filename with a value at that field in the format ('fieldname = $value') where fieldname must be a field in that table and $value is value respect to that field.
* *
* --> delete --> $db->delete( $tb_name, $data, $where ) * --> delete --> $db->delete( $tb_name, $data, $where )
* $tb_name = table name where we want to delete. * $tb_name = table name where we want to delete.
* $data = array of data which is then required in WHERE clause in format array('fieldname'=> $value) where fieldname must be a field in that table. * $data = array of data which is then required in WHERE clause in format array('fieldname'=> $value) where fieldname must be a field in that table.
* $where = string in format ('fieldname=:fieldname') where :fieldname takes it's value from $data array. * $where = string in format ('fieldname=:fieldname') where :fieldname takes it's value from $data array.
* *
* *
* @author Daan Janssens, mentored by Matthew Lagoe * @author Daan Janssens, mentored by Matthew Lagoe
* *
*/ */
$PDOCache = array();
class DBLayer { class DBLayer {
private $PDO; private $PDO;
/** /**
* The PDO object, instantiated by the constructor * The PDO object, instantiated by the constructor
*/ */
/** /**
* The constructor. * The constructor.
* Instantiates the PDO object attribute by connecting to the arguments matching database(the db info is stored in the $cfg global var) * Instantiates the PDO object attribute by connecting to the arguments matching database(the db info is stored in the $cfg global var)
* *
* @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;
$dsn = "mysql:";
$dsn .= "host=" . $cfg['db'][$db]['host'] . ";";
$dsn .= "dbname=" . $cfg['db'][$db]['name'] . ";";
$dsn .= "port=" . $cfg['db'][$db]['port'] . ";";
global $cfg; $opt = array(
$dsn = "mysql:"; PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
$dsn .= "host=" . $cfg['db'][$db]['host'] . ";"; PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
$dsn .= "dbname=" . $cfg['db'][$db]['name'] . ";"; );
$dsn .= "port=" . $cfg['db'][$db]['port'] . ";"; $this->PDO = new PDO($dsn, $cfg['db'][$db]['user'], $cfg['db'][$db]['pass'], $opt);
$PDOCache[$db] = $this->PDO;
}
}
$opt = array( /**
PDO :: ATTR_ERRMODE => PDO :: ERRMODE_EXCEPTION, * Execute a query that doesn't have any parameters.
PDO :: ATTR_DEFAULT_FETCH_MODE => PDO :: FETCH_ASSOC *
); * @param $query the mysql query.
$this -> PDO = new PDO( $dsn, $cfg['db'][$db]['user'], $cfg['db'][$db]['pass'], $opt ); * @return returns a PDOStatement object.
} else { */
global $cfg; public function executeWithoutParams($query) {
$dsn = "mysql:"; $statement = $this->PDO->prepare($query);
$dsn .= "host=" . $cfg['db'][$dbn]['host'] . ";"; $statement->execute();
$dsn .= "port=" . $cfg['db'][$dbn]['port'] . ";"; return $statement;
}
$opt = array( /**
PDO :: ATTR_ERRMODE => PDO :: ERRMODE_EXCEPTION, * Execute a query that has parameters.
PDO :: ATTR_DEFAULT_FETCH_MODE => PDO :: FETCH_ASSOC *
); * @param $query the mysql query.
$this -> PDO = new PDO( $dsn, $_POST['Username'], $_POST['Password'], $opt ); * @param $params the parameters that are being used by the query.
} * @return returns a PDOStatement object.
*/
public function execute( $query, $params ) {
$statement = $this -> PDO -> prepare( $query );
$statement -> execute( $params );
return $statement;
}
} /**
* Insert function which returns id of the inserting field.
*
* @param $tb_name table name where we want to insert data.
* @param $data the parameters that are being inserted into table.
* @return returns the id of the last inserted element.
*/
public function executeReturnId( $tb_name, $data ) {
$field_values = ':' . implode( ',:', array_keys( $data ) );
$field_options = implode( ',', array_keys( $data ) );
try {
$sth = $this -> PDO -> prepare( "INSERT INTO $tb_name ($field_options) VALUE ($field_values)" );
foreach ( $data as $key => $value )
{
$sth -> bindValue( ":$key", $value );
}
$this -> PDO -> beginTransaction();
$sth -> execute();
$lastId = $this -> PDO -> lastInsertId();
$this -> PDO -> commit();
}
catch ( Exception $e )
{
// for rolling back the changes during transaction
$this -> PDO -> rollBack();
throw new Exception( "error in inseting" );
}
return $lastId;
}
/** /**
* Execute a query that doesn't have any parameters. * Select function using prepared statement.
* * For selecting particular fields.
* @param $query the mysql query. *
* @return returns a PDOStatement object. * @param string $param field to select, can be multiple fields.
*/ * @param string $tb_name Table Name to Select.
public function executeWithoutParams( $query ) { * @param array $data array of data to be used in WHERE clause in format('fieldname'=>$value). 'fieldname' must be a field in that table.
$statement = $this -> PDO -> prepare( $query ); * @param string $where where to select.
$statement -> execute(); * @return statement object.
return $statement; */
} public function selectWithParameter( $param, $tb_name, $data, $where )
{
try {
$sth = $this -> PDO -> prepare( "SELECT $param FROM $tb_name WHERE $where" );
$this -> PDO -> beginTransaction();
$sth -> execute( $data );
$this -> PDO -> commit();
}
catch ( Exception $e ) {
$this -> PDO -> rollBack();
throw new Exception( "error selection" );
return false;
}
return $sth;
}
/** /**
* Execute a query that has parameters. * Select function using prepared statement.
* * For selecting all fields in a table.
* @param $query the mysql query. *
* @param $params the parameters that are being used by the query. * @param string $tb_name Table Name to Select.
* @return returns a PDOStatement object. * @param array $data array of data to be used with WHERE part in format('fieldname'=>$value,...). 'fieldname' must be a field in that table.
*/ * @param string $where where to select in format('fieldname=:fieldname' AND ...).
public function execute( $query, $params ) { * @return statement object.
$statement = $this -> PDO -> prepare( $query ); */
$statement -> execute( $params ); public function select( $tb_name, $data , $where )
return $statement; {
} try {
$sth = $this -> PDO -> prepare( "SELECT * FROM $tb_name WHERE $where" );
$this -> PDO -> beginTransaction();
$sth -> execute( $data );
$this -> PDO -> commit();
}
catch( Exception $e )
{
$this -> PDO -> rollBack();
throw new Exception( "error selection" );
return false;
}
return $sth;
}
/** /**
* Insert function which returns id of the inserting field. * Update function with prepared statement.
* *
* @param $tb_name table name where we want to insert data. * @param string $tb_name name of the table on which operation to be performed.
* @param $data the parameters that are being inserted into table. * @param array $data array of data in format('fieldname' => $value,...).Here, only those fields must be stored which needs to be updated.
* @return returns the id of the last inserted element. * @param string $where where part in format ('fieldname'= $value AND ...). 'fieldname' must be a field in that table.
*/ * @throws Exception error in updating.
public function executeReturnId( $tb_name, $data ) { */
$field_values = ':' . implode( ',:', array_keys( $data ) ); public function update( $tb_name, $data, $where )
$field_options = implode( ',', array_keys( $data ) ); {
try { $field_option_values = null;
$sth = $this -> PDO -> prepare( "INSERT INTO $tb_name ($field_options) VALUE ($field_values)" ); foreach ( $data as $key => $value )
foreach ( $data as $key => $value ) {
{ $field_option_values .= ",$key" . '=:' . $key;
$sth -> bindValue( ":$key", $value ); }
} $field_option_values = ltrim( $field_option_values, ',' );
$this -> PDO -> beginTransaction(); try {
$sth -> execute(); $sth = $this -> PDO -> prepare( "UPDATE $tb_name SET $field_option_values WHERE $where " );
$lastId = $this -> PDO -> lastInsertId();
$this -> PDO -> commit();
}
catch ( Exception $e )
{
// for rolling back the changes during transaction
$this -> PDO -> rollBack();
throw new Exception( "error in inseting" );
}
return $lastId;
}
/** foreach ( $data as $key => $value )
* Select function using prepared statement. {
* For selecting particular fields. $sth -> bindValue( ":$key", $value );
* }
* @param string $param field to select, can be multiple fields. $this -> PDO -> beginTransaction();
* @param string $tb_name Table Name to Select. $sth -> execute();
* @param array $data array of data to be used in WHERE clause in format('fieldname'=>$value). 'fieldname' must be a field in that table. $this -> PDO -> commit();
* @param string $where where to select. }
* @return statement object. catch ( Exception $e )
*/ {
public function selectWithParameter( $param, $tb_name, $data, $where ) $this -> PDO -> rollBack();
{ throw new Exception( 'error in updating' );
try { return false;
$sth = $this -> PDO -> prepare( "SELECT $param FROM $tb_name WHERE $where" ); }
$this -> PDO -> beginTransaction(); return true;
$sth -> execute( $data ); }
$this -> PDO -> commit();
}
catch( Exception $e )
{
$this -> PDO -> rollBack();
throw new Exception( "error selection" );
return false;
}
return $sth;
}
/** /**
* Select function using prepared statement. * insert function using prepared statements.
* For selecting all fields in a table. *
* * @param string $tb_name Name of the table on which operation to be performed.
* @param string $tb_name Table Name to Select. * @param array $data array of data to insert in format('fieldname' => $value,....). 'fieldname' must be a field in that table.
* @param array $data array of data to be used with WHERE part in format('fieldname'=>$value,...). 'fieldname' must be a field in that table. * @throws error in inserting.
* @param string $where where to select in format('fieldname=:fieldname' AND ...). */
* @return statement object. public function insert( $tb_name, $data )
*/ {
public function select( $tb_name, $data , $where ) $field_values = ':' . implode( ',:', array_keys( $data ) );
{ $field_options = implode( ',', array_keys( $data ) );
try { try {
$sth = $this -> PDO -> prepare( "SELECT * FROM $tb_name WHERE $where" ); $sth = $this -> PDO -> prepare( "INSERT INTO $tb_name ($field_options) VALUE ($field_values)" );
$this -> PDO -> beginTransaction(); foreach ( $data as $key => $value )
$sth -> execute( $data ); {
$this -> PDO -> commit();
}
catch( Exception $e )
{
$this -> PDO -> rollBack();
throw new Exception( "error selection" );
return false;
}
return $sth;
}
/** $sth -> bindValue( ":$key", $value );
* Update function with prepared statement. }
* $this -> PDO -> beginTransaction();
* @param string $tb_name name of the table on which operation to be performed. // execution
* @param array $data array of data in format('fieldname' => $value,...).Here, only those fields must be stored which needs to be updated. $sth -> execute();
* @param string $where where part in format ('fieldname'= $value AND ...). 'fieldname' must be a field in that table. $this -> PDO -> commit();
* @throws Exception error in updating.
*/
public function update( $tb_name, $data, $where )
{
$field_option_values = null;
foreach ( $data as $key => $value )
{
$field_option_values .= ",$key" . '=:' . $key;
}
$field_option_values = ltrim( $field_option_values, ',' );
try {
$sth = $this -> PDO -> prepare( "UPDATE $tb_name SET $field_option_values WHERE $where " );
foreach ( $data as $key => $value ) }
{ catch ( Exception $e )
$sth -> bindValue( ":$key", $value ); {
} // for rolling back the changes during transaction
$this -> PDO -> beginTransaction(); $this -> PDO -> rollBack();
$sth -> execute(); throw new Exception( "error in inserting" );
$this -> PDO -> commit(); }
} }
catch ( Exception $e )
{
$this -> PDO -> rollBack();
throw new Exception( 'error in updating' );
return false;
}
return true;
}
/** /**
* insert function using prepared statements. * Delete database entery using prepared statement.
* *
* @param string $tb_name Name of the table on which operation to be performed. * @param string $tb_name table name on which operations to be performed.
* @param array $data array of data to insert in format('fieldname' => $value,....). 'fieldname' must be a field in that table. * @param $data array with values in the format('fieldname'=> $value,...). 'fieldname' must be a field in that table.
* @throws error in inserting. * @param string $where condition based on $data array in the format('fieldname=:fieldname' AND ...).
*/ * @throws error in deleting.
public function insert( $tb_name, $data ) */
{ public function delete( $tb_name, $data, $where )
$field_values = ':' . implode( ',:', array_keys( $data ) ); {
$field_options = implode( ',', array_keys( $data ) ); try {
try { $sth = $this -> PDO -> prepare( "DELETE FROM $tb_name WHERE $where" );
$sth = $this -> PDO -> prepare( "INSERT INTO $tb_name ($field_options) VALUE ($field_values)" ); $this -> PDO -> beginTransaction();
foreach ( $data as $key => $value ) $sth -> execute( $data );
{ $this -> PDO -> commit();
}
catch ( Exception $e )
{
$this -> PDO -> rollBack();
throw new Exception( "error in deleting" );
}
$sth -> bindValue( ":$key", $value ); }
} }
$this -> PDO -> beginTransaction();
// execution
$sth -> execute();
$this -> PDO -> commit();
}
catch ( Exception $e )
{
// for rolling back the changes during transaction
$this -> PDO -> rollBack();
throw new Exception( "error in inserting" );
}
}
/**
* Delete database entery using prepared statement.
*
* @param string $tb_name table name on which operations to be performed.
* @param $data array with values in the format('fieldname'=> $value,...). 'fieldname' must be a field in that table.
* @param string $where condition based on $data array in the format('fieldname=:fieldname' AND ...).
* @throws error in deleting.
*/
public function delete( $tb_name, $data, $where )
{
try {
$sth = $this -> PDO -> prepare( "DELETE FROM $tb_name WHERE $where" );
$this -> PDO -> beginTransaction();
$sth -> execute( $data );
$this -> PDO -> commit();
}
catch ( Exception $e )
{
$this -> PDO -> rollBack();
throw new Exception( "error in deleting" );
}
}
}