| 1 | package DBIx::QuickORM::Role::Source; | ||
| 2 | 259 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 259 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | 259 | our $VERSION = '0.000031'; | |
| 6 | |||
| 7 | 259 | ▶ | 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, @class) = @_; | ||
| 49 | ($raw, $orm->_row_builder(undef, @class)); | ||
| 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, @class) | ||
| 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<@class> is the handle's C<row_class> as a one-item list (even undef), or | ||
| 107 | empty when the handle sets none. | ||
| 108 | |||
| 109 | =back | ||
| 110 | |||
| 111 | =cut | ||
| 112 | |||
| 113 | 259 | requires qw/table pk select_fields field_records from_sql query_sql results/; | |
| 114 | |||
| 115 | 259 | 1; | |
| 116 | |||
| 117 | __END__ | ||