blib/lib/Dancer/Plugin/DBIC.pm

filter: Statementstmt 100% 29 / 29 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod n/a totaltotal 100% 37 / 37 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
↑ Summary
1 package Dancer::Plugin::DBIC;
2  
3 7 our $VERSION = '0.2103'; # VERSION
4  
5 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use Dancer::Plugin;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use DBICx::Sugar;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use Module::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub _schema {
13 32 ▶ my ($name) = @_;
Subroutine _schema: called CC=1 SCAR=0.0
14 32 DBICx::Sugar::config( plugin_setting );
15 32 return DBICx::Sugar::schema($name);
16 };
17  
18 sub _rset {
19 14 ▶ my ($rset_name) = @_;
Subroutine _rset: called CC=1 SCAR=0.0
20 14 return _schema()->resultset($rset_name);
21 }
22  
23 7 register schema => \&_schema;
24 7 register resultset => \&_rset;
25 7 register rset => \&_rset;
26 7 register_plugin;
27  
28 # ABSTRACT: DBIx::Class interface for Dancer applications
29  
30  
31 7 1;
32  
33 __END__
34  
35 =pod
36  
37 =encoding UTF-8
38  
39 =head1 NAME
40  
41 Dancer::Plugin::DBIC - DBIx::Class interface for Dancer applications
42  
43 =head1 VERSION
44  
45 version 0.2103
46  
47 =head1 SYNOPSIS
48  
49 use Dancer;
50 use Dancer::Plugin::DBIC qw(schema resultset rset);
51  
52 get '/users/:user_id' => sub {
53 my $user_id = param 'user_id';
54 my $user;
55  
56 # all of the following are equivalent:
57 $user = schema('default')->resultset('User')->find($user_id);
58 $user = schema->resultset('User')->find($user_id);
59 $user = resultset('User')->find($user_id);
60 $user = rset('User')->find($user_id);
61  
62 template user_profile => {
63 user => $user
64 };
65 };
66  
67 dance;
68  
69 =head1 DESCRIPTION
70  
71 This plugin makes it very easy to create L<Dancer> applications that interface
72 with databases.
73 It automatically exports the keyword C<schema> which returns a
74 L<DBIx::Class::Schema> object.
75 You just need to configure your database connection information.
76 For performance, schema objects are cached in memory
77 and are lazy loaded the first time they are accessed.
78  
79 This plugin is now just a thin wrapper around L<DBICx::Sugar>.
80  
81 =head1 CONFIGURATION
82  
83 Configuration can be done in your L<Dancer> config file.
84  
85 =head2 simple example
86  
87 Here is a simple example. It defines one database named C<default>:
88  
89 plugins:
90 DBIC:
91 default:
92 dsn: dbi:SQLite:dbname=myapp.db
93 schema_class: MyApp::Schema
94  
95 =head2 multiple schemas
96  
97 In this example, there are 2 databases configured named C<default> and C<foo>:
98  
99 plugins:
100 DBIC:
101 default:
102 dsn: dbi:SQLite:dbname=myapp.db
103 schema_class: MyApp::Schema
104 foo:
105 dsn: dbi:Pg:dbname=foo
106 schema_class: Foo::Schema
107 user: bob
108 password: secret
109 options:
110 RaiseError: 1
111 PrintError: 1
112  
113 Each database configured must at least have a dsn option.
114 The dsn option should be the L<DBI> driver connection string.
115 All other options are optional.
116  
117 If you only have one schema configured, or one of them is named
118 C<default>, you can call C<schema> without an argument to get the only
119 or C<default> schema, respectively.
120  
121 If a schema_class option is not provided, then L<DBIx::Class::Schema::Loader>
122 will be used to dynamically load the schema by introspecting the database
123 corresponding to the dsn value.
124 You need L<DBIx::Class::Schema::Loader> installed for this to work.
125  
126 WARNING: Dynamic loading is not recommended for production environments.
127 It is almost always better to provide a schema_class option.
128  
129 The schema_class option should be the name of your L<DBIx::Class::Schema> class.
130 See L</"SCHEMA GENERATION">
131 Optionally, a database configuration may have user, password, and options
132 parameters as described in the documentation for C<connect()> in L<DBI>.
133  
134 =head2 connect_info
135  
136 Alternatively, you may also declare your connection information inside an
137 array named C<connect_info>:
138  
139 plugins:
140 DBIC:
141 default:
142 schema_class: MyApp::Schema
143 connect_info:
144 - dbi:Pg:dbname=foo
145 - bob
146 - secret
147 -
148 RaiseError: 1
149 PrintError: 1
150  
151 =head2 replicated
152  
153 You can also add database read slaves to your configuration with the
154 C<replicated> config option.
155 This will automatically make your read queries go to a slave and your write
156 queries go to the master.
157 Keep in mind that this will require additional dependencies:
158 L<DBIx::Class::Optional::Dependencies#Storage::Replicated>
159 See L<DBIx::Class::Storage::DBI::Replicated> for more details.
160 Here is an example configuration that adds two read slaves:
161  
162 plugins:
163 DBIC:
164 default:
165 schema_class: MyApp::Schema
166 dsn: dbi:Pg:dbname=master
167 replicated:
168 balancer_type: ::Random # optional
169 balancer_args: # optional
170 auto_validate_every: 5 # optional
171 master_read_weight:1 # optional
172 # pool_type and pool_args are also allowed and are also optional
173 replicants:
174 -
175 - dbi:Pg:dbname=slave1
176 - user1
177 - password1
178 -
179 quote_names: 1
180 pg_enable_utf8: 1
181 -
182 - dbi:Pg:dbname=slave2
183 - user2
184 - password2
185 -
186 quote_names: 1
187 pg_enable_utf8: 1
188  
189 =head2 alias
190  
191 Schema aliases allow you to reference the same underlying database by multiple
192 names.
193 For example:
194  
195 plugins:
196 DBIC:
197 default:
198 dsn: dbi:Pg:dbname=master
199 schema_class: MyApp::Schema
200 slave1:
201 alias: default
202  
203 Now you can access the default schema with C<schema()>, C<schema('default')>,
204 or C<schema('slave1')>.
205 This can come in handy if, for example, you have master/slave replication in
206 your production environment but only a single database in your development
207 environment.
208 You can continue to reference C<schema('slave1')> in your code in both
209 environments by simply creating a schema alias in your development.yml config
210 file, as shown above.
211  
212 =head1 FUNCTIONS
213  
214 =head2 schema
215  
216 my $user = schema->resultset('User')->find('bob');
217  
218 The C<schema> keyword returns a L<DBIx::Class::Schema> object ready for you to
219 use.
220 If you have configured only one database, then you can simply call C<schema>
221 with no arguments.
222 If you have configured multiple databases,
223 you can still call C<schema> with no arguments if there is a database
224 named C<default> in the configuration.
225 With no argument, the C<default> schema is returned.
226 Otherwise, you B<must> provide C<schema()> with the name of the database:
227  
228 my $user = schema('foo')->resultset('User')->find('bob');
229  
230 =head2 resultset
231  
232 This is a convenience method that will save you some typing.
233 Use this B<only> when accessing the C<default> schema.
234  
235 my $user = resultset('User')->find('bob');
236  
237 is equivalent to:
238  
239 my $user = schema->resultset('User')->find('bob');
240  
241 =head2 rset
242  
243 my $user = rset('User')->find('bob');
244  
245 This is simply an alias for C<resultset>.
246  
247 =head1 SCHEMA GENERATION
248  
249 Setting the schema_class option and having proper DBIx::Class classes
250 is the recommended approach for performance and stability.
251 You can use the L<dbicdump> command line tool provided by
252 L<DBIx::Class::Schema::Loader> to help you.
253 For example, if your app were named Foo, then you could run the following
254 from the root of your project directory:
255  
256 dbicdump -o dump_directory=./lib Foo::Schema dbi:SQLite:/path/to/foo.db
257  
258 For this example, your C<schema_class> setting would be C<'Foo::Schema'>.
259  
260 =head1 SEE ALSO
261  
262 =over 4
263  
264 =item *
265  
266 L<DBICx::Sugar>
267  
268 =back
269  
270 =head1 CONTRIBUTORS
271  
272 =over 4
273  
274 =item *
275  
276 Alexis Sukrieh <sukria@sukria.net>
277  
278 =item *
279  
280 Dagfinn Ilmari Mannsåker <L<https://github.com/ilmari>>
281  
282 =item *
283  
284 David Precious <davidp@preshweb.co.uk>
285  
286 =item *
287  
288 Fabrice Gabolde <L<https://github.com/fgabolde>>
289  
290 =item *
291  
292 Franck Cuny <franck@lumberjaph.net>
293  
294 =item *
295  
296 Steven Humphrey <L<https://github.com/shumphrey>>
297  
298 =item *
299  
300 Yanick Champoux <L<https://github.com/yanick>>
301  
302 =back
303  
304 =head1 AUTHORS
305  
306 =over 4
307  
308 =item *
309  
310 Al Newkirk <awncorp@cpan.org>
311  
312 =item *
313  
314 Naveed Massjouni <naveed@vt.edu>
315  
316 =back
317  
318 =head1 COPYRIGHT AND LICENSE
319  
320 This software is copyright (c) 2010 by awncorp.
321  
322 This is free software; you can redistribute it and/or modify it under
323 the same terms as the Perl 5 programming language system itself.
324  
325 =cut
↑ Summary