Skip to content

Commit

Permalink
Allow some metaprogramming when including modules and read statements…
Browse files Browse the repository at this point in the history
… instead of lines
  • Loading branch information
josacar committed Dec 28, 2023
1 parent f399af7 commit 76cd2ae
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions src/triki/config_scaffold_generator.cr
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
class Triki
module ConfigScaffoldGenerator
macro included
def table_data(line)
{% if @type.name == "Triki::Postgres" %}
parse_copy_statement(line)
{% else %}
parse_insert_statement(line)
{% end %}
end
end

def generate_config(obfuscator, config, input_io, output_io)
buffer = IO::Memory.new

input_io.each_line(chomp: false) do |line|
if obfuscator.database_type == :postgres
parse_copy_statement = ->(statement_line : String) do
if regex_match = /^\s*COPY (.*?) \((.*?)\) FROM\s*/i.match(statement_line)
{
"table_name" => regex_match[1],
"column_names" => regex_match[2].split(/\s*,\s*/),
}
end
end
table_data = parse_copy_statement.call(line)
else
table_data = parse_insert_statement(line)
end
while statement = input_io.gets(';')
table_data = table_data(statement)
next unless table_data

table_name = table_data["table_name"].as(TableName)
Expand Down

0 comments on commit 76cd2ae

Please sign in to comment.