From e0ada80f8582cf3b28e70b8f18de10aa505159ae Mon Sep 17 00:00:00 2001 From: Darkelarious Date: Tue, 10 Feb 2015 18:22:08 +0100 Subject: Postgresql beacon receiving procedure complete and working --- lib/MasterServer/Database/SQLite/dbCore.pm | 64 ++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100755 lib/MasterServer/Database/SQLite/dbCore.pm (limited to 'lib/MasterServer/Database/SQLite/dbCore.pm') diff --git a/lib/MasterServer/Database/SQLite/dbCore.pm b/lib/MasterServer/Database/SQLite/dbCore.pm new file mode 100755 index 0000000..a75f7a0 --- /dev/null +++ b/lib/MasterServer/Database/SQLite/dbCore.pm @@ -0,0 +1,64 @@ + +package MasterServer::Database::SQLite::dbCore; + +use strict; +use warnings; +use Exporter 'import'; + +our @EXPORT = qw| database_login |; + +################################################################################ +## database_login +## login to the database with credentials provided in the config file. +## returns dbh object +################################################################################ +sub database_login { + my $self = shift; + + # check if database file exists + my $db_file = [split(':', $self->{dblogin}->[0])]->[2]; + $db_file =~ s/dbname=//i; + + unless (-e $db_file) { + # fatal error + $self->log("fatal", "Database file $db_file does not exist!"); + + # end program + $self->halt(); + } + + # create the dbi object + my $dbh = DBI->connect(@{$self->{dblogin}}, {PrintError => 0}); + + # verify that the database connected + if (defined $dbh) { + # log the event + $self->log("database","Connected to the SQLite database."); + + # turn on error printing + $dbh->{printerror} = 1; + + # synchronous read/writing to the sql file OFF. That means: when the script + # shuts down unexpectedly, i.e. because of power failure or a crash, changes + # to the database are NOT SAVED. However, if this setting is not turned OFF, + # it takes too long to write to the database, which means that new beacons, + # requests and servers cannot be processed. You don't have a choice, really.. + $dbh->do("PRAGMA synchronous = OFF"); + + # return the dbi object for further use + return $dbh; + } + else { + # fatal error + $self->log("fatal", "$DBI::errstr!"); + + # end program + $self->halt(); + } + + # unreachable + return undef; + +} + +1; -- cgit v1.2.3