[limb-svn] r6005 - 3.x/trunk/limb/dbal/src/query

svn at limb-project.com svn at limb-project.com
Wed Jun 20 01:14:49 MSD 2007


Author: pachanga
Date: 2007-06-20 01:14:49 +0400 (Wed, 20 Jun 2007)
New Revision: 6005
URL: http://fisheye.limb-project.com/changelog/limb/?cs=6005

Modified:
   3.x/trunk/limb/dbal/src/query/lmbCriteriaQuery.class.php
   3.x/trunk/limb/dbal/src/query/lmbDeleteQuery.class.php
   3.x/trunk/limb/dbal/src/query/lmbInsertQuery.class.php
   3.x/trunk/limb/dbal/src/query/lmbSelectQuery.class.php
   3.x/trunk/limb/dbal/src/query/lmbSelectQueryDecorator.class.php
   3.x/trunk/limb/dbal/src/query/lmbTableRecordsQuery.class.php
   3.x/trunk/limb/dbal/src/query/lmbTemplateQuery.class.php
   3.x/trunk/limb/dbal/src/query/lmbUpdateQuery.class.php
Log:
-- lmbTemplateQuery::_declareHints() extracts hints from sql automatically(it resolves DBAL-8)

Modified: 3.x/trunk/limb/dbal/src/query/lmbCriteriaQuery.class.php
===================================================================
--- 3.x/trunk/limb/dbal/src/query/lmbCriteriaQuery.class.php	2007-06-19 21:12:16 UTC (rev 6004)
+++ 3.x/trunk/limb/dbal/src/query/lmbCriteriaQuery.class.php	2007-06-19 21:14:49 UTC (rev 6005)
@@ -1,85 +1,80 @@
-<?php
+<?php
 /*
  * Limb PHP Framework
  *
  * @link http://limb-project.com 
  * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
  * @license    LGPL http://www.gnu.org/copyleft/lesser.html 
- */
-lmb_require('limb/dbal/src/query/lmbTemplateQuery.class.php');
+ */
+lmb_require('limb/dbal/src/query/lmbTemplateQuery.class.php');
 lmb_require('limb/dbal/src/criteria/lmbSQLCriteria.class.php');
 
-/**
- * class lmbCriteriaQuery.
- *
- * @package dbal
- * @version $Id$
+/**
+ * class lmbCriteriaQuery.
+ *
+ * @package dbal
+ * @version $Id$
  */
-class lmbCriteriaQuery extends lmbTemplateQuery
-{
-  protected $_criterias;
-  protected $_stmt_values = array();
-
-  protected function _declareHints()
-  {
-    return array('where');
-  }
-
-  function addCriteria($criteria)
-  {
-    $this->_criterias[] = lmbSQLCriteria :: objectify($criteria);
-  }
-
-  function getStatementValues()
-  {
-    return $this->_stmt_values;
-  }
-
-  function getStatement()
-  {
-    $stmt = parent :: getStatement();
-
-    foreach($this->_stmt_values as $key => $value)
-      $stmt->set($key, $value);
-
-    return $stmt;
-  }
-
-  protected function _getWhereHint()
-  {
-    if(count($this->_criterias) == 0)
-      return '';
-
-    $implode = array();
-    foreach($this->_criterias as $criteria)
-    {
-      $implode[] = $criteria->toStatementString($this->_stmt_values);
-    }
-
-    $where = implode(' AND ', $implode);
-
-    if($this->_whereClauseExists($where_args))
-    {
-      if($where_args)
-        return 'AND ' . $where;
-      else
-        return $where;
-    }
-    else
-      return 'WHERE ' . $where;
-  }
-
-  protected function _whereClauseExists(&$args = array())
-  {
-    //primitive check if WHERE was already in sql
-    //!!!make it better later
-    if(preg_match('~(?<=\Wfrom).+\Wwhere\s+(.*)~si', $this->_getNoHintsSQL(), $matches))
-    {
-      if(preg_match('~([a-zA-Z].*)$~si', $matches[1], $args_matches))
-        $args = $args_matches[1];
-      return true;
-    }
-    return false;
-  }
-}
-?>
+class lmbCriteriaQuery extends lmbTemplateQuery
+{
+  protected $_criterias;
+  protected $_stmt_values = array();
+
+  function addCriteria($criteria)
+  {
+    $this->_criterias[] = lmbSQLCriteria :: objectify($criteria);
+  }
+
+  function getStatementValues()
+  {
+    return $this->_stmt_values;
+  }
+
+  function getStatement()
+  {
+    $stmt = parent :: getStatement();
+
+    foreach($this->_stmt_values as $key => $value)
+      $stmt->set($key, $value);
+
+    return $stmt;
+  }
+
+  protected function _getWhereHint()
+  {
+    if(count($this->_criterias) == 0)
+      return '';
+
+    $implode = array();
+    foreach($this->_criterias as $criteria)
+    {
+      $implode[] = $criteria->toStatementString($this->_stmt_values);
+    }
+
+    $where = implode(' AND ', $implode);
+
+    if($this->_whereClauseExists($where_args))
+    {
+      if($where_args)
+        return 'AND ' . $where;
+      else
+        return $where;
+    }
+    else
+      return 'WHERE ' . $where;
+  }
+
+  protected function _whereClauseExists(&$args = array())
+  {
+    //primitive check if WHERE was already in sql
+    //!!!make it better later
+    if(preg_match('~(?<=\Wfrom).+\Wwhere\s+(.*)~si', $this->_getNoHintsSQL(), $matches))
+    {
+      if(preg_match('~([a-zA-Z].*)$~si', $matches[1], $args_matches))
+        $args = $args_matches[1];
+      return true;
+    }
+    return false;
+  }
+}
+?>

Modified: 3.x/trunk/limb/dbal/src/query/lmbDeleteQuery.class.php
===================================================================
--- 3.x/trunk/limb/dbal/src/query/lmbDeleteQuery.class.php	2007-06-19 21:12:16 UTC (rev 6004)
+++ 3.x/trunk/limb/dbal/src/query/lmbDeleteQuery.class.php	2007-06-19 21:14:49 UTC (rev 6005)
@@ -1,37 +1,32 @@
-<?php
+<?php
 /*
  * Limb PHP Framework
  *
  * @link http://limb-project.com 
  * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
  * @license    LGPL http://www.gnu.org/copyleft/lesser.html 
- */
+ */
 lmb_require('limb/dbal/src/query/lmbCriteriaQuery.class.php');
 
-/**
- * class lmbDeleteQuery.
- *
- * @package dbal
- * @version $Id$
+/**
+ * class lmbDeleteQuery.
+ *
+ * @package dbal
+ * @version $Id$
  */
-class lmbDeleteQuery extends lmbCriteriaQuery
-{
-  protected $_table;
-
-  function __construct($table, $conn)
-  {
-    $this->_table = $table;
-    parent :: __construct("DELETE FROM %table% %where%", $conn);
-  }
-
-  protected function _declareHints()
-  {
-    return array('table', 'where');
-  }
-
-  protected function _getTableHint()
-  {
-    return $this->_conn->quoteIdentifier($this->_table);
-  }
-}
-?>
+class lmbDeleteQuery extends lmbCriteriaQuery
+{
+  protected $_table;
+
+  function __construct($table, $conn)
+  {
+    $this->_table = $table;
+    parent :: __construct("DELETE FROM %table% %where%", $conn);
+  }
+
+  protected function _getTableHint()
+  {
+    return $this->_conn->quoteIdentifier($this->_table);
+  }
+}
+?>

Modified: 3.x/trunk/limb/dbal/src/query/lmbInsertQuery.class.php
===================================================================
--- 3.x/trunk/limb/dbal/src/query/lmbInsertQuery.class.php	2007-06-19 21:12:16 UTC (rev 6004)
+++ 3.x/trunk/limb/dbal/src/query/lmbInsertQuery.class.php	2007-06-19 21:14:49 UTC (rev 6005)
@@ -1,75 +1,70 @@
-<?php
+<?php
 /*
  * Limb PHP Framework
  *
  * @link http://limb-project.com 
  * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
  * @license    LGPL http://www.gnu.org/copyleft/lesser.html 
- */
-lmb_require('limb/dbal/src/query/lmbTemplateQuery.class.php');
-
+ */
+lmb_require('limb/dbal/src/query/lmbTemplateQuery.class.php');
+
 define('LIMB_INSERT_QUERY_NON_VALUE', uniqid());
 
-/**
- * class lmbInsertQuery.
- *
- * @package dbal
- * @version $Id$
+/**
+ * class lmbInsertQuery.
+ *
+ * @package dbal
+ * @version $Id$
  */
-class lmbInsertQuery extends lmbTemplateQuery
-{
-  protected $_table;
-  protected $_fields = array();
-  protected $_set_values = array();
-
-  function __construct($table, $conn)
-  {
-    $this->_table = $table;
-    parent :: __construct("INSERT INTO %table% (%fields%) VALUES (%values%)", $conn);
-  }
-
-  protected function _declareHints()
-  {
-    return array('table', 'fields', 'values');
-  }
-
-  function addField($field, $value = LIMB_INSERT_QUERY_NON_VALUE)
-  {
-    $this->_fields[$field] = $value;
-  }
-
-  protected function _getTableHint()
-  {
-    return $this->_conn->quoteIdentifier($this->_table);
-  }
-
-  protected function _getFieldsHint()
-  {
-    return implode(',', array_map(array($this->_conn, 'quoteIdentifier'), array_keys($this->_fields)));
-  }
-
-  protected function _getValuesHint()
-  {
-    $values = array();
-    foreach($this->_fields as $field => $value)
-    {
-      if($value !== LIMB_INSERT_QUERY_NON_VALUE)
-        $this->_set_values[$field] = $value;
-
-      $values[] = ":{$field}:";
-    }
-
-    return implode(',', $values);
-  }
-
-  function getStatement()
-  {
-    $stmt = parent :: getStatement();
-    foreach($this->_set_values as $key => $value)
-      $stmt->set($key, $value);
-
-    return $stmt;
-  }
-
-}
-?>
+class lmbInsertQuery extends lmbTemplateQuery
+{
+  protected $_table;
+  protected $_fields = array();
+  protected $_set_values = array();
+
+  function __construct($table, $conn)
+  {
+    $this->_table = $table;
+    parent :: __construct("INSERT INTO %table% (%fields%) VALUES (%values%)", $conn);
+  }
+
+  function addField($field, $value = LIMB_INSERT_QUERY_NON_VALUE)
+  {
+    $this->_fields[$field] = $value;
+  }
+
+  protected function _getTableHint()
+  {
+    return $this->_conn->quoteIdentifier($this->_table);
+  }
+
+  protected function _getFieldsHint()
+  {
+    return implode(',', array_map(array($this->_conn, 'quoteIdentifier'), array_keys($this->_fields)));
+  }
+
+  protected function _getValuesHint()
+  {
+    $values = array();
+    foreach($this->_fields as $field => $value)
+    {
+      if($value !== LIMB_INSERT_QUERY_NON_VALUE)
+        $this->_set_values[$field] = $value;
+
+      $values[] = ":{$field}:";
+    }
+
+    return implode(',', $values);
+  }
+
+  function getStatement()
+  {
+    $stmt = parent :: getStatement();
+    foreach($this->_set_values as $key => $value)
+      $stmt->set($key, $value);
+
+    return $stmt;
+  }
+
+}
+?>

Modified: 3.x/trunk/limb/dbal/src/query/lmbSelectQuery.class.php
===================================================================
--- 3.x/trunk/limb/dbal/src/query/lmbSelectQuery.class.php	2007-06-19 21:12:16 UTC (rev 6004)
+++ 3.x/trunk/limb/dbal/src/query/lmbSelectQuery.class.php	2007-06-19 21:14:49 UTC (rev 6005)
@@ -1,258 +1,253 @@
-<?php
+<?php
 /*
  * Limb PHP Framework
  *
  * @link http://limb-project.com 
  * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
  * @license    LGPL http://www.gnu.org/copyleft/lesser.html 
- */
-lmb_require('limb/dbal/src/query/lmbCriteriaQuery.class.php');
-
+ */
+lmb_require('limb/dbal/src/query/lmbCriteriaQuery.class.php');
+
 //TODO: use primitive lexer for parsing sql templates someday...
 
-/**
- * class lmbSelectQuery.
- *
- * @package dbal
- * @version $Id$
+/**
+ * class lmbSelectQuery.
+ *
+ * @package dbal
+ * @version $Id$
  */
-class lmbSelectQuery extends lmbCriteriaQuery
-{
-  protected $_fields = array();
-  protected $_tables = array();
-  protected $_left_join_constraints  = array();
-  protected $_order = array();
-  protected $_having = array();
-  protected $_group_by = array();
-
-  function __construct($sql = null, $conn)
-  {
-    if(is_null($sql))
-      $sql = "SELECT %fields% FROM %tables% %left_join% %where% %group% %having% %order%";
-
-    parent :: __construct($sql, $conn);
-  }
-
-  protected function _declareHints()
-  {
-    return array('fields', 'tables', 'left_join', 'where', 'group', 'having', 'order');
-  }
-
-  function addField($field, $alias = null)
-  {
-    $this->_fields[$field] = $alias;
-  }
-
-  function addTable($table, $alias = null)
-  {
-    $this->_tables[$table] = $alias;
-  }
-
-  function addOrder($field, $type='ASC')
-  {
-    $this->_order[] = "$field $type";
-  }
-
-  function addGroupBy($group)
-  {
-    $this->_group_by[] = $group;
-  }
-
-  function addHaving($criteria)
-  {
-    $this->_having[] = $criteria;
-  }
-
-  function addLeftJoin($table, $connect_by)
-  {
-    $this->_left_join_constraints[$table] = $connect_by;
-  }
-
-  function getRecordSet()
-  {
-    $stmt = $this->getStatement();
-    return $stmt->getRecordSet();
-  }
-
-  protected function _getFieldsHint()
-  {
-    $fields = '';
-    foreach($this->_fields as $field => $alias)
-    {
-      $fields .= $this->_conn->quoteIdentifier($field) .
-                 ($alias ? ' as ' . $this->_conn->quoteIdentifier($alias) : '') . ',';
-    }
-    $fields = rtrim($fields, ',');
-
-    if($this->_selectFieldsExists())
-    {
-      if(count($this->_fields))
-        return ',' . $fields;
-      else
-        return '';
-    }
-    elseif(count($this->_fields) == 0)
-      return '*';
-    else
-      return $fields;
-  }
-
-  protected function _getTablesHint()
-  {
-    if(count($this->_tables) == 0)
-      return '';
-
-    $tables = '';
-    foreach($this->_tables as $table => $alias)
-    {
-      $tables .= $this->_conn->quoteIdentifier($table) .
-                 ($alias ? ' ' . $this->_conn->quoteIdentifier($alias) : '') . ',';
-    }
-    $tables = rtrim($tables, ',');
-
-    if($this->_selectTablesExists())
-      $tables = ',' . $tables;
-
-    return $tables;
-  }
-
-  protected function _getLeftJoinHint()
-  {
-    $join = array();
-    foreach($this->_left_join_constraints as $table => $connect_by)
-    {
-      $foreign_key = $this->_conn->quoteIdentifier(key($connect_by));
-      $alias_key = $this->_conn->quoteIdentifier(reset($connect_by));
-      $join[] = "LEFT JOIN " . $this->_conn->quoteIdentifier($table) . " ON $foreign_key=$alias_key";
-    }
-
-    return implode(' ', $join);
-  }
-
-  protected function _getGroupHint()
-  {
-    if (count($this->_group_by) == 0)
-      return '';
-
-    $group = implode(',', array_map(array($this->_conn, 'quoteIdentifier'), $this->_group_by));
-
-    if($this->_groupByClauseExists($group_by_args))
-    {
-      //primitive check if comma is required
-      if($group_by_args)
-        return ',' . $group;
-      else
-        return $group;
-    }
-    else
-      return 'GROUP BY ' . $group;
-  }
-
-  protected function _getHavingHint()
-  {
-    if (count($this->_having) == 0)
-      return '';
-
-    if(!count($this->_group_by) && !$this->_groupByClauseExists())
-      throw new lmbException('can not generate HAVING condition GROUP BY missing');
-
-    $implode = array();
-    foreach($this->_having as $criteria)
-    {
-      $implode[] = $criteria->toStatementString($this->_stmt_values);
-    }
-
-    $having = implode(' AND ', $implode);
-
-    if($this->_havingClauseExists($having_args))
-    {
-      if($having_args)
-        return 'AND ' . $having;
-      else
-        return $having;
-    }
-    else
-      return 'HAVING ' . $having;
-  }
-
-  protected function _getOrderHint()
-  {
-    if (count($this->_order) == 0)
-      return '';
-
-    $order = implode(',', $this->_order);
-
-    if($this->_orderByClauseExists($order_by_args))
-    {
-      //primitive check if comma is required
-      if($order_by_args)
-        return ',' . $order;
-      else
-        return $order;
-    }
-    else
-      return 'ORDER BY ' . $order;
-  }
-
-  protected function _orderByClauseExists(&$args = '')
-  {
-    //!!!make it better later
-    if(preg_match('~(?<=from).+order\s+by\s(.*)$~si', $this->_getNoHintsSQL(), $matches))
-    {
-      $args = trim($matches[1]);
-      return true;
-    }
-
-    return false;
-  }
-
-  protected function _groupByClauseExists(&$args = '')
-  {
-    //!!!make it better later
-    if(preg_match('~(?<=\Wfrom).+group\s+by\s(.*)$~si', $this->_getNoHintsSQL(), $matches))
-    {
-      $args = trim($matches[1]);
-      return true;
-    }
-
-    return false;
-  }
-
-  protected function _havingClauseExists(&$args = '')
-  {
-    //!!!make it better later
-    if(preg_match('~(?<=\Wgroup)\s+by\s+.+?having\s(.*)(order)?$~si', $this->_getNoHintsSQL(), $matches))
-    {
-      $args = trim($matches[1]);
-      return true;
-    }
-
-    return false;
-  }
-
-  protected function _selectFieldsExists()
-  {
-    //!!!make it better later
-    return preg_match('~^select\s+\S+.+?from~si', $this->_getNoHintsSQL());
-  }
-
-  protected function _selectTablesExists()
-  {
-    return preg_match('~(?<=\Wfrom)\s+\S+(where|order|group)?~si', $this->_getNoHintsSQL());
-  }
-
-  protected function _whereClauseExists(&$args = '')
-  {
-    //primitive check if WHERE was already in sql
-    //!!!make it better later
-    if(preg_match('~(?<=\Wfrom).+where\s+(.*)~si', $this->_getNoHintsSQL(), $matches))
-    {
-      if(preg_match('~([a-zA-Z].*)(group|order)?$~si', $matches[1], $args_matches))
-        $args = $args_matches[1];
-
-      return true;
-    }
-    return false;
-  }
-}
-?>
+class lmbSelectQuery extends lmbCriteriaQuery
+{
+  protected $_fields = array();
+  protected $_tables = array();
+  protected $_left_join_constraints  = array();
+  protected $_order = array();
+  protected $_having = array();
+  protected $_group_by = array();
+
+  function __construct($sql = null, $conn)
+  {
+    if(is_null($sql))
+      $sql = "SELECT %fields% FROM %tables% %left_join% %where% %group% %having% %order%";
+
+    parent :: __construct($sql, $conn);
+  }
+
+  function addField($field, $alias = null)
+  {
+    $this->_fields[$field] = $alias;
+  }
+
+  function addTable($table, $alias = null)
+  {
+    $this->_tables[$table] = $alias;
+  }
+
+  function addOrder($field, $type='ASC')
+  {
+    $this->_order[] = "$field $type";
+  }
+
+  function addGroupBy($group)
+  {
+    $this->_group_by[] = $group;
+  }
+
+  function addHaving($criteria)
+  {
+    $this->_having[] = $criteria;
+  }
+
+  function addLeftJoin($table, $connect_by)
+  {
+    $this->_left_join_constraints[$table] = $connect_by;
+  }
+
+  function getRecordSet()
+  {
+    $stmt = $this->getStatement();
+    return $stmt->getRecordSet();
+  }
+
+  protected function _getFieldsHint()
+  {
+    $fields = '';
+    foreach($this->_fields as $field => $alias)
+    {
+      $fields .= $this->_conn->quoteIdentifier($field) .
+                 ($alias ? ' as ' . $this->_conn->quoteIdentifier($alias) : '') . ',';
+    }
+    $fields = rtrim($fields, ',');
+
+    if($this->_selectFieldsExists())
+    {
+      if(count($this->_fields))
+        return ',' . $fields;
+      else
+        return '';
+    }
+    elseif(count($this->_fields) == 0)
+      return '*';
+    else
+      return $fields;
+  }
+
+  protected function _getTablesHint()
+  {
+    if(count($this->_tables) == 0)
+      return '';
+
+    $tables = '';
+    foreach($this->_tables as $table => $alias)
+    {
+      $tables .= $this->_conn->quoteIdentifier($table) .
+                 ($alias ? ' ' . $this->_conn->quoteIdentifier($alias) : '') . ',';
+    }
+    $tables = rtrim($tables, ',');
+
+    if($this->_selectTablesExists())
+      $tables = ',' . $tables;
+
+    return $tables;
+  }
+
+  protected function _getLeftJoinHint()
+  {
+    $join = array();
+    foreach($this->_left_join_constraints as $table => $connect_by)
+    {
+      $foreign_key = $this->_conn->quoteIdentifier(key($connect_by));
+      $alias_key = $this->_conn->quoteIdentifier(reset($connect_by));
+      $join[] = "LEFT JOIN " . $this->_conn->quoteIdentifier($table) . " ON $foreign_key=$alias_key";
+    }
+
+    return implode(' ', $join);
+  }
+
+  protected function _getGroupHint()
+  {
+    if (count($this->_group_by) == 0)
+      return '';
+
+    $group = implode(',', array_map(array($this->_conn, 'quoteIdentifier'), $this->_group_by));
+
+    if($this->_groupByClauseExists($group_by_args))
+    {
+      //primitive check if comma is required
+      if($group_by_args)
+        return ',' . $group;
+      else
+        return $group;
+    }
+    else
+      return 'GROUP BY ' . $group;
+  }
+
+  protected function _getHavingHint()
+  {
+    if (count($this->_having) == 0)
+      return '';
+
+    if(!count($this->_group_by) && !$this->_groupByClauseExists())
+      throw new lmbException('can not generate HAVING condition GROUP BY missing');
+
+    $implode = array();
+    foreach($this->_having as $criteria)
+    {
+      $implode[] = $criteria->toStatementString($this->_stmt_values);
+    }
+
+    $having = implode(' AND ', $implode);
+
+    if($this->_havingClauseExists($having_args))
+    {
+      if($having_args)
+        return 'AND ' . $having;
+      else
+        return $having;
+    }
+    else
+      return 'HAVING ' . $having;
+  }
+
+  protected function _getOrderHint()
+  {
+    if (count($this->_order) == 0)
+      return '';
+
+    $order = implode(',', $this->_order);
+
+    if($this->_orderByClauseExists($order_by_args))
+    {
+      //primitive check if comma is required
+      if($order_by_args)
+        return ',' . $order;
+      else
+        return $order;
+    }
+    else
+      return 'ORDER BY ' . $order;
+  }
+
+  protected function _orderByClauseExists(&$args = '')
+  {
+    //!!!make it better later
+    if(preg_match('~(?<=from).+order\s+by\s(.*)$~si', $this->_getNoHintsSQL(), $matches))
+    {
+      $args = trim($matches[1]);
+      return true;
+    }
+
+    return false;
+  }
+
+  protected function _groupByClauseExists(&$args = '')
+  {
+    //!!!make it better later
+    if(preg_match('~(?<=\Wfrom).+group\s+by\s(.*)$~si', $this->_getNoHintsSQL(), $matches))
+    {
+      $args = trim($matches[1]);
+      return true;
+    }
+
+    return false;
+  }
+
+  protected function _havingClauseExists(&$args = '')
+  {
+    //!!!make it better later
+    if(preg_match('~(?<=\Wgroup)\s+by\s+.+?having\s(.*)(order)?$~si', $this->_getNoHintsSQL(), $matches))
+    {
+      $args = trim($matches[1]);
+      return true;
+    }
+
+    return false;
+  }
+
+  protected function _selectFieldsExists()
+  {
+    //!!!make it better later
+    return preg_match('~^select\s+\S+.+?from~si', $this->_getNoHintsSQL());
+  }
+
+  protected function _selectTablesExists()
+  {
+    return preg_match('~(?<=\Wfrom)\s+\S+(where|order|group)?~si', $this->_getNoHintsSQL());
+  }
+
+  protected function _whereClauseExists(&$args = '')
+  {
+    //primitive check if WHERE was already in sql
+    //!!!make it better later
+    if(preg_match('~(?<=\Wfrom).+where\s+(.*)~si', $this->_getNoHintsSQL(), $matches))
+    {
+      if(preg_match('~([a-zA-Z].*)(group|order)?$~si', $matches[1], $args_matches))
+        $args = $args_matches[1];
+
+      return true;
+    }
+    return false;
+  }
+}
+?>

Modified: 3.x/trunk/limb/dbal/src/query/lmbSelectQueryDecorator.class.php
===================================================================
--- 3.x/trunk/limb/dbal/src/query/lmbSelectQueryDecorator.class.php	2007-06-19 21:12:16 UTC (rev 6004)
+++ 3.x/trunk/limb/dbal/src/query/lmbSelectQueryDecorator.class.php	2007-06-19 21:14:49 UTC (rev 6005)
@@ -1,81 +1,81 @@
-<?php
+<?php
 /*
  * Limb PHP Framework
  *
  * @link http://limb-project.com 
  * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
  * @license    LGPL http://www.gnu.org/copyleft/lesser.html 
- */
+ */
 lmb_require('limb/dbal/src/query/lmbSelectQuery.class.php');
 
-/**
- * class lmbSelectQueryDecorator.
- *
- * @package dbal
- * @version $Id$
+/**
+ * class lmbSelectQueryDecorator.
+ *
+ * @package dbal
+ * @version $Id$
  */
-class lmbSelectQueryDecorator
-{
-  protected $query;
-
-  function __construct($query)
-  {
-    $this->query = $query;
-  }
-
-  function reset()
-  {
-    $this->query->reset();
-  }
-
-  function addField($field)
-  {
-    $this->query->addField($field);
-  }
-
-  function addTable($table)
-  {
-    $this->query->addTable($table);
-  }
-
-  function addOrder($field, $type='ASC')
-  {
-    $this->query->addOrder($field, $type);
-  }
-
-  function addGroupBy($group)
-  {
-    $this->query->addGroupBy($group);
-  }
-
-  function addLeftJoin($table, $connect_by)
-  {
-    $this->query->addLeftJoin($table, $connect_by);
-  }
-
-  function addCriteria($criteria)
-  {
-    $this->query->addCriteria($criteria);
-  }
-
-  function toString()
-  {
-    return $this->query->toString();
-  }
-
-  function getStatement()
-  {
-    return $this->query->getStatement();
-  }
-
-  function getRecordSet()
-  {
-    return $this->query->getRecordSet();
-  }
-
-  function getStatementValues()
-  {
-    return $this->query->getStatementValues();
-  }
-}
-?>
+class lmbSelectQueryDecorator
+{
+  protected $query;
+
+  function __construct($query)
+  {
+    $this->query = $query;
+  }
+
+  function reset()
+  {
+    $this->query->reset();
+  }
+
+  function addField($field)
+  {
+    $this->query->addField($field);
+  }
+
+  function addTable($table)
+  {
+    $this->query->addTable($table);
+  }
+
+  function addOrder($field, $type='ASC')
+  {
+    $this->query->addOrder($field, $type);
+  }
+
+  function addGroupBy($group)
+  {
+    $this->query->addGroupBy($group);
+  }
+
+  function addLeftJoin($table, $connect_by)
+  {
+    $this->query->addLeftJoin($table, $connect_by);
+  }
+
+  function addCriteria($criteria)
+  {
+    $this->query->addCriteria($criteria);
+  }
+
+  function toString()
+  {
+    return $this->query->toString();
+  }
+
+  function getStatement()
+  {
+    return $this->query->getStatement();
+  }
+
+  function getRecordSet()
+  {
+    return $this->query->getRecordSet();
+  }
+
+  function getStatementValues()
+  {
+    return $this->query->getStatementValues();
+  }
+}
+?>

Modified: 3.x/trunk/limb/dbal/src/query/lmbTableRecordsQuery.class.php
===================================================================
--- 3.x/trunk/limb/dbal/src/query/lmbTableRecordsQuery.class.php	2007-06-19 21:12:16 UTC (rev 6004)
+++ 3.x/trunk/limb/dbal/src/query/lmbTableRecordsQuery.class.php	2007-06-19 21:14:49 UTC (rev 6005)
@@ -1,28 +1,28 @@
-<?php
+<?php
 /*
  * Limb PHP Framework
  *
  * @link http://limb-project.com 
  * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
  * @license    LGPL http://www.gnu.org/copyleft/lesser.html 
- */
+ */
 lmb_require('limb/dbal/src/query/lmbSelectQuery.class.php');
 
-/**
- * class lmbTableRecordsQuery.
- *
- * @package dbal
- * @version $Id$
+/**
+ * class lmbTableRecordsQuery.
+ *
+ * @package dbal
+ * @version $Id$
  */
-class lmbTableRecordsQuery extends lmbSelectQuery
-{
-  function __construct($table, $conn)
-  {
-    $table = $conn->quoteIdentifier($table);
-    $sql = "SELECT {$table}.* %fields% FROM {$table} %tables% ".
-                                  "%left_join% %where% %group% %having% %order%";
-
-    parent :: __construct($sql, $conn);
-  }
-}
-?>
+class lmbTableRecordsQuery extends lmbSelectQuery
+{
+  function __construct($table, $conn)
+  {
+    $table = $conn->quoteIdentifier($table);
+    $sql = "SELECT {$table}.* %fields% FROM {$table} %tables% ".
+                                  "%left_join% %where% %group% %having% %order%";
+
+    parent :: __construct($sql, $conn);
+  }
+}
+?>

