Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Db\Sql\Select use functions without table #5484

Merged
merged 2 commits into from

2 participants

@turrsis

Allow to use functions without table, such as : SELECT LAST_INSERT_ID()

@ralphschindler
Collaborator

You have multiple things going on in this PR (it touches the SqlServer & Oracle SQL decorators, as well as addresses the original topic of Select without table usage.)

@turrsis

fixed

@ralphschindler ralphschindler added the Db label
@ralphschindler ralphschindler self-assigned this
@ralphschindler ralphschindler added this to the 2.3.0 milestone
@ralphschindler ralphschindler referenced this pull request from a commit
@ralphschindler ralphschindler Merge #5484
Merge branch 'db-sql-select-functions-without-table' of git://github.com/turrsis/zf2 into feature/5484-db-select-without-table

* 'db-sql-select-functions-without-table' of git://github.com/turrsis/zf2:
  revert decorators
  allow use db functions without table
3b5f292
@ralphschindler ralphschindler referenced this pull request from a commit
@ralphschindler ralphschindler Close #5484
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
36f262f
@ralphschindler ralphschindler merged commit c29932a into from
@turrsis turrsis deleted the branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 16, 2013
  1. @turrsis
Commits on Nov 21, 2013
  1. @turrsis

    revert decorators

    turrsis authored
This page is out of date. Refresh to see the latest.
Showing with 45 additions and 29 deletions.
  1. +34 −29 library/Zend/Db/Sql/Select.php
  2. +11 −0 tests/ZendTest/Db/Sql/SelectTest.php
View
63 library/Zend/Db/Sql/Select.php
@@ -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(
@@ -647,38 +650,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) {
@@ -697,7 +700,7 @@ protected function processSelect(PlatformInterface $platform, DriverInterface $d
continue;
}
- if ($column instanceof Expression) {
+ if ($column instanceof ExpressionInterface) {
$columnParts = $this->processExpression(
$column,
$platform,
@@ -757,7 +760,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());
@@ -768,7 +771,9 @@ protected function processSelect(PlatformInterface $platform, DriverInterface $d
}
}
- if (isset($quantifier)) {
+ if (!$this->table) {
+ return array($columns);
+ } elseif (isset($quantifier)) {
return array($quantifier, $columns, $table);
} else {
return array($columns, $table);
View
11 tests/ZendTest/Db/Sql/SelectTest.php
@@ -1157,6 +1157,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
@@ -1213,6 +1223,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),
);
}
}
Something went wrong with that request. Please try again.