| 1 | package Mojo::DB::Role::DBIx::Class; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | # ABSTRACT: provides a convenience role creating a DBIx::Class for your Mojo database | ||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | 2 | ▶ | use Mojo::Base -role; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 2 | ▶ | use Mojo::Util qw/dumper/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 2 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 2 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 2 | ▶ | use DBIx::Class::Schema::Loader; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 2 | ▶ | use Module::Load; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | # use Test::Schema; | ||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 2 | $\ = "\n"; $, = "\t"; | |||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | 13 | ▶ | sub dbic { | ||||||||||||||||||
|
Subroutine
dbic: called CC=8 SCAR=21.2
Pod
documented
| |||||||||||||||||||||
| 19 | 13 | my $self = shift; | |||||||||||||||||||
| 20 | 13 | my $opts = shift; | |||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | my $class = | ||||||||||||||||||||
| 23 | $opts ? | ||||||||||||||||||||
| 24 | ref $opts ? | ||||||||||||||||||||
| 25 | delete $opts->{class} | ||||||||||||||||||||
| 26 | : | ||||||||||||||||||||
| 27 | 13 | ▶ | $opts | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 28 | : | ||||||||||||||||||||
| 29 | 'DBIx::Class::Schema::Loader'; | ||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | 13 | ▶ | load $class unless $class eq 'DBIx::Class::Schema::Loader'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 32 | 13 | ▶ | $opts = {} unless ref $opts eq 'HASH'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | 13 | ▶ | $opts->{unsafe} = 1 if $self->dbh->{Driver}{Name} =~ /SQLite/; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | 13 | ▶ | $class->naming(delete $opts->{naming} || 'current') if $class eq 'DBIx::Class::Schema::Loader'; | ||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
delete $opts->{'naming'} || 'current'
MC/DC0%0/1
delete $opts->{'naming'} || 'current'
delete $opts->{'naming'}
Truth table50%1/2
A
A= delete $opts->{'naming'}
| |||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | 13 | ▶ | state $connect = $class->connect(sub { $self->dbh }, $opts); | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 39 | } | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | 9 | ▶ | sub resultset { | ||||||||||||||||||
|
Subroutine
resultset: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 42 | 9 | return shift->dbic()->resultset(@_); | |||||||||||||||||||
| 43 | } | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | 2 | 1; | |||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | =encoding utf8 | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | =pod | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | =head1 NAME | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | Mojo::DB::Role::DBIx::Class - A Mojo role connecting DBIx::Class to Mojo::Pg and Mojo::SQLite | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | =head1 SYNOPSIS | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | use Mojo::SQLite; | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | $\ = "\n"; $, = "\t"; | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | my $sql = Mojo::SQLite->new; | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | my $db = $sql->db; | ||||||||||||||||||||
| 65 | $db = $db->with_roles('Mojo::DB::Role::DBIx::Class'); | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | my $rs = $db->resultset('SomeTable')->search({ some_field => 'some_value' }) | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | print for $db->dbic->sources | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | =head1 DESCRIPTION | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | This module adds a dbic method loading a DBIx::Class on top of an existing Mojo::Pg or Mojo::SQLite database | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | =head2 Methods | ||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | =over 12 | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | =item C<dbic> | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | Connects the database via the schema either with a schema class passed as the parameter: | ||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | $db->dbic('My::Schema') | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | or with a schema class passed as "class" option: | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | $db->dbic({ class => 'My::Schema' }) | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | or without any schema - in which case DBIx::Class::Schema::Loader gets used | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | $db->dbic($opts) | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | All other options get passed on to the class being connected. | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | Returns the DBIx::Class::Schema so that it can be used like this | ||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | print for $db->dbic->sources; | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | my $rs = $db->dbic->resultset('SomeTable')->search({ some_field => 'some_value' }) | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | =item C<resultset> | ||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | Shortcut for $db->dbic->resultset | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | my $rs = $db->resultset('SomeTable')->search({ some_field => 'some_value' }) | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | =back | ||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | This software is copyright (c) 2021 by Simone Cesano. | ||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | =head1 SEE ALSO | ||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | L<DBIx::Class>, L<DBIx::Class::Schema::Loader>, L<Mojo::Pg>, L<Mojo::SQLite> | ||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | =head1 AUTHOR | ||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | Simone Cesano | ||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | =cut | ||||||||||||||||||||
| 124 | |||||||||||||||||||||