| 1 | package Mojo::SQLite::Database::Role::InsertHelpers; | ||
| 2 | |||
| 3 | # ABSTRACT: adds insert_or_ignore, insert_or_replace and insert_or_update to Mojo::SQLite::Database | ||
| 4 | |||
| 5 | use Mojo::Base -role; | ||
| 6 | use Memoize; | ||
| 7 | use Carp; | ||
| 8 | |||
| 9 | sub insert_or_ignore { | ||
| 10 | my ($db, $table, $values) = @_; | ||
| 11 | my ($sql, @binds) = $db->sqlite->abstract->insert($table, $values); | ||
| 12 | $sql =~ s/^INSERT/INSERT OR IGNORE/; | ||
| 13 | return $db->query($sql, @binds); | ||
| 14 | } | ||
| 15 | |||
| 16 | sub insert_or_replace { | ||
| 17 | my ($db, $table, $values) = @_; | ||
| 18 | my ($sql, @binds) = $db->sqlite->abstract->insert($table, $values); | ||
| 19 | $sql =~ s/^INSERT/INSERT OR REPLACE/; | ||
| 20 | return $db->query($sql, @binds); | ||
| 21 | } | ||
| 22 | |||
| 23 | sub insert_or_update { | ||
| 24 | my ($db, $table, $values) = @_; | ||
| 25 | |||
| 26 | # Ensure we have a hash | ||
| 27 | if (ref $values eq 'ARRAY') { | ||
| 28 | my $fields = $db->fields($table); | ||
| 29 | croak "insert or update failed: table %s has %i columns but %i values were supplied", $table, scalar $fields->@*, scalar $values->@* | ||
| 30 | if (scalar $values->@* != scalar $fields->@*); | ||
| 31 | $values = { map { $fields->[$_] => $values->[$_] } 0 .. $#$fields }; | ||
| 32 | } | ||
| 33 | |||
| 34 | my $pk = $db->primary_key($table); | ||
| 35 | # If no PK, fallback to standard insert (or perhaps insert_or_ignore?) | ||
| 36 | return $db->insert($table, $values) unless @$pk; | ||
| 37 | |||
| 38 | # Use 'excluded' to keep the query slim | ||
| 39 | my $update = { map { $_ => \ "excluded.$_" } keys %$values }; | ||
| 40 | |||
| 41 | return $db->insert($table, $values, { on_conflict => [ $pk => $update ] }); | ||
| 42 | } | ||
| 43 | |||
| 44 | sub fields { | ||
| 45 | my ($db, $table) = @_; | ||
| 46 | [ map { $_->[3] } @{$db->dbh->column_info(undef, undef, $table, undef)->fetchall_arrayref} ]; | ||
| 47 | } | ||
| 48 | |||
| 49 | sub primary_key { | ||
| 50 | my ($db, $table) = @_; | ||
| 51 | [ map { $_->[3] } @{$db->dbh->primary_key_info(undef, undef, $table)->fetchall_arrayref} ]; | ||
| 52 | } | ||
| 53 | |||
| 54 | |||
| 55 | memoize('primary_key', normalizer => sub { return join "|", $_[0]->sqlite->dsn, $_[1] }); | ||
| 56 | memoize('fields', normalizer => sub { return join "|", $_[0]->sqlite->dsn, $_[1] }); | ||
| 57 | |||
| 58 | 1; | ||
| 59 | |||
| 60 | =head1 NAME | ||
| 61 | |||
| 62 | Mojo::SQLite::Database::Role::InsertHelpers - Add insert_or_ignore and insert_or_replace methods to Mojo::SQLite::Database | ||
| 63 | |||
| 64 | =head1 SYNOPSIS | ||
| 65 | |||
| 66 | use Mojo::SQLite; | ||
| 67 | |||
| 68 | my $sqlite = Mojo::SQLite->new('sqlite.db'); | ||
| 69 | my $db = $sqlite->db->with_roles("+InsertHelpers"); | ||
| 70 | |||
| 71 | # Insert a row, ignoring conflicts | ||
| 72 | $db->insert_or_ignore('users', { name => 'Alice', age => 30 }); | ||
| 73 | |||
| 74 | # Insert a row, replacing on conflict (dangerous: deletes field content if not explicitly set) | ||
| 75 | $db->insert_or_replace('users', { name => 'Alice', age => 31 }); | ||
| 76 | |||
| 77 | # Insert a row, updating on conflict | ||
| 78 | $db->insert_or_update('users', { name => 'Alice', age => 32 }); | ||
| 79 | |||
| 80 | =head1 DESCRIPTION | ||
| 81 | |||
| 82 | This role adds helper methods to L<Mojo::SQLite::Database> to simplify common | ||
| 83 | SQLite insert patterns: inserting rows while ignoring conflicts or replacing | ||
| 84 | existing rows on conflict. | ||
| 85 | |||
| 86 | =head1 METHODS | ||
| 87 | |||
| 88 | =head2 insert_or_ignore | ||
| 89 | |||
| 90 | $db->insert_or_ignore($table, \%values); | ||
| 91 | |||
| 92 | Builds an SQL INSERT statement with C<OR IGNORE> to insert a row into the | ||
| 93 | specified table. If a conflict occurs (e.g., a unique constraint is violated), | ||
| 94 | the insert is ignored instead of failing. | ||
| 95 | |||
| 96 | =over 4 | ||
| 97 | |||
| 98 | =item * C<$table> - Name of the database table. | ||
| 99 | |||
| 100 | =item * C<\%values> - Hashref of column => value pairs to insert. | ||
| 101 | |||
| 102 | =back | ||
| 103 | |||
| 104 | =head2 insert_or_replace | ||
| 105 | |||
| 106 | $db->insert_or_replace($table, \%values); | ||
| 107 | |||
| 108 | Builds an SQL INSERT statement with C<OR REPLACE> to insert a row into the | ||
| 109 | specified table. If a conflict occurs, the existing row is replaced with | ||
| 110 | the new values. | ||
| 111 | |||
| 112 | =over 4 | ||
| 113 | |||
| 114 | =item * C<$table> - Name of the database table. | ||
| 115 | |||
| 116 | =item * C<\%values> - Hashref of column => value pairs to insert. | ||
| 117 | |||
| 118 | =back | ||
| 119 | |||
| 120 | =head2 insert_or_update | ||
| 121 | |||
| 122 | $db->insert_or_update($table, \%values); | ||
| 123 | |||
| 124 | An alias for C<insert_or_replace>. | ||
| 125 | |||
| 126 | =head1 AUTHOR | ||
| 127 | |||
| 128 | Your Name <you@example.com> | ||
| 129 | |||
| 130 | =head1 LICENSE | ||
| 131 | |||
| 132 | This library is free software; you can redistribute it and/or modify | ||
| 133 | it under the same terms as Perl itself. | ||
| 134 | |||
| 135 | =cut |