13 require_once dirname(__FILE__).
'/DALConverter.inc';
34 protected $tableName =
'';
50 protected $dataTypes = array(
52 'varchar' =>
'NVARCHAR',
53 'clob' =>
'NVARCHAR(4000)',
54 'blob' =>
'VARBINARY',
56 'smallint' =>
'SMALLINT',
57 'numeric' =>
'NUMERIC',
60 'double_precision' =>
'FLOAT',
61 'boolean' =>
'CHAR(1)',
64 'timestamp' =>
'DATETIME',
65 'time with time zone' =>
'DATETIME',
92 $this->tableName = $table[
'table'];
95 $sql .=
"\n".$this->convertCreateColumns($table);
98 if (isset($table[
'CONSTRAINTS']) === TRUE && empty($table[
'CONSTRAINTS']) === FALSE) {
100 $sql .=
"\n".$constraints;
104 if ($indexes !==
'') {
105 $sql .=
"\n".$indexes;
108 if (isset($table[
'SEQUENCES']) === TRUE) {
109 $sql .=
"\n".$this->convertCreateSequences($table[
'SEQUENCES']);
131 if (empty($constraints) === FALSE) {
134 foreach ($convertedCons as $constraint) {
136 if ($incHeader === TRUE) {
140 $sql .=
'ADD '.$constraint;
145 $sql = implode(
";\n", $cons).
';';
165 foreach ($table[
'INDEXES'] as $index) {
169 if (empty($indexes) === FALSE) {
170 $sql .= implode(
";\n", $indexes).
';';
191 $sql =
'CREATE INDEX '.$idx[
'name'].
' ON '.$tableName.
' (';
209 $sql =
'CREATE TABLE '.$sequence.
' ( id INT NOT NULL);
210 INSERT INTO '.$sequence.
' VALUES(0);';
230 $sql =
'UPDATE '.$seqName[0].
' SET id=id+1';
249 $sql =
'SELECT TOP 1 id FROM '.$seqName[0].
'';
298 $sql = implode(
' + ', $args);
321 $sql =
'CONVERT(DATETIME, '.$iso8601_date.
', 120)';
337 if (strtoupper($type) ===
'UNION-ALL') {
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];
369 $msg =
'Could not convert INSERT query.';
370 $msg .=
' Field "'.$field.
'" not found in values list.';
377 $sql .=
'VALUES ('.$this->separateFields($valuesList).
') ';
419 $fnName = $function[
'FUNCTION'][
'function'];
420 $fnHandler =
'handleFunction'.$fnName;
422 foreach ($function[
'FUNCTION'][
'ARGS'] as $arg) {
426 if (method_exists($this, $fnHandler) === FALSE) {
427 $sql .= $function[
'FUNCTION'][
'function'];
428 if ($isProcedure === TRUE) {
436 if ($isProcedure === TRUE) {
443 $sql .= $this->$fnHandler($args);
446 if (isset($function[
'FUNCTION'][
'alias']) === TRUE) {
447 $sql .=
' as '.$function[
'FUNCTION'][
'alias'];
467 if (is_array($tableName) === TRUE) {
468 $tableName = $tableName[0];
471 if ($schema === NULL) {
472 $schema =
'\''.DAL::getDbName().
'\'';
475 $sql =
'SELECT count(*) FROM INFORMATION_SCHEMA.tables WHERE TABLE_NAME = '.$tableName.
' AND TABLE_SCHEMA = \'dbo\' AND TABLE_CATALOG = '.$schema;
494 if (isset($this->query[
'LIMIT']) === TRUE) {
495 $offset = $this->query[
'LIMIT'][
'offset'];
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) {
513 $sql .=
'* FROM (SELECT TOP '.$offset;
516 $sqlFlist =
'SELECT TOP '.$this->query[
'LIMIT'][
'rows'].
' ';
520 foreach ($fields as $field) {
524 $list = array_unique($list);
526 $sqlFlist .= $this->
addSpaces($level).$this->separateFields($list);
549 if (isset($this->query[
'ORDER-BY']) === TRUE) {
550 $orderBy = $this->query[
'ORDER-BY'];
552 foreach ($orderBy as $key => $field) {
553 if (isset($field[
'table']) === TRUE) {
554 $field[
'table'] =
'';
557 $orderBy[$key] = $field;
560 for ($i = 0; $i < 2; $i++) {
561 if (isset($orderBy[
'direction']) === TRUE) {
562 if (strtolower($orderBy[
'direction']) ===
'desc') {
563 $orderBy[
'direction'] =
'';
565 $orderBy[
'direction'] =
'desc';
569 $sql .=
') as limCl '.$this->convertOrderBy($orderBy);
592 if (count($args) != 2 && count($args) != 3) {
593 $msg =
'The SUBSTR() function only accepts two or three parameters.';
599 $length = isset($args[2]) ?
",$args[2]" :
'';
600 $sql =
"SUBSTRING($input, $start $length)";