| 1 | package DBIx::Mojo::Model; | ||
| 2 | use Mojo::Base -base; | ||
| 3 | use Carp 'croak'; | ||
| 4 | use DBIx::Mojo::Template; | ||
| 5 | use Hash::Merge qw( merge ); | ||
| 6 | use Scalar::Util 'weaken'; | ||
| 7 | |||
| 8 | my %DICT_CACHE = ();# для каждого пакета/модуля | ||
| 9 | |||
| 10 | has [qw(dbh dict)], undef, weak=>1; | ||
| 11 | has [qw(template_vars mt)]; | ||
| 12 | |||
| 13 | has dbi_cache_st => 1; # 1 - use DBI caching and 0 overvise this module caching | ||
| 14 | |||
| 15 | has debug => $ENV{DEBUG_DBIx_Mojo_Model} || 0; | ||
| 16 | my $PKG = __PACKAGE__; | ||
| 17 | |||
| 18 | #init once | ||
| 19 | sub singleton { | ||
| 20 | state $singleton = shift->SUPER::new(@_); | ||
| 21 | } | ||
| 22 | |||
| 23 | # child model | ||
| 24 | sub new { | ||
| 25 | my $self = ref $_[0] ? shift : shift->SUPER::new(@_); | ||
| 26 | my $singleton = $self->singleton; | ||
| 27 | |||
| 28 | $self->dbh($singleton->dbh) | ||
| 29 | unless $self->dbh; | ||
| 30 | #~ weaken $self->{dbh}; | ||
| 31 | |||
| 32 | $self->template_vars(merge($self->template_vars || {}, $singleton->template_vars)) | ||
| 33 | #~ and weaken $self->{template_vars} | ||
| 34 | if $singleton->template_vars && %{$singleton->template_vars}; | ||
| 35 | |||
| 36 | $self->mt(merge($self->mt || {}, $singleton->mt)) | ||
| 37 | #~ and weaken $self->{mt} | ||
| 38 | if $singleton->mt && %{$singleton->mt}; | ||
| 39 | |||
| 40 | my $class = ref $self; | ||
| 41 | |||
| 42 | # pass extra class files (inside the class): our $DATA=['foo.txt', 'bar.txt']; | ||
| 43 | my $data_files = do { no strict 'refs'; ${"$class\::DATA"} }; | ||
| 44 | |||
| 45 | $self->dict( $DICT_CACHE{$class} ||= DBIx::Mojo::Template->new($class, mt => $self->mt, vars => $self->template_vars, $data_files ? (data => $data_files) : (), ) ) | ||
| 46 | unless $self->dict; | ||
| 47 | #~ weaken $self->{dict}; | ||
| 48 | |||
| 49 | $self->debug | ||
| 50 | && say STDERR "[DEBUG $PKG new] parsed dict keys [@{[keys %{$self->dict}]}] for class [$class]"; | ||
| 51 | |||
| 52 | $self; | ||
| 53 | } | ||
| 54 | |||
| 55 | sub sth { | ||
| 56 | my $self = shift; | ||
| 57 | my $name = shift; | ||
| 58 | my $attr = ref $_[0] eq 'HASH' ? shift : {}; | ||
| 59 | my $class = ref $self; | ||
| 60 | my $dict = $self->dict; | ||
| 61 | my $st = $dict->{$name} | ||
| 62 | or croak "No such name[$name] in SQL dict! @{[ join ':', keys %$dict ]}"; | ||
| 63 | #~ my %arg = @_; | ||
| 64 | my $sql = $st->render(@_).sprintf("\n--Statement name[%s]::[%s]", $class, $name); # ->template(%$template ? %arg ? %{merge($template, \%arg)} : %$template : %arg) | ||
| 65 | if (my $param_cached = $st->param && $st->param->{cached} || $st->param->{cache}) { | ||
| 66 | $self->debug | ||
| 67 | && say STDERR sprintf("[DEBUG $PKG sth] statement [%s]::[%s] is a %s", $class, $name, $self->dbi_cache_st ? 'DBI prepare_cached' : 'self model cached'); | ||
| 68 | return $self->dbh->prepare_cached($sql, $attr, $attr->{Async} || $attr->{pg_async} ? 3 : ()) # attr Acync for Mojo::Pg::Che | ||
| 69 | if $self->dbi_cache_st; | ||
| 70 | return $st->sth || $st->sth($self->dbh->prepare($sql, $attr))->sth; | ||
| 71 | } | ||
| 72 | $self->debug | ||
| 73 | && say STDERR sprintf("[DEBUG $PKG sth] statement [%s]::[%s] is a dbh prepare", $class, $name); | ||
| 74 | #~ local $dbh->{TraceLevel} = "3|DBD"; | ||
| 75 | return $self->dbh->prepare($sql, $attr); | ||
| 76 | } | ||
| 77 | |||
| 78 | =pod | ||
| 79 | |||
| 80 | =encoding utf8 | ||
| 81 | |||
| 82 | Доброго всем | ||
| 83 | |||
| 84 | =head1 DBIx::Mojo::Model | ||
| 85 | |||
| 86 | ¡ ¡ ¡ ALL GLORY TO GLORIA ! ! ! | ||
| 87 | |||
| 88 | =head1 NAME | ||
| 89 | |||
| 90 | DBIx::Mojo::Model - base class for DBI models with templating statements by Mojo::Template. | ||
| 91 | |||
| 92 | =head1 SINOPSYS | ||
| 93 | |||
| 94 | Init once base singleton for process with dbh and (optional) template vars: | ||
| 95 | |||
| 96 | use DBIx::Mojo::Model; | ||
| 97 | DBIx::Mojo::Model->singleton(dbh=>$dbh, template_vars=>$t); | ||
| 98 | |||
| 99 | In child model must define SQL dict in __DATA__ of model package: | ||
| 100 | |||
| 101 | package Model::Foo; | ||
| 102 | use Mojo::Base 'DBIx::Mojo::Model'; | ||
| 103 | |||
| 104 | # optional outside dict __DATA__ | ||
| 105 | our $DATA = ['Foo.pm.sql']; | ||
| 106 | |||
| 107 | sub new { | ||
| 108 | state $self = shift->SUPER::new(mt=>{tag_start=>'{%', tag_end=>'%}'}, @_); | ||
| 109 | } | ||
| 110 | |||
| 111 | sub foo { | ||
| 112 | my $self = ref $_[0] ? shift : shift->new; | ||
| 113 | $self->dbh->selectrow_hashref($self->sth('foo', where => 'where id=?',), undef, (shift)); | ||
| 114 | } | ||
| 115 | |||
| 116 | __DATA__ | ||
| 117 | @@ foo?cached=1 | ||
| 118 | %# my foo statement with prepare_cached | ||
| 119 | select * | ||
| 120 | from foo | ||
| 121 | {% $where %} | ||
| 122 | ; | ||
| 123 | |||
| 124 | In controller: | ||
| 125 | |||
| 126 | ... | ||
| 127 | has model => sub { require Model::Foo; Model::Foo->new }; | ||
| 128 | |||
| 129 | sub actionFoo { | ||
| 130 | my $c = shift; | ||
| 131 | my $foo = $c->model->foo($c->param('id')); | ||
| 132 | ... | ||
| 133 | |||
| 134 | } | ||
| 135 | |||
| 136 | =head1 ATTRIBUTES | ||
| 137 | |||
| 138 | =head2 dbh | ||
| 139 | |||
| 140 | =head2 dict | ||
| 141 | |||
| 142 | DBIx::Mojo::Template object. If not defined then will auto create from __DATA__ current model package. | ||
| 143 | |||
| 144 | Model::Foo->new(dict=>DBIx::Mojo::Template->new('Model::Foo', ...), ...) | ||
| 145 | |||
| 146 | =head2 mt | ||
| 147 | |||
| 148 | Hashref Mojo::Template object attributes. Will passed to C<< Mojo::Template->new >> then dict auto create | ||
| 149 | |||
| 150 | =head2 template_vars | ||
| 151 | |||
| 152 | Hashref variables applies in statement templates. | ||
| 153 | |||
| 154 | =head2 dbi_cache_st | ||
| 155 | |||
| 156 | Boolean switch: 1(true) - use DBI caching ($dbh->prepare_cached) | ||
| 157 | and 0(false) overvise this module caching. | ||
| 158 | |||
| 159 | The statement must has defined C<cached> param: | ||
| 160 | |||
| 161 | @@ foo query name?cached=1 | ||
| 162 | select ... | ||
| 163 | |||
| 164 | Defaults is true for save statement inside DBIx::Mojo::Statement object atribute C<sth>. | ||
| 165 | |||
| 166 | =head1 METHODS | ||
| 167 | |||
| 168 | =head2 new | ||
| 169 | |||
| 170 | Define or copy/merge attributes from C<singleton>. | ||
| 171 | |||
| 172 | =head2 singleton | ||
| 173 | |||
| 174 | Initialize default attributes for child model modules. Mostly C<dbh> and C<template_vars> | ||
| 175 | |||
| 176 | =head2 sth | ||
| 177 | |||
| 178 | This is main method. | ||
| 179 | |||
| 180 | First input arg is dict statement name, next args C<< key => val >> are template variables. | ||
| 181 | Return DBI prepared (cached if param 'cached' is true) statement. | ||
| 182 | |||
| 183 | =head1 Templates | ||
| 184 | |||
| 185 | Body of template statement get as: | ||
| 186 | |||
| 187 | $mFoo->dict->{'foo'}->sql | ||
| 188 | |||
| 189 | Templates name can has additional params as ulr query: | ||
| 190 | |||
| 191 | @@ foo.bar/baz?a=156&b=c | ||
| 192 | ... | ||
| 193 | |||
| 194 | then model object the name of statement is url path and query is param: | ||
| 195 | |||
| 196 | $mFoo->dict->{'foo.bar/baz'}->param->{b} # 'c' | ||
| 197 | |||
| 198 | =head1 AUTHOR | ||
| 199 | |||
| 200 | Михаил Че (Mikhail Che), C<< <mche[-at-]cpan.org> >> | ||
| 201 | |||
| 202 | =head1 BUGS / CONTRIBUTING | ||
| 203 | |||
| 204 | Please report any bugs or feature requests at L<https://github.com/mche/Mojolicious-Plugin-RoutesAuthDBI/issues>. Pull requests also welcome. | ||
| 205 | |||
| 206 | =head1 COPYRIGHT | ||
| 207 | |||
| 208 | Copyright 2016 Mikhail Che. | ||
| 209 | |||
| 210 | This library is free software; you can redistribute it and/or modify | ||
| 211 | it under the same terms as Perl itself. | ||
| 212 | |||
| 213 | =cut | ||
| 214 | |||
| 215 | 1; |