Created
May 9, 2013 05:55
-
-
Save oojacoboo/5545814 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Builds the SELECT part of a SQL statement based on a Criteria | |
| * taking into account select columns and 'as' columns (i.e. columns aliases) | |
| * Move from BasePeer to DBAdapter and turn from static to non static | |
| * | |
| * @param Criteria $criteria | |
| * @param array $fromClause | |
| * @param boolean $aliasAll | |
| * | |
| * @return string | |
| */ | |
| public function createSelectSqlPart(Criteria $criteria, &$fromClause, $aliasAll = false) | |
| { | |
| $selectClause = array(); | |
| if ($aliasAll) { | |
| $this->turnSelectColumnsToAliases($criteria); | |
| // no select columns after that, they are all aliases | |
| } else { | |
| foreach ($criteria->getSelectColumns() as $columnName) { | |
| // expect every column to be of "table.column" formation | |
| // it could be a function: e.g. MAX(books.price) | |
| $tableName = null; | |
| // $selectClause[] = $columnName; // the full column name: e.g. MAX(books.price) | |
| $parenPos = strrpos($columnName, '('); | |
| $dotPos = strrpos($columnName, '.', ($parenPos !== false ? $parenPos : 0)); | |
| if ($dotPos !== false) { | |
| if ($parenPos === false) { // table.column | |
| $tableName = substr($columnName, 0, $dotPos); | |
| $actualColumnName = substr($columnName, $dotPos); | |
| $selectClause[] = $this->quoteIdentifier($tableName) . $actualColumnName; | |
| } else { // FUNC(table.column) | |
| // functions may contain qualifiers so only take the last | |
| // word as the table name. | |
| // COUNT(DISTINCT books.price) | |
| $tableName = substr($columnName, $parenPos + 1, $dotPos - ($parenPos + 1)); | |
| $lastSpace = strrpos($tableName, ' '); | |
| if ($lastSpace !== false) { // COUNT(DISTINCT books.price) | |
| $tableName = substr($tableName, $lastSpace + 1); | |
| } | |
| } | |
| // is it a table alias? | |
| $tableName2 = $criteria->getTableForAlias($tableName); | |
| if ($tableName2 !== null) { | |
| $fromClause[] = $tableName2 . ' ' . $tableName; | |
| } else { | |
| $fromClause[] = $tableName; | |
| } | |
| } // if $dotPost !== false | |
| } | |
| } | |
| // set the aliases | |
| foreach ($criteria->getAsColumns() as $alias => $col) { | |
| $selectClause[] = $col . ' AS ' . $this->quoteIdentifier($alias); | |
| } | |
| $selectModifiers = $criteria->getSelectModifiers(); | |
| $queryComment = $criteria->getComment(); | |
| // Build the SQL from the arrays we compiled | |
| $sql = "SELECT " | |
| . ($queryComment ? '/* ' . $queryComment . ' */ ' : '') | |
| . ($selectModifiers ? (implode(' ', $selectModifiers) . ' ') : '') | |
| . implode(", ", $selectClause); | |
| return $sql; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment