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