aboutsummaryrefslogtreecommitdiff
path: root/util/tools/r_database.pl
blob: 601b1ae5a575f982069d30e5f0c49bf2c7168be6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/perl
use strict;
use warnings;
use DBI;

our $dbh = open_database();

sub open_database {
  my $dbh = DBI->connect('dbi:Pg:dbname=masterserver', 'user', 'password')
    or die "Cannot connect: $DBI::errstr\n";
  
  # don't forget at end!
  #$dbh->disconnect;
}

################################################################################
## Subroutine dbAddServer
## Add a server to the database (address + port only)
## If the address already exists in the database, it will be ignored.
################################################################################
sub db_add_server {
  my %o = (@_);

  # Try to get the address out of the database.
  my $exists = $dbh->selectall_arrayref(
    "SELECT * FROM serverlist WHERE ip = ? AND port = ?", 
    undef, $o{ip}, $o{port});
  return if (defined $exists->[0]);
  
  $exists = $dbh->selectall_arrayref(
    "SELECT * FROM pending WHERE ip = ? AND heartbeat = ?", 
    undef, $o{ip}, $o{port});
  return if (defined $exists->[0]);
  
  #print "Add $o{ip}\t $o{port}\n";
  $exists = $dbh->do("INSERT INTO pending (ip, heartbeat) VALUES(?, ?)",
     undef, $o{ip}, $o{port});
}

1;