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