| 1 | package DBIx::QuickORM::Manual::Keys; | ||
| 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.000032'; | |
| 6 | |||
| 7 | =pod | ||
| 8 | |||
| 9 | =encoding UTF-8 | ||
| 10 | |||
| 11 | =head1 NAME | ||
| 12 | |||
| 13 | DBIx::QuickORM::Manual::Keys - Primary keys: their forms, typed keys, the format hint, and generated keys, by example. | ||
| 14 | |||
| 15 | =head1 DESCRIPTION | ||
| 16 | |||
| 17 | Every primary key argument (C<find>, C<clear_cache>, C<refresh_row>) takes | ||
| 18 | the same four forms. A typed key (UUID, DateTime) matches the held row in any | ||
| 19 | form, and a UUID key can be generated when an insert leaves it out. | ||
| 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 | CREATE TABLE memberships ( | ||
| 44 | user_id INTEGER NOT NULL REFERENCES users (id), | ||
| 45 | team TEXT NOT NULL, | ||
| 46 | role TEXT, | ||
| 47 | PRIMARY KEY (user_id, team) | ||
| 48 | ); | ||
| 49 | |||
| 50 | CREATE TABLE sessions (id UUID PRIMARY KEY, owner TEXT); | ||
| 51 | CREATE TABLE events (id UUID PRIMARY KEY DEFAULT gen_random_uuid(), name TEXT); | ||
| 52 | CREATE TABLE snapshots (taken TIMESTAMPTZ PRIMARY KEY, note TEXT); | ||
| 53 | CREATE TABLE tokens (token CHAR(36) PRIMARY KEY, note TEXT); | ||
| 54 | |||
| 55 | C<gen_random_uuid()> is built into PostgreSQL 13 and later. Earlier versions | ||
| 56 | need the C<pgcrypto> extension. | ||
| 57 | |||
| 58 | =head1 KEY FORMS | ||
| 59 | |||
| 60 | =for highlighter language=perl | ||
| 61 | |||
| 62 | my $ann = $orm->find(users => $id); # a single value, for a single-field key | ||
| 63 | $ann = $orm->find(users => [$id]); # an arrayref of the values, in the key's declared order | ||
| 64 | |||
| 65 | # a hashref of the key fields (other fields are ignored, so row data works) | ||
| 66 | $ann = $orm->find(users => {id => $id}); | ||
| 67 | |||
| 68 | # a row object (or its row data): read Ann's row again | ||
| 69 | my $fresh = $orm->find( | ||
| 70 | users => $ann, | ||
| 71 | cache => 0, | ||
| 72 | ); | ||
| 73 | |||
| 74 | $orm->insert(memberships => { | ||
| 75 | user_id => $id, | ||
| 76 | team => 'ops', | ||
| 77 | role => 'lead', | ||
| 78 | }); | ||
| 79 | |||
| 80 | my $member = $orm->find(memberships => { | ||
| 81 | user_id => $id, # a compound key | ||
| 82 | team => 'ops', | ||
| 83 | }); | ||
| 84 | |||
| 85 | $member = $orm->find(memberships => [$id, 'ops']); # user_id, then team: the declared order | ||
| 86 | my $pk = $member->pk; # {user_id => $id, team => 'ops'} | ||
| 87 | |||
| 88 | $orm->clear_cache(memberships => [$id, 'ops']); # the same forms | ||
| 89 | |||
| 90 | A single value for a compound key croaks, as does an arrayref of the wrong | ||
| 91 | length or a missing key field. | ||
| 92 | |||
| 93 | =head1 TYPED KEYS | ||
| 94 | |||
| 95 | my $uuid = '0190a6e8-1d2c-7c3b-9f00-123456789abc'; | ||
| 96 | my $session = $orm->insert(sessions => { | ||
| 97 | id => $uuid, | ||
| 98 | owner => 'Ann', | ||
| 99 | }); | ||
| 100 | |||
| 101 | my $same = $orm->find(sessions => uc $uuid); # the held row: a UUID key matches in upper or lower case | ||
| 102 | |||
| 103 | use DateTime; | ||
| 104 | |||
| 105 | my $taken = DateTime->new( | ||
| 106 | year => 2024, | ||
| 107 | month => 1, | ||
| 108 | day => 2, | ||
| 109 | hour => 3, | ||
| 110 | time_zone => 'America/New_York', | ||
| 111 | ); | ||
| 112 | my $snap = $orm->insert(snapshots => { | ||
| 113 | taken => $taken, | ||
| 114 | note => 'nightly', | ||
| 115 | }); | ||
| 116 | |||
| 117 | # the held row: the same instant | ||
| 118 | my $again = $orm->find(snapshots => $taken->clone->set_time_zone('UTC')); | ||
| 119 | |||
| 120 | The ORM keys each held row (L<DBIx::QuickORM::Manual::Caching>) by its type's | ||
| 121 | I<canonical form>: one spelling for every form of the same value (lower-case | ||
| 122 | text for a UUID, the UTC instant for a timestamp with time zone). So every | ||
| 123 | form of one key finds the same row. The database gets the value as you pass | ||
| 124 | it, deflated. | ||
| 125 | |||
| 126 | A native C<uuid> column gets the UUID type from the scan. So does a text or | ||
| 127 | binary one with C<uuid> as a word of its name (C<uuid>, C<owner_uuid>, | ||
| 128 | C<uuid_v7>) and either no declared size or a UUID's (C<CHAR(36)>, | ||
| 129 | C<BINARY(16)>). Any other column gets no UUID type from the scan, so C<types> | ||
| 130 | names it, as C<tokens> shows next | ||
| 131 | (L<DBIx::QuickORM::Manual::Types/"REGISTERING TYPES">). | ||
| 132 | |||
| 133 | =head1 THE FORMAT HINT | ||
| 134 | |||
| 135 | my $legacy = DBIx::QuickORM->new( | ||
| 136 | credentials => { | ||
| 137 | dsn => $dsn, | ||
| 138 | user => $user, | ||
| 139 | pass => $pass, | ||
| 140 | }, | ||
| 141 | types => { | ||
| 142 | tokens => { | ||
| 143 | token => { | ||
| 144 | class => 'UUID', | ||
| 145 | format => sub { uc $_[0] }, # the table stores upper-case text | ||
| 146 | }, | ||
| 147 | }, | ||
| 148 | }, | ||
| 149 | ); | ||
| 150 | |||
| 151 | my $token = $legacy->insert(tokens => { | ||
| 152 | token => $uuid, | ||
| 153 | note => 'legacy', | ||
| 154 | }); | ||
| 155 | |||
| 156 | my $text = $token->raw_field('token'); # '0190A6E8-1D2C-7C3B-9F00-123456789ABC', as stored | ||
| 157 | |||
| 158 | my $found = $legacy->find( | ||
| 159 | tokens => $uuid, | ||
| 160 | cache => 0, # the database gets the upper-case form too | ||
| 161 | ); | ||
| 162 | |||
| 163 | C<format> runs on the text a type's deflation produces for a non-binary | ||
| 164 | field: inserted values, changes, and key lookups. A text column compares | ||
| 165 | exact text, so without the hint a lower-case UUID would never match this | ||
| 166 | table. | ||
| 167 | |||
| 168 | Values sent as given bypass it: C<raw> values, and the values of a C<where>. | ||
| 169 | Wrap a C<where> value in C<inflated()> to deflate and format it | ||
| 170 | (L<DBIx::QuickORM::Manual::Values>). | ||
| 171 | |||
| 172 | C<< type_options => {UUID => {format => sub { uc $_[0] }}} >> sets the | ||
| 173 | default for UUID fields. A field's own C<format> wins. | ||
| 174 | |||
| 175 | =head1 GENERATED KEYS | ||
| 176 | |||
| 177 | my $new = $orm->insert(sessions => {owner => 'Bob'}); # id left out: a new version 7 UUID | ||
| 178 | my $id7 = $new->field('id'); | ||
| 179 | |||
| 180 | # the database fills id (gen_random_uuid), so the ORM leaves it alone | ||
| 181 | my $event = $orm->insert(events => {name => 'start'}); | ||
| 182 | |||
| 183 | Only a type with C<generate_key_value> generates a key. Of the built-in types | ||
| 184 | only UUID has one, so a DateTime key is never generated. A UUID primary key | ||
| 185 | an insert leaves out gets a value when the database does not fill the column. | ||
| 186 | The C<generate> hint changes that: | ||
| 187 | |||
| 188 | my $v7_only = DBIx::QuickORM->new( | ||
| 189 | credentials => { | ||
| 190 | dsn => $dsn, | ||
| 191 | user => $user, | ||
| 192 | pass => $pass, | ||
| 193 | }, | ||
| 194 | # always generate, even over the database default | ||
| 195 | types => {events => {id => {generate => 'prefer_orm'}}}, | ||
| 196 | ); | ||
| 197 | |||
| 198 | my $forced = $v7_only->insert(events => {name => 'forced'}); | ||
| 199 | |||
| 200 | my $never = DBIx::QuickORM->new( | ||
| 201 | credentials => { | ||
| 202 | dsn => $dsn, | ||
| 203 | user => $user, | ||
| 204 | pass => $pass, | ||
| 205 | }, | ||
| 206 | type_options => {UUID => {generate => 'none'}}, # never generate, for every UUID field | ||
| 207 | ); | ||
| 208 | |||
| 209 | # the database rejects the missing key | ||
| 210 | my $ok = eval { $never->insert(sessions => {owner => 'Cy'}); 1 }; | ||
| 211 | |||
| 212 | C<prefer_db> is the default. On MySQL, which cannot return a key the database | ||
| 213 | filled (L<DBIx::QuickORM::Manual::Dialects/RETURNING>), C<prefer_db> generates | ||
| 214 | an omitted UUID anyway. An C<AUTO_INCREMENT> id is still recovered after a | ||
| 215 | single-row insert. A given value always wins, and a compound key's typed part | ||
| 216 | is generated the same way. | ||
| 217 | |||
| 218 | =cut | ||
| 219 | |||
| 220 | 2 | 1; | |
| 221 | |||
| 222 | __END__ | ||