| 1 | package DBIx::QuickORM::Manual::Joins; | ||
| 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::Joins - Join tables and read the joined rows, by example. | ||
| 14 | |||
| 15 | =head1 DESCRIPTION | ||
| 16 | |||
| 17 | A join handle returns one join row per result row. A join row holds a row | ||
| 18 | object for every table in the join, or undef for a left-joined table that | ||
| 19 | matched nothing. | ||
| 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 | C<posts.user_id> and C<users.referred_by> both reference C<users.id>. | ||
| 44 | |||
| 45 | =head1 INNER AND LEFT JOINS | ||
| 46 | |||
| 47 | =for highlighter language=perl | ||
| 48 | |||
| 49 | my $joined = $orm->handle('posts') | ||
| 50 | ->join('users') | ||
| 51 | ->order_by('posts.id'); | ||
| 52 | |||
| 53 | my @pairs = $joined->all; | ||
| 54 | my $count = $joined->count; | ||
| 55 | my $first = $joined->first; | ||
| 56 | |||
| 57 | my $it = $joined->iter; | ||
| 58 | while (my $pair = $it->next) { | ||
| 59 | my $post = $pair->row('posts'); # a full posts row | ||
| 60 | my $title = $post->field('title'); | ||
| 61 | my $author = $pair->row('users')->field('name'); | ||
| 62 | my @names = $pair->aliases; # ('posts', 'users') | ||
| 63 | } | ||
| 64 | |||
| 65 | A join handle is a handle: C<all>, C<first>, C<only>, C<iter>, and C<count> | ||
| 66 | work as on a table. C<row> returns a component's row, in its table's row | ||
| 67 | class (L<DBIx::QuickORM::Manual::RowClasses>), with every row method: | ||
| 68 | C<field>, C<update>, C<follow_fk>, and the rest. | ||
| 69 | |||
| 70 | A left join keeps rows that match nothing. Here there is one row per | ||
| 71 | user-post pair, and one for each user without posts, with C<row('posts')> | ||
| 72 | undef: | ||
| 73 | |||
| 74 | my @everyone = $orm->handle('users') | ||
| 75 | ->left_join('posts') | ||
| 76 | ->order_by('users.id') | ||
| 77 | ->all; | ||
| 78 | |||
| 79 | C<join> and C<inner_join> are the same. Omit C<link> when exactly one foreign | ||
| 80 | key connects the two tables, in either direction. Otherwise the join croaks. | ||
| 81 | A self reference counts once per direction (L</SELF JOINS>). To join on | ||
| 82 | fields no foreign key connects, name them with C<via>, in C<link>'s form | ||
| 83 | (L<DBIx::QuickORM::Manual::ForeignKeys/KEYS THE MAP DOES NOT KNOW>). | ||
| 84 | |||
| 85 | =head1 FILTERING, ORDERING, AND FIELDS | ||
| 86 | |||
| 87 | my @popular = $orm->handle('posts') | ||
| 88 | ->join('users') | ||
| 89 | ->where({ | ||
| 90 | 'users.name' => 'Alice', | ||
| 91 | 'posts.views' => {'>' => 5}, | ||
| 92 | }) | ||
| 93 | ->order_by([{-desc => 'posts.views'}, 'posts.id']) | ||
| 94 | ->fields(['posts.title', 'users.name']) # a subset (each primary key is always added) | ||
| 95 | ->all; | ||
| 96 | |||
| 97 | my $n = $orm->handle('posts') | ||
| 98 | ->join('users') | ||
| 99 | ->where({'users.name' => 'Alice'}) | ||
| 100 | ->count; | ||
| 101 | |||
| 102 | Fields are named C<alias.field>. The alias defaults to the table name. | ||
| 103 | |||
| 104 | =head1 ALIASES | ||
| 105 | |||
| 106 | my @by_author = $orm->handle('posts') | ||
| 107 | ->join('users', as => 'author') | ||
| 108 | ->order_by('author.name') | ||
| 109 | ->all; | ||
| 110 | |||
| 111 | my $writer = $by_author[0]->row('author')->field('name'); | ||
| 112 | |||
| 113 | =head1 SELF JOINS | ||
| 114 | |||
| 115 | # Each referred user with whoever referred them. | ||
| 116 | my @referred = $orm->handle('users') | ||
| 117 | ->join( | ||
| 118 | 'users', | ||
| 119 | as => 'referrer', | ||
| 120 | link => {referred_by => 'id'}, | ||
| 121 | ) | ||
| 122 | ->order_by('users.id') | ||
| 123 | ->all; | ||
| 124 | |||
| 125 | for my $pair (@referred) { | ||
| 126 | my $who = $pair->row('users')->field('name'); | ||
| 127 | my $by = $pair->row('referrer')->field('name'); | ||
| 128 | } | ||
| 129 | |||
| 130 | # The other way round: each referrer with the users they referred. | ||
| 131 | my @referrals = $orm->handle('users') | ||
| 132 | ->join( | ||
| 133 | 'users', | ||
| 134 | as => 'referral', | ||
| 135 | link => {id => 'referred_by'}, | ||
| 136 | ) | ||
| 137 | ->order_by('referral.id') | ||
| 138 | ->all; | ||
| 139 | |||
| 140 | C<as> names the second C<users> row. C<link> picks the direction of | ||
| 141 | C<referred_by>: a self reference offers each direction as its own mapping, | ||
| 142 | so without C<link> the join croaks. C<link> is | ||
| 143 | C<< {from_field => joined_field} >>: the left side is a field of the table | ||
| 144 | joined from (the base table, unless C<from> names another), the right side a | ||
| 145 | field of the table being joined. | ||
| 146 | |||
| 147 | =head1 JOINING FROM A JOINED TABLE | ||
| 148 | |||
| 149 | my @chain = $orm->handle('posts') | ||
| 150 | ->join('users') | ||
| 151 | ->left_join( | ||
| 152 | 'users', | ||
| 153 | as => 'referrer', | ||
| 154 | from => 'users', | ||
| 155 | link => {referred_by => 'id'}, | ||
| 156 | ) | ||
| 157 | ->order_by('posts.id') | ||
| 158 | ->all; | ||
| 159 | |||
| 160 | my $author = $chain[0]->row('users')->field('name'); | ||
| 161 | my $referrer = $chain[0]->row('referrer'); # undef when the author was not referred | ||
| 162 | |||
| 163 | C<from> names a component already in the join, by its alias (the table name | ||
| 164 | unless C<as> gave another). The default is the base table. | ||
| 165 | |||
| 166 | =head1 FLAT ROWS | ||
| 167 | |||
| 168 | # one join row per post | ||
| 169 | my @pairs = $orm->handle('users') | ||
| 170 | ->join('posts') | ||
| 171 | ->order_by('posts.id') | ||
| 172 | ->all; | ||
| 173 | |||
| 174 | my @aliases = $pairs[0]->aliases; # ('users', 'posts'), in join order | ||
| 175 | |||
| 176 | # {'users.id' => 1, 'users.name' => 'Alice', 'posts.title' => 'Hello', ...} | ||
| 177 | my $data = $pairs[0]->data; | ||
| 178 | |||
| 179 | # Plain hashrefs keyed table.field, with no row objects at all. | ||
| 180 | my @raw = $orm->handle('users') | ||
| 181 | ->join('posts') | ||
| 182 | ->row_class(undef) | ||
| 183 | ->all; | ||
| 184 | |||
| 185 | Results are flat: a user with two posts appears in two rows, and both rows in | ||
| 186 | this result share one user object. | ||
| 187 | |||
| 188 | =head1 WRITING AND FOLLOWING KEYS | ||
| 189 | |||
| 190 | my $pair = $orm->handle('posts') | ||
| 191 | ->join('users') | ||
| 192 | ->where({'posts.id' => $post_id}) | ||
| 193 | ->only; | ||
| 194 | |||
| 195 | $pair->row('posts')->update({views => 11}); # write through the table's row | ||
| 196 | my @posts = $pair->row('users')->follow_fk('posts')->all; | ||
| 197 | |||
| 198 | A join row has no write methods. Following keys from join rows: | ||
| 199 | L<DBIx::QuickORM::Manual::ForeignKeys/"FROM AGGREGATE AND JOIN ROWS">. | ||
| 200 | |||
| 201 | =cut | ||
| 202 | |||
| 203 | 1; | ||
| 204 | |||
| 205 | __END__ |