sample perl script to create the sql inserts to convert odl zip file to db entries user/rogan/lp1365073_postal_codes_in_db_reworked
authorRogan Hamby <rogan.hamby@gmail.com>
Wed, 2 Nov 2016 19:25:08 +0000 (15:25 -0400)
committerRogan Hamby <rogan.hamby@gmail.com>
Wed, 2 Nov 2016 19:25:08 +0000 (15:25 -0400)
Open-ILS/src/support-scripts/zips_file_to_sql.pl [new file with mode: 0644]

diff --git a/Open-ILS/src/support-scripts/zips_file_to_sql.pl b/Open-ILS/src/support-scripts/zips_file_to_sql.pl
new file mode 100644 (file)
index 0000000..1700973
--- /dev/null
@@ -0,0 +1,122 @@
+#!/usr/bin/perl
+###############################################################################
+=pod
+
+command line arguments:
+
+--file - required, the file being read
+--outfile - required, file to output to
+
+recommended use ./ziploader.pl --infile /openils/var/data/zips.txt --outfile zips2load.sql
+
+format 0:[blank],1:text,2:text,3: text 29105,4:integer,5:[blank],6:text,7:[blank],8:alert message
+
+validation rules:
+ 3 can not be empty 
+ 4 is either 0 or 1 and can not be empty
+
+if all the above rules are true and 8 is missing fill it in with a null since it's often empty
+
+=cut
+
+###############################################################################
+
+use strict;
+use warnings;
+
+use Text::CSV;
+use Getopt::Long;
+use Switch;
+use Scalar::Util qw(looks_like_number);
+
+my $lineloop = 0;
+my $linesprocessed = 0;
+my $linesize;
+my $exceptions = 0;
+
+my $file;
+my $outfile;
+my $linefail;
+my $field1;
+my $field2;
+my $field3;
+my $field4;
+my $field6;
+my $field8;
+
+
+my $pretext = "insert into config.postal_codes (state,city,postal_code,enabled,county,alert_message) values (";
+my $posttext = ");";
+
+my $ret = GetOptions (
+       'infile:s'              => \$file,
+       'outfile:s'             => \$outfile,
+);
+
+abort('must specify --infile') unless defined $file;
+abort('must specify --outfile') unless defined $outfile;
+
+
+my $csv = Text::CSV->new ({
+  binary    => 1,
+  auto_diag => 1,
+  sep_char  => '|'
+}); 
+
+open(my $of, '>', $outfile);
+
+open(my $data, '<:encoding(utf8)', $file) or die "Could not open '$file' $!\n";
+while (my $fields = $csv->getline( $data )) {
+  $lineloop = 0;
+  $linefail = 0;
+  $linesize = @{ $fields };
+  $field1 = '';
+  $field2 = '';
+  $field3 = '';
+  $field4 = '2' ;
+  $field6 = '';
+  $field8 = '';
+  if ($linesize != 8 and $linesize != 9) { $linefail++ };
+  while ($lineloop <= $linesize - 1) {    
+    $fields->[$lineloop] =~ s/\'/\'\'/g;  ##escape single quotes
+    $fields->[$lineloop] =~ s/^\s+|\s+$//g; ##trim spaces
+    switch ($lineloop) {
+               case 1          { $field1 = $fields->[$lineloop] }
+               case 2          { $field2 = $fields->[$lineloop] }
+               case 3          { if (($fields->[$lineloop] ne '')) {$field3 = $fields->[$lineloop]} 
+                                  else {$linefail++ } }
+               case 4          { if (($fields->[$lineloop] == 0) or ($fields->[$lineloop] == 1)) 
+                                  { $field4 = $fields->[$lineloop] } else { $field4 = $fields->[$lineloop]; $linefail++ } } 
+               case 6          { $field6 = $fields->[$lineloop] }
+               case 8          { $field8 = $fields->[$lineloop] }
+    }
+  ## yes, this part is bodged
+  if ($field4 eq '0') {$field4 = 'FALSE'};
+  if ($field4 eq '1') {$field4 = 'TRUE'};
+  $lineloop++;
+  }
+  if ($linefail != 0) { print $of '--'; $exceptions++ };
+  print $of "$pretext'$field1','$field2','$field3',$field4,'$field6','$field8'$posttext\n";
+  $linesprocessed++;
+}
+
+if (not $csv->eof) {
+  $csv->error_diag();
+}
+
+sub abort {
+  my $msg = shift;
+  print STDERR "$0: $msg", "\n";
+  exit 1;
+}
+
+close $data;
+close $of;
+
+my $recno;
+
+$recno = $csv->record_number ();
+
+print "$linesprocessed lines printed to SQL file\n";
+print "$exceptions lines failed validation and were written as comments\n";
+