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