Task7/TestProject/lib/DB.pm
2023-11-09 14:49:14 +03:00

62 lines
1.3 KiB
Perl

package DB;
use strict;
use warnings FATAL => 'all';
use open qw( :std :encoding(UTF-8) );
use DBI;
sub new {
my $class = shift;
my $params = shift;
my $self = {
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',
dbh => undef,
db_params => $params->{db_params} || {
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 {
my $self = shift;
my $address = shift;
my $alert = 0;
my $sql_logs_template = "select created,str from log where address = ?
order by created,int_id ;";
my $dbh = $self->{dbh};
my $sth = $dbh->prepare($sql_logs_template);
my $rs = $sth->execute($address);
if ( $rs > 100 ) {
$alert = 1;
}
my $table = $sth->fetchall_arrayref;
return {
alert => $alert,
table =>$table
};
}
1;