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