/
SqlCompatibleBehavior.php
294 lines (278 loc) · 6.82 KB
/
SqlCompatibleBehavior.php
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
<?php
/**
* Sql Compatible.
*
* Attach this behavior to be able to query mongo DBs without using mongo specific syntax.
* If you don't need this behavior don't attach it and save a few cycles
*
* PHP version 5
*
* Copyright (c) 2010, Andy Dawson
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @filesource
* @copyright Copyright (c) 2010, Andy Dawson
* @link www.ad7six.com
* @package mongodb
* @subpackage mongodb.models.behaviors
* @since v 1.0 (24-May-2010)
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
/**
* SqlCompatibleBehavior class
*
* @uses ModelBehavior
* @package mongodb
* @subpackage mongodb.models.behaviors
*/
class SqlCompatibleBehavior extends ModelBehavior {
/**
* name property
*
* @var string 'SqlCompatible'
* @access public
*/
public $name = 'SqlCompatible';
/**
* Runtime settings
*
* Keyed on model alias
*
* @var array
* @access public
*/
public $settings = array();
/**
* defaultSettings property
*
* @var array
* @access protected
*/
protected $_defaultSettings = array(
'convertDates' => true,
'operators' => array(
'!=' => '$ne',
'>' => '$gt',
'>=' => '$gte',
'<' => '$lt',
'<=' => '$lte',
'IN' => '$in',
'NOT' => '$not',
'NOT IN' => '$nin'
)
);
/**
* setup method
*
* Allow overriding the operator map
*
* @param mixed $Model
* @param array $config array()
* @return void
* @access public
*/
public function setup(Model $Model, $config = array()) {
$this->settings[$Model->alias] = array_merge($this->_defaultSettings, $config);
}
/**
* If requested, convert dates from MongoDate objects to standard date strings
*
* @param mixed $Model
* @param mixed $results
* @param mixed $primary
* @return void
* @access public
*/
public function afterFind(Model $Model, $results, $primary) {
if ($this->settings[$Model->alias]['convertDates']) {
$this->convertDates($results);
}
return $results;
}
/**
* beforeFind method
*
* If conditions are an array ensure they are mongified
*
* @param mixed $Model
* @param mixed $query
* @return void
* @access public
*/
public function beforeFind(Model $Model, $query) {
if (is_array($query['order'])) {
$this->_translateOrders($Model, $query['order']);
}
if (is_array($query['conditions']) && $this->_translateConditions($Model, $query['conditions'])) {
return $query;
}
return $query;
}
/**
* Convert MongoDate objects to strings for the purpose of view simplicity
*
* @param mixed $results
* @return void
* @access protected
*/
protected function convertDates(&$results) {
if (is_array($results)) {
foreach($results as &$row) {
$this->convertDates($row);
}
} elseif (is_a($results, 'MongoDate')) {
$results = date('Y-M-d h:i:s', $results->sec);
}
}
/**
* translateOrders method
* change order syntax from SQL style to Mongo style
*
* @param mixed $Model
* @param mixed $orders
* @return void
* @access protected
*/
protected function _translateOrders(Model &$Model, &$orders) {
if(!empty($orders[0])) {
foreach($orders[0] as $key => $val) {
if(preg_match('/^(.+) (ASC|DESC)$/i', $val, $match)) {
$orders[0][$match[1]] = $match[2];
unset($orders[0][$key]);
}
}
}
}
/**
* translateConditions method
*
* Loop on conditions and desqlify them
*
* @param mixed $Model
* @param mixed $conditions
* @return void
* @access protected
*/
protected function _translateConditions(Model &$Model, &$conditions) {
$return = false;
foreach($conditions as $key => &$value) {
$uKey = strtoupper($key);
if (substr($uKey, -5) === 'NOT IN') {
// 'Special' case because it has a space in it, and it's the whole key
$conditions[substr($key, 0, -5)]['$nin'] = $value;
unset($conditions[$key]);
$return = true;
continue;
}
if ($uKey === 'OR') {
unset($conditions[$key]);
foreach($value as $key => $part) {
$part = array($key => $part);
$this->_translateConditions($Model, $part);
$conditions['$or'][] = $part;
}
$return = true;
continue;
}
if ($key === $Model->primaryKey && is_array($value)) {
//_id=>array(1,2,3) pattern, set $in operator
$isMongoOperator = false;
foreach($value as $idKey => $idValue) {
//check a mongo operator exists
if(substr($idKey,0,1) === '$') {
$isMongoOperator = true;
continue;
}
}
unset($idKey, $idValue);
if($isMongoOperator === false) {
$conditions[$key] = array('$in' => $value);
}
$return = true;
continue;
}
if (is_numeric($key) && is_array($value)) {
if ($this->_translateConditions($Model, $value)) {
$return = true;
continue;
}
}
if (substr($uKey, -3) === 'NOT') {
// 'Special' case because it's awkward
$childKey = key($value);
$childValue = current($value);
if (in_array(substr($childKey, -1), array('>', '<', '='))) {
$parts = explode(' ', $childKey);
$operator = array_pop($parts);
if ($operator = $this->_translateOperator($Model, $operator)) {
$childKey = implode(' ', $parts);
}
} else {
$conditions[$childKey]['$nin'] = (array)$childValue;
unset($conditions['NOT']);
$return = true;
continue;
}
$conditions[$childKey]['$not'][$operator] = $childValue;
unset($conditions['NOT']);
$return = true;
continue;
}
if (substr($uKey, -5) === ' LIKE') {
// 'Special' case because it's awkward
if ($value[0] === '%') {
$value = substr($value, 1);
} else {
$value = '^' . $value;
}
if (substr($value, -1) === '%') {
$value = substr($value, 0, -1);
} else {
$value .= '$';
}
$value = str_replace('%', '.*', $value);
$conditions[substr($key, 0, -5)] = new MongoRegex("/$value/i");
unset($conditions[$key]);
$return = true;
continue;
}
if (!in_array(substr($key, -1), array('>', '<', '='))) {
$return = true;
continue;
}
$parts = explode(' ', $key);
$operator = array_pop($parts);
if ($operator = $this->_translateOperator($Model, $operator)) {
$newKey = implode(' ', $parts);
$conditions[$newKey][$operator] = $value;
unset($conditions[$key]);
$return = true;
}
if (is_array($value)) {
if ($this->_translateConditions($Model, $value)) {
$return = true;
continue;
}
}
}
return $return;
}
/**
* translateOperator method
*
* Use the operator map for the model and return what the db really wants to hear
*
* @param mixed $Model
* @param mixed $operator
* @return string
* @access protected
*/
protected function _translateOperator(Model $Model, $operator) {
if (!empty($this->settings[$Model->alias]['operators'][$operator])) {
return $this->settings[$Model->alias]['operators'][$operator];
}
return '';
}
}