... | ... |
@@ -1,3 +1,5 @@ |
1 |
+0.2103 |
|
2 |
+ - added EXPERIMENTAL insert bulk_insert option. |
|
1 | 3 |
0.2102 |
2 | 4 |
- fixed bug that DBIx::Custom::Model count method don't receive model's |
3 | 5 |
attribute |
... | ... |
@@ -1,7 +1,7 @@ |
1 | 1 |
package DBIx::Custom; |
2 | 2 |
use Object::Simple -base; |
3 | 3 |
|
4 |
-our $VERSION = '0.2102'; |
|
4 |
+our $VERSION = '0.2103'; |
|
5 | 5 |
use 5.008001; |
6 | 6 |
|
7 | 7 |
use Carp 'croak'; |
... | ... |
@@ -683,8 +683,19 @@ sub insert { |
683 | 683 |
# Insert statement |
684 | 684 |
my $sql = "insert "; |
685 | 685 |
$sql .= "$opt{prefix} " if defined $opt{prefix}; |
686 |
- $sql .= "into " . $self->q($opt{table}) . " " |
|
687 |
- . $self->values_clause($params->[0], {wrap => $opt{wrap}}) . " "; |
|
686 |
+ $sql .= "into " . $self->q($opt{table}) . " "; |
|
687 |
+ if ($opt{bulk_insert}) { |
|
688 |
+ $sql .= $self->_multi_values_clause($params, {wrap => $opt{wrap}}) . " "; |
|
689 |
+ my $new_param = {}; |
|
690 |
+ $new_param->{$_} = [] for keys %{$params->[0]}; |
|
691 |
+ for my $param (@$params) { |
|
692 |
+ push @{$new_param->{$_}}, $param->{$_} for keys %$param; |
|
693 |
+ } |
|
694 |
+ $params = [$new_param]; |
|
695 |
+ } |
|
696 |
+ else { |
|
697 |
+ $sql .= $self->values_clause($params->[0], {wrap => $opt{wrap}}) . " "; |
|
698 |
+ } |
|
688 | 699 |
|
689 | 700 |
# Remove id from parameter |
690 | 701 |
delete $params->[0]->{$_} for @cleanup; |
... | ... |
@@ -1235,6 +1246,30 @@ sub values_clause { |
1235 | 1246 |
')' |
1236 | 1247 |
} |
1237 | 1248 |
|
1249 |
+sub _multi_values_clause { |
|
1250 |
+ my ($self, $params, $opts) = @_; |
|
1251 |
+ |
|
1252 |
+ my $wrap = $opts->{wrap} || {}; |
|
1253 |
+ |
|
1254 |
+ # Create insert parameter tag |
|
1255 |
+ my ($q, $p) = split //, $self->q(''); |
|
1256 |
+ |
|
1257 |
+ # Multi values clause |
|
1258 |
+ my $clause = '(' . join(', ', map { "$q$_$p" } sort keys %{$params->[0]}) . ') values '; |
|
1259 |
+ |
|
1260 |
+ for (1 .. @$params) { |
|
1261 |
+ $clause .= '(' . join(', ', |
|
1262 |
+ map { |
|
1263 |
+ ref $params->[0]->{$_} eq 'SCALAR' ? ${$params->[0]->{$_}} : |
|
1264 |
+ $wrap->{$_} ? $wrap->{$_}->(":$_") : |
|
1265 |
+ ":$_"; |
|
1266 |
+ } sort keys %{$params->[0]} |
|
1267 |
+ ) . '), ' |
|
1268 |
+ } |
|
1269 |
+ $clause =~ s/, $//; |
|
1270 |
+ return $clause; |
|
1271 |
+} |
|
1272 |
+ |
|
1238 | 1273 |
sub where { DBIx::Custom::Where->new(dbi => shift, @_) } |
1239 | 1274 |
|
1240 | 1275 |
sub _create_query { |
... | ... |
@@ -2795,6 +2830,16 @@ and use the following new ones. |
2795 | 2830 |
|
2796 | 2831 |
=over 4 |
2797 | 2832 |
|
2833 |
+=item C<bulk_insert> EXPERIMENTAL |
|
2834 |
+ |
|
2835 |
+ bulk_insert => 1 |
|
2836 |
+ |
|
2837 |
+bulk insert is executed if database support bulk insert and |
|
2838 |
+multiple parameters is passed to C<insert>. |
|
2839 |
+The SQL like the following one is executed. |
|
2840 |
+ |
|
2841 |
+ insert into book (id, title) values (?, ?), (?, ?); |
|
2842 |
+ |
|
2798 | 2843 |
=item C<created_at> |
2799 | 2844 |
|
2800 | 2845 |
created_at => 'created_datetime' |
... | ... |
@@ -47,6 +47,28 @@ ok(!$@); |
47 | 47 |
eval { $dbi->do('drop table table1') }; |
48 | 48 |
$dbi->do('create table table1 (key1 varchar(255), key2 varchar(255)) engine=InnoDB'); |
49 | 49 |
|
50 |
+test 'bulk_insert'; |
|
51 |
+$dbi->delete_all(table => 'table1'); |
|
52 |
+$dbi->insert( |
|
53 |
+ [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], |
|
54 |
+ table => 'table1', |
|
55 |
+ bulk_insert => 1 |
|
56 |
+); |
|
57 |
+like($dbi->last_sql, qr/(\?.+){4}/); |
|
58 |
+$rows = $dbi->select(table => 'table1')->all; |
|
59 |
+is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]); |
|
60 |
+ |
|
61 |
+$dbi->delete_all(table => 'table1'); |
|
62 |
+$dbi->insert( |
|
63 |
+ [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], |
|
64 |
+ table => 'table1', |
|
65 |
+ bulk_insert => 1, |
|
66 |
+ filter => {key1 => sub { $_[0] * 2 }} |
|
67 |
+); |
|
68 |
+like($dbi->last_sql, qr/(\?.+){4}/); |
|
69 |
+$rows = $dbi->select(table => 'table1')->all; |
|
70 |
+is_deeply($rows, [{key1 => 2, key2 => 2}, {key1 => 6, key2 => 4}]); |
|
71 |
+ |
|
50 | 72 |
test 'update_or_insert'; |
51 | 73 |
$dbi->delete_all(table => 'table1'); |
52 | 74 |
$dbi->update_or_insert( |