Exemplo n.º 1
0
 /**
  * Creates and initialize DBPreparedQuery object.
  *
  * @param string $query DB SQL query template.
  * @param string $types Parameters SQL types string ("idsb").
  * @param array $params List of the DB SQL query parameters.
  */
 public function __construct($query = "", $types = "", $params = [])
 {
     $this->query = $query;
     $this->types = $types;
     $this->params = $params;
     if (!empty($this->query)) {
         $this->type = $this->detectType();
     }
     parent::__construct($this->type);
 }
Exemplo n.º 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;
 }
Exemplo n.º 3
0
 /**
  * Executes SQL query.
  *
  * @param boolean $debug Debug mode flag.
  *
  * @return mixed Statement object or FALSE if an error occurred if SELECT
  *           query executed or number of affected rows on success if other
  *           type of query executed.
  */
 public function go($debug = false)
 {
     if ($debug) {
         DBQuery::showQueryDebugInfo($this->query, $this->types, $this->params);
     } else {
         if ($this->isSelect()) {
             return DBCore::doSelectQuery($this);
         } else {
             return DBCore::doUpdateQuery($this);
         }
     }
 }
Exemplo n.º 4
0
 /**
  * Executes SQL UPDATE query to the database.
  *
  * @param DBObject $dbObject DBObject to update.
  * @param bool $debug Debug mode flag.
  *
  * @return int Returns the number of affected rows on success, and -1 if
  *           the last query failed.
  */
 public static function updateDBObject($dbObject, $debug = false)
 {
     $fieldsList = $dbObject->getFieldsList();
     $idFieldName = $dbObject->getIdFieldName();
     $query = "UPDATE " . $dbObject->getTableName() . "\n                  SET " . DBPreparedQuery::sqlQMValuesString($fieldsList, $idFieldName) . "\n                  WHERE " . $idFieldName . " = ?\n                  LIMIT 1";
     $typesString = DBPreparedQuery::sqlTypesString($fieldsList, $idFieldName);
     if (Tools::isInteger($fieldsList[$idFieldName])) {
         $typesString .= "i";
     } else {
         $typesString .= "s";
     }
     $valuesList = self::createValuesList($fieldsList, $idFieldName);
     $valuesList[] = $dbObject->getId();
     if ($debug) {
         DBQuery::showQueryDebugInfo($query, $typesString, $valuesList);
     } else {
         return self::doUpdateQuery($query, $typesString, $valuesList);
     }
 }