From 6255d5c3bfdc5f3d111d58ec8159aeecef9f2c88 Mon Sep 17 00:00:00 2001 From: Ramon Novoa Date: Tue, 5 May 2015 11:20:41 +0200 Subject: [PATCH] Several fixes to support Oracle. --- pandora_server/lib/PandoraFMS/Core.pm | 4 ++-- pandora_server/lib/PandoraFMS/DB.pm | 32 +++++++++++++++++++++++++-- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/pandora_server/lib/PandoraFMS/Core.pm b/pandora_server/lib/PandoraFMS/Core.pm index 604a4e8686..7e01c78331 100644 --- a/pandora_server/lib/PandoraFMS/Core.pm +++ b/pandora_server/lib/PandoraFMS/Core.pm @@ -4380,9 +4380,9 @@ Set the current master server. sub pandora_set_master ($$) { my ($pa_config, $dbh) = @_; - my $current_master = get_db_value ($dbh, 'SELECT name FROM tserver + my $current_master = get_db_value_limit ($dbh, 'SELECT name FROM tserver WHERE master <> 0 AND status = 1 - ORDER BY master DESC LIMIT 1'); + ORDER BY master DESC', 1); return unless defined($current_master) and ($current_master ne $Master); logger($pa_config, "Server $current_master is the current master.", 1); diff --git a/pandora_server/lib/PandoraFMS/DB.pm b/pandora_server/lib/PandoraFMS/DB.pm index 452935dbf5..57bb081880 100644 --- a/pandora_server/lib/PandoraFMS/DB.pm +++ b/pandora_server/lib/PandoraFMS/DB.pm @@ -59,6 +59,7 @@ our @EXPORT = qw( get_db_rows get_db_single_row get_db_value + get_db_value_limit get_first_server_name get_group_id get_group_name @@ -141,7 +142,7 @@ sub db_connect ($$$$$$) { $RDBMS_QUOTE_STRING = '\''; # Connect to Oracle - my $dbh = DBI->connect("DBI:Oracle:dbname=$db_name;host=$db_host;port=$db_port;sid=pandora", $db_user, $db_pass, { RaiseError => 1, AutoCommit => 1 }); + my $dbh = DBI->connect("DBI:Oracle:dbname=$db_name;host=$db_host;port=$db_port;sid=$db_name", $db_user, $db_pass, { RaiseError => 1, AutoCommit => 1 }); return undef unless defined ($dbh); # Set date format @@ -604,7 +605,6 @@ sub get_group_name ($$) { ######################################################################## sub get_db_value ($$;@) { my ($dbh, $query, @values) = @_; - #my @rows; # Cache statements my $sth = $dbh->prepare_cached($query); @@ -622,6 +622,34 @@ sub get_db_value ($$;@) { return undef; } +######################################################################## +## Get a single column returned by an SQL query with a LIMIT statement +## as a hash reference. +######################################################################## +sub get_db_value_limit ($$$;@) { + my ($dbh, $query, $limit, @values) = @_; + + # Cache statements + my $sth; + if ($RDBMS ne 'oracle') { + $sth = $dbh->prepare_cached($query . ' LIMIT ' . $limit); + } else { + $sth = $dbh->prepare_cached('SELECT * FROM (' . $query . ') WHERE ROWNUM <= ' . $limit); + } + + $sth->execute(@values); + + # Save returned rows + while (my $row = $sth->fetchrow_arrayref()) { + $sth->finish(); + return defined ($row->[0]) ? $row->[0] : undef; + } + + $sth->finish(); + + return undef; +} + ########################################################################## ## Get a single row returned by an SQL query as a hash reference. Returns ## -1 on error.