[Repositories] Add datetime support to repositories (#1503)

This commit is contained in:
Chris Miles 2021-08-31 00:34:10 -05:00 committed by GitHub
parent 228e0007ca
commit 06890f695a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 18 deletions

View File

@ -14,6 +14,7 @@
#include "../../database.h" #include "../../database.h"
#include "../../string_util.h" #include "../../string_util.h"
#include <ctime>
class Base{{TABLE_NAME_CLASS}}Repository { class Base{{TABLE_NAME_CLASS}}Repository {
public: public:
@ -33,11 +34,23 @@ public:
}; };
} }
static std::vector<std::string> SelectColumns()
{
return {
{{SELECT_COLUMNS_LIST_QUOTED}}
};
}
static std::string ColumnsRaw() static std::string ColumnsRaw()
{ {
return std::string(implode(", ", Columns())); return std::string(implode(", ", Columns()));
} }
static std::string SelectColumnsRaw()
{
return std::string(implode(", ", SelectColumns()));
}
static std::string TableName() static std::string TableName()
{ {
return std::string("{{TABLE_NAME_VAR}}"); return std::string("{{TABLE_NAME_VAR}}");
@ -47,7 +60,7 @@ public:
{ {
return fmt::format( return fmt::format(
"SELECT {} FROM {}", "SELECT {} FROM {}",
ColumnsRaw(), SelectColumnsRaw(),
TableName() TableName()
); );
} }

View File

@ -208,6 +208,7 @@ foreach my $table_to_generate (@tables) {
my $insert_many_entries = ""; my $insert_many_entries = "";
my $find_one_entries = ""; my $find_one_entries = "";
my $column_names_quoted = ""; my $column_names_quoted = "";
my $select_column_names_quoted = "";
my $table_struct_columns = ""; my $table_struct_columns = "";
my $update_one_entries = ""; my $update_one_entries = "";
my $all_entries = ""; my $all_entries = "";
@ -233,11 +234,9 @@ foreach my $table_to_generate (@tables) {
} }
} }
print $column_default . "\n";
my $default_value = 0; my $default_value = 0;
if ($column_default eq "current_timestamp()") { if ($column_default eq "current_timestamp()") {
$default_value = '""'; $default_value = "std::time(nullptr)"
} }
elsif ($column_default ne "NULL" && $column_default ne "") { elsif ($column_default ne "NULL" && $column_default ne "") {
$column_default =~ s/'/"/g; $column_default =~ s/'/"/g;
@ -246,7 +245,7 @@ foreach my $table_to_generate (@tables) {
elsif ($column_default eq "''") { elsif ($column_default eq "''") {
$default_value = '""'; $default_value = '""';
} }
elsif ((trim($column_default) eq "" || $column_default eq "NULL") && $column_type =~ /text|varchar|datetime/i) { elsif ((trim($column_default) eq "" || $column_default eq "NULL") && $column_type =~ /text|varchar/i) {
$default_value = '""'; $default_value = '""';
} }
@ -260,6 +259,12 @@ foreach my $table_to_generate (@tables) {
# column names (string) # column names (string)
$column_names_quoted .= sprintf("\t\t\t\"%s\",\n", format_column_name_for_mysql($column_name)); $column_names_quoted .= sprintf("\t\t\t\"%s\",\n", format_column_name_for_mysql($column_name));
if ($data_type =~ /datetime/) {
$select_column_names_quoted .= sprintf("\t\t\t\"UNIX_TIMESTAMP(%s)\",\n", format_column_name_for_mysql($column_name));
}
else {
$select_column_names_quoted .= sprintf("\t\t\t\"%s\",\n", format_column_name_for_mysql($column_name));
}
# update one # update one
if ($extra ne "auto_increment") { if ($extra ne "auto_increment") {
@ -267,6 +272,9 @@ foreach my $table_to_generate (@tables) {
if ($data_type =~ /int|float|double|decimal/) { if ($data_type =~ /int|float|double|decimal/) {
$query_value = sprintf('" + std::to_string(%s_entry.%s));', $table_name, $column_name_formatted); $query_value = sprintf('" + std::to_string(%s_entry.%s));', $table_name, $column_name_formatted);
} }
elsif ($data_type =~ /datetime/) {
$query_value = sprintf('FROM_UNIXTIME(" + (%s_entry.%s > 0 ? std::to_string(%s_entry.%s) : "null") + ")");', $table_name, $column_name_formatted, $table_name, $column_name_formatted);
}
$update_one_entries .= sprintf( $update_one_entries .= sprintf(
"\t\t" . 'update_values.push_back(columns[%s] + " = %s' . "\n", "\t\t" . 'update_values.push_back(columns[%s] + " = %s' . "\n",
@ -280,14 +288,21 @@ foreach my $table_to_generate (@tables) {
if ($data_type =~ /int|float|double|decimal/) { if ($data_type =~ /int|float|double|decimal/) {
$value = sprintf('std::to_string(%s_entry.%s)', $table_name, $column_name_formatted); $value = sprintf('std::to_string(%s_entry.%s)', $table_name, $column_name_formatted);
} }
elsif ($data_type =~ /datetime/) {
$value = sprintf('"FROM_UNIXTIME(" + (%s_entry.%s > 0 ? std::to_string(%s_entry.%s) : "null") + ")"', $table_name, $column_name_formatted, $table_name, $column_name_formatted);
}
$insert_one_entries .= sprintf("\t\tinsert_values.push_back(%s);\n", $value); $insert_one_entries .= sprintf("\t\tinsert_values.push_back(%s);\n", $value);
$insert_many_entries .= sprintf("\t\t\tinsert_values.push_back(%s);\n", $value); $insert_many_entries .= sprintf("\t\t\tinsert_values.push_back(%s);\n", $value);
# find one / all (select) # find one / all (select)
if ($data_type =~ /bigint/) { if ($data_type =~ /bigint/) {
$all_entries .= sprintf("\t\t\tentry.%-${longest_column_length}s = strtoll(row[%s], NULL, 10);\n", $column_name_formatted, $index); $all_entries .= sprintf("\t\t\tentry.%-${longest_column_length}s = strtoll(row[%s], nullptr, 10);\n", $column_name_formatted, $index);
$find_one_entries .= sprintf("\t\t\tentry.%-${longest_column_length}s = strtoll(row[%s], NULL, 10);\n", $column_name_formatted, $index); $find_one_entries .= sprintf("\t\t\tentry.%-${longest_column_length}s = strtoll(row[%s], nullptr, 10);\n", $column_name_formatted, $index);
}
elsif ($data_type =~ /datetime/) {
$all_entries .= sprintf("\t\t\tentry.%-${longest_column_length}s = strtoll(row[%s] ? row[%s] : \"-1\", nullptr, 10);\n", $column_name_formatted, $index, $index);
$find_one_entries .= sprintf("\t\t\tentry.%-${longest_column_length}s = strtoll(row[%s] ? row[%s] : \"-1\", nullptr, 10);\n", $column_name_formatted, $index, $index);
} }
elsif ($data_type =~ /int/) { elsif ($data_type =~ /int/) {
$all_entries .= sprintf("\t\t\tentry.%-${longest_column_length}s = atoi(row[%s]);\n", $column_name_formatted, $index); $all_entries .= sprintf("\t\t\tentry.%-${longest_column_length}s = atoi(row[%s]);\n", $column_name_formatted, $index);
@ -366,6 +381,7 @@ foreach my $table_to_generate (@tables) {
} }
chomp($column_names_quoted); chomp($column_names_quoted);
chomp($select_column_names_quoted);
chomp($table_struct_columns); chomp($table_struct_columns);
chomp($default_entries); chomp($default_entries);
chomp($update_one_entries); chomp($update_one_entries);
@ -391,6 +407,7 @@ foreach my $table_to_generate (@tables) {
$new_base_repository =~ s/\{\{DATABASE_CONNECTION}}/$database_connection/g; $new_base_repository =~ s/\{\{DATABASE_CONNECTION}}/$database_connection/g;
$new_base_repository =~ s/\{\{DEFAULT_ENTRIES}}/$default_entries/g; $new_base_repository =~ s/\{\{DEFAULT_ENTRIES}}/$default_entries/g;
$new_base_repository =~ s/\{\{COLUMNS_LIST_QUOTED}}/$column_names_quoted/g; $new_base_repository =~ s/\{\{COLUMNS_LIST_QUOTED}}/$column_names_quoted/g;
$new_base_repository =~ s/\{\{SELECT_COLUMNS_LIST_QUOTED}}/$select_column_names_quoted/g;
$new_base_repository =~ s/\{\{TABLE_STRUCT_COLUMNS}}/$table_struct_columns/g; $new_base_repository =~ s/\{\{TABLE_STRUCT_COLUMNS}}/$table_struct_columns/g;
$new_base_repository =~ s/\{\{FIND_ONE_ENTRIES}}/$find_one_entries/g; $new_base_repository =~ s/\{\{FIND_ONE_ENTRIES}}/$find_one_entries/g;
$new_base_repository =~ s/\{\{UPDATE_ONE_ENTRIES}}/$update_one_entries/g; $new_base_repository =~ s/\{\{UPDATE_ONE_ENTRIES}}/$update_one_entries/g;
@ -409,6 +426,7 @@ foreach my $table_to_generate (@tables) {
$new_repository =~ s/\{\{DATABASE_CONNECTION}}/$database_connection/g; $new_repository =~ s/\{\{DATABASE_CONNECTION}}/$database_connection/g;
$new_repository =~ s/\{\{DEFAULT_ENTRIES}}/$default_entries/g; $new_repository =~ s/\{\{DEFAULT_ENTRIES}}/$default_entries/g;
$new_repository =~ s/\{\{COLUMNS_LIST_QUOTED}}/$column_names_quoted/g; $new_repository =~ s/\{\{COLUMNS_LIST_QUOTED}}/$column_names_quoted/g;
$new_repository =~ s/\{\{SELECT_COLUMNS_LIST_QUOTED}}/$select_column_names_quoted/g;
$new_repository =~ s/\{\{TABLE_STRUCT_COLUMNS}}/$table_struct_columns/g; $new_repository =~ s/\{\{TABLE_STRUCT_COLUMNS}}/$table_struct_columns/g;
$new_repository =~ s/\{\{FIND_ONE_ENTRIES}}/$find_one_entries/g; $new_repository =~ s/\{\{FIND_ONE_ENTRIES}}/$find_one_entries/g;
$new_repository =~ s/\{\{UPDATE_ONE_ENTRIES}}/$update_one_entries/g; $new_repository =~ s/\{\{UPDATE_ONE_ENTRIES}}/$update_one_entries/g;
@ -487,6 +505,9 @@ sub translate_mysql_data_type_to_c {
elsif ($mysql_data_type =~ /float|double|decimal/) { elsif ($mysql_data_type =~ /float|double|decimal/) {
$struct_data_type = 'float'; $struct_data_type = 'float';
} }
elsif ($mysql_data_type =~ /datetime/) {
$struct_data_type = 'time_t';
}
return $struct_data_type; return $struct_data_type;
} }