Zend Framework

Refactor Zend_Db_Select::__toString() method

Details

  • Type: Improvement Improvement
  • Status: Resolved Resolved
  • Priority: Major Major
  • Resolution: Fixed
  • Affects Version/s: 0.9.0
  • Fix Version/s: 1.5.0
  • Component/s: Zend_Db_Select
  • Labels:
    None
  • Fix Version Priority:
    Nice to Have

Description

Extending behavior of Zend_Db_Select for rendering a query to a string, or supporting RDBMS-specific syntax, requires rewriting the whole __toString() method.

What I think we need to do is make it easier to extend Zend_Db_Select by breaking __toString() down into separate methods for each section of the statement, and allow setting of the class returned by select() in the adapter.

Then for situations such as DISTINCT ON support, the solution would be the following:

class MyExtendedSelect extends Zend_Db_Select 
{
     public function distinctOn(array $columns)
     {
         $this->_parts[self::DISTINCT] = $columns;
         return $this;
     }

     protected function _distinctToString() {
         if (is_array($this->_parts[self::DISTINCT])) {
             $sql .= " DISTINCT ON " . /* serialize columns here */;
	} else if ($this->_parts[self::DISTINCT] === true) {
             $sql .= " DISTINCT";
         }
     }
}

Activity

Hide
Bill Karwin added a comment -

Assigning to Mark Gibson. Thanks Mark!

Show
Bill Karwin added a comment - Assigning to Mark Gibson. Thanks Mark!
Hide
Bill Karwin added a comment -

I wrote a prototype implementation of this refactoring. It's in the ZF Laboratory svn repository:

http://framework.zend.com/fisheye/browse/Zend_Framework_Laboratory/library/Zend/Db/Select.php

Show
Bill Karwin added a comment - I wrote a prototype implementation of this refactoring. It's in the ZF Laboratory svn repository: http://framework.zend.com/fisheye/browse/Zend_Framework_Laboratory/library/Zend/Db/Select.php
Hide
Simon Mundy added a comment -

Resolved in trunk r8084

NOTE: The method names are simply:-

_renderFrom()
_renderWhere()
...etc..

(the 'toString' seemed superfluous)

Show
Simon Mundy added a comment - Resolved in trunk r8084 NOTE: The method names are simply:- _renderFrom() _renderWhere() ...etc.. (the 'toString' seemed superfluous)

People

Vote (0)
Watch (1)

Dates

  • Created:
    Updated:
    Resolved:

Time Tracking

Estimated:
3d
Original Estimate - 3 days
Remaining:
2d 21h 30m
Time Spent - 2 hours, 30 minutes Remaining Estimate - 2 days, 21 hours, 30 minutes
Logged:
2h 30m
Time Spent - 2 hours, 30 minutes Remaining Estimate - 2 days, 21 hours, 30 minutes