ChangeLog
[Packages/TYPO3.CMS.git] / typo3 / sysext / adodb / adodb / drivers / adodb-odbc_mssql.inc.php
1 <?php
2 /*
3 V4.90 8 June 2006 (c) 2000-2006 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.
8
9 Latest version is available at http://adodb.sourceforge.net
10
11 MSSQL support via ODBC. Requires ODBC. Works on Windows and Unix.
12 For Unix configuration, see http://phpbuilder.com/columns/alberto20000919.php3
13 */
14
15 // security - hide paths
16 if (!defined('ADODB_DIR')) die();
17
18 if (!defined('_ADODB_ODBC_LAYER')) {
19 include(ADODB_DIR."/drivers/adodb-odbc.inc.php");
20 }
21
22
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';
36 var $length = 'len';
37 var $ansiOuter = true; // for mssql7 or later
38 var $identitySQL = 'select @@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
42
43 function ADODB_odbc_mssql()
44 {
45 $this->ADODB_odbc();
46 //$this->curmode = SQL_CUR_USE_ODBC;
47 }
48
49 // crashes php...
50 function ServerInfo()
51 {
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']);
60 return $arr;
61 }
62
63 function IfNull( $field, $ifNull )
64 {
65 return " ISNULL($field, $ifNull) "; // if MS SQL Server
66 }
67
68 function _insertid()
69 {
70 // SCOPE_IDENTITY()
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);
76 }
77
78
79 function MetaForeignKeys($table, $owner=false, $upper=false)
80 {
81 global $ADODB_FETCH_MODE;
82
83 $save = $ADODB_FETCH_MODE;
84 $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
85 $table = $this->qstr(strtoupper($table));
86
87 $sql =
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
92 from sysforeignkeys
93 where upper(object_name(fkeyid)) = $table
94 order by constraint_name, referenced_table_name, keyno";
95
96 $constraints =& $this->GetArray($sql);
97
98 $ADODB_FETCH_MODE = $save;
99
100 $arr = false;
101 foreach($constraints as $constr) {
102 //print_r($constr);
103 $arr[$constr[0]][$constr[2]][] = $constr[1].'='.$constr[3];
104 }
105 if (!$arr) return false;
106
107 $arr2 = false;
108
109 foreach($arr as $k => $v) {
110 foreach($v as $a => $b) {
111 if ($upper) $a = strtoupper($a);
112 $arr2[$a] = $b;
113 }
114 }
115 return $arr2;
116 }
117
118 function &MetaTables($ttype=false,$showSchema=false,$mask=false)
119 {
120 if ($mask) {$this->debug=1;
121 $save = $this->metaTablesSQL;
122 $mask = $this->qstr($mask);
123 $this->metaTablesSQL .= " AND name like $mask";
124 }
125 $ret =& ADOConnection::MetaTables($ttype,$showSchema);
126
127 if ($mask) {
128 $this->metaTablesSQL = $save;
129 }
130 return $ret;
131 }
132
133 function &MetaColumns($table)
134 {
135 $arr = ADOConnection::MetaColumns($table);
136 return $arr;
137 }
138
139 function _query($sql,$inputarr)
140 {
141 if (is_string($sql)) $sql = str_replace('||','+',$sql);
142 return ADODB_odbc::_query($sql,$inputarr);
143 }
144
145 function SetTransactionMode( $transaction_mode )
146 {
147 $this->_transmode = $transaction_mode;
148 if (empty($transaction_mode)) {
149 $this->Execute('SET TRANSACTION ISOLATION LEVEL READ COMMITTED');
150 return;
151 }
152 if (!stristr($transaction_mode,'isolation')) $transaction_mode = 'ISOLATION LEVEL '.$transaction_mode;
153 $this->Execute("SET TRANSACTION ".$transaction_mode);
154 }
155
156 // "Stein-Aksel Basma" <basma@accelero.no>
157 // tested with MSSQL 2000
158 function &MetaPrimaryKeys($table)
159 {
160 global $ADODB_FETCH_MODE;
161
162 $schema = '';
163 $this->_findschema($table,$schema);
164 //if (!$schema) $schema = $this->database;
165 if ($schema) $schema = "and k.table_catalog like '$schema%'";
166
167 $sql = "select distinct k.column_name,ordinal_position from information_schema.key_column_usage k,
168 information_schema.table_constraints tc
169 where tc.constraint_name = k.constraint_name and tc.constraint_type =
170 'PRIMARY KEY' and k.table_name = '$table' $schema order by ordinal_position ";
171
172 $savem = $ADODB_FETCH_MODE;
173 $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
174 $a = $this->GetCol($sql);
175 $ADODB_FETCH_MODE = $savem;
176
177 if ($a && sizeof($a)>0) return $a;
178 $false = false;
179 return $false;
180 }
181
182 function &SelectLimit($sql,$nrows=-1,$offset=-1, $inputarr=false,$secs2cache=0)
183 {
184 if ($nrows > 0 && $offset <= 0) {
185 $sql = preg_replace(
186 '/(^\s*select\s+(distinctrow|distinct)?)/i','\\1 '.$this->hasTop." $nrows ",$sql);
187 $rs =& $this->Execute($sql,$inputarr);
188 } else
189 $rs =& ADOConnection::SelectLimit($sql,$nrows,$offset,$inputarr,$secs2cache);
190
191 return $rs;
192 }
193
194 // Format date column in sql string given an input format that understands Y M D
195 function SQLDate($fmt, $col=false)
196 {
197 if (!$col) $col = $this->sysTimeStamp;
198 $s = '';
199
200 $len = strlen($fmt);
201 for ($i=0; $i < $len; $i++) {
202 if ($s) $s .= '+';
203 $ch = $fmt[$i];
204 switch($ch) {
205 case 'Y':
206 case 'y':
207 $s .= "datename(yyyy,$col)";
208 break;
209 case 'M':
210 $s .= "convert(char(3),$col,0)";
211 break;
212 case 'm':
213 $s .= "replace(str(month($col),2),' ','0')";
214 break;
215 case 'Q':
216 case 'q':
217 $s .= "datename(quarter,$col)";
218 break;
219 case 'D':
220 case 'd':
221 $s .= "replace(str(day($col),2),' ','0')";
222 break;
223 case 'h':
224 $s .= "substring(convert(char(14),$col,0),13,2)";
225 break;
226
227 case 'H':
228 $s .= "replace(str(datepart(hh,$col),2),' ','0')";
229 break;
230
231 case 'i':
232 $s .= "replace(str(datepart(mi,$col),2),' ','0')";
233 break;
234 case 's':
235 $s .= "replace(str(datepart(ss,$col),2),' ','0')";
236 break;
237 case 'a':
238 case 'A':
239 $s .= "substring(convert(char(19),$col,0),18,2)";
240 break;
241
242 default:
243 if ($ch == '\\') {
244 $i++;
245 $ch = substr($fmt,$i,1);
246 }
247 $s .= $this->qstr($ch);
248 break;
249 }
250 }
251 return $s;
252 }
253
254 }
255
256 class ADORecordSet_odbc_mssql extends ADORecordSet_odbc {
257
258 var $databaseType = 'odbc_mssql';
259
260 function ADORecordSet_odbc_mssql($id,$mode=false)
261 {
262 return $this->ADORecordSet_odbc($id,$mode);
263 }
264 }
265 ?>