App-MonM
view release on metacpan or search on metacpan
lib/App/MonM/Store.pm view on Meta::CPAN
Checks the connection to database
=head2 set
$store->set(
id => 1,
name => "foo",
type => "http",
source => "http://example.com",
status => 1,
message => "Ok"
) or die $store->error;
Update existing record on database
=head1 SEE ALSO
L<App::MonM>, L<CTK::DBI>
=head1 AUTHOR
Serż Minus (Sergey Lepenkov) L<https://www.serzik.com> E<lt>abalama@cpan.orgE<gt>
=head1 COPYRIGHT
Copyright (C) 1998-2022 D&D Corporation. All Rights Reserved
=head1 LICENSE
This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.
See C<LICENSE> file and L<https://dev.perl.org/licenses/>
=cut
use vars qw/ $VERSION /;
$VERSION = '1.01';
use Carp;
use CTK::DBI;
use CTK::Util qw/ touch /;
use CTK::ConfGenUtil;
use CTK::TFVals qw/ :ALL /;
use File::Spec;
use App::MonM::Const;
use App::MonM::Util qw/ set2attr /;
use constant {
DB_FILENAME => 'monm.db',
DEFAULT_DSN_MASK => 'dbi:SQLite:dbname=%s',
DEFAULT_DBI_ATTR => {
dsn => '', # See DEFAULT_DSN_MASK
user => '',
password => '',
set => [
'RaiseError 0',
'PrintError 0',
'sqlite_unicode 1',
],
},
};
use constant CHECKIT_DDL => <<'DDL';
CREATE TABLE IF NOT EXISTS monm (
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
`time` NUMERIC DEFAULT 0, -- time()
`name` CHAR(255) DEFAULT NULL, -- name of checkit section
`type` CHAR(32) DEFAULT NULL, -- http/dbi/command
`source` CHAR(255) DEFAULT NULL, -- URL/DSN/Command
`status` INTEGER DEFAULT 0, -- status value
`message` TEXT DEFAULT NULL -- message
)
DDL
use constant CHECKIT_INSERT => <<'DML';
INSERT INTO monm
(`time`, `name`, `type`, `source`, `status`, `message`)
VALUES
(?, ?, ?, ?, ?, ?)
DML
use constant CHECKIT_UPDATE => <<'DML';
UPDATE monm SET
`time` = ?, `name` = ?, `type` = ?, `source` = ?, `status` = ?, `message` = ?
WHERE `id` = ?
DML
use constant CHECKIT_DELETE => <<'DML';
DELETE FROM monm WHERE `id` = ?
DML
use constant CHECKIT_CLEAN => <<'DML';
DELETE FROM monm WHERE `time` < ?
DML
use constant CHECKIT_SELECT => <<'DML';
SELECT `id`, `time`, `name`, `type`, `source`, `status`, `message`
FROM monm
WHERE `name` = ?
DML
use constant CHECKIT_SELECT_ALL => <<'DML';
SELECT `id`, `time`, `name`, `type`, `source`, `status`, `message`
FROM monm
DML
sub new {
my $class = shift;
my %args = @_;
unless ($args{dsn}) {
my $dda = DEFAULT_DBI_ATTR;
foreach (%$dda) {
$args{$_} //= $dda->{$_}
}
}
my $file = $args{file} || DB_FILENAME;
my $dsn = $args{dsn} || sprintf(DEFAULT_DSN_MASK, $file);
( run in 1.252 second using v1.01-cache-2.11-cpan-39bf76dae61 )