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
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;
 }