Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]
1 <?php 2 /* 3 @version v5.20.16 12-Jan-2020 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.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(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 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 $nrows = (int) $nrows; 284 $offset = (int) $offset; 285 if ($nrows > 0 && $offset <= 0) { 286 $sql = preg_replace( 287 '/(^\s*select\s+(distinctrow|distinct)?)/i','\\1 '.$this->hasTop." $nrows ",$sql); 288 $rs = $this->Execute($sql,$inputarr); 289 } else 290 $rs = ADOConnection::SelectLimit($sql,$nrows,$offset,$inputarr,$secs2cache); 291 292 return $rs; 293 } 294 295 // Format date column in sql string given an input format that understands Y M D 296 function SQLDate($fmt, $col=false) 297 { 298 if (!$col) $col = $this->sysTimeStamp; 299 $s = ''; 300 301 $len = strlen($fmt); 302 for ($i=0; $i < $len; $i++) { 303 if ($s) $s .= '+'; 304 $ch = $fmt[$i]; 305 switch($ch) { 306 case 'Y': 307 case 'y': 308 $s .= "datename(yyyy,$col)"; 309 break; 310 case 'M': 311 $s .= "convert(char(3),$col,0)"; 312 break; 313 case 'm': 314 $s .= "replace(str(month($col),2),' ','0')"; 315 break; 316 case 'Q': 317 case 'q': 318 $s .= "datename(quarter,$col)"; 319 break; 320 case 'D': 321 case 'd': 322 $s .= "replace(str(day($col),2),' ','0')"; 323 break; 324 case 'h': 325 $s .= "substring(convert(char(14),$col,0),13,2)"; 326 break; 327 328 case 'H': 329 $s .= "replace(str(datepart(hh,$col),2),' ','0')"; 330 break; 331 332 case 'i': 333 $s .= "replace(str(datepart(mi,$col),2),' ','0')"; 334 break; 335 case 's': 336 $s .= "replace(str(datepart(ss,$col),2),' ','0')"; 337 break; 338 case 'a': 339 case 'A': 340 $s .= "substring(convert(char(19),$col,0),18,2)"; 341 break; 342 343 default: 344 if ($ch == '\\') { 345 $i++; 346 $ch = substr($fmt,$i,1); 347 } 348 $s .= $this->qstr($ch); 349 break; 350 } 351 } 352 return $s; 353 } 354 355 } 356 357 class ADORecordSet_odbc_mssql extends ADORecordSet_odbc { 358 359 var $databaseType = 'odbc_mssql'; 360 361 function __construct($id,$mode=false) 362 { 363 return parent::__construct($id,$mode); 364 } 365 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body