Differences Between: [Versions 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]
1 <?php 2 /* 3 @version v5.21.0 2021-02-27 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 https://adodb.org/ 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_once(ADODB_DIR."/drivers/adodb-odbc.inc.php"); 22 } 23 24 25 class ADODB_odbc_mssql extends ADODB_odbc { 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 // crashes php... 51 function ServerInfo() 52 { 53 global $ADODB_FETCH_MODE; 54 $save = $ADODB_FETCH_MODE; 55 $ADODB_FETCH_MODE = ADODB_FETCH_NUM; 56 $row = $this->GetRow("execute sp_server_info 2"); 57 $ADODB_FETCH_MODE = $save; 58 if (!is_array($row)) return false; 59 $arr['description'] = $row[2]; 60 $arr['version'] = ADOConnection::_findvers($arr['description']); 61 return $arr; 62 } 63 64 function IfNull( $field, $ifNull ) 65 { 66 return " ISNULL($field, $ifNull) "; // if MS SQL Server 67 } 68 69 function _insertid() 70 { 71 // SCOPE_IDENTITY() 72 // Returns the last IDENTITY value inserted into an IDENTITY column in 73 // the same scope. A scope is a module -- a stored procedure, trigger, 74 // function, or batch. Thus, two statements are in the same scope if 75 // they are in the same stored procedure, function, or batch. 76 return $this->GetOne($this->identitySQL); 77 } 78 79 80 function MetaForeignKeys($table, $owner=false, $upper=false) 81 { 82 global $ADODB_FETCH_MODE; 83 84 $save = $ADODB_FETCH_MODE; 85 $ADODB_FETCH_MODE = ADODB_FETCH_NUM; 86 $table = $this->qstr(strtoupper($table)); 87 88 $sql = 89 "select object_name(constid) as constraint_name, 90 col_name(fkeyid, fkey) as column_name, 91 object_name(rkeyid) as referenced_table_name, 92 col_name(rkeyid, rkey) as referenced_column_name 93 from sysforeignkeys 94 where upper(object_name(fkeyid)) = $table 95 order by constraint_name, referenced_table_name, keyno"; 96 97 $constraints = $this->GetArray($sql); 98 99 $ADODB_FETCH_MODE = $save; 100 101 $arr = false; 102 foreach($constraints as $constr) { 103 //print_r($constr); 104 $arr[$constr[0]][$constr[2]][] = $constr[1].'='.$constr[3]; 105 } 106 if (!$arr) return false; 107 108 $arr2 = false; 109 110 foreach($arr as $k => $v) { 111 foreach($v as $a => $b) { 112 if ($upper) $a = strtoupper($a); 113 $arr2[$a] = $b; 114 } 115 } 116 return $arr2; 117 } 118 119 function MetaTables($ttype=false,$showSchema=false,$mask=false) 120 { 121 if ($mask) {//$this->debug=1; 122 $save = $this->metaTablesSQL; 123 $mask = $this->qstr($mask); 124 $this->metaTablesSQL .= " AND name like $mask"; 125 } 126 $ret = ADOConnection::MetaTables($ttype,$showSchema); 127 128 if ($mask) { 129 $this->metaTablesSQL = $save; 130 } 131 return $ret; 132 } 133 134 function MetaColumns($table, $normalize=true) 135 { 136 137 $this->_findschema($table,$schema); 138 if ($schema) { 139 $dbName = $this->database; 140 $this->SelectDB($schema); 141 } 142 global $ADODB_FETCH_MODE; 143 $save = $ADODB_FETCH_MODE; 144 $ADODB_FETCH_MODE = ADODB_FETCH_NUM; 145 146 if ($this->fetchMode !== false) $savem = $this->SetFetchMode(false); 147 $rs = $this->Execute(sprintf($this->metaColumnsSQL,$table)); 148 149 if ($schema) { 150 $this->SelectDB($dbName); 151 } 152 153 if (isset($savem)) $this->SetFetchMode($savem); 154 $ADODB_FETCH_MODE = $save; 155 if (!is_object($rs)) { 156 $false = false; 157 return $false; 158 } 159 160 $retarr = array(); 161 while (!$rs->EOF){ 162 $fld = new ADOFieldObject(); 163 $fld->name = $rs->fields[0]; 164 $fld->type = $rs->fields[1]; 165 166 $fld->not_null = (!$rs->fields[3]); 167 $fld->auto_increment = ($rs->fields[4] == 128); // sys.syscolumns status field. 0x80 = 128 ref: http://msdn.microsoft.com/en-us/library/ms186816.aspx 168 169 170 if (isset($rs->fields[5]) && $rs->fields[5]) { 171 if ($rs->fields[5]>0) $fld->max_length = $rs->fields[5]; 172 $fld->scale = $rs->fields[6]; 173 if ($fld->scale>0) $fld->max_length += 1; 174 } else 175 $fld->max_length = $rs->fields[2]; 176 177 178 if ($save == ADODB_FETCH_NUM) { 179 $retarr[] = $fld; 180 } else { 181 $retarr[strtoupper($fld->name)] = $fld; 182 } 183 $rs->MoveNext(); 184 } 185 186 $rs->Close(); 187 return $retarr; 188 189 } 190 191 192 function MetaIndexes($table,$primary=false, $owner=false) 193 { 194 $table = $this->qstr($table); 195 196 $sql = "SELECT i.name AS ind_name, C.name AS col_name, USER_NAME(O.uid) AS Owner, c.colid, k.Keyno, 197 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, 198 CASE WHEN I.status & 2 = 2 THEN 1 ELSE 0 END AS IsUnique 199 FROM dbo.sysobjects o INNER JOIN dbo.sysindexes I ON o.id = i.id 200 INNER JOIN dbo.sysindexkeys K ON I.id = K.id AND I.Indid = K.Indid 201 INNER JOIN dbo.syscolumns c ON K.id = C.id AND K.colid = C.Colid 202 WHERE LEFT(i.name, 8) <> '_WA_Sys_' AND o.status >= 0 AND O.Name LIKE $table 203 ORDER BY O.name, I.Name, K.keyno"; 204 205 global $ADODB_FETCH_MODE; 206 $save = $ADODB_FETCH_MODE; 207 $ADODB_FETCH_MODE = ADODB_FETCH_NUM; 208 if ($this->fetchMode !== FALSE) { 209 $savem = $this->SetFetchMode(FALSE); 210 } 211 212 $rs = $this->Execute($sql); 213 if (isset($savem)) { 214 $this->SetFetchMode($savem); 215 } 216 $ADODB_FETCH_MODE = $save; 217 218 if (!is_object($rs)) { 219 return FALSE; 220 } 221 222 $indexes = array(); 223 while ($row = $rs->FetchRow()) { 224 if (!$primary && $row[5]) continue; 225 226 $indexes[$row[0]]['unique'] = $row[6]; 227 $indexes[$row[0]]['columns'][] = $row[1]; 228 } 229 return $indexes; 230 } 231 232 function _query($sql,$inputarr=false) 233 { 234 if (is_string($sql)) $sql = str_replace('||','+',$sql); 235 return ADODB_odbc::_query($sql,$inputarr); 236 } 237 238 function SetTransactionMode( $transaction_mode ) 239 { 240 $this->_transmode = $transaction_mode; 241 if (empty($transaction_mode)) { 242 $this->Execute('SET TRANSACTION ISOLATION LEVEL READ COMMITTED'); 243 return; 244 } 245 if (!stristr($transaction_mode,'isolation')) $transaction_mode = 'ISOLATION LEVEL '.$transaction_mode; 246 $this->Execute("SET TRANSACTION ".$transaction_mode); 247 } 248 249 // "Stein-Aksel Basma" <basma@accelero.no> 250 // tested with MSSQL 2000 251 function MetaPrimaryKeys($table, $owner = false) 252 { 253 global $ADODB_FETCH_MODE; 254 255 $schema = ''; 256 $this->_findschema($table,$schema); 257 //if (!$schema) $schema = $this->database; 258 if ($schema) $schema = "and k.table_catalog like '$schema%'"; 259 260 $sql = "select distinct k.column_name,ordinal_position from information_schema.key_column_usage k, 261 information_schema.table_constraints tc 262 where tc.constraint_name = k.constraint_name and tc.constraint_type = 263 'PRIMARY KEY' and k.table_name = '$table' $schema order by ordinal_position "; 264 265 $savem = $ADODB_FETCH_MODE; 266 $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC; 267 $a = $this->GetCol($sql); 268 $ADODB_FETCH_MODE = $savem; 269 270 if ($a && sizeof($a)>0) return $a; 271 $false = false; 272 return $false; 273 } 274 275 function SelectLimit($sql,$nrows=-1,$offset=-1, $inputarr=false,$secs2cache=0) 276 { 277 $nrows = (int) $nrows; 278 $offset = (int) $offset; 279 if ($nrows > 0 && $offset <= 0) { 280 $sql = preg_replace( 281 '/(^\s*select\s+(distinctrow|distinct)?)/i','\\1 '.$this->hasTop." $nrows ",$sql); 282 $rs = $this->Execute($sql,$inputarr); 283 } else 284 $rs = ADOConnection::SelectLimit($sql,$nrows,$offset,$inputarr,$secs2cache); 285 286 return $rs; 287 } 288 289 // Format date column in sql string given an input format that understands Y M D 290 function SQLDate($fmt, $col=false) 291 { 292 if (!$col) $col = $this->sysTimeStamp; 293 $s = ''; 294 295 $len = strlen($fmt); 296 for ($i=0; $i < $len; $i++) { 297 if ($s) $s .= '+'; 298 $ch = $fmt[$i]; 299 switch($ch) { 300 case 'Y': 301 case 'y': 302 $s .= "datename(yyyy,$col)"; 303 break; 304 case 'M': 305 $s .= "convert(char(3),$col,0)"; 306 break; 307 case 'm': 308 $s .= "replace(str(month($col),2),' ','0')"; 309 break; 310 case 'Q': 311 case 'q': 312 $s .= "datename(quarter,$col)"; 313 break; 314 case 'D': 315 case 'd': 316 $s .= "replace(str(day($col),2),' ','0')"; 317 break; 318 case 'h': 319 $s .= "substring(convert(char(14),$col,0),13,2)"; 320 break; 321 322 case 'H': 323 $s .= "replace(str(datepart(hh,$col),2),' ','0')"; 324 break; 325 326 case 'i': 327 $s .= "replace(str(datepart(mi,$col),2),' ','0')"; 328 break; 329 case 's': 330 $s .= "replace(str(datepart(ss,$col),2),' ','0')"; 331 break; 332 case 'a': 333 case 'A': 334 $s .= "substring(convert(char(19),$col,0),18,2)"; 335 break; 336 337 default: 338 if ($ch == '\\') { 339 $i++; 340 $ch = substr($fmt,$i,1); 341 } 342 $s .= $this->qstr($ch); 343 break; 344 } 345 } 346 return $s; 347 } 348 349 /** 350 * Returns a substring of a varchar type field 351 * 352 * The SQL server version varies because the length is mandatory, so 353 * we append a reasonable string length 354 * 355 * @param string $fld The field to sub-string 356 * @param int $start The start point 357 * @param int $length An optional length 358 * 359 * @return The SQL text 360 */ 361 function substr($fld,$start,$length=0) 362 { 363 if ($length == 0) 364 /* 365 * The length available to varchar is 2GB, but that makes no 366 * sense in a substring, so I'm going to arbitrarily limit 367 * the length to 1K, but you could change it if you want 368 */ 369 $length = 1024; 370 371 $text = "SUBSTRING($fld,$start,$length)"; 372 return $text; 373 } 374 375 /** 376 * Returns the maximum size of a MetaType C field. Because of the 377 * database design, SQL Server places no limits on the size of data inserted 378 * Although the actual limit is 2^31-1 bytes. 379 * 380 * @return int 381 */ 382 function charMax() 383 { 384 return ADODB_STRINGMAX_NOLIMIT; 385 } 386 387 /** 388 * Returns the maximum size of a MetaType X field. Because of the 389 * database design, SQL Server places no limits on the size of data inserted 390 * Although the actual limit is 2^31-1 bytes. 391 * 392 * @return int 393 */ 394 function textMax() 395 { 396 return ADODB_STRINGMAX_NOLIMIT; 397 } 398 399 // returns concatenated string 400 // MSSQL requires integers to be cast as strings 401 // automatically cast every datatype to VARCHAR(255) 402 // @author David Rogers (introspectshun) 403 function Concat() 404 { 405 $s = ""; 406 $arr = func_get_args(); 407 408 // Split single record on commas, if possible 409 if (sizeof($arr) == 1) { 410 foreach ($arr as $arg) { 411 $args = explode(',', $arg); 412 } 413 $arr = $args; 414 } 415 416 array_walk( 417 $arr, 418 function(&$value, $key) { 419 $value = "CAST(" . $value . " AS VARCHAR(255))"; 420 } 421 ); 422 $s = implode('+',$arr); 423 if (sizeof($arr) > 0) return "$s"; 424 425 return ''; 426 } 427 428 } 429 430 class ADORecordSet_odbc_mssql extends ADORecordSet_odbc { 431 432 var $databaseType = 'odbc_mssql'; 433 434 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body