line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::Model::DB; |
2
|
2
|
|
|
2
|
|
1477
|
use Mojo::Base 'Mojolicious::Plugin::Model'; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
16
|
|
3
|
2
|
|
|
2
|
|
4469
|
use Moo; |
|
2
|
|
|
|
|
21146
|
|
|
2
|
|
|
|
|
10
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
after register => sub { |
8
|
|
|
|
|
|
|
my ($plugin, $app, $conf) = @_; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
$app->helper( |
11
|
|
|
|
|
|
|
db => sub { |
12
|
|
|
|
|
|
|
my ($self, $name) = @_; |
13
|
|
|
|
|
|
|
$name //= $conf->{default}; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
my $model; |
16
|
|
|
|
|
|
|
return $model if $model = $plugin->{models}{$name}; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
my $class = _load_class_for_name($plugin, $app, $conf, $name) |
19
|
|
|
|
|
|
|
or return undef; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
my $params = $conf->{params}{$name}; |
22
|
|
|
|
|
|
|
$model = $class->new(ref $params eq 'HASH' ? %$params : (), app => $app); |
23
|
|
|
|
|
|
|
$plugin->{models}{$name} = $model; |
24
|
|
|
|
|
|
|
return $model; |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
); |
27
|
|
|
|
|
|
|
}; |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub _load_class_for_name { |
30
|
2
|
|
|
2
|
|
5
|
my ($plugin, $app, $conf, $name) = @_; |
31
|
2
|
50
|
|
|
|
15
|
return $plugin->{classes_loaded}{$name} if $plugin->{classes_loaded}{$name}; |
32
|
|
|
|
|
|
|
|
33
|
2
|
|
100
|
|
|
12
|
my $namespace = $conf->{namespace} // 'DB'; |
34
|
2
|
|
50
|
|
|
6
|
my $ns = $conf->{namespaces} // [Mojolicious::Plugin::Model::camelize($app->moniker) . '::Model']; |
35
|
2
|
|
50
|
|
|
12
|
my $base = $conf->{base_classes} // [qw(MojoX::Model)]; |
36
|
|
|
|
|
|
|
|
37
|
2
|
50
|
|
|
|
20
|
$name = Mojolicious::Plugin::Model::camelize($name) if $name =~ /^[a-z]/; |
38
|
|
|
|
|
|
|
|
39
|
2
|
|
|
|
|
47
|
for my $class ( map "${_}::$namespace\::$name", @$ns ) { |
40
|
2
|
50
|
|
|
|
15
|
next unless Mojolicious::Plugin::Model::_load_class($class); |
41
|
|
|
|
|
|
|
|
42
|
2
|
50
|
|
2
|
|
132
|
unless ( Mojolicious::Plugin::Model::any { $class->isa($_) } @$base ) { |
|
2
|
|
|
|
|
20
|
|
43
|
0
|
|
|
|
|
0
|
$app->log->debug(qq[Class "$class" is not a model db]); |
44
|
0
|
|
|
|
|
0
|
next; |
45
|
|
|
|
|
|
|
} |
46
|
2
|
|
|
|
|
60
|
$plugin->{classes_loaded}{$name} = $class; |
47
|
2
|
|
|
|
|
32
|
return $class; |
48
|
|
|
|
|
|
|
} |
49
|
0
|
|
|
|
|
|
$app->log->debug(qq[Model db "$name" does not exist]); |
50
|
0
|
|
|
|
|
|
return undef; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
1; |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=encoding utf8 |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
=head1 NAME |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
Mojolicious::Plugin::Model::DB - It is an extension of the module L for Mojolicious applications. |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
=head1 SYNOPSIS |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
Model DB Users |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
package MyApp::Model::DB::Person; |
66
|
|
|
|
|
|
|
use Mojo::Base 'MojoX::Model'; |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
sub save { |
69
|
|
|
|
|
|
|
my ($self, $foo) = @_; |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
$mysql->db->insert( |
72
|
|
|
|
|
|
|
'foo', |
73
|
|
|
|
|
|
|
{ |
74
|
|
|
|
|
|
|
foo => $foo |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
); |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
1; |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
Mojolicious::Lite application |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
#!/usr/bin/env perl |
84
|
|
|
|
|
|
|
use Mojolicious::Lite; |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
use lib 'lib'; |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
plugin 'Model::DB'; |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
any '/' => sub { |
91
|
|
|
|
|
|
|
my $c = shift; |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
my $foo = $c->param('foo') || ''; |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
# model db |
96
|
|
|
|
|
|
|
$c->db('person')->save($foo); |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
$c->render(text => 'Save person foo'); |
99
|
|
|
|
|
|
|
}; |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
app->start; |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
All available options |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
#!/usr/bin/env perl |
106
|
|
|
|
|
|
|
use Mojolicious::Lite; |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
plugin 'Model::DB' => { |
109
|
|
|
|
|
|
|
# Mojolicious::Plugin::Model::DB |
110
|
|
|
|
|
|
|
namespace => 'DataBase', # default is DB |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
# Mojolicious::Plugin::Model |
113
|
|
|
|
|
|
|
namespaces => ['MyApp::Model', 'MyApp::CLI::Model'], |
114
|
|
|
|
|
|
|
base_classes => ['MyApp::Model'], |
115
|
|
|
|
|
|
|
default => 'MyApp::Model::Pg', |
116
|
|
|
|
|
|
|
params => {Pg => {uri => 'postgresql://user@/mydb'}} |
117
|
|
|
|
|
|
|
}; |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=head1 DESCRIPTION |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
Mojolicious::Plugin::Model::DB It is an extension of the module Mojolicious::Plugin::Model, the intention is to separate models of database from other models. See more in L |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
=head1 OPTIONS |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
=head2 namespace |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
# Mojolicious::Lite |
128
|
|
|
|
|
|
|
plugin 'Model::DB' => {namespace => 'DataBase'}; # It's will load from $moniker::Model::DataBase |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
Namespace to load models from, defaults to C<$moniker::Model::DB>. |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=head2 more options |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
see in L |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=head1 HELPERS |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
L implements the following helpers. |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=head2 db |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
my $db = $c->db($name); |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
Load, create and cache a model object with given name. Default class for |
145
|
|
|
|
|
|
|
model db C. Return `undef` if model db not found. |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
=head2 more helpers |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
see in L |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=head1 SEE ALSO |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
L, L, L, L. |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=head1 AUTHOR |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
Lucas Tiago de Moraes C |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
This software is copyright (c) 2019 by Lucas Tiago de Moraes. |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
=cut |