Example #1
0
 /**
  * Execute a query
  *
  * @param array $bind_variables
  *
  * @return WT_DBStatement
  * @throws Exception
  */
 public function execute($bind_variables = array())
 {
     if ($this->executed) {
         throw new Exception('WT_DBStatement::execute() called twice.');
     }
     // Turn booleans into integers.  Otherwise MySQL’s strict mode can get upset.
     foreach ($bind_variables as &$bind_variable) {
         if ($bind_variable === false) {
             // Otherwise true=>'1' and false=>''
             $bind_variable = 0;
         }
     }
     $start = microtime(true);
     $this->pdo_statement->execute($bind_variables);
     $end = microtime(true);
     // If it was a SELECT statement, we cannot run it again.
     $this->executed = strpos($this->pdo_statement->queryString, 'SELECT') === 0;
     WT_DB::logQuery($this->pdo_statement->queryString, $this->pdo_statement->rowCount(), $end - $start, $bind_variables);
     return $this;
 }
Example #2
0
 public function __call($function, $params)
 {
     switch ($function) {
         case 'closeCursor':
             $this->executed = false;
             // no break;
         // no break;
         case 'bindColumn':
         case 'bindParam':
         case 'bindValue':
             // TODO: bind variables need to be stored in $this->bind_variables so we can log them
         // TODO: bind variables need to be stored in $this->bind_variables so we can log them
         case 'setAttribute':
         case 'setFetchMode':
             // Functions that return no values become fluent
             call_user_func_array(array($this->pdostatement, $function), $params);
             return $this;
         case 'execute':
             if ($this->executed) {
                 trigger_error('WT_DBStatement::execute() called twice.', E_USER_ERROR);
             } else {
                 if ($params) {
                     $this->bind_variables = $params[0];
                     foreach ($params[0] as &$param) {
                         if ($param === false) {
                             // For consistency, otherwise true=>'1' and false=>''
                             $param = 0;
                         }
                     }
                 }
                 $start = microtime(true);
                 call_user_func_array(array($this->pdostatement, $function), $params);
                 $end = microtime(true);
                 $this->executed = !preg_match('/^(insert|delete|update|create|alter) /i', $this->pdostatement->queryString);
                 WT_DB::logQuery($this->pdostatement->queryString, $this->pdostatement->rowCount(), $end - $start, $this->bind_variables);
                 return $this;
             }
         case 'fetch':
         case 'fetchColumn':
         case 'fetchObject':
         case 'fetchAll':
             // Automatically execute the query
             if (!$this->executed) {
                 $this->execute();
                 $this->executed = true;
             }
             // no break;
         // no break;
         default:
             return call_user_func_array(array($this->pdostatement, $function), $params);
     }
 }