| 1 | package DBIx::QuickORM::Role::SQLBuilder; | ||
| 2 | 307 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 307 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | 307 | our $VERSION = '0.000032'; | |
| 6 | |||
| 7 | 307 | ▶ | 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::SQLBuilder - The interface a handle builds SQL through. | ||
| 16 | |||
| 17 | =head1 DESCRIPTION | ||
| 18 | |||
| 19 | A L<DBIx::QuickORM::Handle> turns its request into SQL through the ORM's | ||
| 20 | C<sql_builder>. L<DBIx::QuickORM::SQLBuilder::SQLAbstract> is the default. | ||
| 21 | Any class composing this role can replace it. The ORM's CRUD operations write | ||
| 22 | their own SQL and do not use the builder. | ||
| 23 | |||
| 24 | Each method but C<combine_where> returns new SQL and a new arrayref of | ||
| 25 | binds, and executes nothing. C<combine_where> returns a condition. A bind is | ||
| 26 | C<[$field, $value]>: C<$field> names the field of the source's table the | ||
| 27 | value is compared with or written to, and decides how the value is bound. It | ||
| 28 | is undef when there is none. | ||
| 29 | |||
| 30 | A value may be a C<raw>, C<inflated>, or C<binary> marker | ||
| 31 | (L<DBIx::QuickORM::Value>), which the Connection resolves when it runs the | ||
| 32 | SQL. Identifiers are quoted through the dialect. | ||
| 33 | |||
| 34 | A C<where> and an order are opaque to everything but the builder, which | ||
| 35 | documents the syntax it accepts. Every builder must accept a hashref of field | ||
| 36 | names to plain values, meaning each field equals its value (undef: C<IS | ||
| 37 | NULL>), since the ORM builds that form itself. It must also accept value | ||
| 38 | markers as values in a C<where>. | ||
| 39 | |||
| 40 | A handle sends its C<where> with C<where_builders>: the where builders | ||
| 41 | (L<DBIx::QuickORM::Role::WhereBuilder>) to render it through, the first that | ||
| 42 | can build it winning. A builder should use them for the C<WHERE>. One that | ||
| 43 | ignores them renders every C<where> itself. | ||
| 44 | |||
| 45 | =head1 SYNOPSIS | ||
| 46 | |||
| 47 | package My::Builder; | ||
| 48 | use Role::Tiny::With qw/with/; | ||
| 49 | with 'DBIx::QuickORM::Role::SQLBuilder'; | ||
| 50 | |||
| 51 | sub new { bless {}, shift } # the ORM constructs a class it is given | ||
| 52 | |||
| 53 | sub select { my ($self, $source, $dialect, %request) = @_; ... return ($sql, \@binds) } | ||
| 54 | sub count { ... } | ||
| 55 | sub update { ... } | ||
| 56 | sub delete { ... } | ||
| 57 | sub combine_where { my ($self, %params) = @_; ... return $where } | ||
| 58 | |||
| 59 | =head1 REQUIRED METHODS | ||
| 60 | |||
| 61 | C<$source> is a L<DBIx::QuickORM::Role::Source> and C<$dialect> a | ||
| 62 | L<DBIx::QuickORM::Dialect> object. Without a C<where>, C<update> and C<delete> | ||
| 63 | write every row. | ||
| 64 | |||
| 65 | =over 4 | ||
| 66 | |||
| 67 | =item ($sql, \@binds) = $builder->select($source, $dialect, fields => $fields, where => $where, where_builders => \@builders, group_by => \@fields, having => $having, order_by => $order, limit => $n, offset => $n) | ||
| 68 | |||
| 69 | A C<SELECT>. Each request key is optional. A field name anywhere may be | ||
| 70 | qualified as C<name.field>. | ||
| 71 | |||
| 72 | =over 4 | ||
| 73 | |||
| 74 | =item * | ||
| 75 | |||
| 76 | C<fields>: an arrayref of field names, or a hashref of output names to what | ||
| 77 | each selects: a field name, an C<sql> marker, or C<[$FUNC, $field]> for | ||
| 78 | C<FUNC(field)> with the field quoted (the handle has checked C<$FUNC> is a | ||
| 79 | word). Without it every column is selected. | ||
| 80 | |||
| 81 | =item * | ||
| 82 | |||
| 83 | C<group_by>: field names. | ||
| 84 | |||
| 85 | =item * | ||
| 86 | |||
| 87 | C<having>: a condition in the C<where> syntax. | ||
| 88 | |||
| 89 | =item * | ||
| 90 | |||
| 91 | C<order_by>: may name output names. | ||
| 92 | |||
| 93 | =back | ||
| 94 | |||
| 95 | =item ($sql, \@binds) = $builder->count($source, $dialect, where => $where, where_builders => \@builders) | ||
| 96 | |||
| 97 | C<SELECT COUNT(*)>, its one column named C<count>. | ||
| 98 | |||
| 99 | =item ($sql, \@binds) = $builder->update($source, $dialect, \%values, where => $where, where_builders => \@builders) | ||
| 100 | |||
| 101 | An C<UPDATE> setting each field to its value. An C<sql> marker as a value is | ||
| 102 | written into the SQL with its binds. | ||
| 103 | |||
| 104 | =item ($sql, \@binds) = $builder->delete($source, $dialect, where => $where, where_builders => \@builders) | ||
| 105 | |||
| 106 | A C<DELETE> of the rows matching the C<where>. | ||
| 107 | |||
| 108 | =item $where = $builder->combine_where(op => $op, left => $left, right => $right) | ||
| 109 | |||
| 110 | One condition, in the builder's syntax, that holds when both sides hold | ||
| 111 | (C<op> C<'and'>) or when either does (C<'or'>). | ||
| 112 | |||
| 113 | This serves a handle's C<and> and C<or>: C<$left> is the handle's C<where>, | ||
| 114 | C<$right> the new condition. Value markers in either side must still work in | ||
| 115 | the result. | ||
| 116 | |||
| 117 | =back | ||
| 118 | |||
| 119 | =cut | ||
| 120 | |||
| 121 | 307 | requires qw/select count update delete combine_where/; | |
| 122 | |||
| 123 | 307 | 1; | |
| 124 | |||
| 125 | __END__ | ||