mustal improvement on collation handling

This commit is contained in:
OpenXE 2023-03-30 11:19:46 +02:00
parent b7153c423c
commit 5bc7a64c8c

View File

@ -79,20 +79,22 @@ function mustal_load_tables_from_db(string $host, string $schema, string $user,
} }
// Get db_def and views // Get db_def and views
$sql = "SHOW FULL tables WHERE Table_type = 'BASE TABLE'"; $sql = "SHOW TABLE STATUS";
$query_result = mysqli_query($mysqli, $sql); $query_result = mysqli_query($mysqli, $sql);
if (!$query_result) { if (!$query_result) {
return(array()); return(array());
} }
while ($row = mysqli_fetch_assoc($query_result)) { while ($row = mysqli_fetch_assoc($query_result)) {
$table = array(); $table = array();
$table['name'] = $row['Tables_in_'.$schema]; $table['name'] = $row['Name'];
$table['type'] = $row['Table_type']; $table['collation'] = $row['Collation'];
$table['type'] = 'BASE TABLE';
$tables[] = $table; // Add table to list of tables $tables[] = $table; // Add table to list of tables
} }
// Get and add columns of the table // Get and add columns of the table
foreach ($tables as &$table) { foreach ($tables as &$table) {
$sql = "SHOW FULL COLUMNS FROM ".$table['name']; $sql = "SHOW FULL COLUMNS FROM ".$table['name'];
$query_result = mysqli_query($mysqli, $sql); $query_result = mysqli_query($mysqli, $sql);
@ -109,6 +111,10 @@ function mustal_load_tables_from_db(string $host, string $schema, string $user,
$column['Default'] = mustal_mysql_put_text_type_in_quotes($column['Type'],$column['Default']); $column['Default'] = mustal_mysql_put_text_type_in_quotes($column['Type'],$column['Default']);
} }
if (empty($column['Collation']) && mustal_is_string_type($column['Type'])) {
$column['Collation'] = $table['collation']."TABLE";
}
$columns[] = $column; // Add column to list of columns $columns[] = $column; // Add column to list of columns
} }
$table['columns'] = $columns; $table['columns'] = $columns;
@ -402,9 +408,11 @@ function mustal_compare_table_array(array $nominal, string $nominal_name, array
// Generate SQL to create or modify column // Generate SQL to create or modify column
function mustal_column_sql_definition(string $table_name, array $column, array $reserved_words_without_quote) : string { function mustal_column_sql_definition(string $table_name, array $column, array $reserved_words_without_quote) : string {
$column_is_string_type = mustal_is_string_type($column['Type']);
foreach($column as $key => &$value) { foreach($column as $key => &$value) {
$value = (string) $value; $value = (string) $value;
$value = mustal_column_sql_create_property_definition($key,$value,$reserved_words_without_quote); $value = mustal_column_sql_create_property_definition($key,$value,$reserved_words_without_quote,$column_is_string_type);
} }
// Default handling here // Default handling here
@ -423,7 +431,7 @@ function mustal_column_sql_definition(string $table_name, array $column, array $
} }
// Generate SQL to modify a single column property // Generate SQL to modify a single column property
function mustal_column_sql_create_property_definition(string $property, string $property_value, array $reserved_words_without_quote) : string { function mustal_column_sql_create_property_definition(string $property, string $property_value, array $reserved_words_without_quote, $column_is_string_type) : string {
switch ($property) { switch ($property) {
case 'Type': case 'Type':
@ -454,8 +462,10 @@ function mustal_column_sql_create_property_definition(string $property, string $
} }
break; break;
case 'Collation': case 'Collation':
if ($property_value != '') { if ($property_value != '' && $column_is_string_type) {
$property_value = " COLLATE ".$property_value; $property_value = " COLLATE ".$property_value;
} else {
$property_value = "";
} }
break; break;
default: default:
@ -744,6 +754,17 @@ function mustal_calculate_db_upgrade(array $compare_def, array $db_def, array &$
array_unshift($upgrade_sql,"SET SQL_MODE='ALLOW_INVALID_DATES';","SET SESSION innodb_strict_mode=OFF;"); array_unshift($upgrade_sql,"SET SQL_MODE='ALLOW_INVALID_DATES';","SET SESSION innodb_strict_mode=OFF;");
} }
return($result); return($result);
} }
// Check if given type is a string, relevant for collation
function mustal_is_string_type(string $type) {
$mustal_string_types = array('varchar','char','text','tinytext','mediumtext','longtext');
foreach($mustal_string_types as $string_type) {
if (stripos($string_type,$type) === 0) {
return(true);
}
}
return(false);
}