ONO::Cron::CronSync::DBMS

package ONO::Cron::CronSync::DBMS;
################################################################################
# COPYRIGHT / LICENSE #
################################################################################
#
# This file is part of the ONO Software Project.
#
# Copyright (C) 2000-2025 Jos KIRPS [ www.kirps.com | jos_AT_kirps_DOT_com ]
# and The Joopita Project [ www.joopita.org | contact_AT_joopita_DOT_com ]
#
# This file, as well as other parts of the ONO Software Project or related
# elements, are FREE SOFTWARE available under the ARTISTIC LICENSE 2.0.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# For the full license, see /ono/osr/license/LICENSE.txt, or write to
# jos_AT_kirps_DOT_com or contact_AT_joopita_DOT_com.
#
################################################################################
# END OF COPYRIGHT / LICENSE, HERE COMES THE CODE ... #
################################################################################

use strict;

use ONO::DB;
use ONO::Cron::CronSync::ToolBox;

###############################################################################
# ONO
###############################################################################

#: Database related services for ONO's CronSync service.

sub run {

my (
$self,
$CRON_DIR,
$job,
$data_ref,
$TYPE_ref,
$vars_ref,
$vpath,
$sec,$min,$hour,
$mday,$mon,$year,
$wday,$yday,
$timestamp,
$switches,
) = @_;

#: Execution logic.

my %data = %$data_ref;
my %TYPE = %$TYPE_ref;
my %vars = %$vars_ref;

my ($DATA,$MAIL);

if ($data{'type'} eq "dbms_import") {
}

if ($data{'type'} eq "dbms_export") {
}

if ($data{'type'} eq "dbms_copy") {
}

if ($data{'type'} eq "dbms_merge") {
}

if ($data{'type'} eq "dbms_timestamp") {

if ($data{'dbms_database'} && $data{'dbms_username'} && $data{'dbms_password'}) {

$DATA .= ONO::Cron::CronSync::ToolBox->print("Job '$data{'title'}': running for DB '$data{'dbms_database'}'",$switches);

my $db = ONO::DB->connect($data{'dbms_database'},"d",$data{'dbms_username'},$data{'dbms_password'});

if ($db) {

$DATA .= ONO::Cron::CronSync::ToolBox->print("Job '$data{'title'}': DB '$data{'dbms_database'}' connected [OK]",$switches);

my $found;

foreach my $table (ONO::DB->tables($db)) {
if ($table eq "ono_status") {
$DATA .= ONO::Cron::CronSync::ToolBox->print("Job '$data{'title'}': status table found [OK]",$switches);
$found++;
}
}

if (!$found) {

$DATA .= ONO::Cron::CronSync::ToolBox->print("Job '$data{'title'}': creating status table now, job will only execute next time [ABORT]",$switches);

ONO::DB->command(
$db,
"CREATE TABLE ono_status (
id INT unsigned NOT NULL auto_increment,
name varchar(32),
value varchar(255),
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
");

} else {

if (!ONO::DB->count($db,"ono_status","name = 'ono_db_timestamp'")) {
$DATA .= ONO::Cron::CronSync::ToolBox->print("Job '$data{'title'}': ono_db_timestamp not found, creating an initial record now [OK]",$switches);
ONO::DB->command($db,"INSERT INTO ono_status (name,value) VALUES ('ono_db_timestamp','$timestamp')");
}

ONO::DB->command($db,"UPDATE ono_status SET value = '$timestamp' WHERE name = 'ono_db_timestamp';");
$DATA .= ONO::Cron::CronSync::ToolBox->print("Job '$data{'title'}': updating ono_db_timestamp in ono_status to $timestamp [DONE]",$switches);

}

} else {

$DATA .= ONO::Cron::CronSync::ToolBox->print("Job '$data{'title'}': could not connect to DB '$data{'dbms_database'}' [ABORT]",$switches);
}

} else {

$DATA .= ONO::Cron::CronSync::ToolBox->print("Job '$data{'title'}': config missing or incomplete",$switches);

}

}

return ($DATA,$MAIL);

}

###############################################################################
# end of script
###############################################################################

1;

__END__