[Repositories] Add more precise types to repository generator (#2391)

* Make utils/scripts/generators/repository-generator.pl aware of more
datatypes

This adds support for unsigned and more integer types. It also avoids
using parsing functions that require casting (still needed in some
cases)

Having the data types in the Repository structs better map to the types
in the database will allow us to avoid casting when we pull data out of
them. And as a benefit, assume something is wrong if we do :)

Hopefully clean up some warnings due to casting too.

Co-authored-by: Akkadius <akkadius1@gmail.com>
This commit is contained in:
Michael Cook (mackal)
2022-08-31 01:04:27 -04:00
committed by GitHub
parent fcf01f6d87
commit 6f7fa98996
183 changed files with 7849 additions and 7818 deletions
@@ -188,12 +188,13 @@ foreach my $table_to_generate (@tables) {
my $column_name = $row[0];
my $column_name_formatted = format_column_name_for_cpp_var($column_name);
my $data_type = $row[2];
my $column_type = $row[3];
if ($longest_column_length < length($column_name_formatted)) {
$longest_column_length = length($column_name_formatted);
}
my $struct_data_type = translate_mysql_data_type_to_c($data_type);
my $struct_data_type = translate_mysql_data_type_to_c($data_type, $column_type);
if ($longest_data_type_length < length($struct_data_type)) {
$longest_data_type_length = length($struct_data_type);
@@ -252,7 +253,7 @@ foreach my $table_to_generate (@tables) {
$default_value = 0;
}
my $struct_data_type = translate_mysql_data_type_to_c($data_type);
my $struct_data_type = translate_mysql_data_type_to_c($data_type, $column_type);
# struct
$table_struct_columns .= sprintf("\t\t\%-${longest_data_type_length}s %s;\n", $struct_data_type, $column_name_formatted);
@@ -299,7 +300,18 @@ foreach my $table_to_generate (@tables) {
$insert_many_entries .= sprintf("\t\t\tv.push_back(%s);\n", $value);
# find one / all (select)
if ($data_type =~ /bigint/) {
if ($column_type =~ /unsigned/) {
if ($data_type =~ /bigint/) {
$all_entries .= sprintf("\t\t\te.%-${longest_column_length}s = strtoull(row[%s], nullptr, 10);\n", $column_name_formatted, $index);
$find_one_entries .= sprintf("\t\t\te.%-${longest_column_length}s = strtoull(row[%s], nullptr, 10);\n", $column_name_formatted, $index);
}
elsif ($data_type =~ /int/) {
$all_entries .= sprintf("\t\t\te.%-${longest_column_length}s = static_cast<%s>(strtoul(row[%s], nullptr, 10));\n", $column_name_formatted, $struct_data_type, $index);
$find_one_entries .= sprintf("\t\t\te.%-${longest_column_length}s = static_cast<%s>(strtoul(row[%s], nullptr, 10));\n", $column_name_formatted, $struct_data_type, $index);
}
}
elsif ($data_type =~ /bigint/) {
$all_entries .= sprintf("\t\t\te.%-${longest_column_length}s = strtoll(row[%s], nullptr, 10);\n", $column_name_formatted, $index);
$find_one_entries .= sprintf("\t\t\te.%-${longest_column_length}s = strtoll(row[%s], nullptr, 10);\n", $column_name_formatted, $index);
}
@@ -308,12 +320,16 @@ foreach my $table_to_generate (@tables) {
$find_one_entries .= sprintf("\t\t\te.%-${longest_column_length}s = strtoll(row[%s] ? row[%s] : \"-1\", nullptr, 10);\n", $column_name_formatted, $index, $index);
}
elsif ($data_type =~ /int/) {
$all_entries .= sprintf("\t\t\te.%-${longest_column_length}s = atoi(row[%s]);\n", $column_name_formatted, $index);
$find_one_entries .= sprintf("\t\t\te.%-${longest_column_length}s = atoi(row[%s]);\n", $column_name_formatted, $index);
$all_entries .= sprintf("\t\t\te.%-${longest_column_length}s = static_cast<%s>(atoi(row[%s]));\n", $column_name_formatted, $struct_data_type, $index);
$find_one_entries .= sprintf("\t\t\te.%-${longest_column_length}s = static_cast<%s>(atoi(row[%s]));\n", $column_name_formatted, $struct_data_type, $index);
}
elsif ($data_type =~ /float|double|decimal/) {
$all_entries .= sprintf("\t\t\te.%-${longest_column_length}s = static_cast<float>(atof(row[%s]));\n", $column_name_formatted, $index);
$find_one_entries .= sprintf("\t\t\te.%-${longest_column_length}s = static_cast<float>(atof(row[%s]));\n", $column_name_formatted, $index);
elsif ($data_type =~ /float|decimal/) {
$all_entries .= sprintf("\t\t\te.%-${longest_column_length}s = strtof(row[%s], nullptr);\n", $column_name_formatted, $index);
$find_one_entries .= sprintf("\t\t\te.%-${longest_column_length}s = strtof(row[%s], nullptr);\n", $column_name_formatted, $index);
}
elsif ($data_type =~ /double/) {
$all_entries .= sprintf("\t\t\te.%-${longest_column_length}s = strtod(row[%s], nullptr);\n", $column_name_formatted, $index);
$find_one_entries .= sprintf("\t\t\te.%-${longest_column_length}s = strtod(row[%s], nullptr);\n", $column_name_formatted, $index);
}
else {
$all_entries .= sprintf("\t\t\te.%-${longest_column_length}s = row[%s] ? row[%s] : \"\";\n", $column_name_formatted, $index, $index);
@@ -491,23 +507,41 @@ sub trim {
sub translate_mysql_data_type_to_c {
my $mysql_data_type = $_[0];
my $mysql_column_type = $_[1];
my $struct_data_type = "std::string";
if ($mysql_data_type =~ /tinyint/) {
$struct_data_type = 'int';
}
elsif ($mysql_data_type =~ /smallint/) {
$struct_data_type = 'int';
if ($mysql_column_type =~ /unsigned/) {
if ($mysql_data_type =~ /bigint/) {
$struct_data_type = 'uint64_t';
}
elsif ($mysql_data_type =~ /tinyint/) {
$struct_data_type = 'uint8_t';
}
elsif ($mysql_data_type =~ /smallint/) {
$struct_data_type = 'uint16_t';
}
elsif ($mysql_data_type =~ /int/) {
$struct_data_type = 'uint32_t';
}
}
elsif ($mysql_data_type =~ /bigint/) {
$struct_data_type = 'int64';
$struct_data_type = 'int64_t';
}
elsif ($mysql_data_type =~ /tinyint/) {
$struct_data_type = 'int8_t';
}
elsif ($mysql_data_type =~ /smallint/) {
$struct_data_type = 'int16_t';
}
elsif ($mysql_data_type =~ /int/) {
$struct_data_type = 'int';
$struct_data_type = 'int32_t';
}
elsif ($mysql_data_type =~ /float|double|decimal/) {
elsif ($mysql_data_type =~ /float|decimal/) {
$struct_data_type = 'float';
}
elsif ($mysql_data_type =~ /double/) {
$struct_data_type = 'double';
}
elsif ($mysql_data_type =~ /datetime/) {
$struct_data_type = 'time_t';
}
+1
View File
@@ -455,6 +455,7 @@
9199|2022_08_08_task_req_activity_id.sql|SHOW COLUMNS FROM `task_activities` LIKE 'req_activity_id'|empty|
9200|2022_08_19_zone_expansion_consistency.sql|SELECT * FROM db_version WHERE version >= 9200|empty|
9201|2022_08_22_npc_types_heroic_strikethrough.sql|SHOW COLUMNS FROM `npc_types` LIKE 'heroic_strikethrough'|empty|
9202|2022_08_24_task_activities_step.sql|SHOW COLUMNS FROM `task_activities` LIKE 'step'|contains|unsigned
# Upgrade conditions:
# This won't be needed after this system is implemented, but it is used database that are not
@@ -0,0 +1 @@
ALTER TABLE `task_activities` MODIFY `step` INT(11) NOT NULL DEFAULT '0';