Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
DALInsertParser.inc
1 <?php
13 require_once 'DAL/Parsers/DALQueryParser.inc';
14 require_once 'DAL/Parsers/DALSelectParser.inc';
15 require_once 'XML/XML.inc';
16 
25 {
26 
27 
36  private function __construct()
37  {
38 
39  }//end __construct()
40 
41 
60  public static function parse(DomElement $xmlQuery)
61  {
62  $query = array();
63  $insertTag = $xmlQuery->getElementsByTagName('insert')->item(0);
64  if ($insertTag !== NULL) {
65  $query['INSERT'] = array();
66 
67  // Insert fields.
68  $fieldsTag = $insertTag->getElementsByTagName('fields')->item(0);
69  if ($fieldsTag !== NULL) {
70  $query['INSERT']['into'] = $fieldsTag->getAttribute('table');
71  $query['INSERT']['FIELDS'] = array();
72 
73  $fields = $fieldsTag->getElementsByTagName('field');
74  foreach ($fields as $field) {
75  $query['INSERT']['FIELDS'][] = $field->nodeValue;
76  }
77  }
78 
79  // Check if we have select or values.
80  $select = $insertTag->getElementsByTagName('select')->item(0);
81  if ($select === NULL || ($select->parentNode->tagName !== 'insert' && $select->parentNode->tagName !== 'values')) {
82  $query['INSERT']['VALUES'] = array();
83  // Must be using values.
84  $valuesTag = $insertTag->getElementsByTagName('values')->item(0);
85  foreach ($valuesTag->childNodes as $value) {
86  if ($value->nodeType === XML_ELEMENT_NODE) {
87  if ($value->tagName === 'value') {
88  $vCol = $value->getAttribute('column');
89  if (XML::hasChildElements($value) === TRUE) {
90  $colVal = self::parseSingleField(XML::getFirstChildElement($value));
91  } else {
92  $colVal = $value->nodeValue;
93  }
94 
95  $query['INSERT']['VALUES'][$vCol] = $colVal;
96  }
97  }
98  }
99  } else {
100  // Inner Select.
101  $query['INSERT']['VALUES'] = DALSelectParser::parse($select);
102  }//end if
103  }//end if
104 
105  return $query;
106 
107  }//end parse()
108 
109 
123  public static function validate(DomElement $query)
124  {
125  $insertTag = $query->getElementsByTagName('insert')->item(0);
126 
127  if ($insertTag === NULL) {
128  throw new DALParserException('insert tag not found.');
129  } else {
130  $fields = $insertTag->getElementsByTagName('fields')->item(0);
131 
132  if ($fields === NULL) {
133  $msg = 'Insert query must have fields tag.';
134  throw new DALParserException($msg);
135  } else if ($fields->parentNode->tagName === 'insert') {
136 
137  if ($fields->getAttribute('table') === '') {
138  $msg = 'Fields tag must have table attribute.';
139  throw new DALParserException($msg);
140  }
141 
142  // Must have select or values tag.
143  $tags = 0;
144  $valsTag = $insertTag->getElementsByTagName('values')->item(0);
145  if ($valsTag !== NULL) {
146  self::validateFieldsTag($fields);
147  if ($valsTag->parentNode->tagName === 'insert') {
148  $tags++;
149  // Values tag must have value tags.
150  self::validateValuesTag($valsTag);
151  }
152  }
153 
154  $selTag = $insertTag->getElementsByTagName('select')->item(0);
155  if ($selTag !== NULL) {
156  if ($selTag->parentNode->tagName === 'insert') {
157  $tags++;
158  // Validate the select query.
159  DALSelectParser::validate($insertTag);
160  }
161  }
162 
163  if ($tags === 0) {
164  $msg = 'Insert query must have values or select tag.';
165  throw new DALParserException($msg);
166  }
167 
168  } else {
169  // This is not in insert tag.
170  $msg = 'Inser query must have fields tag.';
171  throw new DALParserException($msg);
172  }//end if
173  }//end if
174 
175  }//end validate()
176 
177 
178 }//end class
179 
180 ?>