mirror of
https://port.numenaute.org/aleajactaest/khanat-code-old.git
synced 2024-12-27 02:50:53 +00:00
Merge
This commit is contained in:
commit
d9a2e44e9e
1 changed files with 45 additions and 56 deletions
|
@ -29,13 +29,13 @@
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
$PDOCache = array();
|
// $PDOCache = array();
|
||||||
|
|
||||||
class DBLayer {
|
class DBLayer {
|
||||||
|
|
||||||
private $PDO;
|
private $PDO;
|
||||||
private $host;
|
// private $host;
|
||||||
private $dbname;
|
// private $dbname;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The PDO object, instantiated by the constructor
|
* The PDO object, instantiated by the constructor
|
||||||
|
@ -51,15 +51,15 @@ class DBLayer {
|
||||||
function __construct($db, $dbn = null)
|
function __construct($db, $dbn = null)
|
||||||
{
|
{
|
||||||
global $cfg;
|
global $cfg;
|
||||||
$this->host = $cfg['db'][$db]['host'];
|
// $this->host = $cfg['db'][$db]['host'];
|
||||||
$this->dbname = $cfg['db'][$db]['name'];
|
// $this->dbname = $cfg['db'][$db]['name'];
|
||||||
global $PDOCache;
|
/*global $PDOCache;
|
||||||
if (isset($PDOCache[$this->host])) {
|
if (isset($PDOCache[$this->host])) {
|
||||||
$this->PDO = $PDOCache[$this->host]['pdo'];
|
$this->PDO = $PDOCache[$this->host]['pdo'];
|
||||||
} else {
|
} else {*/
|
||||||
$dsn = "mysql:";
|
$dsn = "mysql:";
|
||||||
$dsn .= "host=" . $cfg['db'][$db]['host'] . ";";
|
$dsn .= "host=" . $cfg['db'][$db]['host'] . ";";
|
||||||
// $dsn .= "dbname=" . $cfg['db'][$db]['name'] . ";";
|
$dsn .= "dbname=" . $cfg['db'][$db]['name'] . ";"; // Comment this out when using the cache
|
||||||
$dsn .= "port=" . $cfg['db'][$db]['port'] . ";";
|
$dsn .= "port=" . $cfg['db'][$db]['port'] . ";";
|
||||||
|
|
||||||
$opt = array(
|
$opt = array(
|
||||||
|
@ -68,11 +68,11 @@ class DBLayer {
|
||||||
PDO::ATTR_PERSISTENT => true
|
PDO::ATTR_PERSISTENT => true
|
||||||
);
|
);
|
||||||
$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);
|
||||||
$PDOCache[$this->host] = array();
|
/* $PDOCache[$this->host] = array();
|
||||||
$PDOCache[$this->host]['pdo'] = $this->PDO;
|
$PDOCache[$this->host]['pdo'] = $this->PDO;
|
||||||
$PDOCache[$this->host]['use'] = $this->dbname;
|
$PDOCache[$this->host]['use'] = $this->dbname;
|
||||||
$this->PDO->query('USE ' . $this->dbname . ';'); // FIXME safety
|
*/ //$this->PDO->query('USE ' . $this->dbname . ';'); // FIXME safety
|
||||||
}
|
/*}*/
|
||||||
}
|
}
|
||||||
|
|
||||||
function __destruct() {
|
function __destruct() {
|
||||||
|
@ -80,11 +80,11 @@ class DBLayer {
|
||||||
}
|
}
|
||||||
|
|
||||||
function useDb() {
|
function useDb() {
|
||||||
global $PDOCache;
|
/*global $PDOCache;
|
||||||
if ($PDOCache[$this->host]['use'] != $this->dbname) {
|
if ($PDOCache[$this->host]['use'] != $this->dbname) {
|
||||||
$PDOCache[$this->host]['use'] = $this->dbname;
|
$PDOCache[$this->host]['use'] = $this->dbname;
|
||||||
$this->PDO->query('USE ' . $this->dbname . ';'); // FIXME safety
|
$this->PDO->query('USE ' . $this->dbname . ';'); // FIXME safety
|
||||||
}
|
}*/
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -135,13 +135,12 @@ class DBLayer {
|
||||||
$sth -> execute();
|
$sth -> execute();
|
||||||
$lastId = $this -> PDO -> lastInsertId();
|
$lastId = $this -> PDO -> lastInsertId();
|
||||||
$this -> PDO -> commit();
|
$this -> PDO -> commit();
|
||||||
}
|
}
|
||||||
catch ( Exception $e )
|
catch ( Exception $e ) {
|
||||||
{
|
|
||||||
// for rolling back the changes during transaction
|
// for rolling back the changes during transaction
|
||||||
$this -> PDO -> rollBack();
|
// $this -> PDO -> rollBack();
|
||||||
throw new Exception( "error in inseting" );
|
throw $e; // new Exception( "error in inseting" );
|
||||||
}
|
}
|
||||||
return $lastId;
|
return $lastId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -158,14 +157,11 @@ class DBLayer {
|
||||||
public function selectWithParameter( $param, $tb_name, $data, $where ) {
|
public function selectWithParameter( $param, $tb_name, $data, $where ) {
|
||||||
$this->useDb();
|
$this->useDb();
|
||||||
try {
|
try {
|
||||||
$sth = $this -> PDO -> prepare( "SELECT $param FROM $tb_name WHERE $where" );
|
$sth = $this->PDO->prepare( "SELECT $param FROM $tb_name WHERE $where" );
|
||||||
$this -> PDO -> beginTransaction();
|
$sth->execute( $data );
|
||||||
$sth -> execute( $data );
|
|
||||||
$this -> PDO -> commit();
|
|
||||||
}
|
}
|
||||||
catch ( Exception $e ) {
|
catch ( Exception $e ) {
|
||||||
$this -> PDO -> rollBack();
|
throw $e; // new Exception( "error selection" );
|
||||||
throw new Exception( "error selection" );
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return $sth;
|
return $sth;
|
||||||
|
@ -180,22 +176,18 @@ class DBLayer {
|
||||||
* @param string $where where to select in format('fieldname=:fieldname' AND ...).
|
* @param string $where where to select in format('fieldname=:fieldname' AND ...).
|
||||||
* @return statement object.
|
* @return statement object.
|
||||||
*/
|
*/
|
||||||
public function select( $tb_name, $data , $where ) {
|
public function select($tb_name, $data , $where) {
|
||||||
$this->useDb();
|
$this->useDb();
|
||||||
try {
|
try {
|
||||||
$sth = $this -> PDO -> prepare( "SELECT * FROM $tb_name WHERE $where" );
|
$sth = $this->PDO->prepare("SELECT * FROM $tb_name WHERE $where");
|
||||||
$this -> PDO -> beginTransaction();
|
$sth->execute( $data );
|
||||||
$sth -> execute( $data );
|
|
||||||
$this -> PDO -> commit();
|
|
||||||
}
|
|
||||||
catch( Exception $e )
|
|
||||||
{
|
|
||||||
$this -> PDO -> rollBack();
|
|
||||||
throw new Exception( "error selection" );
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return $sth;
|
|
||||||
}
|
}
|
||||||
|
catch (Exception $e) {
|
||||||
|
throw $e; // new Exception( "error selection" );
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return $sth;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Update function with prepared statement.
|
* Update function with prepared statement.
|
||||||
|
@ -208,30 +200,27 @@ class DBLayer {
|
||||||
public function update( $tb_name, $data, $where ) {
|
public function update( $tb_name, $data, $where ) {
|
||||||
$this->useDb();
|
$this->useDb();
|
||||||
$field_option_values = null;
|
$field_option_values = null;
|
||||||
foreach ( $data as $key => $value )
|
foreach ( $data as $key => $value ) {
|
||||||
{
|
|
||||||
$field_option_values .= ",$key" . '=:' . $key;
|
$field_option_values .= ",$key" . '=:' . $key;
|
||||||
}
|
}
|
||||||
$field_option_values = ltrim( $field_option_values, ',' );
|
$field_option_values = ltrim( $field_option_values, ',' );
|
||||||
try {
|
try {
|
||||||
$sth = $this -> PDO -> prepare( "UPDATE $tb_name SET $field_option_values WHERE $where " );
|
$sth = $this -> PDO -> prepare( "UPDATE $tb_name SET $field_option_values WHERE $where " );
|
||||||
|
|
||||||
foreach ( $data as $key => $value )
|
foreach ( $data as $key => $value ) {
|
||||||
{
|
|
||||||
$sth -> bindValue( ":$key", $value );
|
$sth -> bindValue( ":$key", $value );
|
||||||
}
|
}
|
||||||
$this -> PDO -> beginTransaction();
|
$this -> PDO -> beginTransaction();
|
||||||
$sth -> execute();
|
$sth -> execute();
|
||||||
$this -> PDO -> commit();
|
$this -> PDO -> commit();
|
||||||
}
|
}
|
||||||
catch ( Exception $e )
|
catch ( Exception $e ) {
|
||||||
{
|
$this->PDO->rollBack();
|
||||||
$this -> PDO -> rollBack();
|
throw $e; // new Exception( 'error in updating' );
|
||||||
throw new Exception( 'error in updating' );
|
|
||||||
return false;
|
return false;
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* insert function using prepared statements.
|
* insert function using prepared statements.
|
||||||
|
@ -272,14 +261,14 @@ class DBLayer {
|
||||||
public function delete( $tb_name, $data, $where ) {
|
public function delete( $tb_name, $data, $where ) {
|
||||||
$this->useDb();
|
$this->useDb();
|
||||||
try {
|
try {
|
||||||
$sth = $this -> PDO -> prepare( "DELETE FROM $tb_name WHERE $where" );
|
$sth = $this->PDO->prepare( "DELETE FROM $tb_name WHERE $where" );
|
||||||
$this -> PDO -> beginTransaction();
|
$this->PDO->beginTransaction();
|
||||||
$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 in deleting" );
|
throw $e; // new Exception( "error in deleting" );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue