diff options
author | knut <> | 2008-08-06 13:33:38 +0000 |
---|---|---|
committer | knut <> | 2008-08-06 13:33:38 +0000 |
commit | bb60bcd2032e887e010904999655d2e5999e9878 (patch) | |
tree | 43bb4e7aa5e12d4781f6922d60211291f5b4c32c /framework/Data | |
parent | f7f3d150ef35c026bf6aee72e78362e263419e1a (diff) |
fixed #899
Diffstat (limited to 'framework/Data')
-rw-r--r-- | framework/Data/DataGateway/TSqlCriteria.php | 53 |
1 files changed, 42 insertions, 11 deletions
diff --git a/framework/Data/DataGateway/TSqlCriteria.php b/framework/Data/DataGateway/TSqlCriteria.php index 004f35d1..4ebdeb48 100644 --- a/framework/Data/DataGateway/TSqlCriteria.php +++ b/framework/Data/DataGateway/TSqlCriteria.php @@ -4,7 +4,7 @@ *
* @author Wei Zhuo <weizhuo[at]gmail[dot]com>
* @link http://www.pradosoft.com/
- * @copyright Copyright © 2005-2008 PradoSoft + * @copyright Copyright © 2005-2008 PradoSoft
* @license http://www.pradosoft.com/license/
* @version $Id: TDbSqlCriteria.php 1835 2007-04-03 01:38:15Z wei $
* @package System.Data.DataGateway
@@ -15,7 +15,7 @@ *
* Criteria object for data gateway finder methods. Usage:
* <code>
- * $criteria = new TDbSqlCriteria;
+ * $criteria = new TSqlCriteria();
* $criteria->Parameters[':name'] = 'admin';
* $criteria->Parameters[':pass'] = 'prado';
* $criteria->OrdersBy['level'] = 'desc';
@@ -68,15 +68,46 @@ class TSqlCriteria extends TComponent * @param string search conditions.
*/
public function setCondition($value)
- {
- if(!empty($value) && preg_match('/ORDER\s+BY\s+(.*?)$/i',$value,$matches)>0)
- {
- // condition contains ORDER BY, we need to strip it output
- $this->_condition=substr($value,0,strpos($value,$matches[0]));
- $this->setOrdersBy($matches[1]);
+ {
+ if(empty($value)) {
+ return;
}
- else
- $this->_condition=$value;
+
+ // supporting the following SELECT-syntax:
+ // [ORDER BY {col_name | expr | position}
+ // [ASC | DESC], ...]
+ // [LIMIT {[offset,] row_count | row_count OFFSET offset}]
+ // See: http://dev.mysql.com/doc/refman/5.0/en/select.html
+
+ if(preg_match('/ORDER\s+BY\s+(.*?)(?=LIMIT)|ORDER\s+BY\s+(.*?)$/i', $value, $matches) > 0) {
+ // condition contains ORDER BY
+ $value = str_replace($matches[0], '', $value);
+ if(strlen($matches[1]) > 0) {
+ $this->setOrdersBy($matches[1]);
+ } else if(strlen($matches[2]) > 0) {
+ $this->setOrdersBy($matches[2]);
+ }
+ }
+
+ if(preg_match('/LIMIT\s+([\d\s,]+)/i', $value, $matches) > 0) {
+ // condition contains limit
+ $value = str_replace($matches[0], '', $value); // remove limit from query
+ if(strpos($matches[1], ',')) { // both offset and limit given
+ list($offset, $limit) = explode(',', $matches[1]);
+ $this->_limit = (int)$limit;
+ $this->_offset = (int)$offset;
+ } else { // only limit given
+ $this->_limit = (int)$matches[1];
+ }
+ }
+
+ if(preg_match('/OFFSET\s+(\d+)/i', $value, $matches) > 0) {
+ // condition contains offset
+ $value = str_replace($matches[0], '', $value); // remove offset from query
+ $this->_offset = (int)$matches[1]; // set offset in criteria
+ }
+
+ $this->_condition = trim($value);
}
/**
@@ -192,4 +223,4 @@ class TSqlCriteria extends TComponent }
}
-?> +?>
|