3 V4.98 13 Feb 2008 (c) 2000-2008 John Lim (jlim#natsoft.com.my). All rights reserved.
4 Released under both BSD license and Lesser GPL library license.
5 Whenever there is any discrepancy between the two licenses,
6 the BSD license will take precedence.
7 Set tabs to 4 for best viewing.
9 Latest version is available at http://adodb.sourceforge.net
11 MSSQL support via ODBC. Requires ODBC. Works on Windows and Unix.
12 For Unix configuration, see http://phpbuilder.com/columns/alberto20000919.php3
15 // security - hide paths
16 if (!defined('ADODB_DIR')) die();
18 if (!defined('_ADODB_ODBC_LAYER')) {
19 include(ADODB_DIR
."/drivers/adodb-odbc.inc.php");
23 class ADODB_odbc_mssql
extends ADODB_odbc
{
24 var $databaseType = 'odbc_mssql';
25 var $fmtDate = "'Y-m-d'";
26 var $fmtTimeStamp = "'Y-m-d H:i:s'";
27 var $_bindInputArray = true;
28 var $metaTablesSQL="select name,case when type='U' then 'T' else 'V' end from sysobjects where (type='U' or type='V') and (name not in ('sysallocations','syscolumns','syscomments','sysdepends','sysfilegroups','sysfiles','sysfiles1','sysforeignkeys','sysfulltextcatalogs','sysindexes','sysindexkeys','sysmembers','sysobjects','syspermissions','sysprotects','sysreferences','systypes','sysusers','sysalternates','sysconstraints','syssegments','REFERENTIAL_CONSTRAINTS','CHECK_CONSTRAINTS','CONSTRAINT_TABLE_USAGE','CONSTRAINT_COLUMN_USAGE','VIEWS','VIEW_TABLE_USAGE','VIEW_COLUMN_USAGE','SCHEMATA','TABLES','TABLE_CONSTRAINTS','TABLE_PRIVILEGES','COLUMNS','COLUMN_DOMAIN_USAGE','COLUMN_PRIVILEGES','DOMAINS','DOMAIN_CONSTRAINTS','KEY_COLUMN_USAGE'))";
29 var $metaColumnsSQL = "select c.name,t.name,c.length from syscolumns c join systypes t on t.xusertype=c.xusertype join sysobjects o on o.id=c.id where o.name='%s'";
30 var $hasTop = 'top'; // support mssql/interbase SELECT TOP 10 * FROM TABLE
31 var $sysDate = 'GetDate()';
32 var $sysTimeStamp = 'GetDate()';
33 var $leftOuter = '*=';
34 var $rightOuter = '=*';
35 var $substr = 'substring';
37 var $ansiOuter = true; // for mssql7 or later
38 var $identitySQL = 'select SCOPE_IDENTITY()'; // 'select SCOPE_IDENTITY'; # for mssql 2000
39 var $hasInsertID = true;
40 var $connectStmt = 'SET CONCAT_NULL_YIELDS_NULL OFF'; # When SET CONCAT_NULL_YIELDS_NULL is ON,
41 # concatenating a null value with a string yields a NULL result
43 function ADODB_odbc_mssql()
46 //$this->curmode = SQL_CUR_USE_ODBC;
52 global $ADODB_FETCH_MODE;
53 $save = $ADODB_FETCH_MODE;
54 $ADODB_FETCH_MODE = ADODB_FETCH_NUM
;
55 $row = $this->GetRow("execute sp_server_info 2");
56 $ADODB_FETCH_MODE = $save;
57 if (!is_array($row)) return false;
58 $arr['description'] = $row[2];
59 $arr['version'] = ADOConnection
::_findvers($arr['description']);
63 function IfNull( $field, $ifNull )
65 return " ISNULL($field, $ifNull) "; // if MS SQL Server
71 // Returns the last IDENTITY value inserted into an IDENTITY column in
72 // the same scope. A scope is a module -- a stored procedure, trigger,
73 // function, or batch. Thus, two statements are in the same scope if
74 // they are in the same stored procedure, function, or batch.
75 return $this->GetOne($this->identitySQL
);
79 function MetaForeignKeys($table, $owner=false, $upper=false)
81 global $ADODB_FETCH_MODE;
83 $save = $ADODB_FETCH_MODE;
84 $ADODB_FETCH_MODE = ADODB_FETCH_NUM
;
85 $table = $this->qstr(strtoupper($table));
88 "select object_name(constid) as constraint_name,
89 col_name(fkeyid, fkey) as column_name,
90 object_name(rkeyid) as referenced_table_name,
91 col_name(rkeyid, rkey) as referenced_column_name
93 where upper(object_name(fkeyid)) = $table
94 order by constraint_name, referenced_table_name, keyno";
96 $constraints =& $this->GetArray($sql);
98 $ADODB_FETCH_MODE = $save;
101 foreach($constraints as $constr) {
103 $arr[$constr[0]][$constr[2]][] = $constr[1].'='.$constr[3];
105 if (!$arr) return false;
109 foreach($arr as $k => $v) {
110 foreach($v as $a => $b) {
111 if ($upper) $a = strtoupper($a);
118 function &MetaTables($ttype=false,$showSchema=false,$mask=false)
120 if ($mask) {$this->debug
=1;
121 $save = $this->metaTablesSQL
;
122 $mask = $this->qstr($mask);
123 $this->metaTablesSQL
.= " AND name like $mask";
125 $ret =& ADOConnection
::MetaTables($ttype,$showSchema);
128 $this->metaTablesSQL
= $save;
133 function &MetaColumns($table)
135 $arr = ADOConnection
::MetaColumns($table);
140 function &MetaIndexes($table,$primary=false)
142 $table = $this->qstr($table);
144 $sql = "SELECT i.name AS ind_name, C.name AS col_name, USER_NAME(O.uid) AS Owner, c.colid, k.Keyno,
145 CASE WHEN I.indid BETWEEN 1 AND 254 AND (I.status & 2048 = 2048 OR I.Status = 16402 AND O.XType = 'V') THEN 1 ELSE 0 END AS IsPK,
146 CASE WHEN I.status & 2 = 2 THEN 1 ELSE 0 END AS IsUnique
147 FROM dbo.sysobjects o INNER JOIN dbo.sysindexes I ON o.id = i.id
148 INNER JOIN dbo.sysindexkeys K ON I.id = K.id AND I.Indid = K.Indid
149 INNER JOIN dbo.syscolumns c ON K.id = C.id AND K.colid = C.Colid
150 WHERE LEFT(i.name, 8) <> '_WA_Sys_' AND o.status >= 0 AND O.Name LIKE $table
151 ORDER BY O.name, I.Name, K.keyno";
153 global $ADODB_FETCH_MODE;
154 $save = $ADODB_FETCH_MODE;
155 $ADODB_FETCH_MODE = ADODB_FETCH_NUM
;
156 if ($this->fetchMode
!== FALSE) {
157 $savem = $this->SetFetchMode(FALSE);
160 $rs = $this->Execute($sql);
162 $this->SetFetchMode($savem);
164 $ADODB_FETCH_MODE = $save;
166 if (!is_object($rs)) {
171 while ($row = $rs->FetchRow()) {
172 if (!$primary && $row[5]) continue;
174 $indexes[$row[0]]['unique'] = $row[6];
175 $indexes[$row[0]]['columns'][] = $row[1];
180 function _query($sql,$inputarr)
182 if (is_string($sql)) $sql = str_replace('||','+',$sql);
183 return ADODB_odbc
::_query($sql,$inputarr);
186 function SetTransactionMode( $transaction_mode )
188 $this->_transmode
= $transaction_mode;
189 if (empty($transaction_mode)) {
190 $this->Execute('SET TRANSACTION ISOLATION LEVEL READ COMMITTED');
193 if (!stristr($transaction_mode,'isolation')) $transaction_mode = 'ISOLATION LEVEL '.$transaction_mode;
194 $this->Execute("SET TRANSACTION ".$transaction_mode);
197 // "Stein-Aksel Basma" <basma@accelero.no>
198 // tested with MSSQL 2000
199 function &MetaPrimaryKeys($table)
201 global $ADODB_FETCH_MODE;
204 $this->_findschema($table,$schema);
205 //if (!$schema) $schema = $this->database;
206 if ($schema) $schema = "and k.table_catalog like '$schema%'";
208 $sql = "select distinct k.column_name,ordinal_position from information_schema.key_column_usage k,
209 information_schema.table_constraints tc
210 where tc.constraint_name = k.constraint_name and tc.constraint_type =
211 'PRIMARY KEY' and k.table_name = '$table' $schema order by ordinal_position ";
213 $savem = $ADODB_FETCH_MODE;
214 $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC
;
215 $a = $this->GetCol($sql);
216 $ADODB_FETCH_MODE = $savem;
218 if ($a && sizeof($a)>0) return $a;
223 function &SelectLimit($sql,$nrows=-1,$offset=-1, $inputarr=false,$secs2cache=0)
225 if ($nrows > 0 && $offset <= 0) {
227 '/(^\s*select\s+(distinctrow|distinct)?)/i','\\1 '.$this->hasTop
." $nrows ",$sql);
228 $rs =& $this->Execute($sql,$inputarr);
230 $rs =& ADOConnection
::SelectLimit($sql,$nrows,$offset,$inputarr,$secs2cache);
235 // Format date column in sql string given an input format that understands Y M D
236 function SQLDate($fmt, $col=false)
238 if (!$col) $col = $this->sysTimeStamp
;
242 for ($i=0; $i < $len; $i++
) {
248 $s .= "datename(yyyy,$col)";
251 $s .= "convert(char(3),$col,0)";
254 $s .= "replace(str(month($col),2),' ','0')";
258 $s .= "datename(quarter,$col)";
262 $s .= "replace(str(day($col),2),' ','0')";
265 $s .= "substring(convert(char(14),$col,0),13,2)";
269 $s .= "replace(str(datepart(hh,$col),2),' ','0')";
273 $s .= "replace(str(datepart(mi,$col),2),' ','0')";
276 $s .= "replace(str(datepart(ss,$col),2),' ','0')";
280 $s .= "substring(convert(char(19),$col,0),18,2)";
286 $ch = substr($fmt,$i,1);
288 $s .= $this->qstr($ch);
297 class ADORecordSet_odbc_mssql
extends ADORecordSet_odbc
{
299 var $databaseType = 'odbc_mssql';
301 function ADORecordSet_odbc_mssql($id,$mode=false)
303 return $this->ADORecordSet_odbc($id,$mode);