The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

SQL::Composer::Upsert - UPSERT statement emulation

SYNOPSIS

    my $upsert = SQL::Composer::Upsert->new(
        into   => 'table',
        values => [ id => 1, foo => 'bar' ],
        driver => $driver # driver must be set
    );

    my $sql = $upsert->to_sql;
    # SQLite: 'INSERT INTO `table` (`id`, `foo`) VALUES (?, ?) ON CONFLICT UPDATE'
    # MySQL: 'INSERT INTO `table` (`id`, `foo`) VALUES (?, ?) ON DUPLICATE KEY UPDATE'
    # Pg: 'INSERT INTO `table` (`id`, `foo`) VALUES (?, ?) ON CONFLICT DO UPDATE'
    my @bind = $upsert->to_bind; # [1, 'bar']

DESCRIPTION

This emulates the UPSERT statement, which is defined as an attempt to INSERT failing due to a key constraint and the query being turned into an UPDATE instead.

CAVEAT

Since this feature is not universally supported, you must specify a driver when creating SQL::Composer::Upsert instance so that we can generate the correct SQL.

It should also be noted that we support the lowest common denominator, which is the basic UPSERT behavior even though some RDBMS support more complex features.