Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401]

   1  <?php
   2  /**
   3   * Frontbase driver.
   4   *
   5   * This file is part of ADOdb, a Database Abstraction Layer library for PHP.
   6   *
   7   * @package ADOdb
   8   * @link https://adodb.org Project's web site and documentation
   9   * @link https://github.com/ADOdb/ADOdb Source code and issue tracker
  10   *
  11   * The ADOdb Library is dual-licensed, released under both the BSD 3-Clause
  12   * and the GNU Lesser General Public Licence (LGPL) v2.1 or, at your option,
  13   * any later version. This means you can use it in proprietary products.
  14   * See the LICENSE.md file distributed with this source code for details.
  15   * @license BSD-3-Clause
  16   * @license LGPL-2.1-or-later
  17   *
  18   * @copyright 2000-2013 John Lim
  19   * @copyright 2014 Damien Regad, Mark Newnham and the ADOdb community
  20   * @author Frank M. Kromann <frank@frontbase.com>
  21   */
  22  
  23  // security - hide paths
  24  if (!defined('ADODB_DIR')) die();
  25  
  26  if (! defined("_ADODB_FBSQL_LAYER")) {
  27   define("_ADODB_FBSQL_LAYER", 1 );
  28  
  29  class ADODB_fbsql extends ADOConnection {
  30  	 var $databaseType = 'fbsql';
  31  	 var $hasInsertID = true;
  32  	 var $hasAffectedRows = true;
  33  	 var $metaTablesSQL = "SHOW TABLES";
  34  	 var $metaColumnsSQL = "SHOW COLUMNS FROM %s";
  35  	 var $fmtTimeStamp = "'Y-m-d H:i:s'";
  36  	 var $hasLimit = false;
  37  
  38  	protected function _insertID($table = '', $column = '')
  39  	 {
  40  	 	 	 return fbsql_insert_id($this->_connectionID);
  41  	 }
  42  
  43  	function _affectedrows()
  44  	 {
  45  	 	 	 return fbsql_affected_rows($this->_connectionID);
  46  	 }
  47  
  48    	function MetaDatabases()
  49  	 {
  50  	 	 $qid = fbsql_list_dbs($this->_connectionID);
  51  	 	 $arr = array();
  52  	 	 $i = 0;
  53  	 	 $max = fbsql_num_rows($qid);
  54  	 	 while ($i < $max) {
  55  	 	 	 $arr[] = fbsql_tablename($qid,$i);
  56  	 	 	 $i += 1;
  57  	 	 }
  58  	 	 return $arr;
  59  	 }
  60  
  61  	 // returns concatenated string
  62  	function Concat()
  63  	 {
  64  	 	 $s = "";
  65  	 	 $arr = func_get_args();
  66  	 	 $first = true;
  67  
  68  	 	 $s = implode(',',$arr);
  69  	 	 if (sizeof($arr) > 0) return "CONCAT($s)";
  70  	 	 else return '';
  71  	 }
  72  
  73  	 // returns true or false
  74  	function _connect($argHostname, $argUsername, $argPassword, $argDatabasename)
  75  	 {
  76  	 	 $this->_connectionID = fbsql_connect($argHostname,$argUsername,$argPassword);
  77  	 	 if ($this->_connectionID === false) return false;
  78  	 	 if ($argDatabasename) return $this->SelectDB($argDatabasename);
  79  	 	 return true;
  80  	 }
  81  
  82  	 // returns true or false
  83  	function _pconnect($argHostname, $argUsername, $argPassword, $argDatabasename)
  84  	 {
  85  	 	 $this->_connectionID = fbsql_pconnect($argHostname,$argUsername,$argPassword);
  86  	 	 if ($this->_connectionID === false) return false;
  87  	 	 if ($argDatabasename) return $this->SelectDB($argDatabasename);
  88  	 	 return true;
  89  	 }
  90  
  91   	function MetaColumns($table, $normalize=true)
  92  	 {
  93  	 	 if ($this->metaColumnsSQL) {
  94  
  95  	 	 	 $rs = $this->Execute(sprintf($this->metaColumnsSQL,$table));
  96  
  97  	 	 	 if ($rs === false) return false;
  98  
  99  	 	 	 $retarr = array();
 100  	 	 	 while (!$rs->EOF){
 101  	 	 	 	 $fld = new ADOFieldObject();
 102  	 	 	 	 $fld->name = $rs->fields[0];
 103  	 	 	 	 $fld->type = $rs->fields[1];
 104  
 105  	 	 	 	 // split type into type(length):
 106  	 	 	 	 if (preg_match("/^(.+)\((\d+)\)$/", $fld->type, $query_array)) {
 107  	 	 	 	 	 $fld->type = $query_array[1];
 108  	 	 	 	 	 $fld->max_length = $query_array[2];
 109  	 	 	 	 } else {
 110  	 	 	 	 	 $fld->max_length = -1;
 111  	 	 	 	 }
 112  	 	 	 	 $fld->not_null = ($rs->fields[2] != 'YES');
 113  	 	 	 	 $fld->primary_key = ($rs->fields[3] == 'PRI');
 114  	 	 	 	 $fld->auto_increment = (strpos($rs->fields[5], 'auto_increment') !== false);
 115  	 	 	 	 $fld->binary = (strpos($fld->type,'blob') !== false);
 116  
 117  	 	 	 	 $retarr[strtoupper($fld->name)] = $fld;
 118  	 	 	 	 $rs->MoveNext();
 119  	 	 	 }
 120  	 	 	 $rs->Close();
 121  	 	 	 return $retarr;
 122  	 	 }
 123  	 	 return false;
 124  	 }
 125  
 126  	 // returns true or false
 127  	function SelectDB($dbName)
 128  	 {
 129  	 	 $this->database = $dbName;
 130  	 	 if ($this->_connectionID) {
 131  	 	 	 return @fbsql_select_db($dbName,$this->_connectionID);
 132  	 	 }
 133  	 	 else return false;
 134  	 }
 135  
 136  
 137  	 // returns queryID or false
 138  	function _query($sql,$inputarr=false)
 139  	 {
 140  	 	 return fbsql_query("$sql;",$this->_connectionID);
 141  	 }
 142  
 143  	 /*	 Returns: the last error message from previous database operation	 */
 144  	function ErrorMsg()
 145  	 {
 146  	 	 $this->_errorMsg = @fbsql_error($this->_connectionID);
 147  	 	 	 return $this->_errorMsg;
 148  	 }
 149  
 150  	 /*	 Returns: the last error number from previous database operation	 */
 151  	function ErrorNo()
 152  	 {
 153  	 	 return @fbsql_errno($this->_connectionID);
 154  	 }
 155  
 156  	 // returns true or false
 157  	function _close()
 158  	 {
 159  	 	 return @fbsql_close($this->_connectionID);
 160  	 }
 161  
 162  }
 163  
 164  /*--------------------------------------------------------------------------------------
 165  	  Class Name: Recordset
 166  --------------------------------------------------------------------------------------*/
 167  
 168  class ADORecordSet_fbsql extends ADORecordSet{
 169  
 170  	 var $databaseType = "fbsql";
 171  	 var $canSeek = true;
 172  
 173  	function __construct($queryID,$mode=false)
 174  	 {
 175  	 	 if (!$mode) {
 176  	 	 	 global $ADODB_FETCH_MODE;
 177  	 	 	 $mode = $ADODB_FETCH_MODE;
 178  	 	 }
 179  	 	 switch ($mode) {
 180  	 	 case ADODB_FETCH_NUM: $this->fetchMode = FBSQL_NUM; break;
 181  	 	 case ADODB_FETCH_ASSOC: $this->fetchMode = FBSQL_ASSOC; break;
 182  	 	 case ADODB_FETCH_BOTH:
 183  	 	 default:
 184  	 	 $this->fetchMode = FBSQL_BOTH; break;
 185  	 	 }
 186  	 	 parent::__construct($queryID);
 187  	 }
 188  
 189  	function _initrs()
 190  	 {
 191  	 GLOBAL $ADODB_COUNTRECS;
 192  	 	 $this->_numOfRows = ($ADODB_COUNTRECS) ? @fbsql_num_rows($this->_queryID):-1;
 193  	 	 $this->_numOfFields = @fbsql_num_fields($this->_queryID);
 194  	 }
 195  
 196  
 197  
 198  	function FetchField($fieldOffset = -1) {
 199  	 	 if ($fieldOffset != -1) {
 200  	 	 	 $o =  @fbsql_fetch_field($this->_queryID, $fieldOffset);
 201  	 	 	 //$o->max_length = -1; // fbsql returns the max length less spaces -- so it is unrealiable
 202  	 	 	 $f = @fbsql_field_flags($this->_queryID,$fieldOffset);
 203  	 	 	 $o->binary = (strpos($f,'binary')!== false);
 204  	 	 }
 205  	 	 else if ($fieldOffset == -1) {	 /*	 The $fieldOffset argument is not provided thus its -1 	 */
 206  	 	 	 $o = @fbsql_fetch_field($this->_queryID);// fbsql returns the max length less spaces -- so it is unrealiable
 207  	 	 	 //$o->max_length = -1;
 208  	 	 }
 209  
 210  	 	 return $o;
 211  	 }
 212  
 213  	function _seek($row)
 214  	 {
 215  	 	 return @fbsql_data_seek($this->_queryID,$row);
 216  	 }
 217  
 218  	function _fetch($ignore_fields=false)
 219  	 {
 220  	 	 $this->fields = @fbsql_fetch_array($this->_queryID,$this->fetchMode);
 221  	 	 return ($this->fields == true);
 222  	 }
 223  
 224  	function _close() {
 225  	 	 return @fbsql_free_result($this->_queryID);
 226  	 }
 227  
 228  	function MetaType($t,$len=-1,$fieldobj=false)
 229  	 {
 230  	 	 if (is_object($t)) {
 231  	 	 	 $fieldobj = $t;
 232  	 	 	 $t = $fieldobj->type;
 233  	 	 	 $len = $fieldobj->max_length;
 234  	 	 }
 235  
 236  	 	 $t = strtoupper($t);
 237  
 238  	 	 if (array_key_exists($t,$this->connection->customActualTypes))
 239  	 	 	 return $this->connection->customActualTypes[$t];
 240  
 241  	 	 $len = -1; // fbsql max_length is not accurate
 242  
 243  	 	 switch ($t) {
 244  	 	 case 'CHARACTER':
 245  	 	 case 'CHARACTER VARYING':
 246  	 	 case 'BLOB':
 247  	 	 case 'CLOB':
 248  	 	 case 'BIT':
 249  	 	 case 'BIT VARYING':
 250  	 	 	 if ($len <= $this->blobSize) return 'C';
 251  
 252  	 	 // so we have to check whether binary...
 253  	 	 case 'IMAGE':
 254  	 	 case 'LONGBLOB':
 255  	 	 case 'BLOB':
 256  	 	 case 'MEDIUMBLOB':
 257  	 	 	 return !empty($fieldobj->binary) ? 'B' : 'X';
 258  
 259  	 	 case 'DATE': return 'D';
 260  
 261  	 	 case 'TIME':
 262  	 	 case 'TIME WITH TIME ZONE':
 263  	 	 case 'TIMESTAMP':
 264  	 	 case 'TIMESTAMP WITH TIME ZONE': return 'T';
 265  
 266  	 	 case 'PRIMARY_KEY':
 267  	 	 	 return 'R';
 268  	 	 case 'INTEGER':
 269  	 	 case 'SMALLINT':
 270  	 	 case 'BOOLEAN':
 271  
 272  	 	 	 if (!empty($fieldobj->primary_key)) return 'R';
 273  	 	 	 else return 'I';
 274  
 275  	 	 default: return ADODB_DEFAULT_METATYPE;
 276  	 	 }
 277  	 }
 278  
 279  } //class
 280  } // defined