NAME

Mojo::SQLite::Database::Role::InsertHelpers - Add insert_or_ignore and insert_or_replace methods to Mojo::SQLite::Database

SYNOPSIS

use Mojo::SQLite;

my $sqlite = Mojo::SQLite->new('sqlite.db');
my $db     = $sqlite->db->with_roles("+InsertHelpers");

# Insert a row, ignoring conflicts
$db->insert_or_ignore('users', { name => 'Alice', age => 30 });

# Insert a row, replacing on conflict (dangerous: deletes field content if not explicitly set)
$db->insert_or_replace('users', { name => 'Alice', age => 31 });

# Insert a row, updating on conflict
$db->insert_or_update('users', { name => 'Alice', age => 32 });

DESCRIPTION

This role adds helper methods to Mojo::SQLite::Database to simplify common SQLite insert patterns: inserting rows while ignoring conflicts or replacing existing rows on conflict.

METHODS

insert_or_ignore

$db->insert_or_ignore($table, \%values);

Builds an SQL INSERT statement with OR IGNORE to insert a row into the specified table. If a conflict occurs (e.g., a unique constraint is violated), the insert is ignored instead of failing.

  • $table - Name of the database table.

  • \%values - Hashref of column => value pairs to insert.

insert_or_replace

$db->insert_or_replace($table, \%values);

Builds an SQL INSERT statement with OR REPLACE to insert a row into the specified table. If a conflict occurs, the existing row is replaced with the new values.

  • $table - Name of the database table.

  • \%values - Hashref of column => value pairs to insert.

insert_or_update

$db->insert_or_update($table, \%values);

An alias for insert_or_replace.

AUTHOR

Your Name <you@example.com>

LICENSE

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.