Is this class perfect for databases? All I want to know is what I should change to make it perfect.
error_reporting(E_ALL);
class RESPETO{
private $_server = 'mysql';
private $_host = '';
private $_database = '';
private $db_username = '';
private $db_password = '';
private $db_charset = 'utf8';
private $conn = NULL;
const FETCH = 0;
const FETCHALL = 3;
const ID = 1;
const ROWCOUNT = 2;
const PARAM_LOB = 'largeObject';
public function SQL( $Query, $value = array(), $req = RESPETO::FETCHALL ){
try{
$pattern = "/^(SELECT)|^(INSERT)|^(UPDATE)|^(DELETE)/";
if(preg_match($pattern, $Query, $matches) === 1):
$ready = $this->conn->prepare($Query);
if( ! empty( $value ) ){
$i = 1;
foreach($value as $key => $keyValue):
if( ( is_string( $key ) ) && ( $key == $this::PARAM_LOB ) ):
$param = PDO::PARAM_LOB;
elseif( is_string( $keyValue ) ):
$param = PDO::PARAM_STR;
elseif( is_int( $keyValue ) ):
$param = PDO::PARAM_INT;
elseif( is_bool( $keyValue ) ):
$param = PDO::PARAM_BOOL;
elseif( is_null( $keyValue ) ):
$param = PDO::PARAM_NULL;
else:
$param = PDO::PARAM_STR;
endif;
$ready->bindValue($i++, $keyValue, $param);
endforeach;
}
$ready->execute();
if( ( $matches[0] === "SELECT" ) && ( $req === $this::FETCHALL ) ):
$value = $ready->fetchall( PDO::FETCH_ASSOC );
elseif( ( $matches[0] === "SELECT" ) && ( $req === $this::FETCH ) ):
$value = $ready->fetch( PDO::FETCH_ASSOC );
elseif( ( $matches[0] === "INSERT" ) && ( $req === $this::ID ) ):
$value = $this->conn->lastInsertId();
elseif( $req === $this::ROWCOUNT ):
$value = $ready->rowCount();
else:
$value = true;
endif;
return $value;
else:
return false;
endif;
}
catch(Exception $e){
print "WHAT ARE YOU DOING? see this Error (SQL) : " . $e->getMessage();
// use email functionality to send errors if your appication is online, not 'print' or 'echo' language construct to show it as html contents.
exit;
}
}
public function __toString(){
$data = sprintf('host = %s, database = %s, username = %s, password = %s, charset = %s', $this->_host, $this->_database, $this->db_username, $this->db_password, $this->db_charset);
return $data;
}
public function __construct($host = NULL, $database = NULL, $charset = NULL, $username = NULL, $password = NULL){
if(!is_null($host))
$this->_host = $host;
if(!is_null($database))
$this->_database = $database;
if(!is_null($charset))
$this->db_charset = $charset;
if(!is_null($username))
$this->db_username = $username;
if(!is_null($password))
$this->db_password = $password;
$connectionString = sprintf("%s:host=%s; dbname=%s; charset=%s",
$this->_server, $this->_host, $this->_database, $this->db_charset);
try {
$this->conn = new PDO($connectionString, $this->db_username, $this->db_password);
$this->conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e){
print "WHAT ARE YOU DOING? see this Error (CONN): " . $e->getMessage();
// use email functionality to send errors if your appication is online, not 'print' or 'echo' language construct to show it as html contents.
exit;
}
}
public function __destruct(){
$this->conn = null;
}
}