line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Dancer2::Plugin::DBIC; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
our $VERSION = '0.0013'; # VERSION |
4
|
|
|
|
|
|
|
|
5
|
4
|
|
|
4
|
|
1076396
|
use strict; |
|
4
|
|
|
|
|
6
|
|
|
4
|
|
|
|
|
96
|
|
6
|
4
|
|
|
4
|
|
14
|
use warnings; |
|
4
|
|
|
|
|
4
|
|
|
4
|
|
|
|
|
73
|
|
7
|
4
|
|
|
4
|
|
11
|
use utf8; |
|
4
|
|
|
|
|
4
|
|
|
4
|
|
|
|
|
19
|
|
8
|
4
|
|
|
4
|
|
1631
|
use Dancer2::Plugin; |
|
4
|
|
|
|
|
6645
|
|
|
4
|
|
|
|
|
21
|
|
9
|
4
|
|
|
4
|
|
6979
|
use DBICx::Sugar; |
|
4
|
|
|
|
|
35733
|
|
|
4
|
|
|
|
|
667
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub _schema { |
12
|
15
|
|
|
15
|
|
2090727
|
my ($dsl, $name) = @_; |
13
|
15
|
|
|
|
|
24
|
my $config; |
14
|
|
|
|
|
|
|
# ugly switch needed to support plugin2 plugins which use this plugin |
15
|
|
|
|
|
|
|
# whilst still working for plugin1 |
16
|
15
|
50
|
|
|
|
135
|
if ( $dsl->app->can('with_plugin') ) { |
17
|
0
|
|
|
|
|
0
|
$config = $dsl->config; |
18
|
|
|
|
|
|
|
} |
19
|
|
|
|
|
|
|
else { |
20
|
15
|
|
|
|
|
54
|
$config = plugin_setting; |
21
|
|
|
|
|
|
|
} |
22
|
15
|
|
|
|
|
1666
|
DBICx::Sugar::config( $config ); |
23
|
15
|
|
|
|
|
124
|
return DBICx::Sugar::schema($name); |
24
|
|
|
|
|
|
|
}; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub _rset { |
27
|
2
|
|
|
2
|
|
5107
|
my ($dsl, $rset_name) = @_; |
28
|
2
|
|
|
|
|
6
|
return schema($dsl)->resultset($rset_name); |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
register schema => \&_schema; |
32
|
|
|
|
|
|
|
register resultset => \&_rset; |
33
|
|
|
|
|
|
|
register rset => \&_rset; |
34
|
|
|
|
|
|
|
register_plugin; |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
# ABSTRACT: DBIx::Class interface for Dancer2 applications |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
1; |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
__END__ |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=pod |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
=encoding UTF-8 |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=head1 NAME |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
Dancer2::Plugin::DBIC - DBIx::Class interface for Dancer2 applications |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=head1 VERSION |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
version 0.0013 |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=head1 SYNOPSIS |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
use Dancer2; |
58
|
|
|
|
|
|
|
use Dancer2::Plugin::DBIC qw(schema resultset rset); |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
get '/users/:user_id' => sub { |
61
|
|
|
|
|
|
|
my $user = schema('default')->resultset('User')->find(param 'user_id'); |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
# If you are accessing the 'default' schema, then all the following |
64
|
|
|
|
|
|
|
# are equivalent to the above: |
65
|
|
|
|
|
|
|
$user = schema->resultset('User')->find(param 'user_id'); |
66
|
|
|
|
|
|
|
$user = resultset('User')->find(param 'user_id'); |
67
|
|
|
|
|
|
|
$user = rset('User')->find(param 'user_id'); |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
template user_profile => { |
70
|
|
|
|
|
|
|
user => $user |
71
|
|
|
|
|
|
|
}; |
72
|
|
|
|
|
|
|
}; |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
dance; |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=head1 DESCRIPTION |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
This plugin makes it very easy to create L<Dancer2> applications that interface |
79
|
|
|
|
|
|
|
with databases. |
80
|
|
|
|
|
|
|
It automatically exports the keyword C<schema> which returns a |
81
|
|
|
|
|
|
|
L<DBIx::Class::Schema> object. |
82
|
|
|
|
|
|
|
You just need to configure your database connection information. |
83
|
|
|
|
|
|
|
For performance, schema objects are cached in memory |
84
|
|
|
|
|
|
|
and are lazy loaded the first time they are accessed. |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
This plugin is a thin wrapper around L<DBICx::Sugar>. |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=head1 CONFIGURATION |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
Configuration can be done in your L<Dancer2> config file. |
91
|
|
|
|
|
|
|
This is a minimal example. It defines one database named C<default>: |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
plugins: |
94
|
|
|
|
|
|
|
DBIC: |
95
|
|
|
|
|
|
|
default: |
96
|
|
|
|
|
|
|
dsn: dbi:SQLite:dbname=some.db |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
In this example, there are 2 databases configured named C<default> and C<foo>: |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
plugins: |
101
|
|
|
|
|
|
|
DBIC: |
102
|
|
|
|
|
|
|
default: |
103
|
|
|
|
|
|
|
dsn: dbi:SQLite:dbname=some.db |
104
|
|
|
|
|
|
|
schema_class: MyApp::Schema |
105
|
|
|
|
|
|
|
foo: |
106
|
|
|
|
|
|
|
dsn: dbi:mysql:foo |
107
|
|
|
|
|
|
|
schema_class: Foo::Schema |
108
|
|
|
|
|
|
|
user: bob |
109
|
|
|
|
|
|
|
password: secret |
110
|
|
|
|
|
|
|
options: |
111
|
|
|
|
|
|
|
RaiseError: 1 |
112
|
|
|
|
|
|
|
PrintError: 1 |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
Each database configured must at least have a dsn option. |
115
|
|
|
|
|
|
|
The dsn option should be the L<DBI> driver connection string. |
116
|
|
|
|
|
|
|
All other options are optional. |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
If you only have one schema configured, or one of them is named |
119
|
|
|
|
|
|
|
C<default>, you can call C<schema> without an argument to get the only |
120
|
|
|
|
|
|
|
or C<default> schema, respectively. |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
If a schema_class option is not provided, then L<DBIx::Class::Schema::Loader> |
123
|
|
|
|
|
|
|
will be used to dynamically load the schema by introspecting the database |
124
|
|
|
|
|
|
|
corresponding to the dsn value. |
125
|
|
|
|
|
|
|
Remember that you need L<DBIx::Class::Schema::Loader> installed to take |
126
|
|
|
|
|
|
|
advantage of that. |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
The schema_class option, should be a proper Perl package name that |
129
|
|
|
|
|
|
|
Dancer2::Plugin::DBIC will use as a L<DBIx::Class::Schema> class. |
130
|
|
|
|
|
|
|
Optionally, a database configuration may have user, password, and options |
131
|
|
|
|
|
|
|
parameters as described in the documentation for C<connect()> in L<DBI>. |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
You may also declare your connection information in the following format |
134
|
|
|
|
|
|
|
(which may look more familiar to DBIC users): |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
plugins: |
137
|
|
|
|
|
|
|
DBIC: |
138
|
|
|
|
|
|
|
default: |
139
|
|
|
|
|
|
|
connect_info: |
140
|
|
|
|
|
|
|
- dbi:mysql:foo |
141
|
|
|
|
|
|
|
- bob |
142
|
|
|
|
|
|
|
- secret |
143
|
|
|
|
|
|
|
- |
144
|
|
|
|
|
|
|
RaiseError: 1 |
145
|
|
|
|
|
|
|
PrintError: 1 |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
=head1 FUNCTIONS |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=head2 schema |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
my $user = schema->resultset('User')->find('bob'); |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
The C<schema> keyword returns a L<DBIx::Class::Schema> object ready for you to |
154
|
|
|
|
|
|
|
use. |
155
|
|
|
|
|
|
|
If you have configured only one database, then you can simply call C<schema> |
156
|
|
|
|
|
|
|
with no arguments. |
157
|
|
|
|
|
|
|
If you have configured multiple databases, |
158
|
|
|
|
|
|
|
you can still call C<schema> with no arguments if there is a database |
159
|
|
|
|
|
|
|
named C<default> in the configuration. |
160
|
|
|
|
|
|
|
With no argument, the C<default> schema is returned. |
161
|
|
|
|
|
|
|
Otherwise, you B<must> provide C<schema()> with the name of the database: |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
my $user = schema('foo')->resultset('User')->find('bob'); |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
=head2 resultset |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
This is a convenience method that will save you some typing. |
168
|
|
|
|
|
|
|
Use this B<only> when accessing the C<default> schema. |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
my $user = resultset('User')->find('bob'); |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
is equivalent to: |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
my $user = schema->resultset('User')->find('bob'); |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=head2 rset |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
my $user = rset('User')->find('bob'); |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
This is simply an alias for C<resultset>. |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=head1 SCHEMA GENERATION |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
There are two approaches for generating schema classes. |
185
|
|
|
|
|
|
|
You may generate your own L<DBIx::Class> classes and set |
186
|
|
|
|
|
|
|
the corresponding C<schema_class> setting in your configuration as shown above. |
187
|
|
|
|
|
|
|
This is the recommended approach for performance and stability. |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
It is also possible to have schema classes dynamically generated |
190
|
|
|
|
|
|
|
if you omit the C<schema_class> configuration setting. |
191
|
|
|
|
|
|
|
This requires you to have L<DBIx::Class::Schema::Loader> installed. |
192
|
|
|
|
|
|
|
The C<v7> naming scheme will be used for naming the auto generated classes. |
193
|
|
|
|
|
|
|
See L<DBIx::Class::Schema::Loader::Base/naming> for more information about |
194
|
|
|
|
|
|
|
naming. |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
For generating your own schema classes, |
197
|
|
|
|
|
|
|
you can use the L<dbicdump> command line tool provided by |
198
|
|
|
|
|
|
|
L<DBIx::Class::Schema::Loader> to help you. |
199
|
|
|
|
|
|
|
For example, if your app were named Foo, then you could run the following |
200
|
|
|
|
|
|
|
from the root of your project directory: |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
dbicdump -o dump_directory=./lib Foo::Schema dbi:SQLite:/path/to/foo.db |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
For that example, your C<schema_class> setting would be C<Foo::Schema>. |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
=head1 SEE ALSO |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
=over 4 |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
=item * |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
L<DBICx::Sugar> |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
=back |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
=head1 CONTRIBUTORS |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
=over 4 |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
=item * |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
Alexis Sukrieh <sukria@sukria.net> |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
=item * |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
Dagfinn Ilmari Mannsåker <L<https://github.com/ilmari>> |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
=item * |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
David Precious <davidp@preshweb.co.uk> |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
=item * |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
ennio <L<https://github.com/scriplit>> |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
=item * |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
Fabrice Gabolde <L<https://github.com/fgabolde>> |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
=item * |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
Franck Cuny <franck@lumberjaph.net> |
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
=item * |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
Steven Humphrey <L<https://github.com/shumphrey>> |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
=item * |
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
Yanick Champoux <L<https://github.com/yanick>> |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
=back |
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
=head1 AUTHOR |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
Naveed Massjouni <naveed@vt.edu> |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
259
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
This software is copyright (c) 2013 by Naveed Massjouni. |
261
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
263
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
=cut |