307986c0e08f083ccdf74f6bda72eb8928f942a4
2 /***************************************************************
5 * (c) 2004 Kasper Skaarhoj (kasperYYYY@typo3.com)
6 * (c) 2004-2006 Karsten Dambekalns <karsten@typo3.org>
9 * This script is part of the TYPO3 project. The TYPO3 project is
10 * free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * The GNU General Public License can be found at
16 * http://www.gnu.org/copyleft/gpl.html.
17 * A copy is found in the textfile GPL.txt and important notices to the license
18 * from the author is found in LICENSE.txt distributed with these scripts.
21 * This script is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * This copyright notice MUST APPEAR in all copies of the script!
27 ***************************************************************/
33 * @author Kasper Skaarhoj <kasperYYYY@typo3.com>
34 * @author Karsten Dambekalns <k.dambekalns@fishfarm.de>
39 * PHP SQL engine / server
40 * Some parts are experimental for now.
42 * @author Kasper Skaarhoj <kasper@typo3.com>
46 class ux_t3lib_sqlparser
extends t3lib_sqlparser
{
48 /*************************
52 *************************/
55 * Compiles an SQL query from components
57 * @param array Array of SQL query components
58 * @return string SQL query
61 function compileSQL($components) {
63 switch($components['type']) {
65 $query = $this->compileSELECT($components);
68 $query = $this->compileUPDATE($components);
71 $query = $this->compileINSERT($components);
74 $query = $this->compileDELETE($components);
77 $query = 'EXPLAIN '.$this->compileSELECT($components);
80 $query = $this->compileDROPTABLE($components);
83 $query = $this->compileCREATETABLE($components);
86 $query = $this->compileALTERTABLE($components);
94 function compileINSERT($components) {
95 switch((string)$GLOBALS['TYPO3_DB']->handlerCfg
[$GLOBALS['TYPO3_DB']->lastHandlerKey
]['type']) {
97 parent
::compileINSERT($components);
100 if(isset($components['VALUES_ONLY']) && is_array($components['VALUES_ONLY'])) {
101 $fields = $GLOBALS['TYPO3_DB']->cache_fieldType
[$components['TABLE']];
103 foreach($fields as $fn => $fd) {
104 $query[$fn] = $components['VALUES_ONLY'][$fc++
][0];
108 foreach($components['FIELDS'] as $fN => $fV) {
118 function compileDROPTABLE($components) {
119 switch((string)$GLOBALS['TYPO3_DB']->handlerCfg
[$GLOBALS['TYPO3_DB']->lastHandlerKey
]['type']) {
121 $query = 'DROP TABLE'.($components['ifExists']?
' IF EXISTS':'').' '.$components['TABLE'];
124 $query = $GLOBALS['TYPO3_DB']->handlerInstance
[$GLOBALS['TYPO3_DB']->handler_getFromTableList($components['TABLE'])]->DataDictionary
->DropTableSQL('`'.$components['TABLE'].'`');
132 * Compiles a CREATE TABLE statement from components array
134 * @param array Array of SQL query components
135 * @return array array with SQL CREATE TABLE/INDEX command(s)
136 * @see parseCREATETABLE()
138 function compileCREATETABLE($components) {
139 // Execute query (based on handler derived from the TABLE name which we actually know for once!)
140 switch((string)$GLOBALS['TYPO3_DB']->handlerCfg
[$GLOBALS['TYPO3_DB']->handler_getFromTableList($components['TABLE'])]['type']) {
142 $query[] = parent
::compileCREATETABLE($components);
145 // Create fields and keys:
146 $fieldsKeys = array();
147 $indexKeys = array();
149 foreach($components['FIELDS'] as $fN => $fCfg) {
150 // the backticks get converted to the correct quote char automatically
151 $fieldsKeys[$fN] = $GLOBALS['TYPO3_DB']->handlerInstance
[$GLOBALS['TYPO3_DB']->handler_getFromTableList($components['TABLE'])]->DataDictionary
->nameQuote('`'.$fN.'`').' '.$this->compileFieldCfg($fCfg['definition']);
154 if(isset($components['KEYS']) && is_array($components['KEYS'])) {
155 foreach($components['KEYS'] as $kN => $kCfg) {
156 if ($kN == 'PRIMARYKEY') {
157 foreach($kCfg as $n => $field) {
158 $fieldsKeys[$field] .= ' PRIMARY';
160 } elseif ($kN == 'UNIQUE') {
161 foreach($kCfg as $n => $field) {
162 $indexKeys = array_merge($indexKeys, $GLOBALS['TYPO3_DB']->handlerInstance
[$GLOBALS['TYPO3_DB']->handler_getFromTableList($components['TABLE'])]->DataDictionary
->CreateIndexSQL($n, $components['TABLE'], $field, array('UNIQUE')));
165 $indexKeys = array_merge($indexKeys, $GLOBALS['TYPO3_DB']->handlerInstance
[$GLOBALS['TYPO3_DB']->handler_getFromTableList($components['TABLE'])]->DataDictionary
->CreateIndexSQL($components['TABLE'].'_'.$kN, $components['TABLE'], $kCfg));
170 // Fetch table/index generation query:
171 $query = array_merge($GLOBALS['TYPO3_DB']->handlerInstance
[$GLOBALS['TYPO3_DB']->lastHandlerKey
]->DataDictionary
->CreateTableSQL('`'.$components['TABLE'].'`',implode(','.chr(10), $fieldsKeys)), $indexKeys);
178 function compileALTERTABLE($components) {
179 // Execute query (based on handler derived from the TABLE name which we actually know for once!)
180 switch((string)$GLOBALS['TYPO3_DB']->handlerCfg
[$GLOBALS['TYPO3_DB']->lastHandlerKey
]['type']) {
182 $query[] = parent
::compileALTERTABLE($components);
185 switch(strtoupper(str_replace(array(" ","\n","\r","\t"),'',$components['action']))) {
187 $query = $GLOBALS['TYPO3_DB']->handlerInstance
[$GLOBALS['TYPO3_DB']->lastHandlerKey
]->DataDictionary
->AddColumnSQL('`'.$components['TABLE'].'`','`'.$components['FIELD'].'` '.$this->compileFieldCfg($components['definition']));
190 $query = $GLOBALS['TYPO3_DB']->handlerInstance
[$GLOBALS['TYPO3_DB']->lastHandlerKey
]->DataDictionary
->AlterColumnSQL('`'.$components['TABLE'].'`','`'.$components['FIELD'].'` '.$this->compileFieldCfg($components['definition']));
196 case 'ADDPRIMARYKEY':
197 $query.=' ('.implode(',',$components['fields']).')';
207 * Compile field definition
209 * @param array Field definition parts
210 * @return string Field definition string
212 function compileFieldCfg($fieldCfg) {
214 switch((string)$GLOBALS['TYPO3_DB']->handlerCfg
[$GLOBALS['TYPO3_DB']->lastHandlerKey
]['type']) {
216 $cfg = parent
::compileFieldCfg($fieldCfg);
220 $cfg = $GLOBALS['TYPO3_DB']->MySQLMetaType($fieldCfg['fieldType']);
222 // Add value, if any:
223 if (strlen($fieldCfg['value']) && (in_array($cfg, array('C','C2')))) {
224 $cfg .= ' '.$fieldCfg['value'];
225 } elseif (!isset($fieldCfg['value']) && (in_array($cfg, array('C','C2')))) {
226 $cfg .= ' 255'; // add 255 as length for varchar without specified length (e.g. coming from tinytext, tinyblob)
229 // Add additional features:
230 if (is_array($fieldCfg['featureIndex'])) {
232 // MySQL assigns DEFAULT value automatically if NOT NULL, fake this here
233 if(isset($fieldCfg['featureIndex']['NOTNULL']) && !isset($fieldCfg['featureIndex']['DEFAULT']) && !isset($fieldCfg['featureIndex']['AUTO_INCREMENT'])) {
234 $fieldCfg['featureIndex']['DEFAULT'] = array('keyword' => 'DEFAULT', 'value' => array('','\''));
237 foreach($fieldCfg['featureIndex'] as $feature => $featureDef) {
239 // unsigned only for mysql, as it is mysql specific
240 case ($feature == 'UNSIGNED' && !$GLOBALS['TYPO3_DB']->runningNative()) :
241 // auto_increment is removed, it is handled by (emulated) sequences
242 case ($feature == 'AUTO_INCREMENT') :
243 // never add NOT NULL as it is useless in TYPO3 and breaks most databases other than MySQL
244 case ($feature == 'NOTNULL') :
248 $cfg.=' '.$featureDef['keyword'];
250 // Add value if found:
251 if (is_array($featureDef['value'])) {
252 if(!is_numeric($featureDef['value'][0]) && empty($featureDef['value'][0])) {
255 $cfg.=' '.$featureDef['value'][1].$this->compileAddslashes($featureDef['value'][0]).$featureDef['value'][1];
264 // Return field definition string:
268 function checkEmptyDefaultValue($featureIndex) {
269 if (is_array($featureIndex['DEFAULT']['value'])) {
270 if(!is_numeric($featureIndex['DEFAULT']['value'][0]) && empty($featureIndex['DEFAULT']['value'][0])) {
281 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE
]['XCLASS']['ext/dbal/class.ux_t3lib_sqlparser.php']) {
282 include_once($TYPO3_CONF_VARS[TYPO3_MODE
]['XCLASS']['ext/dbal/class.ux_t3lib_sqlparser.php']);