Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
DALAlterParser.inc
1 <?php
13 require_once 'DAL/Parsers/DALQueryParser.inc';
14 require_once 'DAL/Parsers/DALSchemaParser.inc';
15 
24 {
25 
26 
35  private function __construct()
36  {
37 
38  }//end __construct()
39 
40 
49  public static function parse(DomElement $xmlQuery)
50  {
51  $query = array();
52  $alterTag = $xmlQuery->getElementsByTagName('alter')->item(0);
53  if ($alterTag !== NULL) {
54  $query['ALTER'] = array();
55  // Alter table.
56  $query['ALTER']['table'] = $alterTag->getAttribute('table');
57 
58  // Get ALTER TABLE query info.
59  foreach ($alterTag->childNodes as $alter) {
60  if ($alter->nodeType === XML_ELEMENT_NODE) {
61  $info = NULL;
62  switch ($alter->tagName) {
63  case 'add-column' :
64  case 'modify-column' :
65  $info = array();
66  $info['COLUMNS'] = DALSchemaParser::getTableColumns($alter);
67  break;
68  case 'drop-column' :
69  $info = $alter->nodeValue;
70  break;
71  case 'rename-column' :
72  $info = array();
73  $info['OLD'] = $alter->getAttribute('old');
74  $info['NEW'] = $alter->nodeValue;
75  break;
76  case 'rename-table' :
77  $info = array();
78  $info['OLD'] = $alter->getAttribute('old');
79  $info['NEW'] = $alter->nodeValue;
80  break;
81  case 'add-constraint' :
83  break;
84  case 'drop-constraint' :
85  $info = $alter->nodeValue;
86  break;
87  }//end switch
88 
89  $query['ALTER'][strtoupper($alter->tagName)] = $info;
90  }//end if
91  }//end foreach
92  }//end if
93 
94  return $query;
95 
96  }//end parse()
97 
98 
115  public static function validate(DomElement $query)
116  {
117  // Alter tag must have table attribute.
118  $alterTag = $query->getElementsByTagName('alter')->item(0);
119  if ($alterTag === NULL) {
120  throw new DALParserException('Alter tag not found.');
121  }
122 
123  if ($alterTag->getAttribute('table') === '') {
124  $msg = 'Alter tag must have table attribute.';
125  throw new DALParserException($msg);
126  }
127 
128  $emptyTag = TRUE;
129  foreach ($alterTag->childNodes as $alter) {
130  if ($alter->nodeType === XML_ELEMENT_NODE) {
131  $emptyTag = FALSE;
132  $info = NULL;
133  switch ($alter->tagName) {
134  case 'add-column' :
135  case 'modify-column' :
137  break;
138  case 'drop-column' :
139  if ($alter->nodeValue === '') {
140  $msg = 'drop-column tag must have a value.';
141  throw new DALParserException($msg);
142  }
143 
144  break;
145  case 'rename-column' :
146  if ($alter->nodeValue === '') {
147  $msg = 'rename-column tag must have a value.';
148  throw new DALParserException($msg);
149  }
150 
151  if ($alter->getAttribute('old') === '') {
152  $msg = 'rename-column tag must have a old attr.';
153  throw new DALParserException($msg);
154  }
155 
156  break;
157  case 'rename-table' :
158  if ($alter->nodeValue === '') {
159  $msg = 'rename-table tag must have a value.';
160  throw new DALParserException($msg);
161  }
162 
163  if ($alter->getAttribute('old') === '') {
164  $msg = 'rename-table tag must have a old attr.';
165  throw new DALParserException($msg);
166  }
167 
168  break;
169  case 'add-constraint' :
171  break;
172  case 'drop-constraint' :
173  if ($alter->nodeValue === '') {
174  $msg = 'drop-constraint tag must have a value.';
175  throw new DALParserException($msg);
176  }
177 
178  break;
179  default :
180  $msg = 'Invalid ALTER TABLE tag: '.$alter->tagName;
181  throw new DALParserException($msg);
182  break;
183  }//end switch
184  }//end if
185  }//end foreach
186 
187  if ($emptyTag === TRUE) {
188  $msg = 'Empty alter tag found.';
189  throw new DALParserException($msg);
190  }
191 
192  }//end validate()
193 
194 
195 }//end class
196 
197 ?>