Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / lib / adodb / drivers / adodb-odbc_mssql.inc.php
blobce36d2e394db7554fbd3390f705e774e5569a526
1 <?php
2 /*
3 V4.94 23 Jan 2007 (c) 2000-2007 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';
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
43 function ADODB_odbc_mssql()
45 $this->ADODB_odbc();
46 //$this->curmode = SQL_CUR_USE_ODBC;
49 // crashes php...
50 //Note replaced by some different code to allow it to work properly
51 //for Moode environmental checks
52 function ServerInfo()
54 $row = $this->GetRow("execute sp_server_info 2");
56 $arr['description'] = $row['attribute_value'];
57 $arr['version'] = ADOConnection::_findvers($arr['description']);
58 return $arr;
61 function IfNull( $field, $ifNull )
63 return " ISNULL($field, $ifNull) "; // if MS SQL Server
66 function _insertid()
68 // SCOPE_IDENTITY()
69 // Returns the last IDENTITY value inserted into an IDENTITY column in
70 // the same scope. A scope is a module -- a stored procedure, trigger,
71 // function, or batch. Thus, two statements are in the same scope if
72 // they are in the same stored procedure, function, or batch.
73 return $this->GetOne($this->identitySQL);
77 function MetaForeignKeys($table, $owner=false, $upper=false)
79 global $ADODB_FETCH_MODE;
81 $save = $ADODB_FETCH_MODE;
82 $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
83 $table = $this->qstr(strtoupper($table));
85 $sql =
86 "select object_name(constid) as constraint_name,
87 col_name(fkeyid, fkey) as column_name,
88 object_name(rkeyid) as referenced_table_name,
89 col_name(rkeyid, rkey) as referenced_column_name
90 from sysforeignkeys
91 where upper(object_name(fkeyid)) = $table
92 order by constraint_name, referenced_table_name, keyno";
94 $constraints =& $this->GetArray($sql);
96 $ADODB_FETCH_MODE = $save;
98 $arr = false;
99 foreach($constraints as $constr) {
100 //print_r($constr);
101 $arr[$constr[0]][$constr[2]][] = $constr[1].'='.$constr[3];
103 if (!$arr) return false;
105 $arr2 = false;
107 foreach($arr as $k => $v) {
108 foreach($v as $a => $b) {
109 if ($upper) $a = strtoupper($a);
110 $arr2[$a] = $b;
113 return $arr2;
116 function &MetaTables($ttype=false,$showSchema=false,$mask=false)
118 if ($mask) {$this->debug=1;
119 $save = $this->metaTablesSQL;
120 $mask = $this->qstr($mask);
121 $this->metaTablesSQL .= " AND name like $mask";
123 $ret =& ADOConnection::MetaTables($ttype,$showSchema);
125 if ($mask) {
126 $this->metaTablesSQL = $save;
128 return $ret;
131 function &MetaColumns($table)
133 $arr = ADOConnection::MetaColumns($table);
134 return $arr;
138 function &MetaIndexes($table,$primary=false)
140 $table = $this->qstr($table);
142 $sql = "SELECT i.name AS ind_name, C.name AS col_name, USER_NAME(O.uid) AS Owner, c.colid, k.Keyno,
143 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,
144 CASE WHEN I.status & 2 = 2 THEN 1 ELSE 0 END AS IsUnique
145 FROM dbo.sysobjects o INNER JOIN dbo.sysindexes I ON o.id = i.id
146 INNER JOIN dbo.sysindexkeys K ON I.id = K.id AND I.Indid = K.Indid
147 INNER JOIN dbo.syscolumns c ON K.id = C.id AND K.colid = C.Colid
148 WHERE LEFT(i.name, 8) <> '_WA_Sys_' AND o.status >= 0 AND O.Name LIKE $table
149 ORDER BY O.name, I.Name, K.keyno";
151 global $ADODB_FETCH_MODE;
152 $save = $ADODB_FETCH_MODE;
153 $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
154 if ($this->fetchMode !== FALSE) {
155 $savem = $this->SetFetchMode(FALSE);
158 $rs = $this->Execute($sql);
159 if (isset($savem)) {
160 $this->SetFetchMode($savem);
162 $ADODB_FETCH_MODE = $save;
164 if (!is_object($rs)) {
165 return FALSE;
168 $indexes = array();
169 while ($row = $rs->FetchRow()) {
170 if (!$primary && $row[5]) continue;
172 $indexes[$row[0]]['unique'] = $row[6];
173 $indexes[$row[0]]['columns'][] = $row[1];
175 return $indexes;
178 function _query($sql,$inputarr)
180 if (is_string($sql)) $sql = str_replace('||','+',$sql);
181 return ADODB_odbc::_query($sql,$inputarr);
184 function SetTransactionMode( $transaction_mode )
186 $this->_transmode = $transaction_mode;
187 if (empty($transaction_mode)) {
188 $this->Execute('SET TRANSACTION ISOLATION LEVEL READ COMMITTED');
189 return;
191 if (!stristr($transaction_mode,'isolation')) $transaction_mode = 'ISOLATION LEVEL '.$transaction_mode;
192 $this->Execute("SET TRANSACTION ".$transaction_mode);
195 // "Stein-Aksel Basma" <basma@accelero.no>
196 // tested with MSSQL 2000
197 function &MetaPrimaryKeys($table)
199 global $ADODB_FETCH_MODE;
201 $schema = '';
202 $this->_findschema($table,$schema);
203 //if (!$schema) $schema = $this->database;
204 if ($schema) $schema = "and k.table_catalog like '$schema%'";
206 $sql = "select distinct k.column_name,ordinal_position from information_schema.key_column_usage k,
207 information_schema.table_constraints tc
208 where tc.constraint_name = k.constraint_name and tc.constraint_type =
209 'PRIMARY KEY' and k.table_name = '$table' $schema order by ordinal_position ";
211 $savem = $ADODB_FETCH_MODE;
212 $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
213 $a = $this->GetCol($sql);
214 $ADODB_FETCH_MODE = $savem;
216 if ($a && sizeof($a)>0) return $a;
217 $false = false;
218 return $false;
221 function &SelectLimit($sql,$nrows=-1,$offset=-1, $inputarr=false,$secs2cache=0)
223 if ($nrows > 0 && $offset <= 0) {
224 $sql = preg_replace(
225 '/(^\s*select\s+(distinctrow|distinct)?)/i','\\1 '.$this->hasTop." $nrows ",$sql);
226 $rs =& $this->Execute($sql,$inputarr);
227 } else
228 $rs =& ADOConnection::SelectLimit($sql,$nrows,$offset,$inputarr,$secs2cache);
230 return $rs;
233 // Format date column in sql string given an input format that understands Y M D
234 function SQLDate($fmt, $col=false)
236 if (!$col) $col = $this->sysTimeStamp;
237 $s = '';
239 $len = strlen($fmt);
240 for ($i=0; $i < $len; $i++) {
241 if ($s) $s .= '+';
242 $ch = $fmt[$i];
243 switch($ch) {
244 case 'Y':
245 case 'y':
246 $s .= "datename(yyyy,$col)";
247 break;
248 case 'M':
249 $s .= "convert(char(3),$col,0)";
250 break;
251 case 'm':
252 $s .= "replace(str(month($col),2),' ','0')";
253 break;
254 case 'Q':
255 case 'q':
256 $s .= "datename(quarter,$col)";
257 break;
258 case 'D':
259 case 'd':
260 $s .= "replace(str(day($col),2),' ','0')";
261 break;
262 case 'h':
263 $s .= "substring(convert(char(14),$col,0),13,2)";
264 break;
266 case 'H':
267 $s .= "replace(str(datepart(hh,$col),2),' ','0')";
268 break;
270 case 'i':
271 $s .= "replace(str(datepart(mi,$col),2),' ','0')";
272 break;
273 case 's':
274 $s .= "replace(str(datepart(ss,$col),2),' ','0')";
275 break;
276 case 'a':
277 case 'A':
278 $s .= "substring(convert(char(19),$col,0),18,2)";
279 break;
281 default:
282 if ($ch == '\\') {
283 $i++;
284 $ch = substr($fmt,$i,1);
286 $s .= $this->qstr($ch);
287 break;
290 return $s;
295 class ADORecordSet_odbc_mssql extends ADORecordSet_odbc {
297 var $databaseType = 'odbc_mssql';
299 function ADORecordSet_odbc_mssql($id,$mode=false)
301 return $this->ADORecordSet_odbc($id,$mode);