Modified: 3.x/trunk/limb/dbal/src/query/lmbTemplateQuery.class.php
===================================================================
--- 3.x/trunk/limb/dbal/src/query/lmbTemplateQuery.class.php	2007-06-19 21:12:16 UTC (rev 6004)
+++ 3.x/trunk/limb/dbal/src/query/lmbTemplateQuery.class.php	2007-06-19 21:14:49 UTC (rev 6005)
@@ -1,4 +1,4 @@
-<?php
+<?php
 /*
  * Limb PHP Framework
  *
@@ -7,86 +7,81 @@
  * @license    LGPL http://www.gnu.org/copyleft/lesser.html 
  */
 
-/**
- * class lmbTemplateQuery.
- *
- * @package dbal
- * @version $Id$
+/**
+ * class lmbTemplateQuery.
+ *
+ * @package dbal
+ * @version $Id$
  */
-class lmbTemplateQuery
-{
-  protected $_template_sql;
-  protected $_no_hints_sql;
-  protected $_conn;
-
-  function __construct($template_sql, $conn)
-  {
-    $this->_template_sql = $template_sql;
-    $this->_conn = $conn;
-  }
-
-  protected function _declareHints()
-  {
-    return array();
-  }
-
-  function _wrapHint($hint)
-  {
-    return "%$hint%";
-  }
-
-  function _getWrappedHints()
-  {
-    return array_map(array($this, '_wrapHint'), $this->_declareHints());
-  }
-
-  function _fillHints()
-  {
-    $hints = $this->_declareHints();
-    $result = array();
-    foreach($hints as $hint)
-    {
-      $method = '_get' . ucfirst(lmb_camel_case($hint)) . 'Hint';
-      $result[$this->_wrapHint($hint)] = $this->$method();
-    }
-    return $result;
-  }
-
-  function toString()
-  {
-    $hints = $this->_fillHints();
-    $this->_validateSQLforTemplateHints($hints);
-
-    return trim(strtr($this->_template_sql, $hints));
-  }
-
-  protected function _validateSQLforTemplateHints($hints)
-  {
-    foreach($hints as $hint => $value)
-    {
-      if(!trim($value)) continue;
-
-      if(strpos($this->_template_sql, $hint) === false)
-        throw new lmbException("Template hint '$hint' not for value '$value' found in '$this->_template_sql'");
-    }
-  }
-
-  function getStatement()
-  {
-    return $this->_conn->newStatement($this->toString());
-  }
-
-  protected function _getNoHintsSQL()
-  {
-    if($this->_no_hints_sql)
-      return $this->_no_hints_sql;
-
-    $result = array();
-    foreach($this->_getWrappedHints() as $hint)
-      $result[$hint] = '';
-
-    $this->_no_hints_sql = strtr($this->_template_sql, $result);
-    return $this->_no_hints_sql;
-  }
-}
-?>
+class lmbTemplateQuery
+{
+  protected $_template_sql;
+  protected $_no_hints_sql;
+  protected $_conn;
+  protected $_hints;
+
+  function __construct($template_sql, $conn)
+  {
+    $this->_template_sql = $template_sql;
+    $this->_conn = $conn;
+  }
+
+  protected function _declareHints()
+  {
+    if($this->_hints !== null)
+      return $this->_hints;
+
+    if(preg_match_all('~%([a-z_]+)%~', $this->_template_sql, $m))
+      $this->_hints = $m[1];
+    else
+      $this->_hints = array();
+    return $this->_hints;
+  }
+
+  function _wrapHint($hint)
+  {
+    return "%$hint%";
+  }
+
+  function _getWrappedHints()
+  {
+    return array_map(array($this, '_wrapHint'), $this->_declareHints());
+  }
+
+  function _fillHints()
+  {
+    $hints = $this->_declareHints();
+    $result = array();
+    foreach($hints as $hint)
+    {
+      $method = '_get' . lmb_camel_case($hint) . 'Hint';
+      $result[$this->_wrapHint($hint)] = $this->$method();
+    }
+    return $result;
+  }
+
+  function toString()
+  {
+    $hints = $this->_fillHints();
+    return trim(strtr($this->_template_sql, $hints));
+  }
+
+  function getStatement()
+  {
+    return $this->_conn->newStatement($this->toString());
+  }
+
+  protected function _getNoHintsSQL()
+  {
+    if($this->_no_hints_sql)
+      return $this->_no_hints_sql;
+
+    $result = array();
+    foreach($this->_getWrappedHints() as $hint)
+      $result[$hint] = '';
+
+    $this->_no_hints_sql = strtr($this->_template_sql, $result);
+    return $this->_no_hints_sql;
+  }
+}
+?>

