Skip to content

Commit

Permalink
Refactor and improve performance in make_insert_statement for mysql
Browse files Browse the repository at this point in the history
  • Loading branch information
josacar committed Dec 19, 2023
1 parent 4f76fe0 commit c393161
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions src/triki/mysql.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c393161

Please sign in to comment.