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