File Coverage

blib/lib/SQL/Maker/Plugin/InsertOnDuplicate.pm
Criterion Covered Total %
statement 14 14 100.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod 1 1 100.0
total 19 19 100.0


line stmt bran cond sub pod time code
1             use strict;
2 1     1   520 use warnings;
  1         3  
  1         32  
3 1     1   5 use utf8;
  1         2  
  1         23  
4 1     1   5  
  1         1  
  1         5  
5             our @EXPORT = qw/insert_on_duplicate/;
6              
7             my ( $self, $table_name, $insert_values, $update_values ) = @_;
8             my ( $sql, @binds ) = $self->insert( $table_name, $insert_values );
9 1     1 1 116 my ( $update_cols, $update_vals ) = $self->make_set_clause($update_values);
10 1         7 $sql .= $self->new_line . "ON DUPLICATE KEY UPDATE " . join( ', ', @$update_cols );
11 1         5 return ( $sql, @binds, @$update_vals );
12 1         50 }
13 1         22  
14             1;
15              
16             =for test_synopsis
17             my ($name);
18              
19             =head1 NAME
20              
21             SQL::Maker::Plugin::InsertOnDuplicate - INSERT ... ON DUPLICATE KEY UPDATE
22              
23             =head1 SYNOPSIS
24              
25             package My::QueryBuilder;
26             use parent qw/SQL::Maker/;
27             __PACKAGE__->load_plugin('InsertOnDuplicate');
28              
29             package main;
30             my $qb = My::QueryBuilder->new(driver => 'mysql');
31             $qb->insert_on_duplicate('member', { email => 'foo@exapmle.com', name => $name }, { name => $name });
32              
33             =head1 DESCRIPTION
34              
35             This is a plugin to generate "INSERT ... ON DUPLICATE KEY UPDATE" query for MySQL.
36              
37             =head1 METHODS
38              
39             This plugin adds only one method for your query builder class.
40              
41             =over 4
42              
43             =item $query_builder->insert_on_duplicate($table_name:Str, $insert_values:HashRef, $update_values:HashRef)
44              
45             Generate "INSERT ... ON DUPLICATE KEY UPDATE ...".
46              
47             C<< $table_name >> is table name to operate.
48              
49             C<< $insert_values >> is values to insert.
50              
51             $table_name and $insert_values are passing to C<< SQL::Maker#insert >>
52              
53             C<< $update_values >> is SET part for ON DUPLICATE KEY UPDATE. It's processed by C<< SQL::Maker#make_set_clause >>.
54              
55             =back
56              
57             =head1 SEE ALSO
58              
59             L<http://dev.mysql.com/doc/refman/5.6/en/insert-on-duplicate.html>