44 $this->deletedPrefixKey = $prefix;
66 foreach ($lines as $value) {
67 if ($value[0] ===
'#') {
71 if (!strlen($table)) {
73 if (strtoupper($parts[0]) ===
'CREATE' && strtoupper($parts[1]) ===
'TABLE') {
74 $table = str_replace(
'`',
'', $parts[2]);
76 if (TYPO3_OS ==
'WIN') {
77 $table = strtolower($table);
81 if ($value[0] ===
')' && substr($value, -1) ===
';') {
83 if (preg_match(
'/(ENGINE|TYPE)[ ]*=[ ]*([a-zA-Z]*)/', $value, $ttype)) {
84 $total[$table][
'extra'][
'ENGINE'] = $ttype[2];
88 if (preg_match(
'/(COLLATE)[ ]*=[ ]*([a-zA-z0-9_-]+)/', $value, $tcollation)) {
89 $total[$table][
'extra'][
'COLLATE'] = $tcollation[2];
92 if (preg_match(
'/(CHARSET|CHARACTER SET)[ ]*=[ ]*([a-zA-z0-9_-]+)/', $value, $tcharset)) {
94 $charset = $tcharset[2];
96 $charset =
$GLOBALS[
'TYPO3_DB']->default_charset;
104 $lineV = preg_replace(
'/,$/',
'', $value);
105 $lineV = str_replace(
'`',
'', $lineV);
107 $lineV = preg_replace(
'/[ \t]+/',
' ', $lineV);
108 $parts = explode(
' ', $lineV, 2);
110 if (!preg_match(
'/(PRIMARY|UNIQUE|FULLTEXT|INDEX|KEY)/', $parts[0])) {
112 if (stristr($parts[1],
'auto_increment')) {
113 $parts[1] = preg_replace(
'/ default \'0\'/i',
'', $parts[1]);
116 if (stristr($parts[1],
' DEFAULT ')) {
117 $parts[1] = str_ireplace(
' DEFAULT ',
' default ', $parts[1]);
120 $parts[1] = preg_replace(
'/(.*) (default .*) (NOT NULL)/',
'$1 $3 $2', $parts[1]);
121 $parts[1] = preg_replace(
'/(.*) (default .*) (NULL)/',
'$1 $3 $2', $parts[1]);
123 $total[$table][
'fields'][$key] = $parts[1];
126 $search = array(
'/UNIQUE (INDEX|KEY)/',
'/FULLTEXT (INDEX|KEY)/',
'/INDEX/');
127 $replace = array(
'UNIQUE',
'FULLTEXT',
'KEY');
128 $lineV = preg_replace($search, $replace, $lineV);
129 if (preg_match(
'/PRIMARY|UNIQUE|FULLTEXT/', $parts[0])) {
130 $parts[1] = preg_replace(
'/^(KEY|INDEX) /',
'', $parts[1]);
132 $newParts = explode(
' ', $parts[1], 2);
133 $key = $parts[0] ==
'PRIMARY' ? $parts[0] : $newParts[0];
134 $total[$table][
'keys'][$key] = $lineV;
136 if (preg_match(
'/^(cache|index)_/', $table)) {
138 $total[$table][
'extra'][
'CLEAR'] = 1;
155 if (!count($this->character_sets)) {
156 if (method_exists(
$GLOBALS[
'TYPO3_DB'],
'admin_get_charsets')) {
157 $this->character_sets =
$GLOBALS[
'TYPO3_DB']->admin_get_charsets();
160 $this->character_sets[$charset] = array();
164 if (isset($this->character_sets[$charset][
'Default collation'])) {
165 $collation = $this->character_sets[$charset][
'Default collation'];
178 $tempKeysPrefix = array();
180 echo
$GLOBALS[
'TYPO3_DB']->sql_error();
181 $tables =
$GLOBALS[
'TYPO3_DB']->admin_get_tables();
182 foreach ($tables as $tableName => $tableStatus) {
184 $fieldInformation =
$GLOBALS[
'TYPO3_DB']->admin_get_fields($tableName);
185 foreach ($fieldInformation as $fN => $fieldRow) {
189 $keyInformation =
$GLOBALS[
'TYPO3_DB']->admin_get_keys($tableName);
190 foreach ($keyInformation as $keyRow) {
191 $keyName = $keyRow[
'Key_name'];
192 $colName = $keyRow[
'Column_name'];
193 if ($keyRow[
'Sub_part']) {
194 $colName .=
'(' . $keyRow[
'Sub_part'] .
')';
196 $tempKeys[$tableName][$keyName][$keyRow[
'Seq_in_index']] = $colName;
197 if ($keyName ==
'PRIMARY') {
198 $prefix =
'PRIMARY KEY';
200 if ($keyRow[
'Index_type'] ==
'FULLTEXT') {
201 $prefix =
'FULLTEXT';
202 } elseif ($keyRow[
'Non_unique']) {
207 $prefix .=
' ' . $keyName;
209 $tempKeysPrefix[$tableName][$keyName] = $prefix;
212 if (is_array($tableStatus)) {
213 $tableExtraFields = array(
214 'Engine' =>
'ENGINE',
215 'Collation' =>
'COLLATE' 217 foreach ($tableExtraFields as $mysqlKey => $internalKey) {
218 if (isset($tableStatus[$mysqlKey])) {
219 $total[$tableName][
'extra'][$internalKey] = $tableStatus[$mysqlKey];
225 if (count($tempKeys)) {
226 foreach ($tempKeys as $table => $keyInf) {
227 foreach ($keyInf as $kName => $index) {
229 $total[$table][
'keys'][$kName] = $tempKeysPrefix[$table][$kName] .
' (' . implode(
',', $index) .
')';
246 public function getDatabaseExtra($FDsrc, $FDcomp, $onlyTableList =
'', $ignoreNotNullWhenComparing = FALSE) {
249 if (is_array($FDsrc)) {
250 foreach ($FDsrc as $table => $info) {
252 if (!isset($FDcomp[$table])) {
254 $extraArr[$table] = $info;
255 $extraArr[$table][
'whole_table'] = 1;
257 $keyTypes = explode(
',',
'extra,fields,keys');
258 foreach ($keyTypes as $theKey) {
259 if (is_array($info[$theKey])) {
260 foreach ($info[$theKey] as $fieldN => $fieldC) {
261 $fieldN = str_replace(
'`',
'', $fieldN);
262 if ($fieldN ==
'COLLATE') {
266 if (!isset($FDcomp[$table][$theKey][$fieldN])) {
267 $extraArr[$table][$theKey][$fieldN] = $fieldC;
269 $fieldC = trim($fieldC);
276 $fieldC = preg_replace_callback(
277 '/^([a-zA-Z0-9]+)(\([^)]*\)\s.*)/',
278 function($matches) {
return strtolower($matches[1]) . $matches[2]; },
282 if ($ignoreNotNullWhenComparing) {
283 $fieldC = str_replace(
' NOT NULL',
'', $fieldC);
284 $FDcomp[$table][$theKey][$fieldN] = str_replace(
' NOT NULL',
'', $FDcomp[$table][$theKey][$fieldN]);
286 if ($fieldC !== $FDcomp[$table][$theKey][$fieldN]) {
287 $diffArr[$table][$theKey][$fieldN] = $fieldC;
288 $diffArr_cur[$table][$theKey][$fieldN] = $FDcomp[$table][$theKey][$fieldN];
299 'extra' => $extraArr,
301 'diff_currentValues' => $diffArr_cur
314 $statements = array();
318 if ($keyList ==
'remove') {
322 $keyList = explode(
',', $keyList);
323 foreach ($keyList as $theKey) {
324 if (is_array($diffArr[$theKey])) {
325 foreach ($diffArr[$theKey] as $table => $info) {
326 $whole_table = array();
327 if (isset($info[
'keys']) && is_array($info[
'keys'])) {
328 foreach ($info[
'keys'] as $fN => $fV) {
329 if (!$info[
'whole_table'] && $theKey ===
'extra' && $remove) {
330 $statement =
'ALTER TABLE ' . $table . ($fN ===
'PRIMARY' ?
' DROP PRIMARY KEY' :
' DROP KEY ' . $fN) .
';';
331 $statements[
'drop'][md5($statement)] = $statement;
335 if (is_array($info[
'fields'])) {
336 foreach ($info[
'fields'] as $fN => $fV) {
337 if ($info[
'whole_table']) {
338 $whole_table[] = $fN .
' ' . $fV;
341 if (stristr($fV,
'auto_increment')) {
344 if (isset($info[
'keys'][
'PRIMARY'])) {
347 $fV .=
', ADD PRIMARY KEY (' . $fN .
')';
348 unset($info[
'keys'][
'PRIMARY']);
352 $info[
'extra'][
'CLEAR'] = 2;
355 if ($theKey ==
'extra') {
359 $prefixedFieldName =
$deletedPrefixKey . substr($fN, ($deletedPrefixLength - self::MYSQL_MAXIMUM_FIELD_WIDTH));
360 $statement =
'ALTER TABLE ' . $table .
' CHANGE ' . $fN .
' ' . $prefixedFieldName .
' ' . $fV .
';';
361 $statements[
'change'][md5($statement)] = $statement;
363 $statement =
'ALTER TABLE ' . $table .
' DROP ' . $fN .
';';
364 $statements[
'drop'][md5($statement)] = $statement;
367 $statement =
'ALTER TABLE ' . $table .
' ADD ' . $fN .
' ' . $fV .
';';
368 $statements[
'add'][md5($statement)] = $statement;
370 } elseif ($theKey ==
'diff') {
371 $statement =
'ALTER TABLE ' . $table .
' CHANGE ' . $fN .
' ' . $fN .
' ' . $fV .
';';
372 $statements[
'change'][md5($statement)] = $statement;
373 $statements[
'change_currentValue'][md5($statement)] = $diffArr[
'diff_currentValues'][$table][
'fields'][$fN];
378 if (is_array($info[
'keys'])) {
379 foreach ($info[
'keys'] as $fN => $fV) {
380 if ($info[
'whole_table']) {
381 $whole_table[] = $fV;
383 if ($theKey ==
'extra') {
385 $statement =
'ALTER TABLE ' . $table .
' ADD ' . $fV .
';';
386 $statements[
'add'][md5($statement)] = $statement;
388 } elseif ($theKey ==
'diff') {
389 $statement =
'ALTER TABLE ' . $table . ($fN ==
'PRIMARY' ?
' DROP PRIMARY KEY' :
' DROP KEY ' . $fN) .
';';
390 $statements[
'change'][md5($statement)] = $statement;
391 $statement =
'ALTER TABLE ' . $table .
' ADD ' . $fV .
';';
392 $statements[
'change'][md5($statement)] = $statement;
397 if (is_array($info[
'extra'])) {
399 $extras_currentValue = array();
400 $clear_table = FALSE;
401 foreach ($info[
'extra'] as $fN => $fV) {
404 if (!$info[
'whole_table']) {
406 if ($fN ==
'CLEAR') {
409 if (count($info[
'keys']) || $fV == 2) {
414 $extras[] = $fN .
'=' . $fV;
415 $extras_currentValue[] = $fN .
'=' . $diffArr[
'diff_currentValues'][$table][
'extra'][$fN];
421 $statement =
'TRUNCATE TABLE ' . $table .
';';
422 $statements[
'clear_table'][md5($statement)] = $statement;
424 if (count($extras)) {
425 $statement =
'ALTER TABLE ' . $table .
' ' . implode(
' ', $extras) .
';';
426 $statements[
'change'][md5($statement)] = $statement;
427 $statements[
'change_currentValue'][md5($statement)] = implode(
' ', $extras_currentValue);
430 if ($info[
'whole_table']) {
434 $prefixedTableName =
$deletedPrefixKey . substr($table, ($deletedPrefixLength - self::MYSQL_MAXIMUM_FIELD_WIDTH));
435 $statement =
'ALTER TABLE ' . $table .
' RENAME ' . $prefixedTableName .
';';
436 $statements[
'change_table'][md5($statement)] = $statement;
438 $statement =
'DROP TABLE ' . $table .
';';
439 $statements[
'drop_table'][md5($statement)] = $statement;
442 $count =
$GLOBALS[
'TYPO3_DB']->exec_SELECTcountRows(
'*', $table);
443 $statements[
'tables_count'][md5($statement)] = $count ?
'Records in table: ' . $count :
'';
445 $statement =
'CREATE TABLE ' . $table .
' ( 449 if ($info[
'extra']) {
450 foreach ($info[
'extra'] as $k => $v) {
451 if ($k ==
'COLLATE' || $k ==
'CLEAR') {
456 $statement .=
' ' . $k .
'=' . $v;
460 $statements[
'create_table'][md5($statement)] = $statement;
476 $field = array($row[
'Type']);
477 if ($row[
'Null'] ==
'NO') {
478 $field[] =
'NOT NULL';
480 if (!strstr($row[
'Type'],
'blob') && !strstr($row[
'Type'],
'text')) {
482 if (!stristr($row[
'Extra'],
'auto_increment')) {
483 if ($row[
'Default'] === NULL) {
484 $field[] =
'default NULL';
486 $field[] =
'default \'' . addslashes($row[
'Default']) .
'\'';
491 $field[] = $row[
'Extra'];
493 return implode(
' ', $field);
505 $sqlcodeArr = explode(LF, $sqlcode);
507 $statementArray = array();
508 $statementArrayPointer = 0;
509 foreach ($sqlcodeArr as $line => $lineContent) {
510 $lineContent = trim($lineContent);
513 if (stristr($lineContent,
'auto_increment')) {
514 $lineContent = preg_replace(
'/ default \'0\'/i',
'', $lineContent);
516 if (!$removeNonSQL || $lineContent !==
'' && $lineContent[0] !==
'#' && substr($lineContent, 0, 2) !==
'--') {
518 $statementArray[$statementArrayPointer] .= $lineContent;
521 if (substr($lineContent, -1) ===
';') {
522 if (isset($statementArray[$statementArrayPointer])) {
523 if (!trim($statementArray[$statementArrayPointer]) || $query_regex && !preg_match((
'/' . $query_regex .
'/i'), trim($statementArray[$statementArrayPointer]))) {
524 unset($statementArray[$statementArrayPointer]);
527 $statementArrayPointer++;
529 $statementArray[$statementArrayPointer] .= LF;
532 return $statementArray;
544 $insertCount = array();
545 foreach ($statements as $line => $lineContent) {
547 if (preg_match(
'/^create[[:space:]]*table[[:space:]]*[`]?([[:alnum:]_]*)[`]?/i', substr($lineContent, 0, 100), $reg)) {
548 $table = trim($reg[1]);
551 if (TYPO3_OS ==
'WIN') {
552 $table = strtolower($table);
554 $sqlLines = explode(LF, $lineContent);
555 foreach ($sqlLines as $k => $v) {
556 if (stristr($v,
'auto_increment')) {
557 $sqlLines[$k] = preg_replace(
'/ default \'0\'/i',
'', $v);
560 $lineContent = implode(LF, $sqlLines);
561 $crTables[$table] = $lineContent;
563 } elseif ($insertCountFlag && preg_match(
'/^insert[[:space:]]*into[[:space:]]*[`]?([[:alnum:]_]*)[`]?/i', substr($lineContent, 0, 100), $reg)) {
564 $nTable = trim($reg[1]);
565 $insertCount[$nTable]++;
568 return array($crTables, $insertCount);
579 $outStatements = array();
580 foreach ($statements as $line => $lineContent) {
582 if (preg_match(
'/^insert[[:space:]]*into[[:space:]]*[`]?([[:alnum:]_]*)[`]?/i', substr($lineContent, 0, 100), $reg)) {
583 $nTable = trim($reg[1]);
584 if ($nTable && $table === $nTable) {
585 $outStatements[] = $lineContent;
589 return $outStatements;
601 if (is_array($arr)) {
602 foreach ($arr as $key => $string) {
603 if (isset($keyArr[$key]) && $keyArr[$key]) {
604 $res =
$GLOBALS[
'TYPO3_DB']->admin_query($string);
605 if ($res === FALSE) {
607 } elseif (is_resource($res) || is_a($res,
'\\mysqli_result')) {
608 $GLOBALS[
'TYPO3_DB']->sql_free_result($res);
627 $whichTables =
$GLOBALS[
'TYPO3_DB']->admin_get_tables(TYPO3_db);
628 foreach ($whichTables as $key => &$value) {
641 return \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded(
'dbal');
getStatementArray($sqlcode, $removeNonSQL=FALSE, $query_regex='')
assembleFieldDefinition($row)
getUpdateSuggestions($diffArr, $keyList='extra, diff')
getTableInsertStatements($statements, $table)
static trimExplode($delim, $string, $removeEmptyValues=FALSE, $limit=0)
getCreateTables($statements, $insertCountFlag=FALSE)
getFieldDefinitions_database()
if($list_of_literals) if(!empty($literals)) if(!empty($literals)) $result
Analyse literals to prepend the N char to them if their contents aren't numeric.
getCollationForCharset($charset)
performUpdateQueries($arr, $keyArr)
setDeletedPrefixKey($prefix)
getFieldDefinitions_fileContent($fileContent)
getDatabaseExtra($FDsrc, $FDcomp, $onlyTableList='', $ignoreNotNullWhenComparing=FALSE)
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]
static inList($list, $item)
const MYSQL_MAXIMUM_FIELD_WIDTH