Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
DALWhereParser.inc
1 <?php
13 require_once 'DAL/Parsers/DALQueryParser.inc';
14 require_once 'DAL/Parsers/DALSelectParser.inc';
15 require_once 'DAL/DALBaker.inc';
16 require_once 'XML/XML.inc';
17 
26 {
27 
28 
37  private function __construct()
38  {
39 
40  }//end __construct()
41 
42 
52  public static function parse(DomElement $parent, $type='where')
53  {
54  $where = array();
55  $whereTag = NULL;
56 
57  foreach ($parent->childNodes as $tag) {
58  if ($tag->nodeType !== XML_ELEMENT_NODE) {
59  continue;
60  } else if ($tag->tagName !== $type) {
61  continue;
62  } else {
63  $whereTag = $tag;
64  }
65  }
66 
67  if ($whereTag !== NULL) {
68  $where[strtoupper($type)] = self::_getWhereConditions($whereTag);
69  }
70 
71  $joins = self::_praseJoinsForSelect($parent);
72  if (empty($joins) === FALSE) {
73  $where = array_merge($where, $joins);
74  }
75 
76  return $where;
77 
78  }//end parse()
79 
80 
91  private static function _getWhereConditions(DomElement $parent, $type='AND', $level=1)
92  {
93  $conditions = array();
94  foreach ($parent->childNodes as $cond) {
95  if ($cond->nodeType !== XML_ELEMENT_NODE) {
96  continue;
97  }
98 
99  if (DALBaker::getComparisonOperators($cond->tagName) !== '') {
100  $currentCond = array();
101  $currentCond['compare'] = array();
102  $currentCond['type'] = $cond->tagName;
103 
104  $offset = 0;
105  if ($cond->getAttribute('table') !== '') {
106  // Field compare.
107  $currentCond['compare']['table'] = $cond->getAttribute('table');
108  $currentCond['compare']['column'] = $cond->getAttribute('column');
109  } else if (XML::hasChildElements($cond) === TRUE) {
110  $offset = 1;
111  $value = self::parseSingleField(XML::getFirstChildElement($cond));
112 
113  $currentCond['compare'] = $value;
114  }
115 
116  $value = '';
117  if (XML::hasChildElements($cond) === TRUE) {
118  $child = XML::getChildAt($cond, $offset);
119  if ($child !== NULL) {
120  $value = self::parseSingleField($child);
121  }
122  } else {
123  $value = $cond->nodeValue;
124  }
125 
126  $currentCond['to'] = $value;
127  $conditions[$type][] = $currentCond;
128  } else if (($cond->tagName === 'in') || ($cond->tagName === 'not-in')) {
129  // This is an IN/NOT-IN condition.
130  $conditions[$type][] = self::parseInClause($cond, $level);
131  } else if (($cond->tagName === 'exists') || ($cond->tagName === 'not-exists')) {
132  $conditions[$type][] = self::parseExistsClause($cond, $level);
133  } else if ($cond->tagName === 'or') {
134  $conditions[$type][] = self::_getWhereConditions($cond, 'OR');
135  } else if ($cond->tagName === 'and') {
136  $conditions[$type][] = self::_getWhereConditions($cond, 'AND');
137  } else if ($cond->tagName === 'hook') {
138  $conditions[$type][] = DALBaker::getHookPrefix().$cond->getAttribute('id');
139  }//end if
140 
141  }//end foreach
142 
143  return $conditions;
144 
145  }//end _getWhereConditions()
146 
147 
175  private static function _praseJoinsForSelect(DomElement $select)
176  {
177  $joinList = array();
178  $joinsTag = $select->getElementsByTagName('joins')->item(0);
179  if ($joinsTag !== NULL) {
180  $joinList['JOIN'] = array();
181 
182  $joins = $joinsTag->getElementsByTagName('join');
183  foreach ($joins as $join) {
184  $condition = array();
185  $condition['ARGS'] = self::parseChildFields($join);
186  $condition['type'] = 'equal';
187  $joinList['JOIN'][] = $condition;
188  }
189  }
190 
191  return $joinList;
192 
193  }//end _praseJoinsForSelect()
194 
195 
207  public static function validate(DomElement $parent)
208  {
209  $condSigns = DALBaker::getComparisonOperators();
210 
211  // Add in and not-in to conditions.
212  $condSigns['in'] = '';
213  $condSigns['not-in'] = '';
214 
215  // Add exists and not-exists to conditions.
216  $condSigns['exists'] = '';
217  $condSigns['not-exists'] = '';
218 
219  // Loop through each childNode and check their contents.
220  foreach ($parent->childNodes as $cond) {
221  if ($cond->nodeType !== XML_ELEMENT_NODE) {
222  continue;
223  }
224 
225  $tagName = $cond->tagName;
226 
227  if (isset($condSigns[$tagName]) === TRUE) {
228  if ($tagName === 'is-null' || $tagName === 'not-null') {
229  if (XML::hasChildElements($cond) === FALSE) {
230  $msg = $tagName.' tag must have child.';
231  throw new DALParserException($msg);
232  }
233 
234  continue;
235  }
236 
237  if ((Xml::childCount($cond) !== 2) && ($tagName !== 'exists') && ($tagName !== 'not-exists')) {
238  if ($cond->getAttribute('table') === '') {
239  $msg = 'WHERE condition with no table attribute.';
240  throw new DALParserException($msg);
241  }
242 
243  if ($cond->getAttribute('column') === '') {
244  $msg = 'WHERE condition with no column attribute.';
245  throw new DALParserException($msg);
246  }
247  }
248 
249  if (($tagName === 'in') || ($tagName === 'not-in')) {
250  // We need additional checks for in and not-in.
251  $value = $cond->getElementsByTagName('value')->item(0);
252  $select = $cond->getElementsByTagName('select')->item(0);
253  if (($value === NULL) && ($select === NULL)) {
254  if ($cond->nodeValue === '') {
255  $msg = $tagName.' must have value or select tag.';
256  throw new DALParserException($msg);
257  }
258  }
259 
260  if ($select !== NULL) {
261  // Validate inner SEELECT query.
263  }
264  }
265 
266  if (($tagName === 'exists') || ($tagName === 'not-exists')) {
267  // We need additional checks for exists and not-exists.
268  $select = $cond->getElementsByTagName('select')->item(0);
269  if ($select === NULL) {
270  $msg = $tagName.' must have select tag.';
271  throw new DALParserException($msg);
272  }
273 
275  }
276 
277  } else if (($tagName === 'or') || ($tagName === 'and')) {
278  self::validate($cond);
279  } else if ($tagName !== 'hook') {
280  $msg = 'Found invalid WHERE comparison type "'.$tagName.'".';
281  throw new DALParserException($msg);
282  }//end if
283  }//end foreach
284 
285  }//end validate()
286 
287 
288 }//end class
289 
290 ?>