Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Commit

Permalink
Close #5484
Browse files Browse the repository at this point in the history
Merge branch 'feature/5484-db-select-without-table' into develop

* feature/5484-db-select-without-table:
  Zend\Db\Sql\Select Fixed variable reference
  revert decorators
  allow use db functions without table
  • Loading branch information
Ralph Schindler committed Mar 6, 2014
2 parents d02d04d + e322a50 commit 36f262f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 29 deletions.
63 changes: 34 additions & 29 deletions library/Zend/Db/Sql/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ class Select extends AbstractSql implements SqlInterface, PreparableSqlInterface
array(1 => '%1$s', 2 => '%1$s AS %2$s', 'combinedby' => ', '),
null
),
'SELECT %1$s' => array(
array(1 => '%1$s', 2 => '%1$s AS %2$s', 'combinedby' => ', '),
),
),
self::JOINS => array(
'%1$s' => array(
Expand Down Expand Up @@ -584,38 +587,38 @@ protected function processSelect(PlatformInterface $platform, DriverInterface $d
{
$expr = 1;

if (!$this->table) {
return null;
}

$table = $this->table;
$schema = $alias = null;
if ($this->table) {
$table = $this->table;
$schema = $alias = null;

if (is_array($table)) {
$alias = key($this->table);
$table = current($this->table);
}
if (is_array($table)) {
$alias = key($this->table);
$table = current($this->table);
}

// create quoted table name to use in columns processing
if ($table instanceof TableIdentifier) {
list($table, $schema) = $table->getTableAndSchema();
}
// create quoted table name to use in columns processing
if ($table instanceof TableIdentifier) {
list($table, $schema) = $table->getTableAndSchema();
}

if ($table instanceof Select) {
$table = '(' . $this->processSubselect($table, $platform, $driver, $parameterContainer) . ')';
} else {
$table = $platform->quoteIdentifier($table);
}
if ($table instanceof Select) {
$table = '(' . $this->processSubselect($table, $platform, $driver, $parameterContainer) . ')';
} else {
$table = $platform->quoteIdentifier($table);
}

if ($schema) {
$table = $platform->quoteIdentifier($schema) . $platform->getIdentifierSeparator() . $table;
}
if ($schema) {
$table = $platform->quoteIdentifier($schema) . $platform->getIdentifierSeparator() . $table;
}

if ($alias) {
$fromTable = $platform->quoteIdentifier($alias);
$table = $this->renderTable($table, $fromTable);
if ($alias) {
$fromTable = $platform->quoteIdentifier($alias);
$table = $this->renderTable($table, $fromTable);
} else {
$fromTable = $table;
}
} else {
$fromTable = $table;
$fromTable = '';
}

if ($this->prefixColumnsWithTable) {
Expand All @@ -634,7 +637,7 @@ protected function processSelect(PlatformInterface $platform, DriverInterface $d
continue;
}

if ($column instanceof Expression) {
if ($column instanceof ExpressionInterface) {
$columnParts = $this->processExpression(
$column,
$platform,
Expand Down Expand Up @@ -694,7 +697,7 @@ protected function processSelect(PlatformInterface $platform, DriverInterface $d
}

if ($this->quantifier) {
if ($this->quantifier instanceof Expression) {
if ($this->quantifier instanceof ExpressionInterface) {
$quantifierParts = $this->processExpression($this->quantifier, $platform, $driver, 'quantifier');
if ($parameterContainer) {
$parameterContainer->merge($quantifierParts->getParameterContainer());
Expand All @@ -705,7 +708,9 @@ protected function processSelect(PlatformInterface $platform, DriverInterface $d
}
}

if (isset($quantifier)) {
if (!isset($table)) {
return array($columns);
} elseif (isset($quantifier)) {
return array($quantifier, $columns, $table);
} else {
return array($columns, $table);
Expand Down
11 changes: 11 additions & 0 deletions tests/ZendTest/Db/Sql/SelectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1173,6 +1173,16 @@ public function providerData()
'processOffset' => array('?')
);

// functions without table
$select46 = new Select;
$select46->columns(array(
new Expression('SOME_DB_FUNCTION_ONE()'),
'foo' => new Expression('SOME_DB_FUNCTION_TWO()'),
));
$sqlPrep46 = 'SELECT SOME_DB_FUNCTION_ONE() AS Expression1, SOME_DB_FUNCTION_TWO() AS "foo"';
$sqlStr46 = 'SELECT SOME_DB_FUNCTION_ONE() AS Expression1, SOME_DB_FUNCTION_TWO() AS "foo"';
$params46 = array();
$internalTests46 = array();
/**
* $select = the select object
* $sqlPrep = the sql as a result of preparation
Expand Down Expand Up @@ -1229,6 +1239,7 @@ public function providerData()
array($select43, $sqlPrep43, array(), $sqlStr43, $internalTests43),
array($select44, $sqlPrep44, array(), $sqlStr44, $internalTests44),
array($select45, $sqlPrep45, $params45, $sqlStr45, $internalTests45),
array($select46, $sqlPrep46, $params46, $sqlStr46, $internalTests46),
);
}
}

0 comments on commit 36f262f

Please sign in to comment.