1 package # hide from PAUSE
2 Catalyst::Model::DBIC::Schema::Types;
4 use MooseX::Types -declare => [qw/
5 ConnectInfo ConnectInfos Replicants SchemaClass CreateOption
9 use Carp::Clan '^Catalyst::Model::DBIC::Schema';
10 use MooseX::Types::Moose qw/ArrayRef HashRef CodeRef Str ClassName/;
11 use MooseX::Types::LoadableClass qw/LoadableClass/;
12 use Scalar::Util 'reftype';
13 use List::MoreUtils 'all';
15 use namespace::clean -except => 'meta';
19 where { $_->isa('DBIx::Class::Schema') };
21 class_type Schema, { class => 'DBIx::Class::Schema' };
25 where { exists $_->{dsn} || exists $_->{dbh_maker} },
26 message { 'Does not look like a valid connect_info' };
30 via(\&_coerce_connect_info_from_str),
32 via(\&_coerce_connect_info_from_arrayref),
34 via { +{ dbh_maker => $_ } },
37 # { connect_info => [ ... ] } coercion would be nice, but no chained coercions
39 # Also no coercion from base type (yet,) but in Moose git already.
41 # via { $_->{connect_info} },
44 as ArrayRef[ConnectInfo],
45 message { "Not a valid array of connect_info's" };
49 via { [ _coerce_connect_info_from_str() ] },
51 via { [ +{ dbh_maker => $_ } ] },
56 !ref $_ ? _coerce_connect_info_from_str()
57 : reftype $_ eq 'HASH' ? $_
58 : reftype $_ eq 'CODE' ? +{ dbh_maker => $_ }
59 : reftype $_ eq 'ARRAY' ? _coerce_connect_info_from_arrayref()
60 : croak 'invalid connect_info'
67 where { /^(?:static|dynamic)\z/ },
68 message { "Invalid create option, must be one of 'static' or 'dynamic'" };
70 sub _coerce_connect_info_from_arrayref {
76 my $slurp_hashes = sub {
78 my $extra = shift @$_;
80 croak "invalid connect_info"
81 unless ref $extra && reftype $extra eq 'HASH';
83 %connect_info = (%connect_info, %$extra);
87 if (!ref $_->[0]) { # array style
88 $connect_info{dsn} = shift @$_;
89 $connect_info{user} = shift @$_ if !ref $_->[0];
90 $connect_info{password} = shift @$_ if !ref $_->[0];
94 croak "invalid connect_info" if @$_;
95 } elsif (ref $_->[0] && reftype $_->[0] eq 'CODE') {
96 $connect_info{dbh_maker} = shift @$_;
100 croak "invalid connect_info" if @$_;
101 } elsif (@$_ == 1 && ref $_->[0] && reftype $_->[0] eq 'HASH') {
104 croak "invalid connect_info";
107 unless ($connect_info{dbh_maker}) {
108 for my $key (qw/user password/) {
109 $connect_info{$key} = ''
110 if not defined $connect_info{$key};
117 sub _coerce_connect_info_from_str {
118 +{ dsn => $_, user => '', password => '' }