13 require_once
'DAL/Exceptions/DALParserException.inc';
14 require_once
'DAL/Parsers/DALSelectParser.inc';
15 require_once
'DAL/Parsers/DALFromParser.inc';
16 require_once
'DAL/Parsers/DALAlterParser.inc';
17 require_once
'DAL/Parsers/DALSchemaParser.inc';
18 require_once
'DAL/Parsers/DALWithParser.inc';
19 require_once
'DAL/Parsers/DALDeleteParser.inc';
20 require_once
'DAL/Parsers/DALDropParser.inc';
21 require_once
'DAL/Parsers/DALInsertParser.inc';
22 require_once
'DAL/Parsers/DALUpdateParser.inc';
23 require_once
'DAL/Parsers/DALWhereParser.inc';
24 require_once
'DAL/Parsers/DALGroupByParser.inc';
25 require_once
'DAL/Parsers/DALOrderByParser.inc';
26 require_once
'DAL/Parsers/DALTruncateParser.inc';
27 require_once
'DAL/Parsers/DALUnionParser.inc';
28 require_once
'DAL/DALBaker.inc';
29 require_once
'XML/XML.inc';
50 private function __construct()
69 public static function parse(DomElement $query)
71 $firstChild = $query->firstChild;
72 if ($firstChild === NULL) {
76 foreach ($query->childNodes as $childNode) {
77 if ($childNode->nodeType === XML_ELEMENT_NODE) {
78 $type = $childNode->tagName;
120 $function = $query->getElementsByTagName(
'function')->item(0);
121 self::validateFunctionTag($function);
122 $sql = self::parseSqlFunction($function);
129 $sql = self::parseCallQuery($query);
133 $sql = current($sql);
145 if (is_array($sql) === TRUE) {
146 $sqlArray[
'query'] = $sql;
148 self::_convertAliases($sqlArray[
'query'], $aliases);
149 $sqlArray[
'query_type'] = $type;
168 private static function _convertAliases(array &$sqlArray, array &$aliases)
171 foreach ($sqlArray as $key => $value) {
172 if (is_array($value) === FALSE) {
176 if (isset($value[
'alias']) === TRUE) {
177 $clonedArray = array();
178 $clonedArray = array_merge($clonedArray, $sqlArray);
179 unset($clonedArray[$key][
'alias']);
180 $aliases[$value[
'alias']] = $clonedArray;
183 if (array_key_exists(
'isalias', $value) === TRUE) {
184 $sqlArray[$key] = $aliases[$value[
'name']];
187 if (is_array($value) === TRUE) {
188 self::_convertAliases($sqlArray[$key], $aliases);
214 $limit = $select->getElementsByTagName(
'limit')->item(0);
215 if ($limit !== NULL) {
216 $query[
'LIMIT'] = array();
217 $query[
'LIMIT'][
'rows'] = $limit->nodeValue;
218 $query[
'LIMIT'][
'offset'] = $limit->getAttribute(
'offset');
249 $conditions = array();
250 if ($inCond !== NULL) {
251 $type = strtoupper($inCond->tagName);
253 $conditions[$type] = array();
254 $conditions[$type][
'table'] = $inCond->getAttribute(
'table');
255 $conditions[$type][
'column'] = $inCond->getAttribute(
'column');
256 $conditions[$type][
'CONDITIONS'] = array();
259 $select = $inCond->getElementsByTagName(
'select')->item(0);
260 if ($select === NULL) {
264 $conditions[$type][
'CONDITIONS'][] = $inCond->nodeValue;
266 foreach ($inCond->childNodes as $child) {
267 if ($child->nodeType === XML_ELEMENT_NODE) {
268 $val = self::parseSingleField($child);
270 $conditions[$type][
'CONDITIONS'][] = $val;
304 $conditions = array();
305 if ($existsCond !== NULL) {
306 $type = strtoupper($existsCond->tagName);
307 $select = $existsCond->getElementsByTagName(
'select')->item(0);
309 if ($select !== NULL) {
311 $conditions[$type] = array(
333 $query[
'CALL'] = array();
336 $function = $call->getElementsByTagName(
'function')->item(0);
337 $query[
'CALL'] = self::parseSqlFunction($function);
357 foreach ($parent->childNodes as $field) {
358 if ($field->nodeType !== XML_ELEMENT_NODE) {
362 $newField = self::parseSingleField($field);
363 if ($newField !== FALSE) {
364 $fields[] = $newField;
387 if ($fieldXML->tagName ===
'field') {
389 $field[
'table'] = $fieldXML->getAttribute(
'table');
390 $field[
'column'] = $fieldXML->getAttribute(
'column');
391 if ($fieldXML->nodeValue !==
'') {
392 $field[
'alias'] = $fieldXML->nodeValue;
394 }
else if ($fieldXML->tagName ===
'value') {
395 $nodeValue = $fieldXML->nodeValue;
396 if ($fieldXML->getAttribute(
'type') ===
'integer') {
397 $nodeValue = (int)$nodeValue;
400 if ($fieldXML->getAttribute(
'alias') ===
'') {
404 $field[
'alias'] = $fieldXML->getAttribute(
'alias');
405 $field[
'value'] = $nodeValue;
407 }
else if ($fieldXML->tagName ===
'alias') {
409 $field[
'name'] = $fieldXML->getAttribute(
'name');
410 $field[
'isalias'] = TRUE;
411 }
else if ($fieldXML->tagName ===
'literal') {
412 $field = $fieldXML->nodeValue;
413 }
else if ($fieldXML->tagName ===
'function') {
414 $field = self::parseSqlFunction($fieldXML);
415 }
else if ($fieldXML->tagName ===
'hook') {
417 }
else if ($fieldXML->tagName ===
'math-op') {
418 $field = self::parseMathOperation($fieldXML);
419 }
else if ($fieldXML->tagName ===
'select') {
440 $query[$op] = array();
441 if (($op ===
'MATH-OP') && ($parent->getAttribute(
'alias') !==
'')) {
442 $query[$op][
'alias'] = $parent->getAttribute(
'alias');
445 foreach ($parent->childNodes as $node) {
446 if ($node->nodeType === XML_ELEMENT_NODE) {
447 switch ($node->tagName) {
452 $query[$op][] = self::parseMathOperation($node, strtoupper($node->tagName));
455 if ($node->tagName ===
'select') {
458 $query[$op][] = self::parseSingleField($node);
489 if ($function !== NULL) {
490 $query[
'FUNCTION'] = array();
491 $query[
'FUNCTION'][
'function'] = $function->getAttribute(
'function');
492 $query[
'FUNCTION'][
'ARGS'] = array();
494 if ($function->getAttribute(
'alias') !==
'') {
495 $query[
'FUNCTION'][
'alias'] = $function->getAttribute(
'alias');
498 foreach ($function->childNodes as $arg) {
499 if ($arg->nodeType === XML_ELEMENT_NODE) {
501 if ($field !== NULL) {
502 switch ($field->tagName) {
508 $fields = self::parseChildFields($arg);
509 $query[
'FUNCTION'][
'ARGS'][] = array_shift($fields);
513 $query[
'FUNCTION'][
'ARGS'][] = $arg->nodeValue;
541 if ($field->hasAttribute(
'table') === FALSE) {
543 }
else if ($field->getAttribute(
'column') ===
'') {
545 }
else if (($allowAlias === FALSE) && ($field->nodeValue !==
'')) {
546 $msg =
'alias for field is not allowed here.';
567 if ($fields->getAttribute(
'table') === FALSE) {
568 $msg =
'Fields tag must have table attribute.';
573 $fields = $fields->getElementsByTagName(
'field');
574 if ($fields->length === 0) {
575 $msg =
'Fields tag with no field tags.';
580 foreach ($fields as $field) {
581 if ($field->nodeValue ===
'') {
582 $msg =
'Field with no value.';
605 foreach ($valuesTag->childNodes as $value) {
606 if ($value->nodeType === XML_ELEMENT_NODE) {
607 if ($value->tagName ===
'value') {
609 self::validateValueTag($value);
610 }
else if ($value->tagName ===
'select') {
617 if ($foundValue === FALSE) {
618 $msg =
'Values tag with no value/select tags.';
639 if ($value->getAttribute(
'column') ===
'') {
640 $msg =
'Update query value tags must have column attribute.';
657 $unionTag = $parentNode->getElementsByTagName(
'union')->item(0);
658 if ($unionTag === NULL) {
659 $unionTag = $parentNode->getElementsByTagName(
'union-all')->item(0);
678 foreach ($xmlQuery->childNodes as $child) {
679 if ($child->nodeType !== XML_ELEMENT_NODE) {
683 if ($child->tagName ===
'hook') {
684 $list[] = $child->getAttribute(
'id');
705 if ($function->getAttribute(
'function') ===
'') {
706 $msg =
'function tag must have \'function\' attribute.';
727 if ($hook->getAttribute(
'id') ===
'') {