Codice PHP:
class cbSQLRetrieveData {
}
class cbSQLConnectVar {
const DB_DEFAULT = 0x000000;
const DB_MYSQL = 0x000001;
const DB_POSTGRE = 0x000005;
const DB_SQLITE = 0x000010;
const FETCH_ASSOC = 15;
const FETCH_LAZY = 25;
const FETCH_OBJECT = 35;
}
class cbSQLConnect {
public $LastQuery; // record last query (temporary)
private $db; // stores setup class and all database related like tablenames, database name and connection string
private $data; // no use when i updated query run mode
private $Rows; // store number of rows affected
private $FetchType; // stores type of fetch result of querys
private $PDOInstance; // class global PDO Instance
public function __construct(cbSQLConnectConfig &$setup, $mode = cbSQLConnectVar::FETCH_ASSOC){
$this->db = &$setup;
$this->FetchType = $mode;
$this->db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_FUNC);
$this->PDOInstance = cbSQLConnectConfig::CreatePDOInstance();
}
public function escape($String)
{
return addcslashes(str_replace('\\', "\\", addslashes($String)), '');
}
public function QuerySingle($query){
$instance = cbSQLConnectConfig::CreatePDOInstance();
$stmt = $instance->prepare($query);
switch( $this->FetchType){
case cbSQLConnectVar::FETCH_ASSOC:
$tp = $this->RunQuery($stmt);
$this->LastQuery = $query;
break;
case cbSQLConnectVar::FETCH_LAZY:
$this->LastQuery = $query;
$tp = $this->RunQueryAsLazy();
break;
case cbSQLConnectVar::FETCH_OBJECT:
$this->LastQuery = $query;
$tp = $this->RunQueryAsObject();
break;
default:
$this->LastQuery = $query;
$stmt = $this->db->prepare($query);
$tp = $this->RunQuery($stmt);
break;
}
return $tp;
}
public function Query($query, $binds =''){
$stmt = $this->db->prepare($query);
$binds = isset($binds)?$binds:'';
if(!empty($binds))
{
foreach($binds as $bindKey => $key)
{
$stmt->bindParam($bindKey, $key, PDO::PARAM_STR| PDO::PARAM_INT);
}
}
return $this->RunQuery($stmt);
}
public function insert_id()
{
return $this->db->lastInsertID();
}
private function RunQuery(PDOStatement $state, $param = NULL)
{
try
{
if(empty($param) || $param == NULL)
{
$state->execute();
$this->data = $state->fetchAll(PDO::FETCH_ASSOC);
$this->Rows = $state->rowCount();
$state->closeCursor();
return $this->data;
}else {
$state->execute($param);
$this->data = $state->fetchAll(PDO::FETCH_ASSOC);
$this->Rows = $state->rowCount();
$state->closeCursor();
return $this->data;
}
}
catch(PDOException $ex)
{
throw new PDOException($ex->getMessage());
}
}
private function RunQueryAsObject(){
$stmt = $this->db->query($this->LastQuery);
$result = $stmt->fetchAll(PDO::FETCH_CLASS, "cbSQLRetrieveData");
return $result;
}
private function RunQueryAsLazy(){
$stmt = $this->db->query($this->LastQuery);
$result = $stmt->fetchAll(PDO::FETCH_BOTH);
return $result;
}
private function implode2($glue1, $glue2, $array){
return ((sizeof($array) > 2)? implode($glue1, array_slice($array, 0, -2)).$glue1 : "").implode($glue2, array_slice($array, -2));
}
private function addQuotes($word, $doublequotes = true){
if($doublequotes){
$pword = '"'.$word.'"';
}else{
$pword = "'".$pword."'";
}
return $pword;
}
public function SQLInsert($array, $table)
{
if(empty($table))
{
return false;
}
$inst = cbSQLConnectConfig::CreatePDOInstance();
$fieldname = array_keys($array[0]);
$table = sprintf("INSERT INTO %s" , $table);
$fields = '('.implode(",",$fieldname).')';
$placeholder = 'VALUES(:'.implode(", :", $fieldname).')';
$query = sprintf("%s %s %s", $table, $fields, $placeholder);
$stmt = $inst->prepare($query);
foreach($array[0] as $param => $key)
{
$stmt->bindValue(':'.$param, $array[0][$param]);
}
$stmt->execute();
if($stmt->rowCount() != 0){
$stmt->closeCursor();
return true;
}else {
$stmt->closeCursor();
return false;
}
}
public function SQLUpdate($table, $fieldToChange, $fieldValue, $idField, $idValue){
$query = sprintf("UPDATE %s SET %s = :value WHERE %s = :comparevalue", $table, $fieldToChange, $idField);
$stmt = $this->db->prepare($query);
if(!$stmt){
print_r( $stmt->errorInfo());
return false;
}
$stmt->bindParam(":value", $fieldValue, PDO::PARAM_INT | PDO::PARAM_STR);
$stmt->bindParam(":comparevalue",$idValue, PDO::PARAM_INT | PDO::PARAM_STR);
$stmt->execute();
if($stmt->rowCount() != 0){
$stmt->closeCursor();
return true;
}
$stmt->closeCursor();
return false;
}
public function SQLDelete($table, $idTable, $idValue){
$inst = cbSQLConnectConfig::CreatePDOInstance();
$query = sprintf("DELETE FROM %s WHERE %s = :value", $table, $idTable);
$stmt = $inst->prepare($query);
$stmt->bindParam(":value", $idValue);
$stmt->execute();
$stmt->closeCursor();
}
public function RowsAffected(){
return $this->Rows;
}
}
class cbSQLConnectConfig extends PDO {
protected $actualStatement;
protected static $qs;
protected static $user;
protected static $pwd;
protected static $options;
protected static $fetchMode;
protected static $driverDB;
public function __construct($driver, $host, $port = 3306, $database, $user, $pwd, $options = array()){
cbSQLConnectConfig::$qs = $this->ConnectionStringGenerator($driver, $host, $port, $database);
cbSQLConnectConfig::$user = $user;
cbSQLConnectConfig::$pwd = $pwd;
cbSQLConnectConfig::$options = $options;
parent::__construct(cbSQLConnectConfig::$qs, cbSQLConnectConfig::$user, cbSQLConnectConfig::$pwd, cbSQLConnectConfig::$options);
}
public static function CreatePDOInstance(){
return new PDO( cbSQLConnectConfig::$qs, cbSQLConnectConfig::$user, cbSQLConnectConfig::$pwd, cbSQLConnectConfig::$options);
}
protected function getConnectionString(){
return $this->qs;
}
public function getTable(){
preg_match("/dbname=([aA-zZ0-9]+)/", $this->qs, $mt);
return $mt[1];
}
private function ConnectionStringGenerator($driver, $host, $port, $database){
switch( (int) $driver){
case cbSQLConnectVar::DB_MYSQL:
cbSQLConnectConfig::$driverDB = "mysql";
break;
case cbSQLConnectVar::DB_POSTGRE:
cbSQLConnectConfig::$driverDB = "psgre";
default:
cbSQLConnectConfig::$driverDB = "mysql";
break;
}
$connectionstring = sprintf("%s:host=%s;port=%d;dbname=%s", cbSQLConnectConfig::$driverDB, $host, $port, $database);
return $connectionstring;
}
}
if(defined("DATABASE_HOST")){$dbhost = DATABASE_HOST ; }
if(defined("DATABASE_USER")){$db_user = DATABASE_USER ; }
if(defined("DATABASE_PASSWORD")){$db_pass = DATABASE_PASSWORD ; }
if(defined("DATABASE_NAME")){$db_name = DATABASE_NAME ; }
if(defined("DATABASE_PORT")){$db_port = DATABASE_PORT ; }
if(defined("PCONNECT")){$db_persistent = PCONNECT ; }
$databaseSetup = new cbSQLConnectConfig( cbSQLConnectVar::DB_MYSQL, $dbhost,$db_port,$db_name,$db_user,'');
$dbcore = new cbSQLConnect($databaseSetup, cbSQLConnectVar::FETCH_ASSOC);