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   * Data Dictionary for MySQL.
   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_mysql extends ADODB_DataDict {
  26  	 var $databaseType = 'mysql';
  27  	 var $alterCol = ' MODIFY COLUMN';
  28  	 var $alterTableAddIndex = true;
  29  	 var $dropTable = 'DROP TABLE IF EXISTS %s'; // requires mysql 3.22 or later
  30  
  31  	 var $dropIndex = 'DROP INDEX %s ON %s';
  32  	 var $renameColumn = 'ALTER TABLE %s CHANGE COLUMN %s %s %s';	 // needs column-definition!
  33  
  34  	 public $blobAllowsNotNull = true;
  35  	 
  36  	function metaType($t,$len=-1,$fieldobj=false)
  37  	 {
  38  	 	 
  39  	 	 if (is_object($t)) {
  40  	 	 	 $fieldobj = $t;
  41  	 	 	 $t = $fieldobj->type;
  42  	 	 	 $len = $fieldobj->max_length;
  43  	 	 }
  44  	 	 $is_serial = is_object($fieldobj) && $fieldobj->primary_key && $fieldobj->auto_increment;
  45  
  46  	 	 $len = -1; // mysql max_length is not accurate
  47  	 	 	 
  48  	 	 $t = strtoupper($t);
  49  	 	 
  50  	 	 if (array_key_exists($t,$this->connection->customActualTypes))
  51  	 	 	 return  $this->connection->customActualTypes[$t];
  52  	 	 
  53  	 	 switch ($t) {
  54  	 	 	 
  55  	 	 case 'STRING':
  56  	 	 case 'CHAR':
  57  	 	 case 'VARCHAR':
  58  	 	 case 'TINYBLOB':
  59  	 	 case 'TINYTEXT':
  60  	 	 case 'ENUM':
  61  	 	 case 'SET':
  62  	 	 	 if ($len <= $this->blobSize) return 'C';
  63  
  64  	 	 case 'TEXT':
  65  	 	 case 'LONGTEXT':
  66  	 	 case 'MEDIUMTEXT':
  67  	 	 	 return 'X';
  68  
  69  	 	 // php_mysql extension always returns 'blob' even if 'text'
  70  	 	 // so we have to check whether binary...
  71  	 	 case 'IMAGE':
  72  	 	 case 'LONGBLOB':
  73  	 	 case 'BLOB':
  74  	 	 case 'MEDIUMBLOB':
  75  	 	 	 return !empty($fieldobj->binary) ? 'B' : 'X';
  76  
  77  	 	 case 'YEAR':
  78  	 	 case 'DATE': return 'D';
  79  
  80  	 	 case 'TIME':
  81  	 	 case 'DATETIME':
  82  	 	 case 'TIMESTAMP': return 'T';
  83  
  84  	 	 case 'FLOAT':
  85  	 	 case 'DOUBLE':
  86  	 	 	 return 'F';
  87  
  88  	 	 case 'INT':
  89  	 	 case 'INTEGER': return $is_serial ? 'R' : 'I';
  90  	 	 case 'TINYINT': return $is_serial ? 'R' : 'I1';
  91  	 	 case 'SMALLINT': return $is_serial ? 'R' : 'I2';
  92  	 	 case 'MEDIUMINT': return $is_serial ? 'R' : 'I4';
  93  	 	 case 'BIGINT':  return $is_serial ? 'R' : 'I8';
  94  	 	 default: 
  95  	 	 	 
  96  	 	 	 return ADODB_DEFAULT_METATYPE;
  97  	 	 }
  98  	 }
  99  
 100  	function ActualType($meta)
 101  	 {
 102  	 	 
 103  	 	 $meta = strtoupper($meta);
 104  	 	 
 105  	 	 /*
 106  	 	 * Add support for custom meta types. We do this
 107  	 	 * first, that allows us to override existing types
 108  	 	 */
 109  	 	 if (isset($this->connection->customMetaTypes[$meta]))
 110  	 	 	 return $this->connection->customMetaTypes[$meta]['actual'];
 111  	 	 	 	 
 112  	 	 switch($meta) 
 113  	 	 {
 114  	 	 
 115  	 	 case 'C': return 'VARCHAR';
 116  	 	 case 'XL':return 'LONGTEXT';
 117  	 	 case 'X': return 'TEXT';
 118  
 119  	 	 case 'C2': return 'VARCHAR';
 120  	 	 case 'X2': return 'LONGTEXT';
 121  
 122  	 	 case 'B': return 'LONGBLOB';
 123  
 124  	 	 case 'D': return 'DATE';
 125  	 	 case 'TS':
 126  	 	 case 'T': return 'DATETIME';
 127  	 	 case 'L': return 'TINYINT';
 128  
 129  	 	 case 'R':
 130  	 	 case 'I4':
 131  	 	 case 'I': return 'INTEGER';
 132  	 	 case 'I1': return 'TINYINT';
 133  	 	 case 'I2': return 'SMALLINT';
 134  	 	 case 'I8': return 'BIGINT';
 135  
 136  	 	 case 'F': return 'DOUBLE';
 137  	 	 case 'N': return 'NUMERIC';
 138  	 	 	 
 139  	 	 default:
 140  	 	 	 
 141  	 	 	 return $meta;
 142  	 	 }
 143  	 }
 144  
 145  	 // return string must begin with space
 146  	function _CreateSuffix($fname,&$ftype,$fnotnull,$fdefault,$fautoinc,$fconstraint,$funsigned)
 147  	 {
 148  	 	 $suffix = '';
 149  	 	 if ($funsigned) $suffix .= ' UNSIGNED';
 150  	 	 if ($fnotnull) $suffix .= ' NOT NULL';
 151  	 	 if (strlen($fdefault)) $suffix .= " DEFAULT $fdefault";
 152  	 	 if ($fautoinc) $suffix .= ' AUTO_INCREMENT';
 153  	 	 if ($fconstraint) $suffix .= ' '.$fconstraint;
 154  	 	 return $suffix;
 155  	 }
 156  
 157  	 /*
 158  	 CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name [(create_definition,...)]
 159  	 	 [table_options] [select_statement]
 160  	 	 create_definition:
 161  	 	 col_name type [NOT NULL | NULL] [DEFAULT default_value] [AUTO_INCREMENT]
 162  	 	 [PRIMARY KEY] [reference_definition]
 163  	 	 or PRIMARY KEY (index_col_name,...)
 164  	 	 or KEY [index_name] (index_col_name,...)
 165  	 	 or INDEX [index_name] (index_col_name,...)
 166  	 	 or UNIQUE [INDEX] [index_name] (index_col_name,...)
 167  	 	 or FULLTEXT [INDEX] [index_name] (index_col_name,...)
 168  	 	 or [CONSTRAINT symbol] FOREIGN KEY [index_name] (index_col_name,...)
 169  	 	 [reference_definition]
 170  	 	 or CHECK (expr)
 171  	 */
 172  
 173  	 /*
 174  	 CREATE [UNIQUE|FULLTEXT] INDEX index_name
 175  	 	 ON tbl_name (col_name[(length)],... )
 176  	 */
 177  
 178  	function _IndexSQL($idxname, $tabname, $flds, $idxoptions)
 179  	 {
 180  	 	 $sql = array();
 181  
 182  	 	 if ( isset($idxoptions['REPLACE']) || isset($idxoptions['DROP']) ) {
 183  	 	 	 if ($this->alterTableAddIndex) $sql[] = "ALTER TABLE $tabname DROP INDEX $idxname";
 184  	 	 	 else $sql[] = sprintf($this->dropIndex, $idxname, $tabname);
 185  
 186  	 	 	 if ( isset($idxoptions['DROP']) )
 187  	 	 	 	 return $sql;
 188  	 	 }
 189  
 190  	 	 if ( empty ($flds) ) {
 191  	 	 	 return $sql;
 192  	 	 }
 193  
 194  	 	 if (isset($idxoptions['FULLTEXT'])) {
 195  	 	 	 $unique = ' FULLTEXT';
 196  	 	 } elseif (isset($idxoptions['UNIQUE'])) {
 197  	 	 	 $unique = ' UNIQUE';
 198  	 	 } else {
 199  	 	 	 $unique = '';
 200  	 	 }
 201  
 202  	 	 if ( is_array($flds) ) $flds = implode(', ',$flds);
 203  
 204  	 	 if ($this->alterTableAddIndex) $s = "ALTER TABLE $tabname ADD $unique INDEX $idxname ";
 205  	 	 else $s = 'CREATE' . $unique . ' INDEX ' . $idxname . ' ON ' . $tabname;
 206  
 207  	 	 $s .= ' (' . $flds . ')';
 208  
 209  	 	 if ( isset($idxoptions[$this->upperName]) )
 210  	 	 	 $s .= $idxoptions[$this->upperName];
 211  
 212  	 	 $sql[] = $s;
 213  
 214  	 	 return $sql;
 215  	 }
 216  }