TYPO3 CMS  TYPO3_7-6
adodb-odbc_mssql.inc.php
Go to the documentation of this file.
1 <?php
2 /*
3 @version v5.20.3 01-Jan-2016
4 @copyright (c) 2000-2013 John Lim (jlim#natsoft.com). All rights reserved.
5 @copyright (c) 2014 Damien Regad, Mark Newnham and the ADOdb community
6  Released under both BSD license and Lesser GPL library license.
7  Whenever there is any discrepancy between the two licenses,
8  the BSD license will take precedence.
9 Set tabs to 4 for best viewing.
10 
11  Latest version is available at http://adodb.sourceforge.net
12 
13  MSSQL support via ODBC. Requires ODBC. Works on Windows and Unix.
14  For Unix configuration, see http://phpbuilder.com/columns/alberto20000919.php3
15 */
16 
17 // security - hide paths
18 if (!defined('ADODB_DIR')) die();
19 
20 if (!defined('_ADODB_ODBC_LAYER')) {
21  include(ADODB_DIR."/drivers/adodb-odbc.inc.php");
22 }
23 
24 
26  var $databaseType = 'odbc_mssql';
27  var $fmtDate = "'Y-m-d'";
28  var $fmtTimeStamp = "'Y-m-d\TH:i:s'";
29  var $_bindInputArray = true;
30  var $metaDatabasesSQL = "select name from sysdatabases where name <> 'master'";
31  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'))";
32  var $metaColumnsSQL = # xtype==61 is datetime
33  "select c.name,t.name,c.length,c.isnullable, c.status,
34  (case when c.xusertype=61 then 0 else c.xprec end),
35  (case when c.xusertype=61 then 0 else c.xscale end)
36  from syscolumns c join systypes t on t.xusertype=c.xusertype join sysobjects o on o.id=c.id where o.name='%s'";
37  var $hasTop = 'top'; // support mssql/interbase SELECT TOP 10 * FROM TABLE
38  var $sysDate = 'GetDate()';
39  var $sysTimeStamp = 'GetDate()';
40  var $leftOuter = '*=';
41  var $rightOuter = '=*';
42  var $substr = 'substring';
43  var $length = 'len';
44  var $ansiOuter = true; // for mssql7 or later
45  var $identitySQL = 'select SCOPE_IDENTITY()'; // 'select SCOPE_IDENTITY'; # for mssql 2000
46  var $hasInsertID = true;
47  var $connectStmt = 'SET CONCAT_NULL_YIELDS_NULL OFF'; # When SET CONCAT_NULL_YIELDS_NULL is ON,
48  # concatenating a null value with a string yields a NULL result
49 
50  function __construct()
51  {
52  parent::__construct();
53  //$this->curmode = SQL_CUR_USE_ODBC;
54  }
55 
56  // crashes php...
57  function ServerInfo()
58  {
59  global $ADODB_FETCH_MODE;
60  $save = $ADODB_FETCH_MODE;
61  $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
62  $row = $this->GetRow("execute sp_server_info 2");
63  $ADODB_FETCH_MODE = $save;
64  if (!is_array($row)) return false;
65  $arr['description'] = $row[2];
66  $arr['version'] = ADOConnection::_findvers($arr['description']);
67  return $arr;
68  }
69 
70  function IfNull( $field, $ifNull )
71  {
72  return " ISNULL($field, $ifNull) "; // if MS SQL Server
73  }
74 
75  function _insertid()
76  {
77  // SCOPE_IDENTITY()
78  // Returns the last IDENTITY value inserted into an IDENTITY column in
79  // the same scope. A scope is a module -- a stored procedure, trigger,
80  // function, or batch. Thus, two statements are in the same scope if
81  // they are in the same stored procedure, function, or batch.
82  return $this->GetOne($this->identitySQL);
83  }
84 
85 
86  function MetaForeignKeys($table, $owner=false, $upper=false)
87  {
88  global $ADODB_FETCH_MODE;
89 
90  $save = $ADODB_FETCH_MODE;
91  $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
92  $table = $this->qstr(strtoupper($table));
93 
94  $sql =
95 "select object_name(constid) as constraint_name,
96  col_name(fkeyid, fkey) as column_name,
97  object_name(rkeyid) as referenced_table_name,
98  col_name(rkeyid, rkey) as referenced_column_name
99 from sysforeignkeys
100 where upper(object_name(fkeyid)) = $table
101 order by constraint_name, referenced_table_name, keyno";
102 
103  $constraints = $this->GetArray($sql);
104 
105  $ADODB_FETCH_MODE = $save;
106 
107  $arr = false;
108  foreach($constraints as $constr) {
109  //print_r($constr);
110  $arr[$constr[0]][$constr[2]][] = $constr[1].'='.$constr[3];
111  }
112  if (!$arr) return false;
113 
114  $arr2 = false;
115 
116  foreach($arr as $k => $v) {
117  foreach($v as $a => $b) {
118  if ($upper) $a = strtoupper($a);
119  $arr2[$a] = $b;
120  }
121  }
122  return $arr2;
123  }
124 
125  function MetaTables($ttype=false,$showSchema=false,$mask=false)
126  {
127  if ($mask) {//$this->debug=1;
128  $save = $this->metaTablesSQL;
129  $mask = $this->qstr($mask);
130  $this->metaTablesSQL .= " AND name like $mask";
131  }
132  $ret = ADOConnection::MetaTables($ttype,$showSchema);
133 
134  if ($mask) {
135  $this->metaTablesSQL = $save;
136  }
137  return $ret;
138  }
139 
140  function MetaColumns($table, $normalize=true)
141  {
142 
143  $this->_findschema($table,$schema);
144  if ($schema) {
145  $dbName = $this->database;
146  $this->SelectDB($schema);
147  }
148  global $ADODB_FETCH_MODE;
149  $save = $ADODB_FETCH_MODE;
150  $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
151 
152  if ($this->fetchMode !== false) $savem = $this->SetFetchMode(false);
153  $rs = $this->Execute(sprintf($this->metaColumnsSQL,$table));
154 
155  if ($schema) {
156  $this->SelectDB($dbName);
157  }
158 
159  if (isset($savem)) $this->SetFetchMode($savem);
160  $ADODB_FETCH_MODE = $save;
161  if (!is_object($rs)) {
162  $false = false;
163  return $false;
164  }
165 
166  $retarr = array();
167  while (!$rs->EOF){
168  $fld = new ADOFieldObject();
169  $fld->name = $rs->fields[0];
170  $fld->type = $rs->fields[1];
171 
172  $fld->not_null = (!$rs->fields[3]);
173  $fld->auto_increment = ($rs->fields[4] == 128); // sys.syscolumns status field. 0x80 = 128 ref: http://msdn.microsoft.com/en-us/library/ms186816.aspx
174 
175 
176  if (isset($rs->fields[5]) && $rs->fields[5]) {
177  if ($rs->fields[5]>0) $fld->max_length = $rs->fields[5];
178  $fld->scale = $rs->fields[6];
179  if ($fld->scale>0) $fld->max_length += 1;
180  } else
181  $fld->max_length = $rs->fields[2];
182 
183 
184  if ($save == ADODB_FETCH_NUM) {
185  $retarr[] = $fld;
186  } else {
187  $retarr[strtoupper($fld->name)] = $fld;
188  }
189  $rs->MoveNext();
190  }
191 
192  $rs->Close();
193  return $retarr;
194 
195  }
196 
197 
198  function MetaIndexes($table,$primary=false, $owner=false)
199  {
200  $table = $this->qstr($table);
201 
202  $sql = "SELECT i.name AS ind_name, C.name AS col_name, USER_NAME(O.uid) AS Owner, c.colid, k.Keyno,
203  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,
204  CASE WHEN I.status & 2 = 2 THEN 1 ELSE 0 END AS IsUnique
205  FROM dbo.sysobjects o INNER JOIN dbo.sysindexes I ON o.id = i.id
206  INNER JOIN dbo.sysindexkeys K ON I.id = K.id AND I.Indid = K.Indid
207  INNER JOIN dbo.syscolumns c ON K.id = C.id AND K.colid = C.Colid
208  WHERE LEFT(i.name, 8) <> '_WA_Sys_' AND o.status >= 0 AND O.Name LIKE $table
209  ORDER BY O.name, I.Name, K.keyno";
210 
211  global $ADODB_FETCH_MODE;
212  $save = $ADODB_FETCH_MODE;
213  $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
214  if ($this->fetchMode !== FALSE) {
215  $savem = $this->SetFetchMode(FALSE);
216  }
217 
218  $rs = $this->Execute($sql);
219  if (isset($savem)) {
220  $this->SetFetchMode($savem);
221  }
222  $ADODB_FETCH_MODE = $save;
223 
224  if (!is_object($rs)) {
225  return FALSE;
226  }
227 
228  $indexes = array();
229  while ($row = $rs->FetchRow()) {
230  if (!$primary && $row[5]) continue;
231 
232  $indexes[$row[0]]['unique'] = $row[6];
233  $indexes[$row[0]]['columns'][] = $row[1];
234  }
235  return $indexes;
236  }
237 
238  function _query($sql,$inputarr=false)
239  {
240  if (is_string($sql)) $sql = str_replace('||','+',$sql);
241  return ADODB_odbc::_query($sql,$inputarr);
242  }
243 
244  function SetTransactionMode( $transaction_mode )
245  {
246  $this->_transmode = $transaction_mode;
247  if (empty($transaction_mode)) {
248  $this->Execute('SET TRANSACTION ISOLATION LEVEL READ COMMITTED');
249  return;
250  }
251  if (!stristr($transaction_mode,'isolation')) $transaction_mode = 'ISOLATION LEVEL '.$transaction_mode;
252  $this->Execute("SET TRANSACTION ".$transaction_mode);
253  }
254 
255  // "Stein-Aksel Basma" <basma@accelero.no>
256  // tested with MSSQL 2000
257  function MetaPrimaryKeys($table, $owner = false)
258  {
259  global $ADODB_FETCH_MODE;
260 
261  $schema = '';
262  $this->_findschema($table,$schema);
263  //if (!$schema) $schema = $this->database;
264  if ($schema) $schema = "and k.table_catalog like '$schema%'";
265 
266  $sql = "select distinct k.column_name,ordinal_position from information_schema.key_column_usage k,
267  information_schema.table_constraints tc
268  where tc.constraint_name = k.constraint_name and tc.constraint_type =
269  'PRIMARY KEY' and k.table_name = '$table' $schema order by ordinal_position ";
270 
271  $savem = $ADODB_FETCH_MODE;
272  $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
273  $a = $this->GetCol($sql);
274  $ADODB_FETCH_MODE = $savem;
275 
276  if ($a && sizeof($a)>0) return $a;
277  $false = false;
278  return $false;
279  }
280 
281  function SelectLimit($sql,$nrows=-1,$offset=-1, $inputarr=false,$secs2cache=0)
282  {
283  if ($nrows > 0 && $offset <= 0) {
284  $sql = preg_replace(
285  '/(^\s*select\s+(distinctrow|distinct)?)/i','\\1 '.$this->hasTop." $nrows ",$sql);
286  $rs = $this->Execute($sql,$inputarr);
287  } else
288  $rs = ADOConnection::SelectLimit($sql,$nrows,$offset,$inputarr,$secs2cache);
289 
290  return $rs;
291  }
292 
293  // Format date column in sql string given an input format that understands Y M D
294  function SQLDate($fmt, $col=false)
295  {
296  if (!$col) $col = $this->sysTimeStamp;
297  $s = '';
298 
299  $len = strlen($fmt);
300  for ($i=0; $i < $len; $i++) {
301  if ($s) $s .= '+';
302  $ch = $fmt[$i];
303  switch($ch) {
304  case 'Y':
305  case 'y':
306  $s .= "datename(yyyy,$col)";
307  break;
308  case 'M':
309  $s .= "convert(char(3),$col,0)";
310  break;
311  case 'm':
312  $s .= "replace(str(month($col),2),' ','0')";
313  break;
314  case 'Q':
315  case 'q':
316  $s .= "datename(quarter,$col)";
317  break;
318  case 'D':
319  case 'd':
320  $s .= "replace(str(day($col),2),' ','0')";
321  break;
322  case 'h':
323  $s .= "substring(convert(char(14),$col,0),13,2)";
324  break;
325 
326  case 'H':
327  $s .= "replace(str(datepart(hh,$col),2),' ','0')";
328  break;
329 
330  case 'i':
331  $s .= "replace(str(datepart(mi,$col),2),' ','0')";
332  break;
333  case 's':
334  $s .= "replace(str(datepart(ss,$col),2),' ','0')";
335  break;
336  case 'a':
337  case 'A':
338  $s .= "substring(convert(char(19),$col,0),18,2)";
339  break;
340 
341  default:
342  if ($ch == '\\') {
343  $i++;
344  $ch = substr($fmt,$i,1);
345  }
346  $s .= $this->qstr($ch);
347  break;
348  }
349  }
350  return $s;
351  }
352 
353 }
354 
356 
357  var $databaseType = 'odbc_mssql';
358 
359  function __construct($id,$mode=false)
360  {
361  return parent::__construct($id,$mode);
362  }
363 }
$database
Definition: server.php:40
if(isset($_REQUEST['nrows'])) else $rs
Definition: server.php:94
MetaIndexes($table, $primary=false, $owner=false)
SQLDate($fmt, $col=false)
IfNull( $field, $ifNull)
MetaForeignKeys($table, $owner=false, $upper=false)
SetTransactionMode( $transaction_mode)
_query($sql, $inputarr=false)
SelectLimit($sql, $nrows=-1, $offset=-1, $inputarr=false, $secs2cache=0)
$sql
Definition: server.php:84
_query($sql, $inputarr=false)
MetaColumns($table, $normalize=true)
MetaTables($ttype=false, $showSchema=false, $mask=false)
MetaPrimaryKeys($table, $owner=false)