Доброго всем
¡ ¡ ¡ ALL GLORY TO GLORIA ! ! !
DBIx::Mojo::Model - base class for DBI models with templating statements by Mojo::Template.
Init once base singleton for process with dbh and (optional) template vars:
use DBIx::Mojo::Model; DBIx::Mojo::Model->singleton(dbh=>$dbh, template_vars=>$t);
In child model must define SQL dict in __DATA__ of model package:
package Model::Foo; use Mojo::Base 'DBIx::Mojo::Model'; # optional outside dict __DATA__ our $DATA = ['Foo.pm.sql']; sub new { state $self = shift->SUPER::new(mt=>{tag_start=>'{%', tag_end=>'%}'}, @_); } sub foo { my $self = ref $_[0] ? shift : shift->new; $self->dbh->selectrow_hashref($self->sth('foo', where => 'where id=?',), undef, (shift)); } __DATA__ @@ foo?cached=1 %# my foo statement with prepare_cached select * from foo {% $where %} ;
In controller:
... has model => sub { require Model::Foo; Model::Foo->new }; sub actionFoo { my $c = shift; my $foo = $c->model->foo($c->param('id')); ... }
DBIx::Mojo::Template object. If not defined then will auto create from __DATA__ current model package.
Model::Foo->new(dict=>DBIx::Mojo::Template->new('Model::Foo', ...), ...)
Hashref Mojo::Template object attributes. Will passed to Mojo::Template->new then dict auto create
Mojo::Template->new
Hashref variables applies in statement templates.
Boolean switch: 1(true) - use DBI caching ($dbh->prepare_cached) and 0(false) overvise this module caching.
The statement must has defined cached param:
cached
@@ foo query name?cached=1 select ...
Defaults is true for save statement inside DBIx::Mojo::Statement object atribute sth.
sth
Define or copy/merge attributes from singleton.
singleton
Initialize default attributes for child model modules. Mostly dbh and template_vars
dbh
template_vars
This is main method.
First input arg is dict statement name, next args key => val are template variables. Return DBI prepared (cached if param 'cached' is true) statement.
key => val
Body of template statement get as:
$mFoo->dict->{'foo'}->sql
Templates name can has additional params as ulr query:
@@ foo.bar/baz?a=156&b=c ...
then model object the name of statement is url path and query is param:
$mFoo->dict->{'foo.bar/baz'}->param->{b} # 'c'
Михаил Че (Mikhail Che), <mche[-at-]cpan.org>
<mche[-at-]cpan.org>
Please report any bugs or feature requests at https://github.com/mche/Mojolicious-Plugin-RoutesAuthDBI/issues. Pull requests also welcome.
Copyright 2016 Mikhail Che.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install DBIx::Mojo::Template, copy and paste the appropriate command in to your terminal.
cpanm
cpanm DBIx::Mojo::Template
CPAN shell
perl -MCPAN -e shell install DBIx::Mojo::Template
For more information on module installation, please visit the detailed CPAN module installation guide.