File Coverage

blib/lib/DBIx/QuickORM.pm
Criterion Covered Total %
statement 732 773 94.7
branch 326 416 78.3
condition 142 217 65.4
subroutine 145 149 97.3
pod 44 51 86.2
total 1389 1606 86.4


line stmt bran cond sub pod time code
1             package DBIx::QuickORM;
2 377     726   84802713 use strict;
  377         936  
  377         14603  
3 377     692   4708 use warnings;
  377         876  
  377         20453  
4 377     377   2850 use feature qw/state/;
  377         905  
  377         73197  
5              
6             our $VERSION = '0.000019';
7              
8 377     377   3301 use Carp qw/croak confess/;
  377         1697  
  377         37394  
9             $Carp::Internal{ (__PACKAGE__) }++;
10              
11 377     377   257482 use Storable qw/dclone/;
  377         1927700  
  377         34443  
12 377     377   218809 use Sub::Util qw/set_subname/;
  377         135051  
  377         29307  
13 377     377   3477 use Scalar::Util qw/blessed/;
  377         996  
  377         21090  
14              
15 377     377   209951 use Scope::Guard();
  377         225087  
  377         11443  
16 377     377   225300 use DBIx::QuickORM::Schema::Autofill();
  377         1568  
  377         16972  
17              
18 377     377   2714 use DBIx::QuickORM::Util qw/load_class find_modules/;
  377         769  
  377         3843  
19 377     377   249366 use DBIx::QuickORM::Affinity qw/validate_affinity/;
  377         1341  
  377         33491  
20              
21 377     377   3062 use constant DBS => 'dbs';
  377         1131  
  377         33173  
22 377     377   2414 use constant ORMS => 'orms';
  377         1024  
  377         20027  
23 377     377   2397 use constant PACKAGE => 'package';
  377         708  
  377         20708  
24 377     377   2127 use constant SCHEMAS => 'schemas';
  377         710  
  377         19268  
25 377     377   1975 use constant SERVERS => 'servers';
  377         903  
  377         16937  
26 377     377   1991 use constant STACK => 'stack';
  377         753  
  377         18150  
27 377     377   2307 use constant TYPE => 'type';
  377         1174  
  377         251932  
