| 1 | package Mad::Mapper; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||
| 3 | =encoding utf8 | ||||||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||
| 7 | Mad::Mapper - Map Perl objects to PostgreSQL, MySQL or SQLite data | ||||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||
| 9 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 0.09 | ||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||
| 13 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||
| 15 | L<Mad::Mapper> is a base class for objects that should be stored in a | ||||||||||||||||||||||||||||||||||||||||||||||
| 16 | persistent SQL database. Currently the supported backends are L<Mojo::Pg> | ||||||||||||||||||||||||||||||||||||||||||||||
| 17 | L<Mojo::mysql> and L<Mojo::SQLite>. These backends need to be installed | ||||||||||||||||||||||||||||||||||||||||||||||
| 18 | separately. | ||||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||
| 20 | $ cpanm Mad::Mapper | ||||||||||||||||||||||||||||||||||||||||||||||
| 21 | $ cpanm Mojo::Pg # Mad::Mapper now supports postgres! | ||||||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||||||
| 23 | THIS MODULE IS EXPERIMENTAL. It is in use in production though, so | ||||||||||||||||||||||||||||||||||||||||||||||
| 24 | big changes will not be made without extreme consideration. | ||||||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||||||
| 26 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||||||
| 28 | package MyApp::Model::User; | ||||||||||||||||||||||||||||||||||||||||||||||
| 29 | use Mad::Mapper -base; | ||||||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||||||
| 31 | # Class attributes | ||||||||||||||||||||||||||||||||||||||||||||||
| 32 | col id => undef; | ||||||||||||||||||||||||||||||||||||||||||||||
| 33 | col email => ''; | ||||||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||||||
| 35 | See also L<Mad::Mapper::Guides::Tutorial> for more details and | ||||||||||||||||||||||||||||||||||||||||||||||
| 36 | L<Mad::Mapper::Guides::Custom> if you want more control. | ||||||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||||||
| 38 | =head1 RELATIONSHIPS | ||||||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||||||
| 40 | See L<Mad::Mapper::Guides::HasMany> for example "has many" relationship. | ||||||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||||||
| 42 | TODO: C<belongs_to()> and maybe C<has_one()>. | ||||||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||||||
| 44 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 5 | ▶ | use Mojo::Base -base; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 5 | ▶ | use Mojo::IOLoop; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 5 | ▶ | use Mojo::JSON (); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 5 | ▶ | use Mojo::Loader 'load_class'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 5 | ▶ | use Scalar::Util 'weaken'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 5 | ▶ | use constant DEBUG => $ENV{MAD_DEBUG} || 0; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.1
Condition50%1/2
$ENV{'MAD_DEBUG'} || 0
MC/DC0%0/1
$ENV{'MAD_DEBUG'} || 0
$ENV{'MAD_DEBUG'}
Truth table50%1/2
A
A= $ENV{'MAD_DEBUG'}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 5 | our $VERSION = '0.09'; | |||||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 5 | my (%COLUMNS, %LOADED, %PK); | |||||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||||||
| 57 | =head1 EXPORTED FUNCTIONS | ||||||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||||||
| 59 | =head2 col | ||||||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||||||
| 61 | Used to define a column. Follow the same rules as L</has>. | ||||||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||||||
| 63 | =head2 has | ||||||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||||||
| 65 | has name => "Bruce"; | ||||||||||||||||||||||||||||||||||||||||||||||
| 66 | has [qw(name email)]; | ||||||||||||||||||||||||||||||||||||||||||||||
| 67 | has pet => sub { Cat->new }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||||||
| 69 | Same as L<Mojo::Base/has>. | ||||||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||||||
| 71 | =head2 pk | ||||||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||||||
| 73 | Used to define a primary key. Follow the same rules as L</has>. | ||||||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||||||
| 75 | The primary key is used by default in L</load> and L</update> to update the | ||||||||||||||||||||||||||||||||||||||||||||||
| 76 | correct row. If omitted, the first L</col> will act as primary key. | ||||||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||||||
| 78 | Note that L</pk> is not returned by L</columns>. | ||||||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||||||
| 80 | =head2 table | ||||||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||||||
| 82 | Used to define a table name. The default is to decamelize the last part of the | ||||||||||||||||||||||||||||||||||||||||||||||
| 83 | class name and add "s" at the end, unless it already has "s" at the end. | ||||||||||||||||||||||||||||||||||||||||||||||
| 84 | Examples: | ||||||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||||||
| 86 | .-------------------------------------. | ||||||||||||||||||||||||||||||||||||||||||||||
| 87 | | Class name | table | | ||||||||||||||||||||||||||||||||||||||||||||||
| 88 | |-----------------------|-------------| | ||||||||||||||||||||||||||||||||||||||||||||||
| 89 | | App::Model::User | users | | ||||||||||||||||||||||||||||||||||||||||||||||
| 90 | | App::Model::Users | users | | ||||||||||||||||||||||||||||||||||||||||||||||
| 91 | | App::Model::Group | groups | | ||||||||||||||||||||||||||||||||||||||||||||||
| 92 | | App::Model::UserAgent | user_agents | | ||||||||||||||||||||||||||||||||||||||||||||||
| 93 | '-------------------------------------' | ||||||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||||||
| 95 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||||||
| 97 | =head2 db | ||||||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||||||
| 99 | $db = $self->db; | ||||||||||||||||||||||||||||||||||||||||||||||
| 100 | $self->db($db_obj); | ||||||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||||||
| 102 | Need to hold either a L<Mojo::Pg::Database> or L<Mojo::mysql::Database> object. | ||||||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||||||
| 104 | =head2 in_storage | ||||||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||||||
| 106 | $bool = $self->in_storage; | ||||||||||||||||||||||||||||||||||||||||||||||
| 107 | $self = $self->in_storage($bool); | ||||||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||||||
| 109 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | ▶ | has db => sub { die "'db' is required in constructor." }; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 5 | has in_storage => 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||||||
| 114 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||||||
| 116 | =head2 expand_sql | ||||||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||||||
| 118 | ($sql, @args) = $self->expand_sql($sql, @args); | ||||||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||||||
| 120 | Used to expand a given C<$sql> statement with variables defined by helpers. | ||||||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||||||
| 122 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||||||
| 124 | =item * %t | ||||||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||||||
| 126 | Will be replaced by L</table>. Example: "SELECT * FROM %t" becomes "SELECT * FROM users". | ||||||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||||||
| 128 | =item * %c | ||||||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||||||
| 130 | Will be replaced by L</columns>. Example: "name,email". | ||||||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||||||
| 132 | =item * %c= | ||||||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||||||
| 134 | Will be replaced by L</columns> assignment. Example: "name=?,email=?" | ||||||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||||||
| 136 | =item * %c? | ||||||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||||||
| 138 | Will be replaced by L</columns> placeholders. Example: "?,?,?" | ||||||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||||||
| 140 | =item * %pc | ||||||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||||||
| 142 | Include L</pk> in list of columns. Example: "id,name,email". | ||||||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||||||
| 144 | =item * \%c | ||||||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||||||
| 146 | Becomes a literal "%c". | ||||||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||||||
| 148 | =back | ||||||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||||||
| 150 | It is also possible to define aliases for "%t", "%c", "%c=" and "%pc". Example: | ||||||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||||||
| 152 | %t.x = some_table as x | ||||||||||||||||||||||||||||||||||||||||||||||
| 153 | %c.x = x.col1 | ||||||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||||||
| 155 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||||||
| 157 | 0 | ▶ | sub expand_sql { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
expand_sql: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 158 | 0 | my ($self, $sql, @args) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 159 | 0 | my $p; | |||||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||||||
| 161 | 0 | ▶ | $sql =~ s|(?<!\\)\%c(?:\.(\w+))?\=|{$p = $1 ? "$1." : ""; join ',', map {"$p$_=?"} $self->columns}|ge; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 0 | $sql =~ s|(?<!\\)\%c\?|{join ',', map {"?"} $self->columns}|ge; | |||||||||||||||||||||||||||||||||||||||||||||
| 163 | 0 | ▶ | $sql =~ s|(?<!\\)\%c(?:\.(\w+))?|{$p = $1 ? "$1." : ""; join ',', map {"$p$_"} $self->columns}|ge; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 0 | ▶ | $sql =~ s|(?<!\\)\%pc(?:\.(\w+))?|{$p = $1 ? "$1." : ""; join ',', map {"$p$_"} $self->pk, $self->columns}|ge; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 165 | 0 | ▶ | $sql =~ s|(?<!\\)\%t(?:\.(\w+))?|{$self->table. ($1 ? " $1" : "")}|ge; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 166 | 0 | $sql =~ s|\\%|%|g; | |||||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 0 | return $sql, @args; | |||||||||||||||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||||||
| 171 | =head2 expand_sst | ||||||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||||||
| 173 | DEPRECATED in favor of L</expand_sql>. | ||||||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||||||
| 175 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||||||
| 177 | 0 | ▶ | sub expand_sst { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
expand_sst: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 178 | 0 | Mojo::Util::deprecated("expand_sst() is deprecated in favor of expand_sql()"); | |||||||||||||||||||||||||||||||||||||||||||||
| 179 | 0 | shift->expand_sql(@_); | |||||||||||||||||||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||||||
| 182 | =head2 columns | ||||||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||||||
| 184 | @str = $self->columns; | ||||||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||||||
| 186 | Returns a list of columns, defined by L</col>. | ||||||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||||||
| 188 | =head2 delete | ||||||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||||||
| 190 | $self = $self->delete; | ||||||||||||||||||||||||||||||||||||||||||||||
| 191 | $self = $self->delete(sub { my ($self, $err) = @_, ... }); | ||||||||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||||||
| 193 | Will delete the object from database if L</in_storage>. | ||||||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||||||
| 195 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||||||
| 197 | 0 | ▶ | sub delete { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
delete: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 198 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 199 | 0 | ▶ | $self->_delete(@_) if $self->in_storage; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 200 | 0 | $self; | |||||||||||||||||||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||||||
| 203 | =head2 fresh | ||||||||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||||||||
| 205 | $self = $self->fresh; | ||||||||||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||||||||||
| 207 | Will mark the next relationship accessor to fetch new data from database, | ||||||||||||||||||||||||||||||||||||||||||||||
| 208 | instead of using the cached data on C<$self>. | ||||||||||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||||||||||
| 210 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||||||||||||||
| 212 | 0 | ▶ | sub fresh { $_[0]->{fresh}++; $_[0] } | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
fresh: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||||||||||||
| 214 | =head2 load | ||||||||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||||||
| 216 | $self = $self->load; | ||||||||||||||||||||||||||||||||||||||||||||||
| 217 | $self = $class->load(sub { my ($self, $err) = @_; }); | ||||||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||||||
| 219 | Used to fetch data from storage and update the object attributes. | ||||||||||||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||||||||||||
| 221 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||||||
| 223 | 0 | ▶ | sub load { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
load: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 225 | 0 | $self->_find(@_); | |||||||||||||||||||||||||||||||||||||||||||||
| 226 | 0 | $self; | |||||||||||||||||||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||||||||||||
| 229 | =head2 save | ||||||||||||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||||||||||||
| 231 | $self = $self->save; | ||||||||||||||||||||||||||||||||||||||||||||||
| 232 | $self = $self->save(sub { my ($self, $err) = @_, ... }); | ||||||||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||||||
| 234 | Will update the object in database if L</in_storage> or insert it if not. | ||||||||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||||||
| 236 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||||||||||||||
| 238 | 0 | ▶ | sub save { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
save: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 239 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 240 | 0 | ▶ | $self->in_storage ? $self->_update(@_) : $self->_insert(@_); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 241 | 0 | $self; | |||||||||||||||||||||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||||||
| 244 | =head2 import | ||||||||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||||||
| 246 | Will set up the caller class with L<Mad::Mapper> functionality if "-base" | ||||||||||||||||||||||||||||||||||||||||||||||
| 247 | is given as argument. See L</SYNOPSIS> for example. | ||||||||||||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||||||||||||
| 249 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||||||
| 251 | # Most of this code is copy/paste from Mojo::Base | ||||||||||||||||||||||||||||||||||||||||||||||
| 252 | 10 | ▶ | sub import { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
import: called CC=8 SCAR=24.2
| |||||||||||||||||||||||||||||||||||||||||||||||
| 253 | 10 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 254 | 10 | ▶ | return unless my $flag = shift; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||||||||||||||
| 256 | 6 | ▶ | if ($flag eq '-base') { $flag = $class } | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 257 | 0 | elsif ($flag eq '-strict') { $flag = undef } | |||||||||||||||||||||||||||||||||||||||||||||
| 258 | ▶ | elsif ((my $file = $flag) && !$flag->can('new')) { | |||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
my $file = $flag and not $flag->can("new")
MC/DC0%0/2
my $file = $flag and not $flag->can("new")
my $file = $flag
not $flag->can("new")
Truth table0%0/3
A and not(B)
A= my $file = $flagB= not $flag->can("new")
| |||||||||||||||||||||||||||||||||||||||||||||||
| 259 | 0 | $file =~ s!::|'!/!g; | |||||||||||||||||||||||||||||||||||||||||||||
| 260 | 0 | require "$file.pm"; | |||||||||||||||||||||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||||||||||
| 263 | 6 | ▶ | if ($flag) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 264 | 6 | my $caller = caller; | |||||||||||||||||||||||||||||||||||||||||||||
| 265 | 6 | my $table = Mojo::Util::decamelize((split /::/, $caller)[-1]); | |||||||||||||||||||||||||||||||||||||||||||||
| 266 | 6 | $table =~ s!s?$!s!; # user => users | |||||||||||||||||||||||||||||||||||||||||||||
| 267 | 6 | ▶ | Mojo::Util::monkey_patch($caller, col => sub { $caller->_define_col(@_) }); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
col: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 268 | 0 | ▶ | Mojo::Util::monkey_patch($caller, columns => sub { @{$COLUMNS{$caller} || []} }); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=9.9
Subroutine
columns: called CC=2 SCAR=9.9
Condition25%1/4
$COLUMNS{$caller} || []
$COLUMNS{$caller} || []
MC/DC0%0/1
$COLUMNS{$caller} || []
$COLUMNS{$caller}
MC/DC0%0/1
$COLUMNS{$caller} || []
$COLUMNS{$caller}
Truth table0%0/2
A
A= $COLUMNS{$caller}
Truth table50%1/2
A
A= $COLUMNS{$caller}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 269 | 0 | ▶ | Mojo::Util::monkey_patch($caller, has => sub { Mojo::Base::attr($caller, @_) }); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
has: called CC=1 SCAR=0.4
Subroutine
__ANON__: not called CC=1 SCAR=0.4
| |||||||||||||||||||||||||||||||||||||||||||||||
| 270 | 0 | ▶ | Mojo::Util::monkey_patch($caller, has_many => sub { $caller->_define_has_many(@_) }); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.4
| |||||||||||||||||||||||||||||||||||||||||||||||
| 271 | Mojo::Util::monkey_patch($caller, | ||||||||||||||||||||||||||||||||||||||||||||||
| 272 | 6 | ▶ | pk => sub { return UNIVERSAL::isa($_[0], $caller) ? $PK{$caller} : $caller->_define_pk(@_) }); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 273 | 0 | ▶ | Mojo::Util::monkey_patch($caller, table => sub { $table = $_[0] unless UNIVERSAL::isa($_[0], $caller); $table }); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=8.5
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 274 | 5 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 275 | 6 | push @{"${caller}::ISA"}, $flag; | |||||||||||||||||||||||||||||||||||||||||||||
| 276 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||||||||||||
| 278 | 6 | $_->import for qw(strict warnings utf8); | |||||||||||||||||||||||||||||||||||||||||||||
| 279 | 6 | feature->import(':5.10'); | |||||||||||||||||||||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||||||||||||||||
| 282 | 0 | ▶ | sub _delete { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_delete: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 283 | 0 | my ($self, $cb) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 284 | 0 | my @sql = $self->_delete_sql; | |||||||||||||||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||||||||||||||||
| 286 | 0 | warn "[Mad::Mapper::delete] ", Mojo::JSON::encode_json(\@sql), "\n" if DEBUG; | |||||||||||||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||||||||||||||
| 288 | 0 | ▶ | if ($cb) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 289 | 0 | weaken $self; | |||||||||||||||||||||||||||||||||||||||||||||
| 290 | $self->db->query( | ||||||||||||||||||||||||||||||||||||||||||||||
| 291 | @sql, | ||||||||||||||||||||||||||||||||||||||||||||||
| 292 | 0 | ▶ | sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 293 | 0 | my ($db, $err, $res) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 294 | 0 | warn "[Mad::Mapper::delete] err=$err\n" if DEBUG and $err; | |||||||||||||||||||||||||||||||||||||||||||||
| 295 | 0 | ▶ | $self->in_storage(0) unless $err; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 296 | 0 | $self->$cb($err); | |||||||||||||||||||||||||||||||||||||||||||||
| 297 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 298 | 0 | ); | |||||||||||||||||||||||||||||||||||||||||||||
| 299 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 300 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 301 | 0 | $self->db->query(@sql); | |||||||||||||||||||||||||||||||||||||||||||||
| 302 | 0 | $self->in_storage(0); | |||||||||||||||||||||||||||||||||||||||||||||
| 303 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 304 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||||||||||||||||
| 306 | 0 | ▶ | sub _delete_sql { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_delete_sql: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 307 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 308 | 0 | my $pk = $self->_pk_or_first_column; | |||||||||||||||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||||||||||||||
| 310 | 0 | $self->expand_sql("DELETE FROM %t WHERE $pk=?"), $self->$pk; | |||||||||||||||||||||||||||||||||||||||||||||
| 311 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||||||||||||||||
| 313 | 0 | ▶ | sub _delete_sst { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_delete_sst: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 314 | 0 | Mojo::Util::deprecated("_delete_sst() is deprecated in favor of _delete_sql()"); | |||||||||||||||||||||||||||||||||||||||||||||
| 315 | 0 | shift->_delete_sql(@_); | |||||||||||||||||||||||||||||||||||||||||||||
| 316 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||||||||||||||||
| 318 | 11 | ▶ | sub _define_col { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_define_col: called CC=3 SCAR=12.7
| |||||||||||||||||||||||||||||||||||||||||||||||
| 319 | 11 | ▶ | my $class = ref($_[0]) || $_[0]; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
ref $_[0] || $_[0]
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0]
$_[0]
Truth table33%1/3
A || B
A= ref $_[0]B= $_[0]
| |||||||||||||||||||||||||||||||||||||||||||||||
| 320 | 0 | ▶ | push @{$COLUMNS{$class}}, ref $_[0] eq 'ARRAY' ? @{$_[1]} : $_[1]; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 321 | 11 | Mojo::Base::attr(@_); | |||||||||||||||||||||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||||||||||||||||
| 324 | 8 | ▶ | sub _define_has_many { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_define_has_many: called CC=1 SCAR=5.8
| |||||||||||||||||||||||||||||||||||||||||||||||
| 325 | 8 | my ($class, $method, $related_class, $related_col) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 326 | 8 | my $pk = $class->_pk_or_first_column; | |||||||||||||||||||||||||||||||||||||||||||||
| 327 | 8 | my $sql_method = $class->can("_has_many_${method}_sql"); | |||||||||||||||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||||||||||||||
| 329 | Mojo::Util::monkey_patch( | ||||||||||||||||||||||||||||||||||||||||||||||
| 330 | 0 | ▶ | $class => $method => sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
groups: not called CC=12 SCAR=50.5
| |||||||||||||||||||||||||||||||||||||||||||||||
| 331 | 0 | ▶ | my $cb = ref $_[-1] eq 'CODE' ? pop : undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 332 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 333 | 0 | ▶ | my $err = $LOADED{$related_class}++ ? 0 : load_class $related_class; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 334 | 0 | my $fresh = delete $self->{fresh}; | |||||||||||||||||||||||||||||||||||||||||||||
| 335 | 0 | ▶ | my $ck = join ':', $method, grep { $_ // '' } @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/5
$_ // ""
$_ // ""
MC/DC0%0/1
$_ // ""
$_
MC/DC0%0/1
$_ // ""
$_
Truth table0%0/2
A
A= $_
Truth table0%0/2
A
A= $_
| |||||||||||||||||||||||||||||||||||||||||||||||
| 336 | 0 | my @sql; | |||||||||||||||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||||||||||||||
| 338 | 0 | ▶ | die ref $err ? "Exception: $err" : "Could not find class $related_class!" if $err; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||||||||||||||||||||
| 340 | @sql | ||||||||||||||||||||||||||||||||||||||||||||||
| 341 | 0 | ▶ | = $sql_method | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 342 | ? $self->$sql_method($related_class, @_) | ||||||||||||||||||||||||||||||||||||||||||||||
| 343 | : $related_class->expand_sql("SELECT %pc FROM %t WHERE $related_col=?", $self->$pk); | ||||||||||||||||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||||||||||||||||
| 345 | warn sprintf "[Mad::Mapper::has_many::$method] %s\n", | ||||||||||||||||||||||||||||||||||||||||||||||
| 346 | 0 | (!$fresh and $self->{cache}{$ck}) ? 'CACHED' : Mojo::JSON::encode_json(\@sql) | |||||||||||||||||||||||||||||||||||||||||||||
| 347 | if DEBUG; | ||||||||||||||||||||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||||||||||||||||||||
| 349 | 0 | ▶ | if ($cb) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 350 | 0 | ▶ | if ($fresh or !$self->{cache}{$ck}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$fresh or not $self->{"cache"}{$ck}
$fresh or not $self->{"cache"}{$ck}
MC/DC0%0/2
$fresh or not $self->{"cache"}{$ck}
$fresh
not $self->{"cache"}{$ck}
MC/DC0%0/2
$fresh or not $self->{"cache"}{$ck}
$fresh
not $self->{"cache"}{$ck}
Truth table0%0/3
A or not(B)
A= $freshB= not $self->{"cache"}{$ck}
Truth table0%0/3
A or not(B)
A= $freshB= not $self->{"cache"}{$ck}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 351 | $self->db->query( | ||||||||||||||||||||||||||||||||||||||||||||||
| 352 | @sql, | ||||||||||||||||||||||||||||||||||||||||||||||
| 353 | 0 | ▶ | sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 354 | 0 | my ($db, $err, $res) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 355 | 0 | warn "[Mad::Mapper::has_many::$method] err=$err\n" if DEBUG and $err; | |||||||||||||||||||||||||||||||||||||||||||||
| 356 | 0 | ▶ | $self->{cache}{$ck} = $res->hashes->map(sub { $related_class->new($_)->in_storage(1) }); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 357 | 0 | $self->$cb($err, $self->{cache}{$ck}); | |||||||||||||||||||||||||||||||||||||||||||||
| 358 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 359 | 0 | ); | |||||||||||||||||||||||||||||||||||||||||||||
| 360 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 361 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 362 | 0 | $self->$cb('', $self->{cache}{$ck}); | |||||||||||||||||||||||||||||||||||||||||||||
| 363 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 364 | 0 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||
| 365 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 366 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 367 | 0 | ▶ | delete $self->{cache}{$ck} if $fresh; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 368 | return $self->{cache}{$ck} | ||||||||||||||||||||||||||||||||||||||||||||||
| 369 | 0 | ▶ | ||= $self->db->query(@sql)->hashes->map(sub { $related_class->new($_)->in_storage(1) }); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Condition0%0/3
$self->{"cache"}{$ck} ||= $self->db->query(@sql)->hashes->map(sub {
$related_class->new($_)->in_storage(1);
}
)
MC/DC0%0/2
$self->{"cache"}{$ck} ||= $self->db->query(@sql)->hashes->map(sub {
$related_class->new($_)->in_storage(1);
}
)
$self->{"cache"}{$ck}
$self->db->query(@sql)->hashes->map(sub {
$related_class->new($_)->in_storage(1);
}
)
Truth table0%0/3
A ||= B
A= $self->{"cache"}{$ck}B= $self->db->query(@sql)->hashes->map(sub {
$related_class->new($_)->in_storage(1);
}
)
| |||||||||||||||||||||||||||||||||||||||||||||||
| 370 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 371 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 372 | 8 | ); | |||||||||||||||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||||||||||||||||
| 374 | 8 | my $add_method = "add_$method"; | |||||||||||||||||||||||||||||||||||||||||||||
| 375 | 8 | $add_method =~ s!s?$!!; | |||||||||||||||||||||||||||||||||||||||||||||
| 376 | Mojo::Util::monkey_patch( | ||||||||||||||||||||||||||||||||||||||||||||||
| 377 | 0 | ▶ | $class => $add_method => sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 378 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 379 | 0 | ▶ | my $err = $LOADED{$related_class}++ ? 0 : load_class $related_class; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 380 | 0 | $related_class->new(db => $self->db, @_, $related_col => $self->$pk); | |||||||||||||||||||||||||||||||||||||||||||||
| 381 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 382 | 8 | ); | |||||||||||||||||||||||||||||||||||||||||||||
| 383 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||||||||||||||||||||
| 385 | 6 | ▶ | sub _define_pk { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_define_pk: called CC=2 SCAR=7.6
| |||||||||||||||||||||||||||||||||||||||||||||||
| 386 | 6 | ▶ | my $class = ref($_[0]) || $_[0]; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
ref $_[0] || $_[0]
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0]
$_[0]
Truth table33%1/3
A || B
A= ref $_[0]B= $_[0]
| |||||||||||||||||||||||||||||||||||||||||||||||
| 387 | 6 | $PK{$class} = $_[1]; | |||||||||||||||||||||||||||||||||||||||||||||
| 388 | 6 | Mojo::Base::attr(@_); | |||||||||||||||||||||||||||||||||||||||||||||
| 389 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||||||||||||||||||||
| 391 | 0 | ▶ | sub _find { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_find: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 392 | 0 | my ($self, $cb) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 393 | 0 | my @sql = $self->_find_sql; | |||||||||||||||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||||||||||||||||
| 395 | 0 | warn "[Mad::Mapper::find] ", Mojo::JSON::encode_json(\@sql), "\n" if DEBUG; | |||||||||||||||||||||||||||||||||||||||||||||
| 396 | 0 | ▶ | if ($cb) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 397 | 0 | weaken $self; | |||||||||||||||||||||||||||||||||||||||||||||
| 398 | $self->db->query( | ||||||||||||||||||||||||||||||||||||||||||||||
| 399 | @sql, | ||||||||||||||||||||||||||||||||||||||||||||||
| 400 | 0 | ▶ | sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||||||||||||||||||||
| 401 | 0 | my ($db, $err, $res) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 402 | 0 | warn "[Mad::Mapper::find] err=$err\n" if DEBUG and $err; | |||||||||||||||||||||||||||||||||||||||||||||
| 403 | 0 | ▶ | $res = $err ? {} : $res->hash || {}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$res->hash or {}
MC/DC0%0/1
$res->hash or {}
$res->hash
Truth table0%0/2
A
A= $res->hash
| |||||||||||||||||||||||||||||||||||||||||||||||
| 404 | 0 | ▶ | $self->in_storage(1) if %$res and !$err; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
%$res and not $err
MC/DC0%0/2
%$res and not $err
%$res
not $err
Truth table0%0/3
A and not(B)
A= %$resB= not $err
| |||||||||||||||||||||||||||||||||||||||||||||||
| 405 | 0 | $self->{$_} = $res->{$_} for keys %$res; | |||||||||||||||||||||||||||||||||||||||||||||
| 406 | 0 | $self->$cb($err); | |||||||||||||||||||||||||||||||||||||||||||||
| 407 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 408 | 0 | ); | |||||||||||||||||||||||||||||||||||||||||||||
| 409 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 410 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 411 | 0 | ▶ | my $res = $self->db->query(@sql)->hash || {}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->db->query(@sql)->hash || {}
MC/DC0%0/1
$self->db->query(@sql)->hash || {}
$self->db->query(@sql)->hash
Truth table0%0/2
A
A= $self->db->query(@sql)->hash
| |||||||||||||||||||||||||||||||||||||||||||||||
| 412 | 0 | ▶ | $self->in_storage(1) if keys %$res; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 413 | 0 | $self->{$_} = $res->{$_} for keys %$res; | |||||||||||||||||||||||||||||||||||||||||||||
| 414 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 415 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||||||||||||||||||||
| 417 | 0 | ▶ | sub _find_sql { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_find_sql: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 418 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 419 | 0 | my $pk = $self->_pk_or_first_column; | |||||||||||||||||||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||||||||||||||||||||
| 421 | 0 | $self->expand_sql("SELECT %pc FROM %t WHERE $pk=?"), $self->$pk; | |||||||||||||||||||||||||||||||||||||||||||||
| 422 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||||||||||||||||||||
| 424 | 0 | ▶ | sub _find_sst { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_find_sst: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 425 | 0 | Mojo::Util::deprecated("_find_sst() is deprecated in favor of _find_sql()"); | |||||||||||||||||||||||||||||||||||||||||||||
| 426 | 0 | shift->_find_sql(@_); | |||||||||||||||||||||||||||||||||||||||||||||
| 427 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||||||||||||||||||||
| 429 | 0 | ▶ | sub _insert { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_insert: not called CC=8 SCAR=42.8
| |||||||||||||||||||||||||||||||||||||||||||||||
| 430 | 0 | my ($self, $cb) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 431 | 0 | my $pk = $self->_pk_or_first_column; | |||||||||||||||||||||||||||||||||||||||||||||
| 432 | 0 | my $db = $self->db; | |||||||||||||||||||||||||||||||||||||||||||||
| 433 | 0 | my @sql = $self->_insert_sql; | |||||||||||||||||||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||||||||||||||||||||
| 435 | 0 | warn "[Mad::Mapper::insert] ", Mojo::JSON::encode_json(\@sql), "\n" if DEBUG; | |||||||||||||||||||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||||||||||||||||||||
| 437 | 0 | ▶ | if ($cb) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 438 | 0 | weaken $self; | |||||||||||||||||||||||||||||||||||||||||||||
| 439 | $db->query( | ||||||||||||||||||||||||||||||||||||||||||||||
| 440 | @sql, | ||||||||||||||||||||||||||||||||||||||||||||||
| 441 | 0 | ▶ | sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||||||||||||||||||||
| 442 | 0 | my ($db, $err, $res) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 443 | 0 | warn "[Mad::Mapper::insert] err=$err\n" if DEBUG and $err; | |||||||||||||||||||||||||||||||||||||||||||||
| 444 | 0 | ▶ | $res = eval { $res->hash } || {}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
eval {
do {
$res->hash
}
} || {}
MC/DC0%0/1
eval {
do {
$res->hash
}
} || {}
eval {
do {
$res->hash
}
}
Truth table0%0/2
A
A= eval {
do {
$res->hash
}
}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||||||||||||||||||||
| 446 | 0 | ▶ | if ($pk) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 447 | 0 | ▶ | $res->{$pk} ||= $db->dbh->last_insert_id(undef, undef, $self->table, $self->pk); | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$res->{$pk} ||= $db->dbh->last_insert_id(undef, undef, $self->table, $self->pk)
MC/DC0%0/2
$res->{$pk} ||= $db->dbh->last_insert_id(undef, undef, $self->table, $self->pk)
$res->{$pk}
$db->dbh->last_insert_id(undef, undef, $self->table, $self->pk)
Truth table0%0/3
A ||= B
A= $res->{$pk}B= $db->dbh->last_insert_id(undef, undef, $self->table, $self->pk)
| |||||||||||||||||||||||||||||||||||||||||||||||
| 448 | 0 | ▶ | $res->{$pk} ||= eval { $res->sth->mysql_insertid }; # can probably be removed | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$res->{$pk} ||= eval {
do {
$res->sth->mysql_insertid
}
}
MC/DC0%0/2
$res->{$pk} ||= eval {
do {
$res->sth->mysql_insertid
}
}
$res->{$pk}
eval {
do {
$res->sth->mysql_insertid
}
}
Truth table0%0/3
A ||= B
A= $res->{$pk}B= eval {
do {
$res->sth->mysql_insertid
}
}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 449 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||||||||||||||||||||
| 451 | 0 | ▶ | $self->in_storage(1) if keys %$res; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 452 | 0 | $self->$_($res->{$_}) for grep { $self->can($_) } keys %$res; | |||||||||||||||||||||||||||||||||||||||||||||
| 453 | 0 | $self->$cb($err); | |||||||||||||||||||||||||||||||||||||||||||||
| 454 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 455 | 0 | ); | |||||||||||||||||||||||||||||||||||||||||||||
| 456 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 457 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 458 | 0 | my $res = $db->query(@sql); | |||||||||||||||||||||||||||||||||||||||||||||
| 459 | 0 | ▶ | $res = eval { $res->hash } || {}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
eval {
do {
$res->hash
}
} || {}
MC/DC0%0/1
eval {
do {
$res->hash
}
} || {}
eval {
do {
$res->hash
}
}
Truth table0%0/2
A
A= eval {
do {
$res->hash
}
}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||||||||||||||||||||
| 461 | 0 | ▶ | if ($pk) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 462 | 0 | ▶ | $res->{$pk} ||= $db->dbh->last_insert_id(undef, undef, $self->table, $self->pk); | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$res->{$pk} ||= $db->dbh->last_insert_id(undef, undef, $self->table, $self->pk)
MC/DC0%0/2
$res->{$pk} ||= $db->dbh->last_insert_id(undef, undef, $self->table, $self->pk)
$res->{$pk}
$db->dbh->last_insert_id(undef, undef, $self->table, $self->pk)
Truth table0%0/3
A ||= B
A= $res->{$pk}B= $db->dbh->last_insert_id(undef, undef, $self->table, $self->pk)
| |||||||||||||||||||||||||||||||||||||||||||||||
| 463 | 0 | ▶ | $res->{$pk} ||= eval { $res->sth->mysql_insertid } # can probably be removed; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$res->{$pk} ||= eval {
do {
$res->sth->mysql_insertid
}
}
MC/DC0%0/2
$res->{$pk} ||= eval {
do {
$res->sth->mysql_insertid
}
}
$res->{$pk}
eval {
do {
$res->sth->mysql_insertid
}
}
Truth table0%0/3
A ||= B
A= $res->{$pk}B= eval {
do {
$res->sth->mysql_insertid
}
}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 464 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||||||||||||||||||||
| 466 | 0 | ▶ | $self->in_storage(1) if keys %$res; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 467 | 0 | $self->$_($res->{$_}) for grep { $self->can($_) } keys %$res; # used with Mojo::Pg and RETURNING | |||||||||||||||||||||||||||||||||||||||||||||
| 468 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 469 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||||||||||||||||||||
| 471 | 0 | ▶ | sub _insert_sql { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_insert_sql: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||||||
| 472 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 473 | 0 | my $pk = $self->pk; | |||||||||||||||||||||||||||||||||||||||||||||
| 474 | 0 | my $sql = "INSERT INTO %t (%c) VALUES (%c?)"; | |||||||||||||||||||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||||||||||||||||||||
| 476 | 0 | ▶ | $sql .= " RETURNING $pk" if $pk and UNIVERSAL::isa($self->db, 'Mojo::Pg::Database'); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$pk and UNIVERSAL::isa($self->db, "Mojo::Pg::Database")
MC/DC0%0/2
$pk and UNIVERSAL::isa($self->db, "Mojo::Pg::Database")
$pk
UNIVERSAL::isa($self->db, "Mojo::Pg::Database")
Truth table0%0/3
A and B
A= $pkB= UNIVERSAL::isa($self->db, "Mojo::Pg::Database")
| |||||||||||||||||||||||||||||||||||||||||||||||
| 477 | 0 | $self->expand_sql($sql), map { $self->$_ } $self->columns; | |||||||||||||||||||||||||||||||||||||||||||||
| 478 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||||||||||||||||||||
| 480 | 0 | ▶ | sub _insert_sst { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_insert_sst: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 481 | 0 | Mojo::Util::deprecated("_insert_sst() is deprecated in favor of _insert_sql()"); | |||||||||||||||||||||||||||||||||||||||||||||
| 482 | 0 | shift->_insert_sql(@_); | |||||||||||||||||||||||||||||||||||||||||||||
| 483 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||||||||||||||||||||
| 485 | 8 | ▶ | sub _pk_or_first_column { $_[0]->pk || ($_[0]->columns)[0] } | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_pk_or_first_column: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||||||||||||||||||||
| 487 | 0 | ▶ | sub _update { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_update: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 488 | 0 | my ($self, $cb) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 489 | 0 | my @sql = $self->_update_sql; | |||||||||||||||||||||||||||||||||||||||||||||
| 490 | |||||||||||||||||||||||||||||||||||||||||||||||
| 491 | 0 | warn "[Mad::Mapper::update] ", Mojo::JSON::encode_json(\@sql), "\n" if DEBUG; | |||||||||||||||||||||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||||||||||||||||||||
| 493 | 0 | ▶ | if ($cb) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 494 | 0 | weaken $self; | |||||||||||||||||||||||||||||||||||||||||||||
| 495 | $self->db->query( | ||||||||||||||||||||||||||||||||||||||||||||||
| 496 | @sql, | ||||||||||||||||||||||||||||||||||||||||||||||
| 497 | 0 | ▶ | sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 498 | 0 | my ($db, $err, $res) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 499 | 0 | warn "[Mad::Mapper::update] err=$err\n" if DEBUG and $err; | |||||||||||||||||||||||||||||||||||||||||||||
| 500 | 0 | $self->$cb($err); | |||||||||||||||||||||||||||||||||||||||||||||
| 501 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 502 | 0 | ); | |||||||||||||||||||||||||||||||||||||||||||||
| 503 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 504 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 505 | 0 | $self->db->query(@sql); | |||||||||||||||||||||||||||||||||||||||||||||
| 506 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 507 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 508 | |||||||||||||||||||||||||||||||||||||||||||||||
| 509 | 0 | ▶ | sub _update_sql { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_update_sql: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 510 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 511 | 0 | my $pk = $self->_pk_or_first_column; | |||||||||||||||||||||||||||||||||||||||||||||
| 512 | |||||||||||||||||||||||||||||||||||||||||||||||
| 513 | 0 | $self->expand_sql("UPDATE %t SET %c= WHERE $pk=?"), (map { $self->$_ } $self->columns), $self->$pk; | |||||||||||||||||||||||||||||||||||||||||||||
| 514 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 515 | |||||||||||||||||||||||||||||||||||||||||||||||
| 516 | 0 | ▶ | sub _update_sst { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_update_sst: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 517 | 0 | Mojo::Util::deprecated("_update_sst() is deprecated in favor of _update_sql()"); | |||||||||||||||||||||||||||||||||||||||||||||
| 518 | 0 | shift->_update_sql(@_); | |||||||||||||||||||||||||||||||||||||||||||||
| 519 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 520 | |||||||||||||||||||||||||||||||||||||||||||||||
| 521 | 1 | ▶ | sub TO_JSON { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
TO_JSON: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 522 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 523 | 1 | my $pk = $self->pk; | |||||||||||||||||||||||||||||||||||||||||||||
| 524 | 1 | ▶ | return {$pk ? ($pk => $self->$pk) : (), map { ($_ => $self->$_) } $self->columns}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 525 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 526 | |||||||||||||||||||||||||||||||||||||||||||||||
| 527 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||||||||||||||
| 528 | |||||||||||||||||||||||||||||||||||||||||||||||
| 529 | Copyright (C) 2014-2016, Jan Henning Thorsen | ||||||||||||||||||||||||||||||||||||||||||||||
| 530 | |||||||||||||||||||||||||||||||||||||||||||||||
| 531 | This program is free software, you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||||||||||||||
| 532 | the terms of the Artistic License version 2.0. | ||||||||||||||||||||||||||||||||||||||||||||||
| 533 | |||||||||||||||||||||||||||||||||||||||||||||||
| 534 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||||||
| 535 | |||||||||||||||||||||||||||||||||||||||||||||||
| 536 | Jan Henning Thorsen - C<jhthorsen@cpan.org> | ||||||||||||||||||||||||||||||||||||||||||||||
| 537 | |||||||||||||||||||||||||||||||||||||||||||||||
| 538 | Красимир Беров - C<berov@cpan.org> | ||||||||||||||||||||||||||||||||||||||||||||||
| 539 | |||||||||||||||||||||||||||||||||||||||||||||||
| 540 | Stefan Adams - C<sadams@cpan.org> | ||||||||||||||||||||||||||||||||||||||||||||||
| 541 | |||||||||||||||||||||||||||||||||||||||||||||||
| 542 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||
| 543 | |||||||||||||||||||||||||||||||||||||||||||||||
| 544 | 5 | 1; | |||||||||||||||||||||||||||||||||||||||||||||