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