Commit | Line | Data |
ad91060a |
1 | package Catalyst::Helper::Model::DBIC::Schema; |
2 | |
73f72d28 |
3 | use namespace::autoclean; |
f090a149 |
4 | use Moose; |
ffbf1967 |
5 | no warnings 'uninitialized'; |
018eb0e2 |
6 | |
9899ff66 |
7 | our $VERSION = '0.48'; |
4e251d1a |
8 | $VERSION = eval $VERSION; |
2201c2e4 |
9 | |
0f2fd2c0 |
10 | use Carp; |
2201c2e4 |
11 | use Tie::IxHash (); |
12 | use Data::Dumper (); |
c4fee9b8 |
13 | use List::Util 'first'; |
4cbe63e7 |
14 | use MooseX::Types::Moose qw/Str HashRef Bool ArrayRef/; |
15 | use Catalyst::Model::DBIC::Schema::Types 'CreateOption'; |
4cbe63e7 |
16 | use List::MoreUtils 'firstidx'; |
5f9a34d7 |
17 | use Scalar::Util 'looks_like_number'; |
6b6abf77 |
18 | use File::Find 'finddepth'; |
19 | use Try::Tiny; |
1fcd7804 |
20 | use Cwd 'getcwd'; |
2201c2e4 |
21 | |
ad91060a |
22 | =head1 NAME |
23 | |
24 | Catalyst::Helper::Model::DBIC::Schema - Helper for DBIC Schema Models |
25 | |
26 | =head1 SYNOPSIS |
27 | |
2201c2e4 |
28 | script/create.pl model CatalystModelName DBIC::Schema MyApp::SchemaClass \ |
c34bcab6 |
29 | [ create=dynamic | create=static ] [ traits=trait1,trait2... ] \ |
2ff00e2b |
30 | [ Schema::Loader opts ] [ dsn user pass ] \ |
31 | [ other connect_info args ] |
0f2fd2c0 |
32 | |
d89e6c8a |
33 | =head1 DESCRIPTION |
34 | |
35 | Helper for the DBIC Schema Models. |
36 | |
37 | =head2 Arguments: |
38 | |
018eb0e2 |
39 | C<CatalystModelName> is the short name for the Catalyst Model class |
40 | being generated (i.e. callable with C<$c-E<gt>model('CatalystModelName')>). |
6116daed |
41 | |
018eb0e2 |
42 | C<MyApp::SchemaClass> is the fully qualified classname of your Schema, |
6116daed |
43 | which might or might not yet exist. Note that you should have a good |
44 | reason to create this under a new global namespace, otherwise use an |
45 | existing top level namespace for your schema class. |
46 | |
018eb0e2 |
47 | C<create=dynamic> instructs this Helper to generate the named Schema |
6116daed |
48 | class for you, basing it on L<DBIx::Class::Schema::Loader> (which |
49 | means the table information will always be dynamically loaded at |
50 | runtime from the database). |
51 | |
018eb0e2 |
52 | C<create=static> instructs this Helper to generate the named Schema |
6116daed |
53 | class for you, using L<DBIx::Class::Schema::Loader> in "one shot" |
54 | mode to create a standard, manually-defined L<DBIx::Class::Schema> |
55 | setup, based on what the Loader sees in your database at this moment. |
56 | A Schema/Model pair generated this way will not require |
57 | L<DBIx::Class::Schema::Loader> at runtime, and will not automatically |
58 | adapt itself to changes in your database structure. You can edit |
59 | the generated classes by hand to refine them. |
60 | |
c34bcab6 |
61 | C<traits> is the list of traits to apply to the model, see |
2ff00e2b |
62 | L<Catalyst::Model::DBIC::Schema> for details. |
63 | |
64 | C<Schema::Loader opts> are described in L</TYPICAL EXAMPLES> below. |
65 | |
018eb0e2 |
66 | C<connect_info> arguments are the same as what |
6116daed |
67 | DBIx::Class::Schema::connect expects, and are storage_type-specific. |
68 | For DBI-based storage, these arguments are the dsn, username, |
69 | password, and connect options, respectively. These are optional for |
70 | existing Schemas, but required if you use either of the C<create=> |
71 | options. |
d89e6c8a |
72 | |
a75b6e58 |
73 | username and password can be omitted for C<SQLite> dsns. |
74 | |
d89e6c8a |
75 | Use of either of the C<create=> options requires L<DBIx::Class::Schema::Loader>. |
0b2a7108 |
76 | |
77 | =head1 TYPICAL EXAMPLES |
78 | |
a75b6e58 |
79 | Use DBIx::Class::Schema::Loader to create a static DBIx::Class::Schema, |
80 | and a Model which references it: |
81 | |
2201c2e4 |
82 | script/myapp_create.pl model CatalystModelName DBIC::Schema \ |
83 | MyApp::SchemaClass create=static dbi:mysql:foodb myuname mypass |
84 | |
a75b6e58 |
85 | Same, with extra connect_info args |
86 | user and pass can be omitted for sqlite, since they are always empty |
87 | |
2201c2e4 |
88 | script/myapp_create.pl model CatalystModelName DBIC::Schema \ |
a75b6e58 |
89 | MyApp::SchemaClass create=static dbi:SQLite:foo.db \ |
2201c2e4 |
90 | AutoCommit=1 cursor_class=DBIx::Class::Cursor::Cached \ |
a75b6e58 |
91 | on_connect_do='["select 1", "select 2"]' quote_char='"' |
92 | |
c34bcab6 |
93 | If using a 2 character quote_char: |
94 | |
95 | script/myapp_create.pl ... quote_char='[]' |
96 | |
a75b6e58 |
97 | B<ON WINDOWS COMMAND LINES QUOTING RULES ARE DIFFERENT> |
98 | |
99 | In C<cmd.exe> the above example would be: |
100 | |
101 | script/myapp_create.pl model CatalystModelName DBIC::Schema \ |
102 | MyApp::SchemaClass create=static dbi:SQLite:foo.db \ |
103 | AutoCommit=1 cursor_class=DBIx::Class::Cursor::Cached \ |
104 | on_connect_do="[\"select 1\", \"select 2\"]" quote_char="\"" |
105 | |
106 | Same, but with extra Schema::Loader args (separate multiple values by commas): |
0b2a7108 |
107 | |
2201c2e4 |
108 | script/myapp_create.pl model CatalystModelName DBIC::Schema \ |
109 | MyApp::SchemaClass create=static db_schema=foodb components=Foo,Bar \ |
4cbe63e7 |
110 | exclude='^(wibble|wobble)$' moniker_map='{ foo => "FOO" }' \ |
2201c2e4 |
111 | dbi:Pg:dbname=foodb myuname mypass |
34f036a0 |
112 | |
ce9e19dc |
113 | Coderefs are also supported: |
114 | |
115 | script/myapp_create.pl model CatalystModelName DBIC::Schema \ |
116 | MyApp::SchemaClass create=static \ |
117 | inflect_singular='sub { $_[0] =~ /\A(.+?)(_id)?\z/; $1 }' \ |
118 | moniker_map='sub { join(q{}, map ucfirst, split(/[\W_]+/, lc $_[0])); }' \ |
119 | dbi:mysql:foodb myuname mypass |
120 | |
a75b6e58 |
121 | See L<DBIx::Class::Schema::Loader::Base> for a list of options |
122 | |
123 | Create a dynamic DBIx::Class::Schema::Loader-based Schema, |
124 | and a Model which references it (B<DEPRECATED>): |
34f036a0 |
125 | |
2201c2e4 |
126 | script/myapp_create.pl model CatalystModelName DBIC::Schema \ |
127 | MyApp::SchemaClass create=dynamic dbi:mysql:foodb myuname mypass |
ad91060a |
128 | |
a75b6e58 |
129 | Reference an existing Schema of any kind, and provide some connection information for ->config: |
130 | |
2201c2e4 |
131 | script/myapp_create.pl model CatalystModelName DBIC::Schema \ |
132 | MyApp::SchemaClass dbi:mysql:foodb myuname mypass |
ad91060a |
133 | |
a75b6e58 |
134 | Same, but don't supply connect information yet (you'll need to do this |
135 | in your app config, or [not recommended] in the schema itself). |
136 | |
d89e6c8a |
137 | script/myapp_create.pl model ModelName DBIC::Schema My::SchemaClass |
ad91060a |
138 | |
f090a149 |
139 | =cut |
140 | |
141 | has helper => (is => 'ro', isa => 'Catalyst::Helper', required => 1); |
4cbe63e7 |
142 | has create => (is => 'rw', isa => CreateOption); |
143 | has args => (is => 'ro', isa => ArrayRef); |
c34bcab6 |
144 | has traits => (is => 'rw', isa => ArrayRef); |
61ed82a5 |
145 | has schema_class => (is => 'ro', isa => Str, required => 1); |
61ed82a5 |
146 | has loader_args => (is => 'rw', isa => HashRef); |
147 | has connect_info => (is => 'rw', isa => HashRef); |
61ed82a5 |
148 | has old_schema => (is => 'rw', isa => Bool, lazy_build => 1); |
6b6abf77 |
149 | has is_moose_schema => (is => 'rw', isa => Bool, lazy_build => 1); |
1fcd7804 |
150 | has result_namespace => (is => 'rw', isa => Str, lazy_build => 1); |
4cbe63e7 |
151 | has components => (is => 'rw', isa => ArrayRef); |
f090a149 |
152 | |
018eb0e2 |
153 | =head1 METHODS |
ad91060a |
154 | |
018eb0e2 |
155 | =head2 mk_compclass |
ad91060a |
156 | |
0fbbc8d5 |
157 | This is called by L<Catalyst::Helper> with the commandline args to generate the |
158 | files. |
159 | |
ad91060a |
160 | =cut |
161 | |
162 | sub mk_compclass { |
2201c2e4 |
163 | my ($package, $helper, $schema_class, @args) = @_; |
164 | |
4cbe63e7 |
165 | my $self = $package->new( |
166 | helper => $helper, |
167 | schema_class => $schema_class, |
168 | args => \@args |
169 | ); |
0f2fd2c0 |
170 | |
4cbe63e7 |
171 | $self->run; |
172 | } |
173 | |
174 | sub BUILD { |
175 | my $self = shift; |
176 | my $helper = $self->helper; |
7dfd616a |
177 | my @args = @{ $self->args || [] }; |
4cbe63e7 |
178 | |
179 | $helper->{schema_class} = $self->schema_class; |
c4fee9b8 |
180 | |
181 | @args = $self->_cleanup_args(\@args); |
d89e6c8a |
182 | |
c34bcab6 |
183 | my ($traits_idx, $traits); |
184 | if (($traits_idx = firstidx { ($traits) = /^traits=(\S*)\z/ } @args) != -1) { |
185 | my @traits = split /,/ => $traits; |
2201c2e4 |
186 | |
c34bcab6 |
187 | $self->traits(\@traits); |
4cbe63e7 |
188 | |
c34bcab6 |
189 | $helper->{traits} = '[' |
7dfd616a |
190 | .(join ',' => map { qq{'$_'} } @traits) |
4cbe63e7 |
191 | .']'; |
192 | |
c34bcab6 |
193 | splice @args, $traits_idx, 1, (); |
4cbe63e7 |
194 | } |
195 | |
196 | if ($args[0] && $args[0] =~ /^create=(\S*)\z/) { |
197 | $self->create($1); |
198 | shift @args; |
2ff00e2b |
199 | |
2201c2e4 |
200 | if (@args) { |
201 | $self->_parse_loader_args(\@args); |
202 | |
4cbe63e7 |
203 | $helper->{loader_args} = $self->_build_helper_loader_args; |
ca7cf6f0 |
204 | } |
205 | } |
4cbe63e7 |
206 | |
ca7cf6f0 |
207 | my $dbi_dsn_part; |
208 | if (first { ($dbi_dsn_part) = /^(dbi):/i } @args) { |
209 | die |
a4803ca6 |
210 | qq{DSN must start with 'dbi:' not '$dbi_dsn_part' (case matters!)} |
ca7cf6f0 |
211 | if $dbi_dsn_part ne 'dbi'; |
a4803ca6 |
212 | |
ca7cf6f0 |
213 | $helper->{setup_connect_info} = 1; |
2201c2e4 |
214 | |
ca7cf6f0 |
215 | $helper->{connect_info} = |
216 | $self->_build_helper_connect_info(\@args); |
2201c2e4 |
217 | |
ca7cf6f0 |
218 | $self->_parse_connect_info(\@args); |
d89e6c8a |
219 | } |
0f2fd2c0 |
220 | |
2201c2e4 |
221 | $helper->{generator} = ref $self; |
222 | $helper->{generator_version} = $VERSION; |
4cbe63e7 |
223 | } |
224 | |
9f7cc709 |
225 | =head2 run |
226 | |
227 | Can be called on an instance to generate the files. |
228 | |
229 | =cut |
230 | |
4cbe63e7 |
231 | sub run { |
232 | my $self = shift; |
2201c2e4 |
233 | |
4cbe63e7 |
234 | if ($self->create eq 'dynamic') { |
c4fee9b8 |
235 | $self->_print_dynamic_deprecation_warning; |
2201c2e4 |
236 | $self->_gen_dynamic_schema; |
4cbe63e7 |
237 | } elsif ($self->create eq 'static') { |
2201c2e4 |
238 | $self->_gen_static_schema; |
239 | } |
240 | |
241 | $self->_gen_model; |
242 | } |
243 | |
244 | sub _parse_loader_args { |
245 | my ($self, $args) = @_; |
246 | |
247 | my %loader_args = $self->_read_loader_args($args); |
248 | |
249 | while (my ($key, $val) = each %loader_args) { |
250 | next if $key =~ /^(?:components|constraint|exclude)\z/; |
251 | |
5f9a34d7 |
252 | $loader_args{$key} = $self->_eval($val); |
c4fee9b8 |
253 | die "syntax error for loader args key '$key' with value '$val': $@" |
2201c2e4 |
254 | if $@; |
255 | } |
256 | |
7e6a622c |
257 | my @components = $self->_build_loader_components( |
258 | delete $loader_args{components}, |
259 | $loader_args{use_namespaces}, |
260 | ); |
2201c2e4 |
261 | |
4cbe63e7 |
262 | $self->components(\@components); |
263 | |
2201c2e4 |
264 | for my $re_opt (qw/constraint exclude/) { |
265 | $loader_args{$re_opt} = qr/$loader_args{$re_opt}/ |
266 | if exists $loader_args{$re_opt}; |
267 | } |
268 | |
269 | tie my %result, 'Tie::IxHash'; |
270 | |
271 | %result = ( |
272 | relationships => 1, |
6b6abf77 |
273 | use_moose => $self->is_moose_schema ? 1 : 0, |
f0279514 |
274 | col_collision_map => 'column_%s', |
f090a149 |
275 | (!$self->old_schema ? ( |
2201c2e4 |
276 | use_namespaces => 1 |
277 | ) : ()), |
278 | (@components ? ( |
279 | components => \@components |
7e6a622c |
280 | ) : ()), |
281 | (%loader_args ? %loader_args : ()), |
2201c2e4 |
282 | ); |
283 | |
284 | $self->loader_args(\%result); |
285 | |
286 | wantarray ? %result : \%result; |
287 | } |
288 | |
289 | sub _read_loader_args { |
290 | my ($self, $args) = @_; |
291 | |
292 | my %loader_args; |
293 | |
a4803ca6 |
294 | while (@$args && $args->[0] !~ /^dbi:/i) { |
2201c2e4 |
295 | my ($key, $val) = split /=/, shift(@$args), 2; |
ca14239e |
296 | |
4cbe63e7 |
297 | if ($self->_is_struct($val)) { |
298 | $loader_args{$key} = $val; |
299 | } elsif ((my @vals = split /,/ => $val) > 1) { |
2201c2e4 |
300 | $loader_args{$key} = \@vals; |
ca14239e |
301 | } else { |
2201c2e4 |
302 | $loader_args{$key} = $val; |
ca14239e |
303 | } |
304 | } |
305 | |
2201c2e4 |
306 | wantarray ? %loader_args : \%loader_args; |
307 | } |
308 | |
309 | sub _build_helper_loader_args { |
310 | my $self = shift; |
311 | |
312 | my $args = $self->loader_args; |
313 | |
314 | tie my %loader_args, 'Tie::IxHash'; |
315 | |
316 | while (my ($arg, $val) = each %$args) { |
317 | if (ref $val) { |
318 | $loader_args{$arg} = $self->_data_struct_to_string($val); |
319 | } else { |
320 | $loader_args{$arg} = qq{'$val'}; |
0b2a7108 |
321 | } |
0f2fd2c0 |
322 | } |
323 | |
2201c2e4 |
324 | \%loader_args |
325 | } |
326 | |
327 | sub _build_loader_components { |
7e6a622c |
328 | my ($self, $components, $use_namespaces) = @_; |
d89e6c8a |
329 | |
7e6a622c |
330 | my @components = $self->old_schema && (not $use_namespaces) ? () |
331 | : ('InflateColumn::DateTime'); |
d89e6c8a |
332 | |
2201c2e4 |
333 | if ($components) { |
334 | $components = [ $components ] if !ref $components; |
335 | push @components, @$components; |
d89e6c8a |
336 | } |
2201c2e4 |
337 | |
338 | wantarray ? @components : \@components; |
339 | } |
340 | |
341 | sub _build_helper_connect_info { |
342 | my ($self, $connect_info) = @_; |
343 | |
344 | my @connect_info = @$connect_info; |
345 | |
a75b6e58 |
346 | my ($dsn, $user, $password) = $self->_get_dsn_user_pass(\@connect_info); |
2201c2e4 |
347 | |
348 | tie my %helper_connect_info, 'Tie::IxHash'; |
349 | |
350 | %helper_connect_info = ( |
351 | dsn => qq{'$dsn'}, |
352 | user => qq{'$user'}, |
353 | password => qq{'$password'} |
354 | ); |
355 | |
356 | for (@connect_info) { |
357 | if (/^\s*{.*}\s*\z/) { |
5f9a34d7 |
358 | my $hash = $self->_eval($_); |
c4fee9b8 |
359 | die "Syntax errorr in connect_info hash: $_: $@" if $@; |
2201c2e4 |
360 | my %hash = %$hash; |
361 | |
362 | for my $key (keys %hash) { |
363 | my $val = $hash{$key}; |
364 | |
365 | if (ref $val) { |
366 | $val = $self->_data_struct_to_string($val); |
367 | } else { |
c34bcab6 |
368 | $val = $self->_quote($val); |
2201c2e4 |
369 | } |
370 | |
371 | $helper_connect_info{$key} = $val; |
592cd3ae |
372 | } |
2201c2e4 |
373 | |
374 | next; |
592cd3ae |
375 | } |
376 | |
2201c2e4 |
377 | my ($key, $val) = split /=/, $_, 2; |
1d155058 |
378 | |
c34bcab6 |
379 | if ($key eq 'quote_char') { |
380 | $helper_connect_info{$key} = length($val) == 1 ? |
381 | $self->_quote($val) : |
382 | $self->_data_struct_to_string([split //, $val]); |
383 | } else { |
384 | $helper_connect_info{$key} = $self->_quote_unless_struct($val); |
385 | } |
2201c2e4 |
386 | } |
1d155058 |
387 | |
2201c2e4 |
388 | \%helper_connect_info |
389 | } |
1d155058 |
390 | |
61ed82a5 |
391 | sub _build_old_schema { |
392 | my $self = shift; |
393 | |
1fcd7804 |
394 | return $self->result_namespace eq '' ? 1 : 0; |
61ed82a5 |
395 | } |
396 | |
6b6abf77 |
397 | sub _build_is_moose_schema { |
398 | my $self = shift; |
399 | |
400 | my @schema_parts = split '::', $self->schema_class; |
1fcd7804 |
401 | |
402 | my $result_dir = File::Spec->catfile( |
403 | $self->helper->{base}, 'lib', @schema_parts, $self->result_namespace |
404 | ); |
6b6abf77 |
405 | |
406 | # assume yes for new schemas |
1fcd7804 |
407 | return 1 if not -d $result_dir; |
6b6abf77 |
408 | |
409 | my $uses_moose = 1; |
410 | |
1fcd7804 |
411 | my $cwd = getcwd; |
412 | |
6b6abf77 |
413 | try { |
414 | finddepth(sub { |
9a6afcd6 |
415 | return if $File::Find::name !~ /\.pm\z/; |
416 | |
6b6abf77 |
417 | open my $fh, '<', $File::Find::name |
418 | or die "Could not open $File::Find::name: $!"; |
419 | |
420 | my $code = do { local $/; <$fh> }; |
421 | close $fh; |
422 | |
423 | $uses_moose = 0 if $code !~ /\nuse Moose;\n/; |
424 | |
425 | die; |
1fcd7804 |
426 | }, $result_dir); |
6b6abf77 |
427 | }; |
428 | |
1fcd7804 |
429 | chdir $cwd; |
430 | |
6b6abf77 |
431 | return $uses_moose; |
432 | } |
433 | |
1fcd7804 |
434 | sub _build_result_namespace { |
435 | my $self = shift; |
436 | |
437 | my @schema_parts = split '::', $self->schema_class; |
438 | my $schema_pm = |
439 | File::Spec->catfile($self->helper->{base}, 'lib', @schema_parts) . '.pm'; |
440 | |
441 | if (not -f $schema_pm) { |
ed895a51 |
442 | eval { Class::MOP::load_class('DBIx::Class::Schema::Loader') }; |
1fcd7804 |
443 | |
444 | return 'Result' if $@; |
445 | |
a48759dd |
446 | return (try { DBIx::Class::Schema::Loader->VERSION('0.05') }) ? 'Result' : ''; |
1fcd7804 |
447 | } |
448 | |
449 | open my $fh, '<', $schema_pm or die "Could not open $schema_pm: $!"; |
450 | my $code = do { local $/; <$fh> }; |
451 | close $fh; |
452 | |
453 | my ($result_namespace) = $code =~ /result_namespace => '([^']+)'/; |
454 | |
9899ff66 |
455 | return $result_namespace if $result_namespace; |
456 | |
457 | return '' if $code =~ /->load_classes/; |
1fcd7804 |
458 | |
9899ff66 |
459 | return 'Result'; |
1fcd7804 |
460 | } |
461 | |
2201c2e4 |
462 | sub _data_struct_to_string { |
463 | my ($self, $data) = @_; |
34f036a0 |
464 | |
2201c2e4 |
465 | local $Data::Dumper::Terse = 1; |
466 | local $Data::Dumper::Quotekeys = 0; |
467 | local $Data::Dumper::Indent = 0; |
468 | local $Data::Dumper::Useqq = 1; |
34f036a0 |
469 | |
2201c2e4 |
470 | return Data::Dumper->Dump([$data]); |
471 | } |
34f036a0 |
472 | |
a75b6e58 |
473 | sub _get_dsn_user_pass { |
474 | my ($self, $connect_info) = @_; |
475 | |
476 | my $dsn = shift @$connect_info; |
477 | my ($user, $password); |
478 | |
479 | if ($dsn =~ /sqlite/i) { |
480 | ($user, $password) = ('', ''); |
108a45d1 |
481 | shift @$connect_info while @$connect_info and $connect_info->[0] eq ''; |
a75b6e58 |
482 | } else { |
483 | ($user, $password) = splice @$connect_info, 0, 2; |
484 | } |
485 | |
486 | ($dsn, $user, $password) |
487 | } |
488 | |
2201c2e4 |
489 | sub _parse_connect_info { |
490 | my ($self, $connect_info) = @_; |
491 | |
492 | my @connect_info = @$connect_info; |
493 | |
a75b6e58 |
494 | my ($dsn, $user, $password) = $self->_get_dsn_user_pass(\@connect_info); |
2201c2e4 |
495 | |
496 | tie my %connect_info, 'Tie::IxHash'; |
497 | @connect_info{qw/dsn user password/} = ($dsn, $user, $password); |
498 | |
499 | for (@connect_info) { |
500 | if (/^\s*{.*}\s*\z/) { |
5f9a34d7 |
501 | my $hash = $self->_eval($_); |
c4fee9b8 |
502 | die "Syntax errorr in connect_info hash: $_: $@" if $@; |
2201c2e4 |
503 | |
504 | %connect_info = (%connect_info, %$hash); |
ca14239e |
505 | |
2201c2e4 |
506 | next; |
ca14239e |
507 | } |
508 | |
2201c2e4 |
509 | my ($key, $val) = split /=/, $_, 2; |
510 | |
c34bcab6 |
511 | if ($key eq 'quote_char') { |
512 | $connect_info{$key} = length($val) == 1 ? $val : [split //, $val]; |
513 | } elsif ($key =~ /^(?:name_sep|limit_dialect)\z/) { |
3f139b02 |
514 | $connect_info{$key} = $val; |
515 | } else { |
5f9a34d7 |
516 | $connect_info{$key} = $self->_eval($val); |
3f139b02 |
517 | } |
518 | |
c4fee9b8 |
519 | die "syntax error for connect_info key '$key' with value '$val': $@" |
2201c2e4 |
520 | if $@; |
d89e6c8a |
521 | } |
202d09c8 |
522 | |
2201c2e4 |
523 | $self->connect_info(\%connect_info); |
524 | |
525 | \%connect_info |
526 | } |
527 | |
4cbe63e7 |
528 | sub _is_struct { |
529 | my ($self, $val) = @_; |
530 | |
ce9e19dc |
531 | return $val =~ /^\s*(?:sub|[[{])/; |
4cbe63e7 |
532 | } |
533 | |
c34bcab6 |
534 | sub _quote { |
535 | my ($self, $val) = @_; |
536 | |
537 | return 'q{'.$val.'}'; |
538 | } |
539 | |
2201c2e4 |
540 | sub _quote_unless_struct { |
541 | my ($self, $val) = @_; |
542 | |
c34bcab6 |
543 | $val = $self->_quote($val) if not $self->_is_struct($val); |
2201c2e4 |
544 | |
c34bcab6 |
545 | return $val; |
2201c2e4 |
546 | } |
547 | |
5f9a34d7 |
548 | sub _eval { |
549 | my ($self, $code) = @_; |
550 | |
551 | return $code if looks_like_number $code; |
552 | |
97631b44 |
553 | return $code if not $self->_is_struct($code); |
5f9a34d7 |
554 | |
555 | return eval "{no strict; $code}"; |
556 | } |
557 | |
2201c2e4 |
558 | sub _gen_dynamic_schema { |
559 | my $self = shift; |
560 | |
561 | my $helper = $self->helper; |
562 | |
563 | my @schema_parts = split(/\:\:/, $self->schema_class); |
564 | my $schema_file_part = pop @schema_parts; |
565 | |
566 | my $schema_dir = File::Spec->catfile( |
567 | $helper->{base}, 'lib', @schema_parts |
568 | ); |
569 | my $schema_file = File::Spec->catfile( |
570 | $schema_dir, $schema_file_part . '.pm' |
571 | ); |
572 | |
573 | $helper->mk_dir($schema_dir); |
574 | $helper->render_file('schemaclass', $schema_file); |
575 | } |
576 | |
577 | sub _gen_static_schema { |
578 | my $self = shift; |
579 | |
c4fee9b8 |
580 | die "cannot load schema without connect info" unless $self->connect_info; |
2201c2e4 |
581 | |
582 | my $helper = $self->helper; |
583 | |
584 | my $schema_dir = File::Spec->catfile($helper->{base}, 'lib'); |
585 | |
ed895a51 |
586 | try { |
587 | Class::MOP::load_class('DBIx::Class::Schema::Loader') |
588 | } |
589 | catch { |
590 | die "Cannot load DBIx::Class::Schema::Loader: $_"; |
591 | }; |
f090a149 |
592 | |
593 | DBIx::Class::Schema::Loader->import( |
2201c2e4 |
594 | "dump_to_dir:$schema_dir", 'make_schema_at' |
f090a149 |
595 | ); |
2201c2e4 |
596 | |
597 | make_schema_at( |
598 | $self->schema_class, |
599 | $self->loader_args, |
600 | [$self->connect_info] |
601 | ); |
602 | } |
603 | |
2201c2e4 |
604 | sub _gen_model { |
605 | my $self = shift; |
606 | my $helper = $self->helper; |
607 | |
608 | $helper->render_file('compclass', $helper->{file} ); |
ad91060a |
609 | } |
610 | |
c4fee9b8 |
611 | sub _print_dynamic_deprecation_warning { |
612 | warn <<EOF; |
613 | ************************************ WARNING ********************************** |
614 | * create=dynamic is DEPRECATED, please use create=static instead. * |
615 | ******************************************************************************* |
616 | EOF |
617 | print "Continue? [y/n]: "; |
618 | chomp(my $response = <STDIN>); |
619 | exit 0 if $response =~ /^n(o)?\z/; |
620 | } |
621 | |
622 | sub _cleanup_args { |
623 | my ($self, $args) = @_; |
624 | |
625 | # remove blanks, ie. someoned doing foo \ bar |
61ed82a5 |
626 | my @res = grep !/^\s+\z/, @$args; |
c4fee9b8 |
627 | |
628 | # remove leading whitespace, ie. foo \ bar |
629 | s/^\s*// for @res; |
630 | |
631 | @res |
632 | } |
633 | |
ad91060a |
634 | =head1 SEE ALSO |
635 | |
7b39f3f0 |
636 | General Catalyst Stuff: |
637 | |
ad91060a |
638 | L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>, |
7b39f3f0 |
639 | L<Catalyst::Response>, L<Catalyst::Helper>, L<Catalyst>, |
640 | |
641 | Stuff related to DBIC and this Model style: |
642 | |
643 | L<DBIx::Class>, L<DBIx::Class::Schema>, |
ef91bcf9 |
644 | L<DBIx::Class::Schema::Loader>, L<Catalyst::Model::DBIC::Schema> |
ad91060a |
645 | |
646 | =head1 AUTHOR |
647 | |
4e251d1a |
648 | See L<Catalyst::Model::DBIC::Schema/AUTHOR> and |
649 | L<Catalyst::Model::DBIC::Schema/CONTRIBUTORS>. |
ad91060a |
650 | |
4e251d1a |
651 | =head1 COPYRIGHT |
2ff00e2b |
652 | |
4e251d1a |
653 | See L<Catalyst::Model::DBIC::Schema/COPYRIGHT>. |
2ff00e2b |
654 | |
ad91060a |
655 | =head1 LICENSE |
656 | |
657 | This library is free software, you can redistribute it and/or modify |
658 | it under the same terms as Perl itself. |
659 | |
660 | =cut |
661 | |
dce0dfe8 |
662 | 1; |
663 | |
ad91060a |
664 | __DATA__ |
665 | |
5d11d759 |
666 | =begin pod_to_ignore |
667 | |
d89e6c8a |
668 | __schemaclass__ |
669 | package [% schema_class %]; |
670 | |
671 | use strict; |
672 | use base qw/DBIx::Class::Schema::Loader/; |
673 | |
674 | __PACKAGE__->loader_options( |
2201c2e4 |
675 | [%- FOREACH key = loader_args.keys %] |
676 | [% key %] => [% loader_args.${key} %], |
677 | [%- END -%] |
678 | |
d89e6c8a |
679 | ); |
680 | |
681 | =head1 NAME |
682 | |
2201c2e4 |
683 | [% schema_class %] - L<DBIx::Class::Schema::Loader> class |
d89e6c8a |
684 | |
685 | =head1 SYNOPSIS |
686 | |
687 | See L<[% app %]> |
688 | |
689 | =head1 DESCRIPTION |
690 | |
2201c2e4 |
691 | Dynamic L<DBIx::Class::Schema::Loader> schema for use in L<[% class %]> |
692 | |
693 | =head1 GENERATED BY |
694 | |
695 | [% generator %] - [% generator_version %] |
d89e6c8a |
696 | |
697 | =head1 AUTHOR |
698 | |
2201c2e4 |
699 | [% author.replace(',+$', '') %] |
d89e6c8a |
700 | |
701 | =head1 LICENSE |
702 | |
703 | This library is free software, you can redistribute it and/or modify |
704 | it under the same terms as Perl itself. |
705 | |
706 | =cut |
707 | |
708 | 1; |
709 | |
ad91060a |
710 | __compclass__ |
711 | package [% class %]; |
712 | |
713 | use strict; |
714 | use base 'Catalyst::Model::DBIC::Schema'; |
715 | |
716 | __PACKAGE__->config( |
0f2fd2c0 |
717 | schema_class => '[% schema_class %]', |
c34bcab6 |
718 | [% IF traits %]traits => [% traits %],[% END %] |
2201c2e4 |
719 | [% IF setup_connect_info %]connect_info => { |
720 | [%- FOREACH key = connect_info.keys %] |
721 | [% key %] => [% connect_info.${key} %], |
722 | [%- END -%] |
723 | |
724 | }[% END %] |
ad91060a |
725 | ); |
726 | |
727 | =head1 NAME |
728 | |
1aeb6e1e |
729 | [% class %] - Catalyst DBIC Schema Model |
2201c2e4 |
730 | |
ad91060a |
731 | =head1 SYNOPSIS |
732 | |
733 | See L<[% app %]> |
734 | |
735 | =head1 DESCRIPTION |
736 | |
d89e6c8a |
737 | L<Catalyst::Model::DBIC::Schema> Model using schema L<[% schema_class %]> |
ad91060a |
738 | |
2201c2e4 |
739 | =head1 GENERATED BY |
740 | |
741 | [% generator %] - [% generator_version %] |
742 | |
ad91060a |
743 | =head1 AUTHOR |
744 | |
2201c2e4 |
745 | [% author.replace(',+$', '') %] |
ad91060a |
746 | |
747 | =head1 LICENSE |
748 | |
749 | This library is free software, you can redistribute it and/or modify |
750 | it under the same terms as Perl itself. |
751 | |
752 | =cut |
753 | |
754 | 1; |
a75b6e58 |
755 | __END__ |
756 | # vim:sts=4 sw=4: |