Task7/TestProject/lib/DB.pm
2023-11-08 14:23:03 +03:00

61 lines
1.2 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 $self = {
db_info => 'dbi:Pg:dbname=postgres;host=localhost;port=5432',
db_username => 'postgres',
db_password => '0000',
dbh => undef,
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 $class = shift;
my $address = shift;
my $alert = 0;
my $sql_logs = "select created,str from log where address = ?
order by created,int_id ;";
my $dbh = $class->{dbh};
my $sth = $dbh->prepare($sql_logs);
my $rs = $sth->execute($address);
if ( $rs eq '0E0' ) {
return ( 0, [ [ 'Нет данных', 'Нет данных' ] ] );
}
elsif ( $rs > 100 ) {
$alert = 1;
}
my $table = $sth->fetchall_arrayref;
return $alert, $table;
}
1;