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
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116 class ux_t3lib_DB extends t3lib_DB {
00117
00118
00119 var $printErrors = FALSE;
00120 var $debug = FALSE;
00121 var $conf = array();
00122
00123 var $mapping = array();
00124 var $table2handlerKeys = array();
00125 var $handlerCfg = array(
00126 '_DEFAULT' => array(
00127 'type' => 'native',
00128 'config' => array(
00129 'username' => '',
00130 'password' => '',
00131 'host' => '',
00132 'database' => '',
00133 'driver' => '',
00134 'sequenceStart' => 1,
00135 'useNameQuote' => 0
00136 )
00137 ),
00138 );
00139
00140
00141
00142 var $handlerInstance = array();
00143 var $lastHandlerKey = '';
00144 var $lastQuery = '';
00145 var $lastParsedAndMappedQueryArray = array();
00146
00147 var $resourceIdToTableNameMap = array();
00148
00149
00150 var $cache_handlerKeyFromTableList = array();
00151 var $cache_mappingFromTableList = array();
00152 var $cache_autoIncFields = array();
00153 var $cache_fieldType = array();
00154 var $cache_primaryKeys = array();
00155
00156
00157
00158
00159
00160
00161 var $SQLparser;
00162
00163
00164
00165
00166
00167
00168 var $Installer;
00169
00170
00171
00172
00173
00174
00175 public function __construct() {
00176
00177 $this->SQLparser = t3lib_div::makeInstance('tx_dbal_sqlengine');
00178 $this->Installer = t3lib_div::makeInstance('t3lib_install');
00179
00180
00181 $this->conf = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['dbal'];
00182 $this->initInternalVariables();
00183 }
00184
00185
00186
00187
00188
00189
00190 protected function initInternalVariables() {
00191
00192 if (isset($this->conf['mapping'])) {
00193 $this->mapping = $this->conf['mapping'];
00194 }
00195 if (isset($this->conf['table2handlerKeys'])) {
00196 $this->table2handlerKeys = $this->conf['table2handlerKeys'];
00197 }
00198 if (isset($this->conf['handlerCfg'])) {
00199 $this->handlerCfg = $this->conf['handlerCfg'];
00200 }
00201
00202 $this->cacheFieldInfo();
00203
00204 $this->printErrors = $this->conf['debugOptions']['printErrors'] ? TRUE : FALSE;
00205 $this->debug = $this->conf['debugOptions']['enabled'] ? TRUE : FALSE;
00206 }
00207
00208
00209
00210
00211
00212
00213 public function clearCachedFieldInfo() {
00214 if (file_exists(PATH_typo3conf . 'temp_fieldInfo.php')) {
00215 unlink(PATH_typo3conf . 'temp_fieldInfo.php');
00216 }
00217 }
00218
00219
00220
00221
00222
00223
00224 public function cacheFieldInfo() {
00225 $extSQL = '';
00226 $parsedExtSQL = array();
00227
00228
00229
00230 if (file_exists(PATH_typo3conf . 'temp_fieldInfo.php')) {
00231 $fdata = unserialize(t3lib_div::getUrl(PATH_typo3conf . 'temp_fieldInfo.php'));
00232 $this->cache_autoIncFields = $fdata['incFields'];
00233 $this->cache_fieldType = $fdata['fieldTypes'];
00234 $this->cache_primaryKeys = $fdata['primaryKeys'];
00235 } else {
00236
00237 $extSQL = t3lib_div::getUrl(PATH_site . 't3lib/stddb/tables.sql');
00238 $parsedExtSQL = $this->Installer->getFieldDefinitions_fileContent($extSQL);
00239 $this->analyzeFields($parsedExtSQL);
00240
00241
00242 foreach ($GLOBALS['TYPO3_LOADED_EXT'] as $ext => $v) {
00243 if (!is_array($v) || !isset($v['ext_tables.sql'])) {
00244 continue;
00245 }
00246
00247
00248 $extSQL = t3lib_div::getUrl($v['ext_tables.sql']);
00249 $parsedExtSQL = $this->Installer->getFieldDefinitions_fileContent($extSQL);
00250 $this->analyzeFields($parsedExtSQL);
00251 }
00252
00253 $cachedFieldInfo = array('incFields' => $this->cache_autoIncFields, 'fieldTypes' => $this->cache_fieldType, 'primaryKeys' => $this->cache_primaryKeys);
00254 $cachedFieldInfo = serialize($this->mapCachedFieldInfo($cachedFieldInfo));
00255
00256
00257 t3lib_div::writeFile(PATH_typo3conf . 'temp_fieldInfo.php', $cachedFieldInfo);
00258
00259 if (strcmp(t3lib_div::getUrl(PATH_typo3conf . 'temp_fieldInfo.php'), $cachedFieldInfo)) {
00260 die('typo3temp/temp_incfields.php was NOT updated properly (written content didn\'t match file content) - maybe write access problem?');
00261 }
00262 }
00263 }
00264
00265
00266
00267
00268
00269
00270
00271
00272 protected function analyzeFields($parsedExtSQL) {
00273 foreach ($parsedExtSQL as $table => $tdef) {
00274 if (is_array($tdef['fields'])) {
00275 foreach ($tdef['fields'] as $field => $fdef) {
00276 $fdef = $this->SQLparser->parseFieldDef($fdef);
00277 $this->cache_fieldType[$table][$field]['type'] = $fdef['fieldType'];
00278 $this->cache_fieldType[$table][$field]['metaType'] = $this->MySQLMetaType($fdef['fieldType']);
00279 $this->cache_fieldType[$table][$field]['notnull'] = (isset($fdef['featureIndex']['NOTNULL']) && !$this->SQLparser->checkEmptyDefaultValue($fdef['featureIndex'])) ? 1 : 0;
00280 if (isset($fdef['featureIndex']['DEFAULT'])) {
00281 $default = $fdef['featureIndex']['DEFAULT']['value'][0];
00282 if (isset($fdef['featureIndex']['DEFAULT']['value'][1])) {
00283 $default = $fdef['featureIndex']['DEFAULT']['value'][1] . $default . $fdef['featureIndex']['DEFAULT']['value'][1];
00284 }
00285 $this->cache_fieldType[$table][$field]['default'] = $default;
00286 }
00287 if (isset($fdef['featureIndex']['AUTO_INCREMENT'])) {
00288 $this->cache_autoIncFields[$table] = $field;
00289 }
00290 if (isset($tdef['keys']['PRIMARY'])) {
00291 $this->cache_primaryKeys[$table] = substr($tdef['keys']['PRIMARY'], 13, -1);
00292 }
00293 }
00294 }
00295 }
00296 }
00297
00298
00299
00300
00301
00302 protected function mapCachedFieldInfo($fieldInfo) {
00303 if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['dbal']['mapping'])) {
00304 foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['dbal']['mapping'] as $mappedTable => $mappedConf) {
00305 if (array_key_exists($mappedTable, $fieldInfo['incFields'])) {
00306 $mappedTableAlias = $mappedConf['mapTableName'];
00307 if (isset($mappedConf['mapFieldNames'][$fieldInfo['incFields'][$mappedTable]])) {
00308 $fieldInfo['incFields'][$mappedTableAlias] = $mappedConf['mapFieldNames'][$fieldInfo['incFields'][$mappedTable]];
00309 } else {
00310 $fieldInfo['incFields'][$mappedTableAlias] = $fieldInfo['incFields'][$mappedTable];
00311 }
00312 }
00313
00314 if (array_key_exists($mappedTable, $fieldInfo['fieldTypes'])) {
00315 foreach ($fieldInfo['fieldTypes'][$mappedTable] as $field => $fieldConf) {
00316 $tempMappedFieldConf[$mappedConf['mapFieldNames'][$field]] = $fieldConf;
00317 }
00318
00319 $fieldInfo['fieldTypes'][$mappedConf['mapTableName']] = $tempMappedFieldConf;
00320 }
00321
00322 if (array_key_exists($mappedTable, $fieldInfo['primaryKeys'])) {
00323 $mappedTableAlias = $mappedConf['mapTableName'];
00324 if (isset($mappedConf['mapFieldNames'][$fieldInfo['primaryKeys'][$mappedTable]])) {
00325 $fieldInfo['primaryKeys'][$mappedTableAlias] = $mappedConf['mapFieldNames'][$fieldInfo['primaryKeys'][$mappedTable]];
00326 } else {
00327 $fieldInfo['primaryKeys'][$mappedTableAlias] = $fieldInfo['primaryKeys'][$mappedTable];
00328 }
00329 }
00330 }
00331 }
00332
00333 return $fieldInfo;
00334 }
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365 public function exec_INSERTquery($table, $fields_values, $no_quote_fields = '') {
00366
00367 if ($this->debug) {
00368 $pt = t3lib_div::milliseconds();
00369 }
00370
00371
00372 $ORIG_tableName = $table;
00373 if ($tableArray = $this->map_needMapping($table)) {
00374
00375
00376 $fields_values = $this->map_assocArray($fields_values, $tableArray);
00377
00378
00379 if ($this->mapping[$table]['mapTableName']) {
00380 $table = $this->mapping[$table]['mapTableName'];
00381 }
00382 }
00383
00384 $this->lastHandlerKey = $this->handler_getFromTableList($ORIG_tableName);
00385 switch ((string)$this->handlerCfg[$this->lastHandlerKey]['type']) {
00386 case 'native':
00387 $this->lastQuery = $this->INSERTquery($table,$fields_values,$no_quote_fields);
00388 if (is_string($this->lastQuery)) {
00389 $sqlResult = mysql_query($this->lastQuery, $this->handlerInstance[$this->lastHandlerKey]['link']);
00390 } else {
00391 $sqlResult = mysql_query($this->lastQuery[0], $this->handlerInstance[$this->lastHandlerKey]['link']);
00392 foreach ($this->lastQuery[1] as $field => $content) {
00393 mysql_query('UPDATE ' . $this->quoteFromTables($table) . ' SET ' . $this->quoteFromTables($field) . '=' . $this->fullQuoteStr($content, $table) . ' WHERE ' . $this->quoteWhereClause($where), $this->handlerInstance[$this->lastHandlerKey]['link']);
00394 }
00395 }
00396 break;
00397 case 'adodb':
00398
00399
00400 if (isset($this->cache_autoIncFields[$table])) {
00401 if (isset($fields_values[$this->cache_autoIncFields[$table]])) {
00402 $new_id = $fields_values[$this->cache_autoIncFields[$table]];
00403 if ($table != 'tx_dbal_debuglog') {
00404 $this->handlerInstance[$this->lastHandlerKey]->last_insert_id = $new_id;
00405 }
00406 } else {
00407 $new_id = $this->handlerInstance[$this->lastHandlerKey]->GenID($table.'_'.$this->cache_autoIncFields[$table], $this->handlerInstance[$this->lastHandlerKey]->sequenceStart);
00408 $fields_values[$this->cache_autoIncFields[$table]] = $new_id;
00409 if ($table != 'tx_dbal_debuglog') {
00410 $this->handlerInstance[$this->lastHandlerKey]->last_insert_id = $new_id;
00411 }
00412 }
00413 }
00414
00415 $this->lastQuery = $this->INSERTquery($table,$fields_values,$no_quote_fields);
00416 if (is_string($this->lastQuery)) {
00417 $sqlResult = $this->handlerInstance[$this->lastHandlerKey]->_query($this->lastQuery,FALSE);
00418 } else {
00419 $this->handlerInstance[$this->lastHandlerKey]->StartTrans();
00420 if (strlen($this->lastQuery[0])) {
00421 $sqlResult = $this->handlerInstance[$this->lastHandlerKey]->_query($this->lastQuery[0],FALSE);
00422 }
00423 if (is_array($this->lastQuery[1])) {
00424 foreach ($this->lastQuery[1] as $field => $content) {
00425 if (empty($content)) continue;
00426
00427 if (isset($this->cache_autoIncFields[$table]) && isset($new_id)) {
00428 $this->handlerInstance[$this->lastHandlerKey]->UpdateBlob($this->quoteFromTables($table),$field,$content,$this->quoteWhereClause($this->cache_autoIncFields[$table].'='.$new_id));
00429 } elseif (isset($this->cache_primaryKeys[$table])) {
00430 $where = '';
00431 $pks = explode(',', $this->cache_primaryKeys[$table]);
00432 foreach ($pks as $pk) {
00433 if (isset($fields_values[$pk]))
00434 $where .= $pk.'='.$this->fullQuoteStr($fields_values[$pk], $table).' AND ';
00435 }
00436 $where = $this->quoteWhereClause($where.'1=1');
00437 $this->handlerInstance[$this->lastHandlerKey]->UpdateBlob($this->quoteFromTables($table),$field,$content,$where);
00438 } else {
00439 $this->handlerInstance[$this->lastHandlerKey]->CompleteTrans(FALSE);
00440 die('Could not update BLOB >>>> no WHERE clause found!');
00441 }
00442 }
00443 }
00444 if (is_array($this->lastQuery[2])) {
00445 foreach ($this->lastQuery[2] as $field => $content) {
00446 if (empty($content)) continue;
00447
00448 if (isset($this->cache_autoIncFields[$table]) && isset($new_id)) {
00449 $this->handlerInstance[$this->lastHandlerKey]->UpdateClob($this->quoteFromTables($table),$field,$content,$this->quoteWhereClause($this->cache_autoIncFields[$table].'='.$new_id));
00450 } elseif (isset($this->cache_primaryKeys[$table])) {
00451 $where = '';
00452 $pks = explode(',', $this->cache_primaryKeys[$table]);
00453 foreach ($pks as $pk) {
00454 if (isset($fields_values[$pk]))
00455 $where .= $pk.'='.$this->fullQuoteStr($fields_values[$pk], $table).' AND ';
00456 }
00457 $where = $this->quoteWhereClause($where.'1=1');
00458 $this->handlerInstance[$this->lastHandlerKey]->UpdateClob($this->quoteFromTables($table),$field,$content,$where);
00459 } else {
00460 $this->handlerInstance[$this->lastHandlerKey]->CompleteTrans(FALSE);
00461 die('Could not update CLOB >>>> no WHERE clause found!');
00462 }
00463 }
00464 }
00465 $this->handlerInstance[$this->lastHandlerKey]->CompleteTrans();
00466 }
00467 break;
00468 case 'userdefined':
00469 $sqlResult = $this->handlerInstance[$this->lastHandlerKey]->exec_INSERTquery($table,$fields_values,$no_quote_fields);
00470 break;
00471 }
00472
00473 if ($this->printErrors && $this->sql_error()) {
00474 debug(array($this->lastQuery, $this->sql_error()));
00475 }
00476
00477 if ($this->debug) {
00478 $this->debugHandler(
00479 'exec_INSERTquery',
00480 t3lib_div::milliseconds()-$pt,
00481 array(
00482 'handlerType' => $hType,
00483 'args' => array($table,$fields_values),
00484 'ORIG_tablename' => $ORIG_tableName
00485 )
00486 );
00487 }
00488
00489 return $sqlResult;
00490 }
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502 public function exec_INSERTmultipleRows($table, array $fields, array $rows, $no_quote_fields = FALSE) {
00503 if ((string)$this->handlerCfg[$this->lastHandlerKey]['type'] === 'native') {
00504 return parent::exec_INSERTmultipleRows($table, $fields, $rows, $no_quote_fields);
00505 }
00506
00507 foreach ($rows as $row) {
00508 $fields_values = array();
00509 foreach ($fields as $key => $value) {
00510 $fields_values[$value] = $row[$key];
00511 }
00512 $res = $this->exec_INSERTquery($table, $fields_values, $no_quote_fields);
00513 }
00514
00515 return $res;
00516 }
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527 public function exec_UPDATEquery($table,$where,$fields_values,$no_quote_fields = '') {
00528 if ($this->debug) {
00529 $pt = t3lib_div::milliseconds();
00530 }
00531
00532
00533 $ORIG_tableName = $table;
00534 if ($tableArray = $this->map_needMapping($table)) {
00535
00536
00537 $fields_values = $this->map_assocArray($fields_values,$tableArray);
00538
00539
00540 $whereParts = $this->SQLparser->parseWhereClause($where);
00541 $this->map_sqlParts($whereParts,$tableArray[0]['table']);
00542 $where = $this->SQLparser->compileWhereClause($whereParts, FALSE);
00543
00544
00545 if ($this->mapping[$table]['mapTableName']) {
00546 $table = $this->mapping[$table]['mapTableName'];
00547 }
00548 }
00549
00550
00551 $this->lastHandlerKey = $this->handler_getFromTableList($ORIG_tableName);
00552 switch ((string)$this->handlerCfg[$this->lastHandlerKey]['type']) {
00553 case 'native':
00554 $this->lastQuery = $this->UPDATEquery($table,$where,$fields_values,$no_quote_fields);
00555 if (is_string($this->lastQuery)) {
00556 $sqlResult = mysql_query($this->lastQuery, $this->handlerInstance[$this->lastHandlerKey]['link']);
00557 }
00558 else {
00559 $sqlResult = mysql_query($this->lastQuery[0], $this->handlerInstance[$this->lastHandlerKey]['link']);
00560 foreach ($this->lastQuery[1] as $field => $content) {
00561 mysql_query('UPDATE '.$this->quoteFromTables($table).' SET '.$this->quoteFromTables($field).'='.$this->fullQuoteStr($content,$table).' WHERE '.$this->quoteWhereClause($where), $this->handlerInstance[$this->lastHandlerKey]['link']);
00562 }
00563 }
00564 break;
00565 case 'adodb':
00566 $this->lastQuery = $this->UPDATEquery($table,$where,$fields_values,$no_quote_fields);
00567 if (is_string($this->lastQuery)) {
00568 $sqlResult = $this->handlerInstance[$this->lastHandlerKey]->_query($this->lastQuery,FALSE);
00569 } else {
00570 $this->handlerInstance[$this->lastHandlerKey]->StartTrans();
00571 if (strlen($this->lastQuery[0])) {
00572 $sqlResult = $this->handlerInstance[$this->lastHandlerKey]->_query($this->lastQuery[0],FALSE);
00573 }
00574 if (is_array($this->lastQuery[1])) {
00575 foreach ($this->lastQuery[1] as $field => $content) {
00576 $this->handlerInstance[$this->lastHandlerKey]->UpdateBlob($this->quoteFromTables($table),$field,$content,$this->quoteWhereClause($where));
00577 }
00578 }
00579 if (is_array($this->lastQuery[2])) {
00580 foreach ($this->lastQuery[2] as $field => $content) {
00581 $this->handlerInstance[$this->lastHandlerKey]->UpdateClob($this->quoteFromTables($table),$field,$content,$this->quoteWhereClause($where));
00582 }
00583 }
00584 $this->handlerInstance[$this->lastHandlerKey]->CompleteTrans();
00585 }
00586 break;
00587 case 'userdefined':
00588 $sqlResult = $this->handlerInstance[$this->lastHandlerKey]->exec_UPDATEquery($table,$where,$fields_values,$no_quote_fields);
00589 break;
00590 }
00591
00592 if ($this->printErrors && $this->sql_error()) {
00593 debug(array($this->lastQuery, $this->sql_error()));
00594 }
00595
00596 if ($this->debug) {
00597 $this->debugHandler(
00598 'exec_UPDATEquery',
00599 t3lib_div::milliseconds()-$pt,
00600 array(
00601 'handlerType' => $hType,
00602 'args' => array($table,$where, $fields_values),
00603 'ORIG_from_table' => $ORIG_tableName
00604 )
00605 );
00606 }
00607
00608
00609 return $sqlResult;
00610 }
00611
00612
00613
00614
00615
00616
00617
00618
00619 public function exec_DELETEquery($table, $where) {
00620 if ($this->debug) {
00621 $pt = t3lib_div::milliseconds();
00622 }
00623
00624
00625 $ORIG_tableName = $table;
00626 if ($tableArray = $this->map_needMapping($table)) {
00627
00628
00629 $whereParts = $this->SQLparser->parseWhereClause($where);
00630 $this->map_sqlParts($whereParts,$tableArray[0]['table']);
00631 $where = $this->SQLparser->compileWhereClause($whereParts, FALSE);
00632
00633
00634 if ($this->mapping[$table]['mapTableName']) {
00635 $table = $this->mapping[$table]['mapTableName'];
00636 }
00637 }
00638
00639
00640 $this->lastHandlerKey = $this->handler_getFromTableList($ORIG_tableName);
00641 switch ((string)$this->handlerCfg[$this->lastHandlerKey]['type']) {
00642 case 'native':
00643 $this->lastQuery = $this->DELETEquery($table,$where);
00644 $sqlResult = mysql_query($this->lastQuery, $this->handlerInstance[$this->lastHandlerKey]['link']);
00645 break;
00646 case 'adodb':
00647 $this->lastQuery = $this->DELETEquery($table,$where);
00648 $sqlResult = $this->handlerInstance[$this->lastHandlerKey]->_query($this->lastQuery,FALSE);
00649 break;
00650 case 'userdefined':
00651 $sqlResult = $this->handlerInstance[$this->lastHandlerKey]->exec_DELETEquery($table,$where);
00652 break;
00653 }
00654
00655 if ($this->printErrors && $this->sql_error()) {
00656 debug(array($this->lastQuery, $this->sql_error()));
00657 }
00658
00659 if ($this->debug) {
00660 $this->debugHandler(
00661 'exec_DELETEquery',
00662 t3lib_div::milliseconds()-$pt,
00663 array(
00664 'handlerType' => $hType,
00665 'args' => array($table,$where),
00666 'ORIG_from_table' => $ORIG_tableName
00667 )
00668 );
00669 }
00670
00671
00672 return $sqlResult;
00673 }
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686 public function exec_SELECTquery($select_fields, $from_table, $where_clause, $groupBy = '', $orderBy = '', $limit = '') {
00687 if ($this->debug) {
00688 $pt = t3lib_div::milliseconds();
00689 }
00690
00691
00692 $ORIG_tableName = $from_table;
00693 if ($tableArray = $this->map_needMapping($ORIG_tableName)) {
00694 $this->map_remapSELECTQueryParts($select_fields,$from_table,$where_clause,$groupBy,$orderBy);
00695 }
00696
00697
00698 $this->lastHandlerKey = $this->handler_getFromTableList($ORIG_tableName);
00699 $hType = (string)$this->handlerCfg[$this->lastHandlerKey]['type'];
00700 switch ($hType) {
00701 case 'native':
00702 $this->lastQuery = $this->SELECTquery($select_fields,$from_table,$where_clause,$groupBy,$orderBy,$limit);
00703 $sqlResult = mysql_query($this->lastQuery, $this->handlerInstance[$this->lastHandlerKey]['link']);
00704 $this->resourceIdToTableNameMap[(string)$sqlResult] = $ORIG_tableName;
00705 break;
00706 case 'adodb':
00707 if ($limit != '') {
00708 $splitLimit = t3lib_div::intExplode(',', $limit);
00709 if ($splitLimit[1]) {
00710 $numrows = $splitLimit[1];
00711 $offset = $splitLimit[0];
00712 } else {
00713 $numrows = $splitLimit[0];
00714 $offset = 0;
00715 }
00716
00717 $sqlResult = $this->handlerInstance[$this->lastHandlerKey]->SelectLimit($this->SELECTquery($select_fields,$from_table,$where_clause,$groupBy,$orderBy), $numrows, $offset);
00718 $this->lastQuery = $sqlResult->sql;
00719 } else {
00720 $this->lastQuery = $this->SELECTquery($select_fields,$from_table,$where_clause,$groupBy,$orderBy);
00721 $sqlResult = $this->handlerInstance[$this->lastHandlerKey]->_Execute($this->lastQuery);
00722 }
00723
00724 $sqlResult->TYPO3_DBAL_handlerType = 'adodb';
00725 $sqlResult->TYPO3_DBAL_tableList = $ORIG_tableName;
00726 break;
00727 case 'userdefined':
00728 $sqlResult = $this->handlerInstance[$this->lastHandlerKey]->exec_SELECTquery($select_fields,$from_table,$where_clause,$groupBy,$orderBy,$limit);
00729 if (is_object($sqlResult)) {
00730 $sqlResult->TYPO3_DBAL_handlerType = 'userdefined';
00731 $sqlResult->TYPO3_DBAL_tableList = $ORIG_tableName;
00732 }
00733 break;
00734 }
00735
00736 if ($this->printErrors && $this->sql_error()) {
00737 debug(array($this->lastQuery, $this->sql_error()));
00738 }
00739
00740 if ($this->debug) {
00741 $this->debugHandler(
00742 'exec_SELECTquery',
00743 t3lib_div::milliseconds()-$pt,
00744 array(
00745 'handlerType' => $hType,
00746 'args' => array($from_table,$select_fields,$where_clause,$groupBy,$orderBy,$limit),
00747 'ORIG_from_table' => $ORIG_tableName
00748 )
00749 );
00750 }
00751
00752
00753 return $sqlResult;
00754 }
00755
00756
00757
00758
00759
00760
00761
00762 public function exec_TRUNCATEquery($table) {
00763 if ($this->debug) {
00764 $pt = t3lib_div::milliseconds();
00765 }
00766
00767
00768 $ORIG_tableName = $table;
00769 if ($tableArray = $this->map_needMapping($table)) {
00770
00771 if ($this->mapping[$table]['mapTableName']) {
00772 $table = $this->mapping[$table]['mapTableName'];
00773 }
00774 }
00775
00776
00777 $this->lastHandlerKey = $this->handler_getFromTableList($ORIG_tableName);
00778 switch ((string)$this->handlerCfg[$this->lastHandlerKey]['type']) {
00779 case 'native':
00780 $this->lastQuery = $this->TRUNCATEquery($table);
00781 $sqlResult = mysql_query($this->lastQuery, $this->handlerInstance[$this->lastHandlerKey]['link']);
00782 break;
00783 case 'adodb':
00784 $this->lastQuery = $this->TRUNCATEquery($table);
00785 $sqlResult = $this->handlerInstance[$this->lastHandlerKey]->_query($this->lastQuery, FALSE);
00786 break;
00787 case 'userdefined':
00788 $sqlResult = $this->handlerInstance[$this->lastHandlerKey]->exec_TRUNCATEquery($table,$where);
00789 break;
00790 }
00791
00792 if ($this->printErrors && $this->sql_error()) {
00793 debug(array($this->lastQuery, $this->sql_error()));
00794 }
00795
00796 if ($this->debug) {
00797 $this->debugHandler(
00798 'exec_TRUNCATEquery',
00799 t3lib_div::milliseconds() - $pt,
00800 array(
00801 'handlerType' => $hType,
00802 'args' => array($table),
00803 'ORIG_from_table' => $ORIG_tableName
00804 )
00805 );
00806 }
00807
00808
00809 return $sqlResult;
00810 }
00811
00812
00813
00814
00815
00816
00817
00818
00819
00820 protected function exec_query(array $queryParts) {
00821 switch ($queryParts['type']) {
00822 case 'SELECT':
00823 $selectFields = $this->SQLparser->compileFieldList($queryParts['SELECT']);
00824 $fromTables = $this->SQLparser->compileFromTables($queryParts['FROM']);
00825 $whereClause = isset($queryParts['WHERE']) ? $this->SQLparser->compileWhereClause($queryParts['WHERE']) : '1=1';
00826 $groupBy = isset($queryParts['GROUPBY']) ? $this->SQLparser->compileWhereClause($queryParts['GROUPBY']) : '';
00827 $orderBy = isset($queryParts['GROUPBY']) ? $this->SQLparser->compileWhereClause($queryParts['ORDERBY']) : '';
00828 $limit = isset($queryParts['LIMIT']) ? $this->SQLparser->compileWhereClause($queryParts['LIMIT']) : '';
00829 return $this->exec_SELECTquery($selectFields, $fromTables, $whereClause, $groupBy, $orderBy, $limit);
00830
00831 case 'UPDATE':
00832 $table = $queryParts['TABLE'];
00833 $fields = array();
00834 foreach ($components['FIELDS'] as $fN => $fV) {
00835 $fields[$fN] = $fV[0];
00836 }
00837 $whereClause = isset($queryParts['WHERE']) ? $this->SQLparser->compileWhereClause($queryParts['WHERE']) : '1=1';
00838 return $this->exec_UPDATEquery($table, $whereClause, $fields);
00839
00840 case 'INSERT':
00841 $table = $queryParts['TABLE'];
00842 $values = array();
00843 if (isset($queryParts['VALUES_ONLY']) && is_array($queryParts['VALUES_ONLY'])) {
00844 $fields = $GLOBALS['TYPO3_DB']->cache_fieldType[$table];
00845 $fc = 0;
00846 foreach ($fields as $fn => $fd) {
00847 $values[$fn] = $queryParts['VALUES_ONLY'][$fc++][0];
00848 }
00849 } else {
00850 foreach ($queryParts['FIELDS'] as $fN => $fV) {
00851 $values[$fN] = $fV[0];
00852 }
00853 }
00854 return $this->exec_INSERTquery($table, $values);
00855
00856 case 'DELETE':
00857 $table = $queryParts['TABLE'];
00858 $whereClause = isset($queryParts['WHERE']) ? $this->SQLparser->compileWhereClause($queryParts['WHERE']) : '1=1';
00859 return $this->exec_DELETEquery($table, $whereClause);
00860
00861 case 'TRUNCATETABLE':
00862 $table = $queryParts['TABLE'];
00863 return $this->exec_TRUNCATEquery($table);
00864 }
00865 }
00866
00867
00868
00869
00870
00871
00872
00873
00874
00875
00876
00877
00878
00879
00880
00881
00882
00883
00884 public function INSERTquery($table, $fields_values, $no_quote_fields = '') {
00885
00886 if (is_array($fields_values) && count($fields_values)) {
00887
00888 if (is_string($no_quote_fields)) {
00889 $no_quote_fields = explode(',', $no_quote_fields);
00890 } elseif (!is_array($no_quote_fields)) {
00891 $no_quote_fields = array();
00892 }
00893
00894 $blobfields = array();
00895 $nArr = array();
00896 foreach ($fields_values as $k => $v) {
00897 if (!$this->runningNative() && $this->sql_field_metatype($table, $k) == 'B') {
00898
00899 $blobfields[$this->quoteFieldNames($k)] = $v;
00900 } elseif (!$this->runningNative() && $this->sql_field_metatype($table, $k) == 'XL') {
00901
00902 $clobfields[$this->quoteFieldNames($k)] = $v;
00903 } else {
00904
00905
00906 $mt = $this->sql_field_metatype($table, $k);
00907 if ($mt{0} == 'I') {
00908 $v = (int)$v;
00909 } else if ($mt{0} == 'F') {
00910 $v = (double)$v;
00911 }
00912
00913 $nArr[$this->quoteFieldNames($k)] = (!in_array($k,$no_quote_fields)) ? $this->fullQuoteStr($v, $table) : $v;
00914 }
00915 }
00916
00917 if (count($blobfields) || count($clobfields)) {
00918 if (count($nArr)) {
00919 $query[0] = 'INSERT INTO ' . $this->quoteFromTables($table) . '
00920 (
00921 ' . implode(',
00922 ', array_keys($nArr)) . '
00923 ) VALUES (
00924 ' . implode(',
00925 ', $nArr) . '
00926 )';
00927 }
00928 if (count($blobfields)) $query[1] = $blobfields;
00929 if (count($clobfields)) $query[2] = $clobfields;
00930 if ($this->debugOutput || $this->store_lastBuiltQuery) $this->debug_lastBuiltQuery = $query[0];
00931 } else {
00932 $query = 'INSERT INTO '.$this->quoteFromTables($table).'
00933 (
00934 ' . implode(',
00935 ', array_keys($nArr)) . '
00936 ) VALUES (
00937 ' . implode(',
00938 ', $nArr) . '
00939 )';
00940
00941 if ($this->debugOutput || $this->store_lastBuiltQuery) $this->debug_lastBuiltQuery = $query;
00942 }
00943
00944 return $query;
00945 }
00946 }
00947
00948
00949
00950
00951
00952
00953
00954
00955
00956
00957
00958 public function INSERTmultipleRows($table, array $fields, array $rows, $no_quote_fields = FALSE) {
00959 if ((string)$this->handlerCfg[$this->lastHandlerKey]['type'] === 'native') {
00960 return parent::INSERTmultipleRows($table, $fields, $rows, $no_quote_fields);
00961 }
00962
00963 $result = array();
00964
00965 foreach ($rows as $row) {
00966 $fields_values = array();
00967 foreach ($fields as $key => $value) {
00968 $fields_values[$value] = $row[$key];
00969 }
00970 $rowQuery = $this->INSERTquery($table, $fields_values, $no_quote_fields);
00971 if (is_array($rowQuery)) {
00972 $result[] = $rowQuery;
00973 } else {
00974 $result[][0] = $rowQuery;
00975 }
00976 }
00977
00978 return $result;
00979 }
00980
00981
00982
00983
00984
00985
00986
00987
00988
00989
00990
00991 public function UPDATEquery($table, $where, $fields_values, $no_quote_fields = '') {
00992
00993 if (is_string($where)) {
00994 if (is_array($fields_values) && count($fields_values)) {
00995
00996 if (is_string($no_quote_fields)) {
00997 $no_quote_fields = explode(',', $no_quote_fields);
00998 } elseif (!is_array($no_quote_fields)) {
00999 $no_quote_fields = array();
01000 }
01001
01002 $blobfields = array();
01003 $nArr = array();
01004 foreach ($fields_values as $k => $v) {
01005 if (!$this->runningNative() && $this->sql_field_metatype($table, $k) == 'B') {
01006
01007 $blobfields[$this->quoteFieldNames($k)] = $v;
01008 } elseif (!$this->runningNative() && $this->sql_field_metatype($table, $k) == 'XL') {
01009
01010 $clobfields[$this->quoteFieldNames($k)] = $v;
01011 } else {
01012
01013
01014 $mt = $this->sql_field_metatype($table, $k);
01015 if ($mt{0} == 'I') {
01016 $v = (int)$v;
01017 } else if ($mt{0} == 'F') {
01018 $v = (double)$v;
01019 }
01020 $nArr[] = $this->quoteFieldNames($k) . '=' . ((!in_array($k, $no_quote_fields)) ? $this->fullQuoteStr($v, $table) : $v);
01021 }
01022 }
01023
01024 if (count($blobfields) || count($clobfields)) {
01025 if (count($nArr)) {
01026 $query[0] = 'UPDATE '.$this->quoteFromTables($table).'
01027 SET
01028 '.implode(',
01029 ',$nArr).
01030 (strlen($where)>0 ? '
01031 WHERE
01032 '.$this->quoteWhereClause($where) : '');
01033 }
01034 if (count($blobfields)) $query[1] = $blobfields;
01035 if (count($clobfields)) $query[2] = $clobfields;
01036 if ($this->debugOutput || $this->store_lastBuiltQuery) $this->debug_lastBuiltQuery = $query[0];
01037 } else {
01038 $query = 'UPDATE '.$this->quoteFromTables($table).'
01039 SET
01040 '.implode(',
01041 ',$nArr).
01042 (strlen($where)>0 ? '
01043 WHERE
01044 '.$this->quoteWhereClause($where) : '');
01045
01046 if ($this->debugOutput || $this->store_lastBuiltQuery) $this->debug_lastBuiltQuery = $query;
01047 }
01048
01049 return $query;
01050 }
01051 } else {
01052 die('<strong>TYPO3 Fatal Error:</strong> "Where" clause argument for UPDATE query was not a string in $this->UPDATEquery() !');
01053 }
01054 }
01055
01056
01057
01058
01059
01060
01061
01062
01063
01064 public function DELETEquery($table, $where) {
01065 if (is_string($where)) {
01066 $table = $this->quoteFromTables($table);
01067 $where = $this->quoteWhereClause($where);
01068
01069 $query = parent::DELETEquery($table, $where);
01070
01071 if ($this->debugOutput || $this->store_lastBuiltQuery) $this->debug_lastBuiltQuery = $query;
01072 return $query;
01073 } else {
01074 die('<strong>TYPO3 Fatal Error:</strong> "Where" clause argument for DELETE query was not a string in $this->DELETEquery() !');
01075 }
01076 }
01077
01078
01079
01080
01081
01082
01083
01084
01085
01086
01087
01088
01089
01090 public function SELECTquery($select_fields, $from_table, $where_clause, $groupBy = '', $orderBy = '', $limit = '') {
01091 $select_fields = $this->quoteFieldNames($select_fields);
01092 $from_table = $this->quoteFromTables($from_table);
01093 $where_clause = $this->quoteWhereClause($where_clause);
01094 $groupBy = $this->quoteGroupBy($groupBy);
01095 $orderBy = $this->quoteOrderBy($orderBy);
01096
01097
01098 $query = parent::SELECTquery($select_fields,$from_table,$where_clause,$groupBy,$orderBy,$limit);
01099
01100 if ($this->debugOutput || $this->store_lastBuiltQuery) $this->debug_lastBuiltQuery = $query;
01101
01102 return $query;
01103 }
01104
01105
01106
01107
01108
01109
01110
01111 public function TRUNCATEquery($table) {
01112 $table = $this->quoteFromTables($table);
01113
01114
01115 $query = parent::TRUNCATEquery($table);
01116
01117 if ($this->debugOutput || $this->store_lastBuiltQuery) {
01118 $this->debug_lastBuiltQuery = $query;
01119 }
01120
01121 return $query;
01122 }
01123
01124
01125
01126
01127
01128
01129
01130
01131
01132
01133
01134
01135
01136
01137 protected function quoteSELECTsubquery(array $components) {
01138 $components['SELECT'] = $this->_quoteFieldNames($components['SELECT']);
01139 $components['FROM'] = $this->_quoteFromTables($components['FROM']);
01140 $components['WHERE'] = $this->_quoteWhereClause($components['WHERE']);
01141 return $components;
01142 }
01143
01144
01145
01146
01147
01148
01149
01150
01151
01152 public function quoteSelectFields($select_fields) {
01153 $this->quoteFieldNames($select_fields);
01154 }
01155
01156
01157
01158
01159
01160
01161
01162 public function quoteFieldNames($select_fields) {
01163 if ($select_fields == '') return '';
01164 if ($this->runningNative()) return $select_fields;
01165
01166 $select_fields = $this->SQLparser->parseFieldList($select_fields);
01167 if ($this->SQLparser->parse_error) {
01168 die($this->SQLparser->parse_error . ' in ' . __FILE__ . ' : ' . __LINE__);
01169 }
01170 $select_fields = $this->_quoteFieldNames($select_fields);
01171
01172 return $this->SQLparser->compileFieldList($select_fields);
01173 }
01174
01175
01176
01177
01178
01179
01180
01181
01182 protected function _quoteFieldNames(array $select_fields) {
01183 foreach ($select_fields as $k => $v) {
01184 if ($select_fields[$k]['field'] != '' && $select_fields[$k]['field'] != '*' && !is_numeric($select_fields[$k]['field'])) {
01185 $select_fields[$k]['field'] = $this->quoteName($select_fields[$k]['field']);
01186 }
01187 if ($select_fields[$k]['table'] != '' && !is_numeric($select_fields[$k]['table'])) {
01188 $select_fields[$k]['table'] = $this->quoteName($select_fields[$k]['table']);
01189 }
01190 if ($select_fields[$k]['as'] != '') {
01191 $select_fields[$k]['as'] = $this->quoteName($select_fields[$k]['as']);
01192 }
01193 if (isset($select_fields[$k]['func_content.']) && $select_fields[$k]['func_content.'][0]['func_content'] != '*'){
01194 $select_fields[$k]['func_content.'][0]['func_content'] = $this->quoteFieldNames($select_fields[$k]['func_content.'][0]['func_content']);
01195 $select_fields[$k]['func_content'] = $this->quoteFieldNames($select_fields[$k]['func_content']);
01196 }
01197 if (isset($select_fields[$k]['flow-control'])) {
01198
01199 if ($select_fields[$k]['flow-control']['type'] === 'CASE') {
01200 if (isset($select_fields[$k]['flow-control']['case_field'])) {
01201 $select_fields[$k]['flow-control']['case_field'] = $this->quoteFieldNames($select_fields[$k]['flow-control']['case_field']);
01202 }
01203 foreach ($select_fields[$k]['flow-control']['when'] as $key => $when) {
01204 $select_fields[$k]['flow-control']['when'][$key]['when_value'] = $this->_quoteWhereClause($when['when_value']);
01205 }
01206 }
01207 }
01208 }
01209
01210 return $select_fields;
01211 }
01212
01213
01214
01215
01216
01217
01218
01219 public function quoteFromTables($from_table) {
01220 if ($from_table == '') return '';
01221 if ($this->runningNative()) return $from_table;
01222
01223 $from_table = $this->SQLparser->parseFromTables($from_table);
01224 $from_table = $this->_quoteFromTables($from_table);
01225 return $this->SQLparser->compileFromTables($from_table);
01226 }
01227
01228
01229
01230
01231
01232
01233
01234
01235 protected function _quoteFromTables(array $from_table) {
01236 foreach ($from_table as $k => $v) {
01237 $from_table[$k]['table'] = $this->quoteName($from_table[$k]['table']);
01238 if ($from_table[$k]['as'] != '') {
01239 $from_table[$k]['as'] = $this->quoteName($from_table[$k]['as']);
01240 }
01241 if (is_array($v['JOIN'])) {
01242 foreach ($v['JOIN'] as $joinCnt => $join) {
01243 $from_table[$k]['JOIN'][$joinCnt]['withTable'] = $this->quoteName($join['withTable']);
01244 $from_table[$k]['JOIN'][$joinCnt]['as'] = ($join['as']) ? $this->quoteName($join['as']) : '';
01245 $from_table[$k]['JOIN'][$joinCnt]['ON'][0]['table'] = ($join['ON'][0]['table']) ? $this->quoteName($join['ON'][0]['table']) : '';
01246 $from_table[$k]['JOIN'][$joinCnt]['ON'][0]['field'] = $this->quoteName($join['ON'][0]['field']);
01247 $from_table[$k]['JOIN'][$joinCnt]['ON'][1]['table'] = ($join['ON'][1]['table']) ? $this->quoteName($join['ON'][1]['table']) : '';
01248 $from_table[$k]['JOIN'][$joinCnt]['ON'][1]['field'] = $this->quoteName($join['ON'][1]['field']);
01249 }
01250 }
01251 }
01252
01253 return $from_table;
01254 }
01255
01256
01257
01258
01259
01260
01261
01262 public function quoteWhereClause($where_clause) {
01263 if ($where_clause === '' || $this->runningNative()) return $where_clause;
01264
01265 $where_clause = $this->SQLparser->parseWhereClause($where_clause);
01266 if (is_array($where_clause)) {
01267 $where_clause = $this->_quoteWhereClause($where_clause);
01268 $where_clause = $this->SQLparser->compileWhereClause($where_clause);
01269 } else {
01270 die('Could not parse where clause in ' . __FILE__ . ' : ' . __LINE__);
01271 }
01272
01273 return $where_clause;
01274 }
01275
01276
01277
01278
01279
01280
01281
01282
01283 protected function _quoteWhereClause(array $where_clause) {
01284 foreach ($where_clause as $k => $v) {
01285
01286 if (is_array($where_clause[$k]['sub'])) {
01287 $where_clause[$k]['sub'] = $this->_quoteWhereClause($where_clause[$k]['sub']);
01288 } elseif (isset($v['func'])) {
01289 switch ($where_clause[$k]['func']['type']) {
01290 case 'EXISTS':
01291 $where_clause[$k]['func']['subquery'] = $this->quoteSELECTsubquery($v['func']['subquery']);
01292 break;
01293 case 'IFNULL':
01294 case 'LOCATE':
01295 if ($where_clause[$k]['func']['table'] != '') {
01296 $where_clause[$k]['func']['table'] = $this->quoteName($v['func']['table']);
01297 }
01298 if ($where_clause[$k]['func']['field'] != '') {
01299 $where_clause[$k]['func']['field'] = $this->quoteName($v['func']['field']);
01300 }
01301 break;
01302 }
01303 } else {
01304 if ($where_clause[$k]['table'] != '') {
01305 $where_clause[$k]['table'] = $this->quoteName($where_clause[$k]['table']);
01306 }
01307 if (!is_numeric($where_clause[$k]['field'])) {
01308 $where_clause[$k]['field'] = $this->quoteName($where_clause[$k]['field']);
01309 }
01310 if (isset($where_clause[$k]['calc_table'])) {
01311 if ($where_clause[$k]['calc_table'] != '') {
01312 $where_clause[$k]['calc_table'] = $this->quoteName($where_clause[$k]['calc_table']);
01313 }
01314 if ($where_clause[$k]['calc_field'] != '') {
01315 $where_clause[$k]['calc_field'] = $this->quoteName($where_clause[$k]['calc_field']);
01316 }
01317 }
01318 }
01319 if ($where_clause[$k]['comparator']) {
01320 if (isset($v['value']['operator'])) {
01321 foreach ($where_clause[$k]['value']['args'] as $argK => $fieldDef) {
01322 $where_clause[$k]['value']['args'][$argK]['table'] = $this->quoteName($fieldDef['table']);
01323 $where_clause[$k]['value']['args'][$argK]['field'] = $this->quoteName($fieldDef['field']);
01324 }
01325 } else {
01326
01327 if (t3lib_div::inList('NOTIN,IN', strtoupper(str_replace(array(' ',"\n", "\r", "\t"), '', $where_clause[$k]['comparator'])))) {
01328 if (isset($v['subquery'])) {
01329 $where_clause[$k]['subquery'] = $this->quoteSELECTsubquery($v['subquery']);
01330 }
01331 } else {
01332 if ((!isset($where_clause[$k]['value'][1]) || $where_clause[$k]['value'][1] == '') && is_string($where_clause[$k]['value'][0]) && strstr($where_clause[$k]['value'][0], '.')) {
01333 $where_clause[$k]['value'][0] = $this->quoteFieldNames($where_clause[$k]['value'][0]);
01334 }
01335 }
01336 }
01337 }
01338 }
01339
01340 return $where_clause;
01341 }
01342
01343
01344
01345
01346
01347
01348
01349 protected function quoteGroupBy($groupBy) {
01350 if ($groupBy === '') return '';
01351 if ($this->runningNative()) return $groupBy;
01352
01353 $groupBy = $this->SQLparser->parseFieldList($groupBy);
01354 foreach ($groupBy as $k => $v) {
01355 $groupBy[$k]['field'] = $this->quoteName($groupBy[$k]['field']);
01356 if ($groupBy[$k]['table'] != '') {
01357 $groupBy[$k]['table'] = $this->quoteName($groupBy[$k]['table']);
01358 }
01359 }
01360 return $this->SQLparser->compileFieldList($groupBy);
01361 }
01362
01363
01364
01365
01366
01367
01368
01369 protected function quoteOrderBy($orderBy) {
01370 if ($orderBy === '') return '';
01371 if ($this->runningNative()) return $orderBy;
01372
01373 $orderBy = $this->SQLparser->parseFieldList($orderBy);
01374 foreach ($orderBy as $k => $v) {
01375 $orderBy[$k]['field'] = $this->quoteName($orderBy[$k]['field']);
01376 if ($orderBy[$k]['table'] != '') {
01377 $orderBy[$k]['table'] = $this->quoteName($orderBy[$k]['table']);
01378 }
01379 }
01380 return $this->SQLparser->compileFieldList($orderBy);
01381 }
01382
01383
01384
01385
01386
01387
01388
01389
01390
01391
01392
01393
01394
01395
01396
01397
01398
01399 public function fullQuoteStr($str, $table) {
01400 return '\'' . $this->quoteStr($str, $table) . '\'';
01401 }
01402
01403
01404
01405
01406
01407
01408
01409
01410
01411
01412 public function quoteStr($str, $table) {
01413 $this->lastHandlerKey = $this->handler_getFromTableList($table);
01414 switch ((string)$this->handlerCfg[$this->lastHandlerKey]['type']) {
01415 case 'native':
01416 $str = mysql_real_escape_string($str, $this->handlerInstance[$this->lastHandlerKey]['link']);
01417 break;
01418 case 'adodb':
01419 $str = substr($this->handlerInstance[$this->lastHandlerKey]->qstr($str), 1, -1);
01420 break;
01421 case 'userdefined':
01422 $str = $this->handlerInstance[$this->lastHandlerKey]->quoteStr($str);
01423 break;
01424 default:
01425 die('No handler found!!!');
01426 break;
01427 }
01428
01429 return $str;
01430 }
01431
01432
01433
01434
01435
01436
01437
01438
01439
01440 public function quoteName($name, $handlerKey = NULL, $useBackticks = FALSE) {
01441 $handlerKey = $handlerKey ? $handlerKey : $this->lastHandlerKey;
01442 $useNameQuote = isset($this->handlerCfg[$handlerKey]['config']['useNameQuote']) ? $this->handlerCfg[$handlerKey]['config']['useNameQuote'] : FALSE;
01443 if ($useNameQuote) {
01444 return $this->handlerInstance[$handlerKey]->DataDictionary->NameQuote($name);
01445 } else {
01446 $quote = $useBackticks ? '`' : $this->handlerInstance[$handlerKey]->nameQuote;
01447 return $quote . $name . $quote;
01448 }
01449 }
01450
01451
01452
01453
01454
01455
01456
01457
01458 public function MetaType($type, $table, $max_length = -1) {
01459 $this->lastHandlerKey = $this->handler_getFromTableList($table);
01460 $str = '';
01461 switch ((string)$this->handlerCfg[$this->lastHandlerKey]['type']) {
01462 case 'native':
01463 $str = $type;
01464 break;
01465 case 'adodb':
01466 if (in_array($table, $this->cache_fieldType)) {
01467 $rs = $this->handlerInstance[$this->lastHandlerKey]->SelectLimit('SELECT * FROM ' . $this->quoteFromTables($table), 1);
01468 $str = $rs->MetaType($type, $max_length);
01469 }
01470 break;
01471 case 'userdefined':
01472 $str = $this->handlerInstance[$this->lastHandlerKey]->MetaType($str,$table,$max_length);
01473 break;
01474 default:
01475 die('No handler found!!!');
01476 break;
01477 }
01478
01479 return $str;
01480 }
01481
01482
01483
01484
01485
01486
01487
01488
01489 public function MySQLMetaType($t) {
01490
01491 switch (strtoupper($t)) {
01492 case 'STRING':
01493 case 'CHAR':
01494 case 'VARCHAR':
01495 case 'TINYBLOB':
01496 case 'TINYTEXT':
01497 case 'ENUM':
01498 case 'SET': return 'C';
01499
01500 case 'TEXT':
01501 case 'LONGTEXT':
01502 case 'MEDIUMTEXT': return 'XL';
01503
01504 case 'IMAGE':
01505 case 'LONGBLOB':
01506 case 'BLOB':
01507 case 'MEDIUMBLOB': return 'B';
01508
01509 case 'YEAR':
01510 case 'DATE': return 'D';
01511
01512 case 'TIME':
01513 case 'DATETIME':
01514 case 'TIMESTAMP': return 'T';
01515
01516 case 'FLOAT':
01517 case 'DOUBLE': return 'F';
01518
01519 case 'INT':
01520 case 'INTEGER':
01521 case 'TINYINT':
01522 case 'SMALLINT':
01523 case 'MEDIUMINT':
01524 case 'BIGINT': return 'I8';
01525
01526 default: return 'N';
01527 }
01528 }
01529
01530
01531
01532
01533
01534
01535
01536 public function MySQLActualType($meta) {
01537 switch (strtoupper($meta)) {
01538 case 'C': return 'VARCHAR';
01539 case 'XL':
01540 case 'X': return 'LONGTEXT';
01541
01542 case 'C2': return 'VARCHAR';
01543 case 'X2': return 'LONGTEXT';
01544
01545 case 'B': return 'LONGBLOB';
01546
01547 case 'D': return 'DATE';
01548 case 'T': return 'DATETIME';
01549 case 'L': return 'TINYINT';
01550
01551 case 'I':
01552 case 'I1':
01553 case 'I2':
01554 case 'I4':
01555 case 'I8': return 'BIGINT';
01556
01557 case 'F': return 'DOUBLE';
01558 case 'N': return 'NUMERIC';
01559
01560 default: return $meta;
01561 }
01562 }
01563
01564
01565
01566
01567
01568
01569
01570
01571
01572
01573
01574
01575
01576
01577
01578
01579 public function sql_error() {
01580 switch ($this->handlerCfg[$this->lastHandlerKey]['type']) {
01581 case 'native':
01582 $output = mysql_error($this->handlerInstance[$this->lastHandlerKey]['link']);
01583 break;
01584 case 'adodb':
01585 $output = $this->handlerInstance[$this->lastHandlerKey]->ErrorMsg();
01586 break;
01587 case 'userdefined':
01588 $output = $this->handlerInstance[$this->lastHandlerKey]->sql_error();
01589 break;
01590 }
01591 return $output;
01592 }
01593
01594
01595
01596
01597
01598
01599 public function sql_errno() {
01600 switch ($this->handlerCfg[$this->lastHandlerKey]['type']) {
01601 case 'native':
01602 $output = mysql_errno($this->handlerInstance[$this->lastHandlerKey]['link']);
01603 break;
01604 case 'adodb':
01605 $output = $this->handlerInstance[$this->lastHandlerKey]->ErrorNo();
01606 break;
01607 case 'userdefined':
01608 $output = $this->handlerInstance[$this->lastHandlerKey]->sql_errno();
01609 break;
01610 }
01611 return $output;
01612 }
01613
01614
01615
01616
01617
01618
01619
01620 public function sql_num_rows(&$res) {
01621 if ($res === FALSE) return 0;
01622
01623 $handlerType = is_object($res) ? $res->TYPO3_DBAL_handlerType : 'native';
01624 switch ($handlerType) {
01625 case 'native':
01626 $output = mysql_num_rows($res);
01627 break;
01628 case 'adodb':
01629 $output = method_exists($res, 'RecordCount') ? $res->RecordCount() : 0;
01630 break;
01631 case 'userdefined':
01632 $output = $res->sql_num_rows();
01633 break;
01634 }
01635 return $output;
01636 }
01637
01638
01639
01640
01641
01642
01643
01644 public function sql_fetch_assoc(&$res) {
01645 $output = array();
01646
01647 $handlerType = is_object($res) ? $res->TYPO3_DBAL_handlerType : (is_resource($res) ? 'native' : FALSE);
01648 switch ($handlerType) {
01649 case 'native':
01650 $output = mysql_fetch_assoc($res);
01651 $tableList = $this->resourceIdToTableNameMap[(string)$res];
01652 break;
01653 case 'adodb':
01654
01655
01656
01657 if (method_exists($res, 'FetchRow')) {
01658 $output = $res->FetchRow();
01659 $tableList = $res->TYPO3_DBAL_tableList;
01660
01661
01662
01663 if (is_array($output)) {
01664 foreach ($output as $key => $value) {
01665 if (is_integer($key)) {
01666 unset($output[$key]);
01667 }
01668 elseif ($value === ' ' && $this->runningADOdbDriver('mssql')) $output[$key] = '';
01669 }
01670 }
01671 }
01672 break;
01673 case 'userdefined':
01674 $output = $res->sql_fetch_assoc();
01675 $tableList = $res->TYPO3_DBAL_tableList;
01676 break;
01677 }
01678
01679
01680 if (is_array($output)) {
01681 if ($tables = $this->map_needMapping($tableList,TRUE)) {
01682 $output = $this->map_assocArray($output,$tables,1);
01683 }
01684 }
01685
01686
01687 return $output;
01688 }
01689
01690
01691
01692
01693
01694
01695
01696
01697 public function sql_fetch_row(&$res) {
01698 $handlerType = is_object($res) ? $res->TYPO3_DBAL_handlerType : 'native';
01699 switch ($handlerType) {
01700 case 'native':
01701 $output = mysql_fetch_row($res);
01702 break;
01703 case 'adodb':
01704
01705
01706
01707 if (method_exists($res, 'FetchRow')) {
01708 $output = $res->FetchRow();
01709
01710
01711
01712 if (is_array($output)) {
01713 foreach ($output as $key => $value) {
01714 if (!is_integer($key)) unset($output[$key]);
01715 elseif ($value === ' ' && $this->runningADOdbDriver('mssql')) $output[$key] = '';
01716 }
01717 }
01718 }
01719 break;
01720 case 'userdefined':
01721 $output = $res->sql_fetch_row();
01722 break;
01723 }
01724 return $output;
01725 }
01726
01727
01728
01729
01730
01731
01732
01733 public function sql_free_result(&$res) {
01734 if ($res === FALSE) return FALSE;
01735
01736 $handlerType = is_object($res) ? $res->TYPO3_DBAL_handlerType : 'native';
01737 switch ($handlerType) {
01738 case 'native':
01739 $output = mysql_free_result($res);
01740 break;
01741 case 'adodb':
01742 if (method_exists($res, 'Close')) {
01743 $res->Close();
01744 unset($res);
01745 $output = TRUE;
01746 } else {
01747 $output = FALSE;
01748 }
01749 break;
01750 case 'userdefined':
01751 unset($res);
01752 break;
01753 }
01754 return $output;
01755 }
01756
01757
01758
01759
01760
01761
01762 public function sql_insert_id() {
01763 switch ($this->handlerCfg[$this->lastHandlerKey]['type']) {
01764 case 'native':
01765 $output = mysql_insert_id($this->handlerInstance[$this->lastHandlerKey]['link']);
01766 break;
01767 case 'adodb':
01768 $output = $this->handlerInstance[$this->lastHandlerKey]->last_insert_id;
01769 break;
01770 case 'userdefined':
01771 $output = $this->handlerInstance[$this->lastHandlerKey]->sql_insert_id();
01772 break;
01773 }
01774 return $output;
01775 }
01776
01777
01778
01779
01780
01781
01782 public function sql_affected_rows() {
01783 switch ($this->handlerCfg[$this->lastHandlerKey]['type']) {
01784 case 'native':
01785 $output = mysql_affected_rows();
01786 break;
01787 case 'adodb':
01788 $output = $this->handlerInstance[$this->lastHandlerKey]->Affected_Rows();
01789 break;
01790 case 'userdefined':
01791 $output = $this->handlerInstance[$this->lastHandlerKey]->sql_affected_rows();
01792 break;
01793 }
01794 return $output;
01795 }
01796
01797
01798
01799
01800
01801
01802
01803
01804 public function sql_data_seek(&$res, $seek) {
01805 $handlerType = is_object($res) ? $res->TYPO3_DBAL_handlerType : 'native';
01806 switch ($handlerType) {
01807 case 'native':
01808 $output = mysql_data_seek($res,$seek);
01809 break;
01810 case 'adodb':
01811 $output = $res->Move($seek);
01812 break;
01813 case 'userdefined':
01814 $output = $res->sql_data_seek($seek);
01815 break;
01816 }
01817 return $output;
01818 }
01819
01820
01821
01822
01823
01824
01825
01826
01827
01828
01829 public function sql_field_metatype($table, $field) {
01830
01831 foreach ($this->mapping as $tableName => $tableMapInfo) {
01832 if (isset($tableMapInfo['mapTableName']) && $tableMapInfo['mapTableName'] === $table) {
01833
01834 $table = $tableName;
01835 }
01836
01837 if (isset($tableMapInfo['mapFieldNames'])) {
01838 foreach ($tableMapInfo['mapFieldNames'] as $fieldName => $fieldMapInfo) {
01839 if ($fieldMapInfo === $field) {
01840
01841 $field = $fieldName;
01842 }
01843 }
01844 }
01845 }
01846
01847 return $this->cache_fieldType[$table][$field]['metaType'];
01848 }
01849
01850
01851
01852
01853
01854
01855
01856
01857
01858
01859 public function sql_field_type(&$res,$pointer) {
01860 if ($res === null) {
01861 debug(array('no res in sql_field_type!'));
01862 return 'text';
01863 }
01864 elseif (is_string($res)){
01865 if ($res === 'tx_dbal_debuglog') return 'text';
01866 $handlerType = 'adodb';
01867 }
01868 else {
01869 $handlerType = is_object($res) ? $res->TYPO3_DBAL_handlerType : 'native';
01870 }
01871
01872 switch ($handlerType) {
01873 case 'native':
01874 $output = mysql_field_type($res,$pointer);
01875 break;
01876 case 'adodb':
01877 if (is_string($pointer)){
01878 $output = $this->cache_fieldType[$res][$pointer]['type'];
01879 }
01880
01881 break;
01882 case 'userdefined':
01883 $output = $res->sql_field_type($pointer);
01884 break;
01885 }
01886
01887 return $output;
01888 }
01889
01890
01891
01892
01893
01894
01895
01896
01897
01898
01899
01900
01901
01902
01903
01904
01905
01906
01907
01908
01909
01910
01911
01912
01913 public function sql($db,$query) {
01914 return $this->sql_query($query);
01915 }
01916
01917
01918
01919
01920
01921
01922
01923
01924
01925
01926
01927
01928
01929
01930
01931 public function sql_query($query) {
01932
01933 $queryParts = $this->SQLparser->parseSQL($query);
01934 if (is_array($queryParts) && t3lib_div::inList('SELECT,UPDATE,INSERT,DELETE', $queryParts['type'])) {
01935 return $this->exec_query($queryParts);
01936 }
01937
01938 switch ($this->handlerCfg['_DEFAULT']['type']) {
01939 case 'native':
01940 $sqlResult = mysql_query($query, $this->handlerInstance['_DEFAULT']['link']);
01941 break;
01942 case 'adodb':
01943 $sqlResult = $this->handlerInstance['_DEFAULT']->Execute($query);
01944 $sqlResult->TYPO3_DBAL_handlerType = 'adodb';
01945 break;
01946 case 'userdefined':
01947 $sqlResult = $this->handlerInstance['_DEFAULT']->sql_query($query);
01948 $sqlResult->TYPO3_DBAL_handlerType = 'userdefined';
01949 break;
01950 }
01951
01952 if ($this->printErrors && $this->sql_error()) {
01953 debug(array($this->lastQuery, $this->sql_error()));
01954 }
01955
01956 return $sqlResult;
01957 }
01958
01959
01960
01961
01962
01963
01964
01965
01966
01967
01968
01969
01970
01971 public function sql_pconnect($TYPO3_db_host, $TYPO3_db_username, $TYPO3_db_password) {
01972
01973 $this->handlerCfg['_DEFAULT']['config']['username'] = $TYPO3_db_username;
01974 $this->handlerCfg['_DEFAULT']['config']['password'] = $TYPO3_db_password;
01975 $this->handlerCfg['_DEFAULT']['config']['host'] = $TYPO3_db_host;
01976 $this->handlerCfg['_DEFAULT']['config']['database'] = TYPO3_db;
01977
01978
01979 $sqlResult = $this->handler_init('_DEFAULT');
01980 return $sqlResult;
01981 }
01982
01983
01984
01985
01986
01987
01988
01989
01990 public function sql_select_db($TYPO3_db) {
01991 return TRUE;
01992 }
01993
01994
01995
01996
01997
01998
01999
02000
02001
02002
02003
02004
02005
02006
02007
02008
02009
02010
02011
02012
02013
02014
02015
02016
02017
02018
02019
02020
02021
02022 public function admin_get_dbs() {
02023 $dbArr = array();
02024 switch ($this->handlerCfg['_DEFAULT']['type']) {
02025 case 'native':
02026 $db_list = mysql_list_dbs($this->link);
02027 while ($row = mysql_fetch_object($db_list)) {
02028 if ($this->sql_select_db($row->Database)) {
02029 $dbArr[] = $row->Database;
02030 }
02031 }
02032 break;
02033 case 'adodb':
02034
02035
02036 if (method_exists($this->handlerInstance['_DEFAULT'],'MetaDatabases')) {
02037 $sqlDBs = $this->handlerInstance['_DEFAULT']->MetaDatabases();
02038 if (is_array($sqlDBs)) {
02039 foreach ($sqlDBs as $k => $theDB) {
02040 $dbArr[] = $theDB;
02041 }
02042 }
02043 }
02044 break;
02045 case 'userdefined':
02046 $dbArr = $this->handlerInstance['_DEFAULT']->admin_get_tables();
02047 break;
02048 }
02049
02050 return $dbArr;
02051 }
02052
02053
02054
02055
02056
02057
02058
02059
02060
02061
02062
02063 public function admin_get_tables() {
02064 $whichTables = array();
02065
02066
02067 switch ($this->handlerCfg['_DEFAULT']['type']) {
02068 case 'native':
02069 $tables_result = mysql_query('SHOW TABLE STATUS FROM `' . TYPO3_db . '`', $this->handlerInstance['_DEFAULT']['link']);
02070 if (!$this->sql_error()) {
02071 while ($theTable = $this->sql_fetch_assoc($tables_result)) {
02072 $whichTables[current($theTable)] = current($theTable);
02073 }
02074 }
02075 break;
02076 case 'adodb':
02077 $sqlTables = $this->handlerInstance['_DEFAULT']->MetaTables('TABLES');
02078 while (list($k, $theTable) = each($sqlTables)) {
02079 if (preg_match('/BIN\$/', $theTable)) continue;
02080 $whichTables[$theTable] = $theTable;
02081 }
02082 break;
02083 case 'userdefined':
02084 $whichTables = $this->handlerInstance['_DEFAULT']->admin_get_tables();
02085 break;
02086 }
02087
02088
02089 if (is_array($this->mapping) && count($this->mapping)) {
02090
02091
02092 $tMap = array();
02093 foreach ($this->mapping as $tN => $tMapInfo) {
02094 if (isset($tMapInfo['mapTableName'])) $tMap[$tMapInfo['mapTableName']]=$tN;
02095 }
02096
02097
02098 $newList=array();
02099 foreach ($whichTables as $tN) {
02100 if (isset($tMap[$tN])) $tN = $tMap[$tN];
02101 $newList[$tN] = $tN;
02102 }
02103
02104 $whichTables = $newList;
02105 }
02106
02107
02108 if (is_array($this->table2handlerKeys)) {
02109 foreach ($this->table2handlerKeys as $key => $handlerKey) {
02110 $whichTables[$key] = $key;
02111 }
02112 }
02113
02114 return $whichTables;
02115 }
02116
02117
02118
02119
02120
02121
02122
02123
02124
02125 public function admin_get_fields($tableName) {
02126 $output = array();
02127
02128
02129 $ORIG_tableName = $tableName;
02130 if ($tableArray = $this->map_needMapping($tableName)) {
02131
02132
02133 if ($this->mapping[$tableName]['mapTableName']) {
02134 $tableName = $this->mapping[$tableName]['mapTableName'];
02135 }
02136 }
02137
02138
02139 $this->lastHandlerKey = $this->handler_getFromTableList($ORIG_tableName);
02140 switch ((string)$this->handlerCfg[$this->lastHandlerKey]['type']) {
02141 case 'native':
02142 $columns_res = mysql_query('SHOW columns FROM '.$tableName, $this->handlerInstance[$this->lastHandlerKey]['link']);
02143 while($fieldRow = mysql_fetch_assoc($columns_res)) {
02144 $output[$fieldRow['Field']] = $fieldRow;
02145 }
02146 break;
02147 case 'adodb':
02148 $fieldRows = $this->handlerInstance[$this->lastHandlerKey]->MetaColumns($tableName, FALSE);
02149 if (is_array($fieldRows)) {
02150 foreach ($fieldRows as $k => $fieldRow) {
02151 settype($fieldRow, 'array');
02152 $fieldRow['Field'] = $fieldRow['name'];
02153 $ntype = $this->MySQLActualType($this->MetaType($fieldRow['type'],$tableName));
02154 $ntype .= (($fieldRow['max_length'] != -1) ? (($ntype == 'INT') ? '(11)' :'('.$fieldRow['max_length'].')') : '');
02155 $fieldRow['Type'] = strtolower($ntype);
02156 $fieldRow['Null'] = '';
02157 $fieldRow['Key'] = '';
02158 $fieldRow['Default'] = $fieldRow['default_value'];
02159 $fieldRow['Extra'] = '';
02160 $output[$fieldRow['name']] = $fieldRow;
02161 }
02162 }
02163 break;
02164 case 'userdefined':
02165 $output = $this->handlerInstance[$this->lastHandlerKey]->admin_get_fields($tableName);
02166 break;
02167 }
02168
02169
02170 if (is_array($tableArray) && is_array($this->mapping[$ORIG_tableName]['mapFieldNames'])) {
02171 $revFields = array_flip($this->mapping[$ORIG_tableName]['mapFieldNames']);
02172
02173 $newOutput = array();
02174 foreach ($output as $fN => $fInfo) {
02175 if (isset($revFields[$fN])) {
02176 $fN = $revFields[$fN];
02177 $fInfo['Field'] = $fN;
02178 }
02179 $newOutput[$fN] = $fInfo;
02180 }
02181 $output = $newOutput;
02182 }
02183
02184 return $output;
02185 }
02186
02187
02188
02189
02190
02191
02192
02193
02194 public function admin_get_keys($tableName) {
02195 $output = array();
02196
02197
02198 $ORIG_tableName = $tableName;
02199 if ($tableArray = $this->map_needMapping($tableName)) {
02200
02201
02202 if ($this->mapping[$tableName]['mapTableName']) {
02203 $tableName = $this->mapping[$tableName]['mapTableName'];
02204 }
02205 }
02206
02207
02208 $this->lastHandlerKey = $this->handler_getFromTableList($ORIG_tableName);
02209 switch ((string)$this->handlerCfg[$this->lastHandlerKey]['type']) {
02210 case 'native':
02211 $keyRes = mysql_query('SHOW keys FROM '.$tableName, $this->handlerInstance[$this->lastHandlerKey]['link']);
02212 while($keyRow = mysql_fetch_assoc($keyRes)) {
02213 $output[] = $keyRow;
02214 }
02215 break;
02216 case 'adodb':
02217 $keyRows = $this->handlerInstance[$this->lastHandlerKey]->MetaIndexes($tableName);
02218 if ($keyRows !== FALSE) {
02219 while (list($k, $theKey) = each($keyRows)) {
02220 $theKey['Table'] = $tableName;
02221 $theKey['Non_unique'] = (int) !$theKey['unique'];
02222 $theKey['Key_name'] = str_replace($tableName.'_','',$k);
02223
02224
02225 $theKey['Collation'] = '';
02226 $theKey['Cardinality'] = '';
02227 $theKey['Sub_part'] = '';
02228 $theKey['Packed'] = '';
02229 $theKey['Null'] = '';
02230 $theKey['Index_type'] = '';
02231 $theKey['Comment'] = '';
02232
02233
02234 $keycols = $theKey['columns'];
02235 while (list($c, $theCol) = each($keycols)) {
02236 $theKey['Seq_in_index'] = $c+1;
02237 $theKey['Column_name'] = $theCol;
02238 $output[] = $theKey;
02239 }
02240 }
02241 }
02242 $priKeyRow = $this->handlerInstance[$this->lastHandlerKey]->MetaPrimaryKeys($tableName);
02243 $theKey = array();
02244 $theKey['Table'] = $tableName;
02245 $theKey['Non_unique'] = 0;
02246 $theKey['Key_name'] = 'PRIMARY';
02247
02248
02249 $theKey['Collation'] = '';
02250 $theKey['Cardinality'] = '';
02251 $theKey['Sub_part'] = '';
02252 $theKey['Packed'] = '';
02253 $theKey['Null'] = '';
02254 $theKey['Index_type'] = '';
02255 $theKey['Comment'] = '';
02256
02257
02258 if ($priKeyRow !== FALSE) {
02259 while (list($c, $theCol) = each($priKeyRow)) {
02260 $theKey['Seq_in_index'] = $c+1;
02261 $theKey['Column_name'] = $theCol;
02262 $output[] = $theKey;
02263 }
02264 }
02265 break;
02266 case 'userdefined':
02267 $output = $this->handlerInstance[$this->lastHandlerKey]->admin_get_keys($tableName);
02268 break;
02269 }
02270
02271
02272 if (is_array($tableArray) && is_array($this->mapping[$ORIG_tableName]['mapFieldNames'])) {
02273 $revFields = array_flip($this->mapping[$ORIG_tableName]['mapFieldNames']);
02274
02275 $newOutput = array();
02276 foreach ($output as $kN => $kInfo) {
02277
02278 $kInfo['Table'] = $ORIG_tableName;
02279
02280
02281 if (isset($revFields[$kInfo['Column_name']])) {
02282 $kInfo['Column_name'] = $revFields[$kInfo['Column_name']];
02283 }
02284
02285
02286 $newOutput[$kN] = $kInfo;
02287 }
02288 $output = $newOutput;
02289 }
02290
02291 return $output;
02292 }
02293
02294
02295
02296
02297
02298
02299 public function admin_get_charsets() {
02300 return array();
02301 }
02302
02303
02304
02305
02306
02307
02308
02309 public function admin_query($query) {
02310 $parsedQuery = $this->SQLparser->parseSQL($query);
02311 $ORIG_table = $parsedQuery['TABLE'];
02312
02313 if (is_array($parsedQuery)) {
02314
02315
02316 switch ($parsedQuery['type']) {
02317 case 'CREATETABLE':
02318 case 'ALTERTABLE':
02319 case 'DROPTABLE':
02320 if (file_exists(PATH_typo3conf.'temp_fieldInfo.php')) unlink(PATH_typo3conf.'temp_fieldInfo.php');
02321 $this->map_genericQueryParsed($parsedQuery);
02322 break;
02323 case 'INSERT':
02324 case 'TRUNCATETABLE':
02325 $this->map_genericQueryParsed($parsedQuery);
02326 break;
02327 case 'CREATEDATABASE':
02328 die('Creating a database with DBAL is not supported. Did you really read the manual?');
02329 break;
02330 default:
02331 die('ERROR: Invalid Query type ('.$parsedQuery['type'].') for ->admin_query() function!: "'.htmlspecialchars($query).'"');
02332 break;
02333 }
02334
02335
02336 $this->lastParsedAndMappedQueryArray = $parsedQuery;
02337
02338
02339 $this->lastHandlerKey = $this->handler_getFromTableList($ORIG_table);
02340 switch ((string)$this->handlerCfg[$this->lastHandlerKey]['type']) {
02341 case 'native':
02342
02343 $compiledQuery = $this->SQLparser->compileSQL($this->lastParsedAndMappedQueryArray);
02344
02345 if ($this->lastParsedAndMappedQueryArray['type']=='INSERT') {
02346 return mysql_query($compiledQuery, $this->link);
02347 }
02348 return mysql_query($compiledQuery[0], $this->link);
02349 break;
02350 case 'adodb':
02351
02352 $compiledQuery = $this->SQLparser->compileSQL($this->lastParsedAndMappedQueryArray);
02353 switch ($this->lastParsedAndMappedQueryArray['type']) {
02354 case 'INSERT':
02355 return $this->exec_INSERTquery($this->lastParsedAndMappedQueryArray['TABLE'], $compiledQuery);
02356 case 'TRUNCATETABLE':
02357 return $this->exec_TRUNCATEquery($this->lastParsedAndMappedQueryArray['TABLE']);
02358 }
02359 return $this->handlerInstance[$this->lastHandlerKey]->DataDictionary->ExecuteSQLArray($compiledQuery);
02360 break;
02361 case 'userdefined':
02362
02363 $compiledQuery = $this->SQLparser->compileSQL($this->lastParsedAndMappedQueryArray);
02364
02365 return $this->handlerInstance[$this->lastHandlerKey]->admin_query($compiledQuery);
02366 break;
02367 }
02368 } else die('ERROR: Query could not be parsed: "'.htmlspecialchars($parsedQuery).'". Query: "'.htmlspecialchars($query).'"');
02369 }
02370
02371
02372
02373
02374
02375
02376
02377
02378
02379
02380
02381
02382
02383
02384
02385
02386
02387
02388
02389
02390
02391
02392
02393 public function handler_getFromTableList($tableList) {
02394
02395 $key = $tableList;
02396
02397 if (!isset($this->cache_handlerKeyFromTableList[$key])) {
02398
02399
02400 $_tableList = $tableList;
02401 $tableArray = $this->SQLparser->parseFromTables($_tableList);
02402
02403
02404 if (is_array($tableArray) && count($tableArray)) {
02405 $outputHandlerKey = '';
02406
02407 foreach ($tableArray as $vArray) {
02408
02409 $handlerKey = $this->table2handlerKeys[$vArray['table']] ? $this->table2handlerKeys[$vArray['table']] : '_DEFAULT';
02410
02411
02412 if ($outputHandlerKey && $handlerKey != $outputHandlerKey) {
02413 die('DBAL fatal error: Tables in this list "'.$tableList.'" didn\'t use the same DB handler!');
02414 }
02415
02416 $outputHandlerKey = $handlerKey;
02417 }
02418
02419
02420 if (!isset($this->handlerInstance[$outputHandlerKey])) {
02421 $this->handler_init($outputHandlerKey);
02422 }
02423
02424
02425 $this->cache_handlerKeyFromTableList[$key] = $outputHandlerKey;
02426 } else {
02427 die('DBAL fatal error: No handler found in handler_getFromTableList() for: "'.$tableList.'" ('.$tableArray.')');
02428 }
02429 }
02430
02431 return $this->cache_handlerKeyFromTableList[$key];
02432 }
02433
02434
02435
02436
02437
02438
02439
02440
02441 public function handler_init($handlerKey) {
02442
02443
02444 $cfgArray = $this->handlerCfg[$handlerKey];
02445 $handlerType = (string)$cfgArray['type'];
02446 $output = FALSE;
02447
02448 if (is_array($cfgArray)) {
02449 switch ($handlerType) {
02450 case 'native':
02451 if ($GLOBALS['TYPO3_CONF_VARS']['SYS']['no_pconnect']) {
02452 $link = mysql_connect($cfgArray['config']['host'].(isset($cfgArray['config']['port']) ? ':'.$cfgArray['config']['port'] : ''), $cfgArray['config']['username'], $cfgArray['config']['password'], TRUE);
02453 } else {
02454 $link = mysql_pconnect($cfgArray['config']['host'].(isset($cfgArray['config']['port']) ? ':'.$cfgArray['config']['port'] : ''), $cfgArray['config']['username'], $cfgArray['config']['password']);
02455 }
02456
02457
02458 $this->handlerInstance[$handlerKey] = array('handlerType' => 'native', 'link' => $link);
02459
02460
02461 if ($link) {
02462
02463 if ($handlerKey == '_DEFAULT') {
02464 $this->link = $link;
02465 }
02466
02467
02468 if (mysql_select_db($cfgArray['config']['database'], $link)) {
02469 $output = TRUE;
02470 }
02471 $setDBinit = t3lib_div::trimExplode(chr(10), $GLOBALS['TYPO3_CONF_VARS']['SYS']['setDBinit'], 1);
02472 foreach ($setDBinit as $v) {
02473 if (mysql_query($v, $this->link) === FALSE) {
02474 t3lib_div::sysLog('Could not initialize DB connection with query "'.$v.'".','Core',3);
02475 }
02476 }
02477 } else {
02478 t3lib_div::sysLog('Could not connect to MySQL server '.$cfgArray['config']['host'].' with user '.$cfgArray['config']['username'].'.','Core',4);
02479 }
02480 break;
02481 case 'adodb':
02482 $output = TRUE;
02483 require_once(t3lib_extMgm::extPath('adodb').'adodb/adodb.inc.php');
02484 if (!defined('ADODB_FORCE_NULLS')) define('ADODB_FORCE_NULLS', 1);
02485 $GLOBALS['ADODB_FORCE_TYPE'] = ADODB_FORCE_VALUE;
02486 $GLOBALS['ADODB_FETCH_MODE'] = ADODB_FETCH_BOTH;
02487
02488 $this->handlerInstance[$handlerKey] = &ADONewConnection($cfgArray['config']['driver']);
02489
02490
02491 if (isset($cfgArray['config']['driverOptions'])) {
02492 foreach ($cfgArray['config']['driverOptions'] as $optionName => $optionValue) {
02493 $optionSetterName = 'set' . ucfirst($optionName);
02494 if (method_exists($this->handlerInstance[$handlerKey], $optionSetterName)) {
02495 $this->handlerInstance[$handlerKey]->$optionSetterName($optionValue);
02496 } else {
02497 $this->handlerInstance[$handlerKey]->$optionName = $optionValue;
02498 }
02499 }
02500 }
02501
02502 if ($GLOBALS['TYPO3_CONF_VARS']['SYS']['no_pconnect']) {
02503 $this->handlerInstance[$handlerKey]->Connect($cfgArray['config']['host'].(isset($cfgArray['config']['port']) ? ':'.$cfgArray['config']['port'] : ''),$cfgArray['config']['username'],$cfgArray['config']['password'],$cfgArray['config']['database']);
02504 } else {
02505 $this->handlerInstance[$handlerKey]->PConnect($cfgArray['config']['host'].(isset($cfgArray['config']['port']) ? ':'.$cfgArray['config']['port'] : ''),$cfgArray['config']['username'],$cfgArray['config']['password'],$cfgArray['config']['database']);
02506 }
02507 if (!$this->handlerInstance[$handlerKey]->isConnected()) {
02508 $dsn = $cfgArray['config']['driver'].':
02509 (strlen($cfgArray['config']['password']) ? ':XXXX@' : '').
02510 $cfgArray['config']['host'].(isset($cfgArray['config']['port']) ? ':'.$cfgArray['config']['port'] : '').'/'.$cfgArray['config']['database'].
02511 ($GLOBALS['TYPO3_CONF_VARS']['SYS']['no_pconnect'] ? '' : '?persistent=1');
02512 t3lib_div::sysLog('Could not connect to DB server using ADOdb on '.$cfgArray['config']['host'].' with user '.$cfgArray['config']['username'].'.','Core',4);
02513 error_log('DBAL error: Connection to '.$dsn.' failed. Maybe PHP doesn\'t support the database?');
02514 $output = FALSE;
02515 } else {
02516 $this->handlerInstance[$handlerKey]->DataDictionary = NewDataDictionary($this->handlerInstance[$handlerKey]);
02517 $this->handlerInstance[$handlerKey]->last_insert_id = 0;
02518 if (isset($cfgArray['config']['sequenceStart'])) {
02519 $this->handlerInstance[$handlerKey]->sequenceStart = $cfgArray['config']['sequenceStart'];
02520 } else {
02521 $this->handlerInstance[$handlerKey]->sequenceStart = 1;
02522 }
02523 }
02524 break;
02525 case 'userdefined':
02526
02527 $fileName = t3lib_div::getFileAbsFileName($cfgArray['config']['classFile']);
02528 if (@is_file($fileName)) {
02529 require_once($fileName);
02530 } else die('DBAL error: "'.$fileName.'" was not a file to include.');
02531
02532
02533 $this->handlerInstance[$handlerKey] = t3lib_div::makeInstance($cfgArray['config']['class']);
02534 $this->handlerInstance[$handlerKey]->init($cfgArray,$this);
02535
02536 if (is_object($this->handlerInstance[$handlerKey])) {
02537 $output = TRUE;
02538 }
02539 break;
02540 default:
02541 die('ERROR: Invalid handler type: "'.$cfgArray['type'].'"');
02542 break;
02543 }
02544
02545 return $output;
02546 } else die('ERROR: No handler for key "'.$handlerKey.'"');
02547 }
02548
02549
02550
02551
02552
02553
02554
02555 public function runningNative() {
02556 return ((string)$this->handlerCfg[$this->lastHandlerKey]['type']==='native');
02557 }
02558
02559
02560
02561
02562
02563
02564
02565
02566 public function runningADOdbDriver($driver) {
02567 return strstr($this->handlerCfg[$this->lastHandlerKey]['config']['driver'], $driver);
02568 }
02569
02570
02571
02572
02573
02574
02575
02576
02577
02578
02579
02580
02581
02582
02583
02584
02585
02586
02587
02588
02589
02590
02591
02592 protected function map_needMapping($tableList, $fieldMappingOnly = FALSE) {
02593 $key = $tableList.'|'.$fieldMappingOnly;
02594 if (!isset($this->cache_mappingFromTableList[$key])) {
02595 $this->cache_mappingFromTableList[$key] = FALSE;
02596
02597 $tables = $this->SQLparser->parseFromTables($tableList);
02598 if (is_array($tables)) {
02599 foreach ($tables as $tableCfg) {
02600 if ($fieldMappingOnly) {
02601 if (is_array($this->mapping[$tableCfg['table']]['mapFieldNames'])) {
02602 $this->cache_mappingFromTableList[$key] = $tables;
02603 } elseif (is_array($tableCfg['JOIN'])) {
02604 foreach ($tableCfg['JOIN'] as $join) {
02605 if (is_array($this->mapping[$join['withTable']]['mapFieldNames'])) {
02606 $this->cache_mappingFromTableList[$key] = $tables;
02607 break;
02608 }
02609 }
02610 }
02611 } else {
02612 if (is_array($this->mapping[$tableCfg['table']])) {
02613 $this->cache_mappingFromTableList[$key] = $tables;
02614 } elseif (is_array($tableCfg['JOIN'])) {
02615 foreach ($tableCfg['JOIN'] as $join) {
02616 if (is_array($this->mapping[$join['withTable']])) {
02617 $this->cache_mappingFromTableList[$key] = $tables;
02618 break;
02619 }
02620 }
02621 }
02622 }
02623 }
02624 }
02625 }
02626
02627 return $this->cache_mappingFromTableList[$key];
02628 }
02629
02630
02631
02632
02633
02634
02635
02636
02637
02638
02639
02640
02641 protected function map_assocArray($input, $tables, $rev = FALSE) {
02642
02643 foreach ($tables as $tableCfg) {
02644 if (is_array($this->mapping[$tableCfg['table']]['mapFieldNames'])) {
02645
02646
02647 if ($rev) {
02648 $theMap = array_flip($this->mapping[$tableCfg['table']]['mapFieldNames']);
02649 } else {
02650 $theMap = $this->mapping[$tableCfg['table']]['mapFieldNames'];
02651 }
02652
02653
02654 $output = array();
02655 foreach ($input as $fN => $value) {
02656
02657
02658 if ($theMap[$fN]) {
02659 $newKey = $theMap[$fN];
02660 } else {
02661 $newKey = $fN;
02662 }
02663
02664
02665 $output[$newKey] = $value;
02666 }
02667
02668
02669 $input = $output;
02670 }
02671 }
02672
02673
02674 return $input;
02675 }
02676
02677
02678
02679
02680
02681
02682
02683
02684
02685
02686
02687
02688
02689 protected function map_remapSELECTQueryParts(&$select_fields, &$from_table, &$where_clause, &$groupBy, &$orderBy) {
02690
02691 $tables = $this->SQLparser->parseFromTables($from_table);
02692 $defaultTable = $tables[0]['table'];
02693 foreach ($tables as $k => $v) {
02694 if ($this->mapping[$v['table']]['mapTableName']) {
02695 $tables[$k]['table'] = $this->mapping[$v['table']]['mapTableName'];
02696 }
02697
02698 if (is_array($v['JOIN'])) {
02699 foreach($v['JOIN'] as $joinCnt => $join) {
02700
02701 if ($this->mapping[$join['withTable']]['mapTableName']) {
02702 $tables[$k]['JOIN'][$joinCnt]['withTable'] = $this->mapping[$join['withTable']]['mapTableName'];
02703 }
02704 $onPartsArray = array();
02705
02706 if (is_array($join['ON'])) {
02707 foreach ($join['ON'] as $onParts) {
02708 if (isset($this->mapping[$onParts['table']]['mapFieldNames'][$onParts['field']])) {
02709 $onParts['field'] = $this->mapping[$onParts['table']]['mapFieldNames'][$onParts['field']];
02710 }
02711 if (isset($this->mapping[$onParts['table']]['mapTableName'])) {
02712 $onParts['table'] = $this->mapping[$onParts['table']]['mapTableName'];
02713 }
02714 $onPartsArray[] = $onParts;
02715 }
02716 $tables[$k]['JOIN'][$joinCnt]['ON'] = $onPartsArray;
02717 }
02718 }
02719 }
02720 }
02721 $from_table = $this->SQLparser->compileFromTables($tables);
02722
02723
02724 $whereParts = $this->SQLparser->parseWhereClause($where_clause);
02725 $this->map_sqlParts($whereParts,$defaultTable);
02726 $where_clause = $this->SQLparser->compileWhereClause($whereParts, FALSE);
02727
02728
02729 $expFields = $this->SQLparser->parseFieldList($select_fields);
02730 $this->map_sqlParts($expFields,$defaultTable);
02731 $select_fields = $this->SQLparser->compileFieldList($expFields, FALSE, FALSE);
02732
02733
02734 $expFields = $this->SQLparser->parseFieldList($groupBy);
02735 $this->map_sqlParts($expFields,$defaultTable);
02736 $groupBy = $this->SQLparser->compileFieldList($expFields);
02737
02738
02739 $expFields = $this->SQLparser->parseFieldList($orderBy);
02740 $this->map_sqlParts($expFields,$defaultTable);
02741 $orderBy = $this->SQLparser->compileFieldList($expFields);
02742 }
02743
02744
02745
02746
02747
02748
02749
02750
02751
02752
02753 protected function map_sqlParts(&$sqlPartArray, $defaultTable) {
02754
02755 if (is_array($sqlPartArray)) {
02756 foreach ($sqlPartArray as $k => $v) {
02757
02758 if (isset($sqlPartArray[$k]['type'])) {
02759 switch ($sqlPartArray[$k]['type']) {
02760 case 'flow-control':
02761 $temp = array($sqlPartArray[$k]['flow-control']);
02762 $this->map_sqlParts($temp, $defaultTable);
02763 $sqlPartArray[$k]['flow-control'] = $temp[0];
02764 break;
02765 case 'CASE':
02766 if (isset($sqlPartArray[$k]['case_field'])) {
02767 $fieldArray = explode('.', $sqlPartArray[$k]['case_field']);
02768 if (count($fieldArray) == 1 && is_array($this->mapping[$defaultTable]['mapFieldNames']) && isset($this->mapping[$defaultTable]['mapFieldNames'][$fieldArray[0]])) {
02769 $sqlPartArray[$k]['case_field'] = $this->mapping[$defaultTable]['mapFieldNames'][$fieldArray[0]];
02770 }
02771 elseif (count($fieldArray) == 2) {
02772
02773 $table = $fieldArray[0];
02774 if (isset($this->mapping[$fieldArray[0]]['mapTableName'])) {
02775 $table = $this->mapping[$fieldArray[0]]['mapTableName'];
02776 }
02777
02778 $field = $fieldArray[1];
02779 if (is_array($this->mapping[$fieldArray[0]]['mapFieldNames']) && isset($this->mapping[$fieldArray[0]]['mapFieldNames'][$fieldArray[1]])) {
02780 $field = $this->mapping[$fieldArray[0]]['mapFieldNames'][$fieldArray[1]];
02781 }
02782 $sqlPartArray[$k]['case_field'] = $table . '.' . $field;
02783 }
02784 }
02785 foreach ($sqlPartArray[$k]['when'] as $key => $when) {
02786 $this->map_sqlParts($sqlPartArray[$k]['when'][$key]['when_value'], $defaultTable);
02787 }
02788 break;
02789 }
02790 }
02791
02792
02793 if (is_array($sqlPartArray[$k]['sub'])) {
02794 $this->map_sqlParts($sqlPartArray[$k]['sub'], $defaultTable);
02795 } elseif (isset($sqlPartArray[$k]['func'])) {
02796 switch ($sqlPartArray[$k]['func']['type']) {
02797 case 'EXISTS':
02798 $subqueryDefaultTable = $sqlPartArray[$k]['func']['subquery']['FROM'][0]['table'];
02799 $this->map_sqlParts($sqlPartArray[$k]['func']['subquery']['SELECT'], $subqueryDefaultTable);
02800 $this->map_sqlParts($sqlPartArray[$k]['func']['subquery']['FROM'], $subqueryDefaultTable);
02801 $this->map_sqlParts($sqlPartArray[$k]['func']['subquery']['WHERE'], $subqueryDefaultTable);
02802 break;
02803 case 'IFNULL':
02804 case 'LOCATE':
02805
02806 $t = $sqlPartArray[$k]['func']['table'] ? $sqlPartArray[$k]['func']['table'] : $defaultTable;
02807 if (is_array($this->mapping[$t]['mapFieldNames']) && $this->mapping[$t]['mapFieldNames'][$sqlPartArray[$k]['func']['field']]) {
02808 $sqlPartArray[$k]['func']['field'] = $this->mapping[$t]['mapFieldNames'][$sqlPartArray[$k]['func']['field']];
02809 }
02810 if ($this->mapping[$t]['mapTableName']) {
02811 $sqlPartArray[$k]['func']['table'] = $this->mapping[$t]['mapTableName'];
02812 }
02813 break;
02814 }
02815 } else {
02816
02817 $t = $sqlPartArray[$k]['table'] ? $sqlPartArray[$k]['table'] : $defaultTable;
02818
02819
02820 if (is_array($this->mapping[$t]['mapFieldNames']) && isset($this->mapping[$t]['mapFieldNames'][$sqlPartArray[$k]['field']])) {
02821 $sqlPartArray[$k]['field'] = $this->mapping[$t]['mapFieldNames'][$sqlPartArray[$k]['field']];
02822 }
02823
02824
02825 if ($this->mapping[$t]['mapFieldNames']) {
02826 $fieldArray = explode('.', $sqlPartArray[$k]['func_content']);
02827 if (count($fieldArray) == 1 && is_array($this->mapping[$t]['mapFieldNames']) && isset($this->mapping[$t]['mapFieldNames'][$fieldArray[0]])) {
02828 $sqlPartArray[$k]['func_content.'][0]['func_content'] = $this->mapping[$t]['mapFieldNames'][$fieldArray[0]];
02829 $sqlPartArray[$k]['func_content'] = $this->mapping[$t]['mapFieldNames'][$fieldArray[0]];
02830 }
02831 elseif (count($fieldArray) == 2) {
02832
02833 $table = $fieldArray[0];
02834 if (isset($this->mapping[$fieldArray[0]]['mapTableName'])) {
02835 $table = $this->mapping[$fieldArray[0]]['mapTableName'];
02836 }
02837
02838 $field = $fieldArray[1];
02839 if (is_array($this->mapping[$fieldArray[0]]['mapFieldNames']) && isset($this->mapping[$fieldArray[0]]['mapFieldNames'][$fieldArray[1]])) {
02840 $field = $this->mapping[$fieldArray[0]]['mapFieldNames'][$fieldArray[1]];
02841 }
02842 $sqlPartArray[$k]['func_content.'][0]['func_content'] = $table . '.' . $field;
02843 $sqlPartArray[$k]['func_content'] = $table . '.' . $field;
02844 }
02845
02846
02847 if (isset($sqlPartArray[$k]['flow-control'])) {
02848 if (isset($sqlPartArray[$k]['flow-control']['type'])) {
02849 $temp = array($sqlPartArray[$k]['flow-control']);
02850 $this->map_sqlParts($temp, $t);
02851 $sqlPartArray[$k]['flow-control'] = $temp[0];
02852 }
02853 }
02854 }
02855
02856
02857 if (isset($v['value']['operator'])) {
02858 foreach ($sqlPartArray[$k]['value']['args'] as $argK => $fieldDef) {
02859 if (isset($this->mapping[$fieldDef['table']]['mapTableName'])) {
02860 $sqlPartArray[$k]['value']['args'][$argK]['table'] = $this->mapping[$fieldDef['table']]['mapTableName'];
02861 }
02862 if (is_array($this->mapping[$fieldDef['table']]['mapFieldNames']) && isset($this->mapping[$fieldDef['table']]['mapFieldNames'][$fieldDef['field']])) {
02863 $sqlPartArray[$k]['value']['args'][$argK]['field'] = $this->mapping[$fieldDef['table']]['mapFieldNames'][$fieldDef['field']];
02864 }
02865 }
02866 }
02867
02868
02869 if (isset($sqlPartArray[$k]['subquery'])) {
02870 $subqueryDefaultTable = $sqlPartArray[$k]['subquery']['FROM'][0]['table'];
02871 $this->map_sqlParts($sqlPartArray[$k]['subquery']['SELECT'], $subqueryDefaultTable);
02872 $this->map_sqlParts($sqlPartArray[$k]['subquery']['FROM'], $subqueryDefaultTable);
02873 $this->map_sqlParts($sqlPartArray[$k]['subquery']['WHERE'], $subqueryDefaultTable);
02874 }
02875
02876
02877
02878 if (!is_numeric($sqlPartArray[$k]['value'][0]) && !isset($sqlPartArray[$k]['value'][1])) {
02879 $fieldArray = explode('.', $sqlPartArray[$k]['value'][0]);
02880 if (count($fieldArray) == 1 && is_array($this->mapping[$t]['mapFieldNames']) && isset($this->mapping[$t]['mapFieldNames'][$fieldArray[0]])) {
02881 $sqlPartArray[$k]['value'][0] = $this->mapping[$t]['mapFieldNames'][$fieldArray[0]];
02882 } elseif (count($fieldArray) == 2) {
02883
02884 $table = $fieldArray[0];
02885 if (isset($this->mapping[$fieldArray[0]]['mapTableName'])) {
02886 $table = $this->mapping[$fieldArray[0]]['mapTableName'];
02887 }
02888
02889 $field = $fieldArray[1];
02890 if (is_array($this->mapping[$fieldArray[0]]['mapFieldNames']) && isset($this->mapping[$fieldArray[0]]['mapFieldNames'][$fieldArray[1]])) {
02891 $field = $this->mapping[$fieldArray[0]]['mapFieldNames'][$fieldArray[1]];
02892 }
02893 $sqlPartArray[$k]['value'][0] = $table . '.' . $field;
02894 }
02895 }
02896
02897
02898 if ($sqlPartArray[$k]['table'] && $this->mapping[$sqlPartArray[$k]['table']]['mapTableName']) {
02899 $sqlPartArray[$k]['table'] = $this->mapping[$sqlPartArray[$k]['table']]['mapTableName'];
02900 }
02901 }
02902 }
02903 }
02904 }
02905
02906
02907
02908
02909
02910
02911
02912
02913
02914 protected function map_genericQueryParsed(&$parsedQuery) {
02915
02916
02917 $table = $parsedQuery['TABLE'];
02918 if ($table) {
02919
02920 if ($tableArray = $this->map_needMapping($table)) {
02921
02922
02923 if ($this->mapping[$table]['mapTableName']) {
02924 $parsedQuery['TABLE'] = $this->mapping[$table]['mapTableName'];
02925 }
02926
02927
02928 switch ($parsedQuery['type']) {
02929 case 'ALTERTABLE':
02930
02931
02932 $newFieldName = $this->mapping[$table]['mapFieldNames'][$parsedQuery['FIELD']];
02933 if ($newFieldName) {
02934 if ($parsedQuery['FIELD'] == $parsedQuery['newField']) {
02935 $parsedQuery['FIELD'] = $parsedQuery['newField'] = $newFieldName;
02936 } else $parsedQuery['FIELD'] = $newFieldName;
02937 }
02938
02939
02940 if (is_array($parsedQuery['fields'])) {
02941 $this->map_fieldNamesInArray($table,$parsedQuery['fields']);
02942 }
02943 break;
02944 case 'CREATETABLE':
02945
02946 if (is_array($parsedQuery['FIELDS'])) {
02947 $newFieldsArray = array();
02948 foreach ($parsedQuery['FIELDS'] as $fN => $fInfo) {
02949 if ($this->mapping[$table]['mapFieldNames'][$fN]) {
02950 $fN = $this->mapping[$table]['mapFieldNames'][$fN];
02951 }
02952 $newFieldsArray[$fN] = $fInfo;
02953 }
02954 $parsedQuery['FIELDS'] = $newFieldsArray;
02955 }
02956
02957
02958 if (is_array($parsedQuery['KEYS'])) {
02959 foreach ($parsedQuery['KEYS'] as $kN => $kInfo) {
02960 $this->map_fieldNamesInArray($table,$parsedQuery['KEYS'][$kN]);
02961 }
02962 }
02963 break;
02964
02965
02966
02967 }
02968 }
02969 } else die('ERROR, mapping: No table found in parsed Query array...');
02970 }
02971
02972
02973
02974
02975
02976
02977
02978
02979 protected function map_fieldNamesInArray($table,&$fieldArray) {
02980 if (is_array($this->mapping[$table]['mapFieldNames'])) {
02981 foreach ($fieldArray as $k => $v) {
02982 if ($this->mapping[$table]['mapFieldNames'][$v]) {
02983 $fieldArray[$k] = $this->mapping[$table]['mapFieldNames'][$v];
02984 }
02985 }
02986 }
02987 }
02988
02989
02990
02991
02992
02993
02994
02995
02996
02997
02998
02999
03000
03001
03002
03003
03004
03005
03006
03007
03008
03009
03010
03011
03012
03013
03014
03015
03016
03017
03018
03019
03020 public function debugHandler($function,$execTime,$inData) {
03021
03022 $script = substr(PATH_thisScript,strlen(PATH_site));
03023
03024 if (substr($script,-strlen('dbal/mod1/index.php'))!='dbal/mod1/index.php' && !strstr($inData['args'][0], 'tx_dbal_debuglog')) {
03025 $data = array();
03026 $errorFlag = 0;
03027 $joinTable = '';
03028
03029 if ($this->sql_error()) {
03030 $data['sqlError'] = $this->sql_error();
03031 $errorFlag|=1;
03032 }
03033
03034
03035 if (empty($this->lastQuery))
03036 $query = implode(' ',$inData['args']);
03037 else
03038 $query = $this->lastQuery;
03039
03040 if ($this->conf['debugOptions']['backtrace']) {
03041 $backtrace = debug_backtrace();
03042 unset($backtrace[0]);
03043 $data['backtrace'] = array_slice($backtrace, 0, $this->conf['debugOptions']['backtrace']);
03044 }
03045
03046 switch ($function) {
03047 case 'exec_INSERTquery':
03048 case 'exec_UPDATEquery':
03049 case 'exec_DELETEquery':
03050 $this->debug_log($query,$execTime,$data,$joinTable,$errorFlag, $script);
03051 break;
03052
03053 case 'exec_SELECTquery':
03054
03055 if ($this->conf['debugOptions']['EXPLAIN'] && t3lib_div::inList('adodb,native',$inData['handlerType'])) {
03056 $data['EXPLAIN'] = $this->debug_explain($this->lastQuery);
03057 }
03058
03059
03060 if ($this->conf['debugOptions']['parseQuery']) {
03061 $parseResults = array();
03062 $parseResults['SELECT'] = $this->SQLparser->debug_parseSQLpart('SELECT',$inData['args'][1]);
03063 $parseResults['FROM'] = $this->SQLparser->debug_parseSQLpart('FROM',$inData['args'][0]);
03064 $parseResults['WHERE'] = $this->SQLparser->debug_parseSQLpart('WHERE',$inData['args'][2]);
03065 $parseResults['GROUPBY'] = $this->SQLparser->debug_parseSQLpart('SELECT',$inData['args'][3]);
03066 $parseResults['ORDERBY'] = $this->SQLparser->debug_parseSQLpart('SELECT',$inData['args'][4]);
03067
03068 foreach ($parseResults as $k => $v) {
03069 if (!strlen($parseResults[$k])) unset($parseResults[$k]);
03070 }
03071 if (count($parseResults)) {
03072 $data['parseError'] = $parseResults;
03073 $errorFlag|=2;
03074 }
03075 }
03076
03077
03078 if ($this->conf['debugOptions']['joinTables']) {
03079 if (count(explode(',', $inData['ORIG_from_table']))>1) {
03080 $joinTable = $inData['args'][0];
03081 }
03082 }
03083
03084
03085 $this->debug_log($query,$execTime,$data,$joinTable,$errorFlag, $script);
03086 if (!empty($inData['args'][2]))
03087 $this->debug_WHERE($inData['args'][0], $inData['args'][2], $script);
03088 break;
03089 }
03090 }
03091 }
03092
03093
03094
03095
03096
03097
03098
03099
03100
03101 public function debug_WHERE($table, $where, $script = '') {
03102 $insertArray = array (
03103 'tstamp' => $GLOBALS['EXEC_TIME'],
03104 'beuser_id' => intval($GLOBALS['BE_USER']->user['uid']),
03105 'script' => $script,
03106 'tablename' => $table,
03107 'whereclause' => $where
03108 );
03109
03110 $this->exec_INSERTquery('tx_dbal_debuglog_where', $insertArray);
03111 }
03112
03113
03114
03115
03116
03117
03118
03119
03120
03121
03122
03123
03124 public function debug_log($query,$ms,$data,$join,$errorFlag, $script='') {
03125 if (is_array($query)) {
03126 $queryToLog = $query[0].' -- ';
03127 if (count($query[1])) {
03128 $queryToLog .= count($query[1]).' BLOB FIELDS: '.implode(', ',array_keys($query[1]));
03129 }
03130 if (count($query[2])) {
03131 $queryToLog .= count($query[2]).' CLOB FIELDS: '.implode(', ',array_keys($query[2]));
03132 }
03133 } else {
03134 $queryToLog = $query;
03135 }
03136 $insertArray = array (
03137 'tstamp' => $GLOBALS['EXEC_TIME'],
03138 'beuser_id' => intval($GLOBALS['BE_USER']->user['uid']),
03139 'script' => $script,
03140 'exec_time' => $ms,
03141 'table_join' => $join,
03142 'serdata' => serialize($data),
03143 'query' => $queryToLog,
03144 'errorFlag' => $errorFlag
03145 );
03146
03147 $this->exec_INSERTquery('tx_dbal_debuglog', $insertArray);
03148 }
03149
03150
03151
03152
03153
03154
03155
03156
03157 public function debug_explain($query) {
03158 $output = array();
03159 $hType = (string)$this->handlerCfg[$this->lastHandlerKey]['type'];
03160 switch ($hType) {
03161 case 'native':
03162 $res = $this->sql_query('EXPLAIN '.$query);
03163 while($row = $this->sql_fetch_assoc($res)) {
03164 $output[] = $row;
03165 }
03166 break;
03167 case 'adodb':
03168 switch ($this->handlerCfg['_DEFAULT']['config']['driver']) {
03169 case 'oci8':
03170 $res = $this->sql_query('EXPLAIN PLAN '.$query);
03171 $output[] = 'EXPLAIN PLAN data logged to default PLAN_TABLE';
03172 break;
03173 default:
03174 $res = $this->sql_query('EXPLAIN '.$query);
03175 while($row = $this->sql_fetch_assoc($res)) {
03176 $output[] = $row;
03177 }
03178 break;
03179 }
03180 break;
03181 }
03182
03183 return $output;
03184 }
03185 }
03186
03187
03188 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/dbal/class.ux_t3lib_db.php']) {
03189 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/dbal/class.ux_t3lib_db.php']);
03190 }
03191
03192 ?>