| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Mojolicious::Plugin::DBIC; | 
| 2 |  |  |  |  |  |  | our $VERSION = '0.002'; | 
| 3 |  |  |  |  |  |  | # ABSTRACT: Mojolicious ♥ DBIx::Class | 
| 4 |  |  |  |  |  |  |  | 
| 5 |  |  |  |  |  |  | #pod =head1 SYNOPSIS | 
| 6 |  |  |  |  |  |  | #pod | 
| 7 |  |  |  |  |  |  | #pod     use Mojolicious::Lite; | 
| 8 |  |  |  |  |  |  | #pod     plugin DBIC => { | 
| 9 |  |  |  |  |  |  | #pod         schema => { 'Local::Schema' => 'dbi:SQLite::memory:' }, | 
| 10 |  |  |  |  |  |  | #pod     }; | 
| 11 |  |  |  |  |  |  | #pod     get '/model', { | 
| 12 |  |  |  |  |  |  | #pod         controller => 'DBIC', | 
| 13 |  |  |  |  |  |  | #pod         action => 'list', | 
| 14 |  |  |  |  |  |  | #pod         resultset => 'Model', | 
| 15 |  |  |  |  |  |  | #pod         template => 'model/list.html.ep', | 
| 16 |  |  |  |  |  |  | #pod     }; | 
| 17 |  |  |  |  |  |  | #pod     app->start; | 
| 18 |  |  |  |  |  |  | #pod     __DATA__ | 
| 19 |  |  |  |  |  |  | #pod     @@ model/list.html.ep | 
| 20 |  |  |  |  |  |  | #pod     % for my $row ( $resultset->all ) { | 
| 21 |  |  |  |  |  |  | #pod <%= $row->id %> | 
| 22 |  |  |  |  |  |  | #pod     % } | 
| 23 |  |  |  |  |  |  | #pod | 
| 24 |  |  |  |  |  |  | #pod =head1 DESCRIPTION | 
| 25 |  |  |  |  |  |  | #pod | 
| 26 |  |  |  |  |  |  | #pod This plugin makes working with L easier in Mojolicious. | 
| 27 |  |  |  |  |  |  | #pod | 
| 28 |  |  |  |  |  |  | #pod =head2 Configuration | 
| 29 |  |  |  |  |  |  | #pod | 
| 30 |  |  |  |  |  |  | #pod Configure your schema in multiple ways: | 
| 31 |  |  |  |  |  |  | #pod | 
| 32 |  |  |  |  |  |  | #pod     # Just DSN | 
| 33 |  |  |  |  |  |  | #pod     plugin DBIC => { | 
| 34 |  |  |  |  |  |  | #pod         schema => { | 
| 35 |  |  |  |  |  |  | #pod             'MySchema' => 'DSN', | 
| 36 |  |  |  |  |  |  | #pod         }, | 
| 37 |  |  |  |  |  |  | #pod     }; | 
| 38 |  |  |  |  |  |  | #pod | 
| 39 |  |  |  |  |  |  | #pod     # Arguments to connect() | 
| 40 |  |  |  |  |  |  | #pod     plugin DBIC => { | 
| 41 |  |  |  |  |  |  | #pod         schema => { | 
| 42 |  |  |  |  |  |  | #pod             'MySchema' => [ 'DSN', 'user', 'password', { RaiseError => 1 } ], | 
| 43 |  |  |  |  |  |  | #pod         }, | 
| 44 |  |  |  |  |  |  | #pod     }; | 
| 45 |  |  |  |  |  |  | #pod | 
| 46 |  |  |  |  |  |  | #pod     # Connected schema object | 
| 47 |  |  |  |  |  |  | #pod     my $schema = MySchema->connect( ... ); | 
| 48 |  |  |  |  |  |  | #pod     plugin DBIC => { | 
| 49 |  |  |  |  |  |  | #pod         schema => $schema, | 
| 50 |  |  |  |  |  |  | #pod     }; | 
| 51 |  |  |  |  |  |  | #pod | 
| 52 |  |  |  |  |  |  | #pod This plugin can also be configured from the application configuration | 
| 53 |  |  |  |  |  |  | #pod file: | 
| 54 |  |  |  |  |  |  | #pod | 
| 55 |  |  |  |  |  |  | #pod     # myapp.conf | 
| 56 |  |  |  |  |  |  | #pod     { | 
| 57 |  |  |  |  |  |  | #pod         dbic => { | 
| 58 |  |  |  |  |  |  | #pod             schema => { | 
| 59 |  |  |  |  |  |  | #pod                 'MySchema' => 'dbi:SQLite:data.db', | 
| 60 |  |  |  |  |  |  | #pod             }, | 
| 61 |  |  |  |  |  |  | #pod         }, | 
| 62 |  |  |  |  |  |  | #pod     } | 
| 63 |  |  |  |  |  |  | #pod | 
| 64 |  |  |  |  |  |  | #pod     # myapp.pl | 
| 65 |  |  |  |  |  |  | #pod     use Mojolicious::Lite; | 
| 66 |  |  |  |  |  |  | #pod     plugin 'Config'; | 
| 67 |  |  |  |  |  |  | #pod     plugin 'DBIC'; | 
| 68 |  |  |  |  |  |  | #pod | 
| 69 |  |  |  |  |  |  | #pod =head2 Controller | 
| 70 |  |  |  |  |  |  | #pod | 
| 71 |  |  |  |  |  |  | #pod This plugin contains a controller to reduce the code needed for simple | 
| 72 |  |  |  |  |  |  | #pod database operations. See L. | 
| 73 |  |  |  |  |  |  | #pod | 
| 74 |  |  |  |  |  |  | #pod =head1 SEE ALSO | 
| 75 |  |  |  |  |  |  | #pod | 
| 76 |  |  |  |  |  |  | #pod L, L, L | 
| 77 |  |  |  |  |  |  | #pod | 
| 78 |  |  |  |  |  |  | #pod =cut | 
| 79 |  |  |  |  |  |  |  | 
| 80 | 3 |  |  | 3 |  | 1122877 | use Mojo::Base 'Mojolicious::Plugin'; | 
|  | 3 |  |  |  |  | 9 |  | 
|  | 3 |  |  |  |  | 18 |  | 
| 81 | 3 |  |  | 3 |  | 512 | use Mojo::Loader qw( load_class ); | 
|  | 3 |  |  |  |  | 7 |  | 
|  | 3 |  |  |  |  | 168 |  | 
| 82 | 3 |  |  | 3 |  | 18 | use Scalar::Util qw( blessed ); | 
|  | 3 |  |  |  |  | 6 |  | 
|  | 3 |  |  |  |  | 1185 |  | 
| 83 |  |  |  |  |  |  |  | 
| 84 |  |  |  |  |  |  | sub register { | 
| 85 | 7 |  |  | 7 | 1 | 32272 | my ( $self, $app, $conf ) = @_; | 
| 86 |  |  |  |  |  |  | # XXX Allow multiple schemas? | 
| 87 | 7 |  |  |  |  | 18 | my $schema_conf = $conf->{schema}; | 
| 88 | 7 | 100 | 66 |  |  | 43 | if ( !$schema_conf && $app->can( 'config' ) ) { | 
| 89 | 4 |  |  |  |  | 96 | $schema_conf = $app->config->{dbic}{schema}; | 
| 90 |  |  |  |  |  |  | } | 
| 91 |  |  |  |  |  |  | $app->helper( schema => sub { | 
| 92 | 11 |  |  | 11 |  | 756 | state $schema = _load_schema( $schema_conf ); | 
| 93 | 9 |  |  |  |  | 76606 | return $schema; | 
| 94 | 7 |  |  |  |  | 85 | } ); | 
| 95 | 7 |  |  |  |  | 527 | push @{ $app->routes->namespaces }, 'Mojolicious::Plugin::DBIC::Controller'; | 
|  | 7 |  |  |  |  | 26 |  | 
| 96 |  |  |  |  |  |  | } | 
| 97 |  |  |  |  |  |  |  | 
| 98 |  |  |  |  |  |  | sub _load_schema { | 
| 99 | 7 |  |  | 7 |  | 18 | my ( $conf ) = @_; | 
| 100 | 7 | 100 | 66 |  |  | 102 | if ( blessed $conf && $conf->isa( 'DBIx::Class::Schema' ) ) { | 
|  |  | 100 |  |  |  |  |  | 
| 101 | 3 |  |  |  |  | 12 | return $conf; | 
| 102 |  |  |  |  |  |  | } | 
| 103 |  |  |  |  |  |  | elsif ( ref $conf eq 'HASH' ) { | 
| 104 | 3 |  |  |  |  | 5 | my ( $class, $args ) = %{ $conf }; | 
|  | 3 |  |  |  |  | 12 |  | 
| 105 | 3 | 100 |  |  |  | 11 | if ( my $e = load_class( $class ) ) { | 
| 106 | 1 |  |  |  |  | 368 | die sprintf 'Unable to load schema class %s: %s', | 
| 107 |  |  |  |  |  |  | $class, $e; | 
| 108 |  |  |  |  |  |  | } | 
| 109 | 2 | 100 |  |  |  | 163931 | return $class->connect( ref $args eq 'ARRAY' ? @$args : $args ); | 
| 110 |  |  |  |  |  |  | } | 
| 111 | 1 |  |  |  |  | 12 | die sprintf "Unknown DBIC schema config. Must be schema object or HASH, not %s", | 
| 112 |  |  |  |  |  |  | ref $conf; | 
| 113 |  |  |  |  |  |  | } | 
| 114 |  |  |  |  |  |  |  | 
| 115 |  |  |  |  |  |  | 1; | 
| 116 |  |  |  |  |  |  |  | 
| 117 |  |  |  |  |  |  | __END__ |