Task7/TestProject/lib/DB.pm

63 lines
1.3 KiB
Perl
Raw Permalink Normal View History

2023-11-08 14:23:03 +03:00
package DB;
use strict;
use warnings FATAL => 'all';
use open qw( :std :encoding(UTF-8) );
use DBI;
sub new {
my $class = shift;
2023-11-09 14:49:14 +03:00
my $params = shift;
2023-11-08 14:23:03 +03:00
my $self = {
2023-11-09 14:49:14 +03:00
db_info => $params->{db_info} || 'dbi:Pg:dbname=postgres;host=localhost;port=5432',
db_username => $params->{db_username} || 'postgres',
db_password => $params->{db_password} || '0000',
2023-11-08 14:23:03 +03:00
dbh => undef,
2023-11-09 14:49:14 +03:00
db_params => $params->{db_params} || {
2023-11-08 14:23:03 +03:00
postgres_enable_utf8 => 1,
RaiseError => 1,
},
};
return bless $self, $class;
}
sub connect {
my $self = shift;
$self->{dbh} = DBI->connect(
$self->{db_info}, $self->{db_username},
$self->{db_password}, $self->{db_another_info}
);
return 1;
}
sub get_rows {
2023-11-09 14:49:14 +03:00
my $self = shift;
2023-11-08 14:23:03 +03:00
my $address = shift;
my $alert = 0;
2023-11-09 14:49:14 +03:00
my $sql_logs_template = "select created,str from log where address = ?
2023-11-08 14:23:03 +03:00
order by created,int_id ;";
2023-11-09 14:49:14 +03:00
my $dbh = $self->{dbh};
2023-11-08 14:23:03 +03:00
2023-11-09 14:49:14 +03:00
my $sth = $dbh->prepare($sql_logs_template);
2023-11-08 14:23:03 +03:00
my $rs = $sth->execute($address);
2023-11-09 14:49:14 +03:00
if ( $rs > 100 ) {
2023-11-08 14:23:03 +03:00
$alert = 1;
}
my $table = $sth->fetchall_arrayref;
2023-11-09 14:49:14 +03:00
return {
alert => $alert,
table =>$table
};
2023-11-08 14:23:03 +03:00
}
1;