Skip to content

Commit

Permalink
do not use Plugin::BulkInsert. support core feature.
Browse files Browse the repository at this point in the history
  • Loading branch information
nekokak committed Nov 21, 2011
1 parent 054ec3f commit 446c484
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 2 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -4,6 +4,7 @@ Revision history for Perl extension Teng
- add Plugin::Lookup.
- fixed fork safe connection. (thanks nihen)
- support auto reconnect dbh. (thanks nihen)
- [IMPORTANT] bulk_insert include core feature. do not use Plugin::BulkInsert.

0.14
- fixed deflate bug. (thanks kentaro)
Expand Down
57 changes: 57 additions & 0 deletions lib/Teng.pm
Expand Up @@ -297,6 +297,38 @@ sub insert {
);
}

sub bulk_insert {
my ($self, $table_name, $args) = @_;

return unless scalar(@{$args||[]});

if ($self->dbh->{Driver}->{Name} eq 'mysql') {
my $table = $self->schema->get_table($table_name);
if (! $table) {
Carp::croak( "Table definition for $table_name does not exist (Did you declare it in our schema?)" );
}

if ( $table->has_deflators ) {
for my $row (@$args) {
for my $col (keys %{$row}) {
$row->{$col} = $table->call_deflate($col, $row->{$col});
}
}
}

my ($sql, @binds) = $self->sql_builder->insert_multi( $table_name, $args );
$self->_execute($sql, \@binds);
} else {
# use transaction for better performance and atomicity.
my $txn = $self->txn_scope();
for my $arg (@$args) {
# do not run trigger for consistency with mysql.
$self->insert($table_name, $arg);
}
$txn->commit;
}
}

sub _update {
my ($self, $table_name, $args, $where) = @_;

Expand Down Expand Up @@ -674,6 +706,31 @@ insert new record and get last_insert_id.
no creation row object.
=item $teng->bulk_insert($table_name, \@rows_data)
Accepts either an arrayref of hashrefs.
each hashref should be a structure suitable
forsubmitting to a Your::Model->insert(...) method.
insert many record by bulk.
example:
Your::Model->bulk_insert('user',[
{
id => 1,
name => 'nekokak',
},
{
id => 2,
name => 'yappo',
},
{
id => 3,
name => 'walf443',
},
]);
=item $update_row_count = $teng->update($table_name, \%update_row_data, [\%update_condition])
Calls UPDATE on C<$table_name>, with values specified in C<%update_ro_data>, and returns the number of rows updated. You may optionally specify C<%update_condition> to create a conditional update query.
Expand Down
4 changes: 3 additions & 1 deletion lib/Teng/Plugin/BulkInsert.pm
Expand Up @@ -5,6 +5,8 @@ use utf8;

our @EXPORT = qw/bulk_insert/;

warn "IMPORTANT: Teng::Plugin::BulkInsert is DEPRECATED AND *WILL* BE REMOVED. DO NOT USE.\n";

sub bulk_insert {
my ($self, $table_name, $args) = @_;

Expand Down Expand Up @@ -42,7 +44,7 @@ __END__
=head1 NAME
Teng::Plugin::BulkInsert - bulk insert helper
Teng::Plugin::BulkInsert - (DEPRECATED) bulk insert helper
=head1 PROVIDED METHODS
Expand Down
13 changes: 12 additions & 1 deletion t/001_basic/023_bulk_insert.t
Expand Up @@ -5,7 +5,6 @@ use Test::More;
my $dbh = t::Utils->setup_dbh;
my $db_basic = Mock::Basic->new({dbh => $dbh});
$db_basic->setup_test_db;
Mock::Basic->load_plugin('BulkInsert');

subtest 'bulk_insert method' => sub {
$db_basic->bulk_insert('mock_basic',[
Expand All @@ -25,5 +24,17 @@ subtest 'bulk_insert method' => sub {
is +$db_basic->count('mock_basic', 'id'), 3;
};

subtest 'DEPRECATED' => sub {
my $buffer = '';
open my $fh, '>', \$buffer or die "Could not open in-memory buffer";
*STDERR = $fh;

Mock::Basic->load_plugin('BulkInsert');

close $fh;

like $buffer, qr/IMPORTANT: Teng::Plugin::BulkInsert is DEPRECATED AND \*WILL\* BE REMOVED\. DO NOT USE\./;
};

done_testing;

0 comments on commit 446c484

Please sign in to comment.