SQL::Maker::Plugin::InsertMulti - insert multiple rows at once on MySQL
use SQL::Maker; SQL::Maker->load_plugin('InsertMulti'); my $table = 'foo'; my @rows = ( +{ bar => 'baz', john => 'man' }, +{ bar => 'bee', john => 'row' } ); my $builder = SQL::Maker->new( driver => 'mysql' ); my ($sql, @binds); ### INSERT INTO `foo` (`bar`, `john`) VALUES (?, ?), (?, ?) ( $sql, @binds ) = $builder->insert_multi($table, \@rows); ( $sql, @binds ) = $builder->insert_multi($table, [qw/bar john/], [ map { @$_{qw/bar john/} } @rows ]); ### INSERT IGNORE `foo` (`bar`, `john`) VALUES (?, ?), (?, ?) ( $sql, @binds ) = $builder->insert_multi($table, [qw/bar john/], [ map { @$_{qw/bar john/} } @rows ], +{ prefix => 'INSERT IGNORE' }); ### INSERT INTO `foo` (`bar`. `john`) VALUES (?, ?), (?, ?) ON DUPLICATE KEY UPDATE `bar` => ? ( $sql, @binds ) = $builder->insert_multi($table, \@rows, +{ update => +{ bar => 'updated' } }); ( $sql, @binds ) = $builder->insert_multi($table, [qw/bar john/], [ map { @$_{qw/bar john/} } @rows ], +{ update => +{ bar => 'updated' } });
This is a plugin to generate MySQL's INSERT-multi statement.
To install SQL::Maker, copy and paste the appropriate command in to your terminal.
cpanm
cpanm SQL::Maker
CPAN shell
perl -MCPAN -e shell install SQL::Maker
For more information on module installation, please visit the detailed CPAN module installation guide.