| 1 | package DBIx::Mojo::Template; | ||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use Mojo::Base -base; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use Mojo::Loader qw(data_section); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use Mojo::Template; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use Mojo::URL; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Mojo::Util qw(url_unescape b64_decode class_to_path);# | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Mojo::File; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Scalar::Util 'weaken'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | #~ has debug => $ENV{DEBUG_DBIx_Mojo_Template} || 0; | ||||||||||||||||||||||||||||
| 11 | #~ my $pkg = __PACKAGE__; | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 2 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 14 | 2 | my ($class) = shift; | |||||||||||||||||||||||||||
| 15 | 2 | bless $class->data(@_); | |||||||||||||||||||||||||||
| 16 | } | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 2 | ▶ | sub singleton { | ||||||||||||||||||||||||||
|
Subroutine
singleton: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 19 | 2 | my ($class) = shift; | |||||||||||||||||||||||||||
| 20 | 2 | state $singleton = bless {}; | |||||||||||||||||||||||||||
| 21 | 2 | my $data = $class->data(@_); | |||||||||||||||||||||||||||
| 22 | 2 | @$singleton{ keys %$data } = values %$data; | |||||||||||||||||||||||||||
| 23 | 2 | $singleton; | |||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 4 | ▶ | sub data { | ||||||||||||||||||||||||||
|
Subroutine
data: called CC=8 SCAR=20.9
Pod
documented
| |||||||||||||||||||||||||||||
| 27 | 4 | my ($class, $pkg, %arg) = @_; | |||||||||||||||||||||||||||
| 28 | 4 | ▶ | die "Package not defined!" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | unless $pkg; | ||||||||||||||||||||||||||||
| 30 | 4 | my $dict = {}; | |||||||||||||||||||||||||||
| 31 | 4 | ▶ | my $data = data_section($pkg) || {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
data_section($pkg) || {}
MC/DC0%0/1
data_section($pkg) || {}
data_section($pkg)
Truth table50%1/2
A
A= data_section($pkg)
| |||||||||||||||||||||||||||||
| 32 | 4 | ▶ | my $extra = $class->_data_dict_files($pkg => @{$arg{data} || []}); | ||||||||||||||||||||||||||
|
Condition100%2/2
$arg{'data'} || []
MC/DC100%1/1
$arg{'data'} || []
$arg{'data'}
Truth table100%2/2
A
A= $arg{'data'}
| |||||||||||||||||||||||||||||
| 33 | #~ if ref($arg{data}) eq 'ARRAY';#$pkg ne 'main' && | ||||||||||||||||||||||||||||
| 34 | #~ @$data{keys %$extra} = values %$extra | ||||||||||||||||||||||||||||
| 35 | #~ if ref($extra) eq 'HASH'; | ||||||||||||||||||||||||||||
| 36 | #prio: near over far | ||||||||||||||||||||||||||||
| 37 | 4 | @$extra{keys %$data} = values %$data; | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 4 | ▶ | while ( my ($k, $t) = each %$extra) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 16 | my $url = Mojo::URL->new($k); | |||||||||||||||||||||||||||
| 41 | 16 | my ($name, $param) = (url_unescape($url->path), $url->query->to_hash); | |||||||||||||||||||||||||||
| 42 | 16 | utf8::decode($name); | |||||||||||||||||||||||||||
| 43 | 16 | ▶ | $dict->{$name} = DBIx::Mojo::Statement->new(dict=>$dict, name=>$name, raw=>$t, param=>$param, mt=>_mt(%{$arg{mt} || {}}), vars=>$arg{vars} || {}); | ||||||||||||||||||||||||||
|
Condition100%4/4
$arg{'mt'} || {}
$arg{'vars'} || {}
MC/DC100%1/1
$arg{'mt'} || {}
$arg{'mt'}
MC/DC100%1/1
$arg{'vars'} || {}
$arg{'vars'}
Truth table100%2/2
A
A= $arg{'mt'}
Truth table100%2/2
A
A= $arg{'vars'}
| |||||||||||||||||||||||||||||
| 44 | #~ weaken $dict->{$name}->{dict}; | ||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | 4 | ▶ | die "None DATA dict in package [$pkg]" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | unless %$dict; | ||||||||||||||||||||||||||||
| 48 | 4 | return $dict; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 16 | ▶ | sub _mt { | ||||||||||||||||||||||||||
|
Subroutine
_mt: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 52 | 16 | Mojo::Template->new(vars => 1, prepend=>'no strict qw(vars); no warnings qw(uninitialized);', @_);# line_start=>'$$', | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 0 | ▶ | sub template { shift->render(@_) } | ||||||||||||||||||||||||||
|
Subroutine
template: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 10 | ▶ | sub render { | ||||||||||||||||||||||||||
|
Subroutine
render: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 58 | 10 | my ($self, $key, %arg) = @_; | |||||||||||||||||||||||||||
| 59 | die "No such item by key [$key] on this DICT, please check processed package" | ||||||||||||||||||||||||||||
| 60 | 10 | ▶ | unless $self->{$key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 10 | $self->{$key}->render(%arg); | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | # можно задать для модуля доп файлы словаря | ||||||||||||||||||||||||||||
| 66 | # $self->_data_dict_files('Foo::Bar'=>'Bar.pm.dict.sql') | ||||||||||||||||||||||||||||
| 67 | # на входе модуль и список имен доп файлов ОТНОСИТЕЛЬНО папки модуля | ||||||||||||||||||||||||||||
| 68 | # @return hashref dict | ||||||||||||||||||||||||||||
| 69 | 4 | ▶ | sub _data_dict_files { | ||||||||||||||||||||||||||
|
Subroutine
_data_dict_files: called CC=8 SCAR=21.2
| |||||||||||||||||||||||||||||
| 70 | 4 | my ($self, $pkg, @files) = @_; | |||||||||||||||||||||||||||
| 71 | #~ require Module::Path; | ||||||||||||||||||||||||||||
| 72 | #~ Module::Path->import('module_path');module_path($pkg) | ||||||||||||||||||||||||||||
| 73 | 4 | ▶ | my $dir = Mojo::File->new($INC{class_to_path($pkg)} || '.')->dirname;## | ||||||||||||||||||||||||||
|
Condition100%2/2
$INC{class_to_path($pkg)} || '.'
MC/DC100%1/1
$INC{class_to_path($pkg)} || '.'
$INC{class_to_path($pkg)}
Truth table100%2/2
A
A= $INC{class_to_path($pkg)}
| |||||||||||||||||||||||||||||
| 74 | 4 | my $dict = {}; | |||||||||||||||||||||||||||
| 75 | 4 | for my $file (@files) { | |||||||||||||||||||||||||||
| 76 | 1 | my $path = Mojo::File->new($file); | |||||||||||||||||||||||||||
| 77 | 1 | ▶ | $path = $dir->child($file) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | unless $path->is_abs; | ||||||||||||||||||||||||||||
| 79 | 1 | ▶ | next unless -f $path && -r _; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
-f $path and -r _
MC/DC0%0/2
-f $path and -r _
-f $path
-r _
Truth table33%1/3
A and B
A= -f $pathB= -r _
| |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 1 | my $data = $path->slurp; | |||||||||||||||||||||||||||
| 82 | 1 | utf8::decode($data); | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | ## copy-paste from Mojo::Loader | ||||||||||||||||||||||||||||
| 85 | # Ignore everything before __DATA__ (some versions seek to start of file) | ||||||||||||||||||||||||||||
| 86 | 1 | $data =~ s/^.*\n__DATA__\r?\n/\n/s; | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | # Ignore everything after __END__ | ||||||||||||||||||||||||||||
| 89 | 1 | $data =~ s/\n__END__\r?\n.*$/\n/s; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | # Split files | ||||||||||||||||||||||||||||
| 92 | 1 | (undef, my @f) = split /^@@\s*(.+?)\s*\r?\n/m, $data; | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | # Find data | ||||||||||||||||||||||||||||
| 95 | 1 | ▶ | while (@f) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 2 | my ($name, $data) = splice @f, 0, 2; | |||||||||||||||||||||||||||
| 97 | 2 | ▶ | $dict->{$name} = $name =~ s/\s*\(\s*base64\s*\)$// ? b64_decode($data) : $data; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | 4 | return $dict; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 2 | our $VERSION = '0.061'; | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | #============================================= | ||||||||||||||||||||||||||||
| 106 | package DBIx::Mojo::Statement; | ||||||||||||||||||||||||||||
| 107 | #============================================= | ||||||||||||||||||||||||||||
| 108 | 2 | ▶ | use Mojo::Base -base; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 109 | 2 | ▶ | use Hash::Merge qw(merge); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 110 | 2 | ▶ | use Scalar::Util 'weaken'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 2 | has [qw(dict sth)], undef, weak=>1; | |||||||||||||||||||||||||||
| 113 | 2 | has [qw(name raw param mt vars )]; | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | # sth - attr for save cached dbi statement | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 2 | ▶ | use overload '""' => sub { shift->raw }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 0 | ▶ | sub template { shift->render(@_) } | ||||||||||||||||||||||||||
|
Subroutine
template: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 21 | ▶ | sub render { | ||||||||||||||||||||||||||
|
Subroutine
render: called CC=3 SCAR=11.3
| |||||||||||||||||||||||||||||
| 122 | 21 | my $self = shift; | |||||||||||||||||||||||||||
| 123 | 21 | ▶ | my $vars =ref $_[0] ? shift : { @_ }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | 21 | my $merge = merge($vars, $self->vars); | |||||||||||||||||||||||||||
| 125 | 21 | ▶ | $merge->{dict} ||= $self->dict; | ||||||||||||||||||||||||||
|
Condition33%1/3
$merge->{"dict"} ||= $self->dict
MC/DC0%0/2
$merge->{"dict"} ||= $self->dict
$merge->{"dict"}
$self->dict
Truth table33%1/3
A ||= B
A= $merge->{"dict"}B= $self->dict
| |||||||||||||||||||||||||||||
| 126 | 21 | $merge->{DICT} = $self->dict; | |||||||||||||||||||||||||||
| 127 | 21 | $merge->{st} = $self; | |||||||||||||||||||||||||||
| 128 | 21 | weaken $merge->{st}; | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 21 | $self->mt->render($self->raw, $merge);#%$vars ? %{$self->vars} ? merge($vars, $self->vars) : $vars : $self->vars | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | =pod | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | =encoding utf8 | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | Доброго всем | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | =head1 DBIx::Mojo::Template | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | ¡ ¡ ¡ ALL GLORY TO GLORIA ! ! ! | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | =head1 NAME | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | DBIx::Mojo::Template - Render SQL statements by Mojo::Template | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | =head1 VERSION | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 0.061 | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | use DBIx::Mojo::Template; | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | my $dict = DBIx::Mojo::Template->new(__PACKAGE__,mt=>{tag_start=>'%{', tag_end=>'%}',}); | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | my $sql = $dict->{'foo'}->render(table=>'foo', where=> 'where id=?'); | ||||||||||||||||||||||||||||
| 159 | # or same | ||||||||||||||||||||||||||||
| 160 | my $sql = $dict->render('bar', where=> 'where id=?'); | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | __DATA__ | ||||||||||||||||||||||||||||
| 163 | @@ foo?cache=1 | ||||||||||||||||||||||||||||
| 164 | %# my foo statement with prepare_cached (model sth) | ||||||||||||||||||||||||||||
| 165 | select * | ||||||||||||||||||||||||||||
| 166 | from {% $table %} | ||||||||||||||||||||||||||||
| 167 | {% $where %} | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | =head1 SUBROUTINES/METHODS | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | =head2 new | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | my $dict = DBIx::Mojo::Template->new('Foo::Bar', vars=>{...}, mt=>{...}) | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | where arguments: | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | =over 4 | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | =item * $pkg (string) | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | Package name, where __DATA__ section SQL dictionary. Package must be loaded (use/require) before! | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | =item * vars (hashref) | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | Hashref of this dict templates variables. Vars can be merged when render - see L<#render>. | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | =item * mt (hashref) | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | For Mojo::Template object attributes. See L<Mojo::Template#ATTRIBUTES>. | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | mt=>{ line_start=>'+', } | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | Defaults <mt> attrs: | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | mt=> {vars => 1, prepend=>'no strict qw(vars); no warnings qw(uninitialized);',} | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | =item * data (arrayref) - optional | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | Define extra data files for dictionary. Absolute or relative to path of the module $pkg file point. | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | =back | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | =head2 singleton | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | Merge ditcs packages to one. Arguments same as L<#new>. | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | DBIx::Mojo::Template->singleton('Foo'); | ||||||||||||||||||||||||||||
| 209 | my $dict = DBIx::Mojo::Template->singleton('Bar'); | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | =head2 render | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | Render template dict key. | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | my $sql = $dict->render($key, var1=>..., var2 => ...,); | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | Each dict item is a object DBIx::Mojo::Statement with one method C<render>: | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | my $sql = $dict->{'key foo'}->render(bar=>'baz', ...); | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | =head2 data | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | Same as L<#new> but returns unblessed hashref dict. | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | Михаил Че (Mikhail Che), C<< <mche[-at-]cpan.org> >> | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | =head1 BUGS / CONTRIBUTING | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | Please report any bugs or feature requests at L<https://github.com/mche/DBIx-Mojo-Template/issues>. Pull requests also welcome. | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | Copyright 2016 Михаил Че (Mikhail Che). | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | This module is free software; you can redistribute it and/or modify it under the term of the Perl itself. | ||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | =cut | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | 2 | 1; # End of DBIx::Mojo::Template | |||||||||||||||||||||||||||