Example #1
0
 /**
  * Selects DB record(s) for current DBObject table according to params.
  *
  * @param bool $debug Debug mode flag.
  *
  * @return mixed DBObject, array of DBObject or null.
  * @throws DBCoreException If some DB or query syntax errors occurred.
  */
 public function go($debug = false)
 {
     switch ($this->dbQuery->getType()) {
         case DBQueryType::SELECT:
             $this->dbQuery->query = "SELECT * FROM " . static::TABLE_NAME;
             break;
         case DBQueryType::UPDATE:
             $this->dbQuery->query = "UPDATE " . static::TABLE_NAME . " SET ";
             $this->dbQuery->sqlPushValues($this->dbQuery->fields);
             break;
         case DBQueryType::DELETE:
             $this->dbQuery->query = "DELETE FROM " . static::TABLE_NAME;
             break;
     }
     /*
      * Conditions
      */
     if ($this->isNewRecord()) {
         $this->dbQuery->prepareConditions();
     } else {
         $this->dbQuery->query .= " WHERE ";
         $this->dbQuery->sqlPushValues([static::ID_FIELD_NAME => $this->id]);
     }
     /*
      * Order
      */
     if ($this->isNewRecord()) {
         $this->dbQuery->prepareOrder();
     }
     /*
      * Limit
      */
     $count = null;
     if ($this->isNewRecord()) {
         $count = $this->dbQuery->prepareLimit();
     } else {
         $this->dbQuery->query .= " LIMIT 1";
         $count = 1;
     }
     if ($debug) {
         $this->dbQuery->debug();
     } else {
         if ($this->dbQuery->isSelector()) {
             $stmt = $this->dbQuery->go();
             if ($stmt !== false) {
                 $data = null;
                 if ($count !== 1) {
                     $data = DBCore::selectDBObjectsFromStatement($stmt, $this);
                 } else {
                     $data = DBCore::selectDBObjectFromStatement($stmt, $this);
                 }
                 $stmt->close();
                 return $data;
             }
             return null;
         }
         return $this->dbQuery->go();
     }
     return null;
 }
Example #2
0
 /**
  * Selects DB record(s) for current DBObject table according to params.
  *
  * @param boolean $debug Debug mode flag.
  *
  * @return mixed DBObject, array of DBObject or null.
  * @throws DBCoreException If some DB or query syntax errors occurred.
  */
 public function go($debug = false)
 {
     switch ($this->dbQuery->getType()) {
         case DBQueryType::SELECT:
             $this->dbQuery->query = "SELECT * FROM " . static::TABLE_NAME;
             break;
         case DBQueryType::UPDATE:
             $this->dbQuery->query = "UPDATE " . static::TABLE_NAME . " SET ";
             $this->dbQuery->sqlPushValues($this->dbQuery->fields);
             break;
     }
     /**
      * Conditions
      */
     if ($this->isNewRecord()) {
         if (!empty($this->dbQuery->conditions)) {
             $this->dbQuery->query .= " WHERE ";
             $this->dbQuery->sqlPushValues($this->dbQuery->conditions, " AND ");
         }
     } else {
         $this->dbQuery->query .= " WHERE ";
         $this->dbQuery->sqlPushValues(array(static::ID_FIELD_NAME => $this->id));
     }
     /**
      * Order
      */
     if ($this->isNewRecord()) {
         if (!empty($this->dbQuery->order)) {
             $this->dbQuery->query .= " ORDER BY";
             if (is_array($this->dbQuery->order)) {
                 foreach ($this->dbQuery->order as $fieldName => $ord) {
                     $this->dbQuery->query .= " " . $fieldName . " " . $ord . ",";
                 }
                 $this->dbQuery->query = substr($this->dbQuery->query, 0, strlen($this->dbQuery->query) - 1);
             } elseif (is_string($this->dbQuery->order)) {
                 $this->dbQuery->query .= " " . $this->dbQuery->order;
             }
         }
     }
     /**
      * Limit
      */
     if ($this->isNewRecord()) {
         if (!is_null($this->dbQuery->limit)) {
             if (Tools::isInteger($this->dbQuery->limit)) {
                 $this->dbQuery->query .= " LIMIT " . $this->dbQuery->limit;
             } elseif (is_array($this->dbQuery->limit) && count($this->dbQuery->limit) == 2) {
                 $offset = $this->dbQuery->limit[0];
                 $count = $this->dbQuery->limit[1];
                 if (Tools::isInteger($offset) && Tools::isInteger($count)) {
                     $this->dbQuery->query .= " LIMIT " . $offset . ", " . $count;
                 } else {
                     throw new DBCoreException("Invalid LIMIT param in select() method.");
                 }
             } else {
                 throw new DBCoreException("Invalid LIMIT param in select() method.");
             }
         }
     } else {
         $this->dbQuery->query .= " LIMIT 1";
     }
     if ($debug) {
         DBQuery::showQueryDebugInfo($this->dbQuery->query, $this->dbQuery->types, $this->dbQuery->params);
     } else {
         if ($this->dbQuery->isSelect()) {
             $stmt = $this->dbQuery->go();
             if ($stmt !== false) {
                 $data = null;
                 if ($stmt->num_rows > 1) {
                     $data = DBCore::selectDBObjectsFromStatement($stmt, $this);
                 } elseif ($stmt->num_rows == 1) {
                     $data = DBCore::selectDBObjectFromStatement($stmt, $this);
                 }
                 $stmt->close();
                 return $data;
             }
         } else {
             return $this->dbQuery->go();
         }
     }
     return null;
 }
Example #3
0
 /**
  * Selects DBObject list from database.
  *
  * @param string $query SQL query.
  * @param string $types Types string (ex: "isdb").
  * @param array $params Parameters in the same order like types string.
  * @param mixed $instance Instance of the some DBObject class or it's class name.
  *
  * @return DBObject Selected DBObject or NULL otherwise.
  */
 public static function selectDBObjects($query, $types, $params, $instance)
 {
     $stmt = DBCore::doSelectQuery($query, $types, $params);
     $obj = null;
     if ($stmt) {
         $obj = DBCore::selectDBObjectsFromStatement($stmt, $instance);
         $stmt->close();
     }
     return $obj;
 }