Modified: 3.x/trunk/limb/dbal/src/query/lmbUpdateQuery.class.php
===================================================================
--- 3.x/trunk/limb/dbal/src/query/lmbUpdateQuery.class.php	2007-06-19 21:12:16 UTC (rev 6004)
+++ 3.x/trunk/limb/dbal/src/query/lmbUpdateQuery.class.php	2007-06-19 21:14:49 UTC (rev 6005)
@@ -1,81 +1,76 @@
-<?php
+<?php
 /*
  * Limb PHP Framework
  *
  * @link http://limb-project.com 
  * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
  * @license    LGPL http://www.gnu.org/copyleft/lesser.html 
- */
-lmb_require('limb/dbal/src/query/lmbCriteriaQuery.class.php');
-
+ */
+lmb_require('limb/dbal/src/query/lmbCriteriaQuery.class.php');
+
 define('LIMB_UPDATE_QUERY_NON_VALUE', uniqid());
 
-/**
- * class lmbUpdateQuery.
- *
- * @package dbal
- * @version $Id$
+/**
+ * class lmbUpdateQuery.
+ *
+ * @package dbal
+ * @version $Id$
  */
-class lmbUpdateQuery extends lmbCriteriaQuery
-{
-  protected $_table;
-  protected $_fields = array();
-  protected $_raw_fields = array();
-  protected $_set_values = array();
-
-  function __construct($table, $conn)
-  {
-    $this->_table = $table;
-    parent :: __construct("UPDATE %table% SET %fields% %where%", $conn);
-  }
-
-  protected function _declareHints()
-  {
-    return array('table', 'fields', 'where');
-  }
-
-  function addField($field, $value = LIMB_UPDATE_QUERY_NON_VALUE)
-  {
-    $this->_fields[$field] = $value;
-  }
-
-  function addRawField($field)
-  {
-    $this->_raw_fields[] = $field;
-  }
-
-  protected function _getTableHint()
-  {
-    return $this->_conn->quoteIdentifier($this->_table);
-  }
-
-  protected function _getFieldsHint()
-  {
-    $values = array();
-    foreach($this->_fields as $field => $value)
-    {
-      if($value !== LIMB_UPDATE_QUERY_NON_VALUE)
-        $this->_set_values[$field] = $value;
-
-      $values[] = $this->_conn->quoteIdentifier($field) . " = :{$field}:";
-    }
-
-    foreach($this->_raw_fields as $field)
-    {
-      $values[] = $field;
-    }
-
-    return implode(',', $values);
-  }
-
-  function getStatement()
-  {
-    $stmt = parent :: getStatement();
-    foreach($this->_set_values as $key => $value)
-      $stmt->set($key, $value);
-
-    return $stmt;
-  }
-
-}
-?>
+class lmbUpdateQuery extends lmbCriteriaQuery
+{
+  protected $_table;
+  protected $_fields = array();
+  protected $_raw_fields = array();
+  protected $_set_values = array();
+
+  function __construct($table, $conn)
+  {
+    $this->_table = $table;
+    parent :: __construct("UPDATE %table% SET %fields% %where%", $conn);
+  }
+
+  function addField($field, $value = LIMB_UPDATE_QUERY_NON_VALUE)
+  {
+    $this->_fields[$field] = $value;
+  }
+
+  function addRawField($field)
+  {
+    $this->_raw_fields[] = $field;
+  }
+
+  protected function _getTableHint()
+  {
+    return $this->_conn->quoteIdentifier($this->_table);
+  }
+
+  protected function _getFieldsHint()
+  {
+    $values = array();
+    foreach($this->_fields as $field => $value)
+    {
+      if($value !== LIMB_UPDATE_QUERY_NON_VALUE)
+        $this->_set_values[$field] = $value;
+
+      $values[] = $this->_conn->quoteIdentifier($field) . " = :{$field}:";
+    }
+
+    foreach($this->_raw_fields as $field)
+    {
+      $values[] = $field;
+    }
+
+    return implode(',', $values);
+  }
+
+  function getStatement()
+  {
+    $stmt = parent :: getStatement();
+    foreach($this->_set_values as $key => $value)
+      $stmt->set($key, $value);
+
+    return $stmt;
+  }
+
+}
+?>



More information about the limb-svn mailing list