Some more work on the database (and related methods). Still a long way from working.

Signed-off-by: Digimer <digimer@alteeve.ca>
main
Digimer 8 years ago
parent 3ea562a79c
commit 13ae1f1701
  1. 99
      AN/Tools/Database.pm
  2. 2
      AN/an-tools.xml

@ -330,51 +330,38 @@ sub connect
}); });
} }
# Get a time stamp for this run, if not yet gotten.
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
"cache::db_fh::$id" => $an->data->{cache}{db_fh}{$id},
"sys::db_timestamp" => $an->data->{sys}{db_timestamp}
});
### NOTE: Left off here.
# Get a time stamp for this run, if not yet gotten. ### NOTE: Left off here.
$an->Log->entry({log_level => 3, message_key => "an_variables_0002", message_variables => {
name1 => "cache::db_fh::$id", value1 => $an->data->{cache}{db_fh}{$id},
name2 => "sys::db_timestamp", value2 => $an->data->{sys}{db_timestamp}
}, file => $THIS_FILE, line => __LINE__});
if (not $an->data->{sys}{db_timestamp}) if (not $an->data->{sys}{db_timestamp})
{ {
my $query = "SELECT cast(now() AS timestamp with time zone)"; my $query = "SELECT cast(now() AS timestamp with time zone)";
$an->Log->entry({log_level => 3, message_key => "an_variables_0001", message_variables => { $an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { query => $query });
name1 => "query", value1 => $query
}, file => $THIS_FILE, line => __LINE__});
$an->data->{sys}{db_timestamp} = $an->Database->query({id => $id, query => $query, source => $THIS_FILE, line => __LINE__})->[0]->[0];
$an->Log->entry({log_level => 3, message_key => "an_variables_0001", message_variables => { $an->data->{sys}{db_timestamp} = $an->Database->query({id => $id, query => $query, source => $THIS_FILE, line => __LINE__})->[0]->[0];
name1 => "sys::db_timestamp", value1 => $an->data->{sys}{db_timestamp}, $an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { "sys::db_timestamp" => $an->data->{sys}{db_timestamp} });
}, file => $THIS_FILE, line => __LINE__});
} }
$an->Log->entry({log_level => 3, message_key => "an_variables_0003", message_variables => { $an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
name1 => "sys::read_db_id", value1 => $an->data->{sys}{read_db_id}, "sys::read_db_id" => $an->data->{sys}{read_db_id},
name2 => "sys::use_db_fh", value2 => $an->data->{sys}{use_db_fh}, "sys::use_db_fh" => $an->data->{sys}{use_db_fh},
name3 => "sys::db_timestamp", value3 => $an->data->{sys}{db_timestamp}, "sys::db_timestamp" => $an->data->{sys}{db_timestamp},
}, file => $THIS_FILE, line => __LINE__}); });
} }
} }
# Do I have any connections? Don't die, if not, just return. # Do I have any connections? Don't die, if not, just return.
$an->Log->entry({log_level => 3, message_key => "an_variables_0001", message_variables => { $an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { connections => $connections }});
name1 => "connections", value1 => $connections,
}, file => $THIS_FILE, line => __LINE__});
if (not $connections) if (not $connections)
{ {
# Failed to connect to any database. Log this, print to the caller and return. # Failed to connect to any database. Log this, print to the caller and return.
$an->Log->entry({log_level => 1, message_key => "tools_log_0021", message_variables => { $an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0091"});
title => $an->String->get({key => "tools_title_0003"}),
message => $an->String->get({key => "error_message_0060"}),
}, file => $THIS_FILE, line => __LINE__});
print $an->String->get({ key => "tools_log_0021", variables => {
title => $an->String->get({key => "tools_title_0003"}),
message => $an->String->get({key => "error_message_0060"}),
}})."\n";
return($connections); return($connections);
} }
@ -385,9 +372,9 @@ sub connect
my $error_array = []; my $error_array = [];
# Delete this DB so that we don't try to use it later. # Delete this DB so that we don't try to use it later.
$an->Log->entry({log_level => 3, message_key => "error_title_0018", message_variables => { $an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 1, priority => "alert", key => "log_0092", variables => { server => $say_server, id => $id }});
id => $id
}, file => $THIS_FILE, line => __LINE__}); # Delete it from the list of known databases for this run.
delete $an->data->{database}{$id}; delete $an->data->{database}{$id};
# If I've not sent an alert about this DB loss before, send one now. # If I've not sent an alert about this DB loss before, send one now.
@ -562,6 +549,9 @@ sub initialize
sql_file => $sql_file, sql_file => $sql_file,
}}); }});
# This just makes some logging cleaner below.
my $say_server = $an->data->{database}{$id}{host}.":".$an->data->{database}{$id}{port}." -> ".$an->data->{database}{$id}{name};
if (not $id) if (not $id)
{ {
# No database to talk to... # No database to talk to...
@ -577,7 +567,7 @@ sub initialize
if (not $sql_file) if (not $sql_file)
{ {
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0079", variables => { $an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0079", variables => {
server => $an->data->{database}{$id}{host}.":".$an->data->{database}{$id}{port}." -> ".$an->data->{database}{$id}{name} server => $say_server,
id => $id, id => $id,
}}); }});
return(0); return(0);
@ -585,7 +575,7 @@ sub initialize
elsif (not -e $sql_file) elsif (not -e $sql_file)
{ {
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0080", variables => { $an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0080", variables => {
server => $an->data->{database}{$id}{host}.":".$an->data->{database}{$id}{port}." -> ".$an->data->{database}{$id}{name} server => $say_server,
id => $id, id => $id,
sql_file => $sql_file, sql_file => $sql_file,
}}); }});
@ -594,7 +584,7 @@ sub initialize
elsif (not -r $sql_file) elsif (not -r $sql_file)
{ {
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0081", variables => { $an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0081", variables => {
server => $an->data->{database}{$id}{host}.":".$an->data->{database}{$id}{port}." -> ".$an->data->{database}{$id}{name} server => $say_server,
id => $id, id => $id,
sql_file => $sql_file, sql_file => $sql_file,
}}); }});
@ -603,13 +593,13 @@ sub initialize
# Tell the user we need to initialize # Tell the user we need to initialize
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 1, key => "log_0082", variables => { $an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 1, key => "log_0082", variables => {
server => $an->data->{database}{$id}{host}.":".$an->data->{database}{$id}{port}." -> ".$an->data->{database}{$id}{name} server => $say_server,
id => $id, id => $id,
sql_file => $sql_file, sql_file => $sql_file,
}}); }});
$an->Log->entry({log_level => 1, title_key => "tools_title_0005", message_key => "tools_log_0020", message_variables => { $an->Log->entry({log_level => 1, title_key => "tools_title_0005", message_key => "tools_log_0020", message_variables => {
server => $an->data->{database}{$id}{host}.":".$an->data->{database}{$id}{port}." -> ".$an->data->{database}{$id}{name} server => $say_server,
sql_file => $sql_file, sql_file => $sql_file,
}, file => $THIS_FILE, line => __LINE__}); }, file => $THIS_FILE, line => __LINE__});
@ -720,6 +710,9 @@ sub query
source => $source, source => $source,
}}); }});
# Make logging code a little cleaner
my $say_server = $an->data->{database}{$id}{host}.":".$an->data->{database}{$id}{port}." -> ".$an->data->{database}{$id}{name};
if (not $id) if (not $id)
{ {
# No database to talk to... # No database to talk to...
@ -736,7 +729,7 @@ sub query
{ {
# No query # No query
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0084", variables => { $an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0084", variables => {
server => $an->data->{database}{$id}{host}.":".$an->data->{database}{$id}{port}." -> ".$an->data->{database}{$id}{name}, server => $say_server,
}}); }});
return(undef); return(undef);
} }
@ -759,15 +752,15 @@ sub query
# Do the query. # Do the query.
my $DBreq = $an->data->{cache}{db_fh}{$id}->prepare($query) or $an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0075", variables => { my $DBreq = $an->data->{cache}{db_fh}{$id}->prepare($query) or $an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0075", variables => {
query => ((not $an->Log->secure) && ($secure)), query => ((not $an->Log->secure) && ($secure)) ? $query : "--",
server => $an->data->{database}{$id}{host}.":".$an->data->{database}{$id}{port}." -> ".$an->data->{database}{$id}{name}, server => $say_server,
db_error => $DBI::errstr, db_error => $DBI::errstr,
}}); }});
# Execute on the query # Execute on the query
$DBreq->execute() or $an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0076", variables => { $DBreq->execute() or $an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0076", variables => {
query => ((not $an->Log->secure) && ($secure)), query => ((not $an->Log->secure) && ($secure)) ? $query : "--",
server => $an->data->{database}{$id}{host}.":".$an->data->{database}{$id}{port}." -> ".$an->data->{database}{$id}{name}, server => $say_server,
db_error => $DBI::errstr, db_error => $DBI::errstr,
}}); }});
@ -800,13 +793,14 @@ sub write
source => $source, source => $source,
}}); }});
# Make logging code a little cleaner
my $say_server = $an->data->{database}{$id}{host}.":".$an->data->{database}{$id}{port}." -> ".$an->data->{database}{$id}{name};
# We don't check if ID is set here because not being set simply means to write to all available DBs. # We don't check if ID is set here because not being set simply means to write to all available DBs.
if (not $query) if (not $query)
{ {
# No query # No query
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0085", variables => { $an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0085", variables => { server => $say_server }});
server => $an->data->{database}{$id}{host}.":".$an->data->{database}{$id}{port}." -> ".$an->data->{database}{$id}{name},
}});
return(undef); return(undef);
} }
@ -938,8 +932,8 @@ sub write
# Do the do. # Do the do.
$an->data->{cache}{db_fh}{$id}->do($query) or $an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0090", variables => { $an->data->{cache}{db_fh}{$id}->do($query) or $an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0090", variables => {
query => ((not $an->Log->secure) && ($secure)), query => ((not $an->Log->secure) && ($secure)) ? $query : "--",
server => $an->data->{database}{$id}{host}.":".$an->data->{database}{$id}{port}." -> ".$an->data->{database}{$id}{name}, server => $say_server,
db_error => $DBI::errstr, db_error => $DBI::errstr,
}}); }});
} }
@ -988,15 +982,16 @@ sub _test_access
my $id = $parameter->{id} ? $parameter->{id} : ""; my $id = $parameter->{id} ? $parameter->{id} : "";
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { id => $id }}); $an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { id => $id }});
# Make logging code a little cleaner
my $say_server = $an->data->{database}{$id}{host}.":".$an->data->{database}{$id}{port}." -> ".$an->data->{database}{$id}{name};
# Log our test # Log our test
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 2, key => "log_0087", variables => { $an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 2, key => "log_0087", variables => { server => $say_server }});
server => $an->data->{database}{$id}{host}.":".$an->data->{database}{$id}{port}." -> ".$an->data->{database}{$id}{name},
}});
my $query = "SELECT 1"; my $query = "SELECT 1";
my $DBreq = $an->data->{cache}{db_fh}{$id}->prepare($query) or $an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0075", variables => { my $DBreq = $an->data->{cache}{db_fh}{$id}->prepare($query) or $an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0075", variables => {
query => $query, query => $query,
server => $an->data->{database}{$id}{host}.":".$an->data->{database}{$id}{port}." -> ".$an->data->{database}{$id}{name}, server => $say_server,
db_error => $DBI::errstr, db_error => $DBI::errstr,
}}); }});
@ -1004,7 +999,7 @@ sub _test_access
alarm(10); alarm(10);
$DBreq->execute() or $an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0076", variables => { $DBreq->execute() or $an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0076", variables => {
query => $query, query => $query,
server => $an->data->{database}{$id}{host}.":".$an->data->{database}{$id}{port}." -> ".$an->data->{database}{$id}{name}, server => $say_server,
db_error => $DBI::errstr, db_error => $DBI::errstr,
}}); }});
# If we're here, we made contact. # If we're here, we made contact.

