3 * Data Dictionary for MySQL.
5 * This file is part of ADOdb, a Database Abstraction Layer library for PHP.
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)
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)) {
55 if ($len <= $this->blobSize
) return 'C';
62 // php_mysql extension always returns 'blob' even if 'text'
63 // so we have to check whether binary...
68 return !empty($fieldobj->binary
) ?
'B' : 'X';
71 case 'DATE': return 'D';
75 case 'TIMESTAMP': return 'T';
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';
105 case 'T': return 'DATETIME';
106 case 'L': return 'TINYINT';
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';
122 // return string must begin with space
123 function _CreateSuffix($fname,&$ftype,$fnotnull,$fdefault,$fautoinc,$fconstraint,$funsigned)
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;
135 CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name [(create_definition,...)]
136 [table_options] [select_statement]
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]
151 CREATE [UNIQUE|FULLTEXT] INDEX index_name
152 ON tbl_name (col_name[(length)],... )
155 function _IndexSQL($idxname, $tabname, $flds, $idxoptions)
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']) )
167 if ( empty ($flds) ) {
171 if (isset($idxoptions['FULLTEXT'])) {
172 $unique = ' FULLTEXT';
173 } elseif (isset($idxoptions['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
];