aboutsummaryrefslogtreecommitdiff
path: root/util/tools/r_database.pl
diff options
context:
space:
mode:
authorDarkelarious <darkelarious@333networks.com>2016-11-19 20:56:04 +0100
committerDarkelarious <darkelarious@333networks.com>2016-11-19 20:56:04 +0100
commitc3f8d65a4fb1f5674557ee67cf7f74369df86ad1 (patch)
tree92aab2a394bda28da0ed7c7c75e633fdf386fc71 /util/tools/r_database.pl
parent1de3da4b8027508a91144639455c934fd6ccb9b7 (diff)
downloadMasterServer-Perl-c3f8d65a4fb1f5674557ee67cf7f74369df86ad1.tar.gz
MasterServer-Perl-c3f8d65a4fb1f5674557ee67cf7f74369df86ad1.zip
Massive improvements on efficiency, robustness, security, reliability and more
Diffstat (limited to 'util/tools/r_database.pl')
-rwxr-xr-xutil/tools/r_database.pl40
1 files changed, 40 insertions, 0 deletions
diff --git a/util/tools/r_database.pl b/util/tools/r_database.pl
new file mode 100755
index 0000000..601b1ae
--- /dev/null
+++ b/util/tools/r_database.pl
@@ -0,0 +1,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;