| 1 | #!/usr/bin/env perl | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use Getopt::Long; | ||
| 5 | use Path::Tiny; | ||
| 6 | use Text::CSV_XS; | ||
| 7 | |||
| 8 | # Define variables for command-line options, default separator to tab | ||
| 9 | my ( $ddl_file, $input_csv, $output_csv, $ddl_type, $help, $table_name ); | ||
| 10 | my $sep_char = "\t"; | ||
| 11 | my $NULL = '\\N'; | ||
| 12 | |||
| 13 | # Parse options using Getopt::Long | ||
| 14 | GetOptions( | ||
| 15 | "ddl=s" => \$ddl_file, | ||
| 16 | "input=s" => \$input_csv, | ||
| 17 | "output=s" => \$output_csv, | ||
| 18 | "ddl-type=s" => \$ddl_type, | ||
| 19 | "table|t=s" => \$table_name, # <-- New optional parameter | ||
| 20 | "sep=s" => \$sep_char, | ||
| 21 | "help" => \$help, | ||
| 22 | "version|V" => sub { | ||
| 23 | my $version = '0.02'; # Adjust as needed | ||
| 24 | print "$0 Version $version\n"; | ||
| 25 | exit; | ||
| 26 | }, | ||
| 27 | ) or die "Error in command line arguments\n"; | ||
| 28 | |||
| 29 | # Show usage if help requested or required arguments are missing | ||
| 30 | if ( $help || !( $ddl_file && $input_csv && $output_csv && $ddl_type ) ) { | ||
| 31 | print <<"USAGE"; | ||
| 32 | Usage: | ||
| 33 | $0 --ddl DDL_FILE --ddl-type TYPE --input INPUT_CSV --output OUTPUT_CSV | ||
| 34 | [--table TABLE_NAME] [--sep SEPARATOR] [--help] [--version] | ||
| 35 | |||
| 36 | Options: | ||
| 37 | --ddl (required) File containing the DDL (CREATE TABLE or index definitions) | ||
| 38 | --ddl-type (required) Type of the DDL (e.g., sqlite, postgresql) | ||
| 39 | --input (required) Input CSV file (with header row) | ||
| 40 | --output (required) Output CSV file with columns reordered to match the DDL | ||
| 41 | --table, -t (optional) Override the table name to look up in the DDL. | ||
| 42 | If not provided, the script derives it from the CSV filename. | ||
| 43 | --sep Field separator character (default: tab) | ||
| 44 | --help Show this help message | ||
| 45 | --version, -V Show the script's version | ||
| 46 | |||
| 47 | Example: | ||
| 48 | $0 --ddl schema.sql --ddl-type postgresql --input PERSON.csv --output output.csv --sep ";" | ||
| 49 | $0 --ddl schema.sql --ddl-type postgresql --table person --input ANY_CSV.csv --output output.csv | ||
| 50 | |||
| 51 | USAGE | ||
| 52 | exit; | ||
| 53 | } | ||
| 54 | |||
| 55 | # Determine the table name. If --table was provided, use it; otherwise derive from CSV filename. | ||
| 56 | my $table; | ||
| 57 | if ($table_name) { | ||
| 58 | $table = lc $table_name; | ||
| 59 | } | ||
| 60 | else { | ||
| 61 | ( $table = lc $input_csv ) =~ s{^.*/}{}; # remove any path and lowercase | ||
| 62 | $table =~ s/\.csv$//i; # remove the .csv extension | ||
| 63 | } | ||
| 64 | |||
| 65 | # Read the DDL file into a single string | ||
| 66 | my $ddl_text = path($ddl_file)->slurp_utf8; | ||
| 67 | |||
| 68 | # Remove comments (/* ... */ and -- ...) | ||
| 69 | $ddl_text =~ s|/\*.*?\*/||gs; | ||
| 70 | $ddl_text =~ s/--.*\n/\n/g; | ||
| 71 | |||
| 72 | my @desired_order; | ||
| 73 | |||
| 74 | # Extract columns for the specified table based on DDL type | ||
| 75 | if ( $ddl_type eq 'sqlite' ) { | ||
| 76 | if ( $ddl_text =~ /CREATE\s+TABLE\s+$table\s*\((.*?)\);/is ) { | ||
| 77 | my $columns_text = $1; | ||
| 78 | my @lines = split /,/, $columns_text; | ||
| 79 | for my $line (@lines) { | ||
| 80 | $line =~ s/^\s+|\s+$//g; | ||
| 81 | next unless $line; | ||
| 82 | if ( $line =~ /^\"?(\w+)\"?\s+/ ) { | ||
| 83 | push @desired_order, $1; | ||
| 84 | } | ||
| 85 | } | ||
| 86 | } | ||
| 87 | } | ||
| 88 | elsif ( $ddl_type eq 'postgresql' ) { | ||
| 89 | my $table_re = qr/CREATE\s+TABLE\s+[^.]*\.?$table\s*\((.*?)\);/is; | ||
| 90 | if ( $ddl_text =~ /$table_re/ ) { | ||
| 91 | my $columns_text = $1; | ||
| 92 | my @lines = split /,\n/, $columns_text; | ||
| 93 | for my $line (@lines) { | ||
| 94 | $line =~ s/^\s+|\s+$//g; | ||
| 95 | next unless $line; | ||
| 96 | if ( $line =~ /^\"?(\w+)\"?\s+/ ) { | ||
| 97 | push @desired_order, $1; | ||
| 98 | } | ||
| 99 | } | ||
| 100 | } | ||
| 101 | } | ||
| 102 | |||
| 103 | # Die if no columns were found | ||
| 104 | unless (@desired_order) { | ||
| 105 | die "Could not find columns for table '$table' in the DDL file (type: $ddl_type)\n"; | ||
| 106 | } | ||
| 107 | |||
| 108 | # Set up CSV reading | ||
| 109 | my $csv_in = Text::CSV_XS->new( { binary => 1, sep_char => $sep_char } ) | ||
| 110 | or die "Cannot use CSV: " . Text::CSV_XS->error_diag(); | ||
| 111 | open my $fh_in, "<:encoding(utf8)", $input_csv | ||
| 112 | or die "Cannot open '$input_csv': $!"; | ||
| 113 | |||
| 114 | my $header = $csv_in->getline($fh_in); | ||
| 115 | die "Input CSV has no header row\n" unless $header; | ||
| 116 | |||
| 117 | my %header_idx = map { $header->[$_] => $_ } 0 .. $#$header; | ||
| 118 | |||
| 119 | # Set up output CSV writer | ||
| 120 | my $csv_out = Text::CSV_XS->new( { binary => 1, sep_char => $sep_char } ) | ||
| 121 | or die "Cannot use CSV: " . Text::CSV_XS->error_diag(); | ||
| 122 | open my $fh_out, ">:encoding(utf8)", $output_csv | ||
| 123 | or die "Cannot open '$output_csv': $!"; | ||
| 124 | |||
| 125 | # Write the output header | ||
| 126 | $csv_out->print( $fh_out, \@desired_order ); | ||
| 127 | print $fh_out "\n"; | ||
| 128 | |||
| 129 | # For each row in the input, reorder columns and print | ||
| 130 | while ( my $row = $csv_in->getline($fh_in) ) { | ||
| 131 | my @new_row = | ||
| 132 | map { exists $header_idx{$_} ? $row->[ $header_idx{$_} ] : $NULL } | ||
| 133 | @desired_order; | ||
| 134 | $csv_out->print( $fh_out, \@new_row ); | ||
| 135 | print $fh_out "\n"; | ||
| 136 | } | ||
| 137 | |||
| 138 | close $fh_in; | ||
| 139 | close $fh_out; | ||
| 140 | |||
| 141 | print "Reordered CSV written to '$output_csv'.\n"; |