[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/adodb/drivers/ -> adodb-sybase.inc.php (source)

   1  <?php
   2  /*
   3  @version   v5.20.3  01-Jan-2016
   4  @copyright (c) 2000-2013 John Lim. 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    Sybase driver contributed by Toni (toni.tunkkari@finebyte.com)
  14  
  15    - MSSQL date patch applied.
  16  
  17    Date patch by Toni 15 Feb 2002
  18  */
  19  
  20   // security - hide paths
  21  if (!defined('ADODB_DIR')) die();
  22  
  23  class ADODB_sybase extends ADOConnection {
  24      var $databaseType = "sybase";
  25      var $dataProvider = 'sybase';
  26      var $replaceQuote = "''"; // string to use to replace quotes
  27      var $fmtDate = "'Y-m-d'";
  28      var $fmtTimeStamp = "'Y-m-d H:i:s'";
  29      var $hasInsertID = true;
  30      var $hasAffectedRows = true;
  31        var $metaTablesSQL="select name from sysobjects where type='U' or type='V'";
  32      // see http://sybooks.sybase.com/onlinebooks/group-aw/awg0800e/dbrfen8/@ebt-link;pt=5981;uf=0?target=0;window=new;showtoc=true;book=dbrfen8
  33      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'";
  34      /*
  35      "select c.name,t.name,c.length from
  36      syscolumns c join systypes t on t.xusertype=c.xusertype join sysobjects o on o.id=c.id
  37      where o.name='%s'";
  38      */
  39      var $concat_operator = '+';
  40      var $arrayClass = 'ADORecordSet_array_sybase';
  41      var $sysDate = 'GetDate()';
  42      var $leftOuter = '*=';
  43      var $rightOuter = '=*';
  44  
  45      var $port;
  46  
  47  	function __construct()
  48      {
  49      }
  50  
  51      // might require begintrans -- committrans
  52  	function _insertid()
  53      {
  54          return $this->GetOne('select @@identity');
  55      }
  56        // might require begintrans -- committrans
  57  	function _affectedrows()
  58      {
  59          return $this->GetOne('select @@rowcount');
  60      }
  61  
  62  
  63  	function BeginTrans()
  64      {
  65  
  66          if ($this->transOff) return true;
  67          $this->transCnt += 1;
  68  
  69          $this->Execute('BEGIN TRAN');
  70          return true;
  71      }
  72  
  73  	function CommitTrans($ok=true)
  74      {
  75          if ($this->transOff) return true;
  76  
  77          if (!$ok) return $this->RollbackTrans();
  78  
  79          $this->transCnt -= 1;
  80          $this->Execute('COMMIT TRAN');
  81          return true;
  82      }
  83  
  84  	function RollbackTrans()
  85      {
  86          if ($this->transOff) return true;
  87          $this->transCnt -= 1;
  88          $this->Execute('ROLLBACK TRAN');
  89          return true;
  90      }
  91  
  92      // http://www.isug.com/Sybase_FAQ/ASE/section6.1.html#6.1.4
  93  	function RowLock($tables,$where,$col='top 1 null as ignore')
  94      {
  95          if (!$this->_hastrans) $this->BeginTrans();
  96          $tables = str_replace(',',' HOLDLOCK,',$tables);
  97          return $this->GetOne("select $col from $tables HOLDLOCK where $where");
  98  
  99      }
 100  
 101  	function SelectDB($dbName)
 102      {
 103          $this->database = $dbName;
 104          $this->databaseName = $dbName; # obsolete, retained for compat with older adodb versions
 105          if ($this->_connectionID) {
 106              return @sybase_select_db($dbName);
 107          }
 108          else return false;
 109      }
 110  
 111      /*    Returns: the last error message from previous database operation
 112          Note: This function is NOT available for Microsoft SQL Server.    */
 113  
 114  
 115  	function ErrorMsg()
 116      {
 117          if ($this->_logsql) return $this->_errorMsg;
 118          if (function_exists('sybase_get_last_message'))
 119              $this->_errorMsg = sybase_get_last_message();
 120          else
 121              $this->_errorMsg = isset($php_errormsg) ? $php_errormsg : 'SYBASE error messages not supported on this platform';
 122          return $this->_errorMsg;
 123      }
 124  
 125      // returns true or false
 126  	function _connect($argHostname, $argUsername, $argPassword, $argDatabasename)
 127      {
 128          if (!function_exists('sybase_connect')) return null;
 129  
 130          // Sybase connection on custom port
 131          if ($this->port) {
 132              $argHostname .= ':' . $this->port;
 133          }
 134  
 135          if ($this->charSet) {
 136              $this->_connectionID = sybase_connect($argHostname,$argUsername,$argPassword, $this->charSet);
 137          } else {
 138              $this->_connectionID = sybase_connect($argHostname,$argUsername,$argPassword);
 139          }
 140  
 141          if ($this->_connectionID === false) return false;
 142          if ($argDatabasename) return $this->SelectDB($argDatabasename);
 143          return true;
 144      }
 145  
 146      // returns true or false
 147  	function _pconnect($argHostname, $argUsername, $argPassword, $argDatabasename)
 148      {
 149          if (!function_exists('sybase_connect')) return null;
 150  
 151          // Sybase connection on custom port
 152          if ($this->port) {
 153              $argHostname .= ':' . $this->port;
 154          }
 155  
 156          if ($this->charSet) {
 157              $this->_connectionID = sybase_pconnect($argHostname,$argUsername,$argPassword, $this->charSet);
 158          } else {
 159              $this->_connectionID = sybase_pconnect($argHostname,$argUsername,$argPassword);
 160          }
 161  
 162          if ($this->_connectionID === false) return false;
 163          if ($argDatabasename) return $this->SelectDB($argDatabasename);
 164          return true;
 165      }
 166  
 167      // returns query ID if successful, otherwise false
 168  	function _query($sql,$inputarr=false)
 169      {
 170      global $ADODB_COUNTRECS;
 171  
 172          if ($ADODB_COUNTRECS == false && ADODB_PHPVER >= 0x4300)
 173              return sybase_unbuffered_query($sql,$this->_connectionID);
 174          else
 175              return sybase_query($sql,$this->_connectionID);
 176      }
 177  
 178      // See http://www.isug.com/Sybase_FAQ/ASE/section6.2.html#6.2.12
 179  	function SelectLimit($sql,$nrows=-1,$offset=-1,$inputarr=false,$secs2cache=0)
 180      {
 181          if ($secs2cache > 0) {// we do not cache rowcount, so we have to load entire recordset
 182              $rs = ADOConnection::SelectLimit($sql,$nrows,$offset,$inputarr,$secs2cache);
 183              return $rs;
 184          }
 185  
 186          $nrows = (integer) $nrows;
 187          $offset = (integer) $offset;
 188  
 189          $cnt = ($nrows >= 0) ? $nrows : 999999999;
 190          if ($offset > 0 && $cnt) $cnt += $offset;
 191  
 192          $this->Execute("set rowcount $cnt");
 193          $rs = ADOConnection::SelectLimit($sql,$nrows,$offset,$inputarr,0);
 194          $this->Execute("set rowcount 0");
 195  
 196          return $rs;
 197      }
 198  
 199      // returns true or false
 200  	function _close()
 201      {
 202          return @sybase_close($this->_connectionID);
 203      }
 204  
 205  	static function UnixDate($v)
 206      {
 207          return ADORecordSet_array_sybase::UnixDate($v);
 208      }
 209  
 210  	static function UnixTimeStamp($v)
 211      {
 212          return ADORecordSet_array_sybase::UnixTimeStamp($v);
 213      }
 214  
 215  
 216  
 217      # Added 2003-10-05 by Chris Phillipson
 218      # 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
 219      # to convert similar Microsoft SQL*Server (mssql) API into Sybase compatible version
 220      // Format date column in sql string given an input format that understands Y M D
 221  	function SQLDate($fmt, $col=false)
 222      {
 223          if (!$col) $col = $this->sysTimeStamp;
 224          $s = '';
 225  
 226          $len = strlen($fmt);
 227          for ($i=0; $i < $len; $i++) {
 228              if ($s) $s .= '+';
 229              $ch = $fmt[$i];
 230              switch($ch) {
 231              case 'Y':
 232              case 'y':
 233                  $s .= "datename(yy,$col)";
 234                  break;
 235              case 'M':
 236                  $s .= "convert(char(3),$col,0)";
 237                  break;
 238              case 'm':
 239                  $s .= "str_replace(str(month($col),2),' ','0')";
 240                  break;
 241              case 'Q':
 242              case 'q':
 243                  $s .= "datename(qq,$col)";
 244                  break;
 245              case 'D':
 246              case 'd':
 247                  $s .= "str_replace(str(datepart(dd,$col),2),' ','0')";
 248                  break;
 249              case 'h':
 250                  $s .= "substring(convert(char(14),$col,0),13,2)";
 251                  break;
 252  
 253              case 'H':
 254                  $s .= "str_replace(str(datepart(hh,$col),2),' ','0')";
 255                  break;
 256  
 257              case 'i':
 258                  $s .= "str_replace(str(datepart(mi,$col),2),' ','0')";
 259                  break;
 260              case 's':
 261                  $s .= "str_replace(str(datepart(ss,$col),2),' ','0')";
 262                  break;
 263              case 'a':
 264              case 'A':
 265                  $s .= "substring(convert(char(19),$col,0),18,2)";
 266                  break;
 267  
 268              default:
 269                  if ($ch == '\\') {
 270                      $i++;
 271                      $ch = substr($fmt,$i,1);
 272                  }
 273                  $s .= $this->qstr($ch);
 274                  break;
 275              }
 276          }
 277          return $s;
 278      }
 279  
 280      # Added 2003-10-07 by Chris Phillipson
 281      # 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
 282      # to convert similar Microsoft SQL*Server (mssql) API into Sybase compatible version
 283  	function MetaPrimaryKeys($table, $owner = false)
 284      {
 285          $sql = "SELECT c.column_name " .
 286                 "FROM syscolumn c, systable t " .
 287                 "WHERE t.table_name='$table' AND c.table_id=t.table_id " .
 288                 "AND t.table_type='BASE' " .
 289                 "AND c.pkey = 'Y' " .
 290                 "ORDER BY c.column_id";
 291  
 292          $a = $this->GetCol($sql);
 293          if ($a && sizeof($a)>0) return $a;
 294          return false;
 295      }
 296  }
 297  
 298  /*--------------------------------------------------------------------------------------
 299       Class Name: Recordset
 300  --------------------------------------------------------------------------------------*/
 301  global $ADODB_sybase_mths;
 302  $ADODB_sybase_mths = array(
 303      'JAN'=>1,'FEB'=>2,'MAR'=>3,'APR'=>4,'MAY'=>5,'JUN'=>6,
 304      'JUL'=>7,'AUG'=>8,'SEP'=>9,'OCT'=>10,'NOV'=>11,'DEC'=>12);
 305  
 306  class ADORecordset_sybase extends ADORecordSet {
 307  
 308      var $databaseType = "sybase";
 309      var $canSeek = true;
 310      // _mths works only in non-localised system
 311      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);
 312  
 313  	function __construct($id,$mode=false)
 314      {
 315          if ($mode === false) {
 316              global $ADODB_FETCH_MODE;
 317              $mode = $ADODB_FETCH_MODE;
 318          }
 319          if (!$mode) $this->fetchMode = ADODB_FETCH_ASSOC;
 320          else $this->fetchMode = $mode;
 321          parent::__construct($id,$mode);
 322      }
 323  
 324      /*    Returns: an object containing field information.
 325          Get column information in the Recordset object. fetchField() can be used in order to obtain information about
 326          fields in a certain query result. If the field offset isn't specified, the next field that wasn't yet retrieved by
 327          fetchField() is retrieved.    */
 328  	function FetchField($fieldOffset = -1)
 329      {
 330          if ($fieldOffset != -1) {
 331              $o = @sybase_fetch_field($this->_queryID, $fieldOffset);
 332          }
 333          else if ($fieldOffset == -1) {    /*    The $fieldOffset argument is not provided thus its -1     */
 334              $o = @sybase_fetch_field($this->_queryID);
 335          }
 336          // older versions of PHP did not support type, only numeric
 337          if ($o && !isset($o->type)) $o->type = ($o->numeric) ? 'float' : 'varchar';
 338          return $o;
 339      }
 340  
 341  	function _initrs()
 342      {
 343      global $ADODB_COUNTRECS;
 344          $this->_numOfRows = ($ADODB_COUNTRECS)? @sybase_num_rows($this->_queryID):-1;
 345          $this->_numOfFields = @sybase_num_fields($this->_queryID);
 346      }
 347  
 348  	function _seek($row)
 349      {
 350          return @sybase_data_seek($this->_queryID, $row);
 351      }
 352  
 353  	function _fetch($ignore_fields=false)
 354      {
 355          if ($this->fetchMode == ADODB_FETCH_NUM) {
 356              $this->fields = @sybase_fetch_row($this->_queryID);
 357          } else if ($this->fetchMode == ADODB_FETCH_ASSOC) {
 358              $this->fields = @sybase_fetch_assoc($this->_queryID);
 359  
 360              if (is_array($this->fields)) {
 361                  $this->fields = $this->GetRowAssoc();
 362                  return true;
 363              }
 364              return false;
 365          }  else {
 366              $this->fields = @sybase_fetch_array($this->_queryID);
 367          }
 368          if ( is_array($this->fields)) {
 369              return true;
 370          }
 371  
 372          return false;
 373      }
 374  
 375      /*    close() only needs to be called if you are worried about using too much memory while your script
 376          is running. All associated result memory for the specified result identifier will automatically be freed.    */
 377  	function _close() {
 378          return @sybase_free_result($this->_queryID);
 379      }
 380  
 381      // sybase/mssql uses a default date like Dec 30 2000 12:00AM
 382  	static function UnixDate($v)
 383      {
 384          return ADORecordSet_array_sybase::UnixDate($v);
 385      }
 386  
 387  	static function UnixTimeStamp($v)
 388      {
 389          return ADORecordSet_array_sybase::UnixTimeStamp($v);
 390      }
 391  }
 392  
 393  class ADORecordSet_array_sybase extends ADORecordSet_array {
 394  	function __construct($id=-1)
 395      {
 396          parent::__construct($id);
 397      }
 398  
 399          // sybase/mssql uses a default date like Dec 30 2000 12:00AM
 400  	static function UnixDate($v)
 401      {
 402      global $ADODB_sybase_mths;
 403  
 404          //Dec 30 2000 12:00AM
 405          if (!preg_match( "/([A-Za-z]{3})[-/\. ]+([0-9]{1,2})[-/\. ]+([0-9]{4})/"
 406              ,$v, $rr)) return parent::UnixDate($v);
 407  
 408          if ($rr[3] <= TIMESTAMP_FIRST_YEAR) return 0;
 409  
 410          $themth = substr(strtoupper($rr[1]),0,3);
 411          $themth = $ADODB_sybase_mths[$themth];
 412          if ($themth <= 0) return false;
 413          // h-m-s-MM-DD-YY
 414          return  adodb_mktime(0,0,0,$themth,$rr[2],$rr[3]);
 415      }
 416  
 417  	static function UnixTimeStamp($v)
 418      {
 419      global $ADODB_sybase_mths;
 420          //11.02.2001 Toni Tunkkari toni.tunkkari@finebyte.com
 421          //Changed [0-9] to [0-9 ] in day conversion
 422          if (!preg_match( "/([A-Za-z]{3})[-/\. ]([0-9 ]{1,2})[-/\. ]([0-9]{4}) +([0-9]{1,2}):([0-9]{1,2}) *([apAP]{0,1})/"
 423              ,$v, $rr)) return parent::UnixTimeStamp($v);
 424          if ($rr[3] <= TIMESTAMP_FIRST_YEAR) return 0;
 425  
 426          $themth = substr(strtoupper($rr[1]),0,3);
 427          $themth = $ADODB_sybase_mths[$themth];
 428          if ($themth <= 0) return false;
 429  
 430          switch (strtoupper($rr[6])) {
 431          case 'P':
 432              if ($rr[4]<12) $rr[4] += 12;
 433              break;
 434          case 'A':
 435              if ($rr[4]==12) $rr[4] = 0;
 436              break;
 437          default:
 438              break;
 439          }
 440          // h-m-s-MM-DD-YY
 441          return  adodb_mktime($rr[4],$rr[5],0,$themth,$rr[2],$rr[3]);
 442      }
 443  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1