File Coverage

blib/lib/Teng/Plugin/BulkInsert.pm
Criterion Covered Total %
statement 9 27 33.3
branch 0 10 0.0
condition n/a
subroutine 3 4 75.0
pod 1 1 100.0
total 13 42 30.9


line stmt bran cond sub pod time code
1             package Teng::Plugin::BulkInsert;
2 1     1   688 use strict;
  1         3  
  1         30  
3 1     1   5 use warnings;
  1         2  
  1         24  
4 1     1   5 use utf8;
  1         2  
  1         5  
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 0     0 1   my ($self, $table_name, $args) = @_;
12              
13 0 0         return unless scalar(@{$args||[]});
  0 0          
14              
15 0 0         if ($self->dbh->{Driver}->{Name} eq 'mysql') {
16 0           my $table = $self->schema->get_table($table_name);
17 0 0         if (! $table) {
18 0           Carp::croak( "Table definition for $table_name does not exist (Did you declare it in our schema?)" );
19             }
20              
21 0 0         if ( $table->has_deflators ) {
22 0           for my $row (@$args) {
23 0           for my $col (keys %{$row}) {
  0            
24 0           $row->{$col} = $table->call_deflate($col, $row->{$col});
25             }
26             }
27             }
28              
29 0           my ($sql, @binds) = $self->sql_builder->insert_multi( $table_name, $args );
30 0           $self->execute($sql, \@binds);
31             } else {
32             # use transaction for better performance and atomicity.
33 0           my $txn = $self->txn_scope();
34 0           for my $arg (@$args) {
35             # do not run trigger for consistency with mysql.
36 0           $self->insert($table_name, $arg);
37             }
38 0           $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