Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
DALMssqlConverter.inc
1 <?php
13 require_once dirname(__FILE__).'/DALConverter.inc';
14 
26 {
27 
34  protected $tableName = '';
35 
42  protected $name = 'MSSQL';
43 
50  protected $dataTypes = array(
51  'char' => 'NCHAR',
52  'varchar' => 'NVARCHAR',
53  'clob' => 'NVARCHAR(4000)',
54  'blob' => 'VARBINARY',
55  'integer' => 'INT',
56  'smallint' => 'SMALLINT',
57  'numeric' => 'NUMERIC',
58  'float' => 'FLOAT',
59  'real' => 'REAL',
60  'double_precision' => 'FLOAT',
61  'boolean' => 'CHAR(1)',
62  'date' => 'DATETIME',
63  'time' => 'DATETIME',
64  'timestamp' => 'DATETIME',
65  'time with time zone' => 'DATETIME',
66  );
67 
68 
74  public function __construct()
75  {
76 
77  }//end __construct()
78 
79 
90  public function convertCreateTable(array $table)
91  {
92  $this->tableName = $table['table'];
93 
94  $sql = $this->convertCreateHeader($table);
95  $sql .= "\n".$this->convertCreateColumns($table);
96  $sql .= $this->convertCreateFooter($table);
97 
98  if (isset($table['CONSTRAINTS']) === TRUE && empty($table['CONSTRAINTS']) === FALSE) {
99  $constraints = $this->convertAlterQueryAddConstraint($table['CONSTRAINTS'], TRUE);
100  $sql .= "\n".$constraints;
101  }
102 
103  $indexes = $this->convertCreateIndexes($table);
104  if ($indexes !== '') {
105  $sql .= "\n".$indexes;
106  }
107 
108  if (isset($table['SEQUENCES']) === TRUE) {
109  $sql .= "\n".$this->convertCreateSequences($table['SEQUENCES']);
110  }
111 
112  return $sql;
113 
114  }//end convertCreateTable()
115 
116 
128  protected function convertAlterQueryAddConstraint(array $constraints, $incHeader=FALSE)
129  {
130  $sql = '';
131  if (empty($constraints) === FALSE) {
132  $convertedCons = $this->convertConstraints($constraints, TRUE);
133  $cons = array();
134  foreach ($convertedCons as $constraint) {
135  $sql = '';
136  if ($incHeader === TRUE) {
137  $sql .= $this->convertAlterQueryHeader($this->tableName);
138  }
139 
140  $sql .= 'ADD '.$constraint;
141 
142  $cons[] = $sql;
143  }
144 
145  $sql = implode(";\n", $cons).';';
146  }
147 
148  return $sql;
149 
150  }//end convertAlterQueryAddConstraint()
151 
152 
161  protected function convertCreateIndexes(array $table)
162  {
163  $sql = '';
164  $indexes = array();
165  foreach ($table['INDEXES'] as $index) {
166  $indexes[] = $this->convertSingleIndex($index, $table['table']);
167  }
168 
169  if (empty($indexes) === FALSE) {
170  $sql .= implode(";\n", $indexes).';';
171  }
172 
173  return $sql;
174 
175  }//end convertCreateIndexes()
176 
177 
189  protected function convertSingleIndex(array $idx, $tableName)
190  {
191  $sql = 'CREATE INDEX '.$idx['name'].' ON '.$tableName.' (';
192  $sql .= $this->separateFields($idx['COLUMNS']).')';
193  return $sql;
194 
195  }//end convertSingleIndex()
196 
197 
206  protected function convertSingleSequence($sequence)
207  {
208  // Create a new sequence table and a row.
209  $sql = 'CREATE TABLE '.$sequence.' ( id INT NOT NULL);
210  INSERT INTO '.$sequence.' VALUES(0);';
211 
212  return $sql;
213 
214  }//end convertSingleSequence()
215 
216 
228  protected function handleFunctionSeqNextVal($seqName)
229  {
230  $sql = 'UPDATE '.$seqName[0].' SET id=id+1';
231  return $sql;
232 
233  }//end handleFunctionSeqNextVal()
234 
235 
247  protected function handleFunctionSeqCurrVal($seqName)
248  {
249  $sql = 'SELECT TOP 1 id FROM '.$seqName[0].'';
250  return $sql;
251 
252  }//end handleFunctionSeqCurrVal()
253 
254 
263  public function handleFunctionSequenceExists($sequenceName)
264  {
265  return $this->handleFunctionTableExists($sequenceName);
266 
267  }//end handleFunctionSequenceExists()
268 
269 
280  public function convertDropSequence($sequenceName)
281  {
282  $sql = $this->convertDropTable($sequenceName);
283  return $sql;
284 
285  }//end convertDropSequence()
286 
287 
296  protected function handleFunctionConcat(array $args)
297  {
298  $sql = implode(' + ', $args);
299  return $sql;
300 
301  }//end handleFunctionConcat()
302 
303 
315  protected function handleFunctionToDate(array $args)
316  {
317  $iso8601_date = $this->convertSingleField($args[0]);
318 
319  // Explicitly convert using the "yyyy-mm-dd HH:mi:ss" style (which
320  // is what the '120' magic number is for).
321  $sql = 'CONVERT(DATETIME, '.$iso8601_date.', 120)';
322  return $sql;
323 
324  }//end handleFunctionToDate()
325 
326 
335  protected function convertUnionDisplayName($type)
336  {
337  if (strtoupper($type) === 'UNION-ALL') {
338  return 'UNION ALL';
339  }
340 
341  return $type;
342 
343  }//end convertUnionDisplayName()
344 
345 
360  protected function convertInsertQueryValues(array $insert)
361  {
362  $sql = '';
363  if (isset($insert['VALUES']['SELECT']) === FALSE) {
364  $valuesList = array();
365  foreach ($insert['FIELDS'] as $field) {
366  if (isset($insert['VALUES'][$field]) === TRUE) {
367  $value = $insert['VALUES'][$field];
368  } else {
369  $msg = 'Could not convert INSERT query.';
370  $msg .= ' Field "'.$field.'" not found in values list.';
371  throw new DALConverterException($msg);
372  }
373 
374  $valuesList[] = $this->convertSingleField($value);
375  }
376 
377  $sql .= 'VALUES ('.$this->separateFields($valuesList).') ';
378  } else {
379  // Select statement.
380  $sql .= $this->convertSelectQuery($insert['VALUES']);
381  }
382 
383  return $sql;
384 
385  }//end convertInsertQueryValues()
386 
387 
396  protected function convertCallQuery(array $callQuery)
397  {
398  $sql = 'EXEC ';
399  $sql .= $this->convertSingleFunction($callQuery, TRUE);
400  return $sql;
401 
402  }//end convertCallQuery()
403 
404 
416  protected function convertSingleFunction(array $function, $isProcedure=FALSE)
417  {
418  $sql = '';
419  $fnName = $function['FUNCTION']['function'];
420  $fnHandler = 'handleFunction'.$fnName;
421  $args = array();
422  foreach ($function['FUNCTION']['ARGS'] as $arg) {
423  $args[] = $this->convertSingleField($arg);
424  }
425 
426  if (method_exists($this, $fnHandler) === FALSE) {
427  $sql .= $function['FUNCTION']['function'];
428  if ($isProcedure === TRUE) {
429  $sql .= ' ';
430  } else {
431  $sql .= '(';
432  }
433 
434  $sql .= $this->separateFields($args);
435 
436  if ($isProcedure === TRUE) {
437  $sql .= ' ';
438  } else {
439  $sql .= ')';
440  }
441 
442  } else {
443  $sql .= $this->$fnHandler($args);
444  }
445 
446  if (isset($function['FUNCTION']['alias']) === TRUE) {
447  $sql .= ' as '.$function['FUNCTION']['alias'];
448  }
449 
450  return $sql;
451 
452  }//end convertSingleFunction()
453 
454 
465  public function handleFunctionTableExists($tableName, $schema=NULL)
466  {
467  if (is_array($tableName) === TRUE) {
468  $tableName = $tableName[0];
469  }
470 
471  if ($schema === NULL) {
472  $schema = '\''.DAL::getDbName().'\'';
473  }
474 
475  $sql = 'SELECT count(*) FROM INFORMATION_SCHEMA.tables WHERE TABLE_NAME = '.$tableName.' AND TABLE_SCHEMA = \'dbo\' AND TABLE_CATALOG = '.$schema;
476  return $sql;
477 
478  }//end handleFunctionTableExists()
479 
480 
490  protected function convertSelectFieldList(array $fields, $level=1)
491  {
492  $sql = '';
493  $sqlFlist = '';
494  if (isset($this->query['LIMIT']) === TRUE) {
495  $offset = $this->query['LIMIT']['offset'];
496  if ($offset === 0) {
497  $offset = 1;
498  }
499 
500  if (isset($this->query['ORDER-BY']) === TRUE) {
501  $orderBy = $this->query['ORDER-BY'];
502  foreach ($orderBy as $key => $field) {
503  if ($key !== 'direction') {
504  if (is_array($field) === TRUE) {
505  $fields[] = $this->convertSingleField($field);
506  } else {
507  $fields[] = $field;
508  }
509  }
510  }
511  }
512 
513  $sql .= '* FROM (SELECT TOP '.$offset;
514  $sql .= ' * FROM (';
515 
516  $sqlFlist = 'SELECT TOP '.$this->query['LIMIT']['rows'].' ';
517  }//end if
518 
519  $list = array();
520  foreach ($fields as $field) {
521  $list[] = $this->convertSingleField($field);
522  }
523 
524  $list = array_unique($list);
525 
526  $sqlFlist .= $this->addSpaces($level).$this->separateFields($list);
527 
528 
529  $sql .= $sqlFlist;
530 
531 
532  return $sql;
533 
534  }//end convertSelectFieldList()
535 
536 
545  protected function convertLimit(array $limit)
546  {
547  // Reverse order by direction.
548  $sql = '';
549  if (isset($this->query['ORDER-BY']) === TRUE) {
550  $orderBy = $this->query['ORDER-BY'];
551 
552  foreach ($orderBy as $key => $field) {
553  if (isset($field['table']) === TRUE) {
554  $field['table'] = '';
555  }
556 
557  $orderBy[$key] = $field;
558  }
559 
560  for ($i = 0; $i < 2; $i++) {
561  if (isset($orderBy['direction']) === TRUE) {
562  if (strtolower($orderBy['direction']) === 'desc') {
563  $orderBy['direction'] = '';
564  } else {
565  $orderBy['direction'] = 'desc';
566  }
567  }
568 
569  $sql .= ') as limCl '.$this->convertOrderBy($orderBy);
570  }
571  }//end if
572 
573  return $sql;
574 
575  }//end convertLimit()
576 
577 
589  protected function handleFunctionSubstring(array $args)
590  {
591  // SUBSTRING(input FROM start-position [FOR length])
592  if (count($args) != 2 && count($args) != 3) {
593  $msg = 'The SUBSTR() function only accepts two or three parameters.';
594  throw new DALConverterException($msg);
595  }
596 
597  $input = $this->convertSingleField($args[0]);
598  $start = $args[1];
599  $length = isset($args[2]) ? ",$args[2]" : '';
600  $sql = "SUBSTRING($input, $start $length)";
601 
602  return $sql;
603 
604  }//end handleFunctionSubstring()
605 
606 
607 }//end class
608 ?>