Advertisement
Guest User

Untitled

a guest
Jun 8th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.12 KB | None | 0 0
  1. Class myPDOStatement extends PDOStatement {
  2.  
  3.     public $sql;
  4.    
  5.     protected function __construct() {}
  6.    
  7. }
  8.  
  9. Class myPDO extends PDO {
  10.    
  11.     public final function __construct(
  12.         $dsn, $user, $pass, $options
  13.         =
  14.         array(\PDO::ATTR_EMULATE_PREPARES => FALSE)
  15.     ){
  16.    
  17.         parent::__construct($dsn, $user, $pass, $options);
  18.         $this->setAttribute(
  19.             \PDO::ATTR_STATEMENT_CLASS, array('myPDOStatement', array($this))
  20.         );
  21.        
  22.     }
  23.    
  24.     function prepare($statement, $driver_options = array()) {
  25.    
  26.         $stmt = parent::prepare($statement, $driver_options);
  27.         $stmt->sql = $statement;
  28.         return $stmt;
  29.        
  30.     }
  31.    
  32. }
  33.  
  34. Class PDOHandler {
  35.    
  36.     private $dsn = d_dsn;
  37.     private $user = d_user;
  38.     private $pass = d_pass;
  39.    
  40.     private $fstyle = Array(
  41.         0 => PDO::FETCH_ASSOC,
  42.         1 => PDO::FETCH_BOTH,
  43.         2 => PDO::FETCH_BOUND,
  44.         3 => PDO::FETCH_CLASS,
  45.         4 => PDO::FETCH_INTO,
  46.         5 => PDO::FETCH_LAZY,
  47.         6 => PDO::FETCH_NUM,
  48.         7 => PDO::FETCH_OBJ
  49.     );
  50.    
  51.     private $connection;
  52.     private $stmt = NULL;
  53.     private $errors = Array();
  54.  
  55.     public function __construct(){
  56.    
  57.         try {
  58.        
  59.             $this->connection = new MyPDO($this->dsn,$this->user,$this->pass);
  60.             unset( $this->dsn, $this->user , $this->pass );
  61.             return true;
  62.            
  63.         } catch( PDOException $e ) {
  64.        
  65.             $this->connection = NULL;
  66.             $this->errors[] = $e->getMessage();
  67.             return false;
  68.            
  69.         }
  70.        
  71.     }
  72.  
  73.     public function __destruct(){
  74.    
  75.         $this->connection = NULL;
  76.        
  77.     }
  78.  
  79.     public function connect(){
  80.    
  81.         return false;
  82.        
  83.     }
  84.  
  85.     public function disconnect(){
  86.    
  87.         $this->connection = NULL;
  88.         return true;
  89.        
  90.     }
  91.    
  92.     private static function paramcompare( $stmt , $params=NULL ){
  93.    
  94.         // check if $params is an array, and if set count the
  95.         // number of supplied parameters and store the value
  96.         if( !is_null( $params ) ) {
  97.        
  98.             if( is_array($params) ){
  99.            
  100.                 $supplied = count($params);
  101.                
  102.             } else {
  103.            
  104.                 $supplied = 1;
  105.                
  106.             }
  107.            
  108.         } else { // if $params is NULL the number of parameters are 0
  109.        
  110.             $supplied = 0;
  111.            
  112.         }
  113.        
  114.         // check the number of paramaters required by the statement
  115.         // if no parameters in statement default to 0
  116.         $expected = 0;
  117.         if( $check = preg_match_all('/\:[\w]+/',$stmt,$count) ){
  118.        
  119.             $expected = $check;
  120.            
  121.         } elseif ( $check = preg_match_all('/\?/',$stmt,$count) ){
  122.        
  123.             $expected = $check;
  124.            
  125.         }
  126.        
  127.         if( $supplied == $expected ){
  128.        
  129.             return true;
  130.            
  131.         }
  132.        
  133.         return false;
  134.        
  135.     }
  136.    
  137.     private function catchexception( $e ){
  138.         $this->errors[] = $e->getMessage();
  139.     }
  140.    
  141.     public function run( $stmt , $params=NULL ){
  142.    
  143.         try {
  144.        
  145.             if($this->connection == NULL ){
  146.                 throw new PDOException('No active database connection');
  147.             }
  148.            
  149.             // Makes sure the number of supplied arguments in $params
  150.             // match the number of arguements that is required by the
  151.             // statment $stmt and if they match prepare the
  152.             // statement for execution otherwise throw an exception
  153.             if( $this->paramcompare( $stmt , $params ) ){
  154.            
  155.                 // make sure statement is valid SQL otherwise
  156.                 // throw an exception
  157.                 if( !$this->stmt=$this->connection->prepare($stmt) ){
  158.                
  159.                     throw new PDOException('Statement failed: Invalid SQL');
  160.                    
  161.                 }
  162.                
  163.                 // If $params were supplied bind the parameters into
  164.                 // the query before query execution
  165.                 if( $params !== NULL ){
  166.                
  167.                     // Make sure the bindings are successful
  168.                     // otherwise throw an Exception
  169.                     if( !$this->bind($params) ) {
  170.                    
  171.                         throw new PDOException('Statement failed: Binding parameters failed');
  172.                        
  173.                     }
  174.                    
  175.                 }
  176.                
  177.                 // Execute the statement but throw an Exception
  178.                 // on a failure
  179.                 if( $this->stmt->execute() ){
  180.                
  181.                     return $this->stmt;
  182.                    
  183.                 } else {
  184.                
  185.                     throw new PDOException('Statement failed: Statement execution failed');
  186.                    
  187.                 }
  188.                
  189.             } else {
  190.            
  191.                 throw new PDOException("Statement failed: Number of parameters supplied doesn't match the amount of parameters required by query statement.");
  192.            
  193.             }
  194.            
  195.         } catch( PDOException $e ) {
  196.        
  197.             // $this->errors[] = $e->getMessage();
  198.             $this->catchexception($e);
  199.             return false;
  200.            
  201.         }
  202.        
  203.     }
  204.    
  205.     public function prepare( $query ){
  206.    
  207.         try{
  208.        
  209.             if($this->connection == NULL ){
  210.                 throw new PDOException('No active database connection');
  211.             }
  212.            
  213.             // make sure statement is valid SQL otherwise
  214.             // throw an Exception
  215.             if ( !$this->stmt = $this->connection->prepare( $query ) ) {
  216.            
  217.                 throw new PDOException('Failed to prepare statement: Invalid SQL');
  218.                
  219.             }
  220.            
  221.             return $this->stmt;
  222.            
  223.         } catch( PDOException $e ) {
  224.        
  225.             // $this->errors[] = $e->getMessage();
  226.             $this->catchexception($e);
  227.             return false;
  228.            
  229.         }
  230.        
  231.     }
  232.    
  233.     public function execute( $stmt=NULL ){
  234.    
  235.         try{
  236.        
  237.             if($this->connection == NULL ){
  238.            
  239.                 throw new PDOException('No active database connection');
  240.                
  241.             }
  242.            
  243.             // make sure there is a prepared statement to run
  244.             // if not throw an Exception
  245.             if( !is_null($this->stmt) && !is_null($stmt) ) {
  246.            
  247.                 throw new PDOException('Failed to execute statement: No prepared statement to execute');
  248.                
  249.             }
  250.            
  251.             // check where the statement is set
  252.             // first check is the $stmt variable
  253.             // second the internal object $this->stmt is checked
  254.             if( !is_null( $stmt ) ){
  255.            
  256.                 $this->stmt = $stmt->execute();
  257.                
  258.             } else {
  259.            
  260.                 $this->stmt = $this->stmt->execute();
  261.                
  262.             }
  263.            
  264.             return $this->stmt;
  265.            
  266.         } catch( PDOException $e ) {
  267.        
  268.             // $this->errors[] = $e->getMessage();
  269.             $this->catchexception($e);
  270.             return false;
  271.            
  272.         }
  273.        
  274.     }
  275.    
  276.     public function query( $query ){
  277.    
  278.         try{
  279.        
  280.             if($this->connection == NULL ){
  281.            
  282.                 throw new PDOException('No active database connection');
  283.                
  284.             }
  285.            
  286.             // check if the query executes correctly
  287.             // if not throw an Exception
  288.             if( !$this->stmt = $this->connection->query( $query ) ) {
  289.            
  290.                 throw new PDOException( 'Query failed: Invalid SQL.' );
  291.                
  292.             }
  293.            
  294.             return true;
  295.            
  296.         } catch ( PDOException $e ) {
  297.        
  298.             // $this->errors[] = $e->getMessage();
  299.             $this->catchexception($e);
  300.             return false;
  301.            
  302.         }
  303.        
  304.     }
  305.    
  306.     public function bind( $params , $stmt=NULL ){
  307.    
  308.         try{
  309.        
  310.             if($this->connection == NULL ){
  311.            
  312.                 throw new PDOException('Failed to bind Parameters: No active database connection');
  313.                
  314.             }
  315.            
  316.             // make sure there is a prepared statement to run
  317.             // if not throw an Exception
  318.             if( !is_null($this->stmt) && !is_null($stmt) ) {
  319.                 throw new PDOException('Failed to bind Parameters: No prepared statement to bind parameters to');
  320.             }
  321.            
  322.             // check where the statement is set
  323.             // first check is the $stmt variable
  324.             // second the internal object $this->stmt is checked
  325.             if( !is_null( $stmt ) ){
  326.            
  327.                 $stmt = $stmt;
  328.                
  329.             } else {
  330.            
  331.                 $stmt = &$this->stmt;
  332.                
  333.             }
  334.            
  335.             // Makes sure the number of supplied arguments in $params
  336.             // match the number of arguements that is required by the
  337.             // statment $stmt->sql and if they match prepare the
  338.             // statement for execution otherwise throw an exception
  339.             if( $this->paramcompare( $stmt->sql , $params) ){
  340.            
  341.                 // indentify if the supplied parameters in the shape of an
  342.                 // array or a string so you can bind the supplied parameters
  343.                 // in a correct manner
  344.                 $bind = FALSE;
  345.                
  346.                 if( is_array( $params ) ){
  347.                
  348.                     // validate which placeholder format is used to
  349.                     // ensure the paramaters are bound in a correct manner
  350.                     // if format isn't recognized throw an Exception
  351.                     if( preg_match_all('/\:[\w]+/',$stmt->sql,$count) ){
  352.                    
  353.                         // check where the statement is set
  354.                         // first check is the $stmt variable
  355.                         // second the internal object $this->stmt is checked
  356.                         // and then bind all parameters into statement
  357.                         foreach( $params as $key => $value){
  358.                        
  359.                             if( $stmt->bindValue( $key , $value ) ){
  360.                            
  361.                                 $bind = TRUE;
  362.                                
  363.                             } else {
  364.                            
  365.                                 $bind = FALSE;
  366.                                
  367.                             }
  368.                        
  369.                         }
  370.                    
  371.                     } elseif ( preg_match_all('/\?/',$stmt->sql,$count) ){
  372.                    
  373.                         // check where the statement is set
  374.                         // first check is the $stmt variable
  375.                         // second the internal object $this->stmt is checked
  376.                         // and then bind all parameters into statement
  377.                         //die( var_dump($params) );
  378.                         for( $i=1 ; $i<=count($params) ; $i++ ){
  379.                        
  380.                             if( $stmt->bindValue( $i , $params[$i-1] ) ){
  381.                            
  382.                                 $bind = TRUE;
  383.                                
  384.                             } else {
  385.                            
  386.                                 $bind = FALSE;
  387.                                
  388.                             }
  389.                            
  390.                         }
  391.                    
  392.                     } else {
  393.                    
  394.                         throw new PDOException('Failed to bind Parameters: Placeholder format not recognized, expected :placeholders or ? placeholders');
  395.                    
  396.                     }
  397.                    
  398.                 } else {
  399.                
  400.                     // validate which placeholder format is used to
  401.                     // ensure the paramaters are bound in a correct manner
  402.                     // if format isn't recognized throw an Exception
  403.                     if( preg_match_all('/\:[\w]+/',$stmt->sql,$count) ){
  404.                    
  405.                         // check where the statement is set
  406.                         // first check is the $stmt variable
  407.                         // second the internal object $this->stmt is checked
  408.                         // and then bind all parameters into statement
  409.                         if( $stmt->bindValue( $params , $params ) ){
  410.                        
  411.                             $bind = TRUE;
  412.                            
  413.                         }
  414.                        
  415.                     } elseif ( preg_match_all('/\?/',$stmt->sql,$count) ){
  416.                    
  417.                         // check where the statement is set
  418.                         // first check is the $stmt variable
  419.                         // second the internal object $this->stmt is checked
  420.                         // and then bind all parameters into statement
  421.                         if( $stmt->bindValue( 1 , $params ) ){
  422.                        
  423.                             $bind = TRUE;
  424.                            
  425.                         }
  426.                        
  427.                     } else {
  428.                    
  429.                         throw new PDOException('Failed to bind Parameters: Placeholder format not recognized, expected :placeholders or ? placeholders');
  430.                    
  431.                     }
  432.                    
  433.                 }
  434.                
  435.                 // If binding of parameters failed throw an Exception
  436.                 // otherwise return true.
  437.                 if( $bind ){
  438.                
  439.                     return true;
  440.                    
  441.                 } else {
  442.                
  443.                     throw new PDOException('Failed to bind Parameters: Parameter binding failed during value bind assignment');
  444.                
  445.                 }
  446.                
  447.             } else {
  448.            
  449.                 throw new PDOException("Failed to bind Parameters: Number of parameters supplied doesn't match the amount of parameters required by query statement.");
  450.            
  451.             }
  452.            
  453.         } catch( PDOException $e ) {
  454.        
  455.             // $this->errors[] = $e->getMessage();
  456.             $this->catchexception($e);
  457.             return false;
  458.            
  459.         }
  460.        
  461.     }
  462.    
  463.     public function fetch( $stmt=NULL , $all=TRUE , $style=0 ){
  464.    
  465.         try{
  466.        
  467.             if($this->connection == NULL ){
  468.            
  469.                 throw new PDOException('Failed to fetch data: No active database connection');
  470.                
  471.             }
  472.            
  473.             // make sure there is a prepared statement to run
  474.             // if not throw an Exception
  475.             if( !is_null($this->stmt) && !is_null($stmt) ) {
  476.            
  477.                 throw new PDOException('Failed to fetch data: No prepared statement to fetch data from');
  478.                
  479.             }
  480.            
  481.             // ensure that the fetch style supplied exist
  482.             // if not recognized throw an Exception
  483.             if( !array_key_exists( $style , $this->fstyle ) ){
  484.            
  485.                 throw new PDOException('Failed to fetch data: Fetch style not recognized');
  486.                
  487.             }
  488.            
  489.             // determine if a single record or all records are to be returned
  490.             if($all===TRUE){
  491.                
  492.                 $fetch='fetchAll';
  493.                
  494.             } else {
  495.            
  496.                 $fetch='fetch';
  497.            
  498.             }
  499.            
  500.             // check where the statement is set
  501.             // first check is the $stmt variable
  502.             // second the internal object $this->stmt is checked
  503.             if( !is_null( $stmt ) ){
  504.            
  505.                 return $stmt->$fetch( $this->fstyle[$style] );
  506.                
  507.             } else {
  508.            
  509.                 return $this->stmt->$fetch( $this->fstyle[$style] );
  510.                
  511.             }
  512.            
  513.         } catch( PDOException $e ) {
  514.        
  515.             // $this->errors[] = $e->getMessage();
  516.             $this->catchexception($e);
  517.             return false;
  518.        
  519.         }
  520.        
  521.     }
  522.     /*
  523.     public function getLastInsertID( ) {
  524.         return $this->connection->lastInsertId( );
  525.     }
  526.     */
  527.     public function flush(){
  528.         // flush statement object
  529.         $this->stmt = NULL;
  530.     }
  531.    
  532.     public function error($all=FALSE){
  533.         if($all==TRUE){
  534.             // var_dump all errors
  535.             return $this->errors;
  536.         }
  537.         // return last cast error
  538.         return end($this->errors);
  539.     }
  540.    
  541. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement