Beispiel #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;
 }
Beispiel #2
0
require_once "../modules/dbconnection.php";
use Asymptix\db\DBCore;
use Asymptix\db\DBPreparedQuery;
use Asymptix\core\OutputStream;
const RESULTS_PATH = "../classes/db/";
const CLASS_TPL = "templates/bean.tpl";
const AUTHOR = "Dmytro Zarezenko";
const EMAIL = "*****@*****.**";
OutputStream::start();
if (!file_exists(RESULTS_PATH) || is_file(RESULTS_PATH)) {
    OutputStream::msg(OutputStream::MSG_ERROR, "Destination directory '" . RESULTS_PATH . "' doesn't exists.");
    OutputStream::close();
    exit;
}
OutputStream::msg(OutputStream::MSG_INFO, "Reading tables list...");
$query = new DBPreparedQuery("SHOW TABLES");
$stmt = $query->go();
if ($stmt !== false) {
    $tpl = file_get_contents(CLASS_TPL);
    while ($resultSet = DBCore::bindResults($stmt)) {
        $tableName = $resultSet['TABLE_NAMES']['Tables_in_' . conf\Config::getDBConfigParam('DBNAME')];
        OutputStream::msg(OutputStream::MSG_INFO, "Reading structure for table '" . $tableName . "'...");
        $idFieldName = 'id';
        $fieldsListStr = "";
        $fieldsList = DBCore::getTableFieldsList($tableName);
        if (!empty($fieldsList)) {
            foreach ($fieldsList as $field => $attributes) {
                if ($attributes['key'] === 'PRI') {
                    $idFieldName = $field;
                }
                $fieldsListStr .= "        " . DBCore::getPrintableFieldString($field, $attributes);
Beispiel #3
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;
 }
Beispiel #4
0
 /**
  * Calls DBCore magic methods like:
  *    get[User]By[Id]($userId)
  *    get[User]By[Email]($email)
  *    get[Users]()
  *    delete[Users]($ids)
  *    delete[User]($userId)
  *    set[User]Activation($activationFieldName, $flagValue)
  *
  * @param string $methodName Name of the magic method.
  * @param array $methodParams List of dynamic parameters.
  *
  * @return mixed
  * @throws DBCoreException
  */
 public function __call($methodName, $methodParams)
 {
     if (strrpos($methodName, "ies") == strlen($methodName) - 3) {
         $methodName = substr($methodName, 0, strlen($methodName) - 3) . "ys";
     }
     /**
      * Get database record object by Id
      */
     if (preg_match("#get([a-zA-Z]+)ById#", $methodName, $matches)) {
         $dbSelector = new DBSelector($matches[1]);
         return $dbSelector->selectDBObjectById($methodParams[0]);
     }
     /**
      * Get database record object by some field value
      */
     if (preg_match("#get([a-zA-Z]+)By([a-zA-Z]+)#", $methodName, $matches)) {
         if (empty($methodParams[0])) {
             return null;
         }
         $dbSelector = new DBSelector($matches[1]);
         $fieldName = substr(strtolower(preg_replace("#([A-Z]{1})#", "_\$1", $matches[2])), 1);
         return $dbSelector->selectDBObjectByField($fieldName, $methodParams[0]);
     }
     /**
      * Get all database records
      */
     if (preg_match("#get([a-zA-Z]+)s#", $methodName, $matches)) {
         return self::Selector()->selectDBObjects();
     }
     /**
      * Delete selected records from the database
      */
     if (preg_match("#delete([a-zA-Z]+)s#", $methodName, $matches)) {
         $className = $matches[1];
         $idsList = $methodParams[0];
         $idsList = array_filter($idsList, "isInteger");
         if (!empty($idsList)) {
             $itemsNumber = count($idsList);
             $types = DBPreparedQuery::sqlSingleTypeString("i", $itemsNumber);
             $dbObject = new $className();
             if (!isInstanceOf($dbObject, $className)) {
                 throw new DBCoreException("Class with name '" . $className . "' is not exists");
             }
             $query = "DELETE FROM " . $dbObject->getTableName() . "\n                          WHERE " . $dbObject->getIdFieldName() . "\n                             IN (" . DBPreparedQuery::sqlQMString($itemsNumber) . ")";
             return self::doUpdateQuery($query, $types, $idsList);
         }
         return 0;
     }
     /**
      * Delete selected record from the database
      */
     if (preg_match("#delete([a-zA-Z]+)#", $methodName, $matches)) {
         return call_user_func(array(self::Instance(), $methodName . "s"), array($methodParams[0]));
     }
     /**
      * Set activation value of selected records
      */
     if (preg_match("#set([a-zA-Z]+)Activation#", $methodName, $matches)) {
         $className = $matches[1];
         if (strrpos($className, "ies") == strlen($className) - 3) {
             $className = substr($className, 0, strlen($className) - 3) . "y";
         } else {
             $className = substr($className, 0, strlen($className) - 1);
         }
         $idsList = $methodParams[0];
         $activationFieldName = $methodParams[1];
         $activationValue = $methodParams[2];
         if (empty($activationFieldName)) {
             throw new DBCoreException("Invalid activation field name");
         }
         $idsList = array_filter($idsList, "isInteger");
         if (!empty($idsList)) {
             $itemsNumber = count($idsList);
             $types = DBPreparedQuery::sqlSingleTypeString("i", $itemsNumber);
             $dbObject = new $className();
             if (!isInstanceOf($dbObject, $className)) {
                 throw new DBCoreException("Class with name '" . $className . "' is not exists");
             }
             $query = "UPDATE " . $dbObject->getTableName() . " SET `" . $activationFieldName . "` = '" . $activationValue . "'\n                          WHERE " . $dbObject->getIdFieldName() . " IN (" . DBPreparedQuery::sqlQMString($itemsNumber) . ")";
             return self::doUpdateQuery($query, $types, $idsList);
         }
     }
     throw new DBCoreException('No such method "' . $methodName . '"');
 }
Beispiel #5
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);
     }
 }