[BUGFIX] ADOdb: support NOT NULL/DEFAULT field attributes on BLOB/TEXT fields
[Packages/TYPO3.CMS.git] / typo3 / sysext / adodb / adodb / datadict / datadict-mysql.inc.php
1 <?php
2
3 /**
4 V5.19 23-Apr-2014 (c) 2000-2014 John Lim (jlim#natsoft.com). All rights reserved.
5 Released under both BSD license and Lesser GPL library license.
6 Whenever there is any discrepancy between the two licenses,
7 the BSD license will take precedence.
8
9 Set tabs to 4 for best viewing.
10
11 */
12
13 // security - hide paths
14 if (!defined('ADODB_DIR')) die();
15
16 class ADODB2_mysql extends ADODB_DataDict {
17 var $databaseType = 'mysql';
18 var $alterCol = ' MODIFY COLUMN';
19 var $alterTableAddIndex = true;
20 var $dropTable = 'DROP TABLE IF EXISTS %s'; // requires mysql 3.22 or later
21
22 var $dropIndex = 'DROP INDEX %s ON %s';
23 var $renameColumn = 'ALTER TABLE %s CHANGE COLUMN %s %s %s'; // needs column-definition!
24 var $blobNotNull = true;
25
26 function MetaType($t,$len=-1,$fieldobj=false)
27 {
28 if (is_object($t)) {
29 $fieldobj = $t;
30 $t = $fieldobj->type;
31 $len = $fieldobj->max_length;
32 }
33 $is_serial = is_object($fieldobj) && $fieldobj->primary_key && $fieldobj->auto_increment;
34
35 $len = -1; // mysql max_length is not accurate
36 switch (strtoupper($t)) {
37 case 'STRING':
38 case 'CHAR':
39 case 'VARCHAR':
40 case 'TINYBLOB':
41 case 'TINYTEXT':
42 case 'ENUM':
43 case 'SET':
44 if ($len <= $this->blobSize) return 'C';
45
46 case 'TEXT':
47 case 'LONGTEXT':
48 case 'MEDIUMTEXT':
49 return 'X';
50
51 // php_mysql extension always returns 'blob' even if 'text'
52 // so we have to check whether binary...
53 case 'IMAGE':
54 case 'LONGBLOB':
55 case 'BLOB':
56 case 'MEDIUMBLOB':
57 return !empty($fieldobj->binary) ? 'B' : 'X';
58
59 case 'YEAR':
60 case 'DATE': return 'D';
61
62 case 'TIME':
63 case 'DATETIME':
64 case 'TIMESTAMP': return 'T';
65
66 case 'FLOAT':
67 case 'DOUBLE':
68 return 'F';
69
70 case 'INT':
71 case 'INTEGER': return $is_serial ? 'R' : 'I';
72 case 'TINYINT': return $is_serial ? 'R' : 'I1';
73 case 'SMALLINT': return $is_serial ? 'R' : 'I2';
74 case 'MEDIUMINT': return $is_serial ? 'R' : 'I4';
75 case 'BIGINT': return $is_serial ? 'R' : 'I8';
76 default: return 'N';
77 }
78 }
79
80 function ActualType($meta)
81 {
82 switch(strtoupper($meta)) {
83 case 'C': return 'VARCHAR';
84 case 'XL':return 'LONGTEXT';
85 case 'X': return 'TEXT';
86
87 case 'C2': return 'VARCHAR';
88 case 'X2': return 'LONGTEXT';
89
90 case 'B': return 'LONGBLOB';
91
92 case 'D': return 'DATE';
93 case 'TS':
94 case 'T': return 'DATETIME';
95 case 'L': return 'TINYINT';
96
97 case 'R':
98 case 'I4':
99 case 'I': return 'INTEGER';
100 case 'I1': return 'TINYINT';
101 case 'I2': return 'SMALLINT';
102 case 'I8': return 'BIGINT';
103
104 case 'F': return 'DOUBLE';
105 case 'N': return 'NUMERIC';
106 default:
107 return $meta;
108 }
109 }
110
111 // return string must begin with space
112 function _CreateSuffix($fname,&$ftype,$fnotnull,$fdefault,$fautoinc,$fconstraint,$funsigned)
113 {
114 $suffix = '';
115 if ($funsigned) $suffix .= ' UNSIGNED';
116 if ($fnotnull) $suffix .= ' NOT NULL';
117 if (strlen($fdefault)) $suffix .= " DEFAULT $fdefault";
118 if ($fautoinc) $suffix .= ' AUTO_INCREMENT';
119 if ($fconstraint) $suffix .= ' '.$fconstraint;
120 return $suffix;
121 }
122
123 /*
124 CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name [(create_definition,...)]
125 [table_options] [select_statement]
126 create_definition:
127 col_name type [NOT NULL | NULL] [DEFAULT default_value] [AUTO_INCREMENT]
128 [PRIMARY KEY] [reference_definition]
129 or PRIMARY KEY (index_col_name,...)
130 or KEY [index_name] (index_col_name,...)
131 or INDEX [index_name] (index_col_name,...)
132 or UNIQUE [INDEX] [index_name] (index_col_name,...)
133 or FULLTEXT [INDEX] [index_name] (index_col_name,...)
134 or [CONSTRAINT symbol] FOREIGN KEY [index_name] (index_col_name,...)
135 [reference_definition]
136 or CHECK (expr)
137 */
138
139 /*
140 CREATE [UNIQUE|FULLTEXT] INDEX index_name
141 ON tbl_name (col_name[(length)],... )
142 */
143
144 function _IndexSQL($idxname, $tabname, $flds, $idxoptions)
145 {
146 $sql = array();
147
148 if ( isset($idxoptions['REPLACE']) || isset($idxoptions['DROP']) ) {
149 if ($this->alterTableAddIndex) $sql[] = "ALTER TABLE $tabname DROP INDEX $idxname";
150 else $sql[] = sprintf($this->dropIndex, $idxname, $tabname);
151
152 if ( isset($idxoptions['DROP']) )
153 return $sql;
154 }
155
156 if ( empty ($flds) ) {
157 return $sql;
158 }
159
160 if (isset($idxoptions['FULLTEXT'])) {
161 $unique = ' FULLTEXT';
162 } elseif (isset($idxoptions['UNIQUE'])) {
163 $unique = ' UNIQUE';
164 } else {
165 $unique = '';
166 }
167
168 if ( is_array($flds) ) $flds = implode(', ',$flds);
169
170 if ($this->alterTableAddIndex) $s = "ALTER TABLE $tabname ADD $unique INDEX $idxname ";
171 else $s = 'CREATE' . $unique . ' INDEX ' . $idxname . ' ON ' . $tabname;
172
173 $s .= ' (' . $flds . ')';
174
175 if ( isset($idxoptions[$this->upperName]) )
176 $s .= $idxoptions[$this->upperName];
177
178 $sql[] = $s;
179
180 return $sql;
181 }
182 }