Code style changed:

- added more curly braces
- made SQL keywords all-uppercase
- added spaces between if/foreach and brace
- // Is for single-line comments *only*, please don't abuse it for multiple
  line comments, use /* */ instead and a asterisk in front of every line.

Signed-off-by: Roland Häder <roland@mxchange.org>
This commit is contained in:
Roland Häder 2016-12-22 16:50:40 +01:00
parent c9194b0381
commit 30642756e5
1 changed files with 54 additions and 37 deletions

View File

@ -26,7 +26,6 @@ function update_fail($update_id, $error_message){
} }
// every admin could had different language // every admin could had different language
foreach ($adminlist as $admin) { foreach ($adminlist as $admin) {
$lang = (($admin['language'])?$admin['language']:'en'); $lang = (($admin['language'])?$admin['language']:'en');
push_lang($lang); push_lang($lang);
@ -83,8 +82,9 @@ function table_structure($table) {
if (dbm::is_result($indexes)) if (dbm::is_result($indexes))
foreach ($indexes AS $index) { foreach ($indexes AS $index) {
if ($index["Index_type"] == "FULLTEXT") if ($index["Index_type"] == "FULLTEXT") {
continue; continue;
}
if ($index['Key_name'] != 'PRIMARY' && $index['Non_unique'] == '0' && !isset($indexdata[$index["Key_name"]])) { if ($index['Key_name'] != 'PRIMARY' && $index['Non_unique'] == '0' && !isset($indexdata[$index["Key_name"]])) {
$indexdata[$index["Key_name"]] = array('UNIQUE'); $indexdata[$index["Key_name"]] = array('UNIQUE');
@ -95,26 +95,31 @@ function table_structure($table) {
// To avoid the need to add this to every index definition we just ignore it here. // To avoid the need to add this to every index definition we just ignore it here.
// Exception are primary indexes // Exception are primary indexes
// Since there are some combindex primary indexes we use the limit of 180 here. // Since there are some combindex primary indexes we use the limit of 180 here.
if (($index["Sub_part"] != "") AND (($index["Sub_part"] < 180) OR ($index["Key_name"] == "PRIMARY"))) if (($index["Sub_part"] != "") AND (($index["Sub_part"] < 180) OR ($index["Key_name"] == "PRIMARY"))) {
$column .= "(".$index["Sub_part"].")"; $column .= "(".$index["Sub_part"].")";
}
$indexdata[$index["Key_name"]][] = $column; $indexdata[$index["Key_name"]][] = $column;
} }
if (dbm::is_result($structures)) { if (dbm::is_result($structures)) {
foreach($structures AS $field) { foreach ($structures AS $field) {
$fielddata[$field["Field"]]["type"] = $field["Type"]; $fielddata[$field["Field"]]["type"] = $field["Type"];
if ($field["Null"] == "NO") if ($field["Null"] == "NO") {
$fielddata[$field["Field"]]["not null"] = true; $fielddata[$field["Field"]]["not null"] = true;
}
if (isset($field["Default"])) if (isset($field["Default"])) {
$fielddata[$field["Field"]]["default"] = $field["Default"]; $fielddata[$field["Field"]]["default"] = $field["Default"];
}
if ($field["Extra"] != "") if ($field["Extra"] != "") {
$fielddata[$field["Field"]]["extra"] = $field["Extra"]; $fielddata[$field["Field"]]["extra"] = $field["Extra"];
}
if ($field["Key"] == "PRI") if ($field["Key"] == "PRI") {
$fielddata[$field["Field"]]["primary"] = true; $fielddata[$field["Field"]]["primary"] = true;
}
} }
} }
return(array("fields"=>$fielddata, "indexes"=>$indexdata)); return(array("fields"=>$fielddata, "indexes"=>$indexdata));
@ -138,13 +143,15 @@ function print_structure($database, $charset) {
function update_structure($verbose, $action, $tables=null, $definition=null) { function update_structure($verbose, $action, $tables=null, $definition=null) {
global $a, $db; global $a, $db;
if ($action) if ($action) {
set_config('system', 'maintenance', 1); set_config('system', 'maintenance', 1);
}
if (isset($a->config["system"]["db_charset"])) if (isset($a->config["system"]["db_charset"])) {
$charset = $a->config["system"]["db_charset"]; $charset = $a->config["system"]["db_charset"];
else } else {
$charset = "utf8"; $charset = "utf8";
}
$errors = false; $errors = false;
@ -153,8 +160,9 @@ function update_structure($verbose, $action, $tables=null, $definition=null) {
// Get the current structure // Get the current structure
$database = array(); $database = array();
if (is_null($tables)) if (is_null($tables)) {
$tables = q("show tables"); $tables = q("SHOW TABLES");
}
foreach ($tables AS $table) { foreach ($tables AS $table) {
$table = current($table); $table = current($table);
@ -164,21 +172,24 @@ function update_structure($verbose, $action, $tables=null, $definition=null) {
} }
// Get the definition // Get the definition
if (is_null($definition)) if (is_null($definition)) {
$definition = db_definition($charset); $definition = db_definition($charset);
}
// Ensure index conversion to unique removes duplicates // Ensure index conversion to unique removes duplicates
$sql_config = "SET session old_alter_table=1;"; $sql_config = "SET session old_alter_table=1;";
if ($verbose) if ($verbose) {
echo $sql_config."\n"; echo $sql_config."\n";
if ($action) }
@$db->q($sql_config); if ($action) {
$db->q($sql_config);
}
// MySQL >= 5.7.4 doesn't support the IGNORE keyword in ALTER TABLE statements // MySQL >= 5.7.4 doesn't support the IGNORE keyword in ALTER TABLE statements
if ((version_compare($db->server_info(), '5.7.4') >= 0) AND if ((version_compare($db->server_info(), '5.7.4') >= 0) AND
!(strpos($db->server_info(), 'MariaDB') !== false)) { !(strpos($db->server_info(), 'MariaDB') !== false)) {
$ignore = ''; $ignore = '';
}else { } else {
$ignore = ' IGNORE'; $ignore = ' IGNORE';
} }
@ -193,10 +204,12 @@ function update_structure($verbose, $action, $tables=null, $definition=null) {
} }
$is_new_table = True; $is_new_table = True;
} else { } else {
// Drop the index if it isn't present in the definition /*
// or the definition differ from current status * Drop the index if it isn't present in the definition
// and index name doesn't start with "local_" * or the definition differ from current status
foreach ($database[$name]["indexes"] AS $indexname => $fieldnames) { * and index name doesn't start with "local_"
*/
foreach ($database[$name]["indexes"] as $indexname => $fieldnames) {
$current_index_definition = implode(",",$fieldnames); $current_index_definition = implode(",",$fieldnames);
if (isset($structure["indexes"][$indexname])) { if (isset($structure["indexes"][$indexname])) {
$new_index_definition = implode(",",$structure["indexes"][$indexname]); $new_index_definition = implode(",",$structure["indexes"][$indexname]);
@ -205,39 +218,44 @@ function update_structure($verbose, $action, $tables=null, $definition=null) {
} }
if ($current_index_definition != $new_index_definition && substr($indexname, 0, 6) != 'local_') { if ($current_index_definition != $new_index_definition && substr($indexname, 0, 6) != 'local_') {
$sql2=db_drop_index($indexname); $sql2=db_drop_index($indexname);
if ($sql3 == "") if ($sql3 == "") {
$sql3 = "ALTER".$ignore." TABLE `".$name."` ".$sql2; $sql3 = "ALTER".$ignore." TABLE `".$name."` ".$sql2;
else } else {
$sql3 .= ", ".$sql2; $sql3 .= ", ".$sql2;
}
} }
} }
// Compare the field structure field by field // Compare the field structure field by field
foreach ($structure["fields"] AS $fieldname => $parameters) { foreach ($structure["fields"] AS $fieldname => $parameters) {
if (!isset($database[$name]["fields"][$fieldname])) { if (!isset($database[$name]["fields"][$fieldname])) {
$sql2=db_add_table_field($fieldname, $parameters); $sql2=db_add_table_field($fieldname, $parameters);
if ($sql3 == "") if ($sql3 == "") {
$sql3 = "ALTER TABLE `".$name."` ".$sql2; $sql3 = "ALTER TABLE `".$name."` ".$sql2;
else } else {
$sql3 .= ", ".$sql2; $sql3 .= ", ".$sql2;
}
} else { } else {
// Compare the field definition // Compare the field definition
$current_field_definition = implode(",",$database[$name]["fields"][$fieldname]); $current_field_definition = implode(",",$database[$name]["fields"][$fieldname]);
$new_field_definition = implode(",",$parameters); $new_field_definition = implode(",",$parameters);
if ($current_field_definition != $new_field_definition) { if ($current_field_definition != $new_field_definition) {
$sql2=db_modify_table_field($fieldname, $parameters); $sql2=db_modify_table_field($fieldname, $parameters);
if ($sql3 == "") if ($sql3 == "") {
$sql3 = "ALTER TABLE `".$name."` ".$sql2; $sql3 = "ALTER TABLE `".$name."` ".$sql2;
else } else {
$sql3 .= ", ".$sql2; $sql3 .= ", ".$sql2;
}
} }
} }
} }
} }
// Create the index if the index don't exists in database /*
// or the definition differ from the current status. * Create the index if the index don't exists in database
// Don't create keys if table is new * or the definition differ from the current status.
* Don't create keys if table is new
*/
if (!$is_new_table) { if (!$is_new_table) {
foreach ($structure["indexes"] AS $indexname => $fieldnames) { foreach ($structure["indexes"] AS $indexname => $fieldnames) {
if (isset($database[$name]["indexes"][$indexname])) { if (isset($database[$name]["indexes"][$indexname])) {
@ -367,10 +385,11 @@ function db_create_index($indexname, $fieldnames, $method="ADD") {
if ($names != "") if ($names != "")
$names .= ","; $names .= ",";
if (preg_match('|(.+)\((\d+)\)|', $fieldname, $matches)) if (preg_match('|(.+)\((\d+)\)|', $fieldname, $matches)) {
$names .= "`".dbesc($matches[1])."`(".intval($matches[2]).")"; $names .= "`".dbesc($matches[1])."`(".intval($matches[2]).")";
else } else {
$names .= "`".dbesc($fieldname)."`"; $names .= "`".dbesc($fieldname)."`";
}
} }
if ($indexname == "PRIMARY") { if ($indexname == "PRIMARY") {
@ -383,8 +402,9 @@ function db_create_index($indexname, $fieldnames, $method="ADD") {
} }
function db_index_suffix($charset, $reduce = 0) { function db_index_suffix($charset, $reduce = 0) {
if ($charset != "utf8mb4") if ($charset != "utf8mb4") {
return ""; return "";
}
// On utf8mb4 indexes can only have a length of 191 // On utf8mb4 indexes can only have a length of 191
$indexlength = 191 - $reduce; $indexlength = 191 - $reduce;
@ -1573,9 +1593,6 @@ function dbstructure_run(&$argv, &$argc) {
echo "dumpsql dump database schema\n"; echo "dumpsql dump database schema\n";
return; return;
} }
if (array_search(__file__,get_included_files())===0){ if (array_search(__file__,get_included_files())===0){