blib/lib/DBIx/FixtureLoader.pm

filter: Statementstmt 27.0% 40 / 148 Branchbran 0.0% 0 / 86 Conditioncond 0.0% 0 / 32 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 36.0% 9 / 25 Podpod 100% 1 / 1 totaltotal 16.0% 50 / 312 SCAR 76.3
CC
57
Cov
15%
_load_fixture_from_data
930.0
load_fixture
210.0
_normalize_data
56.0
CRAP 2049.6
↑ Summary Build »
1 package DBIx::FixtureLoader;
2 7 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 7 our $VERSION = "0.12";
7  
8 7 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use DBIx::TransactionManager;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use File::Basename qw/basename/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 7 ▶use SQL::Maker;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 7 SQL::Maker->load_plugin('InsertMulti');
14 7 SQL::Maker->load_plugin('InsertOnDuplicate');
15  
16 7 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 has dbh => (
19 is => 'ro',
20 0 ▶ isa => sub { shift->isa('DBI::db') },
Subroutine __ANON__: not called CC=1 SCAR=6.9
21 7 required => 1,
22 );
23  
24 has transaction_manager => (
25 is => 'lazy',
26 default => sub {
27 0 ▶ DBIx::TransactionManager->new(shift->dbh);
Subroutine __ANON__: not called CC=1 SCAR=6.9
28 },
29 7 );
30  
31 has bulk_insert => (
32 is => 'lazy',
33 default => sub {
34 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=5 SCAR=34.0
35 0 ▶ return undef if $self->skip_null_column;
Branch: 1 branch
BranchTrueFalseTotal
if $self->skip_null_column 0 0 0
36  
37 0 my $driver_name = $self->_driver_name;
38 0 my $dbh = $self->dbh;
39 $driver_name eq 'mysql' ? 1 :
40 0 ▶ $driver_name eq 'Pg' && $dbh->{ pg_server_version } >= 82000 ? 1 :
Branch: 2 branches
BranchTrueFalseTotal
$driver_name eq 'mysql' ? : 0 0 0
$driver_name eq 'Pg' && $dbh->{'pg_server_version'} >= 82000 ? : 0 0 0
Condition0%0/3
$driver_name eq "Pg" and $dbh->{"pg_server_version"} >= 82000
!ll&&!rl&&r
000
MC/DC0%0/2
$driver_name eq "Pg" and $dbh->{"pg_server_version"} >= 82000
$driver_name eq "Pg" $dbh->{"pg_server_version"} >= 82000
Truth table0%0/3
A and B
A= $driver_name eq "Pg"B= $dbh->{"pg_server_version"} >= 82000
ABresult
0X0
100
111
41 0 ;
42 },
43 7 );
44  
45 has update => (
46 is => 'ro',
47 0 ▶ default => sub { undef },
Subroutine __ANON__: not called CC=1 SCAR=6.9
48 7 );
49  
50 has ignore => (
51 is => 'ro',
52 0 ▶ default => sub { undef },
Subroutine __ANON__: not called CC=1 SCAR=6.9
53 7 );
54  
55 has delete => (
56 is => 'ro',
57 0 ▶ default => sub { undef },
Subroutine __ANON__: not called CC=1 SCAR=6.9
58 7 );
59  
60 has skip_null_column => (
61 is => 'ro',
62 0 ▶ default => sub { undef },
Subroutine __ANON__: not called CC=1 SCAR=6.9
63 7 );
64  
65 has csv_option => (
66 is => 'ro',
67 0 ▶ isa => sub { ref $_[0] eq 'HASH' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
68 0 ▶ default => sub { {} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
69 7 );
70  
71 has _driver_name => (
72 is => 'lazy',
73 default => sub {
74 0 ▶ shift->dbh->{Driver}{Name};
Subroutine __ANON__: not called CC=1 SCAR=6.9
75 },
76 7 );
77  
78 has _sql_builder => (
79 is => 'lazy',
80 default => sub {
81 0 ▶ SQL::Maker->new(
Subroutine __ANON__: not called CC=1 SCAR=6.9
82 driver => shift->_driver_name,
83 );
84 }
85 7 );
86  
87 7 ▶no Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
88  
89 sub load_fixture {
90 0 ▶ my $self = shift;
Subroutine load_fixture: not called CC=14 SCAR=53.5
Pod documented
91 0 my $file = shift;
92 0 ▶ my %opts = ref $_[0] ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
93  
94 0 my $update = $opts{update};
95 0 my $ignore = $opts{ignore};
96 0 ▶ croak '`update` and `ignore` are exclusive argument' if $update && $ignore;
Branch: 1 branch
BranchTrueFalseTotal
if $update and $ignore 0 0 0
Condition0%0/3
$update and $ignore
!ll&&!rl&&r
000
MC/DC0%0/2
$update and $ignore
$update $ignore
Truth table0%0/3
A and B
A= $updateB= $ignore
ABresult
0X0
100
111
97  
98 0 ▶ if (ref($file) =~ /^(?:ARRAY|HASH)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($file) =~ /^(?:ARRAY|HASH)$/) 0 0 0
99 0 return $self->_load_fixture_from_data(data => $file, %opts);
100 }
101  
102 0 my $table = $opts{table};
103 0 ▶ my $format = lc($opts{format} || '');
Condition0%0/2
$opts{'format'} || ''
l!l
00
MC/DC0%0/1
$opts{'format'} || ''
$opts{'format'}
Truth table0%0/2
A
A= $opts{'format'}
Aresult
11
00
104  
105 0 ▶ unless ($table) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($table) 0 0 0
106 0 my $basename = basename($file);
107 0 ($table) = $basename =~ /^([_A-Za-z0-9]+)/;
108 }
109  
110 0 ▶ unless ($format) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($format) 0 0 0
111 0 ($format) = $file =~ /\.([^.]*$)/;
112 }
113  
114 0 my $rows;
115 0 ▶ if ($format eq 'csv' || $format eq 'tsv') {
Branch: 1 branch
BranchTrueFalseTotal
if ($format eq 'csv' or $format eq 'tsv') { } 0 0 0
Condition0%0/3
$format eq "csv" or $format eq "tsv"
l!l&&r!l&&!r
000
MC/DC0%0/2
$format eq "csv" or $format eq "tsv"
$format eq "csv" $format eq "tsv"
Truth table0%0/3
A or B
A= $format eq "csv"B= $format eq "tsv"
ABresult
1X1
011
000
116 0 $rows = $self->_get_data_from_csv($file, $format);
117 }
118 else {
119 0 ▶ if ($format eq 'json') {
Branch: 2 branches
BranchTrueFalseTotal
if ($format eq 'json') { } 0 0 0
elsif ($format =~ /ya?ml/) { } 0 0 0
120 0 require JSON;
121 0 my $content = do {
122 0 local $/;
123 0 ▶ open my $fh, '<', $file or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or die $! 0 0 0
124 0 <$fh>;
125 };
126 0 $rows = JSON::decode_json($content);
127 }
128 elsif ($format =~ /ya?ml/) {
129 0 require YAML::Tiny;
130 0 ▶ $rows = YAML::Tiny->read($file) or croak( YAML::Tiny->errstr );
Branch: 1 branch
BranchTrueFalseTotal
$rows = "YAML::Tiny"->read($file) or croak("YAML::Tiny"->errstr) 0 0 0
131 0 $rows = $rows->[0];
132 }
133 }
134  
135 0 $self->load_fixture($rows,
136 table => $table,
137 %opts,
138 );
139 }
140  
141 sub _get_data_from_csv {
142 0 ▶ my ($self, $file, $format) = @_;
Subroutine _get_data_from_csv: not called CC=5 SCAR=34.0
143 0 require Text::CSV;
144  
145 my $csv = Text::CSV->new({
146 binary => 1,
147 blank_is_undef => 1,
148 sep_char => $format eq 'tsv' ? "\t" : ',',
149 0 ▶ %{ $self->csv_option },
Branch: 2 branches
BranchTrueFalseTotal
my $csv = "Text::CSV"->new({"binary", 1, "blank_is_undef", 1, "sep_char", $format eq "tsv" ? "\t" : ",", %{$self->csv_option;}}) or croak("Text::CSV"->error_diag) 0 0 0
$format eq 'tsv' ? : 0 0 0
150 }) or croak( Text::CSV->error_diag );
151  
152 0 ▶ open my $fh, '<', $file or die "$!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or die "$!" 0 0 0
153 0 my $columns = $csv->getline($fh);
154 0 my @records;
155 0 ▶ while ( my $row = $csv->getline($fh) ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $csv->getline($fh)) 0 0 0
156 0 my %cols = map { $columns->[$_] => $row->[$_] } 0..$#$columns;
157 0 push @records, \%cols;
158 }
159 0 \@records;
160 }
161  
162 sub _load_fixture_from_data {
163 0 ▶ my ($self, %args) = @_;
Subroutine _load_fixture_from_data: not called CC=30 SCAR=68.4
164 0 my ($table, $data) = @args{qw/table data/};
165  
166 0 ▶ croak '`update` and `ignore` are exclusive option' if $args{update} && $args{ignore};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"update"} and $args{"ignore"} 0 0 0
Condition0%0/3
$args{"update"} and $args{"ignore"}
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"update"} and $args{"ignore"}
$args{"update"} $args{"ignore"}
Truth table0%0/3
A and B
A= $args{"update"}B= $args{"ignore"}
ABresult
0X0
100
111
167  
168 0 my $update = $self->update;
169 0 my $ignore = $self->ignore;
170 0 ▶ croak '`update` and `ignore` are exclusive option' if $update && $ignore;
Branch: 1 branch
BranchTrueFalseTotal
if $update and $ignore 0 0 0
Condition0%0/3
$update and $ignore
!ll&&!rl&&r
000
MC/DC0%0/2
$update and $ignore
$update $ignore
Truth table0%0/3
A and B
A= $updateB= $ignore
ABresult
0X0
100
111
171  
172 0 my $bulk_insert = $self->bulk_insert;
173 0 my $skip_null_column = $self->skip_null_column;
174 0 ▶ croak '`bulk_insert` and `skip_null_column` are exclusive option' if $bulk_insert && $skip_null_column;
Branch: 1 branch
BranchTrueFalseTotal
if $bulk_insert and $skip_null_column 0 0 0
Condition0%0/3
$bulk_insert and $skip_null_column
!ll&&!rl&&r
000
MC/DC0%0/2
$bulk_insert and $skip_null_column
$bulk_insert $skip_null_column
Truth table0%0/3
A and B
A= $bulk_insertB= $skip_null_column
ABresult
0X0
100
111
175  
176 # The $args has priority. So default object property is ignored.
177 0 ▶ if (exists $args{update}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"update"}) 0 0 0
178 0 $update = $args{update};
179 0 ▶ $ignore = undef if $update;
Branch: 1 branch
BranchTrueFalseTotal
if $update 0 0 0
180 }
181 0 ▶ if (exists $args{ignore}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"ignore"}) 0 0 0
182 0 $ignore = $args{ignore};
183 0 ▶ $update = undef if $ignore;
Branch: 1 branch
BranchTrueFalseTotal
if $ignore 0 0 0
184 }
185  
186 0 ▶ if ($update && $self->_driver_name ne 'mysql') {
Branch: 1 branch
BranchTrueFalseTotal
if ($update and $self->_driver_name ne "mysql") 0 0 0
Condition0%0/3
$update and $self->_driver_name ne "mysql"
!ll&&!rl&&r
000
MC/DC0%0/2
$update and $self->_driver_name ne "mysql"
$update $self->_driver_name ne "mysql"
Truth table0%0/3
A and B
A= $updateB= $self->_driver_name ne "mysql"
ABresult
0X0
100
111
187 0 croak '`update` option only support mysql'
188 }
189 0 ▶ my $delete = $self->delete || $args{delete};
Condition0%0/3
$self->delete || $args{'delete'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->delete || $args{'delete'}
$self->delete $args{'delete'}
Truth table0%0/3
A || B
A= $self->deleteB= $args{'delete'}
ABresult
1X1
011
000
190  
191 0 $data = $self->_normalize_data($data);
192 0 ▶ return unless @$data;
Branch: 1 branch
BranchTrueFalseTotal
unless @$data 0 0 0
193  
194 0 my $dbh = $self->dbh;
195 # needs limit ?
196 0 ▶ my $txn = $self->transaction_manager->txn_scope or croak $dbh->errstr;
Branch: 1 branch
BranchTrueFalseTotal
my $txn = $self->transaction_manager->txn_scope or croak($dbh->errstr) 0 0 0
197  
198 0 ▶ if ($delete) {
Branch: 1 branch
BranchTrueFalseTotal
if ($delete) 0 0 0
199 0 my ($sql, @binds) = $self->_sql_builder->delete($table);
200 0 $dbh->do($sql, undef, @binds);
201 }
202  
203 0 ▶ my $opt; $opt->{prefix} = 'INSERT IGNORE INTO' if $ignore;
Branch: 1 branch
BranchTrueFalseTotal
if $ignore 0 0 0
204 0 ▶ if ($bulk_insert) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bulk_insert) { } 0 0 0
205 0 ▶ $opt->{update} = _build_on_duplicate(keys %{$data->[0]}) if $update;
Branch: 1 branch
BranchTrueFalseTotal
if $update 0 0 0
206  
207 0 ▶ my ($sql, @binds) = $self->_sql_builder->insert_multi($table, $data, $opt ? $opt : ());
Branch: 1 branch
BranchTrueFalseTotal
$opt ? : 0 0 0
208  
209 0 ▶ $dbh->do( $sql, undef, @binds ) or croak $dbh->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$dbh->do($sql, undef, @binds) or croak($dbh->errstr) 0 0 0
210 }
211 else {
212 0 ▶ my $method = $update ? 'insert_on_duplicate' : 'insert';
Branch: 1 branch
BranchTrueFalseTotal
$update ? : 0 0 0
213 0 for my $row_orig (@$data) {
214 my $row = !$skip_null_column ? $row_orig : {map {
215 0 ▶ defined $row_orig->{$_} ? ($_ => $row_orig->{$_}) : ()
Branch: 2 branches
BranchTrueFalseTotal
!$skip_null_column ? : 0 0 0
defined $row_orig->{$_} ? : 0 0 0
216 } keys %$row_orig};
217 0 ▶ $opt = _build_on_duplicate(keys %$row) if $update;
Branch: 1 branch
BranchTrueFalseTotal
if $update 0 0 0
218 0 ▶ my ($sql, @binds) = $self->_sql_builder->$method($table, $row, $opt ? $opt : ());
Branch: 1 branch
BranchTrueFalseTotal
$opt ? : 0 0 0
219  
220 0 ▶ $dbh->do( $sql, undef, @binds ) or croak $dbh->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$dbh->do($sql, undef, @binds) or croak($dbh->errstr) 0 0 0
221 }
222 }
223 0 ▶ $txn->commit or croak $dbh->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$txn->commit or croak($dbh->errstr) 0 0 0
224 }
225  
226 sub _build_on_duplicate {
227 0 ▶ +{ map {($_ => \"VALUES(`$_`)")} @_ };
Subroutine _build_on_duplicate: not called CC=1 SCAR=6.9
228 }
229  
230 sub _normalize_data {
231 0 ▶ my ($self, $data) = @_;
Subroutine _normalize_data: not called CC=7 SCAR=40.3
232 0 my @ret;
233 0 ▶ if (ref $data eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $data eq 'HASH') { } 0 0 0
elsif (ref $data eq 'ARRAY') { } 0 0 0
234 0 push @ret, $data->{$_} for keys %$data;
235 }
236 elsif (ref $data eq 'ARRAY') {
237 0 ▶ if ($data->[0] && $data->[0]{data} && ref $data->[0]{data} eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->[0] and $data->[0]{'data'} and ref $data->[0]{'data'} eq 'HASH') { } 0 0 0
Condition0%0/6
$data->[0] and $data->[0]{"data"} and ref $data->[0]{"data"} eq "HASH"
!ll&&!rl&&r
000
$data->[0] and $data->[0]{"data"}
!ll&&!rl&&r
000
MC/DC0%0/3
$data->[0] and $data->[0]{"data"} and ref $data->[0]{"data"} eq "HASH"
$data->[0] $data->[0]{"data"} ref $data->[0]{"data"} eq "HASH"
Truth table0%0/4
A and B and C
A= $data->[0]B= $data->[0]{"data"}C= ref $data->[0]{"data"} eq "HASH"
ABCresult
0XX0
10X0
1100
1111
238 0 @ret = map { $_->{data} } @$data;
239 }
240 else {
241 0 @ret = @$data;
242 }
243 }
244 0 \@ret;
245 }
246  
247 7 1;
248 __END__
249  
250 =encoding utf-8
251  
252 =head1 NAME
253  
254 DBIx::FixtureLoader - Loading fixtures and inserting to your database
255  
256 =head1 SYNOPSIS
257  
258 use DBI;
259 use DBIx::FixtureLoader;
260
261 my $dbh = DBI->connect(...);
262 my $loader = DBIx::FixtureLoader->new(dbh => $dbh);
263 $loader->load_fixture('item.csv');
264  
265 =head1 DESCRIPTION
266  
267 DBIx::FixtureLoader is to load fixture data and insert to your database.
268  
269 =head1 INTEFACE
270  
271 =head2 Constructor
272  
273 $loader = DBIx::FixtureLoader->new(%option)
274  
275 C<new> is Constructor method. Various options may be set in C<%option>, which affect
276 the behaviour of the object (Type and defaults in parentheses):
277  
278 =head3 C<< dbh (DBI::db) >>
279  
280 Required. Database handler.
281  
282 =head3 C<< bulk_insert (Bool) >>
283  
284 Using bulk_insert or not. Default value depends on your database.
285  
286 =head3 C<< update (Bool, Default: false) >>
287  
288 Using C<< INSERT ON DUPLICATE >> or not. It only works on MySQL.
289  
290 =head3 C<< ignore (Bool, Default: false) >>
291  
292 Using C<< INSERT IGNORE >> or not. This option is exclusive with C<update>.
293  
294 =head3 C<< delete (Bool, Default: false) >>
295  
296 DELETE all data from table before inserting or not.
297  
298 =head3 C<< csv_option (HashRef, Default: +{}) >>
299  
300 Specifying L<Text::CSV>'s option. C<binary> and C<blank_is_undef>
301 are automatically set.
302  
303 =head3 C<< skip_null_column (Bool, Default: false) >>
304  
305 If true, null data is not to be inserted or updated explicitly. It it for using default value.
306  
307 NOTE: If this option is true, data can't be overwritten by null value.
308  
309 =head2 Methods
310  
311 =head3 C<< $loader->load_fixture($file_or_data:(Str|HashRef|ArrayRef), [%option]) >>
312  
313 Loading fixture and inserting to your database. Table name and file format is guessed from
314 file name. For example, "item.csv" contains data of "item" table and format is "CSV".
315  
316 In most cases C<%option> is not needed. Available keys of C<%option> are as follows.
317  
318 =over
319  
320 =item C<table:Str>
321  
322 table name of database.
323  
324 =item C<format:Str>
325  
326 data format. "CSV", "YAML" and "JSON" are available.
327  
328 =item C<update:Bool>
329  
330 Using C<< ON DUPLICATE KEY UPDATE >> or not. Default value depends on object setting.
331  
332 =item C<< ignore:Bool >>
333  
334 Using C<< INSERT IGNORE >> or not.
335  
336 =item C<< delete:Bool >>
337  
338 DELETE all data from table before inserting or not.
339  
340 =back
341  
342 =head2 File Name and Data Format
343  
344 =head3 file name
345  
346 Data format is guessed from extension. Table name is guessed from basename. Leading alphabets,
347 underscores and numbers are considered table name. So, C<"user_item-2.csv"> is considered CSV format
348 and containing data of "user_item" table.
349  
350 =head3 data format
351  
352 "CSV", "YAML" and "JSON" are parsable. CSV file must have header line for determining column names.
353  
354 Datas in "YAML" or "JSON" must be ArrayRef or HashRef containing HashRefs. Each HashRef is the data
355 of database record and keys of HashRef is matching to column names of the table.
356  
357 =head1 LICENSE
358  
359 Copyright (C) Masayuki Matsuki.
360  
361 This library is free software; you can redistribute it and/or modify
362 it under the same terms as Perl itself.
363  
364 =head1 AUTHOR
365  
366 Masayuki Matsuki E<lt>y.songmu@gmail.comE<gt>
367  
368 =cut
↑ Summary Build »