line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Service; |
2
|
2
|
|
|
2
|
|
1011
|
use Mojo::Base -base; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
28
|
|
3
|
2
|
|
|
2
|
|
399
|
use Carp qw/cluck confess/; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
900
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
has [qw/dbi models app c dmn parent/]; |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
sub model{ |
8
|
0
|
|
|
0
|
1
|
|
my ($self, $name) = @_; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
# Check model existence |
11
|
0
|
0
|
0
|
|
|
|
cluck qq{model "$name" is not yet created } unless($self->models && $self->models->{$name}); |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
# Get model |
14
|
0
|
|
|
|
|
|
return $self->models->{$name}; |
15
|
|
|
|
|
|
|
} |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub service{ |
18
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
19
|
0
|
0
|
|
|
|
|
return $self->parent->service(@_) if($self->parent); |
20
|
0
|
|
|
|
|
|
confess "require [parent] field"; |
21
|
|
|
|
|
|
|
} |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
## 调用 model 层的 create 方法 |
24
|
|
|
|
|
|
|
sub mcreate{ |
25
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
26
|
0
|
|
|
|
|
|
my $table = $self->dmn; |
27
|
0
|
|
|
|
|
|
my $model = $self->model($table); |
28
|
0
|
|
|
|
|
|
my $obj = $model->create(@_); |
29
|
0
|
0
|
0
|
|
|
|
return undef unless($obj && $obj->{object}); |
30
|
0
|
|
|
|
|
|
return $obj->{object}; |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
## 调用 model 层的 edit 方法 |
34
|
|
|
|
|
|
|
sub medit{ |
35
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
36
|
0
|
|
|
|
|
|
my $table = $self->dmn; |
37
|
0
|
|
|
|
|
|
my $model = $self->model($table); |
38
|
0
|
|
|
|
|
|
my $obj = $model->edit(@_); |
39
|
0
|
0
|
0
|
|
|
|
return undef unless($obj && $obj->{rows}); |
40
|
0
|
|
|
|
|
|
return $obj->{rows}; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
## 调用 model 层的 remove 方法 |
44
|
|
|
|
|
|
|
sub mremove{ |
45
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
46
|
0
|
|
|
|
|
|
my $table = $self->dmn; |
47
|
0
|
|
|
|
|
|
my $model = $self->model($table); |
48
|
0
|
|
|
|
|
|
my $obj = $model->remove(@_); |
49
|
0
|
0
|
0
|
|
|
|
return undef unless($obj && $obj->{rows}); |
50
|
0
|
|
|
|
|
|
return $obj->{rows}; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
## 调用 model 层的 sremove 方法 |
54
|
|
|
|
|
|
|
sub msremove{ |
55
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
56
|
0
|
|
|
|
|
|
my $table = $self->dmn; |
57
|
0
|
|
|
|
|
|
my $model = $self->model($table); |
58
|
0
|
|
|
|
|
|
my $obj = $model->sremove(@_); |
59
|
0
|
0
|
0
|
|
|
|
return undef unless($obj && $obj->{rows}); |
60
|
0
|
|
|
|
|
|
return $obj->{rows}; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
## 调用 model 层的 count 方法 |
65
|
|
|
|
|
|
|
sub mcount{ |
66
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
67
|
0
|
|
|
|
|
|
my $table = $self->dmn; |
68
|
0
|
|
|
|
|
|
my $model = $self->model($table); |
69
|
0
|
|
|
|
|
|
return $model->count(@_); |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub AUTOLOAD{ |
75
|
0
|
|
|
0
|
|
|
my $self = shift; |
76
|
0
|
|
|
|
|
|
my ($package, $method) = our $AUTOLOAD =~ /^(.+)::(.+)$/; |
77
|
2
|
|
|
2
|
|
13
|
no strict qw/refs/; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
2014
|
|
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
## 在哪个包里调用的这个方法 |
80
|
0
|
|
|
|
|
|
my $pkg = caller(0); |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
## 调用 model 层的create方法 |
83
|
0
|
0
|
|
|
|
|
if($method =~ /^mcreate_(.+)$/){ |
84
|
0
|
|
|
|
|
|
my $table = $1; |
85
|
0
|
|
|
|
|
|
my $model = $self->model($table); |
86
|
0
|
|
|
|
|
|
my $obj = $model->create(@_); |
87
|
0
|
0
|
0
|
|
|
|
return undef unless($obj && $obj->{object}); |
88
|
0
|
0
|
|
|
|
|
return $pkg->isa(__PACKAGE__) ? $obj : $obj->{object}; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
## 调用 model 层的edit方法 |
93
|
0
|
0
|
|
|
|
|
if($method =~ /^medit_(.+)$/){ |
94
|
0
|
|
|
|
|
|
my $table = $1; |
95
|
0
|
|
|
|
|
|
my $model = $self->model($table); |
96
|
0
|
|
|
|
|
|
my $obj = $model->edit(@_); |
97
|
0
|
0
|
0
|
|
|
|
return undef unless($obj && $obj->{rows}); |
98
|
0
|
0
|
|
|
|
|
return $pkg->isa(__PACKAGE__) ? $obj : $obj->{rows}; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
## 调用 model 层的remove方法 |
103
|
0
|
0
|
|
|
|
|
if($method =~ /^mremove_(.+)$/){ |
104
|
0
|
|
|
|
|
|
my $table = $1; |
105
|
0
|
|
|
|
|
|
my $model = $self->model($table); |
106
|
0
|
|
|
|
|
|
my $obj = $model->remove(@_); |
107
|
0
|
0
|
0
|
|
|
|
return undef unless($obj && $obj->{rows}); |
108
|
0
|
0
|
|
|
|
|
return $pkg->isa(__PACKAGE__) ? $obj : $obj->{rows}; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
## 调用 model 层的sremove方法 |
113
|
0
|
0
|
|
|
|
|
if($method =~ /^msremove_(.+)$/){ |
114
|
0
|
|
|
|
|
|
my $table = $1; |
115
|
0
|
|
|
|
|
|
my $model = $self->model($table); |
116
|
0
|
|
|
|
|
|
my $obj = $model->sremove(@_); |
117
|
0
|
0
|
0
|
|
|
|
return undef unless($obj && $obj->{rows}); |
118
|
0
|
0
|
|
|
|
|
return $pkg->isa(__PACKAGE__) ? $obj : $obj->{rows}; |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
## 调用 model 层的 count 方法 |
122
|
0
|
0
|
|
|
|
|
if($method =~ /^mcount_(.+)$/){ |
123
|
0
|
|
|
|
|
|
my $table = $1; |
124
|
0
|
|
|
|
|
|
my $model = $self->model($table); |
125
|
0
|
|
|
|
|
|
return $model->count(@_); |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
## get_by_字段名 |
129
|
0
|
0
|
|
|
|
|
if($method =~ /^get_by_(.+)$/){ |
130
|
0
|
|
|
|
|
|
my $table = $self->dmn; |
131
|
0
|
|
|
|
|
|
my $field = $1; |
132
|
0
|
|
|
|
|
|
my $mmethod = "get_by_" . $field; |
133
|
0
|
|
|
|
|
|
my $model = $self->model($table); |
134
|
0
|
0
|
|
|
|
|
cluck "the model [$table] if not found!" unless($model); |
135
|
|
|
|
|
|
|
|
136
|
0
|
|
|
|
|
|
my $obj = $model->$mmethod(@_); |
137
|
0
|
0
|
0
|
|
|
|
return undef unless($obj && $obj->{$model->name}); |
138
|
0
|
0
|
|
|
|
|
return $pkg->isa(__PACKAGE__) ? $obj : $field eq "id" ? $obj->{$model->name} : $obj->{list}; |
|
|
0
|
|
|
|
|
|
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
## get_表名_by_字段名 |
142
|
0
|
0
|
|
|
|
|
if($method =~ /^get_(.+)_by_(.+)$/){ |
143
|
0
|
|
|
|
|
|
my $table = $1; |
144
|
0
|
|
|
|
|
|
my $field = $2; |
145
|
0
|
|
|
|
|
|
my $mmethod = "get_by_" . $field; |
146
|
0
|
|
|
|
|
|
my $model = $self->model($table); |
147
|
0
|
0
|
|
|
|
|
cluck "the model [$table] if not found!" unless($model); |
148
|
|
|
|
|
|
|
|
149
|
0
|
|
|
|
|
|
my $obj = $model->$mmethod(@_); |
150
|
0
|
0
|
0
|
|
|
|
return undef unless($obj && $obj->{$model->name}); |
151
|
0
|
0
|
|
|
|
|
return $pkg->isa(__PACKAGE__) ? $obj : $field eq "id" ? $obj->{$model->name} : $obj->{list}; |
|
|
0
|
|
|
|
|
|
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
## remove_by_字段名 |
156
|
0
|
0
|
|
|
|
|
if($method =~ /^remove_by_(.+)$/){ |
157
|
0
|
|
|
|
|
|
my $table = $self->dmn; |
158
|
0
|
|
|
|
|
|
my $field = $1; |
159
|
0
|
|
|
|
|
|
my $mmethod = "remove_by_" . $field; |
160
|
0
|
|
|
|
|
|
my $model = $self->model($table); |
161
|
0
|
0
|
|
|
|
|
cluck "the model [$table] if not found!" unless($model); |
162
|
|
|
|
|
|
|
|
163
|
0
|
|
|
|
|
|
my $obj = $model->$mmethod(@_); |
164
|
0
|
0
|
0
|
|
|
|
return undef unless($obj && defined $obj->{rows}); |
165
|
0
|
0
|
|
|
|
|
return $pkg->isa(__PACKAGE__) ? $obj : $obj->{rows}; |
166
|
|
|
|
|
|
|
} |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
## remove_表名_by_字段名 |
169
|
0
|
0
|
|
|
|
|
if($method =~ /^remove_(.+)_by_(.+)$/){ |
170
|
0
|
|
|
|
|
|
my $table = $1; |
171
|
0
|
|
|
|
|
|
my $field = $2; |
172
|
0
|
|
|
|
|
|
my $mmethod = "remove_by_" . $field; |
173
|
0
|
|
|
|
|
|
my $model = $self->model($table); |
174
|
0
|
0
|
|
|
|
|
cluck "the model [$table] if not found!" unless($model); |
175
|
|
|
|
|
|
|
|
176
|
0
|
|
|
|
|
|
my $obj = $model->$mmethod(@_); |
177
|
0
|
0
|
0
|
|
|
|
return undef unless($obj && defined $obj->{rows}); |
178
|
0
|
0
|
|
|
|
|
return $pkg->isa(__PACKAGE__) ? $obj : $obj->{rows}; |
179
|
|
|
|
|
|
|
} |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
## sremove_表名_by_字段名 |
184
|
0
|
0
|
|
|
|
|
if($method =~ /^sremove_by_(.+)$/){ |
185
|
0
|
|
|
|
|
|
my $table = $self->dmn; |
186
|
0
|
|
|
|
|
|
my $field = $1; |
187
|
0
|
|
|
|
|
|
my $mmethod = "sremove_by_" . $field; |
188
|
0
|
|
|
|
|
|
my $model = $self->model($table); |
189
|
0
|
0
|
|
|
|
|
cluck "the model [$table] if not found!" unless($model); |
190
|
|
|
|
|
|
|
|
191
|
0
|
|
|
|
|
|
my $obj = $model->$mmethod(@_); |
192
|
0
|
0
|
0
|
|
|
|
return undef unless($obj && defined $obj->{rows}); |
193
|
0
|
0
|
|
|
|
|
return $pkg->isa(__PACKAGE__) ? $obj : $obj->{rows}; |
194
|
|
|
|
|
|
|
} |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
## sremove_表名_by_字段名 |
197
|
0
|
0
|
|
|
|
|
if($method =~ /^sremove_(.+)_by_(.+)$/){ |
198
|
0
|
|
|
|
|
|
my $table = $1; |
199
|
0
|
|
|
|
|
|
my $field = $2; |
200
|
0
|
|
|
|
|
|
my $mmethod = "sremove_by_" . $field; |
201
|
0
|
|
|
|
|
|
my $model = $self->model($table); |
202
|
0
|
0
|
|
|
|
|
cluck "the model [$table] if not found!" unless($model); |
203
|
|
|
|
|
|
|
|
204
|
0
|
|
|
|
|
|
my $obj = $model->$mmethod(@_); |
205
|
0
|
0
|
0
|
|
|
|
return undef unless($obj && defined $obj->{rows}); |
206
|
0
|
0
|
|
|
|
|
return $pkg->isa(__PACKAGE__) ? $obj : $obj->{rows}; |
207
|
|
|
|
|
|
|
} |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
## count_表名_by_字段名 |
211
|
0
|
0
|
|
|
|
|
if($method =~ /^count_by_(.+)$/){ |
212
|
0
|
|
|
|
|
|
my $table = $self->dmn; |
213
|
0
|
|
|
|
|
|
my $field = $2; |
214
|
0
|
|
|
|
|
|
my $mmethod = "count_by_" . $field; |
215
|
0
|
|
|
|
|
|
my $model = $self->model($table); |
216
|
0
|
0
|
|
|
|
|
cluck "the model [$table] if not found!" unless($model); |
217
|
|
|
|
|
|
|
|
218
|
0
|
|
|
|
|
|
return $model->$mmethod(@_); |
219
|
|
|
|
|
|
|
} |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
## count_表名_by_字段名 |
223
|
0
|
0
|
|
|
|
|
if($method =~ /^count_(.+)_by_(.+)$/){ |
224
|
0
|
|
|
|
|
|
my $table = $1; |
225
|
0
|
|
|
|
|
|
my $field = $2; |
226
|
0
|
|
|
|
|
|
my $mmethod = "count_by_" . $field; |
227
|
0
|
|
|
|
|
|
my $model = $self->model($table); |
228
|
0
|
0
|
|
|
|
|
cluck "the model [$table] if not found!" unless($model); |
229
|
|
|
|
|
|
|
|
230
|
0
|
|
|
|
|
|
return $model->$mmethod(@_); |
231
|
|
|
|
|
|
|
} |
232
|
|
|
|
|
|
|
|
233
|
0
|
|
|
|
|
|
confess qq{Can't locate object method "$method" via package "$package"} |
234
|
|
|
|
|
|
|
} |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
=encoding utf8 |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
=head1 NAME |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
Mojolicious::Service - Mojolicious框架中所有Service的基类(具体的Service需要用户实现)! |
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
=head1 SYNOPSIS |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
use Mojolicious::Service |
248
|
|
|
|
|
|
|
my $service = Mojolicious::Service->new({ |
249
|
|
|
|
|
|
|
dbi=>DBIx::Custom->new(), |
250
|
|
|
|
|
|
|
models=>{} |
251
|
|
|
|
|
|
|
}); |
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
my $user->some_mothed(arg1,arg2,……); |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
=head1 DESCRIPTION |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
Mojolicious框架中所有Service的基类(具体的Service需要用户实现)! |
259
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
261
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
=head2 dbi |
263
|
|
|
|
|
|
|
|
264
|
|
|
|
|
|
|
dbi 是为service提供数据库操作接口的对象。 |
265
|
|
|
|
|
|
|
|
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
=head2 models |
268
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
models 是为service提供数据模型操作接口的对象。 |
270
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
|
272
|
|
|
|
|
|
|
=head2 app |
273
|
|
|
|
|
|
|
|
274
|
|
|
|
|
|
|
当前应用程序的引用,通常是Mojolicious对象。 |
275
|
|
|
|
|
|
|
|
276
|
|
|
|
|
|
|
=head2 c |
277
|
|
|
|
|
|
|
|
278
|
|
|
|
|
|
|
当前控制器的引用,通常是Mojolicious::Controller子类的对象。 |
279
|
|
|
|
|
|
|
|
280
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
=head1 METHODS |
282
|
|
|
|
|
|
|
|
283
|
|
|
|
|
|
|
=head2 model |
284
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
根据model的名称从 models 属性中获取model。 |
286
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
|
288
|
|
|
|
|
|
|
=head1 AUTHOR |
289
|
|
|
|
|
|
|
|
290
|
|
|
|
|
|
|
wfso, C<< <461663376@qq.com> >> |
291
|
|
|
|
|
|
|
|
292
|
|
|
|
|
|
|
=head1 BUGS |
293
|
|
|
|
|
|
|
|
294
|
|
|
|
|
|
|
Please report any bugs or feature requests to C, or through |
295
|
|
|
|
|
|
|
the web interface at L. I will be notified, and then you'll |
296
|
|
|
|
|
|
|
automatically be notified of progress on your bug as I make changes. |
297
|
|
|
|
|
|
|
|
298
|
|
|
|
|
|
|
|
299
|
|
|
|
|
|
|
|
300
|
|
|
|
|
|
|
|
301
|
|
|
|
|
|
|
=head1 SUPPORT |
302
|
|
|
|
|
|
|
|
303
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
304
|
|
|
|
|
|
|
|
305
|
|
|
|
|
|
|
perldoc Mojolicious::Service |
306
|
|
|
|
|
|
|
|
307
|
|
|
|
|
|
|
|
308
|
|
|
|
|
|
|
You can also look for information at: |
309
|
|
|
|
|
|
|
|
310
|
|
|
|
|
|
|
=over 4 |
311
|
|
|
|
|
|
|
|
312
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker (report bugs here) |
313
|
|
|
|
|
|
|
|
314
|
|
|
|
|
|
|
L |
315
|
|
|
|
|
|
|
|
316
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
317
|
|
|
|
|
|
|
|
318
|
|
|
|
|
|
|
L |
319
|
|
|
|
|
|
|
|
320
|
|
|
|
|
|
|
=item * CPAN Ratings |
321
|
|
|
|
|
|
|
|
322
|
|
|
|
|
|
|
L |
323
|
|
|
|
|
|
|
|
324
|
|
|
|
|
|
|
=item * Search CPAN |
325
|
|
|
|
|
|
|
|
326
|
|
|
|
|
|
|
L |
327
|
|
|
|
|
|
|
|
328
|
|
|
|
|
|
|
=back |
329
|
|
|
|
|
|
|
|
330
|
|
|
|
|
|
|
|
331
|
|
|
|
|
|
|
=cut |
332
|
|
|
|
|
|
|
|
333
|
|
|
|
|
|
|
1; # End of Mojolicious::Service |