示例#1
0
文件: Query.php 项目: abcarroll/DABL
 /**
  * Builds and returns the query string
  *
  * @param mixed $conn Database connection to use
  * @return QueryStatement
  */
 function getQuery(PDO $conn = null)
 {
     if (null === $conn && class_exists('DBManager')) {
         $conn = DBManager::getConnection();
     }
     // the QueryStatement for the Query
     $stmnt = new QueryStatement($conn);
     // the string $statement will use
     $qry_s = '';
     $action = $this->_action;
     switch ($action) {
         default:
         case self::ACTION_COUNT:
         case self::ACTION_SELECT:
             $columns_stmnt = $this->getColumnsClause($conn);
             $stmnt->addIdentifiers($columns_stmnt->identifiers);
             $stmnt->addParams($columns_stmnt->params);
             $qry_s .= 'SELECT ' . $columns_stmnt->string . "\nFROM ";
             break;
         case self::ACTION_DELETE:
             $qry_s .= "DELETE\nFROM ";
             break;
         case self::ACTION_UPDATE:
             $qry_s .= "UPDATE\n";
             break;
     }
     $table_stmnt = $this->getTablesClause($conn);
     $stmnt->addIdentifiers($table_stmnt->identifiers);
     $stmnt->addParams($table_stmnt->params);
     $qry_s .= $table_stmnt->string;
     if ($this->_joins) {
         foreach ($this->_joins as $join) {
             $join_stmnt = $join->getQueryStatement($conn);
             $qry_s .= "\n\t" . $join_stmnt->string;
             $stmnt->addParams($join_stmnt->params);
             $stmnt->addIdentifiers($join_stmnt->identifiers);
         }
     }
     if (self::ACTION_UPDATE === $action) {
         if (empty($this->_updateColumnValues)) {
             throw new RuntimeException('Unable to build UPDATE query without update column values');
         }
         $column_updates = array();
         foreach ($this->_updateColumnValues as $column_name => &$column_value) {
             $column_updates[] = QueryStatement::IDENTIFIER . '=' . QueryStatement::PARAM;
             $stmnt->addIdentifier($column_name);
             $stmnt->addParam($column_value);
         }
         $qry_s .= "\nSET " . implode(',', $column_updates);
     }
     $where_stmnt = $this->getWhereClause();
     if (null !== $where_stmnt && $where_stmnt->string !== '') {
         $qry_s .= "\nWHERE " . $where_stmnt->string;
         $stmnt->addParams($where_stmnt->params);
         $stmnt->addIdentifiers($where_stmnt->identifiers);
     }
     if ($this->_groups) {
         $clause = $this->getGroupByClause();
         $stmnt->addIdentifiers($clause->identifiers);
         $stmnt->addParams($clause->params);
         $qry_s .= $clause->string;
     }
     if (null !== $this->getHaving()) {
         $having_stmnt = $this->getHaving()->getQueryStatement();
         if (null !== $having_stmnt) {
             $qry_s .= "\nHAVING " . $having_stmnt->string;
             $stmnt->addParams($having_stmnt->params);
             $stmnt->addIdentifiers($having_stmnt->identifiers);
         }
     }
     if ($action !== self::ACTION_COUNT && $this->_orders) {
         $clause = $this->getOrderByClause();
         $stmnt->addIdentifiers($clause->identifiers);
         $stmnt->addParams($clause->params);
         $qry_s .= $clause->string;
     }
     if (null !== $this->_limit) {
         if ($conn) {
             if (class_exists('DBMSSQL') && $conn instanceof DBMSSQL) {
                 $qry_s = QueryStatement::embedIdentifiers($qry_s, $stmnt->getIdentifiers(), $conn);
                 $stmnt->setIdentifiers(array());
             }
             $conn->applyLimit($qry_s, $this->_offset, $this->_limit);
         } else {
             $qry_s .= "\nLIMIT " . ($this->_offset ? $this->_offset . ', ' : '') . $this->_limit;
         }
     }
     if (self::ACTION_COUNT === $action && $this->needsComplexCount()) {
         $qry_s = "SELECT count(0)\nFROM ({$qry_s}) a";
     }
     $stmnt->string = $qry_s;
     return $stmnt;
 }