weekly release 3.11.8+
[moodle.git] / lib / adodb / datadict / datadict-mysql.inc.php
blob58f1110265b1770c3c6f51e050fee140183c9d5f
1 <?php
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.
11 Set tabs to 4 for best viewing.
15 // security - hide paths
16 if (!defined('ADODB_DIR')) die();
18 class ADODB2_mysql extends ADODB_DataDict {
19 var $databaseType = 'mysql';
20 var $alterCol = ' MODIFY COLUMN';
21 var $alterTableAddIndex = true;
22 var $dropTable = 'DROP TABLE IF EXISTS %s'; // requires mysql 3.22 or later
24 var $dropIndex = 'DROP INDEX %s ON %s';
25 var $renameColumn = 'ALTER TABLE %s CHANGE COLUMN %s %s %s'; // needs column-definition!
27 public $blobAllowsNotNull = true;
29 function MetaType($t,$len=-1,$fieldobj=false)
32 if (is_object($t)) {
33 $fieldobj = $t;
34 $t = $fieldobj->type;
35 $len = $fieldobj->max_length;
37 $is_serial = is_object($fieldobj) && $fieldobj->primary_key && $fieldobj->auto_increment;
39 $len = -1; // mysql max_length is not accurate
40 switch (strtoupper($t)) {
41 case 'STRING':
42 case 'CHAR':
43 case 'VARCHAR':
44 case 'TINYBLOB':
45 case 'TINYTEXT':
46 case 'ENUM':
47 case 'SET':
48 if ($len <= $this->blobSize) return 'C';
50 case 'TEXT':
51 case 'LONGTEXT':
52 case 'MEDIUMTEXT':
53 return 'X';
55 // php_mysql extension always returns 'blob' even if 'text'
56 // so we have to check whether binary...
57 case 'IMAGE':
58 case 'LONGBLOB':
59 case 'BLOB':
60 case 'MEDIUMBLOB':
61 return !empty($fieldobj->binary) ? 'B' : 'X';
63 case 'YEAR':
64 case 'DATE': return 'D';
66 case 'TIME':
67 case 'DATETIME':
68 case 'TIMESTAMP': return 'T';
70 case 'FLOAT':
71 case 'DOUBLE':
72 return 'F';
74 case 'INT':
75 case 'INTEGER': return $is_serial ? 'R' : 'I';
76 case 'TINYINT': return $is_serial ? 'R' : 'I1';
77 case 'SMALLINT': return $is_serial ? 'R' : 'I2';
78 case 'MEDIUMINT': return $is_serial ? 'R' : 'I4';
79 case 'BIGINT': return $is_serial ? 'R' : 'I8';
80 default: return ADODB_DEFAULT_METATYPE;
84 function ActualType($meta)
86 switch(strtoupper($meta)) {
87 case 'C': return 'VARCHAR';
88 case 'XL':return 'LONGTEXT';
89 case 'X': return 'TEXT';
91 case 'C2': return 'VARCHAR';
92 case 'X2': return 'LONGTEXT';
94 case 'B': return 'LONGBLOB';
96 case 'D': return 'DATE';
97 case 'TS':
98 case 'T': return 'DATETIME';
99 case 'L': return 'TINYINT';
101 case 'R':
102 case 'I4':
103 case 'I': return 'INTEGER';
104 case 'I1': return 'TINYINT';
105 case 'I2': return 'SMALLINT';
106 case 'I8': return 'BIGINT';
108 case 'F': return 'DOUBLE';
109 case 'N': return 'NUMERIC';
110 default:
111 return $meta;
115 // return string must begin with space
116 function _CreateSuffix($fname,&$ftype,$fnotnull,$fdefault,$fautoinc,$fconstraint,$funsigned)
118 $suffix = '';
119 if ($funsigned) $suffix .= ' UNSIGNED';
120 if ($fnotnull) $suffix .= ' NOT NULL';
121 if (strlen($fdefault)) $suffix .= " DEFAULT $fdefault";
122 if ($fautoinc) $suffix .= ' AUTO_INCREMENT';
123 if ($fconstraint) $suffix .= ' '.$fconstraint;
124 return $suffix;
128 CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name [(create_definition,...)]
129 [table_options] [select_statement]
130 create_definition:
131 col_name type [NOT NULL | NULL] [DEFAULT default_value] [AUTO_INCREMENT]
132 [PRIMARY KEY] [reference_definition]
133 or PRIMARY KEY (index_col_name,...)
134 or KEY [index_name] (index_col_name,...)
135 or INDEX [index_name] (index_col_name,...)
136 or UNIQUE [INDEX] [index_name] (index_col_name,...)
137 or FULLTEXT [INDEX] [index_name] (index_col_name,...)
138 or [CONSTRAINT symbol] FOREIGN KEY [index_name] (index_col_name,...)
139 [reference_definition]
140 or CHECK (expr)
144 CREATE [UNIQUE|FULLTEXT] INDEX index_name
145 ON tbl_name (col_name[(length)],... )
148 function _IndexSQL($idxname, $tabname, $flds, $idxoptions)
150 $sql = array();
152 if ( isset($idxoptions['REPLACE']) || isset($idxoptions['DROP']) ) {
153 if ($this->alterTableAddIndex) $sql[] = "ALTER TABLE $tabname DROP INDEX $idxname";
154 else $sql[] = sprintf($this->dropIndex, $idxname, $tabname);
156 if ( isset($idxoptions['DROP']) )
157 return $sql;
160 if ( empty ($flds) ) {
161 return $sql;
164 if (isset($idxoptions['FULLTEXT'])) {
165 $unique = ' FULLTEXT';
166 } elseif (isset($idxoptions['UNIQUE'])) {
167 $unique = ' UNIQUE';
168 } else {
169 $unique = '';
172 if ( is_array($flds) ) $flds = implode(', ',$flds);
174 if ($this->alterTableAddIndex) $s = "ALTER TABLE $tabname ADD $unique INDEX $idxname ";
175 else $s = 'CREATE' . $unique . ' INDEX ' . $idxname . ' ON ' . $tabname;
177 $s .= ' (' . $flds . ')';
179 if ( isset($idxoptions[$this->upperName]) )
180 $s .= $idxoptions[$this->upperName];
182 $sql[] = $s;
184 return $sql;