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