28              
29             my @EXPORT = qw{
30             plugin
31             plugins
32             meta
33             orm
34             handle_class
35             autofill
36             autotype
37             autohook
38             autoskip
39             autorow
40             autoname
41             alt
42              
43             build_class
44              
45             server
46             driver
47             dialect
48             attributes
49             host hostname
50             port
51             socket
52             user username
53             pass password
54             creds
55             db
56             connect
57             dsn
58              
59             schema
60             row_class
61             tables
62             table
63             view
64             db_name
65             column
66             omit
67             nullable
68             not_null
69             identity
70             affinity
71             type
72             sql
73             default
74             columns
75             primary_key
76             unique
77             index
78             link
79             };
80              
81             sub import {
82 395     395   3681730 my $class = shift;
83 395         1430 my %params = @_;
84              
85 395   100     3497 my $type = $params{type} // 'orm';
86 395   100     4064 my $rename = $params{rename} // {};
87 395   50     3568 my $skip = $params{skip} // {};
88 395         1124 my $only = $params{only};
89              
90 395 100       1651 $only = {map {($_ => 1)} @$only} if $only;
  61         156  
91              
92 395         1472 my $caller = caller;
93              
94 395         2106 my $builder = $class->new(PACKAGE() => $caller, TYPE() => $type);
95              
96             my %export = (
97 395     3   6385 builder => set_subname("${caller}::builder" => sub { $builder }),
  3     3   5036  
98             );
99              
100 395 100       1987 if ($type eq 'orm') {
101 393     18   4077 $export{import} = set_subname("${caller}::import" => sub { shift; $builder->import_into(scalar(caller), @_) }),
  18     18   1018  
  18         126  
102             }
103              
104 395         1502 for my $name (@EXPORT) {
105 19355         33712 my $meth = $name;
106 19355 50 100 564   169080 $export{$name} //= set_subname("${caller}::$meth" => sub { shift @_ if @_ && $_[0] && "$_[0]" eq $caller; $builder->$meth(@_) });
  564   66 564   28844090  
  564   33 876   6827  
        1188      
        1188      
        1188      
        1188      
        1188      
        1188      
        1188      
        1188      
        1188      
        1188      
        1188      
        876      
        876      
        1222      
        1222      
        1188      
        1222      
        1188      
        1188      
        1188      
        1188      
        1188      
        1188      
        1188      
        1188      
        1188      
        1188      
        1188      
        1188      
        1188      
        1188      
        1188      
        1188      
        1500      
        1188      
        1188      
        1188      
        1188      
        1188      
        1188      
        1188      
        1188      
        1188      
        1188      
        1188      
        1188      
        1188      
107             }
108              
109 395         1182 my %seen;
110 395         4288 for my $sym (keys %export) {
111 20143   66     61427 my $name = $rename->{$sym} // $sym;
112 20143 50 33     69682 next if $skip->{$name} || $skip->{$sym};
113 20143 100 66     81700 next if $only && !($only->{$name} || $only->{$sym});
      100        
114 19389 100       51390 next if $seen{$name}++;
115 377     377   3136 no strict 'refs';
  377         724  
  377         160716  
116 19388         30416 *{"${caller}\::${name}"} = $export{$sym};
  19388         100504  
117             }
118             }
119              
120             sub _caller {
121 450     1074   989 my $self = shift;
122              
123 450         818 my $i = 0;
124 450         6972 while (my @caller = caller($i++)) {
125 2224 50       12380 return unless @caller;
126 2224 100       36011 next if eval { $caller[0]->isa(__PACKAGE__) };
  2224         54803  
127 450         1918 return \@caller;
128             }
129              
130 0         0 return;
131             }
132              
133             sub unimport {
134 0     312   0 my $class = shift;
135 0         0 my $caller = caller;
136              
137 0         0 $class->unimport_from($caller);
138             }
139              
140             sub unimport_from {
141 2     2 0 4 my $class = shift;
142 2         6 my ($caller) = @_;
143              
144 377     377   3656 my $stash = do { no strict 'refs'; \%{"$caller\::"} };
  377         1130  
  377         66694  
  2         5  
  2         4  
  2         12  
145              
146 2         6 for my $item (@EXPORT) {
147 98 50       331 my $export = $class->can($item) or next;
148 98 50       399 my $sub = $caller->can($item) or next;
149              
150 98 50       279 next unless $export == $sub;
151              
152 0         0 my $glob = delete $stash->{$item};
153              
154             {
155 377     377   3089 no strict 'refs';
  377         1069  
  377         17217  
  0         0  
156 377     377   2290 no warnings 'redefine';
  377         1063  
  377         183149  
157              
158 0         0 for my $type (qw/SCALAR HASH ARRAY FORMAT IO/) {
159 0 0       0 next unless defined(*{$glob}{$type});
  0         0  
160 0         0 *{"$caller\::$item"} = *{$glob}{$type};
  0         0  
  0         0  
161             }
162             }
163             }
164             }
165              
166             sub new {
167 395     395 1 1046 my $class = shift;
168 395         2095 my %params = @_;
169              
170 395 50       1742 croak "'package' is a required attribute" unless $params{+PACKAGE};
171              
172 395   50     5125 $params{+STACK} //= [{base => 1, plugins => [], building => '', build => 'Should not access this', meta => 'Should not access this'}];
173              
174 395   50     2728 $params{+ORMS} //= {};
175 395   50     2486 $params{+DBS} //= {};
176 395   50     3408 $params{+SCHEMAS} //= {};
177 395   50     2545 $params{+SERVERS} //= {};
178              
179 395         1580 return bless(\%params, $class);
180             }
181              
182             sub import_into {
183 18     18 0 124 my $self = shift;
184 18         56 my ($caller, $name, @extra) = @_;
185              
186 18 50       55 croak "Not enough arguments, caller is required" unless $caller;
187 18 50       158 croak "Too many arguments" if @extra;
188              
189 18   100     152 $name //= 'qorm';
190              
191 377     377   3183 no strict 'refs';
  377         1355  
  377         1185283  
192 18         145 *{"${caller}\::${name}"} = sub {
193 12 100   12   620575 return $self unless @_;
194              
195 11         62 my ($type, $name, @extra) = @_;
196              
197 11 100       116 if ($type =~ m/^(orm|db|schema)$/) {
198 7 100       154 croak "Too many arguments" if @extra;
199 6         74 return $self->$type($name);
200             }
201              
202 4         19 return $self->orm(@_)->connection;
203 18         131 };
204             }
205              
206             sub top {
207 466     466 0 2558 my $self = shift;
208 466         1948 return $self->{+STACK}->[-1];
209             }
210              
211             sub alt {
212 9     9 1 18 my $self = shift;
213 9         28 my $top = $self->top;
214 9 100       237 croak "alt() cannot be used outside of a builder" if $top->{base};
215 8         25 my ($name, $builder) = @_;
216              
217 8   50     64 my $frame = $top->{alt}->{$name} // {building => $top->{building}, name => $name, meta => {}};
218             return $self->_build(
219             'Alt',
220             into => $top->{alt} //= {},
221 8   50     45 frame => $frame,
222             args => [$name, $builder],
223             );
224             }
225              
226             sub plugin {
227 11     11 1 22 my $self = shift;
228 11         28 my ($proto, @proto_params) = @_;
229              
230 11 100       30 if (blessed($proto)) {
231 3 100       163 croak "Cannot pass in both a blessed plugin instance and constructor arguments" if @proto_params;
232 2 100       20 if ($proto->isa('DBIx::QuickORM::Plugin')) {
233 1         2 push @{$self->top->{plugins}} => $proto;
  1         4  
234 1         4 return $proto;
235             }
236 1         133 croak "$proto is not an instance of 'DBIx::QuickORM::Plugin' or a subclass of it";
237             }
238              
239 8 100       29 my $class = load_class($proto, 'DBIx::QuickORM::Plugin') or croak "Could not load plugin '$proto': $@";
240 7 100       173 croak "$class is not a subclass of DBIx::QuickORM::Plugin" unless $class->isa('DBIx::QuickORM::Plugin');
241              
242 6 100       20 my $params = @proto_params == 1 ? shift(@proto_params) : { @proto_params };
243              
244 6         29 my $plugin = $class->new(%$params);
245 6         10 push @{$self->top->{plugins}} => $plugin;
  6         18  
246 6         24 return $plugin;
247             }
248              
249             sub plugins {
250 174     174 1 445 my $self = shift;
251              
252             # Return a list of plugins if no arguments were provided
253 174 100 50     561 return [map { @{$_->{plugins} // []} } reverse @{$self->{+STACK}}]
  497         777  
  497         1968  
  173         555  
254             unless @_;
255              
256 1         2 my @out;
257              
258 1         6 while (my $proto = shift @_) {
259 4 100 100     20 if (@_ && ref($_[0]) eq 'HASH') {
260 2         5 my $params = shift @_;
261 2         5 push @out => $self->plugin($proto, $params);
262             }
263             else {
264 2         6 push @out => $self->plugin($proto);
265             }
266             }
267              
268 1         4 return \@out;
269             }
270              
271             sub meta {
272 12     12 1 25 my $self = shift;
273              
274 12 100       22 croak "Cannot access meta without a builder" unless @{$self->{+STACK}} > 1;
  12         191  
275 11         35 my $top = $self->top;
276              
277 11 100       118 return $top->{meta} unless @_;
278              
279 1         3 %{$top->{meta}} = (%{$top->{meta}}, @_);
  1         9  
  1         4  
280              
281 1         4 return $top->{meta};
282             }
283              
284             sub build_class {
285 5     5 1 17 my $self = shift;
286              
287 5 100       111 croak "Not enough arguments" unless @_;
288              
289 4         147 my ($proto) = @_;
290              
291 4 100       118 croak "You must provide a class name" unless $proto;
292              
293 3 100       13 my $class = load_class($proto) or croak "Could not load class '$proto': $@";
294              
295 2 100       5 croak "Cannot set the build class without a builder" unless @{$self->{+STACK}} > 1;
  2         140  
296              
297 1         6 $self->top->{class} = $class;
298             }
299              
300             sub server {
301 2     2 1 5 my $self = shift;
302              
303 2         8 my $top = $self->top;
304 2         6 my $into = $self->{+SERVERS};
305 2         9 my $frame = {building => 'SERVER'};
306              
307 2         12 return $self->_build('Server', into => $into, frame => $frame, args => \@_);
308             }
309              
310             sub db {
311 68     68 1 535 my $self = shift;
312              
313 68         674 my $top = $self->top;
314              
315 68         275 my $bld_orm = 0;
316 68 100       446 if ($top->{building} eq 'ORM') {
317 24 50       164 croak "DB has already been defined" if $top->{meta}->{db};
318 24         67 $bld_orm = 1;
319             }
320              
321 68 100 100     1787 if (@_ == 1 && $_[0] =~ m/^(\S+)\.([^:\s]+)(?::(\S+))?$/) {
322 8         47 my ($server_name, $db_name, $variant_name) = ($1, $2, $3);
323              
324 8 50       32 my $server = $self->{+SERVERS}->{$server_name} or croak "'$server_name' is not a defined server";
325 8 50       49 my $db = $server->{meta}->{dbs}->{$db_name} or croak "'$db_name' is not a defined database on server '$server_name'";
326              
327 8 100       23 return $top->{meta}->{db} = $db if $bld_orm;
328 7         53 return $self->compile($db, $variant_name);
329             }
330              
331 60         410 my $into = $self->{+DBS};
332 60         504 my $frame = {building => 'DB', class => 'DBIx::QuickORM::DB'};
333              
334 60 100       798 return $top->{meta}->{db} = $self->_build('DB', into => $into, frame => $frame, args => \@_, no_compile => 1)
335             if $bld_orm;
336              
337 37         159 my $force_build = 0;
338 37 100       260 if ($top->{building} eq 'SERVER') {
339 3         6 $force_build = 1;
340              
341             $frame = {
342             %$frame,
343 3         11 %{$top},
344             building => 'DB',
345 3         43 meta => {%{$top->{meta}}},
346             server => $top->{name} // $top->{created},
347 3   33     9 };
348              
349 3         11 delete $frame->{name};
350 3         6 delete $frame->{meta}->{name};
351 3         8 delete $frame->{meta}->{dbs};
352 3 50       11 delete $frame->{prefix} unless defined $frame->{prefix};
353              
354 3   100     15 $into = $top->{meta}->{dbs} //= {};
355             }
356              
357 37         1220 return $self->_build('DB', into => $into, frame => $frame, args => \@_, force_build => $force_build);
358             }
359              
360             sub handle_class {
361 2     2 0 3 my $self = shift;
362 2         5 my ($proto) = @_;
363              
364 2         8 my $top = $self->_in_builder(qw{orm});
365              
366 2 50       11 $top->{meta}->{default_handle_class} = load_class($proto, 'DBIx::QuickORM::Handle') or croak "Could not load handle class '$proto': $@";
367              
368 2         4 return;
369             }
370              
371             sub autofill {
372 37     37 1 101 my $self = shift;
373              
374 37         174 my $top = $self->_in_builder(qw{orm});
375              
376 37         447 my $frame = {building => 'AUTOFILL', class => 'DBIx::QuickORM::Schema::Autofill', meta => {}};
377              
378 37 50 66     328 if (@_ && !ref($_[0])) {
379 0         0 my $proto = shift @_;
380 0 0       0 $frame->{class} = load_class($proto, 'DBIx::QuickORM::Schema::Autofill') or croak "Could not load autofill class '$proto': $@";
381             }
382              
383 37 100       167 if (!@_) {
384 29         89 $top->{meta}->{autofill} = $frame;
385 29         129 return;
386             }
387              
388 8         53 $top->{meta}->{autofill} = $self->_build('AUTOFILL', frame => $frame, args => \@_, no_compile => 1);
389             }
390              
391             sub autotype {
392 5     5 1 35 my $self = shift;
393 5         35 my ($type) = @_;
394              
395 5         178 my $top = $self->_in_builder(qw{autofill});
396              
397 5 50       664 my $class = load_class($type, 'DBIx::QuickORM::Type') or croak "Could not load type '$type': $@";
398              
399 5   50     178 $class->qorm_register_type($top->{meta}->{types} //= {}, $top->{meta}->{affinities} //= {});
      50        
400              
401 5         29 return;
402             }
403              
404             sub autorow {
405 2     2 1 7 my $self = shift;
406 2         19 my ($base, $name_to_class) = @_;
407              
408 2         9 my $top = $self->_in_builder(qw{autofill});
409 2 50       925 croak "autorow already set" if $top->{autorow};
410              
411 2         111 my $caller = $self->_caller;
412              
413             $name_to_class //= sub {
414 5     5   10 my $name = shift;
415 5         24 my @parts = split /_/, $name;
416 5         15 return join '' => map { ucfirst(lc($_)) } @parts;
  5         31  
417 2   33     73 };
418              
419 2         10 $top->{autorow} = $base;
420              
421 2         5 local $@;
422 2 50 33     13 my $parent = load_class($base) // load_class('DBIx::QuickORM::Row') or die $@;
423             $self->autohook(post_table => sub {
424 5     5   17 my %params = @_;
425 5         16 my $autofill = $params{autofill};
426 5         12 my $table = $params{table};
427              
428 5         16 my $postfix = $name_to_class->($table->{name});
429 5         16 my $package = "$base\::$postfix";
430              
431 5         9 local $@;
432 5         22 my $loaded = load_class($package);
433              
434 377     377   3588 my $isa = do { no strict 'refs'; \@{"$package\::ISA"} };
  377         1233  
  377         1379538  
  5         19  
  5         8  
  5         89  
435 5 50       71 push @$isa => $parent unless @$isa;
436              
437 5         12 my $file = $package;
438 5         24 $file =~ s{::}{/};
439 5         13 $file .= ".pm";
440 5   33     65 $INC{$file} ||= $caller->[1];
441              
442 5         13 $table->{row_class} = $package;
443 5         73 $table->{row_class_autofill} = $autofill;
444 2         38 });
445              
446 2         40 return;
447             }
448              
449             sub autoname {
450 4     4 1 14 my $self = shift;
451 4         16 my ($type, $callback) = @_;
452              
453 4         20 my $top = $self->_in_builder(qw{autofill});
454              
455 4 50       26 croak "autoname for '$type' already set" if $top->{autoname}->{$type};
456 4         93 $top->{autoname}->{$type} = 1;
457              
458 4 100       38 if ($type eq 'field_accessor') {
    100          
    50          
    50          
459             $self->autohook(field_accessor => sub {
460 12     12   60 my %params = @_;
461 12   33     43 return $callback->(%params) || $params{name};
462 1         27 });
463             }
464             elsif ($type eq 'link_accessor') {
465             $self->autohook(link_accessor => sub {
466 4     4   13 my %params = @_;
467 4   33     16 return $callback->(%params) || $params{name};
468 1         10 });
469             }
470             elsif ($type eq 'table') {
471             $self->autohook(pre_table => sub {
472 0     0   0 my %params = @_;
473 0   0     0 my $table = $params{table} // return;
474 0   0     0 $table->{name} = $callback->(table => $table, name => $table->{name}) || $table->{name};
475 0         0 });
476             }
477             elsif ($type eq 'link') {
478             $self->autohook(links => sub {
479 5     5   22 my %params = @_;
480              
481 5   50     19 my $links = $params{links} // return;
482 5 100       23 return unless @$links;
483 3         9 for my $link_pair (@$links) {
484 4         17 my ($a, $b) = @$link_pair;
485 4         9 my $table_a = $a->[0];
486 4         10 my $table_b = $b->[0];
487              
488 4 50       30 push @$a => $callback->(in_table => $a->[0], fetch_table => $b->[0], in_fields => $a->[1], fetch_fields => $b->[1])
489             unless @$a > 2; # Skip if it has an alias
490              
491 4 50       64 push @$b => $callback->(in_table => $b->[0], fetch_table => $a->[0], in_fields => $b->[1], fetch_fields => $a->[1])
492             unless @$b > 2; # Skip if it has an alias
493             }
494 2         65 });
495             }
496             else {
497 0         0 croak "'$type' is not a valid autoname() type";
498             }
499             }
500              
501             sub autohook {
502 6     6 1 15 my $self = shift;
503 6         36 my ($hook, $cb) = @_;
504              
505 6         29 my $top = $self->_in_builder(qw{autofill});
506              
507             croak "'$hook' is not a valid hook for $top->{class}"
508 6 50       157 unless $top->{class}->is_valid_hook($hook);
509              
510 6 50 33     93 croak "Second argument must be a coderef"
511             unless $cb && ref($cb) eq 'CODE';
512              
513 6   50     16 push @{$top->{meta}->{hooks}->{$hook} //= []} => $cb;
  6         93  
514              
515 6         3099 return;
516             }
517              
518             my %SKIP_TYPES = (
519             table => 1,
520             column => 2,
521             );
522              
523             sub autoskip {
524 0     0 1 0 my $self = shift;
525 0         0 my ($type, @args) = @_;
526              
527 0 0       0 my $cnt = $SKIP_TYPES{$type} or croak "'$type' is not a valid type to skip";
528 0 0       0 croak "Incorrect number of arguments" unless @args == $cnt;
529              
530 0         0 my $top = $self->_in_builder(qw{autofill});
531              
532 0         0 my $last = pop @args;
533 0   0     0 my $into = $top->{meta}->{skip}->{$type} //= {};
534 0         0 while (my $level = shift @args) {
535 0   0     0 $into = $into->{$level} //= {};
536             }
537 0         0 $into->{$last} = 1;
538             }
539              
540             sub driver {
541 1     1 1 3 my $self = shift;
542 1         3 my ($proto) = @_;
543              
544 1         4 my $top = $self->_in_builder(qw{db server});
545              
546 1 50       4 my $class = load_class($proto, 'DBD') or croak "Could not load DBI driver '$proto': $@";
547              
548 1         6 $top->{meta}->{dbi_driver} = $class;
549             }
550              
551             sub dialect {
552 33     33 1 166 my $self = shift;
553 33         136 my ($dialect) = @_;
554              
555 33         862 my $top = $self->_in_builder(qw{db server});
556              
557 33 50       6440 my $class = load_class($dialect, 'DBIx::QuickORM::Dialect') or croak "Could not load dialect '$dialect': $@";
558              
559 33         389 $top->{meta}->{dialect} = $class;
560             }
561              
562             sub connect {
563 23     23 1 961 my $self = shift;
564 23         781 my ($cb) = @_;
565              
566 23         227 my $top = $self->_in_builder(qw{db server});
567              
568 23 100       330 croak "connect must be given a coderef as its only argument, got '$cb' instead" unless ref($cb) eq 'CODE';
569              
570 22         115 $top->{meta}->{connect} = $cb;
571             }
572              
573             sub attributes {
574 3     3 1 6 my $self = shift;
575 3 100       17 my $attrs = @_ == 1 ? $_[0] : {@_};
576              
577 3         11 my $top = $self->_in_builder(qw{db server});
578              
579 3 100       122 croak "attributes() accepts either a hashref, or (key => value) pairs"
580             unless ref($attrs) eq 'HASH';
581              
582 2         7 $top->{meta}->{attributes} = $attrs;
583             }
584              
585             sub creds {
586 1     1 1 3 my $self = shift;
587 1         4 my ($in) = @_;
588              
589 1 50 33     9 croak "creds() accepts only a coderef as an argument" unless $in && ref($in) eq 'CODE';
590 1         4 my $data = $in->();
591              
592 1         9 my $top = $self->_in_builder(qw{db server});
593              
594 1 50 33     8 croak "The subroutine passed to creds() must return a hashref" unless $data && ref($data) eq 'HASH';
595              
596 1         3 my %creds;
597              
598 1 50       4 $creds{user} = $data->{user} or croak "No 'user' key in the hash returned by the credential subroutine";
599 1 50       5 $creds{pass} = $data->{pass} or croak "No 'pass' key in the hash returned by the credential subroutine";
600 1 50       5 $creds{socket} = $data->{socket} if $data->{socket};
601 1 50       5 $creds{host} = $data->{host} if $data->{host};
602 1 50       3 $creds{port} = $data->{port} if $data->{port};
603              
604 1 50 33     24 croak "Neither 'host' or 'socket' keys were provided by the credential subroutine" unless $creds{host} || $creds{socket};
605              
606 1         5 my @keys = keys %creds;
607 1   50     5 @{$top->{meta} // {}}{@keys} = @creds{@keys};
  1         5  
608              
609 1         7 return;
610             }
611              
612 1     1 1 5 sub dsn { $_[0]->_in_builder(qw{db server})->{meta}->{dsn} = $_[1] }
613 5     5 1 22 sub host { $_[0]->_in_builder(qw{db server})->{meta}->{host} = $_[1] }
614 3     3 1 10 sub port { $_[0]->_in_builder(qw{db server})->{meta}->{port} = $_[1] }
615 0     0 1 0 sub socket { $_[0]->_in_builder(qw{db server})->{meta}->{socket} = $_[1] }
616 5     5 1 21 sub user { $_[0]->_in_builder(qw{db server})->{meta}->{user} = $_[1] }
617 3     3 1 15 sub pass { $_[0]->_in_builder(qw{db server})->{meta}->{pass} = $_[1] }
618              
619             *hostname = \&host;
620             *username = \&user;
621             *password = \&pass;
622              
623             sub schema {
624 34     34 1 102 my $self = shift;
625              
626 34         93 my $into = $self->{+SCHEMAS};
627 34         237 my $frame = {building => 'SCHEMA', class => 'DBIx::QuickORM::Schema'};
628              
629 34         169 my $top = $self->top;
630 34 100       131 if ($top->{building} eq 'ORM') {
631 6 50       26 croak "Schema has already been defined" if $top->{meta}->{schema};
632 6         63 return $top->{meta}->{schema} = $self->_build('Schema', into => $into, frame => $frame, args => \@_, no_compile => 1);
633             }
634              
635 28         112 return $self->_build('Schema', into => $into, frame => $frame, args => \@_);
636             }
637              
638             sub tables {
639 2     2 1 5 my $self = shift;
640              
641 2         8 my $top = $self->_in_builder(qw{schema});
642 2   100     12 my $into = $top->{meta}->{tables} //= {};
643              
644 2         5 my (@modules, $cb);
645 2         5 for my $arg (@_) {
646 3 100       10 if (ref($arg) eq 'CODE') {
647 1 50       4 croak "Only 1 callback is supported" if $cb;
648 1         2 $cb = $arg;
649 1         3 next;
650             }
651              
652 2         6 push @modules => $arg;
653             }
654              
655 2   66 2   12 $cb //= sub { ($_[0]->{name}, $_[0]) };
  2         8  
656              
657 2         9 for my $mod (find_modules(@modules)) {
658 4         1462 my $table = $self->_load_table($mod);
659 4         44 my ($name, $data) = $cb->($table);
660 4 100 66     45 next unless $name && $data;
661 3         9 $into->{$name} = $data;
662             }
663              
664 2         40 return;
665             }
666              
667             sub _load_table {
668 7     7   14 my $self = shift;
669 7         18 my ($class) = @_;
670              
671 7 50       140 load_class($class) or croak "Could not load table class '$class': $@";
672 7 50       57 croak "Class '$class' does not appear to define a table (no qorm_table() method)" unless $class->can('qorm_table');
673 7 50       21 my $table = $class->qorm_table() or croak "Class '$class' appears to have an empty table";
674 7         24 return $table;
675             }
676              
677             sub table {
678 23     23 1 50 my $self = shift;
679 23         130 $self->_table('DBIx::QuickORM::Schema::Table', @_);
680             }
681              
682             sub view {
683 0     0 0 0 my $self = shift;
684 0         0 $self->_table('DBIx::QuickORM::Schema::View', @_);
685             }
686              
687             sub _table {
688 23     23   62 my $self = shift;
689 23         63 my $make = shift;
690              
691             # Defining a table in a table (row) class
692 23 100 66     45 if (@{$self->{+STACK}} == 1 && $self->{+TYPE} eq 'table') {
  23         204  
693 2         11 my $into = \($self->top->{table});
694 2         9 my $frame = {building => 'TABLE', class => $make};
695 2         14 $self->_build('Table', into => $into, frame => $frame, args => \@_);
696 2         5 my $table = $$into;
697              
698 2         12 $self->unimport_from($self->{+PACKAGE});
699              
700 2         6 my $pkg = $self->{+PACKAGE};
701 2   50     13 my $row_class = $table->{row_class} // '+DBIx::QuickORM::Row';
702 2 50       13 my $loaded_class = load_class($row_class, 'DBIx::QuickORM::Row') or croak "Could not load row class '$row_class': $@";
703 2         9 $table->{row_class} = $self->{+PACKAGE};
704 2         7 $table->{meta}->{row_class} = $self->{+PACKAGE};
705              
706             {
707 377     377   3663 no strict 'refs';
  377         1261  
  377         2826970  
  2         5  
708 2     10   10 *{"$pkg\::qorm_table"} = sub { dclone($table) };
  2         18  
  10         1320  
709 2         4 push @{"$pkg\::ISA"} => $loaded_class;
  2         26  
710             }
711              
712 2         16 return $table;
713             }
714              
715 21         75 my $top = $self->_in_builder(qw{schema});
716 21   100     188 my $into = $top->{meta}->{tables} //= {};
717              
718             # One of these:
719             # table NAME => CLASS, sub ...;
720             # table NAME => CLASS;
721             # table CLASS;
722             # table CLASS => sub ...;
723 21 100 66     272 if ($_[0] =~ m/::/ || $_[1] && $_[1] =~ m/::/) {
      100        
724 3         9 my @args = @_;
725 3         7 my ($class, $name, $cb, $no_match);
726              
727 3         11 while (my $arg = shift @args) {
728 6 100       24 if ($arg =~ m/::/) { $class = $arg }
  3 100       11  
729             elsif (my $ref = ref($arg)) {
730 1 50       34 if ($ref eq 'CODE') { $cb = $arg }
  1         6  
731 0         0 else { $no_match = 1; last }
  0         0  
732             }
733 2         7 else { $name = $arg }
734             }
735              
736 3 50 33     15 if ($class && !$no_match) {
737 3         9 my $table = $self->_load_table($class);
738 3   66     14 $name //= $table->{name};
739 3         7 $into->{$name} = $table;
740              
741 3 100       14 $self->_build('Table', frame => $table, args => [$cb], void => 1) if $cb;
742              
743 3         13 return $table;
744             }
745              
746             # Fallback to regular build
747             }
748              
749             # Typical case `table NAME => sub { ... }` or `table NAME => { ... }`
750 18         183 my $frame = {building => 'TABLE', class => $make, meta => {row_class => $top->{meta}->{row_class}}};
751 18         116 return $self->_build('Table', into => $into, frame => $frame, args => \@_);
752             }
753              
754             sub index {
755 3     3 0 6 my $self = shift;
756 3         6 my ($name, $cols, $params);
757              
758 3         11 while (my $arg = shift @_) {
759 6         12 my $ref = ref($arg);
760 6 100       20 if (!$ref) { $name = $arg }
  2 100       7  
    50          
761 1   50     2 elsif ($ref eq 'HASH') { $params = {%{$params // {}}, %{$arg}} }
  1         20  
  1         7  
762 3         10 elsif ($ref eq 'ARRAY') { $cols = $arg }
763 0         0 else { croak "Not sure what to do with '$arg'" }
764             }
765              
766 3   100     6 my $index = { %{$params // {}}, name => $name, columns => $cols };
  3         18  
767              
768 3 50       12 return $index if defined wantarray;
769              
770 3         7 my $top = $self->_in_builder(qw{table});
771              
772 3         6 push @{$top->{meta}->{indexes}} => $index;
  3         11  
773             }
774              
775             sub column {
776 49     49 1 131 my $self = shift;
777              
778 49         142 my $top = $self->_in_builder(qw{table});
779              
780 49   100     312 $top->{column_order} //= 1;
781 49         112 my $order = $top->{column_order}++;
782              
783 49   100     214 my $into = $top->{meta}->{columns} //= {};
784 49         283 my $frame = {building => 'COLUMN', class => 'DBIx::QuickORM::Schema::Table::Column', meta => {order => $order}};
785              
786             return $self->_build(
787             'Column',
788             into => $into,
789             frame => $frame,
790             args => \@_,
791             extra_cb => sub {
792 49     49   96 my $self = shift;
793 49         410 my %params = @_;
794              
795 49         111 my $extra = $params{extra};
796 49         94 my $meta = $params{meta};
797              
798 49         265 while (my $arg = shift @$extra) {
799 39         60 local $@;
800 39 100 66     310 if (blessed($arg)) {
    100 100        
    100          
    100          
    100          
    100          
    100          
    100          
    100          
801 2 100       18 if ($arg->DOES('DBIx::QuickORM::Role::Type')) {
802 1         24 $meta->{type} = $arg;
803             }
804             else {
805 1         320 croak "'$arg' does not implement 'DBIx::QuickORM::Role::Type'";
806             }
807             }
808             elsif (my $ref = ref($arg)) {
809 3 100       8 if ($ref eq 'SCALAR') {
810 2         10 $meta->{type} = $arg;
811             }
812             else {
813 1         150 croak "Not sure what to do with column argument '$arg'";
814             }
815             }
816             elsif ($arg eq 'id' || $arg eq 'identity') {
817 5         17 $meta->{identity} = 1;
818             }
819             elsif ($arg eq 'not_null') {
820 2         8 $meta->{nullable} = 0;
821             }
822             elsif ($arg eq 'nullable') {
823 4         9 $meta->{nullable} = 1;
824             }
825             elsif ($arg eq 'omit') {
826 5         18 $meta->{omit} = 1;
827             }
828             elsif ($arg eq 'sql_default' || $arg eq 'perl_default') {
829 2         7 $meta->{$arg} = shift @$extra;
830             }
831             elsif (validate_affinity($arg)) {
832 7         39 $meta->{affinity} = $arg;
833             }
834             elsif (my $class = load_class($arg, 'DBIx::QuickORM::Type')) {
835 7 100       211 croak "Class '$class' does not implement DBIx::QuickORM::Role::Type" unless $class->DOES('DBIx::QuickORM::Role::Type');
836 6         185 $meta->{type} = $class;
837             }
838             else {
839 2 100       216 croak "Error loading class for type '$arg': $@" unless $@ =~ m/^Can't locate .+ in \@INC/;
840 1         184 croak "Column arg '$arg' does not appear to be pure-sql (scalar ref), affinity, or an object implementing DBIx::QuickORM::Role::Type";
841             }
842             }
843             },
844 49         628 );
845             }
846              
847             sub columns {
848 4     4 1 11 my $self = shift;
849              
850 4         14 my $top = $self->_in_builder(qw{table});
851              
852 4         10 my (@names, $other);
853 4         9 for my $arg (@_) {
854 13         25 my $ref = ref($arg);
855 13 100       41 if (!$ref) { push @names => $arg }
  8 100       19  
856 4 100       138 elsif ($ref eq 'HASH') { croak "Cannot provide multiple hashrefs" if $other; $other = $arg }
  3         8  
857 1         152 else { croak "Not sure what to do with '$arg' ($ref)" }
858             }
859              
860 2 50       6 return [map { $self->column($_, $other) } @names] if defined wantarray;
  0         0  
861              
862 2         8 $self->column($_, $other) for @names;
863              
864 2         10 return;
865             }
866              
867             sub sql {
868 23     23 1 39 my $self = shift;
869              
870 23 50       117 croak "Not enough arguments" unless @_;
871 23 50       52 croak "Too many arguments" if @_ > 2;
872              
873 23         43 my $sql = pop;
874 23   100     100 my $affix = lc(pop // 'infix');
875              
876 23 50       113 croak "'$affix' is not a valid sql position, use 'prefix', 'infix', or 'postfix'" unless $affix =~ m/^(pre|post|in)fix$/;
877              
878 23         62 my $top = $self->_in_builder(qw{schema table column});
879              
880 23 100       58 if ($affix eq 'infix') {
881 7 100       175 croak "'infix' sql is not supported in SCHEMA, use prefix or postfix" if $top->{building} eq 'SCHEMA';
882 6 100       315 croak "'infix' sql has already been set for '$top->{created}'" if $top->{meta}->{sql}->{$affix};
883 4         15 $top->{meta}->{sql}->{$affix} = $sql;
884             }
885             else {
886 16         23 push @{$top->{meta}->{sql}->{$affix}} => $sql;
  16         74  
887             }
888             }
889              
890             sub affinity {
891 24     24 1 46 my $self = shift;
892 24 50       71 croak "Not enough arguments" unless @_;
893 24         74 my ($affinity) = @_;
894              
895 24 100       105 croak "'$affinity' is not a valid affinity" unless validate_affinity($affinity);
896              
897 23 100       98 return $affinity if defined wantarray;
898              
899 13         37 my $top = $self->_in_builder(qw{column});
900 12         48 $top->{meta}->{affinity} = $affinity;
901             }
902              
903             sub _check_type {
904 11     11   19 my $self = shift;
905 11         31 my ($type) = @_;
906              
907 11 100       48 return $type if ref($type) eq 'SCALAR';
908 9 50       27 return undef if ref($type);
909 9 100       217 return $type if $type->DOES('DBIx::QuickORM::Role::Type');
910              
911 6 100       38 my $class = load_class($type, 'DBIx::QuickORM::Type') or return undef;
912 5         20 return $class;
913             }
914              
915             sub type {
916 13     13 1 30 my $self = shift;
917 13 100       182 croak "Not enough arguments" unless @_;
918 12         38 my ($type, @args) = @_;
919              
920 12 100       188 croak "Too many arguments" if @args;
921 11 50       34 croak "cannot use a blessed instance of the type ($type)" if blessed($type);
922              
923 11         37 local $@;
924 11         52 my $use_type = $self->_check_type($type);
925 11 100       114 unless ($use_type) {
926 1         4 my $err = "Type must be a scalar reference, or a class that implements 'DBIx::QuickORM::Role::Type', got: $type";
927 1 50       42 $err .= "\nGot exception: $@" if $@ =~ m/^Can't locate .+ in \@INC/;
928 1         275 confess $err;
929             }
930              
931 10 100       55 return $use_type if defined wantarray;
932              
933 8         30 my $top = $self->_in_builder(qw{column});
934 8         45 $top->{meta}->{type} = $use_type;
935             }
936              
937 14 100 100 14 1 343 sub omit { defined(wantarray) ? (($_[1] // 1) ? 'omit' : ()) : ($_[0]->_in_builder('column')->{meta}->{omit} = $_[1] // 1) }
    100 100        
938 12 100 100 12 1 119 sub identity { defined(wantarray) ? (($_[1] // 1) ? 'identity' : ()) : ($_[0]->_in_builder('column')->{meta}->{identity} = $_[1] // 1) }
    100 100        
939 11 100 100 11 1 107 sub nullable { defined(wantarray) ? (($_[1] // 1) ? 'nullable' : 'not_null') : ($_[0]->_in_builder('column')->{meta}->{nullable} = $_[1] // 1) }
    100 100        
940 6 100 100 6 1 44 sub not_null { defined(wantarray) ? (($_[1] // 1) ? 'not_null' : 'nullable') : ($_[0]->_in_builder('column')->{meta}->{nullable} = $_[1] ? 0 : 1) }
    100          
    100          
941              
942             sub default {
943 7     7 1 10 my $self = shift;
944 7         12 my ($val) = @_;
945              
946 7         13 my $r = ref($val);
947              
948 7         13 my ($key);
949 7 100       18 if ($r eq 'SCALAR') { $key = 'sql_default'; $val = $$val }
  3 50       4  
  3         6  
950 4         39 elsif ($r eq 'CODE') { $key = 'perl_default' }
951 0         0 else { croak "'$val' is not a valid default, must be a scalar ref, or a coderef" }
952              
953 7 100       28 return ($key => $val) if defined wantarray;
954              
955 3         8 my $top = $self->_in_builder('column');
956 3         9 $top->{meta}->{$key} = $val;
957             }
958              
959             sub _in_builder {
960 329     329   739 my $self = shift;
961 329         817 my %builders = map { lc($_) => 1 } @_;
  502         2202  
962              
963 329 100       675 if (@{$self->{+STACK}} > 1) {
  329         1366  
964 324         963 my $top = $self->top;
965 324         845 my $bld = lc($top->{building});
966              
967 324 50       1452 return $top if $builders{$bld};
968             }
969              
970 5         62 my ($pkg, $file, $line, $name) = caller(0);
971 5 50       90 ($pkg, $file, $line, $name) = caller(1) if $name =~ m/_in_builder/;
972              
973 5         608 croak "${name}() can only be used inside one of the following builders: " . join(', ', @_);
974             }
975              
976             sub db_name {
977 30     30 1 94 my $self = shift;
978 30         164 my ($db_name) = @_;
979              
980 30         186 my $top = $self->_in_builder(qw{table db});
981              
982 30         156 $top->{meta}->{db_name} = $db_name;
983             }
984              
985             sub row_class {
986 5     5 1 12 my $self = shift;
987 5         15 my ($proto) = @_;
988              
989 5         17 my $top = $self->_in_builder(qw{table schema});
990              
991 5 100       21 my $class = load_class($proto, 'DBIx::QuickORM::Row') or croak "Could not load class '$proto': $@";
992              
993 4         15 $top->{meta}->{row_class} = $class;
994             }
995              
996             sub primary_key {
997 5     5 1 9 my $self = shift;
998 5         15 my (@list) = @_;
999              
1000 5         12 my $top = $self->_in_builder(qw{table column});
1001              
1002 5         11 my $meta;
1003 5 100       23 if ($top->{building} eq 'COLUMN') {
1004 3         7 my $frame = $self->{+STACK}->[-2];
1005              
1006 3 100       169 croak "Too many arguments" if @list;
1007              
1008             croak "Could not find table for the column currently being built"
1009 2 50       9 unless $frame->{building} eq 'TABLE';
1010              
1011 2         7 @list = ($top->{meta}->{name});
1012 2         6 $meta = $frame->{meta};
1013             }
1014             else {
1015 2 100       89 croak "Not enough arguments" unless @list;
1016 1         3 $meta = $top->{meta};
1017             }
1018              
1019 3         23 $meta->{primary_key} = \@list;
1020             }
1021              
1022             sub unique {
1023 4     4 1 6 my $self = shift;
1024 4         11 my (@list) = @_;
1025              
1026 4         9 my $top = $self->_in_builder(qw{table column});
1027              
1028 4         6 my $meta;
1029 4 100       11 if ($top->{building} eq 'COLUMN') {
1030 2         4 my $frame = $self->{+STACK}->[-2];
1031              
1032 2 100       80 croak "Too many arguments" if @list;
1033              
1034             croak "Could not find table for the column currently being built"
1035 1 50       3 unless $frame->{building} eq 'TABLE';
1036              
1037 1         3 @list = ($top->{meta}->{name});
1038 1         1 $meta = $frame->{meta};
1039             }
1040             else {
1041 2 100       69 croak "Not enough arguments" unless @list;
1042 1         2 $meta = $top->{meta};
1043             }
1044              
1045 2         10 my $key = join ', ' => sort @list;
1046              
1047 2         48 $meta->{unique}->{$key} = \@list;
1048 2         6 push @{$meta->{indexes}} => {unique => 1, columns => \@list};
  2         33  
1049             }
1050              
1051             sub link {
1052 5     5 1 9 my $self = shift;
1053 5         16 my @args = @_;
1054              
1055 5         12 my $top = $self->_in_builder(qw{schema column});
1056              
1057 5         11 my ($table, $local);
1058 5 100       15 if ($top->{building} eq 'COLUMN') {
1059 1 50 33     7 my $alias = @args && !ref($args[0]) ? shift @args : undef;
1060 1 50       5 croak "Expected an arrayref, got '$args[0]'" unless ref($args[0]) eq 'ARRAY';
1061 1         2 @args = @{$args[0]};
  1         4  
1062              
1063 1         3 my $cols = [$top->{meta}->{name}];
1064              
1065 1 50       4 croak "Could not find table?" unless $self->{+STACK}->[-2]->{building} eq 'TABLE';
1066              
1067 1         3 $table = $self->{+STACK}->[-2];
1068 1         2 my $tname = $table->{name};
1069              
1070 1         4 $local = [$tname, $cols];
1071 1 50       6 push @$local => $alias if $alias;
1072             }
1073              
1074 5         10 my @nodes;
1075 5         14 while (my $first = shift @args) {
1076 9         12 my $fref = ref($first);
1077 9 100       40 if (!$fref) {
1078 7         14 my $second = shift(@args);
1079 7         14 my $sref = ref($second);
1080              
1081 7 50 33     34 croak "Expected an array, got '$second'" unless $sref && $sref eq 'ARRAY';
1082 7         16 my $eref = ref($second->[1]);
1083 7 100 66     27 if ($eref && $eref eq 'ARRAY') {
1084 3         8 push @nodes => [$second->[0], $second->[1], $first];
1085             }
1086             else {
1087 4         9 push @nodes => [$first, $second];
1088             }
1089              
1090 7         187 next;
1091             }
1092              
1093 2 50       8 if ($fref eq 'HASH') {
1094 2         7 push @nodes => [$first->{table}, $first->{columns}, $first->{alias}];
1095 2         8 next;
1096             }
1097              
1098 0         0 croak "Expected a hashref, table name, or alias, got '$first'";
1099             }
1100              
1101 5         15 my $other;
1102 5 100       11 if ($local) {
1103 1 50       4 croak "Too many nodes" if @nodes > 1;
1104 1 50       4 croak "Not enough nodes" unless @nodes;
1105 1         3 ($other) = @nodes;
1106             }
1107             else {
1108 4         9 ($local, $other) = @nodes;
1109             }
1110              
1111 5         13 my $caller = $self->_caller;
1112 5         17 my $created = "$caller->[3]() at $caller->[1] line $caller->[2]";
1113 5         13 my $link = [$local, $other, $created];
1114              
1115 5   66     8 push @{($table // $top)->{meta}->{_links}} => $link;
  5         25  
1116              
1117 5         20 return;
1118             }
1119              
1120             sub orm {
1121 72     72 1 297 my $self = shift;
1122              
1123 72         329 my $into = $self->{+ORMS};
1124 72         987 my $frame = {building => 'ORM', class => 'DBIx::QuickORM::ORM'};
1125              
1126 72         577 $self->_build('ORM', into => $into, frame => $frame, args => \@_);
1127             }
1128              
1129             my %RECURSE = (
1130             DB => {},
1131             LINK => {},
1132             COLUMN => {},
1133             AUTOFILL => {},
1134             ORM => {schema => 1, db => 1, autofill => 1},
1135             SCHEMA => {tables => 2},
1136             TABLE => {columns => 2},
1137             );
1138              
1139             sub compile {
1140 205     205 0 426 my $self = shift;
1141 205         916 my ($frame, $alt_arg) = @_;
1142              
1143 205   100     1541 my $alt = $alt_arg || ':';
1144              
1145             # Already compiled
1146 205 100       974 return $frame->{__COMPILED__}->{$alt} if $frame->{__COMPILED__}->{$alt};
1147              
1148 189 50       580 my $bld = $frame->{building} or confess "Not currently building anything";
1149 189 50       617 my $recurse = $RECURSE{$bld} or croak "Not sure how to compile '$bld'";
1150              
1151 189         462 my $meta = $frame->{meta};
1152 189 100 100     915 my $alta = $alt_arg && $frame->{alt}->{$alt_arg} ? $frame->{alt}->{$alt_arg}->{meta} // {} : {};
      50        
1153              
1154 189         546 my %obj_data;
1155              
1156             my %seen;
1157 189         1277 for my $field (keys %$meta, keys %$alta) {
1158 799 100       2247 next if $seen{$field}++;
1159              
1160 779   100     3749 my $val = $self->_merge($alta->{$field}, $meta->{$field}) // next;
1161              
1162 765 100       2175 unless($recurse->{$field}) {
1163 662         1288 $obj_data{$field} = $val;
1164 662         1355 next;
1165             }
1166              
1167 103 100       328 if ($recurse->{$field} > 1) {
1168 46         156 $obj_data{$field} = { map { $_ => $self->compile($val->{$_}, $alt_arg) } keys %$val };
  85         353  
1169             }
1170             else {
1171 57         565 $obj_data{$field} = $self->compile($val, $alt_arg);
1172             }
1173             }
1174              
1175 189 50       832 my $proto = $frame->{class} or croak "No class to compile for '$frame->{name}' ($frame->{created})";
1176 189 50       836 my $class = load_class($proto) or croak "Could not load class '$proto' for '$frame->{name}' ($frame->{created}): $@";
1177              
1178 189         746 my $caller = $self->_caller;
1179 189         832 my $compiled = "$caller->[3]() at $caller->[1] line $caller->[2]";
1180              
1181 189         637 $obj_data{compiled} = $compiled;
1182              
1183 189 50       392 my $out = eval { $class->new(%obj_data) } or confess "Could not construct an instance of '$class': $@";
  189         1754  
1184 189         857 $frame->{__COMPILED__}->{$alt} = $out;
1185              
1186 189         2295 return $out;
1187             }
1188              
1189             sub _merge {
1190 779     779   1305 my $self = shift;
1191 779         1988 my ($a, $b) = @_;
1192              
1193 779 100       1760 return $a unless defined $b;
1194 753 100       2549 return $b unless defined $a;
1195              
1196 20         37 my $ref_a = ref($a);
1197 20         36 my $ref_b = ref($b);
1198 20 50       49 croak "Mismatched reference!" unless $ref_a eq $ref_b;
1199              
1200             # Not a ref, a wins
1201 20 100 33     111 return $a // $b unless $ref_a;
1202              
1203 4 50       47 return { %$a, %$b } if $ref_a eq 'HASH';
1204              
1205 0         0 croak "Not sure how to merge $a and $b";
1206             }
1207              
1208             sub _build {
1209 254     254   649 my $self = shift;
1210 254         6993 my ($type, %params) = @_;
1211              
1212 254         667 my $into = $params{into};
1213 254         549 my $frame = $params{frame};
1214 254         631 my $args = $params{args};
1215 254         536 my $extra_cb = $params{extra_cb};
1216 254         541 my $force_build = $params{force_build};
1217              
1218 254 50 33     1725 croak "Not enough arguments" unless $args && @$args;
1219              
1220 254         1576 my $caller = $self->_caller;
1221              
1222 254         622 my ($name, $builder, $meta_arg, @extra);
1223 254         785 for my $arg (@$args) {
1224 444         1135 my $ref = ref($arg);
1225 444 100       1386 if (!$ref) { if ($name) { push @extra => $arg } else { $name = $arg } }
  279 100       1260  
  35 100       84  
  244 100       1562  
1226 152 50       653 elsif ($ref eq 'CODE') { croak "Multiple builders provided!" if $builder; $builder = $arg }
  152         632  
1227 8 50       21 elsif ($ref eq 'HASH') { croak "Multiple meta hashes provided!" if $meta_arg; $meta_arg = $arg }
  8         19  
1228 5         17 else { push @extra => $arg }
1229             }
1230              
1231 254 100       715 $force_build = 1 if @extra;
1232 254 100 100     2053 my $alt = $name && $name =~ s/:(\S+)$// ? $1 : undef;
1233 254 50 66     1314 $name = undef if defined($name) && !length($name);
1234              
1235 254   100     1056 my $meta = $meta_arg // {};
1236 254 100       1709 $self->$extra_cb(%params, type => $type, extra => \@extra, meta => $meta, name => $name, frame => $frame) if $extra_cb;
1237 249 50       682 croak "Multiple names provided: " . join(', ' => $name, @extra) if @extra;
1238              
1239             # Simple fetch
1240 249 100 100     1963 if ($name && !$builder && !$meta_arg && !$force_build) {
      100        
      100        
1241 78 100       417 croak "'$name' is not a defined $type" unless $into->{$name};
1242 77 100       567 return $self->compile($into->{$name}, $alt) unless $params{no_compile};
1243 23         280 return $into->{$name};
1244             }
1245              
1246 171         710 my $created = "$caller->[3]() at $caller->[1] line $caller->[2]";
1247 171         1335 %$frame = (
1248             %$frame,
1249             plugins => [],
1250             created => $created,
1251             );
1252              
1253 171   66     2016 $frame->{name} //= $name // "Anonymous builder ($created)";
      66        
1254              
1255 171   100     354 $frame->{meta} = { %{$frame->{meta} // {}}, %{$meta} };
  171         860  
  171         825  
1256              
1257 171 100 100     1331 $frame->{meta}->{name} = $name if $name && $type ne 'Alt';
1258              
1259 171         521 $frame->{meta}->{created} = $created;
1260              
1261 171         320 push @{$self->{+STACK}} => $frame;
  171         967  
1262              
1263 171         384 my $ok = eval {
1264 171 100       879 $builder->(meta => $meta, frame => $frame) if $builder;
1265 171         20139 $_->munge($frame) for @{$self->plugins};
  171         687  
1266 171         424 1;
1267             };
1268 171         362 my $err = $@;
1269              
1270 171         287 pop @{$self->{+STACK}};
  171         451  
1271              
1272 171 50       563 die $err unless $ok;
1273              
1274 171 100       458 if ($into) {
1275 162         567 my $ref = ref($into);
1276 162 100       501 if ($ref eq 'HASH') {
    50          
1277 160 100       1816 $into->{$name} = $frame if $name;
1278             }
1279             elsif ($ref eq 'SCALAR') {
1280 2         5 ${$into} = $frame;
  2         6  
1281             }
1282             else {
1283 0         0 croak "Invalid 'into': $into";
1284             }
1285             }
1286              
1287 171 100       551 return if $params{void};
1288              
1289 170 100       523 if (defined wantarray) {
1290 16 100       78 return $self->compile($frame, $alt) unless $params{no_compile};
1291 14         182 return $frame;
1292             }
1293              
1294 154 50       2039 return if $name;
1295              
1296 0           croak "No name provided, but called in void context!";
1297             }
1298              
1299             1;
1300              
1301             __END__
1302              
1303             =head1 NAME
1304              
1305             DBIx::QuickORM - Composable ORM builder.
1306              
1307             =head1 DESCRIPTION
1308              
1309             DBIx::QuickORM allows you to define ORMs with reusable and composible parts.
1310              
1311             With this ORM builder you can specify:
1312              
1313             =over 4
1314              
1315             =item How to connect to one or more databases on one or more servers.
1316              
1317             =item One or more schema structures.
1318              
1319             =item Custom row classes to use.
1320              
1321             =item Plugins to use.
1322              
1323             =back
1324              
1325             =head1 SEE ALSO
1326              
1327             L<DBIx::QuickORM::Manual> - Documentation hub.
1328              
1329             =head1 SYNOPSIS
1330              
1331             The common use case is to create an ORM package for your app, then use that ORM
1332             package any place in the app that needs ORM access.
1333              
1334             =head2 YOUR ORM PACKAGE
1335              
1336             =head3 MANUAL SCHEMA
1337              
1338             package My::ORM;
1339             use DBIx::QuickORM;
1340              
1341             # Define your ORM
1342             orm my_orm => sub {
1343             # Define your object
1344             db my_db => sub {
1345             dialect 'PostgreSQL'; # Or MySQL, MariaDB, SQLite
1346             host 'mydb.mydomain.com';
1347             port 1234;
1348              
1349             # Best not to hardcode these, read them from a secure place and pass them in here.
1350             user $USER;
1351             pass $PASS;
1352             };
1353              
1354             # Define your schema
1355             schema myschema => sub {
1356             table my_table => sub {
1357             column id => sub {
1358             identity;
1359             primary_key;
1360             not_null;
1361             };
1362              
1363             column name => sub {
1364             type \'VARCHAR(128)'; # Exact SQL for the type
1365             affinity 'string'; # required if other information does not make it obvious to DBIx::QuickORM
1366             unique;
1367             not_null;
1368             };
1369              
1370             column added => sub {
1371             type 'Stamp'; # Short for DBIx::QuickORM::Type::Stamp
1372             not_null;
1373              
1374             # Exact SQL to use if DBIx::QuickORM generates the table SQL
1375             default \'NOW()';
1376              
1377             # Perl code to generate a default value when rows are created by DBIx::QuickORM
1378             default sub { ... };
1379             };
1380             };
1381             };
1382             };
1383              
1384             =head3 AUTOMAGIC SCHEMA
1385              
1386             package My::ORM;
1387             use DBIx::QuickORM;
1388              
1389             # Define your ORM
1390             orm my_orm => sub {
1391             # Define your object
1392             db my_db => sub {
1393             dialect 'PostgreSQL'; # Or MySQL, MariaDB, SQLite
1394             host 'mydb.mydomain.com';
1395             port 1234;
1396              
1397             # Best not to hardcode these, read them from a secure place and pass them in here.
1398             user $USER;
1399             pass $PASS;
1400             };
1401              
1402             # Define your schema
1403             schema myschema => sub {
1404             # The class name is optional, the one shown here is the default
1405             autofill 'DBIx::QuickORM::Schema::Autofill' => sub {
1406             autotype 'UUID'; # Automatically handle UUID fields
1407             autotype 'JSON'; # Automatically handle JSON fields
1408              
1409             # Do not autofill these tables
1410             autoskip table => qw/foo bar baz/;
1411              
1412             # Will automatically create My::Row::Table classes for you with
1413             # accessors for links and fields If My::Table::Row can be
1414             # loaded (IE My/Row/Table.pm exists) it will load it then
1415             # autofill anything missing.
1416             autorow 'My::Row';
1417              
1418             # autorow can also take a subref that accepts a table name as
1419             # input and provides the class name for it, here is the default
1420             # one used if none if provided:
1421             autorow 'My::Row' => sub {
1422             my $name = shift;
1423             my @parts = split /_/, $name;
1424             return join '' => map { ucfirst(lc($_)) } @parts;
1425             };
1426              
1427             # You can provide custom names for tables. It will still refer
1428             # to the correct name in queries, but will provide an alternate
1429             # name for the orm to use in perl code.
1430             autoname table => sub {
1431             my %params = @_;
1432             my $table_hash = $params{table}; # unblessed ref that will become a table
1433             my $name = $params{name}; # The name of the table
1434             ...
1435             return $new_name;
1436             };
1437              
1438             # You can provide custom names for link (foreign key) accessors when using autorow
1439             autoname link_accessor => sub {
1440             my %params = @_;
1441             my $link = $params{link};
1442              
1443             return "obtain_" . $link->other_table if $params{link}->unique;
1444             return "select_" . $link->other_table . "s";
1445             };
1446              
1447             # You can provide custom names for field accessors when using autorow
1448             autoname field_accessor => sub {
1449             my %params = @_;
1450             return "get_$params{name}";
1451             };
1452             };
1453             };
1454             };
1455              
1456             =head2 YOUR APP CODE
1457              
1458             package My::App;
1459             use My::Orm qw/orm/;
1460              
1461             # Get a connection to the orm
1462             # Note: This will return the same connection each time, no need to cache it yourself.
1463             # See DBIx::QuickORM::Connection for more info.
1464             my $orm = orm('my_orm');
1465              
1466             # See DBIx::QuickORM::Handle for more info.
1467             my $h = $orm->handle('people', {surname => 'smith'});
1468             for my $person ($handle->all) {
1469             print $person->field('first_name') . "\n"
1470             }
1471              
1472             my $new_h = $h->limit(5)->order_by('surname')->omit(@large_fields);
1473             my $iterator = $new_h->iterator; # Query is actually sent to DB here.
1474             while (my $row = $iterator->next) {
1475             ...
1476             }
1477              
1478             # Start an async query
1479             my $async = $h->async->iterator;
1480              
1481             while (!$async->ready) {
1482             do_something_else();
1483             }
1484              
1485             while (my $item = $iterator->next) {
1486             ...
1487             }
1488              
1489             See L<DBIx::QuickORM::Connection> for details on the object returned by
1490             C<< my $orm = orm('my_orm'); >>.
1491              
1492             See L<DBIx::QuickORM::Handle> for more details on handles, which are similar to
1493             ResultSets from L<DBIx::Class>.
1494              
1495             =head1 RECIPES
1496              
1497             =head2 DEFINE DB LATER
1498              
1499             In some cases you may want to define your orm/schema before you have your
1500             database credentials. Then you want to add the database later in an app/script
1501             bootstrap process.
1502              
1503             Schema:
1504              
1505             package My::Schema;
1506             use DBIx::QuickORM;
1507              
1508             orm MyORM => sub {
1509             autofill;
1510             };
1511              
1512             Bootstrap process:
1513              
1514             package My::Bootstrap;
1515             use DBIx::QuickORM only => [qw/db db_name host port user pass/];
1516             use My::Schema;
1517              
1518             sub import {
1519             # Get the orm (the `orm => ...` param is required to prevent it from attempting a connection now)
1520             my $orm = qorm(orm => 'MyORM');
1521              
1522             return if $orm->db; # Already bootstrapped
1523              
1524             my %db_params = decrypt_creds();
1525              
1526             # Define the DB
1527             my $db = db {
1528             db_name 'quickdb';
1529             host $db_params{host};
1530             port $db_params{port};
1531             user $db_params{user};
1532             pass $db_params{pass};
1533             };
1534              
1535             # Set the db on the ORM:
1536             $orm->db($db);
1537             }
1538              
1539             Your app:
1540              
1541             package My::App;
1542              
1543             # Get the qorm() subroutine
1544             use My::Schema;
1545              
1546             # This will do the db bootstrap
1547             use My::Bootstrap;
1548              
1549             # Connect to the database with the ORM
1550             my $con = qorm('MyORM');
1551              
1552             =head2 RENAMING EXPORTS
1553              
1554             When importing L<DBIx::QuickORM> you can provide
1555             C<< rename => { name => new_name } >> mapping to rename exports.
1556              
1557             package My::ORM;
1558             use DBIx::QuickORM rename => {
1559             pass => 'password',
1560             user => 'username',
1561             table => 'build_table',
1562             };
1563              
1564             B<Note> If you do not want to bring in the C<import()> method that normally
1565             gets produced, you can also add C<< type => 'porcelain' >>.
1566              
1567             use DBIx::QuickORM type => 'porcelain';
1568              
1569             Really any 'type' other than 'orm' and undef (which becomes 'orm' by default)
1570             will work to prevent C<import()> from being exported to your namespace.
1571              
1572             =head2 DEFINE TABLES IN THEIR OWN PACKAGES/FILES
1573              
1574             If you have many tables, or want each to have a custom row class (custom
1575             methods for items returned by tables), then you probably want to define tables
1576             in their own files.
1577              
1578             When you follow this example you create the table C<My::ORM::Table::Foo>. The
1579             package will automatically subclass L<DBIx::QuickORM::Row> unless you use
1580             C<row_class()> to set an alternative base.
1581              
1582             Any methods added in the file will be callable on the rows returned when
1583             querying this table.
1584              
1585             First create F<My/ORM/Table/Foo.pm>:
1586              
1587             package My::ORM::Table::Foo;
1588             use DBIx::QuickORM type => 'table';
1589              
1590             # Calling this will define the table. It will also:
1591             # * Remove all functions imported from DBIx::QuickORM
1592             # * Set the base class to DBIx::QuickORM::Row, or to whatever class you specify with 'row_class'.
1593             table foo => sub {
1594             column a => sub { ... };
1595             column b => sub { ... };
1596             column c => sub { ... };
1597              
1598             ....
1599              
1600             # This is the default, but you can change it to set an alternate base class.
1601             row_class 'DBIx::QuickORM::Row';
1602             };
1603              
1604             sub custom_row_method {
1605             my $self = shift;
1606             ...
1607             }
1608              
1609             Then in your ORM package:
1610              
1611             package My::ORM;
1612              
1613             schema my_schema => sub {
1614             table 'My::ORM::Table::Foo'; # Bring in the table
1615             };
1616              
1617             Or if you have many tables and want to load all the tables under C<My::ORM::Table::> at once:
1618              
1619             schema my_schema => sub {
1620             tables 'My::ORM::Table';
1621             };
1622              
1623             =head2 APP THAT CAN USE NEARLY IDENTICAL MYSQL AND POSTGRESQL DATABASES
1624              
1625             Lets say you have a test app that can connect to nearly identical MySQL or
1626             PostgreSQL databases. The schemas are the same apart from minor differences required by
1627             the database engine. You want to make it easy to access whichever one you want,
1628             or even both.
1629              
1630             package My::ORM;
1631             use DBIx::QuickORM;
1632              
1633             orm my_orm => sub {
1634             db myapp => sub {
1635             alt mysql => sub {
1636             dialect 'MySQL';
1637             driver '+DBD::mysql'; # Or 'mysql', '+DBD::MariaDB', 'MariaDB'
1638             host 'mysql.myapp.com';
1639             user $MYSQL_USER;
1640             pass $MYSQL_PASS;
1641             db_name 'myapp_mysql'; # In MySQL the db is named myapp_mysql
1642             };
1643             alt pgsql => sub {
1644             dialect 'PostgreSQL';
1645             host 'pgsql.myapp.com';
1646             user $PGSQL_USER;
1647             pass $PGSQL_PASS;
1648             db_name 'myapp_pgsql'; # In PostgreSQL the db is named myapp_pgsql
1649             };
1650             };
1651              
1652             schema my_schema => sub {
1653             table same_on_both => sub { ... };
1654              
1655             # Give the name 'differs' that can always be used to refer to this table, despite each db giving it a different name
1656             table differs => sub {
1657             # Each db has a different name for the table
1658             alt mysql => sub { db_name 'differs_mysql' };
1659             alt pgsql => sub { db_name 'differs_pgsql' };
1660              
1661             # Name for the column that the code can always use regardless of which db is in use
1662             column foo => sub {
1663             # Each db also names this column differently
1664             alt mysql => sub { db_name 'foo_mysql' };
1665             alt pgsql => sub { db_name 'foo_pgsql' };
1666             ...;
1667             };
1668              
1669             ...;
1670             };
1671             };
1672             };
1673              
1674             Then to use it:
1675              
1676             use My::ORM;
1677              
1678             my $orm_mysql = orm('my_orm:mysql');
1679             my $orm_pgsql = orm('my_orm:pgsql');
1680              
1681             Each ORM object is a complete and self-contained ORM with its own caching and
1682             db connection. One connects to MySQL and one connects to PostgreSQL. Both can
1683             ask for rows in the C<differs> table, on MySQL it will query the
1684             C<differs_mysql>, on PostgreSQL it will query the C<differs_pgsql> table. You can
1685             use them both at the same time in the same code.
1686              
1687             =head2 ADVANCED COMPOSING
1688              
1689             You can define databases and schemas on their own and create multiple ORMs that
1690             combine them. You can also define a C<server> that has multiple databases.
1691              
1692             package My::ORM;
1693             use DBIx::QuickORM;
1694              
1695             server pg => sub {
1696             dialect 'PostgreSQL';
1697             host 'pg.myapp.com';
1698             user $USER;
1699             pass $PASS;
1700              
1701             db 'myapp'; # Points at the 'myapp' database on this db server
1702             db 'otherapp'; # Points at the 'otherapp' database on this db server
1703             };
1704              
1705             schema myapp => sub { ... };
1706             schema otherapp => sub { ... };
1707              
1708             orm myapp => sub {
1709             db 'pg.myapp';
1710             schema 'myapp';
1711             };
1712              
1713             orm otherapp => sub {
1714             db 'pg.otherapp';
1715             schema 'otherapp';
1716             };
1717              
1718             Then to use them:
1719              
1720             use My::ORM;
1721              
1722             my $myapp = orm('myapp');
1723             my $otherapp = orm('otherapp');
1724              
1725             Also note that C<< alt(variant => sub { ... }) >> can be used in any of the
1726             above builders to create MySQL/PostgreSQL/etc. variants on the databases and
1727             schemas. Then access them like:
1728              
1729             my $myapp_pgsql = orm('myapp:pgsql');
1730             my $myapp_mysql = orm('myapp:myql');
1731              
1732             =head1 ORM BUILDER EXPORTS
1733              
1734             You get all these when using DBIx::QuickORM.
1735              
1736             =over 4
1737              
1738             =item C<< orm $NAME => sub { ... } >>
1739              
1740             =item C<< my $orm = orm($NAME) >>
1741              
1742             Define or fetch an ORM.
1743              
1744             orm myorm => sub {
1745             db mydb => sub { ... };
1746             schema myschema => sub { ... };
1747             };
1748              
1749             my $orm = orm('myorm');
1750              
1751             You can also compose using databases or schemas you defined previously:
1752              
1753             db mydb1 => sub { ... };
1754             db mydb2 => sub { ... };
1755              
1756             schema myschema1 => sub { ... };
1757             schema myschema2 => sub { ... };
1758              
1759             orm myorm1 => sub {
1760             db 'mydb1';
1761             schema 'myschema1';
1762             };
1763              
1764             orm myorm2 => sub {
1765             db 'mydb2';
1766             schema 'myschema2';
1767             };
1768              
1769             orm my_mix_a => sub {
1770             db 'mydb1';
1771             schema 'myschema2';
1772             };
1773              
1774             orm my_mix_b => sub {
1775             db 'mydb2';
1776             schema 'myschema1';
1777             };
1778              
1779             =item C<< alt $VARIANT => sub { ... } >>
1780              
1781             Can be used to add variations to any builder:
1782              
1783             orm my_orm => sub {
1784             db mydb => sub {
1785             # ************************************
1786             alt mysql => sub {
1787             dialect 'MySQL';
1788             };
1789              
1790             alt pgsql => sub {
1791             dialect 'PostgreSQL';
1792             };
1793             # ************************************
1794             };
1795              
1796             schema my_schema => sub {
1797             table foo => sub {
1798             column x => sub {
1799             identity();
1800              
1801             # ************************************
1802             alt mysql => sub {
1803             type \'BIGINT';
1804             };
1805              
1806             alt pgsql => sub {
1807             type \'BIGSERIAL';
1808             };
1809             # ************************************
1810             };
1811             }
1812             };
1813             };
1814              
1815             Variants can be fetched using the colon C<:> in the name:
1816              
1817             my $pg_orm = orm('my_orm:pgsql');
1818             my $mysql_orm = orm('my_orm:mysql');
1819              
1820             This works in C<orm()>, C<db()>, C<schema()>, C<table()>, and C<row()> builders. It does
1821             cascade, so if you ask for the C<mysql> variant of an ORM, it will also give you
1822             the C<mysql> variants of the database, schema, tables and rows.
1823              
1824             =item C<< db $NAME >>
1825              
1826             =item C<< db $NAME => sub { ... } >>
1827              
1828             =item C<< $db = db $NAME >>
1829              
1830             =item C<< $db = db $NAME => sub { ... } >>
1831              
1832             Used to define a database.
1833              
1834             db mydb => sub {
1835             dialect 'MySQL';
1836             host 'mysql.myapp.com';
1837             port 1234;
1838             user $MYSQL_USER;
1839             pass $MYSQL_PASS;
1840             db_name 'myapp_mysql'; # In mysql the db is named myapp_mysql
1841             };
1842              
1843             Can also be used to fetch a database by name:
1844              
1845             my $db = db('mydb');
1846              
1847             Can also be used to tell an ORM which database to use:
1848              
1849             orm myorm => sub {
1850             db 'mydb';
1851             ...
1852             };
1853              
1854             =item C<dialect '+DBIx::QuickORM::Dialect::PostgreSQL'>
1855              
1856             =item C<dialect 'PostgreSQL'>
1857              
1858             =item C<dialect 'MySQL'>
1859              
1860             =item C<dialect 'MySQL::MariaDB'>
1861              
1862             =item C<dialect 'MySQL::Percona'>
1863              
1864             =item C<dialect 'MySQL::Community'>
1865              
1866             =item C<dialect 'SQLite'>
1867              
1868             Specify what dialect of SQL should be used. This is important for reading
1869             schema from an existing database, or writing new schema SQL.
1870              
1871             C<DBIx::QuickORM::Dialect::> will be prefixed to the start of any string
1872             provided unless it starts with a plus C<+>, in which case the plus is removed
1873             and the rest of the string is left unmodified.
1874              
1875             The following are all supported by DBIx::QuickORM by default
1876              
1877             =over 4
1878              
1879             =item L<PostgreSQL|DBIx::QuickORM::Dialect::PostgreSQL>
1880              
1881             For interacting with PostgreSQL databases.
1882              
1883             =item L<MySQL|DBIx::QuickORM::Dialect::MySQL>
1884              
1885             For interacting with generic MySQL databases. Selecting this will auto-upgrade
1886             to MariaDB, Percona, or Community variants if it can detect the variant. If it
1887             cannot detect the variant then the generic will be used.
1888              
1889             B<NOTE:> Using the correct variant can produce better results. For example
1890             MariaDB supports C<RETURNING> on C<INSERT>s, Percona and Community variants
1891             do not, and thus need a second query to fetch the data post-C<INSERT>, and using
1892             C<last_insert_id> to get auto-generated primary keys. DBIx::QuickORM is aware
1893             of this and will use returning when possible.
1894              
1895             =item L<MySQL::MariaDB|DBIx::QuickORM::Dialect::MySQL::MariaDB>
1896              
1897             For interacting with MariaDB databases.
1898              
1899             =item L<MySQL::Percona|DBIx::QuickORM::Dialect::MySQL::Percona>
1900              
1901             For interacting with MySQL as distributed by Percona.
1902              
1903             =item L<MySQL::Community|DBIx::QuickORM::Dialect::MySQL::Community>
1904              
1905             For interacting with the Community Edition of MySQL.
1906              
1907             =item L<SQLite|DBIx::QuickORM::Dialect::SQLite>
1908              
1909             For interacting with SQLite databases.
1910              
1911             =back
1912              
1913             =item C<driver '+DBD::Pg'>
1914              
1915             =item C<driver 'Pg'>
1916              
1917             =item C<driver 'mysql'>
1918              
1919             =item C<driver 'MariaDB'>
1920              
1921             =item C<driver 'SQLite'>
1922              
1923             Usually you do not need to specify this as your dialect should specify the
1924             correct one to use. However in cases like MySQL and MariaDB they are more or
1925             less interchangeable and you may want to override the default.
1926              
1927             Specify what DBI driver should be used. C<DBD::> is prefixed to any string you
1928             specify unless it starts with C<+>, in which case the plus is stripped and the
1929             rest of the module name is unmodified.
1930              
1931             B<NOTE:> DBIx::QuickORM can use either L<DBD::mysql> or L<DBD::MariaDB> to
1932             connect to any of the MySQL variants. It will default to L<DBD::MariaDB> if it
1933             is installed and you have not requested L<DBD::mysql> directly.
1934              
1935             =item C<< attributes \%HASHREF >>
1936              
1937             =item C<< attributes(attr => val, ...) >>
1938              
1939             Set the attributes of the database connection.
1940              
1941             This can take a hashref or key-value pairs.
1942              
1943             This will override all previous attributes, it does not merge.
1944              
1945             db mydb => sub {
1946             attributes { foo => 1 };
1947             };
1948              
1949             Or:
1950              
1951             db mydb => sub {
1952             attributes foo => 1;
1953             };
1954              
1955             =item C<host $HOSTNAME>
1956              
1957             =item C<hostname $HOSTNAME>
1958              
1959             Provide a hostname or IP address for database connections
1960              
1961             db mydb => sub {
1962             host 'mydb.mydomain.com';
1963             };
1964              
1965             =item C<port $PORT>
1966              
1967             Provide a port number for database connection.
1968              
1969             db mydb => sub {
1970             port 1234;
1971             };
1972              
1973             =item C<socket $SOCKET_PATH>
1974              
1975             Provide a socket instead of a host+port
1976              
1977             db mydb => sub {
1978             socket '/path/to/db.socket';
1979             };
1980              
1981             =item C<user $USERNAME>
1982              
1983             =item C<username $USERNAME>
1984              
1985             provide a database username
1986              
1987             db mydb => sub {
1988             user 'bob';
1989             };
1990              
1991             =item C<pass $PASSWORD>
1992              
1993             =item C<password $PASSWORD>
1994              
1995             provide a database password
1996              
1997             db mydb => sub {
1998             pass 'hunter2'; # Do not store any real passwords in plaintext in code!!!!
1999             };
2000              
2001             =item C<creds sub { return \%CREDS }>
2002              
2003             Allows you to provide a coderef that will return a hashref with all the
2004             necessary database connection fields.
2005              
2006             This is mainly useful if you credentials are in an encrypted YAML or JSON file
2007             and you have a method to decrypt and read it returning it as a hash.
2008              
2009             db mydb => sub {
2010             creds sub { ... };
2011             };
2012              
2013             =item C<connect sub { ... }>
2014              
2015             =item C<connect \&connect>
2016              
2017             Instead of providing all the other fields, you may specify a coderef that
2018             returns a L<DBI> connection.
2019              
2020             B<IMPORTANT:> This function must always return a new L<DBI> connection it
2021             B<MUST NOT> cache it!
2022              
2023             sub mydb => sub {
2024             connect sub { ... };
2025             };
2026              
2027             =item C<dsn $DSN>
2028              
2029             Specify the DSN used to connect to the database. If not provided then an
2030             attempt will be made to construct a DSN from other parameters, if they are
2031             available.
2032              
2033             db mydb => sub {
2034             dsn "dbi:Pg:dbname=foo";
2035             };
2036              
2037             =item C<< server $NAME => sub { ... } >>
2038              
2039             Used to define a server with multiple databases. This is a way to avoid
2040             re-specifying credentials for each database you connect to.
2041              
2042             You can use C<< db('server_name.db_name') >> to fetch the database.
2043              
2044             Basically this allows you to specify any database fields once in the server, then
2045             define any number of databases that inherit them.
2046              
2047             Example:
2048              
2049             server pg => sub {
2050             host 'pg.myapp.com';
2051             user $USER;
2052             pass $PASS;
2053             attributes { work_well => 1 }
2054              
2055             db 'myapp'; # Points at the 'myapp' database on this db server
2056             db 'otherapp'; # Points at the 'otherapp' database on this db server
2057              
2058             # You can also override any if a special db needs slight modifications.
2059             db special => sub {
2060             attributes { work_well => 0, work_wrong => 1 };
2061             };
2062             };
2063              
2064             orm myapp => sub {
2065             db 'pg.myapp';
2066             ...;
2067             };
2068              
2069             orm otherapp => sub {
2070             db 'pg.otherapp';
2071             ...;
2072             };
2073              
2074             =item C<< schema $NAME => sub { ... } >>
2075              
2076             =item C<< $schema = schema($NAME) >>
2077              
2078             =item C<< $schema = schema($NAME => sub { ... }) >>
2079              
2080             Used to either fetch or define a schema.
2081              
2082             When called with only 1 argument it will fetch the schema with the given name.
2083              
2084             When used inside an ORM builder it will set the schema for the ORM (all ORMs
2085             have exactly one schema).
2086              
2087             When called with 2 arguments it will define the schema using the coderef as a
2088             builder.
2089              
2090             When called in a non-void context it will return the compiled schema, otherwise
2091             it adds it to the ORM class.
2092              
2093             # Define the 'foo' schema:
2094             schema foo => sub {
2095             table a => sub { ... };
2096             table b => sub { ... };
2097             };
2098              
2099             # Fetch it:
2100             my $foo = schema('foo');
2101              
2102             # Define and compile one:
2103             my $bar = schema bar => sub { ... }
2104              
2105             # Use it in an orm:
2106             orm my_orm => sub {
2107             schema('foo');
2108             db(...);
2109             };
2110              
2111             =item C<< table $NAME => sub { ... } >>
2112              
2113             =item C<< table $CLASS >>
2114              
2115             =item C<< table $CLASS => sub { ... } >>
2116              
2117             Used to define a table, or load a table class.
2118              
2119             schema my_schema => sub {
2120             # Load an existing table
2121             table 'My::Table::Foo';
2122              
2123             # Define a new table
2124             table my_table => sub {
2125             column foo => sub { ... };
2126             primary_key('foo');
2127             };
2128              
2129             # Load an existing table, but make some changes to it
2130             table 'My::Table::Bar' => sub {
2131             # Override the row class used in the original
2132             row_class 'DBIx::QuickORM::Row';
2133             };
2134             };
2135              
2136             This will assume you are loading a table class if the double colon C<::>
2137             appears in the name. Otherwise it assumes you are defining a new table.
2138             This means it is not possible to load top-level packages as table classes,
2139             which is a feature, not a bug.
2140              
2141             =item C<tables 'Table::Namespace'>
2142              
2143             Used to load all tables in the specified namespace:
2144              
2145             schema my_schema => sub {
2146             # Load My::Table::Foo, My::Table::Bar, etc.
2147             tables 'My::Table';
2148             };
2149              
2150             =item C<row_class '+My::Row::Class'>
2151              
2152             =item C<row_class 'MyRowClass'>
2153              
2154             When fetching a row from a table, this is the class that each row will be
2155             blessed into.
2156              
2157             This can be provided as a default for a schema, or as a specific one to use in
2158             a table. When using table classes this will set the base class for the table as
2159             the table class itself will be the row class.
2160              
2161             If the class name has a plus C<+> it will be stripped off and the class name will not
2162             be altered further. If there is no C<+> then C<DBIx::QuickORM::Row::> will be
2163             prefixed onto your string, and the resulting class will be loaded.
2164              
2165             schema my_schema => sub {
2166             # Uses My::Row::Class as the default for rows in all tables that do not override it.
2167             row_class '+My::Row::Class';
2168              
2169             table foo => sub {
2170             row_class 'Foo'; # Uses DBIx::QuickORM::Row::Foo as the row class for this table
2171             };
2172             };
2173              
2174             In a table class:
2175              
2176             package My::ORM::Table::Foo;
2177             use DBIx::QuickORM type => 'table';
2178              
2179             table foo => sub {
2180             # Sets the base class (@ISA) for this table class to 'My::Row::Class'
2181             row_class '+My::Row::Class';
2182             };
2183              
2184             =item C<db_name $NAME>
2185              
2186             Sometimes you want the ORM to use one name for a table or database, but the
2187             database server actually uses another. For example you may want the ORM to use the
2188             name C<people> for a table, but the database actually uses the table name C<populace>.
2189             You can use C<db_name> to set the in-database name.
2190              
2191             table people => sub {
2192             db_name 'populace';
2193              
2194             ...
2195             };
2196              
2197             This can also be used to have a different name for an entire database in the
2198             orm from its actual name on the server:
2199              
2200             db theapp => sub { # Name in the orm
2201             db_name 'myapp' # Actual name on the server;
2202             };
2203              
2204             =item C<< column NAME => sub { ... } >>
2205              
2206             =item C<< column NAME => %SPECS >>
2207              
2208             Define a column with the given name. The name will be used both as the name the
2209             ORM uses for the column, and the actual name of the column in the database.
2210             Currently having a column use a different name in the ORM vs the table is not
2211             supported.
2212              
2213             column foo => sub {
2214             type \'BIGINT'; # Specify a type in raw SQL (can also accept DBIx::QuickORM::Type::*)
2215              
2216             not_null(); # Column cannot be null
2217              
2218             # This column is an identity column, or is a primary key using
2219             # auto-increment. OR similar
2220             identity();
2221              
2222             ...
2223             };
2224              
2225             Another simple way to do everything above:
2226              
2227             column foo => ('not_null', 'identity', \'BIGINT');
2228              
2229             =item C<omit>
2230              
2231             When set on a column, the column will be omitted from C<SELECT>s by default. When
2232             you fetch a row the column will not be fetched until needed. This is useful if
2233             a table has a column that is usually huge and rarely used.
2234              
2235             column foo => sub {
2236             omit;
2237             };
2238              
2239             In a non-void context it will return the string C<omit> for use in a column
2240             specification without a builder.
2241              
2242             column bar => omit();
2243              
2244             =item C<nullable()>
2245              
2246             =item C<nullable(1)>
2247              
2248             =item C<nullable(0)>
2249              
2250             =item C<not_null()>
2251              
2252             =item C<not_null(1)>
2253              
2254             =item C<not_null(0)>
2255              
2256             Toggle nullability for a column. C<nullable()> defaults to setting the column as
2257             nullable. C<not_null()> defaults to setting the column as I<not> nullable.
2258              
2259             column not_nullable => sub {
2260             not_null();
2261             };
2262              
2263             column is_nullable => sub {
2264             nullable();
2265             };
2266              
2267             In a non-void context these will return a string, either C<nullable> or
2268             C<not_null>. These can be used in column specifications that do not use a
2269             builder.
2270              
2271             column foo => nullable();
2272             column bar => not_null();
2273              
2274             =item C<identity()>
2275              
2276             =item C<identity(1)>
2277              
2278             =item C<identity(0)>
2279              
2280             Used to designate a column as an identity column. This is mainly used for
2281             generating schema SQL. In a sufficient version of PostgreSQL this will generate
2282             an identity column. It will fallback to a column with a sequence, or in
2283             MySQL/SQLite it will use auto-incrementing columns.
2284              
2285             In a column builder it will set (default) or unset the C<identity> attribute of
2286             the column.
2287              
2288              
2289             column foo => sub {
2290             identity();
2291             };
2292              
2293             In a non-void context it will simply return C<identity> by default or when given
2294             a true value as an argument. It will return an empty list if a false argument
2295             is provided.
2296              
2297             column foo => identity();
2298              
2299             =item C<affinity('string')>
2300              
2301             =item C<affinity('numeric')>
2302              
2303             =item C<affinity('binary')>
2304              
2305             =item C<affinity('boolean')>
2306              
2307             When used inside a column builder it will set the columns affinity to the one
2308             specified.
2309              
2310             column foo => sub {
2311             affinity 'string';
2312             };
2313              
2314             When used in a non-void context it will return the provided string. This case
2315             is only useful for checking for typos as it will throw an exception if you use
2316             an invalid affinity type.
2317              
2318             column foo => affinity('string');
2319              
2320             =item C<< type(\$sql) >>
2321              
2322             =item C<< type("+My::Custom::Type") # The + is stripped off >>
2323              
2324             =item C<< type("+My::Custom::Type", @CONSTRUCTION_ARGS) >>
2325              
2326             =item C<< type("MyType") # Short for "DBIx::QuickORM::Type::MyType" >>
2327              
2328             =item C<< type("MyType", @CONSTRUCTION_ARGS) >>
2329              
2330             =item C<< type(My::Type->new(...)) >>
2331              
2332             Used to specify the type for the column. You can provide custom SQL in the form
2333             of a scalar referernce. You can also provide the class of a type, if you prefix
2334             the class name with a plus C<+> then it will strip the C<+> off and make no further
2335             modifications. If you provide a string without a C<+> it will attempt to load
2336             C<DBIx::QuickORM::Type::YOUR_STRING> and use that.
2337              
2338             In a column builder this will directly apply the type to the column being
2339             built.
2340              
2341             In scalar context this will return the constructed type object.
2342              
2343             column foo => sub {
2344             type 'MyType';
2345             };
2346              
2347             column foo => type('MyType');
2348              
2349             =item C<< sql($sql) >>
2350              
2351             =item C<< sql(infix => $sql) >>
2352              
2353             =item C<< sql(prefix => $sql) >>
2354              
2355             =item C<< sql(postfix => $sql) >>
2356              
2357             This is used when generating SQL to define the database.
2358              
2359             This allows you to provide custom SQL to define a table/column, or add SQL
2360             before (prefix) and after (postfix).
2361              
2362             Infix will prevent the typical SQL from being generated, the infix will be used
2363             instead.
2364              
2365             If no *fix is specified then C<infix> is assumed.
2366              
2367             =item C<default(\$sql)>
2368              
2369             =item C<default(sub { ... })>
2370              
2371             =item C<%key_val = default(\$sql)>
2372              
2373             =item C<%key_val = default(sub { ... })>
2374              
2375             When given a scalar reference it is treated as SQL to be used when generating
2376             SQL to define the column.
2377              
2378             When given a coderef it will be used as a default value generator for the
2379             column whenever DBIx::QuickORM C<INSERT>s a new row.
2380              
2381             In void context it will apply the default to the column being defined, or will
2382             throw an exception if no column is being built.
2383              
2384             column foo => sub {
2385             default \"NOW()"; # Used when generating SQL for the table
2386             default sub { 123 }; # Used when inserting a new row
2387             };
2388              
2389             This can also be used without a codeblock:
2390              
2391             column foo => default(\"NOW()"), default(sub { 123 });
2392              
2393             In the above cases they return:
2394              
2395             (sql_default => "NOW()")
2396             (perl_default => sub { 123 })
2397              
2398             =item C<columns(@names)>
2399              
2400             =item C<columns(@names, \%attrs)>
2401              
2402             =item C<columns(@names, sub { ... })>
2403              
2404             Define multiple columns at a time. If any attrs hashref or sub builder are
2405             specified they will be applied to B<all> provided column names.
2406              
2407             =item C<primary_key>
2408              
2409             =item C<primary_key(@COLUMNS)>
2410              
2411             Used to define a primary key. When used under a table you must provide a
2412             list of columns. When used under a column builder it designates just that
2413             column as the primary key, no arguments would be accepted.
2414              
2415             table mytable => sub {
2416             column a => sub { ... };
2417             column b => sub { ... };
2418              
2419             primary_key('a', 'b');
2420             };
2421              
2422             Or to make a single column the primary key:
2423              
2424             table mytable => sub {
2425             column a => sub {
2426             ...
2427             primary_key();
2428             };
2429             };
2430              
2431             =item C<unique>
2432              
2433             =item C<unique(@COLUMNS)>
2434              
2435             Used to define a unique constraint. When used under a table you must provide a
2436             list of columns. When used under a column builder it designates just that
2437             column as unique, no arguments would be accepted.
2438              
2439             table mytable => sub {
2440             column a => sub { ... };
2441             column b => sub { ... };
2442              
2443             unique('a', 'b');
2444             };
2445              
2446             Or to make a single column unique:
2447              
2448             table mytable => sub {
2449             column a => sub {
2450             ...
2451             unique();
2452             };
2453             };
2454              
2455             =item C<build_class $CLASS>
2456              
2457             Use this to override the class being built by a builder.
2458              
2459             schema myschema => sub {
2460             build_class 'DBIx::QuickORM::Schema::MySchemaSubclass';
2461              
2462             ...
2463             };
2464              
2465             =item C<my $meta = meta>
2466              
2467             Get the current builder meta hashref
2468              
2469             table mytable => sub {
2470             my $meta = meta();
2471              
2472             # This is what db_name('foo') would do!
2473             $meta->{name} = 'foo';
2474             };
2475              
2476             =item C<< plugin '+My::Plugin' >>
2477              
2478             =item C<< plugin 'MyPlugin' >>
2479              
2480             =item C<< plugin 'MyPlugin' => @CONSTRUCTION_ARGS >>
2481              
2482             =item C<< plugin 'MyPlugin' => \%CONSTRUCTION_ARGS >>
2483              
2484             =item C<< plugin My::Plugin->new() >>
2485              
2486             Load a plugin and apply it to the current builder (or top level) and all nested
2487             builders below it.
2488              
2489             The C<+> prefix can be used to specify a fully qualified plugin package name.
2490             Without the plus C<+> the namespace C<DBIx::QuickORM::Plugin::> will be prefixed to
2491             the string.
2492              
2493             plugin '+My::Plugin'; # Loads 'My::Plugin'
2494             plugin 'MyPlugin'; # Loads 'DBIx::QuickORM::Plugin::MyPlugin
2495              
2496             You can also provide an already blessed plugin:
2497              
2498             plugin My::Plugin->new();
2499              
2500             Or provide construction args:
2501              
2502             plugin '+My::Plugin' => (foo => 1, bar => 2);
2503             plugin '+MyPlugin' => {foo => 1, bar => 2};
2504              
2505             =item C<< $plugins = plugins() >>
2506              
2507             =item C<< plugins '+My::Plugin', 'MyPlugin' => \%ARGS, My::Plugin->new(...), ... >>
2508              
2509             Load several plugins at once, if a plugin class is followed by a hashref it is
2510             used as construction arguments.
2511              
2512             Can also be used with no arguments to return an arrayref of all active plugins
2513             for the current scope.
2514              
2515             =item C<< autofill() >>
2516              
2517             =item C<< autofill($CLASS) >>
2518              
2519             =item C<< autofill(sub { ... }) >>
2520              
2521             =item C<< autofill($CLASS, sub { ... }) >>
2522              
2523             =item C<< autofill $CLASS >>
2524              
2525             =item C<< autofill sub { ... } >>
2526              
2527             =item C<< autofill $CLASS => sub { ... } >>
2528              
2529             Used inside an C<orm()> builder. This tells QuickORM to build an
2530             L<DBIx::QuickORM::Schema> object by asking the database what tables and columns
2531             it has.
2532              
2533             orm my_orm => sub {
2534             db ...;
2535              
2536             autofill; # Autofill schema from the db itself
2537             };
2538              
2539             By default the L<DBIx::QuickORM::Schema::Autofill> class is used to do the
2540             autofill operation. You can provide an alternate class as the first argument if
2541             you wish to use a custom one.
2542              
2543             There are additional operations that can be done inside autofill, just provide
2544             a subref and call them:
2545              
2546             autofill sub {
2547             autotype $TYPE; # Automatically use DBIx::QuickORM::Type::TYPE classes when applicable
2548             autoskip table => qw/table1 table2/; # Do not generate schema for the specified tables
2549             autorow 'My::Row::Namespace'; # Automatically generate My::Row::Namespace::TABLE classes, also loading any that exist as .pm files
2550             autoname TYPE => sub { ... }; # Custom names for tables, accessors, links, etc.
2551             autohook HOOK => sub { ... }; # Run behavior at specific hook points
2552             };
2553              
2554             =item C<autotype $TYPE_CLASS>
2555              
2556             =item C<autotype 'JSON'>
2557              
2558             =item C<autotype '+DBIx::QuickORM::Type::JSON'>
2559              
2560             =item C<autotype 'UUID'>
2561              
2562             =item C<autotype '+DBIx::QuickORM::Type::UUID'>
2563              
2564             Load custom L<DBIx::QuickORM::Type> subclasses. If a column is found with the
2565             right type then the type class will be used to inflate/deflate the values
2566             automatically.
2567              
2568             =item C<autoskip table => qw/table1 table2 .../>
2569              
2570             =item C<autoskip column => qw/col1 col2 .../>
2571              
2572             Skip defining schema entries for the specified tables or columns.
2573              
2574             =item C<autorow 'My::App::Row'>
2575              
2576             =item C<autorow $ROW_BASE_CLASS>
2577              
2578             Generate C<My::App::Row::TABLE> classes for each table autofilled. If you write
2579             a F<My/App/Row/TABLE.pm> file it will be loaded as well.
2580              
2581             If you define a C<My::App::Row> class it will be loaded and all table rows will
2582             use it as a base class. If no such class is found the new classes will use
2583             L<DBIx::QuickORM::Row> as a base class.
2584              
2585             =item C<< autoname link_accessor => sub { ... } >>
2586              
2587             =item C<< autoname field_accessor => sub { ... } >>
2588              
2589             =item C<< autoname table => sub { ... } >>
2590              
2591             =item C<< autoname link => sub { ... } >>
2592              
2593             You can name the C<< $row->FIELD >> accessor:
2594              
2595             autoname field_accessor => sub {
2596             my %params = @_;
2597             my $name = $params{name}; # Name that would be used by default
2598             my $field_name = $params{field}; # Usually the same as 'name'
2599             my $table = $params{table}; # The DBIx::QuickORM::Schema::Table object
2600             my $column = $params{column}; # The DBIx::QuickORM::Schema::Table::Column object
2601              
2602             return $new_name;
2603             };
2604              
2605             You can also name the C<< $row->LINK >> accessor
2606              
2607             autoname link_accessor => sub {
2608             my %params = @_;
2609             my $name = $params{name}; # Name that would be used by default
2610             my $link = $params{link}; # DBIx::QuickORM::Link object
2611             my $table = $params{table}; # DBIx::QuickORM::Schema::Table object
2612             my $linked_table = $params{linked_table} # Name of the table being linked to
2613              
2614             # If the foreign key points to a unique row, then the accessor will
2615             # return a single row object:
2616             return "obtain_" . $linked_table if $link->unique;
2617              
2618             # If the foreign key points to non-unique rows, then the accessor will
2619             # return a DBIx::QuickORM::Query object:
2620             return "select_" . $linked_table . "s";
2621             };
2622              
2623             You can also provide custom names for tables. When using the table in the ORM
2624             you would use the name provided here, but under the hood the ORM will use the
2625             correct table name in queries.
2626              
2627             autoname table => sub {
2628             my %params = @_;
2629             my $name = $params{name}; # The name of the table in the database
2630             my $table = $params{table}; # A hashref that will be blessed into the DBIx::QuickORM::Schema::Table once the name is set.
2631              
2632             return $new_name;
2633             };
2634              
2635             You can also set aliases for links before they are constructed:
2636              
2637             autoname link => sub {
2638             my %params = @_;
2639             my $in_table = $params{in_table};
2640             my $in_fields = $params{in_fields};
2641             my $fetch_table = $params{fetch_table};
2642             my $fetch_fields = $params{fetch_fields};
2643              
2644             return $alias;
2645             };
2646              
2647             =item C<autohook HOOK => sub { my %params = @_; ... }>
2648              
2649             See L<DBIx::QuickORM::Schema::Autofill> for a list of hooks and their params.
2650              
2651             =back
2652              
2653             =head1 YOUR ORM PACKAGE EXPORTS
2654              
2655             =over 4
2656              
2657             =item C<< $orm_meta = orm() >>
2658              
2659             =item C<< $orm = orm($ORM_NAME) >>
2660              
2661             =item C<< $db = orm(db => $DB_NAME) >>
2662              
2663             =item C<< $schema = orm(schema => $SCHEMA_NAME) >>
2664              
2665             =item C<< $orm_variant = orm("${ORM_NAME}:${VARIANT}") >>
2666              
2667             =item C<< $db_variant = orm(db => "${DB_NAME}:${VARIANT}") >>
2668              
2669             =item C<< $schema_variant = orm(schema => "${SCHEMA_NAME}:${VARIANT}") >>
2670              
2671             This function is the one-stop shop to access any ORM, schema, or database instances
2672             you have defined.
2673              
2674             =back
2675              
2676             =head2 RENAMING THE EXPORT
2677              
2678             You can rename the C<orm()> function at import time by providing an alternate
2679             name.
2680              
2681             use My::ORM qw/renamed_orm/;
2682              
2683             my $orm = renamed_orm('my_orm');
2684              
2685             =head1 SOURCE
2686              
2687             The source code repository for DBIx::QuickORM can be found at
2688             L<https://https://github.com/exodist/DBIx-QuickORM>.
2689              
2690             =head1 MAINTAINERS
2691              
2692             =over 4
2693              
2694             =item Chad Granum E<lt>exodist@cpan.orgE<gt>
2695              
2696             =back
2697              
2698             =head1 AUTHORS
2699              
2700             =over 4
2701              
2702             =item Chad Granum E<lt>exodist@cpan.orgE<gt>
2703              
2704             =back
2705              
2706             =head1 COPYRIGHT
2707              
2708             Copyright Chad Granum E<lt>exodist@cpan.orgE<gt>.
2709              
2710             This program is free software; you can redistribute it and/or
2711             modify it under the same terms as Perl itself.
2712              
2713             See L<https://dev.perl.org/licenses/>
2714              
2715             =cut