blib/lib/Otogiri/Plugin/BulkInsert.pm

filter: Statementstmt 100% 21 / 21 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod 100% 1 / 1 totaltotal 100% 26 / 26 SCAR 6.9
CC
2
Cov
100%
bulk_insert
2.0
BEGIN
1.0
BEGIN
1.0
CRAP 2.0
« Build ↑ Summary _build/lib/MyBuilder.pm »
1 package Otogiri::Plugin::BulkInsert;
2 2 ▶use 5.008005;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 our $VERSION = "0.01";
7 2 our @EXPORT = qw(bulk_insert);
8  
9 sub bulk_insert {
10 1 ▶ my ($self, $table, $key_list, $row_list) = @_;
Subroutine bulk_insert: called CC=2 SCAR=6.9
Pod documented
11  
12 1 my $keys = join(', ', @$key_list);
13 1 my $binds = join(', ', map {'?'} @$key_list);
14  
15 1 my $sql = sprintf('INSERT INTO %s (%s) VALUES (%s)', $table, $keys, $binds);
16 1 my $sth = $self->dbh->prepare($sql);
17  
18 1 for my $row (@$row_list) {
19 8 my %rowdata = %$row;
20 8 $sth->execute(@rowdata{@$key_list});
21 }
22  
23 1 $sth->finish;
24 }
25  
26 2 1;
27 __END__
28  
29 =encoding utf-8
30  
31 =head1 NAME
32  
33 Otogiri::Plugin::BulkInsert - bulk insert for Otogiri
34  
35 =head1 SYNOPSIS
36  
37 use Otogiri;
38 use Otogiri::Plugin;
39
40 my $otogiri = Otogiri->new(...);
41 $otogiri->load_plugin('BulkInsert');
42  
43 $otogiri->bulk_insert(
44 'book',
45 [qw| title author |],
46 [
47 {title => 'Acmencyclopedia 2009', author => 'Makamaka Hannyaharamitu'},
48 {title => 'Acmencyclopedia Reverse', author => 'Makamaka Hannyaharamitu'},
49 {title => 'Acmencyclopedia 2010', author => 'Makamaka Hannyaharamitu'},
50 {title => 'Acmencyclopedia 2011', author => 'Makamaka Hannyaharamitu'},
51 {title => 'Acmencyclopedia 2012', author => 'Makamaka Hannyaharamitu'},
52 {title => 'Acmencyclopedia 2013', author => 'Makamaka Hannyaharamitu'},
53 {title => 'Miyabi-na-Perl Nyuumon', author => 'Miyabi-na-Rakuda'},
54 {title => 'Miyabi-na-Perl Nyuumon 2nd edition', author => 'Miyabi-na-Rakuda'},
55 ],
56 );
57  
58 =head1 DESCRIPTION
59  
60 Otogiri::Plugin::BulkInsert is A plugin for otogiri that provides 'bulk insert' method.
61  
62 =head1 METHODS
63  
64 =head2 $otogiri->bulk_insert($tablename, [ @colnames ], [ @rowdatas ]);
65  
66 Insert multiple rowdata into specified table.
67  
68 =head1 LICENSE
69  
70 Copyright (C) ytnobody.
71  
72 This library is free software; you can redistribute it and/or modify
73 it under the same terms as Perl itself.
74  
75 =head1 AUTHOR
76  
77 ytnobody E<lt>ytnobody@gmail.comE<gt>
78  
79 =cut
80  
« Build ↑ Summary _build/lib/MyBuilder.pm »