Documentation TYPO3 par Ameos |
00001 <?php 00002 /* 00003 V4.80 8 Mar 2006 (c) 2000-2006 John Lim (jlim@natsoft.com.my). All rights reserved. 00004 Released under both BSD license and Lesser GPL library license. 00005 Whenever there is any discrepancy between the two licenses, 00006 the BSD license will take precedence. 00007 Set tabs to 4 for best viewing. 00008 00009 Latest version is available at http://adodb.sourceforge.net 00010 00011 MSSQL support via ODBC. Requires ODBC. Works on Windows and Unix. 00012 For Unix configuration, see http://phpbuilder.com/columns/alberto20000919.php3 00013 */ 00014 00015 // security - hide paths 00016 if (!defined('ADODB_DIR')) die(); 00017 00018 if (!defined('_ADODB_ODBC_LAYER')) { 00019 include(ADODB_DIR."/drivers/adodb-odbc.inc.php"); 00020 } 00021 00022 00023 class ADODB_odbc_mssql extends ADODB_odbc { 00024 var $databaseType = 'odbc_mssql'; 00025 var $fmtDate = "'Y-m-d'"; 00026 var $fmtTimeStamp = "'Y-m-d H:i:s'"; 00027 var $_bindInputArray = true; 00028 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'))"; 00029 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'"; 00030 var $hasTop = 'top'; // support mssql/interbase SELECT TOP 10 * FROM TABLE 00031 var $sysDate = 'GetDate()'; 00032 var $sysTimeStamp = 'GetDate()'; 00033 var $leftOuter = '*='; 00034 var $rightOuter = '=*'; 00035 var $substr = 'substring'; 00036 var $length = 'len'; 00037 var $ansiOuter = true; // for mssql7 or later 00038 var $identitySQL = 'select @@IDENTITY'; // 'select SCOPE_IDENTITY'; # for mssql 2000 00039 var $hasInsertID = true; 00040 var $connectStmt = 'SET CONCAT_NULL_YIELDS_NULL OFF'; # When SET CONCAT_NULL_YIELDS_NULL is ON, 00041 # concatenating a null value with a string yields a NULL result 00042 00043 function ADODB_odbc_mssql() 00044 { 00045 $this->ADODB_odbc(); 00046 //$this->curmode = SQL_CUR_USE_ODBC; 00047 } 00048 00049 // crashes php... 00050 function ServerInfo() 00051 { 00052 global $ADODB_FETCH_MODE; 00053 $save = $ADODB_FETCH_MODE; 00054 $ADODB_FETCH_MODE = ADODB_FETCH_NUM; 00055 $row = $this->GetRow("execute sp_server_info 2"); 00056 $ADODB_FETCH_MODE = $save; 00057 if (!is_array($row)) return false; 00058 $arr['description'] = $row[2]; 00059 $arr['version'] = ADOConnection::_findvers($arr['description']); 00060 return $arr; 00061 } 00062 00063 function IfNull( $field, $ifNull ) 00064 { 00065 return " ISNULL($field, $ifNull) "; // if MS SQL Server 00066 } 00067 00068 function _insertid() 00069 { 00070 // SCOPE_IDENTITY() 00071 // Returns the last IDENTITY value inserted into an IDENTITY column in 00072 // the same scope. A scope is a module -- a stored procedure, trigger, 00073 // function, or batch. Thus, two statements are in the same scope if 00074 // they are in the same stored procedure, function, or batch. 00075 return $this->GetOne($this->identitySQL); 00076 } 00077 00078 00079 function MetaForeignKeys($table, $owner=false, $upper=false) 00080 { 00081 global $ADODB_FETCH_MODE; 00082 00083 $save = $ADODB_FETCH_MODE; 00084 $ADODB_FETCH_MODE = ADODB_FETCH_NUM; 00085 $table = $this->qstr(strtoupper($table)); 00086 00087 $sql = 00088 "select object_name(constid) as constraint_name, 00089 col_name(fkeyid, fkey) as column_name, 00090 object_name(rkeyid) as referenced_table_name, 00091 col_name(rkeyid, rkey) as referenced_column_name 00092 from sysforeignkeys 00093 where upper(object_name(fkeyid)) = $table 00094 order by constraint_name, referenced_table_name, keyno"; 00095 00096 $constraints =& $this->GetArray($sql); 00097 00098 $ADODB_FETCH_MODE = $save; 00099 00100 $arr = false; 00101 foreach($constraints as $constr) { 00102 //print_r($constr); 00103 $arr[$constr[0]][$constr[2]][] = $constr[1].'='.$constr[3]; 00104 } 00105 if (!$arr) return false; 00106 00107 $arr2 = false; 00108 00109 foreach($arr as $k => $v) { 00110 foreach($v as $a => $b) { 00111 if ($upper) $a = strtoupper($a); 00112 $arr2[$a] = $b; 00113 } 00114 } 00115 return $arr2; 00116 } 00117 00118 function &MetaTables($ttype=false,$showSchema=false,$mask=false) 00119 { 00120 if ($mask) {$this->debug=1; 00121 $save = $this->metaTablesSQL; 00122 $mask = $this->qstr($mask); 00123 $this->metaTablesSQL .= " AND name like $mask"; 00124 } 00125 $ret =& ADOConnection::MetaTables($ttype,$showSchema); 00126 00127 if ($mask) { 00128 $this->metaTablesSQL = $save; 00129 } 00130 return $ret; 00131 } 00132 00133 function &MetaColumns($table) 00134 { 00135 $arr = ADOConnection::MetaColumns($table); 00136 return $arr; 00137 } 00138 00139 function _query($sql,$inputarr) 00140 { 00141 if (is_string($sql)) $sql = str_replace('||','+',$sql); 00142 return ADODB_odbc::_query($sql,$inputarr); 00143 } 00144 00145 // "Stein-Aksel Basma" <basma@accelero.no> 00146 // tested with MSSQL 2000 00147 function &MetaPrimaryKeys($table) 00148 { 00149 global $ADODB_FETCH_MODE; 00150 00151 $schema = ''; 00152 $this->_findschema($table,$schema); 00153 //if (!$schema) $schema = $this->database; 00154 if ($schema) $schema = "and k.table_catalog like '$schema%'"; 00155 00156 $sql = "select distinct k.column_name,ordinal_position from information_schema.key_column_usage k, 00157 information_schema.table_constraints tc 00158 where tc.constraint_name = k.constraint_name and tc.constraint_type = 00159 'PRIMARY KEY' and k.table_name = '$table' $schema order by ordinal_position "; 00160 00161 $savem = $ADODB_FETCH_MODE; 00162 $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC; 00163 $a = $this->GetCol($sql); 00164 $ADODB_FETCH_MODE = $savem; 00165 00166 if ($a && sizeof($a)>0) return $a; 00167 $false = false; 00168 return $false; 00169 } 00170 00171 function &SelectLimit($sql,$nrows=-1,$offset=-1, $inputarr=false,$secs2cache=0) 00172 { 00173 if ($nrows > 0 && $offset <= 0) { 00174 $sql = preg_replace( 00175 '/(^\s*select\s+(distinctrow|distinct)?)/i','\\1 '.$this->hasTop." $nrows ",$sql); 00176 $rs =& $this->Execute($sql,$inputarr); 00177 } else 00178 $rs =& ADOConnection::SelectLimit($sql,$nrows,$offset,$inputarr,$secs2cache); 00179 00180 return $rs; 00181 } 00182 00183 // Format date column in sql string given an input format that understands Y M D 00184 function SQLDate($fmt, $col=false) 00185 { 00186 if (!$col) $col = $this->sysTimeStamp; 00187 $s = ''; 00188 00189 $len = strlen($fmt); 00190 for ($i=0; $i < $len; $i++) { 00191 if ($s) $s .= '+'; 00192 $ch = $fmt[$i]; 00193 switch($ch) { 00194 case 'Y': 00195 case 'y': 00196 $s .= "datename(yyyy,$col)"; 00197 break; 00198 case 'M': 00199 $s .= "convert(char(3),$col,0)"; 00200 break; 00201 case 'm': 00202 $s .= "replace(str(month($col),2),' ','0')"; 00203 break; 00204 case 'Q': 00205 case 'q': 00206 $s .= "datename(quarter,$col)"; 00207 break; 00208 case 'D': 00209 case 'd': 00210 $s .= "replace(str(day($col),2),' ','0')"; 00211 break; 00212 case 'h': 00213 $s .= "substring(convert(char(14),$col,0),13,2)"; 00214 break; 00215 00216 case 'H': 00217 $s .= "replace(str(datepart(hh,$col),2),' ','0')"; 00218 break; 00219 00220 case 'i': 00221 $s .= "replace(str(datepart(mi,$col),2),' ','0')"; 00222 break; 00223 case 's': 00224 $s .= "replace(str(datepart(ss,$col),2),' ','0')"; 00225 break; 00226 case 'a': 00227 case 'A': 00228 $s .= "substring(convert(char(19),$col,0),18,2)"; 00229 break; 00230 00231 default: 00232 if ($ch == '\\') { 00233 $i++; 00234 $ch = substr($fmt,$i,1); 00235 } 00236 $s .= $this->qstr($ch); 00237 break; 00238 } 00239 } 00240 return $s; 00241 } 00242 00243 } 00244 00245 class ADORecordSet_odbc_mssql extends ADORecordSet_odbc { 00246 00247 var $databaseType = 'odbc_mssql'; 00248 00249 function ADORecordSet_odbc_mssql($id,$mode=false) 00250 { 00251 return $this->ADORecordSet_odbc($id,$mode); 00252 } 00253 } 00254 ?>