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