| 1 | package DBIx::Insert::Multi::Batch; | ||||||||||
| 2 | 2 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | |||||||||||
| 4 | =head1 NAME | ||||||||||
| 5 | |||||||||||
| 6 | DBIx::Insert::Multi::Batch -- Batch of records to insert | ||||||||||
| 7 | |||||||||||
| 8 | =cut | ||||||||||
| 9 | |||||||||||
| 10 | 2 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 2 | ▶ | use autobox::Core; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 2 | ▶ | use autobox::Transform; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 2 | ▶ | use Scalar::Util qw/ blessed /; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | |||||||||||
| 15 | |||||||||||
| 16 | |||||||||||
| 17 | 2 | has dbh => ( is => "ro", required => 1 ); | |||||||||
| 18 | 2 | has table => ( is => "ro", required => 1 ); | |||||||||
| 19 | 2 | has records => ( is => "ro", required => 1 ); | |||||||||
| 20 | 2 | has insert_sql_fragment => ( is => "ro", required => 1 ); | |||||||||
| 21 | 2 | has is_last_insert_id_required => ( is => "ro", required => 1 ); | |||||||||
| 22 | |||||||||||
| 23 | |||||||||||
| 24 | |||||||||||
| 25 | 2 | has column_names => ( is => "lazy" ); | |||||||||
| 26 | 3 | ▶ | sub _build_column_names { | ||||||||
|
Subroutine
_build_column_names: called CC=2 SCAR=7.1
| |||||||||||
| 27 | 3 | my $self = shift; | |||||||||
| 28 | 3 | ▶ | my $record = $self->records->[0] or return []; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 29 | 3 | return $record->keys->order, # Assume all records are uniform | |||||||||
| 30 | } | ||||||||||
| 31 | |||||||||||
| 32 | 2 | has record_placeholders => ( is => "lazy" ); | |||||||||
| 33 | 2 | ▶ | sub _build_record_placeholders { | ||||||||
|
Subroutine
_build_record_placeholders: called CC=1 SCAR=0.0
| |||||||||||
| 34 | 2 | my $self = shift; | |||||||||
| 35 | 2 | ▶ | my $record = " (" . $self->column_names->map(sub { "?" })->join(", ") . ")"; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 36 | 2 | ▶ | return $self->records->map(sub { $record })->join(",\n") . "\n"; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 37 | } | ||||||||||
| 38 | |||||||||||
| 39 | 2 | has record_values => ( is => "lazy" ); | |||||||||
| 40 | 1 | ▶ | sub _build_record_values { | ||||||||
|
Subroutine
_build_record_values: called CC=1 SCAR=0.0
| |||||||||||
| 41 | 1 | my $self = shift; | |||||||||
| 42 | 1 | my @columns = $self->column_names->elements; | |||||||||
| 43 | return $self->records | ||||||||||
| 44 | 2 | ▶ | ->map(sub { @{ $_ }{ @columns } }) | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 45 | 1 | ▶ | ->map(sub { blessed($_) ? "$_" : $_ }); # Stringify objects | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||
| 46 | } | ||||||||||
| 47 | |||||||||||
| 48 | 2 | has sql => ( is => "lazy" ); | |||||||||
| 49 | 1 | ▶ | sub _build_sql { | ||||||||
|
Subroutine
_build_sql: called CC=2 SCAR=7.0
| |||||||||||
| 50 | 1 | my $self = shift; | |||||||||
| 51 | 1 | ▶ | $self->column_names->length or return undef; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | |||||||||||
| 53 | 1 | my $table = $self->table; | |||||||||
| 54 | 1 | my $dbh = $self->dbh; | |||||||||
| 55 | my $column_names = $self->column_names | ||||||||||
| 56 | 3 | ▶ | ->map(sub { $dbh->quote_identifier($_) }) | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 57 | 1 | ->join(", "); | |||||||||
| 58 | |||||||||||
| 59 | 1 | return sprintf( | |||||||||
| 60 | "%s %s (%s) VALUES\n%s", | ||||||||||
| 61 | $self->insert_sql_fragment, | ||||||||||
| 62 | $dbh->quote_identifier($table), | ||||||||||
| 63 | $column_names, | ||||||||||
| 64 | $self->record_placeholders, | ||||||||||
| 65 | ); | ||||||||||
| 66 | } | ||||||||||
| 67 | |||||||||||
| 68 | # For MySQL this is the id of the first of the rows | ||||||||||
| 69 | # For Postgres, this seems to be the id of the last of the rows | ||||||||||
| 70 | 0 | ▶ | sub _get_insert_id { | ||||||||
|
Subroutine
_get_insert_id: not called CC=3 SCAR=24.8
| |||||||||||
| 71 | 0 | my $self = shift; | |||||||||
| 72 | 0 | my ($dbh) = @_; | |||||||||
| 73 | |||||||||||
| 74 | 0 | my $last_insert_id = $dbh->last_insert_id( | |||||||||
| 75 | undef, undef, $self->table, undef, | ||||||||||
| 76 | ); | ||||||||||
| 77 | # This will return 0 if any of the inserts failed and you were | ||||||||||
| 78 | # using ->insert_sql_fragment "INSERT IGNORE INTO". | ||||||||||
| 79 | 0 | ▶ | if( ! defined $last_insert_id ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 80 | 0 | ▶ | if($self->is_last_insert_id_required) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 81 | 0 | croak("No dbh last_insert_id returned"); | |||||||||
| 82 | } | ||||||||||
| 83 | 0 | return undef; | |||||||||
| 84 | } | ||||||||||
| 85 | |||||||||||
| 86 | 0 | return $last_insert_id; | |||||||||
| 87 | } | ||||||||||
| 88 | |||||||||||
| 89 | =head2 insert() : | ||||||||||
| 90 | |||||||||||
| 91 | Perform the insert into ->table of all the records in ->records. | ||||||||||
| 92 | |||||||||||
| 93 | The return value not specified. If the query fails, die. | ||||||||||
| 94 | |||||||||||
| 95 | =cut | ||||||||||
| 96 | |||||||||||
| 97 | 0 | ▶ | sub insert { | ||||||||
|
Subroutine
insert: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 98 | 0 | my $self = shift; | |||||||||
| 99 | 0 | ▶ | my $sql = $self->sql or return undef; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 100 | 0 | my $dbh = $self->dbh; | |||||||||
| 101 | 0 | ▶ | $dbh->do( $sql, {}, $self->record_values->elements ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 102 | or croak( "Could not insert rows: " . $dbh->errstr ); | ||||||||||
| 103 | 0 | return $self->_get_insert_id( $dbh ); | |||||||||
| 104 | } | ||||||||||
| 105 | |||||||||||
| 106 | 2 | 1; | |||||||||