summaryrefslogtreecommitdiff
path: root/framework/Testing/Data/ActiveRecord/TActiveRecordCriteria.php
blob: 46eddaf908fbc1c0fe78792d4943e4a2bbd1e7dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<?php
/**
 * TActiveRecordCriteria class file
 * Adapts *deprecated* Prado TActiveRecordCriteria to TDbCriteria (Yii)
 * @class TActiveRecordCriteria
 * @deprecated
 * @author Daniel Mueller <tux@penguinfriends.org>
 */

Prado::using('System.Testing.Data.Schema.TDbCriteria');

class TSqlCriteria extends TDbCriteria
{
	
	private $_TDbCriteria = null;

	public function __construct($condition=null, $parameters=array())
	{
		if(!is_array($parameters) && func_num_args() > 1)
			$parameters = array_slice(func_get_args(), 1);

		$this->_TDbCriteria = $this;
		
		$this->_TDbCriteria->params = new ArrayObject();

		$param = array(
			'condition' => ($condition !== null) ? $condition : '',
			'params' => $parameters
		);
	}

	public function getCondition()
	{
		return $this->_TDbCriteria->condition;
	}

	public function setCondition($value)
	{
		if(empty($value))
			return;

		// 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->_TDbCriteria->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->_TDbCriteria->limit = (int)$limit;
				$this->_TDbCriteria->offset = (int)$offset;
			} else { // only limit given
				$this->_TDbCriteria->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->_TDbCriteria->offset = (int)$matches[1]; // set offset in criteria
		}
		
		$this->_TDbCriteria->condition = trim($value);
	}

	public function getParameters()
	{
		return $this->_TDbCriteria->params;
	}

	public function setParameters($value)
	{
		if(is_array($value))
		{
			$this->_TDbCriteria->params = $value;
		}
		elseif ($value instanceof ArrayAccess)
		{
			$this->_TDbCriteria->params = (array)$value;
		}
		else
			throw new TException('Value must be an array or of type ArrayAccess.');
	}

	public function getIsNamedParameters()
	{
		foreach($this->_TDbCriteria->params as $key=>$val)
			return is_string($key);
	}

	public function getLimit()
	{
		return ($this->_TDbCriteria->limit != -1) ? $this->_TDbCriteria->limit : null;
	}

	public function setLimit($value)
	{
		$this->_TDbCriteria->limit = (int)$value;
	}

	public function getOffset()
	{
		return ($this->_TDbCriteria->offset != -1) ? $this->_TDbCriteria->offset : null;
	}

	public function setOffset($value)
	{
		$this->_TDbCriteria->offset = (int)$value;
	}
	
	public function getOrdersBy()
	{
		if(empty($this->_TDbCriteria->order))
			return array();

		$value=trim(preg_replace('/\s+/',' ', $this->_TDbCriteria->order));
		$orderBys=array();
		foreach(explode(',',$value) as $orderBy)
		{
			$vs=explode(' ',trim($orderBy));
			
		$orderBys[$vs[0]]=isset($vs[1])?$vs[1]:'asc';
		}
		return $orderBys;
	}

	public function setOrdersBy($value)
	{
		if(is_string($value))
			$this->TDbCriteria->order = $value;
		elseif (is_array($value) || $value instanceof Traversable)
		{
			$str = '';
			foreach($value as $key => $val)
				$str .= (($str != '') ? ', ': '').$key.' '.$val;
			
			$this->_TDbCriteria->order = $str;
		}
	}	

	public function __toString()
	{
		$str = '';
		if(strlen((string)$this->getCondition()) > 0)
			$str .= '"'.(string)$this->getCondition().'"';
		$params = array();
		foreach($this->getParameters() as $k=>$v)
			$params[] = "{$k} => ${v}";
		if(count($params) > 0)
			$str .= ', "'.implode(', ',$params).'"';
		$orders = array();
		foreach($this->getOrdersBy() as $k=>$v)
			$orders[] = "{$k} => ${v}";
		if(count($orders) > 0)
			$str .= ', "'.implode(', ',$orders).'"';
		if($this->getLimit() !==null)
			$str .= ', '.$this->_limit;
		if($this->getOffset() !== null)
			$str .= ', '.$this->_offset;
		return $str;
	}

	/**
	 * Returns a property value or an event handler list by property or event name.
	 * Do not call this method. This is a PHP magic method that we override
	 * to allow using the following syntax to read a property:
	 * <code>
	 * $value=$component->PropertyName;
	 * </code>
	 * and to obtain the event handler list for an event,
	 * <code>
	 * $eventHandlerList=$component->EventName;
	 * </code>
	 * @param string the property name or the event name
	 * @return mixed the property value or the event handler list
	 * @throws TInvalidOperationException if the property/event is not defined.
	 */
	public function __get($name)
	{
		$getter='get'.$name;
		if(method_exists($this,$getter))
		{
			// getting a property
			return $this->$getter();
		}
		else if(strncasecmp($name,'on',2)===0 && method_exists($this,$name))
		{
			// getting an event (handler list)
			$name=strtolower($name);
			if(!isset($this->_e[$name]))
				$this->_e[$name]=new TList;
			return $this->_e[$name];
		}
		else
		{
			throw new TInvalidOperationException('component_property_undefined',get_class($this),$name);
		}
	}

	/**
	 * Sets value of a component property.
	 * Do not call this method. This is a PHP magic method that we override
	 * to allow using the following syntax to set a property or attach an event handler.
	 * <code>
	 * $this->PropertyName=$value;
	 * $this->EventName=$handler;
	 * </code>
	 * @param string the property name or event name
	 * @param mixed the property value or event handler
	 * @throws TInvalidOperationException If the property is not defined or read-only.
	 */
	public function __set($name,$value)
	{
		$setter='set'.$name;
		if(method_exists($this,$setter))
		{
			$this->$setter($value);
		}
		else if(strncasecmp($name,'on',2)===0 && method_exists($this,$name))
		{
			$this->attachEventHandler($name,$value);
		}
		else if(method_exists($this,'get'.$name))
		{
			throw new TInvalidOperationException('component_property_readonly',get_class($this),$name);
		}
		else
		{
			throw new TInvalidOperationException('component_property_undefined',get_class($this),$name);
		}
	}

}

class TActiveRecordCriteria extends TSqlCriteria
{
	
}