MDL-69542 enrol_lti: add published resource view object support
[moodle.git] / lib / adodb / datadict / datadict-mysql.inc.php
bloba1ee950a6ba2ab267205e62ffc9faa0b955daffc
1 <?php
2 /**
3 * Data Dictionary for MySQL.
5 * This file is part of ADOdb, a Database Abstraction Layer library for PHP.
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
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
18 * @copyright 2000-2013 John Lim
19 * @copyright 2014 Damien Regad, Mark Newnham and the ADOdb community
22 // security - hide paths
23 if (!defined('ADODB_DIR')) die();
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
31 var $dropIndex = 'DROP INDEX %s ON %s';
32 var $renameColumn = 'ALTER TABLE %s CHANGE COLUMN %s %s %s'; // needs column-definition!
34 public $blobAllowsNotNull = true;
36 function MetaType($t,$len=-1,$fieldobj=false)
39 if (is_object($t)) {
40 $fieldobj = $t;
41 $t = $fieldobj->type;
42 $len = $fieldobj->max_length;
44 $is_serial = is_object($fieldobj) && $fieldobj->primary_key && $fieldobj->auto_increment;
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';
57 case 'TEXT':
58 case 'LONGTEXT':
59 case 'MEDIUMTEXT':
60 return 'X';
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';
70 case 'YEAR':
71 case 'DATE': return 'D';
73 case 'TIME':
74 case 'DATETIME':
75 case 'TIMESTAMP': return 'T';
77 case 'FLOAT':
78 case 'DOUBLE':
79 return 'F';
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;
91 function ActualType($meta)
93 switch(strtoupper($meta)) {
94 case 'C': return 'VARCHAR';
95 case 'XL':return 'LONGTEXT';
96 case 'X': return 'TEXT';
98 case 'C2': return 'VARCHAR';
99 case 'X2': return 'LONGTEXT';
101 case 'B': return 'LONGBLOB';
103 case 'D': return 'DATE';
104 case 'TS':
105 case 'T': return 'DATETIME';
106 case 'L': return 'TINYINT';
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';
115 case 'F': return 'DOUBLE';
116 case 'N': return 'NUMERIC';
117 default:
118 return $meta;
122 // return string must begin with space
123 function _CreateSuffix($fname,&$ftype,$fnotnull,$fdefault,$fautoinc,$fconstraint,$funsigned)
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;
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)
151 CREATE [UNIQUE|FULLTEXT] INDEX index_name
152 ON tbl_name (col_name[(length)],... )
155 function _IndexSQL($idxname, $tabname, $flds, $idxoptions)
157 $sql = array();
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);
163 if ( isset($idxoptions['DROP']) )
164 return $sql;
167 if ( empty ($flds) ) {
168 return $sql;
171 if (isset($idxoptions['FULLTEXT'])) {
172 $unique = ' FULLTEXT';
173 } elseif (isset($idxoptions['UNIQUE'])) {
174 $unique = ' UNIQUE';
175 } else {
176 $unique = '';
179 if ( is_array($flds) ) $flds = implode(', ',$flds);
181 if ($this->alterTableAddIndex) $s = "ALTER TABLE $tabname ADD $unique INDEX $idxname ";
182 else $s = 'CREATE' . $unique . ' INDEX ' . $idxname . ' ON ' . $tabname;
184 $s .= ' (' . $flds . ')';
186 if ( isset($idxoptions[$this->upperName]) )
187 $s .= $idxoptions[$this->upperName];
189 $sql[] = $s;
191 return $sql;