Catalyst-Model-DBIC-Schema

 view release on metacpan or  search on metacpan

lib/Catalyst/Helper/Model/DBIC/Schema.pm  view on Meta::CPAN

Use DBIx::Class::Schema::Loader to create a static DBIx::Class::Schema,
and a Model which references it:

  script/myapp_create.pl model CatalystModelName DBIC::Schema \
    MyApp::SchemaClass create=static dbi:mysql:foodb myuname mypass

Same, with extra connect_info args
user and pass can be omitted for sqlite, since they are always empty

  script/myapp_create.pl model CatalystModelName DBIC::Schema \
    MyApp::SchemaClass create=static dbi:SQLite:foo.db \
    AutoCommit=1 cursor_class=DBIx::Class::Cursor::Cached \
    on_connect_do='["select 1", "select 2"]' quote_names=1

B<ON WINDOWS COMMAND LINES QUOTING RULES ARE DIFFERENT>

In C<cmd.exe> the above example would be:

  script/myapp_create.pl model CatalystModelName DBIC::Schema \
    MyApp::SchemaClass create=static dbi:SQLite:foo.db \
    AutoCommit=1 cursor_class=DBIx::Class::Cursor::Cached \
    on_connect_do="[\"select 1\", \"select 2\"]" quote_names=1

Same, but with extra Schema::Loader args (separate multiple values by commas):

  script/myapp_create.pl model CatalystModelName DBIC::Schema \
    MyApp::SchemaClass create=static db_schema=foodb components=Foo,Bar \
    exclude='^(wibble|wobble)$' moniker_map='{ foo => "FOO" }' \
    dbi:Pg:dbname=foodb myuname mypass

Coderefs are also supported:

  script/myapp_create.pl model CatalystModelName DBIC::Schema \
    MyApp::SchemaClass create=static \
    inflect_singular='sub { $_[0] =~ /\A(.+?)(_id)?\z/; $1 }' \
    moniker_map='sub { join(q{}, map ucfirst, split(/[\W_]+/, lc $_[0])); }' \
    dbi:mysql:foodb myuname mypass

See L<DBIx::Class::Schema::Loader::Base> for a list of options

Create a dynamic DBIx::Class::Schema::Loader-based Schema,
and a Model which references it (B<DEPRECATED>):

  script/myapp_create.pl model CatalystModelName DBIC::Schema \
    MyApp::SchemaClass create=dynamic dbi:mysql:foodb myuname mypass

Reference an existing Schema of any kind, and provide some connection information for ->config:

  script/myapp_create.pl model CatalystModelName DBIC::Schema \
    MyApp::SchemaClass dbi:mysql:foodb myuname mypass

Same, but don't supply connect information yet (you'll need to do this
in your app config, or [not recommended] in the schema itself).

  script/myapp_create.pl model ModelName DBIC::Schema My::SchemaClass

=cut

has helper => (is => 'ro', isa => 'Catalyst::Helper', required => 1);
has create => (is => 'rw', isa => CreateOption);
has args => (is => 'ro', isa => ArrayRef);
has traits => (is => 'rw', isa => ArrayRef);
has schema_class => (is => 'ro', isa => Str, required => 1);
has loader_args => (is => 'rw', isa => HashRef);
has connect_info => (is => 'rw', isa => HashRef);
has old_schema => (is => 'rw', isa => Bool, lazy_build => 1);
has is_moose_schema => (is => 'rw', isa => Bool, lazy_build => 1);
has result_namespace => (is => 'rw', isa => Str, lazy_build => 1);
has components => (is => 'rw', isa => ArrayRef);

=head1 METHODS

=head2 mk_compclass

This is called by L<Catalyst::Helper> with the commandline args to generate the
files.

=cut

sub mk_compclass {
    my ($package, $helper, $schema_class, @args) = @_;

    my $self = $package->new(
        helper => $helper,
        schema_class => $schema_class,
        args => \@args
    );

    $self->run;
}

sub BUILD {
    my $self   = shift;
    my $helper = $self->helper;
    my @args   = @{ $self->args || [] };

    $helper->{schema_class} = $self->schema_class;

    @args = $self->_cleanup_args(\@args);

    my ($traits_idx, $traits);
    if (($traits_idx = firstidx { ($traits) = /^traits=(\S*)\z/ } @args) != -1) {
        my @traits = split /,/ => $traits;

        $self->traits(\@traits);

        $helper->{traits} = '['
            .(join ',' => map { qq{'$_'} } @traits)
            .']';

        splice @args, $traits_idx, 1, ();
    }

    if ($args[0] && $args[0] =~ /^create=(\S*)\z/) {
        $self->create($1);
        shift @args;

        if (@args) {
            $self->_parse_loader_args(\@args);

            $helper->{loader_args} = $self->_build_helper_loader_args;



( run in 1.342 second using v1.01-cache-2.11-cpan-39bf76dae61 )