Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
DALSelectParser.inc
1 <?php
13 require_once 'DAL/Parsers/DALQueryParser.inc';
14 require_once 'DAL/Parsers/DALOrderByParser.inc';
15 require_once 'DAL/Parsers/DALGroupByParser.inc';
16 require_once 'DAL/Parsers/DALWhereParser.inc';
17 require_once 'DAL/Parsers/DALFromParser.inc';
18 
27 {
28 
29 
38  private function __construct()
39  {
40 
41  }//end __construct()
42 
43 
52  public static function parse(DomElement $xmlQuery)
53  {
54  if (strtolower($xmlQuery->tagName) !== 'select') {
55  $selectTag = $xmlQuery->getElementsByTagName('select')->item(0);
56  } else {
57  $selectTag = $xmlQuery;
58  }
59 
60  $query = self::_getSelectClause($selectTag);
61 
62  $query = array_merge($query, DALFromParser::parse($selectTag));
63 
64  $query = array_merge($query, DALWhereParser::parse($selectTag));
65 
66  $query = array_merge($query, DALWhereParser::parse($selectTag, 'having'));
67 
68  $query = array_merge($query, DALWhereParser::parse($selectTag, 'start-with'));
69 
70  $query = array_merge($query, DALWhereParser::parse($selectTag, 'connect-by'));
71 
72  // Get GROUP By clause.
73  $query = array_merge($query, DALGroupByParser::parse($selectTag));
74 
75  // Get Order BY clause.
76  $query = array_merge($query, DALOrderByParser::parse($selectTag));
77 
78  // Get the LIMIT clause.
79  $query = array_merge($query, DALQueryParser::parseLimitClause($selectTag));
80 
81  $selectHooks = DALQueryParser::addHookIds($selectTag);
82  if (empty($selectHooks) === FALSE) {
83  $query['HOOKS'] = $selectHooks;
84  }
85 
86  return $query;
87 
88  }//end parse()
89 
90 
107  private static function _getSelectClause(DomElement $select, $level=0)
108  {
109  $query['SELECT'] = array();
110 
111  if (strtolower($select->getAttribute('distinct')) === 'true') {
112  $query['SELECT']['distinct'] = TRUE;
113  }
114 
115  // Note: This query should already be validated.
116  $fieldsTag = $select->getElementsByTagName('fields')->item(0);
117 
118  // Add fields for the select clause.
119  $query['SELECT']['FIELDS'] = self::parseChildFields($fieldsTag);
120  return $query;
121 
122  }//end _getSelectClause()
123 
124 
136  public static function validate(DomElement $query)
137  {
138  // Get the select tag.
139  $selectTag = $query->getElementsByTagName('select');
140 
141  // Check if we have select query.
142  $numSelects = $selectTag->length;
143  if ($numSelects === 0) {
144  throw new DALParserException('No select tag found.');
145  } else {
146  $selectTag = $selectTag->item(0);
147 
148  // Select query must have 1 fields tag with 1 or more field tags.
149  $fieldsTag = $selectTag->getElementsByTagName('fields');
150  if ($numSelects !== $fieldsTag->length) {
151  $msg = 'Each select query must have a fields tag.';
152  throw new DALParserException($msg);
153  }
154 
155  $fieldsTag = $fieldsTag->item(0);
156  if ($fieldsTag === NULL) {
157  throw new DALParserException('Select query must have fields tag');
158  } else {
159  // We have a fields tag, check if its fields are correct.
160  $fields = $fieldsTag->getElementsByTagName('field');
161 
162  foreach ($fields as $field) {
163  self::validateField($field, TRUE);
164  }
165  }
166 
167  /*
168  Check FROM tag. WE need to make sure there are equal number of
169  select and from tags.
170  */
171 
172  $fromTag = $selectTag->getElementsByTagName('from');
173  if ($fromTag !== NULL) {
174  $fromTag = $fromTag->item(0);
175  if ($fromTag !== NULL) {
176  // Look for unions or tables.
177  $unionTag = self::getUnionTag($fromTag);
178  if ($unionTag === NULL) {
179  $tables = $fromTag->getElementsByTagName('table');
180  if ($tables->length === 0) {
181  $msg = 'No "table" tags found in "from" tag.';
182  throw new DALParserException($msg);
183  } else {
184  foreach ($tables as $table) {
185  if ($table->nodeValue === '') {
186  $msg = 'table tag with empty value.';
187  throw new DALParserException($msg);
188  }
189  }
190  }
191  } else {
192  // Get all select Tags inside union.
193  $foundSelect = FALSE;
194  foreach ($unionTag->childNodes as $node) {
195  if ($node->nodeType !== XML_ELEMENT_NODE) {
196  continue;
197  }
198 
199  if ($node->tagName === 'select' || $node->tagName === 'hook') {
200  $foundSelect = TRUE;
201  break;
202  }
203  }
204 
205  if ($foundSelect === FALSE) {
206  $msg = strtoupper($unionTag->tagName);
207  $msg .= ' must have 1 or more SELECT queries or HOOKs.';
208  throw new DALParserException($msg);
209  }
210  }//end if
211  }//end if
212  }//end if
213 
214  // Check WHERE tag.
215  $whereTag = $selectTag->getElementsByTagName('where')->item(0);
216  if ($whereTag !== NULL) {
217  DALWhereParser::validate($whereTag);
218  }
219 
220  // Group By.
221  DALGroupByParser::validate($selectTag);
222 
223  // Order By.
224  DALOrderByParser::validate($selectTag);
225 
226  }//end if
227 
228  }//end validate()
229 
230 
231 }//end class
232 
233 ?>