Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Class myPDOStatement extends PDOStatement {
- public $sql;
- protected function __construct() {}
- }
- Class myPDO extends PDO {
- public final function __construct(
- $dsn, $user, $pass, $options
- =
- array(\PDO::ATTR_EMULATE_PREPARES => FALSE)
- ){
- parent::__construct($dsn, $user, $pass, $options);
- $this->setAttribute(
- \PDO::ATTR_STATEMENT_CLASS, array('myPDOStatement', array($this))
- );
- }
- function prepare($statement, $driver_options = array()) {
- $stmt = parent::prepare($statement, $driver_options);
- $stmt->sql = $statement;
- return $stmt;
- }
- }
- Class PDOHandler {
- private $dsn = d_dsn;
- private $user = d_user;
- private $pass = d_pass;
- private $fstyle = Array(
- 0 => PDO::FETCH_ASSOC,
- 1 => PDO::FETCH_BOTH,
- 2 => PDO::FETCH_BOUND,
- 3 => PDO::FETCH_CLASS,
- 4 => PDO::FETCH_INTO,
- 5 => PDO::FETCH_LAZY,
- 6 => PDO::FETCH_NUM,
- 7 => PDO::FETCH_OBJ
- );
- private $connection;
- private $stmt = NULL;
- private $errors = Array();
- public function __construct(){
- try {
- $this->connection = new MyPDO($this->dsn,$this->user,$this->pass);
- unset( $this->dsn, $this->user , $this->pass );
- return true;
- } catch( PDOException $e ) {
- $this->connection = NULL;
- $this->errors[] = $e->getMessage();
- return false;
- }
- }
- public function __destruct(){
- $this->connection = NULL;
- }
- public function connect(){
- return false;
- }
- public function disconnect(){
- $this->connection = NULL;
- return true;
- }
- private static function paramcompare( $stmt , $params=NULL ){
- // check if $params is an array, and if set count the
- // number of supplied parameters and store the value
- if( !is_null( $params ) ) {
- if( is_array($params) ){
- $supplied = count($params);
- } else {
- $supplied = 1;
- }
- } else { // if $params is NULL the number of parameters are 0
- $supplied = 0;
- }
- // check the number of paramaters required by the statement
- // if no parameters in statement default to 0
- $expected = 0;
- if( $check = preg_match_all('/\:[\w]+/',$stmt,$count) ){
- $expected = $check;
- } elseif ( $check = preg_match_all('/\?/',$stmt,$count) ){
- $expected = $check;
- }
- if( $supplied == $expected ){
- return true;
- }
- return false;
- }
- private function catchexception( $e ){
- $this->errors[] = $e->getMessage();
- }
- public function run( $stmt , $params=NULL ){
- try {
- if($this->connection == NULL ){
- throw new PDOException('No active database connection');
- }
- // Makes sure the number of supplied arguments in $params
- // match the number of arguements that is required by the
- // statment $stmt and if they match prepare the
- // statement for execution otherwise throw an exception
- if( $this->paramcompare( $stmt , $params ) ){
- // make sure statement is valid SQL otherwise
- // throw an exception
- if( !$this->stmt=$this->connection->prepare($stmt) ){
- throw new PDOException('Statement failed: Invalid SQL');
- }
- // If $params were supplied bind the parameters into
- // the query before query execution
- if( $params !== NULL ){
- // Make sure the bindings are successful
- // otherwise throw an Exception
- if( !$this->bind($params) ) {
- throw new PDOException('Statement failed: Binding parameters failed');
- }
- }
- // Execute the statement but throw an Exception
- // on a failure
- if( $this->stmt->execute() ){
- return $this->stmt;
- } else {
- throw new PDOException('Statement failed: Statement execution failed');
- }
- } else {
- throw new PDOException("Statement failed: Number of parameters supplied doesn't match the amount of parameters required by query statement.");
- }
- } catch( PDOException $e ) {
- // $this->errors[] = $e->getMessage();
- $this->catchexception($e);
- return false;
- }
- }
- public function prepare( $query ){
- try{
- if($this->connection == NULL ){
- throw new PDOException('No active database connection');
- }
- // make sure statement is valid SQL otherwise
- // throw an Exception
- if ( !$this->stmt = $this->connection->prepare( $query ) ) {
- throw new PDOException('Failed to prepare statement: Invalid SQL');
- }
- return $this->stmt;
- } catch( PDOException $e ) {
- // $this->errors[] = $e->getMessage();
- $this->catchexception($e);
- return false;
- }
- }
- public function execute( $stmt=NULL ){
- try{
- if($this->connection == NULL ){
- throw new PDOException('No active database connection');
- }
- // make sure there is a prepared statement to run
- // if not throw an Exception
- if( !is_null($this->stmt) && !is_null($stmt) ) {
- throw new PDOException('Failed to execute statement: No prepared statement to execute');
- }
- // check where the statement is set
- // first check is the $stmt variable
- // second the internal object $this->stmt is checked
- if( !is_null( $stmt ) ){
- $this->stmt = $stmt->execute();
- } else {
- $this->stmt = $this->stmt->execute();
- }
- return $this->stmt;
- } catch( PDOException $e ) {
- // $this->errors[] = $e->getMessage();
- $this->catchexception($e);
- return false;
- }
- }
- public function query( $query ){
- try{
- if($this->connection == NULL ){
- throw new PDOException('No active database connection');
- }
- // check if the query executes correctly
- // if not throw an Exception
- if( !$this->stmt = $this->connection->query( $query ) ) {
- throw new PDOException( 'Query failed: Invalid SQL.' );
- }
- return true;
- } catch ( PDOException $e ) {
- // $this->errors[] = $e->getMessage();
- $this->catchexception($e);
- return false;
- }
- }
- public function bind( $params , $stmt=NULL ){
- try{
- if($this->connection == NULL ){
- throw new PDOException('Failed to bind Parameters: No active database connection');
- }
- // make sure there is a prepared statement to run
- // if not throw an Exception
- if( !is_null($this->stmt) && !is_null($stmt) ) {
- throw new PDOException('Failed to bind Parameters: No prepared statement to bind parameters to');
- }
- // check where the statement is set
- // first check is the $stmt variable
- // second the internal object $this->stmt is checked
- if( !is_null( $stmt ) ){
- $stmt = $stmt;
- } else {
- $stmt = &$this->stmt;
- }
- // Makes sure the number of supplied arguments in $params
- // match the number of arguements that is required by the
- // statment $stmt->sql and if they match prepare the
- // statement for execution otherwise throw an exception
- if( $this->paramcompare( $stmt->sql , $params) ){
- // indentify if the supplied parameters in the shape of an
- // array or a string so you can bind the supplied parameters
- // in a correct manner
- $bind = FALSE;
- if( is_array( $params ) ){
- // validate which placeholder format is used to
- // ensure the paramaters are bound in a correct manner
- // if format isn't recognized throw an Exception
- if( preg_match_all('/\:[\w]+/',$stmt->sql,$count) ){
- // check where the statement is set
- // first check is the $stmt variable
- // second the internal object $this->stmt is checked
- // and then bind all parameters into statement
- foreach( $params as $key => $value){
- if( $stmt->bindValue( $key , $value ) ){
- $bind = TRUE;
- } else {
- $bind = FALSE;
- }
- }
- } elseif ( preg_match_all('/\?/',$stmt->sql,$count) ){
- // check where the statement is set
- // first check is the $stmt variable
- // second the internal object $this->stmt is checked
- // and then bind all parameters into statement
- //die( var_dump($params) );
- for( $i=1 ; $i<=count($params) ; $i++ ){
- if( $stmt->bindValue( $i , $params[$i-1] ) ){
- $bind = TRUE;
- } else {
- $bind = FALSE;
- }
- }
- } else {
- throw new PDOException('Failed to bind Parameters: Placeholder format not recognized, expected :placeholders or ? placeholders');
- }
- } else {
- // validate which placeholder format is used to
- // ensure the paramaters are bound in a correct manner
- // if format isn't recognized throw an Exception
- if( preg_match_all('/\:[\w]+/',$stmt->sql,$count) ){
- // check where the statement is set
- // first check is the $stmt variable
- // second the internal object $this->stmt is checked
- // and then bind all parameters into statement
- if( $stmt->bindValue( $params , $params ) ){
- $bind = TRUE;
- }
- } elseif ( preg_match_all('/\?/',$stmt->sql,$count) ){
- // check where the statement is set
- // first check is the $stmt variable
- // second the internal object $this->stmt is checked
- // and then bind all parameters into statement
- if( $stmt->bindValue( 1 , $params ) ){
- $bind = TRUE;
- }
- } else {
- throw new PDOException('Failed to bind Parameters: Placeholder format not recognized, expected :placeholders or ? placeholders');
- }
- }
- // If binding of parameters failed throw an Exception
- // otherwise return true.
- if( $bind ){
- return true;
- } else {
- throw new PDOException('Failed to bind Parameters: Parameter binding failed during value bind assignment');
- }
- } else {
- throw new PDOException("Failed to bind Parameters: Number of parameters supplied doesn't match the amount of parameters required by query statement.");
- }
- } catch( PDOException $e ) {
- // $this->errors[] = $e->getMessage();
- $this->catchexception($e);
- return false;
- }
- }
- public function fetch( $stmt=NULL , $all=TRUE , $style=0 ){
- try{
- if($this->connection == NULL ){
- throw new PDOException('Failed to fetch data: No active database connection');
- }
- // make sure there is a prepared statement to run
- // if not throw an Exception
- if( !is_null($this->stmt) && !is_null($stmt) ) {
- throw new PDOException('Failed to fetch data: No prepared statement to fetch data from');
- }
- // ensure that the fetch style supplied exist
- // if not recognized throw an Exception
- if( !array_key_exists( $style , $this->fstyle ) ){
- throw new PDOException('Failed to fetch data: Fetch style not recognized');
- }
- // determine if a single record or all records are to be returned
- if($all===TRUE){
- $fetch='fetchAll';
- } else {
- $fetch='fetch';
- }
- // check where the statement is set
- // first check is the $stmt variable
- // second the internal object $this->stmt is checked
- if( !is_null( $stmt ) ){
- return $stmt->$fetch( $this->fstyle[$style] );
- } else {
- return $this->stmt->$fetch( $this->fstyle[$style] );
- }
- } catch( PDOException $e ) {
- // $this->errors[] = $e->getMessage();
- $this->catchexception($e);
- return false;
- }
- }
- /*
- public function getLastInsertID( ) {
- return $this->connection->lastInsertId( );
- }
- */
- public function flush(){
- // flush statement object
- $this->stmt = NULL;
- }
- public function error($all=FALSE){
- if($all==TRUE){
- // var_dump all errors
- return $this->errors;
- }
- // return last cast error
- return end($this->errors);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement