// wrapper for mysqli statement object
class MySqlCommand{
//
// private members
//
private $Statement; // prepared statement
//
// public members
//
public $CommandText; // sql to execute
public $Connection; // mysqli object
public $Parameters = array(); // array of input parameters
public $Results = array(); // array of output parameters
//
// constructor
//
public function __construct($CommandText = '', $Connection = ''){
$this->CommandText = $CommandText;
$this->Connection = $Connection;
}
//
// private methods
//
// bind input parameters
private function BindParameters(){
if(count($this->Parameters)){
// determine type of input parameter (i)nteger/(d)ouble/(s)tring
$types = '';
foreach ($this->Parameters as $Parameter){
if(is_int($Parameter))
$types .= 'i';
elseif(is_float($Parameter))
$types .= 'd';
else
$types .= 's';
}
// $this->Statement->bind_param($types, $this->Parameters[0], $this->Parameters[1], ...);
call_user_func_array(array($this->Statement, 'bind_param'), array_merge(array($types), $this->Parameters));
}
}
// log errors
private function handle_exception(){
$oMySqlError = new MySqlError();
$oMySqlError->Message = $this->Connection->Error();
$oMySqlError->CommandText = $this->CommandText;
$oMySqlError->Parameters = $this->Parameters;
$oMySqlError->Log();
$oMySqlError = null;
}
//
// public methods
//
// helper method for adding bound parameters
public function AddParameter(&$parameter){
$this->Parameters[] = &$parameter;
}
// close the statement if it hasn't been closed already
public function Close(){
if(is_object($this->Statement))
$this->Statement->close();
}
// execute a non-query, like insert, update, delete
public function ExecuteNonQuery(){
try{
// prepare statement
if(!$this->Statement = $this->Connection->Prepare($this->CommandText))
throw new Exception();
// bind input parameters
$this->BindParameters();
// execute statement
if(!$this->Statement->execute())
throw new Exception();
}
catch (Exception $e){
$this->handle_exception();
}
}
// return a data reader from a select statement
public function ExecuteReader(){
try{
// prepare statement
if(!$this->Statement = $this->Connection->Prepare($this->CommandText))
throw new Exception();
// bind input parameters
$this->BindParameters();
// execute statement
if(!$this->Statement->execute())
throw new Exception();
}
catch (Exception $e){
$this->handle_exception();
}
// create and return a new data reader for statement
$oDataReader = new MySqlDataReader($this->Statement);
return $oDataReader;
}
// return a scalar value
public function ExecuteScalar(){
try{
// prepare statement
if(!$this->Statement = $this->Connection->Prepare($this->CommandText))
throw new Exception();
// bind input parameters
$this->BindParameters();
// execute statement
if(!$this->Statement->execute())
throw new Exception();
}
catch (Exception $e){
$this->handle_exception();
}
// create a data reader
$oDataReader = new MySqlDataReader($this->Statement);
// bind the result
$oDataReader->AddResult($result);
// execute the reader
if(!$oDataReader->Read())
$result = null;
$oDataReader = null;
// return the result
return $result;
}
}
?>
Wednesday, August 22, 2007
php 5 mysqli wrapper - command
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment