| 1 | package DBIx::QuickORM::Role::WhereBuilder; | ||
| 2 | 264 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 264 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | 264 | our $VERSION = '0.000033'; | |
| 6 | |||
| 7 | 264 | ▶ | 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::WhereBuilder - The interface a handle's WHERE is rendered through. | ||
| 16 | |||
| 17 | =head1 DESCRIPTION | ||
| 18 | |||
| 19 | The SQL builder renders a handle's C<where> through a chain of where | ||
| 20 | builders: the first whose C<can_build_where> is true builds it. The chain is | ||
| 21 | the ORM's C<where_builders>, or the handle's when set. The default chain is | ||
| 22 | L<DBIx::QuickORM::WhereBuilder::RawSQL>, | ||
| 23 | L<DBIx::QuickORM::WhereBuilder::Simple>, then | ||
| 24 | L<DBIx::QuickORM::WhereBuilder::SQLAbstract>, which takes everything. | ||
| 25 | |||
| 26 | With the default SQL builder, a C<where> that every builder in the chain | ||
| 27 | declines croaks. | ||
| 28 | |||
| 29 | Only the C<WHERE> goes through the chain. C<having> and C<order_by> do not. | ||
| 30 | |||
| 31 | A where builder is a class or an object, and its methods are called on it as | ||
| 32 | given. | ||
| 33 | |||
| 34 | A handle's C<and> and C<or> combine its C<where> in the SQL builder's syntax | ||
| 35 | (C<combine_where>). A shape only your builder knows reaches it whole only | ||
| 36 | through C<where>. | ||
| 37 | |||
| 38 | =head1 SYNOPSIS | ||
| 39 | |||
| 40 | A builder that takes C<< {ids => \@ids} >>. An empty list matches no rows. | ||
| 41 | A shape that let an empty list fall through to another builder could render | ||
| 42 | no C<WHERE> at all, and an C<update> or C<delete> would then touch every row. | ||
| 43 | |||
| 44 | |||
| 45 | package My::WhereBuilder::Ids; | ||
| 46 | use Role::Tiny::With qw/with/; | ||
| 47 | with 'DBIx::QuickORM::Role::WhereBuilder'; | ||
| 48 | |||
| 49 | sub can_build_where { | ||
| 50 | my ($class, $where) = @_; | ||
| 51 | return 0 unless ref($where) eq 'HASH' && keys(%$where) == 1 && ref($where->{ids}) eq 'ARRAY'; | ||
| 52 | return !grep { !defined($_) || ref($_) || !m/^\d+\z/ } @{$where->{ids}}; # every id a plain integer | ||
| 53 | } | ||
| 54 | |||
| 55 | sub build_where { | ||
| 56 | my ($class, $where, $source, $dialect) = @_; | ||
| 57 | my @ids = @{$where->{ids}}; | ||
| 58 | return ('1 = 0', []) unless @ids; # no ids match no rows, never every row | ||
| 59 | |||
| 60 | my $id = $dialect->quote_identifier('id'); # assumes the key field is named id | ||
| 61 | my $in = join ', ' => map { '?' } @ids; | ||
| 62 | return ("$id IN ($in)", [map { ['id', $_] } @ids]); # each bound as the id field | ||
| 63 | } | ||
| 64 | |||
| 65 | package main; | ||
| 66 | |||
| 67 | # first in the ORM's chain, the defaults after it | ||
| 68 | my $orm = DBIx::QuickORM->new( | ||
| 69 | credentials => \%credentials, | ||
| 70 | where_builders => [ | ||
| 71 | 'My::WhereBuilder::Ids', | ||
| 72 | 'DBIx::QuickORM::WhereBuilder::RawSQL', | ||
| 73 | 'DBIx::QuickORM::WhereBuilder::Simple', | ||
| 74 | 'DBIx::QuickORM::WhereBuilder::SQLAbstract', | ||
| 75 | ], | ||
| 76 | ); | ||
| 77 | |||
| 78 | # SELECT ... FROM "users" WHERE "id" IN (?, ?) | ||
| 79 | my @users = $orm->handle('users') | ||
| 80 | ->where({ids => [1, 3]}) | ||
| 81 | ->all; | ||
| 82 | |||
| 83 | # any other where falls through to the defaults | ||
| 84 | my @bobs = $orm->handle('users') | ||
| 85 | ->where({name => 'Bob'}) | ||
| 86 | ->all; | ||
| 87 | |||
| 88 | A handle can take its own chain instead of the ORM's: | ||
| 89 | |||
| 90 | my $by_id = $orm->handle('users')->where_builders(['My::WhereBuilder::Ids']); | ||
| 91 | |||
| 92 | my @rows = $by_id->where({ids => [1, 3]})->all; | ||
| 93 | |||
| 94 | =head1 REQUIRED METHODS | ||
| 95 | |||
| 96 | The L</SYNOPSIS> builder shows both. | ||
| 97 | |||
| 98 | =over 4 | ||
| 99 | |||
| 100 | =item $bool = $builder->can_build_where($where) | ||
| 101 | |||
| 102 | True when this builder renders C<$where>. The chain asks each builder in | ||
| 103 | order until one says true, for every C<where> built, so keep it cheap. | ||
| 104 | |||
| 105 | =item ($sql, \@binds) = $builder->build_where($where, $source, $dialect) | ||
| 106 | |||
| 107 | The condition without the C<WHERE> keyword, or an empty string for none, and | ||
| 108 | its binds. Binds are C<[$field, $value]> pairs as | ||
| 109 | L<DBIx::QuickORM::Role::SQLBuilder> describes them. C<$source> is a | ||
| 110 | L<DBIx::QuickORM::Role::Source> and C<$dialect> a L<DBIx::QuickORM::Dialect>. | ||
| 111 | |||
| 112 | =back | ||
| 113 | |||
| 114 | =cut | ||
| 115 | |||
| 116 | 264 | requires qw/can_build_where build_where/; | |
| 117 | |||
| 118 | 264 | 1; | |
| 119 | |||
| 120 | __END__ | ||