[Solar-svn] Revision 2834
pmjones at solarphp.com
pmjones at solarphp.com
Sat Oct 6 11:18:12 CDT 2007
Solar_Sql_Select
----------------
* [ADD] Per intuitive usage from Chris Cornutt, adding fetchAll(), fetchOne(),
etc. methods. Thanks Chris.
* [FIX] When finding identifer aliases, use strripos() to get the right-most
AS (vice stripos(), which finds the left-most one).
* [CHG] In queries with only one source (e.g., FROM a single table) column names are no longer prefixed. This helps soothe SQLite when using a SELECT as the source for a JOIN (it has problems with the ORDER BY clause in some cases).
* [DEL] Removed deprecated methods fetchRow() and fetchRowset().
Modified: trunk/Solar/Sql/Select.php
===================================================================
--- trunk/Solar/Sql/Select.php 2007-10-06 16:14:23 UTC (rev 2833)
+++ trunk/Solar/Sql/Select.php 2007-10-06 16:18:11 UTC (rev 2834)
@@ -52,14 +52,14 @@
* // limit by which page of results we want
* $select->limitPage(1);
*
- * // get a PDOStatement object (the default)
- * $result = $select->fetch(); // or fetch('pdo')
+ * // get a PDOStatement object
+ * $result = $select->fetchPdo();
*
- * // alternatively, get an array of data
- * $rows = $select->fetch('all');
+ * // alternatively, get an array of all rows
+ * $rows = $select->fetchAll();
*
- * // or, get a Solar_Sql_Rowset object
- * $rows = $select->fetch('rowset');
+ * // or an array of one row
+ * $rows = $select->fetchOne();
*
* // find out the count of rows, and how many pages there are.
* // this comes back as an array('count' => ?, 'pages' => ?).
@@ -883,13 +883,10 @@
* @param string $type The type of fetch to perform (all, one, col,
* etc). Default is 'pdo'.
*
- * @param string $class When fetching 'row' or 'rowset', use this as
- * the return class.
- *
* @return mixed The query results.
*
*/
- public function fetch($type = 'pdo', $class = null)
+ public function fetch($type = 'pdo')
{
// does the fetch-method exist? (this allows for extended
// adapters to define their own fetch methods)
@@ -905,6 +902,12 @@
$this->_parts['from'] = array();
$this->_parts['join'] = array();
+ // get a count of how many sources there are. if there's only 1, we
+ // won't use column-name prefixes below. this will help soothe SQLite
+ // on JOINs of sub-selects. e.g., `JOIN (SELECT alias.col FROM tbl AS alias) ...`
+ // won't work right, it needs `JOIN (SELECT col FROM tbl AS alias)`.
+ $count_sources = count($this->_sources);
+
// build from sources.
foreach ($this->_sources as $source) {
@@ -941,10 +944,10 @@
$parens = strpos($col, '(');
// choose our column-name deconfliction strategy
- if ($prefix == '' || $parens) {
- // no prefix (generally because of countPages()).
- // if there are parens in the name, it's a function,
- // so don't prefix it.
+ if ($prefix == '' || $parens || $count_sources == 1) {
+ // - if no prefix, that's a no-brainer.
+ // - if there are parens in the name, it's a function.
+ // - if there's only one source, deconfliction not needed.
$this->_parts['cols'][] = $col;
} else {
// auto deconfliction
@@ -957,6 +960,7 @@
return $this->_sql->$fetch($this->_parts, $this->_bind, $class);
}
+
public function fetchAll()
{
return $this->fetch('all');
@@ -992,16 +996,6 @@
return $this->fetch('one');
}
- public function fetchRow($class = null)
- {
- return $this->fetch('row', $class);
- }
-
- public function fetchRowset($class = null)
- {
- return $this->fetch('rowset', $class);
- }
-
public function fetchSql()
{
return $this->fetch('sql');
@@ -1030,16 +1024,16 @@
// add a single COUNT() column
$select->_addSource(
- 'cols', // type
- null, // name
- null, // orig
- null, // join
- null, // cond
+ 'cols', // type
+ null, // name
+ null, // orig
+ null, // join
+ null, // cond
"COUNT($col)"
);
// get the count and calculate pages
- $count = $select->fetch('value');
+ $count = $select->fetchValue();
$pages = 0;
if ($count > 0) {
$pages = ceil($count / $this->_paging);
@@ -1132,8 +1126,9 @@
*/
protected function _origAlias($name)
{
- // does the name have an "AS" alias?
- $pos = stripos($name, ' AS ');
+ // does the name have an "AS" alias? pick the right-most one near the
+ // end of the string (note the "rr" in strripos).
+ $pos = strripos($name, ' AS ');
if ($pos !== false) {
return array(
'orig' => trim(substr($name, 0, $pos)),
More information about the Solar-svn
mailing list