4 V4.94 23 Jan 2007 (c) 2000-2007 John Lim (jlim#natsoft.com.my). All rights reserved.
5 Released under both BSD license and Lesser GPL library license.
6 Whenever there is any discrepancy between the two licenses,
7 the BSD license will take precedence.
9 Set tabs to 4 for best viewing.
14 In ADOdb, named quotes for MS SQL Server use ". From the MSSQL Docs:
16 Note Delimiters are for identifiers only. Delimiters cannot be used for keywords,
17 whether or not they are marked as reserved in SQL Server.
19 Quoted identifiers are delimited by double quotation marks ("):
20 SELECT * FROM "Blanks in Table Name"
22 Bracketed identifiers are delimited by brackets ([ ]):
23 SELECT * FROM [Blanks In Table Name]
25 Quoted identifiers are valid only when the QUOTED_IDENTIFIER option is set to ON. By default,
26 the Microsoft OLE DB Provider for SQL Server and SQL Server ODBC driver set QUOTED_IDENTIFIER ON
29 In Transact-SQL, the option can be set at various levels using SET QUOTED_IDENTIFIER,
30 the quoted identifier option of sp_dboption, or the user options option of sp_configure.
32 When SET ANSI_DEFAULTS is ON, SET QUOTED_IDENTIFIER is enabled.
36 SET QUOTED_IDENTIFIER { ON | OFF }
41 // security - hide paths
42 if (!defined('ADODB_DIR')) die();
44 class ADODB2_mssql
extends ADODB_DataDict
{
45 var $databaseType = 'mssql';
46 var $dropIndex = 'DROP INDEX %2$s.%1$s';
47 var $renameTable = "EXEC sp_rename '%s','%s'";
48 var $renameColumn = "EXEC sp_rename '%s.%s','%s'";
50 var $typeX = 'TEXT'; ## Alternatively, set it to VARCHAR(4000)
53 //var $alterCol = ' ALTER COLUMN ';
55 function MetaType($t,$len=-1,$fieldobj=false)
60 $len = $fieldobj->max_length
;
63 $len = -1; // mysql max_length is not accurate
64 switch (strtoupper($t)) {
67 case 'INTEGER': return 'I';
69 case 'TINYINT': return 'I1';
70 case 'SMALLINT': return 'I2';
71 case 'BIGINT': return 'I8';
74 case 'FLOAT': return 'F';
75 default: return parent
::MetaType($t,$len,$fieldobj);
79 function ActualType($meta)
81 switch(strtoupper($meta)) {
83 case 'C': return 'VARCHAR';
84 case 'XL': return (isset($this)) ?
$this->typeXL
: 'TEXT';
85 case 'X': return (isset($this)) ?
$this->typeX
: 'TEXT'; ## could be varchar(8000), but we want compat with oracle
86 case 'C2': return 'NVARCHAR';
87 case 'X2': return 'NTEXT';
89 case 'B': return 'IMAGE';
91 case 'D': return 'DATETIME';
92 case 'T': return 'DATETIME';
93 case 'L': return 'BIT';
96 case 'I': return 'INT';
97 case 'I1': return 'TINYINT';
98 case 'I2': return 'SMALLINT';
99 case 'I4': return 'INT';
100 case 'I8': return 'BIGINT';
102 case 'F': return 'REAL';
103 case 'N': return 'NUMERIC';
110 function AddColumnSQL($tabname, $flds)
112 $tabname = $this->TableName ($tabname);
114 list($lines,$pkey) = $this->_GenFields($flds);
115 $s = "ALTER TABLE $tabname $this->addCol";
116 foreach($lines as $v) {
119 $s .= implode(', ',$f);
125 function AlterColumnSQL($tabname, $flds)
127 $tabname = $this->TableName ($tabname);
129 list($lines,$pkey) = $this->_GenFields($flds);
130 foreach($lines as $v) {
131 $sql[] = "ALTER TABLE $tabname $this->alterCol $v";
138 function DropColumnSQL($tabname, $flds)
140 $tabname = $this->TableName ($tabname);
141 if (!is_array($flds))
142 $flds = explode(',',$flds);
144 $s = 'ALTER TABLE ' . $tabname;
145 foreach($flds as $v) {
146 $f[] = "\n$this->dropCol ".$this->NameQuote($v);
148 $s .= implode(', ',$f);
153 // return string must begin with space
154 function _CreateSuffix($fname,$ftype,$fnotnull,$fdefault,$fautoinc,$fconstraint)
157 if (strlen($fdefault)) $suffix .= " DEFAULT $fdefault";
158 if ($fautoinc) $suffix .= ' IDENTITY(1,1)';
159 if ($fnotnull) $suffix .= ' NOT NULL';
160 else if ($suffix == '') $suffix .= ' NULL';
161 if ($fconstraint) $suffix .= ' '.$fconstraint;
167 [ database_name.[ owner ] . | owner. ] table_name
168 ( { < column_definition >
169 | column_name AS computed_column_expression
170 | < table_constraint > ::= [ CONSTRAINT constraint_name ] }
172 | [ { PRIMARY KEY | UNIQUE } [ ,...n ]
175 [ ON { filegroup | DEFAULT } ]
176 [ TEXTIMAGE_ON { filegroup | DEFAULT } ]
178 < column_definition > ::= { column_name data_type }
179 [ COLLATE < collation_name > ]
180 [ [ DEFAULT constant_expression ]
181 | [ IDENTITY [ ( seed , increment ) [ NOT FOR REPLICATION ] ] ]
184 [ < column_constraint > ] [ ...n ]
186 < column_constraint > ::= [ CONSTRAINT constraint_name ]
187 { [ NULL | NOT NULL ]
188 | [ { PRIMARY KEY | UNIQUE }
189 [ CLUSTERED | NONCLUSTERED ]
190 [ WITH FILLFACTOR = fillfactor ]
191 [ON {filegroup | DEFAULT} ] ]
194 REFERENCES ref_table [ ( ref_column ) ]
195 [ ON DELETE { CASCADE | NO ACTION } ]
196 [ ON UPDATE { CASCADE | NO ACTION } ]
197 [ NOT FOR REPLICATION ]
199 | CHECK [ NOT FOR REPLICATION ]
200 ( logical_expression )
203 < table_constraint > ::= [ CONSTRAINT constraint_name ]
204 { [ { PRIMARY KEY | UNIQUE }
205 [ CLUSTERED | NONCLUSTERED ]
206 { ( column [ ASC | DESC ] [ ,...n ] ) }
207 [ WITH FILLFACTOR = fillfactor ]
208 [ ON { filegroup | DEFAULT } ]
211 [ ( column [ ,...n ] ) ]
212 REFERENCES ref_table [ ( ref_column [ ,...n ] ) ]
213 [ ON DELETE { CASCADE | NO ACTION } ]
214 [ ON UPDATE { CASCADE | NO ACTION } ]
215 [ NOT FOR REPLICATION ]
216 | CHECK [ NOT FOR REPLICATION ]
217 ( search_conditions )
224 CREATE [ UNIQUE ] [ CLUSTERED | NONCLUSTERED ] INDEX index_name
225 ON { table | view } ( column [ ASC | DESC ] [ ,...n ] )
226 [ WITH < index_option > [ ,...n] ]
228 < index_option > :: =
230 FILLFACTOR = fillfactor |
233 STATISTICS_NORECOMPUTE |
237 function _IndexSQL($idxname, $tabname, $flds, $idxoptions)
241 if ( isset($idxoptions['REPLACE']) ||
isset($idxoptions['DROP']) ) {
242 $sql[] = sprintf ($this->dropIndex
, $idxname, $tabname);
243 if ( isset($idxoptions['DROP']) )
247 if ( empty ($flds) ) {
251 $unique = isset($idxoptions['UNIQUE']) ?
' UNIQUE' : '';
252 $clustered = isset($idxoptions['CLUSTERED']) ?
' CLUSTERED' : '';
254 if ( is_array($flds) )
255 $flds = implode(', ',$flds);
256 $s = 'CREATE' . $unique . $clustered . ' INDEX ' . $idxname . ' ON ' . $tabname . ' (' . $flds . ')';
258 if ( isset($idxoptions[$this->upperName
]) )
259 $s .= $idxoptions[$this->upperName
];
268 function _GetSize($ftype, $ty, $fsize, $fprec)
277 if ($ty == 'T') return $ftype;
278 return parent
::_GetSize($ftype, $ty, $fsize, $fprec);