diff --git a/Changes b/Changes index cad7055..c84eb56 100644 --- a/Changes +++ b/Changes @@ -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) diff --git a/lib/Teng.pm b/lib/Teng.pm index 17c8330..57ad65b 100644 --- a/lib/Teng.pm +++ b/lib/Teng.pm @@ -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) = @_; @@ -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. diff --git a/lib/Teng/Plugin/BulkInsert.pm b/lib/Teng/Plugin/BulkInsert.pm index e1967d6..4a4ef18 100644 --- a/lib/Teng/Plugin/BulkInsert.pm +++ b/lib/Teng/Plugin/BulkInsert.pm @@ -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) = @_; @@ -42,7 +44,7 @@ __END__ =head1 NAME -Teng::Plugin::BulkInsert - bulk insert helper +Teng::Plugin::BulkInsert - (DEPRECATED) bulk insert helper =head1 PROVIDED METHODS diff --git a/t/001_basic/023_bulk_insert.t b/t/001_basic/023_bulk_insert.t index f03694c..10d29a7 100644 --- a/t/001_basic/023_bulk_insert.t +++ b/t/001_basic/023_bulk_insert.t @@ -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',[ @@ -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;