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 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  	 	 switch (strtoupper($t)) {
  48  	 	 case 'STRING':
  49  	 	 case 'CHAR':
  50  	 	 case 'VARCHAR':
  51  	 	 case 'TINYBLOB':
  52  	 	 case 'TINYTEXT':
  53  	 	 case 'ENUM':
  54  	 	 case 'SET':
  55  	 	 	 if ($len <= $this->blobSize) return 'C';
  56  
  57  	 	 case 'TEXT':
  58  	 	 case 'LONGTEXT':
  59  	 	 case 'MEDIUMTEXT':
  60  	 	 	 return 'X';
  61  
  62  	 	 // php_mysql extension always returns 'blob' even if 'text'
  63  	 	 // so we have to check whether binary...
  64  	 	 case 'IMAGE':
  65  	 	 case 'LONGBLOB':
  66  	 	 case 'BLOB':
  67  	 	 case 'MEDIUMBLOB':
  68  	 	 	 return !empty($fieldobj->binary) ? 'B' : 'X';
  69  
  70  	 	 case 'YEAR':
  71  	 	 case 'DATE': return 'D';
  72  
  73  	 	 case 'TIME':
  74  	 	 case 'DATETIME':
  75  	 	 case 'TIMESTAMP': return 'T';
  76  
  77  	 	 case 'FLOAT':
  78  	 	 case 'DOUBLE':
  79  	 	 	 return 'F';
  80  
  81  	 	 case 'INT':
  82  	 	 case 'INTEGER': return $is_serial ? 'R' : 'I';
  83  	 	 case 'TINYINT': return $is_serial ? 'R' : 'I1';
  84  	 	 case 'SMALLINT': return $is_serial ? 'R' : 'I2';
  85  	 	 case 'MEDIUMINT': return $is_serial ? 'R' : 'I4';
  86  	 	 case 'BIGINT':  return $is_serial ? 'R' : 'I8';
  87  	 	 default: return ADODB_DEFAULT_METATYPE;
  88  	 	 }
  89  	 }
  90  
  91  	function ActualType($meta)
  92  	 {
  93  	 	 switch(strtoupper($meta)) {
  94  	 	 case 'C': return 'VARCHAR';
  95  	 	 case 'XL':return 'LONGTEXT';
  96  	 	 case 'X': return 'TEXT';
  97  
  98  	 	 case 'C2': return 'VARCHAR';
  99  	 	 case 'X2': return 'LONGTEXT';
 100  
 101  	 	 case 'B': return 'LONGBLOB';
 102  
 103  	 	 case 'D': return 'DATE';
 104  	 	 case 'TS':
 105  	 	 case 'T': return 'DATETIME';
 106  	 	 case 'L': return 'TINYINT';
 107  
 108  	 	 case 'R':
 109  	 	 case 'I4':
 110  	 	 case 'I': return 'INTEGER';
 111  	 	 case 'I1': return 'TINYINT';
 112  	 	 case 'I2': return 'SMALLINT';
 113  	 	 case 'I8': return 'BIGINT';
 114  
 115  	 	 case 'F': return 'DOUBLE';
 116  	 	 case 'N': return 'NUMERIC';
 117  	 	 default:
 118  	 	 	 return $meta;
 119  	 	 }
 120  	 }
 121  
 122  	 // return string must begin with space
 123  	function _CreateSuffix($fname,&$ftype,$fnotnull,$fdefault,$fautoinc,$fconstraint,$funsigned)
 124  	 {
 125  	 	 $suffix = '';
 126  	 	 if ($funsigned) $suffix .= ' UNSIGNED';
 127  	 	 if ($fnotnull) $suffix .= ' NOT NULL';
 128  	 	 if (strlen($fdefault)) $suffix .= " DEFAULT $fdefault";
 129  	 	 if ($fautoinc) $suffix .= ' AUTO_INCREMENT';
 130  	 	 if ($fconstraint) $suffix .= ' '.$fconstraint;
 131  	 	 return $suffix;
 132  	 }
 133  
 134  	 /*
 135  	 CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name [(create_definition,...)]
 136  	 	 [table_options] [select_statement]
 137  	 	 create_definition:
 138  	 	 col_name type [NOT NULL | NULL] [DEFAULT default_value] [AUTO_INCREMENT]
 139  	 	 [PRIMARY KEY] [reference_definition]
 140  	 	 or PRIMARY KEY (index_col_name,...)
 141  	 	 or KEY [index_name] (index_col_name,...)
 142  	 	 or INDEX [index_name] (index_col_name,...)
 143  	 	 or UNIQUE [INDEX] [index_name] (index_col_name,...)
 144  	 	 or FULLTEXT [INDEX] [index_name] (index_col_name,...)
 145  	 	 or [CONSTRAINT symbol] FOREIGN KEY [index_name] (index_col_name,...)
 146  	 	 [reference_definition]
 147  	 	 or CHECK (expr)
 148  	 */
 149  
 150  	 /*
 151  	 CREATE [UNIQUE|FULLTEXT] INDEX index_name
 152  	 	 ON tbl_name (col_name[(length)],... )
 153  	 */
 154  
 155  	function _IndexSQL($idxname, $tabname, $flds, $idxoptions)
 156  	 {
 157  	 	 $sql = array();
 158  
 159  	 	 if ( isset($idxoptions['REPLACE']) || isset($idxoptions['DROP']) ) {
 160  	 	 	 if ($this->alterTableAddIndex) $sql[] = "ALTER TABLE $tabname DROP INDEX $idxname";
 161  	 	 	 else $sql[] = sprintf($this->dropIndex, $idxname, $tabname);
 162  
 163  	 	 	 if ( isset($idxoptions['DROP']) )
 164  	 	 	 	 return $sql;
 165  	 	 }
 166  
 167  	 	 if ( empty ($flds) ) {
 168  	 	 	 return $sql;
 169  	 	 }
 170  
 171  	 	 if (isset($idxoptions['FULLTEXT'])) {
 172  	 	 	 $unique = ' FULLTEXT';
 173  	 	 } elseif (isset($idxoptions['UNIQUE'])) {
 174  	 	 	 $unique = ' UNIQUE';
 175  	 	 } else {
 176  	 	 	 $unique = '';
 177  	 	 }
 178  
 179  	 	 if ( is_array($flds) ) $flds = implode(', ',$flds);
 180  
 181  	 	 if ($this->alterTableAddIndex) $s = "ALTER TABLE $tabname ADD $unique INDEX $idxname ";
 182  	 	 else $s = 'CREATE' . $unique . ' INDEX ' . $idxname . ' ON ' . $tabname;
 183  
 184  	 	 $s .= ' (' . $flds . ')';
 185  
 186  	 	 if ( isset($idxoptions[$this->upperName]) )
 187  	 	 	 $s .= $idxoptions[$this->upperName];
 188  
 189  	 	 $sql[] = $s;
 190  
 191  	 	 return $sql;
 192  	 }
 193  }