00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00046 class ux_t3lib_sqlengine extends t3lib_sqlengine {
00047
00048
00049
00050
00051
00052
00053
00061 function compileSQL($components) {
00062
00063 switch($components['type']) {
00064 case 'SELECT':
00065 $query = $this->compileSELECT($components);
00066 break;
00067 case 'UPDATE':
00068 $query = $this->compileUPDATE($components);
00069 break;
00070 case 'INSERT':
00071 $query = $this->compileINSERT($components);
00072 break;
00073 case 'DELETE':
00074 $query = $this->compileDELETE($components);
00075 break;
00076 case 'EXPLAIN':
00077 $query = 'EXPLAIN '.$this->compileSELECT($components);
00078 break;
00079 case 'DROPTABLE':
00080 $query = $this->compileDROPTABLE($components);
00081 break;
00082 case 'CREATETABLE':
00083 $query = $this->compileCREATETABLE($components);
00084 break;
00085 case 'ALTERTABLE':
00086 $query = $this->compileALTERTABLE($components);
00087 break;
00088 }
00089
00090 return $query;
00091 }
00092
00093
00094 function compileINSERT($components) {
00095 switch((string)$GLOBALS['TYPO3_DB']->handlerCfg[$GLOBALS['TYPO3_DB']->lastHandlerKey]['type']) {
00096 case 'native':
00097 $query = parent::compileINSERT($components);
00098 break;
00099 case 'adodb':
00100 if(isset($components['VALUES_ONLY']) && is_array($components['VALUES_ONLY'])) {
00101 $fields = $GLOBALS['TYPO3_DB']->cache_fieldType[$components['TABLE']];
00102 $fc = 0;
00103 foreach($fields as $fn => $fd) {
00104 $query[$fn] = $components['VALUES_ONLY'][$fc++][0];
00105 }
00106 } else {
00107
00108 foreach($components['FIELDS'] as $fN => $fV) {
00109 $query[$fN]=$fV[0];
00110 }
00111 }
00112 break;
00113 }
00114
00115 return $query;
00116 }
00117
00118 function compileDROPTABLE($components) {
00119 switch((string)$GLOBALS['TYPO3_DB']->handlerCfg[$GLOBALS['TYPO3_DB']->lastHandlerKey]['type']) {
00120 case 'native':
00121 $query = 'DROP TABLE'.($components['ifExists']?' IF EXISTS':'').' '.$components['TABLE'];
00122 break;
00123 case 'adodb':
00124 $query = $GLOBALS['TYPO3_DB']->handlerInstance[$GLOBALS['TYPO3_DB']->handler_getFromTableList($components['TABLE'])]->DataDictionary->DropTableSQL('`'.$components['TABLE'].'`');
00125 break;
00126 }
00127
00128 return $query;
00129 }
00130
00138 function compileCREATETABLE($components) {
00139
00140 switch((string)$GLOBALS['TYPO3_DB']->handlerCfg[$GLOBALS['TYPO3_DB']->handler_getFromTableList($components['TABLE'])]['type']) {
00141 case 'native':
00142 $query[] = parent::compileCREATETABLE($components);
00143 break;
00144 case 'adodb':
00145
00146 $fieldsKeys = array();
00147 $indexKeys = array();
00148
00149 foreach($components['FIELDS'] as $fN => $fCfg) {
00150
00151 $fieldsKeys[$fN] = '`'.$fN.'` '.$this->compileFieldCfg($fCfg['definition']);
00152 }
00153
00154 if(isset($components['KEYS']) && is_array($components['KEYS'])) {
00155 foreach($components['KEYS'] as $kN => $kCfg) {
00156 if ($kN == 'PRIMARYKEY') {
00157 foreach($kCfg as $n => $field) {
00158 $fieldsKeys[$field] .= ' PRIMARY';
00159 }
00160 } elseif ($kN == 'UNIQUE') {
00161 foreach($kCfg as $n => $field) {
00162 $indexKeys = array_merge($indexKeys, $GLOBALS['TYPO3_DB']->handlerInstance[$GLOBALS['TYPO3_DB']->handler_getFromTableList($components['TABLE'])]->DataDictionary->CreateIndexSQL($n, $components['TABLE'], $field, array('UNIQUE')));
00163 }
00164 } else {
00165 $indexKeys = array_merge($indexKeys, $GLOBALS['TYPO3_DB']->handlerInstance[$GLOBALS['TYPO3_DB']->handler_getFromTableList($components['TABLE'])]->DataDictionary->CreateIndexSQL($components['TABLE'].'_'.$kN, $components['TABLE'], $kCfg));
00166 }
00167 }
00168 }
00169
00170
00171 $query = array_merge($GLOBALS['TYPO3_DB']->handlerInstance[$GLOBALS['TYPO3_DB']->lastHandlerKey]->DataDictionary->CreateTableSQL('`'.$components['TABLE'].'`',implode(','.chr(10), $fieldsKeys)), $indexKeys);
00172 break;
00173 }
00174
00175 return $query;
00176 }
00177
00178 function compileALTERTABLE($components) {
00179
00180 switch((string)$GLOBALS['TYPO3_DB']->handlerCfg[$GLOBALS['TYPO3_DB']->lastHandlerKey]['type']) {
00181 case 'native':
00182 $query[] = parent::compileALTERTABLE($components);
00183 break;
00184 case 'adodb':
00185 switch(strtoupper(str_replace(array(" ","\n","\r","\t"),'',$components['action']))) {
00186 case 'ADD':
00187 $query = $GLOBALS['TYPO3_DB']->handlerInstance[$GLOBALS['TYPO3_DB']->lastHandlerKey]->DataDictionary->AddColumnSQL('`'.$components['TABLE'].'`','`'.$components['FIELD'].'` '.$this->compileFieldCfg($components['definition']));
00188 break;
00189 case 'CHANGE':
00190 $query = $GLOBALS['TYPO3_DB']->handlerInstance[$GLOBALS['TYPO3_DB']->lastHandlerKey]->DataDictionary->AlterColumnSQL('`'.$components['TABLE'].'`','`'.$components['FIELD'].'` '.$this->compileFieldCfg($components['definition']));
00191 break;
00192 case 'DROP':
00193 case 'DROPKEY':
00194 break;
00195 case 'ADDKEY':
00196 case 'ADDPRIMARYKEY':
00197 $query.=' ('.implode(',',$components['fields']).')';
00198 break;
00199 }
00200 break;
00201 }
00202
00203 return $query;
00204 }
00205
00212 function compileFieldCfg($fieldCfg) {
00213
00214 switch((string)$GLOBALS['TYPO3_DB']->handlerCfg[$GLOBALS['TYPO3_DB']->lastHandlerKey]['type']) {
00215 case 'native':
00216 $cfg = parent::compileFieldCfg($fieldCfg);
00217 break;
00218 case 'adodb':
00219
00220 $type = $GLOBALS['TYPO3_DB']->MySQLMetaType($fieldCfg['fieldType']);
00221 $cfg = $type;
00222
00223
00224 if (strlen($fieldCfg['value']) && (in_array($type, array('C','C2')))) {
00225 $cfg .= ' '.$fieldCfg['value'];
00226 } elseif (!isset($fieldCfg['value']) && (in_array($type, array('C','C2')))) {
00227 $cfg .= ' 255';
00228 }
00229
00230
00231 if (is_array($fieldCfg['featureIndex'])) {
00232
00233
00234
00235 if(isset($fieldCfg['featureIndex']['NOTNULL']) && !isset($fieldCfg['featureIndex']['DEFAULT']) && !isset($fieldCfg['featureIndex']['AUTO_INCREMENT'])) {
00236 switch($type) {
00237 case 'I8':
00238 case 'F':
00239 case 'N':
00240 $fieldCfg['featureIndex']['DEFAULT'] = array('keyword' => 'DEFAULT', 'value' => array('0',''));
00241 break;
00242 default:
00243 $fieldCfg['featureIndex']['DEFAULT'] = array('keyword' => 'DEFAULT', 'value' => array('','\''));
00244 }
00245 }
00246
00247 foreach($fieldCfg['featureIndex'] as $feature => $featureDef) {
00248 switch(true) {
00249
00250 case ($feature == 'UNSIGNED' && !$GLOBALS['TYPO3_DB']->runningADOdbDriver('mysql')) :
00251
00252 case ($feature == 'AUTO_INCREMENT') :
00253
00254 case ($feature == 'NOTNULL' && $GLOBALS['TYPO3_DB']->runningADOdbDriver('oci8')) :
00255 continue;
00256 case ($feature == 'NOTNULL') :
00257 $cfg.=' NOTNULL';
00258 break;
00259 default :
00260 $cfg.=' '.$featureDef['keyword'];
00261 }
00262
00263
00264 if (is_array($featureDef['value'])) {
00265 if($featureDef['value'][0]==='') {
00266 $cfg .= ' "\'\'"';
00267 } else {
00268 $cfg.=' '.$featureDef['value'][1].$this->compileAddslashes($featureDef['value'][0]).$featureDef['value'][1];
00269 }
00270 }
00271 }
00272 }
00273 $cfg .= ' NOQUOTE';
00274 break;
00275 }
00276
00277
00278 return $cfg;
00279 }
00280
00281 function checkEmptyDefaultValue($featureIndex) {
00282 if (is_array($featureIndex['DEFAULT']['value'])) {
00283 if(!is_numeric($featureIndex['DEFAULT']['value'][0]) && empty($featureIndex['DEFAULT']['value'][0])) {
00284 return true;
00285 } else {
00286 return false;
00287 }
00288 }
00289 return true;
00290 }
00291 }
00292
00293
00294 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/dbal/class.ux_t3lib_sqlengine.php']) {
00295 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/dbal/class.ux_t3lib_sqlengine.php']);
00296 }
00297 ?>