Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
DALOracleConverter.inc
1 <?php
13 require_once dirname(__FILE__).'/DALConverter.inc';
14 
26 {
27 
34  protected $name = 'Oracle';
35 
42  protected $dataTypes = array(
43  'char' => 'CHAR',
44  'varchar' => 'VARCHAR2',
45  'clob' => 'CLOB',
46  'blob' => 'BLOB',
47  'integer' => 'INT',
48  'smallint' => 'SMALLINT',
49  'numeric' => 'NUMERIC',
50  'float' => 'FLOAT',
51  'real' => 'REAL',
52  'double_precision' => 'DOUBLE PRECISION',
53  'boolean' => 'CHAR(1)',
54  'date' => 'DATE',
55  'time' => 'TIMESTAMP',
56  'timestamp' => 'TIMESTAMP',
57  'interval' => '',
58  'time with time zone' => 'TIMESTAMP',
59  );
60 
61 
67  public function __construct()
68  {
69 
70  }//end __construct()
71 
72 
85  public function convertCreateTable(array $table)
86  {
87  $sql = $this->convertCreateHeader($table);
88  $sql .= "\n".$this->convertCreateColumns($table).',';
89  $sql .= "\n".$this->convertConstraints($table['CONSTRAINTS']);
90  $sql .= $this->convertCreateFooter($table);
91 
92  $indexes = $this->convertCreateIndexes($table);
93  if ($indexes !== '') {
94  $sql .= "\n".$indexes;
95  }
96 
97  if (isset($table['SEQUENCES']) === TRUE) {
98  $sql .= "\n".$this->convertCreateSequences($table['SEQUENCES']);
99  }
100 
101  return $sql;
102 
103  }//end convertCreateTable()
104 
105 
119  protected function convertSingleCreateColumn(array $col)
120  {
121  if (strtolower($col['type']) === 'boolean') {
122  if ($col['default'] !== '') {
123  if (strtolower($col['default']) === 'false') {
124  $col['default'] = "'0'";
125  } else if (strtolower($col['default']) === 'true') {
126  $col['default'] = "'1'";
127  }
128  }
129  }
130 
131  return parent::convertSingleCreateColumn($col);
132 
133  }//end convertSingleCreateColumn()
134 
135 
147  protected function handleFunctionSeqNextVal($seqName)
148  {
149  $sql = $seqName[0].'.nextval';
150  return $sql;
151 
152  }//end handleFunctionSeqNextVal()
153 
154 
165  protected function handleFunctionSeqCurrVal($seqName)
166  {
167  $sql = $seqName[0].'.currval';
168  return $sql;
169 
170  }//end handleFunctionSeqCurrVal()
171 
172 
182  public function handleFunctionTableExists($tableName, $schema=NULL)
183  {
184  if (is_array($tableName) === TRUE) {
185  $tableName = $tableName[0];
186  }
187 
188  $sql = 'SELECT count(table_name) FROM user_tables WHERE table_name = UPPER('.$tableName.')';
189  return $sql;
190 
191  }//end handleFunctionTableExists()
192 
193 
202  public function handleFunctionSequenceExists($sequenceName)
203  {
204  if (is_array($sequenceName) === TRUE) {
205  $sequenceName = $sequenceName[0];
206  }
207 
208  $sql = 'SELECT count(sequence_name) FROM user_sequences WHERE sequence_name = UPPER('.$sequenceName.')';
209  return $sql;
210 
211  }//end handleFunctionSequenceExists()
212 
213 
225  protected function handleFunctionToDate(array $args)
226  {
227  $iso8601_date = $this->convertSingleField($args[0]);
228  $sql = 'TO_DATE('.$iso8601_date.', \'YYYY-MM-DD HH24:MI:SS\')';
229  return $sql;
230 
231  }//end handleFunctionToDate()
232 
233 
242  protected function convertWithQuery(array $withQuery)
243  {
244  $firstSelectQuery = $withQuery['AFTER'];
245 
246  $sql = '';
247  $sql .= $this->convertSelectQuery($firstSelectQuery, 0, array('where'));
248 
249  $secondSelectQuery = $withQuery['UNION-ALL']['SELECTS'][1];
250 
251  $sql .= 'CONNECT BY PRIOR ';
252  $sql .= $this->convertWhereClause($withQuery['CONNECT-BY-PRIOR'], 0, FALSE);
253 
254  $sql .= 'START WITH ';
255  if (isset($firstSelectQuery['WHERE']) === TRUE) {
256  $sql .= $this->convertWhereClause($firstSelectQuery['WHERE'], 0, FALSE);
257  }
258 
259  return $sql;
260 
261  }//end convertWithQuery()
262 
263 
274  public function convertDropTable($tableName, $cascade=FALSE)
275  {
276  $sql = 'DROP TABLE '.$tableName;
277  if ($cascade === TRUE) {
278  $sql .= ' CASCADE CONSTRAINTS';
279  }
280 
281  return $sql;
282 
283  }//end convertDropTable()
284 
285 
297  protected function convertSelectQuery(array $sqlArray, $level=0, array $ignore=array())
298  {
299  // If there are no tables specified in a SELECT query,
300  // use DUAL automatically.
301  if (empty($sqlArray['FROM'])) {
302  $sqlArray['FROM'] = Array('dual');
303  }
304 
305  $sql = parent::convertSelectQuery($sqlArray, $level, $ignore);
306 
307  // Start with.
308  if (isset($sqlArray['START-WITH']) === TRUE) {
309  $sql .= "\n".$this->getConstructName('start-with');
310  $startWith = $this->convertWhereClause($sqlArray['START-WITH'], $level);
311  $startWith = trim($startWith);
312  $startWith = trim($startWith, '()');
313  $sql .= "\n".$startWith;
314  }
315 
316  // Start with.
317  if (isset($sqlArray['CONNECT-BY']) === TRUE) {
318  $sql .= "\n".$this->getConstructName('connect-by');
319  $connectBy = $this->convertWhereClause($sqlArray['CONNECT-BY'], $level);
320  $connectBy = trim($connectBy);
321  $connectBy = trim($connectBy, '()');
322  $sql .= "\n".$connectBy;
323  }
324 
325  return $sql;
326 
327  }//end convertSelectQuery()
328 
329 
338  protected function convertTableAlias($alias)
339  {
340  return ' '.$alias;
341 
342  }//end convertTableAlias()
343 
344 
354  protected function convertUnionsInFromClause(array $union, $level=0)
355  {
356  $type = $this->getUnionType($union);
357  $sql = "(\n".$this->addSpaces($level);
358  $sql .= $this->convertUnions($union, ($level + 2));
359  $sql .= ') '.$union[$type]['alias'];
360  return $sql;
361 
362  }//end convertUnionsInFromClause()
363 
364 
373  protected function convertLimit(array $limit)
374  {
375  $sql = ')';
376  $sql .= ' WHERE row_num >= '.($limit['offset'] + 1);
377 
378  return $sql;
379 
380  }//end convertLimit()
381 
382 
392  protected function convertSelectFieldList(array $fields, $level=1)
393  {
394  $sql = '';
395  // Get Select field names.
396  if (isset($this->query['LIMIT']) === TRUE) {
397  $names = $this->getSelectFieldNames($fields);
398  $namesStr = array();
399  $counter = 0;
400  foreach ($names as $name) {
401  $namesStr[] = 'col_'.($counter++).' as '.$name;
402  }
403 
404  $nameStr = implode(', ', $namesStr);
405  $sql = $nameStr.' FROM (SELECT ';
406 
407  $counter = 0;
408  foreach ($fields as $field) {
409  $field = $this->setSelectFieldAlias($field, ('col_'.$counter++));
410  $list[] = $this->convertSingleField($field);
411  }
412 
413  $sql .= $this->addSpaces($level).$this->separateFields($list);
414  $sql .= ', rownum as row_num';
415 
416  } else {
417  $list = array();
418  foreach ($fields as $field) {
419  $list[] = $this->convertSingleField($field);
420  }
421 
422  $sql = $this->addSpaces($level).$this->separateFields($list);
423  }//end if
424 
425 
426  return $sql;
427 
428  }//end convertSelectFieldList()
429 
430 
439  protected function addSqlFilters(array $sqlArray)
440  {
441  $sql = '';
442 
443  if (isset($this->query['LIMIT']) === TRUE) {
444  $limit = $this->query['LIMIT'];
445  if (isset($this->query['WHERE']) === TRUE) {
446  $sql .= ' AND ';
447  } else {
448  $sql .= ' WHERE ';
449  }
450 
451  $sql .= ' rownum <= '.($limit['rows'] + $limit['offset']);
452  }
453 
454  // Group by.
455  if (isset($sqlArray['GROUP-BY']) === TRUE) {
456  $sql .= $this->convertGroupBy($sqlArray['GROUP-BY']);
457  }
458 
459  // Having.
460  if (isset($sqlArray['HAVING']) === TRUE) {
461  $sql .= "\n".$this->getConstructName('having');
462  $sql .= "\n".$this->convertWhereClause($sqlArray['HAVING']);
463  }
464 
465  // Order By.
466  if (isset($sqlArray['ORDER-BY']) === TRUE) {
467  $sql .= "\n".$this->convertOrderBy($sqlArray['ORDER-BY']);
468  }
469 
470  // Limit.
471  if (isset($sqlArray['LIMIT']) === TRUE) {
472  $sql .= "\n".$this->convertLimit($sqlArray['LIMIT']);
473  }
474 
475  return $sql;
476 
477  }//end addSqlFilters()
478 
479 
491  protected function handleFunctionSubstring(array $args)
492  {
493  // SUBSTRING(input FROM start-position [FOR length])
494  if (count($args) != 2 && count($args) != 3) {
495  $msg = 'The SUBSTR() function only accepts two or three parameters.';
496  throw new DALConverterException($msg);
497  }
498 
499  $input = $this->convertSingleField($args[0]);
500  $start = $args[1];
501  $length = isset($args[2]) ? ",$args[2]" : '';
502  $sql = "SUBSTR($input, $start $length)";
503 
504  return $sql;
505 
506  }//end handleFunctionSubstring()
507 
508 
509 }//end class
510 ?>