feature->import(':5.10');
}
+sub on_start_up {
+ return 1;
+}
+
+sub table_exists {
+ my ($self, $table) = @_;
+
+ my $dbh = $self->connect_db;
+ my $DB_NAME = $self->{options}->{Database}->{name};
+
+ my $sql = <<"EOSQL";
+SELECT * FROM information_schema.TABLES
+ WHERE TABLE_SCHEMA = ?
+ AND TABLE_NAME = ?
+EOSQL
+ my $sth = $dbh->prepare($sql);
+ $sth->execute($DB_NAME, $table);
+ my $rs = $sth->fetchall_arrayref({});
+ $dbh->disconnect;
+ if (@$rs == 0) {
+ return;
+ }
+ return 1;
+}
+
sub new {
my ($class, $options) = @_;
bless {options => $options, _error => undef}, $class;
my $caller = caller;
{
+ # export functions
no strict 'refs';
*{"$caller\::load"} = \&load_model;
*{"$caller\::loader"} = \&loader;
+ *{"$caller\::startup"} = \&startup;
}
$_->import for qw(strict warings utf8);
state $models;
sub loader {
- my ($self, $opts) = @_;
+ my ($opts) = @_;
my $r = $models;
return sub {
my $caller = shift;
$models->{$accessor} = $class;
}
+sub startup {
+ my $opts = shift;
+ return 1 if !$models;
+ for my $k (keys %$models) {
+ my $m = $models->{$k}->new($opts);
+ return if !$m->on_start_up;
+ }
+ return 1;
+}
+
1;
# add Model
my $model_opts = $app->config;
$model_opts->{Logger} = $app->log;
- $app->helper(model => Newslash::Model->loader($model_opts));
+ $app->helper(model => Newslash::Model::loader($model_opts));
+ Newslash::Model::startup($model_opts);
# use Template::Toolkit 2 render
$app->plugin('Newslash::Plugin::TT2Renderer');