From c393161a55c224a815b2290bef5a0ead231f6565 Mon Sep 17 00:00:00 2001 From: Jose Luis Salas Date: Tue, 19 Dec 2023 12:03:36 +0100 Subject: [PATCH] Refactor and improve performance in make_insert_statement for mysql https://github.com/cantino/my_obfuscate/pull/25/files --- src/triki/mysql.cr | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/src/triki/mysql.cr b/src/triki/mysql.cr index c4c95ea..14965ad 100644 --- a/src/triki/mysql.cr +++ b/src/triki/mysql.cr @@ -20,12 +20,28 @@ class Triki end end - def make_insert_statement(table_name, column_names, values, ignore = nil) - values_strings = values.map do |string_values| - "(" + string_values.join(",") + ")" - end.join(",") + def make_insert_statement(table_name, column_names, rows, ignore = nil) + String.build do |buffer| + buffer << %{INSERT #{ignore ? "IGNORE " : "" }INTO `#{table_name}` (`#{column_names.join("`, `")}`) VALUES } + write_rows(buffer, rows) + buffer << ";" + end + end + + def write_rows(buffer, rows) + rows.each_with_index do |row_values, i| + buffer << "(" + write_row_values(buffer, row_values) + buffer << ")" + buffer << "," if i < rows.size - 1 + end + end - "INSERT #{ignore ? "IGNORE " : ""}INTO `#{table_name}` (`#{column_names.join("`, `")}`) VALUES #{values_strings};" + def write_row_values(buffer, row_values) + row_values.each_with_index do |value, j| + buffer << value + buffer << "," if j < row_values.size - 1 + end end def insert_regex