Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

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
   5  @copyright (c) 2014      Damien Regad, Mark Newnham and the ADOdb community
   6  reserved.
   7    Released under both BSD license and Lesser GPL library license.
   8    Whenever there is any discrepancy between the two licenses,
   9    the BSD license will take precedence.
  10  Set tabs to 4 for best viewing.
  11  
  12    Latest version is available at https://adodb.org/
  13  
  14    21.02.2002 - Wade Johnson wade@wadejohnson.de
  15  	 	 	    Extended ODBC class for Sybase SQLAnywhere.
  16     1) Added support to retrieve the last row insert ID on tables with
  17  	   primary key column using autoincrement function.
  18  
  19     2) Added blob support.  Usage:
  20  	 	  a) create blob variable on db server:
  21  
  22  	 	 $dbconn->create_blobvar($blobVarName);
  23  
  24  	   b) load blob var from file.  $filename must be complete path
  25  
  26  	   $dbcon->load_blobvar_from_file($blobVarName, $filename);
  27  
  28  	   c) Use the $blobVarName in SQL insert or update statement in the values
  29  	   clause:
  30  
  31  	 	 $recordSet = $dbconn->Execute('INSERT INTO tabname (idcol, blobcol) '
  32  	 	 .
  33  	    'VALUES (\'test\', ' . $blobVarName . ')');
  34  
  35  	  instead of loading blob from a file, you can also load from
  36  	   an unformatted (raw) blob variable:
  37  	   $dbcon->load_blobvar_from_var($blobVarName, $varName);
  38  
  39  	   d) drop blob variable on db server to free up resources:
  40  	   $dbconn->drop_blobvar($blobVarName);
  41  
  42    Sybase_SQLAnywhere data driver. Requires ODBC.
  43  
  44  */
  45  
  46  // security - hide paths
  47  if (!defined('ADODB_DIR')) die();
  48  
  49  if (!defined('_ADODB_ODBC_LAYER')) {
  50   include_once(ADODB_DIR."/drivers/adodb-odbc.inc.php");
  51  }
  52  
  53  if (!defined('ADODB_SYBASE_SQLANYWHERE')){
  54  
  55   define('ADODB_SYBASE_SQLANYWHERE',1);
  56  
  57   class ADODB_sqlanywhere extends ADODB_odbc {
  58    	 var $databaseType = "sqlanywhere";
  59  	 var $hasInsertID = true;
  60  
  61  	 function _insertid() {
  62    	    return $this->GetOne('select @@identity');
  63  	  }
  64  
  65    function create_blobvar($blobVarName) {
  66     $this->Execute("create variable $blobVarName long binary");
  67     return;
  68    }
  69  
  70    function drop_blobvar($blobVarName) {
  71     $this->Execute("drop variable $blobVarName");
  72     return;
  73    }
  74  
  75    function load_blobvar_from_file($blobVarName, $filename) {
  76     $chunk_size = 1000;
  77  
  78     $fd = fopen ($filename, "rb");
  79  
  80     $integer_chunks = (integer)filesize($filename) / $chunk_size;
  81     $modulus = filesize($filename) % $chunk_size;
  82     if ($modulus != 0){
  83  	 $integer_chunks += 1;
  84     }
  85  
  86     for($loop=1;$loop<=$integer_chunks;$loop++){
  87  	 $contents = fread ($fd, $chunk_size);
  88  	 $contents = bin2hex($contents);
  89  
  90  	 $hexstring = '';
  91  
  92  	 for($loop2=0;$loop2<strlen($contents);$loop2+=2){
  93  	  $hexstring .= '\x' . substr($contents,$loop2,2);
  94  	  }
  95  
  96  	 $hexstring = $this->qstr($hexstring);
  97  
  98  	 $this->Execute("set $blobVarName = $blobVarName || " . $hexstring);
  99     }
 100  
 101     fclose ($fd);
 102     return;
 103    }
 104  
 105    function load_blobvar_from_var($blobVarName, &$varName) {
 106     $chunk_size = 1000;
 107  
 108     $integer_chunks = (integer)strlen($varName) / $chunk_size;
 109     $modulus = strlen($varName) % $chunk_size;
 110     if ($modulus != 0){
 111  	 $integer_chunks += 1;
 112     }
 113  
 114     for($loop=1;$loop<=$integer_chunks;$loop++){
 115  	 $contents = substr ($varName, (($loop - 1) * $chunk_size), $chunk_size);
 116  	 $contents = bin2hex($contents);
 117  
 118  	 $hexstring = '';
 119  
 120  	 for($loop2=0;$loop2<strlen($contents);$loop2+=2){
 121  	  $hexstring .= '\x' . substr($contents,$loop2,2);
 122  	  }
 123  
 124  	 $hexstring = $this->qstr($hexstring);
 125  
 126  	 $this->Execute("set $blobVarName = $blobVarName || " . $hexstring);
 127     }
 128  
 129     return;
 130    }
 131  
 132   /*
 133    Insert a null into the blob field of the table first.
 134    Then use UpdateBlob to store the blob.
 135  
 136    Usage:
 137  
 138    $conn->Execute('INSERT INTO blobtable (id, blobcol) VALUES (1, null)');
 139    $conn->UpdateBlob('blobtable','blobcol',$blob,'id=1');
 140   */
 141    function UpdateBlob($table,$column,&$val,$where,$blobtype='BLOB')
 142    {
 143     $blobVarName = 'hold_blob';
 144     $this->create_blobvar($blobVarName);
 145     $this->load_blobvar_from_var($blobVarName, $val);
 146     $this->Execute("UPDATE $table SET $column=$blobVarName WHERE $where");
 147     $this->drop_blobvar($blobVarName);
 148     return true;
 149    }
 150   }; //class
 151  
 152   class  ADORecordSet_sqlanywhere extends ADORecordSet_odbc {
 153  
 154    var $databaseType = "sqlanywhere";
 155  
 156  
 157   }; //class
 158  
 159  
 160  } //define