Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

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

   1  <?php
   2  /**
   3   * Data Dictionary for Firebird.
   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   */
  21  
  22  // security - hide paths
  23  if (!defined('ADODB_DIR')) die();
  24  
  25  class ADODB2_firebird extends ADODB_DataDict {
  26  
  27  	 var $databaseType = 'firebird';
  28  	 var $seqField = false;
  29  	 var $seqPrefix = 's_';
  30  	 var $blobSize = 40000;
  31  	 var $renameColumn = 'ALTER TABLE %s ALTER %s TO %s';
  32  	 var $alterCol = ' ALTER';
  33  	 var $dropCol = ' DROP';
  34  
  35  	function ActualType($meta)
  36  	 {
  37  	 	 switch($meta) {
  38  	 	 case 'C': return 'VARCHAR';
  39  	 	 case 'XL':
  40  	 	 case 'X': return 'BLOB SUB_TYPE TEXT';
  41  
  42  	 	 case 'C2': return 'VARCHAR(32765)'; // up to 32K
  43  	 	 case 'X2': return 'VARCHAR(4096)';
  44  
  45  	 	 case 'V': return 'CHAR';
  46  	 	 case 'C1': return 'CHAR(1)';
  47  
  48  	 	 case 'B': return 'BLOB';
  49  
  50  	 	 case 'D': return 'DATE';
  51  	 	 case 'TS':
  52  	 	 case 'T': return 'TIMESTAMP';
  53  
  54  	 	 case 'L': return 'SMALLINT';
  55  	 	 case 'I': return 'INTEGER';
  56  	 	 case 'I1': return 'SMALLINT';
  57  	 	 case 'I2': return 'SMALLINT';
  58  	 	 case 'I4': return 'INTEGER';
  59  	 	 case 'I8': return 'BIGINT';
  60  
  61  	 	 case 'F': return 'DOUBLE PRECISION';
  62  	 	 case 'N': return 'DECIMAL';
  63  	 	 default:
  64  	 	 	 return $meta;
  65  	 	 }
  66  	 }
  67  
  68  	function NameQuote($name = NULL,$allowBrackets=false)
  69  	 {
  70  	 	 if (!is_string($name)) {
  71  	 	 	 return FALSE;
  72  	 	 }
  73  
  74  	 	 $name = trim($name);
  75  
  76  	 	 if ( !is_object($this->connection) ) {
  77  	 	 	 return $name;
  78  	 	 }
  79  
  80  	 	 $quote = $this->connection->nameQuote;
  81  
  82  	 	 // if name is of the form `name`, quote it
  83  	 	 if ( preg_match('/^`(.+)`$/', $name, $matches) ) {
  84  	 	 	 return $quote . $matches[1] . $quote;
  85  	 	 }
  86  
  87  	 	 // if name contains special characters, quote it
  88  	 	 if ( !preg_match('/^[' . $this->nameRegex . ']+$/', $name) ) {
  89  	 	 	 return $quote . $name . $quote;
  90  	 	 }
  91  
  92  	 	 return $quote . $name . $quote;
  93  	 }
  94  
  95  	function CreateDatabase($dbname, $options=false)
  96  	 {
  97  	 	 $options = $this->_Options($options);
  98  	 	 $sql = array();
  99  
 100  	 	 $sql[] = "DECLARE EXTERNAL FUNCTION LOWER CSTRING(80) RETURNS CSTRING(80) FREE_IT ENTRY_POINT 'IB_UDF_lower' MODULE_NAME 'ib_udf'";
 101  
 102  	 	 return $sql;
 103  	 }
 104  
 105  	function _DropAutoIncrement($t)
 106  	 {
 107  	 	 if (strpos($t,'.') !== false) {
 108  	 	 	 $tarr = explode('.',$t);
 109  	 	 	 return 'DROP GENERATOR '.$tarr[0].'."s_'.$tarr[1].'"';
 110  	 	 }
 111  	 	 return 'DROP GENERATOR s_'.$t;
 112  	 }
 113  
 114  
 115  	function _CreateSuffix($fname,&$ftype,$fnotnull,$fdefault,$fautoinc,$fconstraint,$funsigned)
 116  	 {
 117  	 	 $suffix = '';
 118  
 119  	 	 if (strlen($fdefault)) $suffix .= " DEFAULT $fdefault";
 120  	 	 if ($fnotnull) $suffix .= ' NOT NULL';
 121  	 	 if ($fautoinc) $this->seqField = $fname;
 122  	 	 $fconstraint = preg_replace("/``/", "\"", $fconstraint);
 123  	 	 if ($fconstraint) $suffix .= ' '.$fconstraint;
 124  
 125  	 	 return $suffix;
 126  	 }
 127  
 128  	 /**
 129  	  Generate the SQL to create table. Returns an array of sql strings.
 130  	 */
 131  	function CreateTableSQL($tabname, $flds, $tableoptions=array())
 132  	 {
 133  	 	 list($lines,$pkey,$idxs) = $this->_GenFields($flds, true);
 134  	 	 // genfields can return FALSE at times
 135  	 	 if ($lines == null) $lines = array();
 136  
 137  	 	 $taboptions = $this->_Options($tableoptions);
 138  	 	 $tabname = $this->TableName ($tabname);
 139  	 	 $sql = $this->_TableSQL($tabname,$lines,$pkey,$taboptions);
 140  
 141  	 	 if ($this->autoIncrement && !isset($taboptions['DROP']))
 142  	 	 { $tsql = $this->_Triggers($tabname,$taboptions);
 143  	 	 	 foreach($tsql as $s) $sql[] = $s;
 144  	 	 }
 145  
 146  	 	 if (is_array($idxs)) {
 147  	 	 	 foreach($idxs as $idx => $idxdef) {
 148  	 	 	 	 $sql_idxs = $this->CreateIndexSql($idx, $tabname,  $idxdef['cols'], $idxdef['opts']);
 149  	 	 	 	 $sql = array_merge($sql, $sql_idxs);
 150  	 	 	 }
 151  	 	 }
 152  
 153  	 	 return $sql;
 154  	 }
 155  
 156  
 157  /*
 158  CREATE or replace TRIGGER jaddress_insert
 159  before insert on jaddress
 160  for each row
 161  begin
 162  IF ( NEW."seqField" IS NULL OR NEW."seqField" = 0 ) THEN
 163    NEW."seqField" = GEN_ID("GEN_tabname", 1);
 164  end;
 165  */
 166  	function _Triggers($tabname,$tableoptions)
 167  	 {
 168  	 	 if (!$this->seqField) return array();
 169  
 170  	 	 $tab1 = preg_replace( '/"/', '', $tabname );
 171  	 	 if ($this->schema) {
 172  	 	 	 $t = strpos($tab1,'.');
 173  	 	 	 if ($t !== false) $tab = substr($tab1,$t+1);
 174  	 	 	 else $tab = $tab1;
 175  	 	 	 $seqField = $this->seqField;
 176  	 	 	 $seqname = $this->schema.'.'.$this->seqPrefix.$tab;
 177  	 	 	 $trigname = $this->schema.'.t_'.$this->seqPrefix.$tab;
 178  	 	 } else {
 179  	 	 	 $seqField = $this->seqField;
 180  	 	 	 $seqname = $this->seqPrefix.$tab1;
 181  	 	 	 $trigname = 't_'.$seqname;
 182  	 	 }
 183  
 184  	 	 if (isset($tableoptions['DROP']))
 185  	 	 { $sql[] = "DROP GENERATOR $seqname";
 186  	 	 }
 187  	 	 elseif (isset($tableoptions['REPLACE']))
 188  	 	 { $sql[] = "DROP GENERATOR \"$seqname\"";
 189  	 	   $sql[] = "CREATE GENERATOR \"$seqname\"";
 190  	 	   $sql[] = "ALTER TRIGGER \"$trigname\" BEFORE INSERT OR UPDATE AS BEGIN IF ( NEW.$seqField IS NULL OR NEW.$seqField = 0 ) THEN NEW.$seqField = GEN_ID(\"$seqname\", 1); END";
 191  	 	 }
 192  	 	 else
 193  	 	 { $sql[] = "CREATE GENERATOR $seqname";
 194  	 	   $sql[] = "CREATE TRIGGER $trigname FOR $tabname BEFORE INSERT OR UPDATE AS BEGIN IF ( NEW.$seqField IS NULL OR NEW.$seqField = 0 ) THEN NEW.$seqField = GEN_ID($seqname, 1); END";
 195  	 	 }
 196  
 197  	 	 $this->seqField = false;
 198  	 	 return $sql;
 199  	 }
 200  
 201  	 /**
 202  	  * Change the definition of one column
 203  	  *
 204  	  * As some DBM's can't do that on there own, you need to supply the complete definition of the new table,
 205  	  * to allow, recreating the table and copying the content over to the new table
 206  	  * @param string $tabname table-name
 207  	  * @param string $flds column-name and type for the changed column
 208  	  * @param string $tableflds='' complete definition of the new table, eg. for postgres, default ''
 209  	  * @param array/string $tableoptions='' options for the new table see CreateTableSQL, default ''
 210  	  * @return array with SQL strings
 211  	  */
 212  	function AlterColumnSQL($tabname, $flds, $tableflds='',$tableoptions='')
 213  	 {
 214  	 	 $tabname = $this->TableName ($tabname);
 215  	 	 $sql = array();
 216  	 	 list($lines,$pkey,$idxs) = $this->_GenFields($flds);
 217  	 	 // genfields can return FALSE at times
 218  	 	 if ($lines == null) $lines = array();
 219  	 	 $alter = 'ALTER TABLE ' . $tabname . $this->alterCol . ' ';
 220  	 	 foreach($lines as $v) {
 221  	 	 	 $sql[] = $alter . $v;
 222  	 	 }
 223  	 	 if (is_array($idxs)) {
 224  	 	 	 foreach($idxs as $idx => $idxdef) {
 225  	 	 	 	 $sql_idxs = $this->CreateIndexSql($idx, $tabname, $idxdef['cols'], $idxdef['opts']);
 226  	 	 	 	 $sql = array_merge($sql, $sql_idxs);
 227  	 	 	 }
 228  
 229  	 	 }
 230  	 	 return $sql;
 231  	 }
 232  
 233  }