line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::DBIxCustom; |
2
|
1
|
|
|
1
|
|
53250
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
1
|
|
|
|
|
108379
|
|
|
1
|
|
|
|
|
7
|
|
3
|
1
|
|
|
1
|
|
1340
|
use Mojo::Loader qw/load_class/; |
|
1
|
|
|
|
|
22142
|
|
|
1
|
|
|
|
|
322
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION = '1.2.1'; |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
sub register{ |
9
|
0
|
|
|
0
|
1
|
|
my ($self, $app, $conf) = @_; |
10
|
0
|
0
|
|
|
|
|
$conf = {%{$conf}, %{$app->config->{dbi_config}}} if($app->config->{dbi_config}); |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
11
|
0
|
|
0
|
|
|
|
my $dbi_class = delete $conf->{dbi_class} || 'Mojolicious::DBIxCustom'; |
12
|
0
|
0
|
|
|
|
|
my $model_namespace = delete $conf->{model_namespace} if($conf->{model_namespace}); |
13
|
0
|
0
|
|
|
|
|
my $cb = delete $conf->{cb} if($conf->{cb}); |
14
|
0
|
|
|
|
|
|
my $dbi; |
15
|
0
|
|
|
|
|
|
my $e = load_class($dbi_class); |
16
|
0
|
0
|
|
|
|
|
if($e){ |
|
|
0
|
|
|
|
|
|
17
|
0
|
0
|
|
|
|
|
ref $e ? die $e : die "can't fond the module named '$dbi_class' ,inspect your installed please"; |
18
|
0
|
|
|
|
|
|
return undef; |
19
|
|
|
|
|
|
|
}elsif($dbi_class->isa("DBIx::Custom")){ |
20
|
0
|
|
|
|
|
|
$dbi = $dbi_class->new($conf); |
21
|
0
|
0
|
|
|
|
|
$dbi->include_model($model_namespace) if($model_namespace); |
22
|
0
|
0
|
|
|
|
|
if($cb){ |
23
|
0
|
|
|
|
|
|
$cb->($dbi); |
24
|
|
|
|
|
|
|
} |
25
|
0
|
|
|
0
|
|
|
$app->helper(dbi => sub{state $dbi_connected = $dbi->connect}); |
|
0
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
$app->helper(model => sub{ |
27
|
0
|
|
|
0
|
|
|
my ($c, $model_name) = @_; |
28
|
0
|
|
|
|
|
|
$c->dbi->model($model_name); |
29
|
|
|
|
|
|
|
} |
30
|
0
|
|
|
|
|
|
); |
31
|
|
|
|
|
|
|
}else{ |
32
|
0
|
|
|
|
|
|
$app->log->fatal("dbi_class named '$dbi_class' is not a subclass for DBIx::Custom "); |
33
|
0
|
|
|
|
|
|
die "dbi_class named '$dbi_class' is not a subclass for DBIx::Custom"; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=encoding utf8 |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
=head1 NAME |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
Mojolicious::Plugin::DBIxCustom - 链接DBIx::Custom到Mojolicious的插件 |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
=head1 SYNOPSIS |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
# Mojolicious |
49
|
|
|
|
|
|
|
$self->plugin('DBIxCustom',{ |
50
|
|
|
|
|
|
|
dsn=>"DBI:SQLite:dbname=:memory:", |
51
|
|
|
|
|
|
|
connector=>1,## 默认使用DBIx::Connector |
52
|
|
|
|
|
|
|
model_namespace=>"T::Model", |
53
|
|
|
|
|
|
|
dbi_class=>"T::MyDBIxCustom" |
54
|
|
|
|
|
|
|
}); |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
# Mojolicious::Lite |
57
|
|
|
|
|
|
|
plugin 'DBIxCustom',{ |
58
|
|
|
|
|
|
|
dsn=>"DBI:SQLite:dbname=:memory:", |
59
|
|
|
|
|
|
|
connector=>1,## 默认使用DBIx::Connector |
60
|
|
|
|
|
|
|
model_namespace=>"T::Model", |
61
|
|
|
|
|
|
|
dbi_class=>"T::MyDBIxCustom" |
62
|
|
|
|
|
|
|
}; |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
=head1 METHODS |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
Mojolicious::Plugin::DBIxCustom 覆盖了Mojolicious::Plugin中的register方法。 |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
=head2 register |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
向Mojolicious中添加了两具helper:dbi和model。 |
72
|
|
|
|
|
|
|
其中dbi默认是一个DBIx::Custom对象,你也可以通过指定dbi_class来指定一个DBIx::Custom的子类,用来实例化为dbi。 |
73
|
|
|
|
|
|
|
名为model的helper是dbi中model方法的别名。 |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
=head1 OPTION |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
这里的OPTION介绍的是register方法的参数支持的OPTION。 |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=head2 DBIx::Custom初始化参数 |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
所有用于DBIx::Custom初始化参数都可以通过register的option来指定。 |
82
|
|
|
|
|
|
|
如:dsn、user、password、connector、option等。 |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=head2 dbi_class |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
dbi_class 默认为DBIx::Custom你也可以自己指定,但必须为DBIx::Custom的子类,它会被实例化得到dbi对象。 |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=head2 model_namespace |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
model_namespace是用于作为参数来调用dbi_class的include_model方法的。调用此方法时会自动加载对应命名空间下的所有model。 |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
$dbi->include_model($model_namespace); |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
如果没有给出此参数,则不会调用include_model方法。 |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=head1 AUTHOR |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
wfso, C<< <461663376 at qq.com> >> |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=head1 BUGS |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
Please report any bugs or feature requests to C, or through |
105
|
|
|
|
|
|
|
the web interface at L. I will be notified, and then you'll |
106
|
|
|
|
|
|
|
automatically be notified of progress on your bug as I make changes. |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=head1 SUPPORT |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
perldoc Mojolicious::Plugin::DBIxCustom |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
You can also look for information at: |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=over 4 |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker (report bugs here) |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
L |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
L |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=item * CPAN Ratings |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
L |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=item * Search CPAN |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
L |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=back |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=cut |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
1; # End of Mojolicious::Plugin::DBIxCustom |