Example #1
0
 /**
  * sends the query / update / insert statement to the database and returns
  * a resource of the table
  *
  * @throws DatabaseException
  *
  * @param $query string
  *
  * @return resource
  */
 public function query($query)
 {
     if (empty($query) || !$this->db) {
         return false;
     }
     // if the resource type is not a mysql link it should try to reconnect
     if (!$this->db->isConnected()) {
         $this->db->ping();
     }
     // the last sql query
     $this->lastSql = (string) $query;
     // if the master is down a select may query the slave but it should not
     // insert anything
     if ($this->slaveConnection === $this->db && stripos(trim($query), 'SELECT') !== 0) {
         throw new DatabaseException(__METHOD__ . ' no Select on slave, abort !!', self::ERR_CONN, self::SEVERITY_LOG, __FILE__, __LINE__);
     }
     // tries execute the query and fetches the result
     $res = $this->db->getDbAdapter()->query($query);
     try {
         // in cases of errors
         if (!$res) {
             $this->error = $this->db->getDbAdapter()->getErrorInfo();
             $this->errorno = $this->db->getDbAdapter()->getErrorCode();
             throw new DatabaseException(__METHOD__ . "\nsql: {$this->lastSql}\nsql_error:{$this->error}\nsql_errorno:{$this->errorno}", self::ERR_EXEC, self::SEVERITY_LOG, __FILE__, __LINE__);
         } else {
             $this->insert_id = $this->db->getDbAdapter()->getLastInsertId();
         }
     } catch (DatabaseException $e) {
         if ($this->useException !== true) {
             return false;
         }
         throw $e;
     }
     // reset old errors
     $this->error = '';
     $this->errorno = 0;
     if ($res && $res instanceof \PDOStatement) {
         $this->affected_rows = $this->db->getDbAdapter()->getAffectedRows($res);
     } elseif ($res) {
         $this->affected_rows = $this->db->getDbAdapter()->getAffectedRows();
     } else {
         $this->affected_rows = 0;
     }
     return $res;
 }