| 1 | package DBIx::QuickORM::Manual::BulkInsert; | ||
| 2 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | 2 | our $VERSION = '0.000031'; | |
| 6 | |||
| 7 | =pod | ||
| 8 | |||
| 9 | =encoding UTF-8 | ||
| 10 | |||
| 11 | =head1 NAME | ||
| 12 | |||
| 13 | DBIx::QuickORM::Manual::BulkInsert - Insert many rows at once with insert_many, by example. | ||
| 14 | |||
| 15 | =head1 DESCRIPTION | ||
| 16 | |||
| 17 | C<insert_many> inserts an arrayref of rows in as few statements as the | ||
| 18 | database allows, all or nothing. C<insert_many_fk> does the same for rows | ||
| 19 | linked to one row (L<DBIx::QuickORM::Manual::ForeignKeys/INSERTING LINKED ROWS>). | ||
| 20 | |||
| 21 | =head1 SCHEMA | ||
| 22 | |||
| 23 | The examples use PostgreSQL and this schema: | ||
| 24 | |||
| 25 | =for highlighter language=sql | ||
| 26 | |||
| 27 | CREATE TABLE users ( | ||
| 28 | id SERIAL PRIMARY KEY, | ||
| 29 | name TEXT NOT NULL, | ||
| 30 | email TEXT UNIQUE, | ||
| 31 | prefs JSON, | ||
| 32 | referred_by INTEGER REFERENCES users (id), | ||
| 33 | created TIMESTAMP NOT NULL DEFAULT now() | ||
| 34 | ); | ||
| 35 | |||
| 36 | CREATE TABLE posts ( | ||
| 37 | id SERIAL PRIMARY KEY, | ||
| 38 | user_id INTEGER NOT NULL REFERENCES users (id), | ||
| 39 | title TEXT NOT NULL, | ||
| 40 | views INTEGER NOT NULL DEFAULT 0 | ||
| 41 | ); | ||
| 42 | |||
| 43 | =head1 INSERTING MANY ROWS | ||
| 44 | |||
| 45 | =for highlighter language=perl | ||
| 46 | |||
| 47 | my $users = $orm->insert_many(users => [{name => 'Ann'}, {name => 'Bob'}]); # an arrayref of rows | ||
| 48 | my $id = $users->[0]->field('id'); # whole rows, canonical | ||
| 49 | |||
| 50 | # the same, on a table handle | ||
| 51 | my $more = $orm->handle('users')->insert_many([{name => 'Cy'}, {name => 'Di'}]); | ||
| 52 | |||
| 53 | my $data = $orm->insert_many( | ||
| 54 | users => [{name => 'Gus'}], | ||
| 55 | data => 1, # hashrefs instead of row objects | ||
| 56 | ); | ||
| 57 | |||
| 58 | Called in void context it only inserts. It builds no rows, returns nothing, | ||
| 59 | and sends no C<RETURNING>, so it works on every database, even for rows | ||
| 60 | without their whole primary key: | ||
| 61 | |||
| 62 | $orm->insert_many(users => [{name => 'Hal'}, {name => 'Ida'}]); # void: inserts only | ||
| 63 | |||
| 64 | Otherwise C<insert_many> returns an arrayref of the new rows, never a list. | ||
| 65 | Through C<RETURNING> they hold the table's default fields (every field, | ||
| 66 | unless C<omit> or C<default_fields> narrows them), keys and defaults | ||
| 67 | included. | ||
| 68 | |||
| 69 | Every row must have the same field names. Values are deflated as C<insert> | ||
| 70 | deflates them (L<DBIx::QuickORM::Manual::Values>), and a typed primary key a | ||
| 71 | row leaves out is generated as C<insert> generates it | ||
| 72 | (L<DBIx::QuickORM::Manual::Keys>). | ||
| 73 | |||
| 74 | =head1 JUST THE COUNT | ||
| 75 | |||
| 76 | my $count = $orm->insert_many( | ||
| 77 | users => [{name => 'Eve'}, {name => 'Fay'}], | ||
| 78 | count => 1, | ||
| 79 | ); # 2 | ||
| 80 | |||
| 81 | C<< count => 1 >> returns how many rows went in. It works on every database. | ||
| 82 | |||
| 83 | With C<count>, and in void context, the new rows are not read back, so the | ||
| 84 | ORM holds no copy of them (L<DBIx::QuickORM::Manual::Caching>). | ||
| 85 | |||
| 86 | =head1 ALL OR NOTHING | ||
| 87 | |||
| 88 | # more binds than one statement may hold: two statements | ||
| 89 | my @many = map { | ||
| 90 | { | ||
| 91 | name => "User $_", | ||
| 92 | email => "user$_\@example.com", | ||
| 93 | } | ||
| 94 | } 1 .. 40_000; | ||
| 95 | |||
| 96 | $many[-1]->{email} = 'user1@example.com'; # a duplicate, in the last row | ||
| 97 | |||
| 98 | # fails, and no row went in, not even the first statement's | ||
| 99 | my $ok = eval { $orm->insert_many(users => \@many); 1 }; | ||
| 100 | |||
| 101 | Each statement holds as many rows as the dialect's C<bind_limit> allows (65535 | ||
| 102 | binds on PostgreSQL, and L<DBIx::QuickORM::Manual::Dialects/BULK INSERTS> | ||
| 103 | lists the others). More than one statement runs in a transaction of its own | ||
| 104 | (L<DBIx::QuickORM::Manual::Transactions>). | ||
| 105 | |||
| 106 | Inside an open transaction C<insert_many> always takes a savepoint, so a | ||
| 107 | failure undoes only its own rows and leaves your transaction usable. DuckDB | ||
| 108 | has no savepoints: there a failure inside your transaction is not undone by | ||
| 109 | itself, so roll the transaction back. | ||
| 110 | |||
| 111 | =head1 WITHOUT RETURNING (MySQL) | ||
| 112 | |||
| 113 | # on MySQL every row must carry its whole primary key | ||
| 114 | my $rows = $orm->insert_many( | ||
| 115 | users => [ | ||
| 116 | { | ||
| 117 | id => 1, | ||
| 118 | name => 'A', | ||
| 119 | }, | ||
| 120 | { | ||
| 121 | id => 2, | ||
| 122 | name => 'B', | ||
| 123 | }, | ||
| 124 | ], | ||
| 125 | ); | ||
| 126 | |||
| 127 | # on MySQL the row holds only its key, and fetches the rest when read | ||
| 128 | my $name = $rows->[0]->field('name'); | ||
| 129 | |||
| 130 | MySQL cannot return the rows it inserted, so there C<insert_many> gives rows | ||
| 131 | holding only their primary keys (L<DBIx::QuickORM::Manual::Dialects/RETURNING>). | ||
| 132 | Reading another field of such a row fetches it from the database. | ||
| 133 | |||
| 134 | Every key must be given, or generated by its type before the insert (a UUID | ||
| 135 | key is, see L<DBIx::QuickORM::Manual::Keys>). A row without its whole key, or | ||
| 136 | a table without a primary key, croaks before anything is sent. Pass | ||
| 137 | C<< count => 1 >>, or call it in void context, to insert such rows. | ||
| 138 | |||
| 139 | MariaDB, PostgreSQL, SQLite, and DuckDB return whole rows, with the keys they | ||
| 140 | generated. | ||
| 141 | |||
| 142 | =cut | ||
| 143 | |||
| 144 | 2 | 1; | |
| 145 | |||
| 146 | __END__ | ||