@ -142,6 +142,8 @@ The database connection error was:
<key name="log_0088">Access confirmed.</key> <key name="log_0088">Access confirmed.</key>
<key name="log_0089"><![CDATA[[ Error ] - The method Database->write() was asked to write to the database with ID: [#!variable!id!#] but there is no file handle open to the database. Was the connection lost?]]></key> <key name="log_0089"><![CDATA[[ Error ] - The method Database->write() was asked to write to the database with ID: [#!variable!id!#] but there is no file handle open to the database. Was the connection lost?]]></key>
<key name="log_0090"><![CDATA[[ Error ] - Failed to 'do' the database query: [#!variable!query!#] on: [#!variable!server!#]. The error was: [#!variable!db_error!#]. Note that if the query reports '--', the query was listed as containing sensitive data and '$an->Log->secure' is not set.]]></key> <key name="log_0090"><![CDATA[[ Error ] - Failed to 'do' the database query: [#!variable!query!#] on: [#!variable!server!#]. The error was: [#!variable!db_error!#]. Note that if the query reports '--', the query was listed as containing sensitive data and '$an->Log->secure' is not set.]]></key>
<key name="log_0091">Failed to connect to any database.</key>
<key name="log_0092"><![CDATA[[ Error ] - Unable to connect to the database: [#!variable!server!#] (id: [#!variable!id!#]_.]]></key>
<!-- Test words. Do NOT change unless you update 't/Words.t' or tests will needlessly fail. --> <!-- Test words. Do NOT change unless you update 't/Words.t' or tests will needlessly fail. -->
<key name="t_0000">Test</key> <key name="t_0000">Test</key>

Loading…
Cancel
Save