X-Git-Url: http://git.equinoxoli.org/?p=migration-tools.git;a=blobdiff_plain;f=text%2Fcsv2sql;fp=text%2Fcsv2sql;h=39ce2d2298543349c4a253f01fc11d052c0cd8ba;hp=de73881a83ffbba1a965bb9c554500eac905afbb;hb=b0f046246177b7157d1354dc08c1ae89a5f5ec24;hpb=5d03cbc1b03b66811e4c8882b2dd14b6993a76bd diff --git a/text/csv2sql b/text/csv2sql index de73881..39ce2d2 100755 --- a/text/csv2sql +++ b/text/csv2sql @@ -4,6 +4,10 @@ use Text::CSV::Auto; use Data::Dumper; use DBI; use File::Basename; +use Env qw( + HOME PGHOST PGPORT PGUSER PGDATABASE MIGSCHEMA + MIGBASEWORKDIR MIGBASEGITDIR MIGGITDIR MIGWORKDIR +); my $dbh; my $cfg; @@ -12,7 +16,13 @@ my $csv_config; sub init { our %config; do '/openils/conf/offline-config.pl'; - $dbh = DBI->connect( $config{dsn}, $config{usr}, $config{pw} ) or die $DBI::errstr; + if ($PGHOST and $PGPORT and $PGUSER and $PGDATABASE) + { + $db = connect_db($PGDATABASE,$PGUSER,undef,$PGHOST) or die $DBI::errstr; + } else { + $dbh = DBI->connect( $config{dsn}, $config{usr}, $config{pw} ) or die $DBI::errstr; + } + $cfg = { schema => 'm_foo', auto_options => { @@ -49,6 +59,23 @@ sub init { } } +sub connect_db { + my ($db, $dbuser, $dbpw, $dbhost) = @_; + + my $dsn = "dbi:Pg:host=$dbhost;dbname=$db;port=5432"; + + my $attrs = { + ShowErrorStatement => 1, + RaiseError => 1, + PrintError => 1, + pg_enable_utf8 => 1, + }; + my $dbh = DBI->connect($dsn, $dbuser, $dbpw, $attrs); + + return $dbh; +} + + sub write_sql_sample { my $cfg = shift; my $info = shift;