blib/lib/DBIx/QuickORM/Role/Source.pm

filter: Statementstmt 100% 12 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 15 / 15 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/DBIx/QuickORM/Role/SQLBuilder.pm ↑ Summary blib/lib/DBIx/QuickORM/Role/WhereBuilder.pm »
1 package DBIx::QuickORM::Role::Source;
2 305 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 305 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 305 our $VERSION = '0.000032';
6  
7 305 ▶use Role::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =pod
10  
11 =encoding UTF-8
12  
13 =head1 NAME
14  
15 DBIx::QuickORM::Role::Source - What a handle reads from.
16  
17 =head1 DESCRIPTION
18  
19 A source is what a L<DBIx::QuickORM::Handle> selects from: a table
20 (L<DBIx::QuickORM::Source::Table>), a query (L<DBIx::QuickORM::Source::Query>),
21 or tables joined together (L<DBIx::QuickORM::Source::Join>). Most code gets
22 one from C<< $orm->handle >> and never touches it.
23  
24 The handle asks a source what to select and has it turn the fetched rows into
25 results. The SQL builder asks it for the C<FROM> part of a statement. The
26 Connection asks it which table, if any, its rows belong to and what fields the
27 names in the SQL stand for.
28  
29 Rows from a source without a table or without a primary key bypass the lookup
30 table: they are never canon and cannot be updated, refreshed, or deleted
31 through the ORM.
32  
33 =head1 SYNOPSIS
34  
35 package My::Source;
36 use Object::HashBase qw{<view};
37 use Role::Tiny::With qw/with/;
38 with 'DBIx::QuickORM::Role::Source';
39  
40 sub table { undef }
41 sub pk { undef }
42 sub select_fields { $_[1] }
43 sub field_records { {} }
44 sub query_sql { () }
45 sub from_sql { my ($self, $dialect) = @_; ($dialect->quote_identifier($self->view), []) }
46  
47 sub results {
48 my ($self, $orm, $raw, $ingest, %opts) = @_;
49 ($raw, $orm->_row_builder(undef, %opts));
50 }
51  
52 =head1 REQUIRED METHODS
53  
54 =over 4
55  
56 =item $name = $source->table
57  
58 The table the rows belong to, or undef when they belong to none.
59  
60 =item \@fields = $source->pk
61  
62 The primary key fields, or undef when there is none.
63  
64 =item $fields = $source->select_fields($given)
65  
66 What a select names, for the builder's C<fields> request (see
67 L<DBIx::QuickORM::Role::SQLBuilder>), given the handle's C<fields>.
68  
69 C<$given> is undef when the handle has none (the result may be undef too, for
70 every column), an arrayref of field names (to which a primary key is added),
71 or a hashref of computed fields, which is used as it is. A table or join
72 croaks for names it does not have. A query passes them on.
73  
74 =item \%fields = $source->field_records
75  
76 The map's field records (see L<DBIx::QuickORM::Map>) by the names the SQL and
77 the fetched columns use them under, or empty when there are none. They are
78 used for binding and fetching values by their fields.
79  
80 The hash may be shared and returned on every call: callers read it and never
81 change it.
82  
83 =item ($sql, \@binds) = $source->from_sql($dialect)
84  
85 What goes after C<FROM>, with its binds as C<[$field, $value]> pairs (the
86 field undef when there is none).
87  
88 =item ($sql, \@binds) = $source->query_sql
89  
90 A complete query to run as it is when the handle adds nothing to it, or an
91 empty list when the source is not a query.
92  
93 =item (\@rows, \&build) = $source->results($orm, \@raw, $ingest, %opts)
94  
95 Turns the rows a query fetched (C<\@raw>, hashrefs keyed by column name) into
96 results: C<\@rows> for the iterator (L<DBIx::QuickORM::Iterator>) and
97 C<build>, which turns each into what the iterator hands out, or undef to
98 hand the rows out as they are. C<< $orm->_row_builder >> gives the C<build>
99 for rows of a table.
100  
101 Ingesting happens here, at once, through the ORM's Connection
102 (C<< $orm->connection >>), unless C<$ingest> is false: then rows are kept as
103 fetched and never enter the lookup table (a C<no_cache> handle's rows).
104 Building happens as each row is handed out.
105  
106 C<row_class> in C<%opts> is the handle's C<row_class>, present (even undef)
107 only when the handle sets one. Pass the options on to C<_row_builder> to
108 build rows the way the handle asks.
109  
110 =back
111  
112 =cut
113  
114 305 requires qw/table pk select_fields field_records from_sql query_sql results/;
115  
116 305 1;
117  
118 __END__
« blib/lib/DBIx/QuickORM/Role/SQLBuilder.pm ↑ Summary blib/lib/DBIx/QuickORM/Role/WhereBuilder.pm »