blib/lib/DBIx/QuickORM/Manual/Classes.pm untested

filter: Statementstmt 0.0% 0 / 8 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 2 Podpod n/a totaltotal 0.0% 0 / 10 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package DBIx::QuickORM::Manual::Classes;
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::Classes - Defining an ORM as classes: the orm, table, and row blocks, by example.
14  
15 =head1 DESCRIPTION
16  
17 You can define your ORM in a Perl package. The C<orm> and C<table> blocks
18 collect the arguments you would otherwise pass to C<new>. A C<row> block sets
19 a row class's parent or applies a role to it
20 (L<DBIx::QuickORM::Manual::RowClasses>).
21  
22 The ORM reads the columns from the database, so the package declares none. A
23 table needs its own class only to adjust what the scan found, or to give its
24 rows methods.
25  
26 =head1 SCHEMA
27  
28 The examples use PostgreSQL and this schema:
29  
30 =for highlighter language=sql
31  
32 CREATE TABLE users (
33 id SERIAL PRIMARY KEY,
34 name TEXT NOT NULL,
35 email TEXT UNIQUE,
36 prefs JSON,
37 referred_by INTEGER REFERENCES users (id),
38 created TIMESTAMP NOT NULL DEFAULT now()
39 );
40  
41 CREATE TABLE posts (
42 id SERIAL PRIMARY KEY,
43 user_id INTEGER NOT NULL REFERENCES users (id),
44 title TEXT NOT NULL,
45 views INTEGER NOT NULL DEFAULT 0
46 );
47  
48 CREATE TABLE events (
49 id CHAR(36) PRIMARY KEY,
50 user_id INTEGER NOT NULL REFERENCES users (id),
51 type TEXT NOT NULL,
52 data JSON
53 );
54  
55 =head1 THE ORM CLASS
56  
57 F<lib/My/App.pm>:
58  
59 =for highlighter language=perl
60  
61 package My::App;
62 use DBIx::QuickORM::DSL ':orm';
63  
64 orm {
65 # rows of every table, unless a table class says otherwise
66 row_class 'My::App::Row';
67 override(omit => {posts => ['views']}); # posts needs nothing else, so it has no class
68 };
69  
70 1;
71  
72 C<override> takes any argument of C<new> except the connection arguments
73 (C<dbh>, C<connect>, C<credentials>). C<row_class>, C<autotype>,
74 C<append_autotype>, and C<retain_cache> are keywords for the arguments of the
75 same names (L<DBIx::QuickORM::Manual::Types>,
76 L<DBIx::QuickORM::Manual::Caching>). C<tables> is the DSL's own (below).
77  
78 C<new> loads every module directly under F<My/App/>. Those with a C<table>
79 block adjust their table. The rest (row classes, say) are only loaded.
80  
81 =head1 A ROW CLASS FOR EVERY TABLE
82  
83 F<lib/My/App/Row.pm>:
84  
85 package My::App::Row;
86 use DBIx::QuickORM::DSL ':row';
87  
88 row {
89 parent; # DBIx::QuickORM::Row
90 };
91  
92 sub label { my $self = shift; $self->table . ' #' . $self->field('id') }
93  
94 1;
95  
96 C<parent> loads the class and adds it to C<@ISA>, so there is no C<use
97 parent>. C<with_role> applies a role instead (L<DBIx::QuickORM::DSL>).
98  
99 =head1 A TABLE CLASS
100  
101 F<lib/My/App/Users.pm>, a table class that is also its table's row class:
102  
103 package My::App::Users;
104 use DBIx::QuickORM::DSL qw/:table :row/;
105  
106 table {
107 name 'users'; # required: the package name decides nothing
108 omit qw/prefs/; # selected only when asked for
109 type created => undef; # plain text instead of a DateTime
110 };
111  
112 row {
113 parent 'My::App::Row';
114 };
115  
116 no DBIx::QuickORM::DSL; # the keywords are gone, so a method may take a keyword's name
117  
118 sub name { $_[0]->field('name') }
119  
120 1;
121  
122 A package with both blocks is its table's row class once both have run. Its
123 C<row_class> keyword may only name itself.
124  
125 Importing a tag registers nothing. Only a block that runs does. A package
126 that imports C<:row> but has no C<row> block is not a row class: its table
127 keeps the default row class.
128  
129 Each block removes its keywords when it finishes. The C<no> matters only for
130 a sub named like a keyword: Perl compiles subs before any block runs, so
131 without it C<sub name> would replace the keyword and the C<table> block would
132 croak. Renaming the keyword (next section) is the other way out.
133  
134 The other table keywords: C<default_fields> instead of C<omit> (below), and
135 C<< fk $table => {field => 'their_field'} >> for a foreign key the database
136 does not declare (L<DBIx::QuickORM::Manual::ForeignKeys>). A table class
137 without a C<row> block may name its row class with C<row_class>: another
138 package, or undef for hashrefs.
139  
140 =head1 TYPES WITH HINTS, AND A RENAMED KEYWORD
141  
142 F<lib/My/App/Events.pm>:
143  
144 package My::App::Events;
145 # frees 'type' for the method below
146 use DBIx::QuickORM::DSL qw/:table :row/, type => {-as => 'col_type'};
147  
148 table {
149 name 'events';
150 default_fields qw/id user_id type/; # data only when asked for
151 col_type id => {
152 class => 'UUID', # generated when an insert leaves it out
153 format => sub { uc $_[0] }, # stored upper-case
154 };
155 };
156  
157 row {
158 parent 'My::App::Row';
159 };
160  
161 sub type { $_[0]->field('type') }
162  
163 1;
164  
165 C<type> takes a type name (C<JSON>, or C<+My::Type> for a full class name),
166 undef, or a hashref of options: the hints C<format> and C<generate>, and
167 C<class>. A hashref without C<class> keeps the field's type. See
168 L<DBIx::QuickORM::Manual::Types> and L<DBIx::QuickORM::Manual::Keys>.
169  
170 =head1 USING IT
171  
172 use My::App;
173  
174 my $orm = My::App->new(
175 credentials => {
176 dsn => $dsn,
177 user => $user,
178 pass => $pass,
179 },
180 );
181  
182 my $ann = $orm->insert(users => {
183 name => 'Ann',
184 prefs => {theme => 'dark'},
185 });
186 my $name = $ann->name; # 'Ann'
187  
188 my $post = $orm->insert(posts => {
189 user_id => $ann->field('id'),
190 title => 'Hi',
191 });
192 my $label = $post->label; # 'posts #1', from My::App::Row: posts has no class of its own
193  
194 my $event = $orm->insert(events => {
195 user_id => $ann->field('id'),
196 type => 'login',
197 });
198 my $kind = $event->type; # 'login'
199 my $id = $event->raw_field('id'); # a version 7 UUID in upper case, generated for the insert
200  
201 =head1 PRECEDENCE
202  
203 my $other = My::App->new(
204 credentials => {
205 dsn => $dsn,
206 user => $user,
207 pass => $pass,
208 },
209 # replaces the Users class's omit, for this ORM only
210 omit => {users => ['prefs', 'email']},
211 );
212  
213 Lowest to highest: the C<orm> block's keywords, its C<override>, the table
214 classes, the arguments to C<new>.
215  
216 C<omit>, C<default_fields>, and C<row_classes> merge per table: a higher
217 source's entry for a table replaces the lower one's whole entry for that
218 table, and other tables keep theirs. C<omit> and C<default_fields> count as
219 one setting: either replaces both.
220  
221 C<types> merge per field, and C<fks> per target table: typing one field in
222 C<new> keeps the table class's types for the others.
223  
224 =head1 NAMING THE TABLE CLASSES
225  
226 package My::Listed;
227 use DBIx::QuickORM::DSL ':orm';
228  
229 orm {
230 tables ['My::App::Users']; # only these: nothing under My::Listed:: is searched for
231 };
232  
233 1;
234  
235 C<tables> also takes a namespace (C<'My::App'>) to search instead of the ORM
236 class's own.
237  
238 =cut
239  
240 1;
241  
242 __END__