blib/lib/Teng/Plugin/BulkInsert.pm untested

filter: Statementstmt 0.0% 0 / 30 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 2 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 4 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 45 CC 5 SCAR 29.7
CC
5
Cov
0%
bulk_insert
30.0
BEGIN
2.0
BEGIN
2.0
CRAP 19.5
1 package Teng::Plugin::BulkInsert;
2 use strict;
3 use warnings;
4 use utf8;
5  
6 our @EXPORT = qw/bulk_insert/;
7  
8 warn "IMPORTANT: Teng::Plugin::BulkInsert is DEPRECATED AND *WILL* BE REMOVED. DO NOT USE.\n";
9  
10 sub bulk_insert {
11 my ($self, $table_name, $args) = @_;
12  
13 return unless scalar(@{$args||[]});
14  
15 if ($self->dbh->{Driver}->{Name} eq 'mysql') {
16 my $table = $self->schema->get_table($table_name);
17 if (! $table) {
18 Carp::croak( "Table definition for $table_name does not exist (Did you declare it in our schema?)" );
19 }
20  
21 if ( $table->has_deflators ) {
22 for my $row (@$args) {
23 for my $col (keys %{$row}) {
24 $row->{$col} = $table->call_deflate($col, $row->{$col});
25 }
26 }
27 }
28  
29 my ($sql, @binds) = $self->sql_builder->insert_multi( $table_name, $args );
30 $self->execute($sql, \@binds);
31 } else {
32 # use transaction for better performance and atomicity.
33 my $txn = $self->txn_scope();
34 for my $arg (@$args) {
35 # do not run trigger for consistency with mysql.
36 $self->insert($table_name, $arg);
37 }
38 $txn->commit;
39 }
40 }
41  
42 1;
43 __END__
44  
45 =head1 NAME
46  
47 Teng::Plugin::BulkInsert - (DEPRECATED) Bulk insert helper
48  
49 =head1 PROVIDED METHODS
50  
51 =over 4
52  
53 =item C<< $teng->bulk_insert($table_name, \@rows_data) >>
54  
55 Accepts either an arrayref of hashrefs.
56 each hashref should be a structure suitable
57 for submitting to a Your::Model->insert(...) method.
58  
59 insert many record by bulk.
60  
61 example:
62  
63 Your::Model->bulk_insert('user',[
64 {
65 id => 1,
66 name => 'nekokak',
67 },
68 {
69 id => 2,
70 name => 'yappo',
71 },
72 {
73 id => 3,
74 name => 'walf443',
75 },
76 ]);
77  
78 =back
79