diff options
author | Eduardo M KALINOWSKI <eduardo@kalinowski.com.br> | 2022-06-04 23:10:29 +0300 |
---|---|---|
committer | Eduardo M KALINOWSKI <eduardo@kalinowski.com.br> | 2022-06-06 14:33:03 +0300 |
commit | e5bef3555741aff3c32271fa1358ea3eb4329bfa (patch) | |
tree | 8104061ac77d11e60e5ecac5f28d41ab06caafb0 | |
parent | ef90e4848dbceb2d3ca0ff67bad5b25fb8b0fc74 (diff) |
Adjustments to make insertion in PgSQL work
Objects with database-specific definitions for each database type were
created to make that possible.
-rw-r--r-- | dbx_Pg.pl | 9 | ||||
-rw-r--r-- | dbx_mysql.pl | 9 | ||||
-rwxr-xr-x | dmarcts-report-parser.pl | 10 |
3 files changed, 26 insertions, 2 deletions
diff --git a/dbx_Pg.pl b/dbx_Pg.pl new file mode 100644 index 0000000..46bb7f0 --- /dev/null +++ b/dbx_Pg.pl @@ -0,0 +1,9 @@ +%dbx = ( + epoch_to_timestamp_fn => 'TO_TIMESTAMP', + to_hex_string => sub { + my ($bin) = @_; + return "'\\x" . unpack("H*", $bin) . "'"; + }, +); + +1; diff --git a/dbx_mysql.pl b/dbx_mysql.pl new file mode 100644 index 0000000..3c03bc6 --- /dev/null +++ b/dbx_mysql.pl @@ -0,0 +1,9 @@ +%dbx = ( + epoch_to_timestamp_fn => 'FROM_UNIXTIME', + to_hex_string => sub { + my ($bin) = @_; + return "X'" . unpack("H*", $bin) . "'"; + }, +); + +1; diff --git a/dmarcts-report-parser.pl b/dmarcts-report-parser.pl index 5b240c4..aba9a85 100755 --- a/dmarcts-report-parser.pl +++ b/dmarcts-report-parser.pl @@ -264,6 +264,12 @@ if (exists $options{delete}) {$delete_reports = 1;} if (exists $options{info}) {$processInfo = 1;} # Setup connection to database server. +our %dbx; +my $dbx_file = File::Basename::dirname($0) . "/dbx_$dbtype.pl"; +my $dbx_return = do $dbx_file; +die "$scriptname: couldn't load DB definition for type $dbtype: $@" if $@; +die "$scriptname: couldn't load DB definition for type $dbtype: $!" unless defined $dbx_return; + my $dbh = DBI->connect("DBI:$dbtype:database=$dbname;host=$dbhost;port=$dbport", $dbuser, $dbpass) or die "$scriptname: Cannot connect to database\n"; @@ -833,7 +839,7 @@ sub storeXMLInDatabase { } my $sql = qq{INSERT INTO report(mindate,maxdate,domain,org,reportid,email,extra_contact_info,policy_adkim, policy_aspf, policy_p, policy_sp, policy_pct, raw_xml) - VALUES(FROM_UNIXTIME(?),FROM_UNIXTIME(?),?,?,?,?,?,?,?,?,?,?,?)}; + VALUES($dbx{epoch_to_timestamp_fn}(?),$dbx{epoch_to_timestamp_fn}(?),?,?,?,?,?,?,?,?,?,?,?)}; my $storexml = $xml->{'raw_xml'}; if ($compress_xml) { my $gzipdata; @@ -985,7 +991,7 @@ sub storeXMLInDatabase { $ipval = unpack "N", $nip; $iptype = "ip"; } elsif($nip = inet_pton(AF_INET6, $ip)) { - $ipval = "X'" . unpack("H*",$nip) . "'"; + $ipval = $dbx{to_hex_string}($nip); $iptype = "ip6"; } else { warn "$scriptname: $org: $id: ??? mystery ip $ip\n"; |