Documentation TYPO3 par Ameos |
00001 <?php 00002 /* 00003 V4.93 10 Oct 2006 (c) 2000-2006 John Lim. 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 Sybase driver contributed by Toni (toni.tunkkari@finebyte.com) 00012 00013 - MSSQL date patch applied. 00014 00015 Date patch by Toni 15 Feb 2002 00016 */ 00017 00018 // security - hide paths 00019 if (!defined('ADODB_DIR')) die(); 00020 00021 class ADODB_sybase extends ADOConnection { 00022 var $databaseType = "sybase"; 00023 var $dataProvider = 'sybase'; 00024 var $replaceQuote = "''"; // string to use to replace quotes 00025 var $fmtDate = "'Y-m-d'"; 00026 var $fmtTimeStamp = "'Y-m-d H:i:s'"; 00027 var $hasInsertID = true; 00028 var $hasAffectedRows = true; 00029 var $metaTablesSQL="select name from sysobjects where type='U' or type='V'"; 00030 // see http://sybooks.sybase.com/onlinebooks/group-aw/awg0800e/dbrfen8/@ebt-link;pt=5981;uf=0?target=0;window=new;showtoc=true;book=dbrfen8 00031 var $metaColumnsSQL = "SELECT c.column_name, c.column_type, c.width FROM syscolumn c, systable t WHERE t.table_name='%s' AND c.table_id=t.table_id AND t.table_type='BASE'"; 00032 /* 00033 "select c.name,t.name,c.length from 00034 syscolumns c join systypes t on t.xusertype=c.xusertype join sysobjects o on o.id=c.id 00035 where o.name='%s'"; 00036 */ 00037 var $concat_operator = '+'; 00038 var $arrayClass = 'ADORecordSet_array_sybase'; 00039 var $sysDate = 'GetDate()'; 00040 var $leftOuter = '*='; 00041 var $rightOuter = '=*'; 00042 00043 function ADODB_sybase() 00044 { 00045 } 00046 00047 // might require begintrans -- committrans 00048 function _insertid() 00049 { 00050 return $this->GetOne('select @@identity'); 00051 } 00052 // might require begintrans -- committrans 00053 function _affectedrows() 00054 { 00055 return $this->GetOne('select @@rowcount'); 00056 } 00057 00058 00059 function BeginTrans() 00060 { 00061 00062 if ($this->transOff) return true; 00063 $this->transCnt += 1; 00064 00065 $this->Execute('BEGIN TRAN'); 00066 return true; 00067 } 00068 00069 function CommitTrans($ok=true) 00070 { 00071 if ($this->transOff) return true; 00072 00073 if (!$ok) return $this->RollbackTrans(); 00074 00075 $this->transCnt -= 1; 00076 $this->Execute('COMMIT TRAN'); 00077 return true; 00078 } 00079 00080 function RollbackTrans() 00081 { 00082 if ($this->transOff) return true; 00083 $this->transCnt -= 1; 00084 $this->Execute('ROLLBACK TRAN'); 00085 return true; 00086 } 00087 00088 // http://www.isug.com/Sybase_FAQ/ASE/section6.1.html#6.1.4 00089 function RowLock($tables,$where,$flds='top 1 null as ignore') 00090 { 00091 if (!$this->_hastrans) $this->BeginTrans(); 00092 $tables = str_replace(',',' HOLDLOCK,',$tables); 00093 return $this->GetOne("select $flds from $tables HOLDLOCK where $where"); 00094 00095 } 00096 00097 function SelectDB($dbName) 00098 { 00099 $this->database = $dbName; 00100 $this->databaseName = $dbName; # obsolete, retained for compat with older adodb versions 00101 if ($this->_connectionID) { 00102 return @sybase_select_db($dbName); 00103 } 00104 else return false; 00105 } 00106 00107 /* Returns: the last error message from previous database operation 00108 Note: This function is NOT available for Microsoft SQL Server. */ 00109 00110 00111 function ErrorMsg() 00112 { 00113 if ($this->_logsql) return $this->_errorMsg; 00114 if (function_exists('sybase_get_last_message')) 00115 $this->_errorMsg = sybase_get_last_message(); 00116 else 00117 $this->_errorMsg = isset($php_errormsg) ? $php_errormsg : 'SYBASE error messages not supported on this platform'; 00118 return $this->_errorMsg; 00119 } 00120 00121 // returns true or false 00122 function _connect($argHostname, $argUsername, $argPassword, $argDatabasename) 00123 { 00124 if (!function_exists('sybase_connect')) return null; 00125 00126 $this->_connectionID = sybase_connect($argHostname,$argUsername,$argPassword); 00127 if ($this->_connectionID === false) return false; 00128 if ($argDatabasename) return $this->SelectDB($argDatabasename); 00129 return true; 00130 } 00131 // returns true or false 00132 function _pconnect($argHostname, $argUsername, $argPassword, $argDatabasename) 00133 { 00134 if (!function_exists('sybase_connect')) return null; 00135 00136 $this->_connectionID = sybase_pconnect($argHostname,$argUsername,$argPassword); 00137 if ($this->_connectionID === false) return false; 00138 if ($argDatabasename) return $this->SelectDB($argDatabasename); 00139 return true; 00140 } 00141 00142 // returns query ID if successful, otherwise false 00143 function _query($sql,$inputarr) 00144 { 00145 global $ADODB_COUNTRECS; 00146 00147 if ($ADODB_COUNTRECS == false && ADODB_PHPVER >= 0x4300) 00148 return sybase_unbuffered_query($sql,$this->_connectionID); 00149 else 00150 return sybase_query($sql,$this->_connectionID); 00151 } 00152 00153 // See http://www.isug.com/Sybase_FAQ/ASE/section6.2.html#6.2.12 00154 function &SelectLimit($sql,$nrows=-1,$offset=-1,$inputarr=false,$secs2cache=0) 00155 { 00156 if ($secs2cache > 0) {// we do not cache rowcount, so we have to load entire recordset 00157 $rs =& ADOConnection::SelectLimit($sql,$nrows,$offset,$inputarr,$secs2cache); 00158 return $rs; 00159 } 00160 00161 $nrows = (integer) $nrows; 00162 $offset = (integer) $offset; 00163 00164 $cnt = ($nrows >= 0) ? $nrows : 999999999; 00165 if ($offset > 0 && $cnt) $cnt += $offset; 00166 00167 $this->Execute("set rowcount $cnt"); 00168 $rs =& ADOConnection::SelectLimit($sql,$nrows,$offset,$inputarr,0); 00169 $this->Execute("set rowcount 0"); 00170 00171 return $rs; 00172 } 00173 00174 // returns true or false 00175 function _close() 00176 { 00177 return @sybase_close($this->_connectionID); 00178 } 00179 00180 function UnixDate($v) 00181 { 00182 return ADORecordSet_array_sybase::UnixDate($v); 00183 } 00184 00185 function UnixTimeStamp($v) 00186 { 00187 return ADORecordSet_array_sybase::UnixTimeStamp($v); 00188 } 00189 00190 00191 00192 # Added 2003-10-05 by Chris Phillipson 00193 # Used ASA SQL Reference Manual -- http://sybooks.sybase.com/onlinebooks/group-aw/awg0800e/dbrfen8/@ebt-link;pt=16756?target=%25N%15_12018_START_RESTART_N%25 00194 # to convert similar Microsoft SQL*Server (mssql) API into Sybase compatible version 00195 // Format date column in sql string given an input format that understands Y M D 00196 function SQLDate($fmt, $col=false) 00197 { 00198 if (!$col) $col = $this->sysTimeStamp; 00199 $s = ''; 00200 00201 $len = strlen($fmt); 00202 for ($i=0; $i < $len; $i++) { 00203 if ($s) $s .= '+'; 00204 $ch = $fmt[$i]; 00205 switch($ch) { 00206 case 'Y': 00207 case 'y': 00208 $s .= "datename(yy,$col)"; 00209 break; 00210 case 'M': 00211 $s .= "convert(char(3),$col,0)"; 00212 break; 00213 case 'm': 00214 $s .= "replace(str(month($col),2),' ','0')"; 00215 break; 00216 case 'Q': 00217 case 'q': 00218 $s .= "datename(qq,$col)"; 00219 break; 00220 case 'D': 00221 case 'd': 00222 $s .= "replace(str(datepart(dd,$col),2),' ','0')"; 00223 break; 00224 case 'h': 00225 $s .= "substring(convert(char(14),$col,0),13,2)"; 00226 break; 00227 00228 case 'H': 00229 $s .= "replace(str(datepart(hh,$col),2),' ','0')"; 00230 break; 00231 00232 case 'i': 00233 $s .= "replace(str(datepart(mi,$col),2),' ','0')"; 00234 break; 00235 case 's': 00236 $s .= "replace(str(datepart(ss,$col),2),' ','0')"; 00237 break; 00238 case 'a': 00239 case 'A': 00240 $s .= "substring(convert(char(19),$col,0),18,2)"; 00241 break; 00242 00243 default: 00244 if ($ch == '\\') { 00245 $i++; 00246 $ch = substr($fmt,$i,1); 00247 } 00248 $s .= $this->qstr($ch); 00249 break; 00250 } 00251 } 00252 return $s; 00253 } 00254 00255 # Added 2003-10-07 by Chris Phillipson 00256 # Used ASA SQL Reference Manual -- http://sybooks.sybase.com/onlinebooks/group-aw/awg0800e/dbrfen8/@ebt-link;pt=5981;uf=0?target=0;window=new;showtoc=true;book=dbrfen8 00257 # to convert similar Microsoft SQL*Server (mssql) API into Sybase compatible version 00258 function MetaPrimaryKeys($table) 00259 { 00260 $sql = "SELECT c.column_name " . 00261 "FROM syscolumn c, systable t " . 00262 "WHERE t.table_name='$table' AND c.table_id=t.table_id " . 00263 "AND t.table_type='BASE' " . 00264 "AND c.pkey = 'Y' " . 00265 "ORDER BY c.column_id"; 00266 00267 $a = $this->GetCol($sql); 00268 if ($a && sizeof($a)>0) return $a; 00269 return false; 00270 } 00271 } 00272 00273 /*-------------------------------------------------------------------------------------- 00274 Class Name: Recordset 00275 --------------------------------------------------------------------------------------*/ 00276 global $ADODB_sybase_mths; 00277 $ADODB_sybase_mths = array( 00278 'JAN'=>1,'FEB'=>2,'MAR'=>3,'APR'=>4,'MAY'=>5,'JUN'=>6, 00279 'JUL'=>7,'AUG'=>8,'SEP'=>9,'OCT'=>10,'NOV'=>11,'DEC'=>12); 00280 00281 class ADORecordset_sybase extends ADORecordSet { 00282 00283 var $databaseType = "sybase"; 00284 var $canSeek = true; 00285 // _mths works only in non-localised system 00286 var $_mths = array('JAN'=>1,'FEB'=>2,'MAR'=>3,'APR'=>4,'MAY'=>5,'JUN'=>6,'JUL'=>7,'AUG'=>8,'SEP'=>9,'OCT'=>10,'NOV'=>11,'DEC'=>12); 00287 00288 function ADORecordset_sybase($id,$mode=false) 00289 { 00290 if ($mode === false) { 00291 global $ADODB_FETCH_MODE; 00292 $mode = $ADODB_FETCH_MODE; 00293 } 00294 if (!$mode) $this->fetchMode = ADODB_FETCH_ASSOC; 00295 else $this->fetchMode = $mode; 00296 $this->ADORecordSet($id,$mode); 00297 } 00298 00299 /* Returns: an object containing field information. 00300 Get column information in the Recordset object. fetchField() can be used in order to obtain information about 00301 fields in a certain query result. If the field offset isn't specified, the next field that wasn't yet retrieved by 00302 fetchField() is retrieved. */ 00303 function &FetchField($fieldOffset = -1) 00304 { 00305 if ($fieldOffset != -1) { 00306 $o = @sybase_fetch_field($this->_queryID, $fieldOffset); 00307 } 00308 else if ($fieldOffset == -1) { /* The $fieldOffset argument is not provided thus its -1 */ 00309 $o = @sybase_fetch_field($this->_queryID); 00310 } 00311 // older versions of PHP did not support type, only numeric 00312 if ($o && !isset($o->type)) $o->type = ($o->numeric) ? 'float' : 'varchar'; 00313 return $o; 00314 } 00315 00316 function _initrs() 00317 { 00318 global $ADODB_COUNTRECS; 00319 $this->_numOfRows = ($ADODB_COUNTRECS)? @sybase_num_rows($this->_queryID):-1; 00320 $this->_numOfFields = @sybase_num_fields($this->_queryID); 00321 } 00322 00323 function _seek($row) 00324 { 00325 return @sybase_data_seek($this->_queryID, $row); 00326 } 00327 00328 function _fetch($ignore_fields=false) 00329 { 00330 if ($this->fetchMode == ADODB_FETCH_NUM) { 00331 $this->fields = @sybase_fetch_row($this->_queryID); 00332 } else if ($this->fetchMode == ADODB_FETCH_ASSOC) { 00333 $this->fields = @sybase_fetch_row($this->_queryID); 00334 if (is_array($this->fields)) { 00335 $this->fields = $this->GetRowAssoc(ADODB_ASSOC_CASE); 00336 return true; 00337 } 00338 return false; 00339 } else { 00340 $this->fields = @sybase_fetch_array($this->_queryID); 00341 } 00342 if ( is_array($this->fields)) { 00343 return true; 00344 } 00345 00346 return false; 00347 } 00348 00349 /* close() only needs to be called if you are worried about using too much memory while your script 00350 is running. All associated result memory for the specified result identifier will automatically be freed. */ 00351 function _close() { 00352 return @sybase_free_result($this->_queryID); 00353 } 00354 00355 // sybase/mssql uses a default date like Dec 30 2000 12:00AM 00356 function UnixDate($v) 00357 { 00358 return ADORecordSet_array_sybase::UnixDate($v); 00359 } 00360 00361 function UnixTimeStamp($v) 00362 { 00363 return ADORecordSet_array_sybase::UnixTimeStamp($v); 00364 } 00365 } 00366 00367 class ADORecordSet_array_sybase extends ADORecordSet_array { 00368 function ADORecordSet_array_sybase($id=-1) 00369 { 00370 $this->ADORecordSet_array($id); 00371 } 00372 00373 // sybase/mssql uses a default date like Dec 30 2000 12:00AM 00374 function UnixDate($v) 00375 { 00376 global $ADODB_sybase_mths; 00377 00378 //Dec 30 2000 12:00AM 00379 if (!ereg( "([A-Za-z]{3})[-/\. ]+([0-9]{1,2})[-/\. ]+([0-9]{4})" 00380 ,$v, $rr)) return parent::UnixDate($v); 00381 00382 if ($rr[3] <= TIMESTAMP_FIRST_YEAR) return 0; 00383 00384 $themth = substr(strtoupper($rr[1]),0,3); 00385 $themth = $ADODB_sybase_mths[$themth]; 00386 if ($themth <= 0) return false; 00387 // h-m-s-MM-DD-YY 00388 return mktime(0,0,0,$themth,$rr[2],$rr[3]); 00389 } 00390 00391 function UnixTimeStamp($v) 00392 { 00393 global $ADODB_sybase_mths; 00394 //11.02.2001 Toni Tunkkari toni.tunkkari@finebyte.com 00395 //Changed [0-9] to [0-9 ] in day conversion 00396 if (!ereg( "([A-Za-z]{3})[-/\. ]([0-9 ]{1,2})[-/\. ]([0-9]{4}) +([0-9]{1,2}):([0-9]{1,2}) *([apAP]{0,1})" 00397 ,$v, $rr)) return parent::UnixTimeStamp($v); 00398 if ($rr[3] <= TIMESTAMP_FIRST_YEAR) return 0; 00399 00400 $themth = substr(strtoupper($rr[1]),0,3); 00401 $themth = $ADODB_sybase_mths[$themth]; 00402 if ($themth <= 0) return false; 00403 00404 switch (strtoupper($rr[6])) { 00405 case 'P': 00406 if ($rr[4]<12) $rr[4] += 12; 00407 break; 00408 case 'A': 00409 if ($rr[4]==12) $rr[4] = 0; 00410 break; 00411 default: 00412 break; 00413 } 00414 // h-m-s-MM-DD-YY 00415 return mktime($rr[4],$rr[5],0,$themth,$rr[2],$rr[3]); 00416 } 00417 } 00418 ?>