Example #1
0
 /**
  * general insert or update query
  * @param $query
  * @param array|null $params
  * @return int
  * @throws \RuntimeException
  */
 public function query($query, $params = null)
 {
     try {
         // Start the clock
         $startTime = microtime(true);
         // if the db connection failed, fail right back...
         $this->enableActiveConnection();
         if (!$this->pdo) {
             return 0;
         }
         // prepare a query, bind a value to it and execute it
         $this->resetResults();
         $this->lastQuery = $query;
         $stmt = $this->pdo->prepare($query);
         if ($stmt) {
             $this->autoBind($stmt, $query, $params);
             if ($stmt->execute()) {
                 // Try and grab the resulting insert id
                 $this->lastInsertID = $this->pdo->lastInsertId();
                 $this->lastRowsAffected = $stmt->rowCount();
             }
             // Finished with the statement now
             $stmt = null;
         }
         $this->logger->logQuery('Generic SQL Query', $query, $params, microtime(true) - $startTime);
         return $this->lastRowsAffected;
     } catch (PDOException $e) {
         $this->logger->error('Query Failed. PDO threw an exception: ' . $e->getMessage(), array('query' => $query, 'args' => $params));
         throw new \RuntimeException("PDO Query Failed", 0, $e);
     }
 }