Example #1
0
 /**
  * Recreates the PDO statement
  *
  * To recreate a PDO statement, all attributes, options, and bindings are restored from the previous one, giving the
  * illusion that the PDO statement is the same one as before. However, since sometimes columns can only be bound
  * after a result set is retrieved, any errors binding columns here will be ignored, and the column bindings will be
  * tried again after the statement is next executed.
  *
  * @return bool whether successful
  * @throws \Compeek\PDOWrapper\NotConnectedException
  */
 protected function reconstructPdoStatement()
 {
     if ($this->pdoWrapper->reconstructPdoStatement($this, $this->prepared, $this->args)) {
         foreach ($this->pdoStatementAttributes as $attribute => $value) {
             $this->pdoStatement->setAttribute($attribute, $value);
         }
         $this->pdoStatementPostExecuteBindColumnNames = array();
         foreach ($this->pdoStatementBindColumns as $column => $args) {
             $args[1] =& $args[1];
             // ensure reference is passed to function since PHP seems to convert reference to value if no other variables reference data (e.g. if column bound to local variable in function that has ended)
             try {
                 $success = call_user_func_array(array($this->pdoStatement, 'bindColumn'), $args);
             } catch (\PDOException $e) {
                 $success = false;
             }
             if (!$success) {
                 // column cannot be bound before statement execution
                 $this->pdoStatementPostExecuteBindColumnNames[$column] = $column;
             }
         }
         foreach ($this->pdoStatementBindParams as $args) {
             $args[1] =& $args[1];
             // ensure reference is passed to function since PHP seems to convert reference to value if no other variables reference data (e.g. if param bound to local variable in function that has ended)
             call_user_func_array(array($this->pdoStatement, 'bindParam'), $args);
         }
         foreach ($this->pdoStatementBindValues as $args) {
             call_user_func_array(array($this->pdoStatement, 'bindValue'), $args);
         }
         if ($this->pdoStatementFetchModeArgs !== null) {
             call_user_func_array(array($this->pdoStatement, 'setFetchMode'), $this->pdoStatementFetchModeArgs);
         }
         return true;
     } else {
         // applies only if PDO::ATTR_ERRMODE attribute != PDO::ERRMODE_EXCEPTION (will not reach here on error otherwise)
         return false;
     }
 }