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 DB2.
   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_db2 extends ADODB_DataDict {
  26  
  27  	 var $databaseType = 'db2';
  28  	 var $seqField = false;
  29  	 var $dropCol = 'ALTER TABLE %s DROP COLUMN %s';
  30  
  31  	 public $blobAllowsDefaultValue = true;
  32  	 public $blobAllowsNotNull      = true;
  33  
  34  	 
  35   	function ActualType($meta)
  36  	 {
  37  	 	 switch($meta) {
  38  	 	 case 'C': return 'VARCHAR';
  39  	 	 case 'XL': return 'CLOB';
  40  	 	 case 'X': return 'VARCHAR(3600)';
  41  
  42  	 	 case 'C2': return 'VARCHAR'; // up to 32K
  43  	 	 case 'X2': return 'VARCHAR(3600)'; // up to 32000, but default page size too small
  44  
  45  	 	 case 'B': return 'BLOB';
  46  
  47  	 	 case 'D': return 'DATE';
  48  	 	 case 'TS':
  49  	 	 case 'T': return 'TIMESTAMP';
  50  
  51  	 	 case 'L': return 'SMALLINT';
  52  	 	 case 'I': return 'INTEGER';
  53  	 	 case 'I1': return 'SMALLINT';
  54  	 	 case 'I2': return 'SMALLINT';
  55  	 	 case 'I4': return 'INTEGER';
  56  	 	 case 'I8': return 'BIGINT';
  57  
  58  	 	 case 'F': return 'DOUBLE';
  59  	 	 case 'N': return 'DECIMAL';
  60  	 	 default:
  61  	 	 	 return $meta;
  62  	 	 }
  63  	 }
  64  
  65  	 // return string must begin with space
  66  	function _CreateSuffix($fname,&$ftype,$fnotnull,$fdefault,$fautoinc,$fconstraint,$funsigned)
  67  	 {
  68  	 	 $suffix = '';
  69  	 	 if ($fautoinc) return ' GENERATED ALWAYS AS IDENTITY'; # as identity start with
  70  	 	 if (strlen($fdefault)) $suffix .= " DEFAULT $fdefault";
  71  	 	 if ($fnotnull) $suffix .= ' NOT NULL';
  72  	 	 if ($fconstraint) $suffix .= ' '.$fconstraint;
  73  	 	 return $suffix;
  74  	 }
  75  
  76  	function alterColumnSQL($tabname, $flds, $tableflds='',$tableoptions='')
  77  	 {
  78  	 	 $tabname = $this->TableName ($tabname);
  79  	 	 $sql = array();
  80  	 	 list($lines,$pkey,$idxs) = $this->_GenFields($flds);
  81  	 	 // genfields can return FALSE at times
  82  	 	 if ($lines == null) $lines = array();
  83  	 	 $alter = 'ALTER TABLE ' . $tabname . $this->alterCol . ' ';
  84  	 	 
  85  	 	 $dataTypeWords = array('SET','DATA','TYPE');
  86  	 	 
  87  	 	 foreach($lines as $v) 
  88  	 	 {
  89  	 	 	 /*
  90  	 	 	  * We must now post-process the line to insert the 'SET DATA TYPE'
  91  	 	 	  * text into the alter statement
  92  	 	 	  */
  93  	 	 	 $e = explode(' ',$v);
  94  	 	 	 
  95  	 	 	 array_splice($e,1,0,$dataTypeWords);
  96  	 	 	 
  97  	 	 	 $v = implode(' ',$e);
  98  	 	 	 
  99  	 	 	 $sql[] = $alter . $v;
 100  	 	 }
 101  	 	 if (is_array($idxs)) 
 102  	 	 {
 103  	 	 	 foreach($idxs as $idx => $idxdef) {
 104  	 	 	 	 $sql_idxs = $this->CreateIndexSql($idx, $tabname, $idxdef['cols'], $idxdef['opts']);
 105  	 	 	 	 $sql = array_merge($sql, $sql_idxs);
 106  	 	 	 }
 107  
 108  	 	 }
 109  	 	 return $sql;
 110  	 }
 111  
 112  
 113  	 
 114  	function dropColumnSql($tabname, $flds, $tableflds='',$tableoptions='')
 115  	 {
 116  	 	 
 117  	 	 
 118  	 	 $tabname = $this->connection->getMetaCasedValue($tabname);
 119  	 	 $flds    = $this->connection->getMetaCasedValue($flds);
 120  	 	 
 121  	 	 if (ADODB_ASSOC_CASE  == ADODB_ASSOC_CASE_NATIVE )
 122  	 	 {
 123  	 	 	 /*
 124  	 	 	  * METACASE_NATIVE
 125  	 	 	  */
 126  	 	 	 $tabname = $this->connection->nameQuote . $tabname . $this->connection->nameQuote;
 127  	 	 	 $flds    = $this->connection->nameQuote . $flds . $this->connection->nameQuote;
 128  	 	 }
 129  	 	 $sql = sprintf($this->dropCol,$tabname,$flds);
 130  	 	 return (array)$sql;
 131  
 132  	 }
 133      
 134  
 135  	function changeTableSQL($tablename, $flds, $tableoptions = false, $dropOldFields=false)
 136  	 {
 137  
 138  	 	 /**
 139  	 	   Allow basic table changes to DB2 databases
 140  	 	   DB2 will fatally reject changes to non character columns
 141  
 142  	 	 */
 143  
 144  	 	 $validTypes = array("CHAR","VARC");
 145  	 	 $invalidTypes = array("BIGI","BLOB","CLOB","DATE", "DECI","DOUB", "INTE", "REAL","SMAL", "TIME");
 146  	 	 // check table exists
 147  	 	 
 148  	 	 
 149  	 	 $cols = $this->metaColumns($tablename);
 150  	 	 if ( empty($cols)) {
 151  	 	 	 return $this->createTableSQL($tablename, $flds, $tableoptions);
 152  	 	 }
 153  
 154  	 	 // already exists, alter table instead
 155  	 	 list($lines,$pkey) = $this->_GenFields($flds);
 156  	 	 $alter = 'ALTER TABLE ' . $this->tableName($tablename);
 157  	 	 $sql = array();
 158  
 159  	 	 foreach ( $lines as $id => $v ) {
 160  	 	 	 /*
 161  	 	 	  * If the metaCasing was NATIVE the col returned with nameQuotes
 162  	 	 	  * around the field. We need to remove this for the metaColumn
 163  	 	 	  * match
 164  	 	 	  */
 165  	 	 	 $id = str_replace($this->connection->nameQuote,'',$id);
 166  	 	 	 if ( isset($cols[$id]) && is_object($cols[$id]) ) {
 167  	 	 	 	 /**
 168  	 	 	 	   If the first field of $v is the fieldname, and
 169  	 	 	 	   the second is the field type/size, we assume its an
 170  	 	 	 	   attempt to modify the column size, so check that it is allowed
 171  	 	 	 	   $v can have an indeterminate number of blanks between the
 172  	 	 	 	   fields, so account for that too
 173  	 	 	 	  */
 174  	 	 	 	 $vargs = explode(' ' , $v);
 175  	 	 	 	 // assume that $vargs[0] is the field name.
 176  	 	 	 	 $i=0;
 177  	 	 	 	 // Find the next non-blank value;
 178  	 	 	 	 for ($i=1;$i<sizeof($vargs);$i++)
 179  	 	 	 	 	 if ($vargs[$i] != '')
 180  	 	 	 	 	 	 break;
 181  
 182  	 	 	 	 // if $vargs[$i] is one of the following, we are trying to change the
 183  	 	 	 	 // size of the field, if not allowed, simply ignore the request.
 184  	 	 	 	 if (in_array(substr($vargs[$i],0,4),$invalidTypes))
 185  	 	 	 	 	 continue;
 186  	 	 	 	 // insert the appropriate DB2 syntax
 187  	 	 	 	 if (in_array(substr($vargs[$i],0,4),$validTypes)) {
 188  	 	 	 	 	 array_splice($vargs,$i,0,array('SET','DATA','TYPE'));
 189  	 	 	 	 }
 190  
 191  	 	 	 	 // Now Look for the NOT NULL statement as this is not allowed in
 192  	 	 	 	 // the ALTER table statement. If it is in there, remove it
 193  	 	 	 	 if (in_array('NOT',$vargs) && in_array('NULL',$vargs)) {
 194  	 	 	 	 	 for ($i=1;$i<sizeof($vargs);$i++)
 195  	 	 	 	 	 if ($vargs[$i] == 'NOT')
 196  	 	 	 	 	 	 break;
 197  	 	 	 	 	 array_splice($vargs,$i,2,'');
 198  	 	 	 	 }
 199  	 	 	 	 $v = implode(' ',$vargs);
 200  	 	 	 	 $sql[] = $alter . $this->alterCol . ' ' . $v;
 201  	 	 	 } else {
 202  	 	 	 	 $sql[] = $alter . $this->addCol . ' ' . $v;
 203  	 	 	 }
 204  	 	 }
 205  
 206  	 	 return $sql;
 207  	 }
 208  
 209  }