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';
37 private function __construct()
52 public static function parse(DomElement $parent, $type=
'where')
57 foreach ($parent->childNodes as $tag) {
58 if ($tag->nodeType !== XML_ELEMENT_NODE) {
60 }
else if ($tag->tagName !== $type) {
67 if ($whereTag !== NULL) {
68 $where[strtoupper($type)] = self::_getWhereConditions($whereTag);
71 $joins = self::_praseJoinsForSelect($parent);
72 if (empty($joins) === FALSE) {
73 $where = array_merge($where, $joins);
91 private static function _getWhereConditions(DomElement $parent, $type=
'AND', $level=1)
93 $conditions = array();
94 foreach ($parent->childNodes as $cond) {
95 if ($cond->nodeType !== XML_ELEMENT_NODE) {
100 $currentCond = array();
101 $currentCond[
'compare'] = array();
102 $currentCond[
'type'] = $cond->tagName;
105 if ($cond->getAttribute(
'table') !==
'') {
107 $currentCond[
'compare'][
'table'] = $cond->getAttribute(
'table');
108 $currentCond[
'compare'][
'column'] = $cond->getAttribute(
'column');
113 $currentCond[
'compare'] = $value;
119 if ($child !== NULL) {
120 $value = self::parseSingleField($child);
123 $value = $cond->nodeValue;
126 $currentCond[
'to'] = $value;
127 $conditions[$type][] = $currentCond;
128 }
else if (($cond->tagName ===
'in') || ($cond->tagName ===
'not-in')) {
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') {
175 private static function _praseJoinsForSelect(DomElement $select)
178 $joinsTag = $select->getElementsByTagName(
'joins')->item(0);
179 if ($joinsTag !== NULL) {
180 $joinList[
'JOIN'] = array();
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;
207 public static function validate(DomElement $parent)
212 $condSigns[
'in'] =
'';
213 $condSigns[
'not-in'] =
'';
216 $condSigns[
'exists'] =
'';
217 $condSigns[
'not-exists'] =
'';
220 foreach ($parent->childNodes as $cond) {
221 if ($cond->nodeType !== XML_ELEMENT_NODE) {
225 $tagName = $cond->tagName;
227 if (isset($condSigns[$tagName]) === TRUE) {
228 if ($tagName ===
'is-null' || $tagName ===
'not-null') {
230 $msg = $tagName.
' tag must have child.';
237 if ((Xml::childCount($cond) !== 2) && ($tagName !==
'exists') && ($tagName !==
'not-exists')) {
238 if ($cond->getAttribute(
'table') ===
'') {
239 $msg =
'WHERE condition with no table attribute.';
243 if ($cond->getAttribute(
'column') ===
'') {
244 $msg =
'WHERE condition with no column attribute.';
249 if (($tagName ===
'in') || ($tagName ===
'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.';
260 if ($select !== NULL) {
266 if (($tagName ===
'exists') || ($tagName ===
'not-exists')) {
268 $select = $cond->getElementsByTagName(
'select')->item(0);
269 if ($select === NULL) {
270 $msg = $tagName.
' must have select tag.';
277 }
else if (($tagName ===
'or') || ($tagName ===
'and')) {
278 self::validate($cond);
279 }
else if ($tagName !==
'hook') {
280 $msg =
'Found invalid WHERE comparison type "'.$tagName.
'".';