3 * Data Dictionary for Microsoft SQL Server (mssql)
5 * This file is part of ADOdb, a Database Abstraction Layer library for PHP.
8 * @link https://adodb.org Project's web site and documentation
9 * @link https://github.com/ADOdb/ADOdb Source code and issue tracker
11 * The ADOdb Library is dual-licensed, released under both the BSD 3-Clause
12 * and the GNU Lesser General Public Licence (LGPL) v2.1 or, at your option,
13 * any later version. This means you can use it in proprietary products.
14 * See the LICENSE.md file distributed with this source code for details.
15 * @license BSD-3-Clause
16 * @license LGPL-2.1-or-later
18 * @copyright 2000-2013 John Lim
19 * @copyright 2014 Damien Regad, Mark Newnham and the ADOdb community
23 In ADOdb, named quotes for MS SQL Server use ". From the MSSQL Docs:
25 Note Delimiters are for identifiers only. Delimiters cannot be used for keywords,
26 whether or not they are marked as reserved in SQL Server.
28 Quoted identifiers are delimited by double quotation marks ("):
29 SELECT * FROM "Blanks in Table Name"
31 Bracketed identifiers are delimited by brackets ([ ]):
32 SELECT * FROM [Blanks In Table Name]
34 Quoted identifiers are valid only when the QUOTED_IDENTIFIER option is set to ON. By default,
35 the Microsoft OLE DB Provider for SQL Server and SQL Server ODBC driver set QUOTED_IDENTIFIER ON
38 In Transact-SQL, the option can be set at various levels using SET QUOTED_IDENTIFIER,
39 the quoted identifier option of sp_dboption, or the user options option of sp_configure.
41 When SET ANSI_DEFAULTS is ON, SET QUOTED_IDENTIFIER is enabled.
45 SET QUOTED_IDENTIFIER { ON | OFF }
50 // security - hide paths
51 if (!defined('ADODB_DIR')) die();
53 class ADODB2_mssql
extends ADODB_DataDict
{
54 var $databaseType = 'mssql';
55 var $dropIndex = 'DROP INDEX %2$s.%1$s';
56 var $renameTable = "EXEC sp_rename '%s','%s'";
57 var $renameColumn = "EXEC sp_rename '%s.%s','%s'";
59 var $typeX = 'TEXT'; ## Alternatively, set it to VARCHAR(4000)
62 //var $alterCol = ' ALTER COLUMN ';
64 function MetaType($t,$len=-1,$fieldobj=false)
69 $len = $fieldobj->max_length
;
72 $len = -1; // mysql max_length is not accurate
73 switch (strtoupper($t)) {
76 case 'INTEGER': return 'I';
78 case 'TINYINT': return 'I1';
79 case 'SMALLINT': return 'I2';
80 case 'BIGINT': return 'I8';
81 case 'SMALLDATETIME': return 'T';
83 case 'FLOAT': return 'F';
84 default: return parent
::MetaType($t,$len,$fieldobj);
88 function ActualType($meta)
90 switch(strtoupper($meta)) {
92 case 'C': return 'VARCHAR';
93 case 'XL': return (isset($this)) ?
$this->typeXL
: 'TEXT';
94 case 'X': return (isset($this)) ?
$this->typeX
: 'TEXT'; ## could be varchar(8000), but we want compat with oracle
95 case 'C2': return 'NVARCHAR';
96 case 'X2': return 'NTEXT';
98 case 'B': return 'IMAGE';
100 case 'D': return 'DATETIME';
103 case 'T': return 'DATETIME';
104 case 'L': return 'BIT';
107 case 'I': return 'INT';
108 case 'I1': return 'TINYINT';
109 case 'I2': return 'SMALLINT';
110 case 'I4': return 'INT';
111 case 'I8': return 'BIGINT';
113 case 'F': return 'REAL';
114 case 'N': return 'NUMERIC';
121 function AddColumnSQL($tabname, $flds)
123 $tabname = $this->TableName ($tabname);
125 list($lines,$pkey) = $this->_GenFields($flds);
126 $s = "ALTER TABLE $tabname $this->addCol";
127 foreach($lines as $v) {
130 $s .= implode(', ',$f);
136 function AlterColumnSQL($tabname, $flds, $tableflds='', $tableoptions='')
138 $tabname = $this->TableName ($tabname);
140 list($lines,$pkey) = $this->_GenFields($flds);
141 foreach($lines as $v) {
142 $sql[] = "ALTER TABLE $tabname $this->alterCol $v";
149 function DropColumnSQL($tabname, $flds, $tableflds='',$tableoptions='')
151 $tabname = $this->TableName ($tabname);
152 if (!is_array($flds))
153 $flds = explode(',',$flds);
155 $s = 'ALTER TABLE ' . $tabname;
156 foreach($flds as $v) {
157 $f[] = "\n$this->dropCol ".$this->NameQuote($v);
159 $s .= implode(', ',$f);
164 // return string must begin with space
165 function _CreateSuffix($fname,&$ftype,$fnotnull,$fdefault,$fautoinc,$fconstraint,$funsigned)
168 if (strlen($fdefault)) $suffix .= " DEFAULT $fdefault";
169 if ($fautoinc) $suffix .= ' IDENTITY(1,1)';
170 if ($fnotnull) $suffix .= ' NOT NULL';
171 else if ($suffix == '') $suffix .= ' NULL';
172 if ($fconstraint) $suffix .= ' '.$fconstraint;
178 [ database_name.[ owner ] . | owner. ] table_name
179 ( { < column_definition >
180 | column_name AS computed_column_expression
181 | < table_constraint > ::= [ CONSTRAINT constraint_name ] }
183 | [ { PRIMARY KEY | UNIQUE } [ ,...n ]
186 [ ON { filegroup | DEFAULT } ]
187 [ TEXTIMAGE_ON { filegroup | DEFAULT } ]
189 < column_definition > ::= { column_name data_type }
190 [ COLLATE < collation_name > ]
191 [ [ DEFAULT constant_expression ]
192 | [ IDENTITY [ ( seed , increment ) [ NOT FOR REPLICATION ] ] ]
195 [ < column_constraint > ] [ ...n ]
197 < column_constraint > ::= [ CONSTRAINT constraint_name ]
198 { [ NULL | NOT NULL ]
199 | [ { PRIMARY KEY | UNIQUE }
200 [ CLUSTERED | NONCLUSTERED ]
201 [ WITH FILLFACTOR = fillfactor ]
202 [ON {filegroup | DEFAULT} ] ]
205 REFERENCES ref_table [ ( ref_column ) ]
206 [ ON DELETE { CASCADE | NO ACTION } ]
207 [ ON UPDATE { CASCADE | NO ACTION } ]
208 [ NOT FOR REPLICATION ]
210 | CHECK [ NOT FOR REPLICATION ]
211 ( logical_expression )
214 < table_constraint > ::= [ CONSTRAINT constraint_name ]
215 { [ { PRIMARY KEY | UNIQUE }
216 [ CLUSTERED | NONCLUSTERED ]
217 { ( column [ ASC | DESC ] [ ,...n ] ) }
218 [ WITH FILLFACTOR = fillfactor ]
219 [ ON { filegroup | DEFAULT } ]
222 [ ( column [ ,...n ] ) ]
223 REFERENCES ref_table [ ( ref_column [ ,...n ] ) ]
224 [ ON DELETE { CASCADE | NO ACTION } ]
225 [ ON UPDATE { CASCADE | NO ACTION } ]
226 [ NOT FOR REPLICATION ]
227 | CHECK [ NOT FOR REPLICATION ]
228 ( search_conditions )
235 CREATE [ UNIQUE ] [ CLUSTERED | NONCLUSTERED ] INDEX index_name
236 ON { table | view } ( column [ ASC | DESC ] [ ,...n ] )
237 [ WITH < index_option > [ ,...n] ]
239 < index_option > :: =
241 FILLFACTOR = fillfactor |
244 STATISTICS_NORECOMPUTE |
248 function _IndexSQL($idxname, $tabname, $flds, $idxoptions)
252 if ( isset($idxoptions['REPLACE']) ||
isset($idxoptions['DROP']) ) {
253 $sql[] = sprintf ($this->dropIndex
, $idxname, $tabname);
254 if ( isset($idxoptions['DROP']) )
258 if ( empty ($flds) ) {
262 $unique = isset($idxoptions['UNIQUE']) ?
' UNIQUE' : '';
263 $clustered = isset($idxoptions['CLUSTERED']) ?
' CLUSTERED' : '';
265 if ( is_array($flds) )
266 $flds = implode(', ',$flds);
267 $s = 'CREATE' . $unique . $clustered . ' INDEX ' . $idxname . ' ON ' . $tabname . ' (' . $flds . ')';
269 if ( isset($idxoptions[$this->upperName
]) )
270 $s .= $idxoptions[$this->upperName
];
279 function _GetSize($ftype, $ty, $fsize, $fprec, $options=false)
288 if ($ty == 'T') return $ftype;
289 return parent
::_GetSize($ftype, $ty, $fsize, $fprec, $options);