blib/lib/DBIx/QuickORM/Role/SQLBuilder.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.pm ↑ Summary blib/lib/DBIx/QuickORM/Role/Source.pm »
1 package DBIx::QuickORM::Role::SQLBuilder;
2 262 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 262 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 262 our $VERSION = '0.000033';
6  
7 262 ▶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 A builder that tags the SQL of C<select>, handing everything to the default
48 builder:
49  
50 package My::SQLBuilder;
51 use Role::Tiny::With qw/with/;
52 with 'DBIx::QuickORM::Role::SQLBuilder';
53  
54 use DBIx::QuickORM::SQLBuilder::SQLAbstract;
55  
56 # the ORM calls new() without arguments on a class it is given
57 sub new { bless {inner => DBIx::QuickORM::SQLBuilder::SQLAbstract->new}, shift }
58  
59 sub select {
60 my ($self, $source, $dialect, %request) = @_;
61 my ($sql, $binds) = $self->{inner}->select($source, $dialect, %request);
62 return ("/* my-app */ $sql", $binds);
63 }
64  
65 sub count { my $self = shift; $self->{inner}->count(@_) }
66 sub update { my $self = shift; $self->{inner}->update(@_) }
67 sub delete { my $self = shift; $self->{inner}->delete(@_) }
68 sub combine_where { my $self = shift; $self->{inner}->combine_where(@_) }
69  
70 package main;
71  
72 my $orm = DBIx::QuickORM->new(
73 credentials => \%credentials,
74 sql_builder => 'My::SQLBuilder', # a class name or an object
75 );
76  
77 # runs: /* my-app */ SELECT ... FROM "users" WHERE ( "name" = ? )
78 my @bobs = $orm->handle('users')
79 ->where({name => 'Bob'})
80 ->all;
81  
82 =head1 REQUIRED METHODS
83  
84 C<$source> is a L<DBIx::QuickORM::Role::Source> and C<$dialect> a
85 L<DBIx::QuickORM::Dialect> object. Without a C<where>, C<update> and C<delete>
86 write every row.
87  
88 =over 4
89  
90 =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)
91  
92 A C<SELECT>. Each request key is optional. A field name anywhere may be
93 qualified as C<name.field>.
94  
95 =over 4
96  
97 =item *
98  
99 C<fields>: an arrayref of field names, or a hashref of output names to what
100 each selects: a field name, an C<sql> marker, or C<[$FUNC, $field]> for
101 C<FUNC(field)> with the field quoted (the handle has checked C<$FUNC> is a
102 word). Without it every column is selected.
103  
104 =item *
105  
106 C<group_by>: field names.
107  
108 =item *
109  
110 C<having>: a condition in the C<where> syntax.
111  
112 =item *
113  
114 C<order_by>: may name output names.
115  
116 =back
117  
118 A handle sends what it has set. It adds C<where_builders> to every C<where>,
119 and sends C<fields> unless its source is a query with none set. A grouped
120 handle (C<group_by> or C<having>) sends computed C<fields>, by default its
121 C<group_by> fields:
122  
123 my $h = $orm->handle('users')
124 ->fields(['name'])
125 ->where({name => 'Bob'})
126 ->order_by('name')
127 ->limit(10);
128  
129 my @bobs = $h->all;
130  
131 # asks the SQL builder for
132 my ($sql, $binds) = $orm->sql_builder->select(
133 $h->source,
134 $orm->connection->dialect,
135 fields => ['name', 'id'], # the handle adds the primary key
136 where => {name => 'Bob'},
137 where_builders => $orm->where_builders,
138 order_by => 'name',
139 limit => 10,
140 );
141  
142 =item ($sql, \@binds) = $builder->count($source, $dialect, where => $where, where_builders => \@builders)
143  
144 C<SELECT COUNT(*)>, its one column named C<count>.
145  
146 =item ($sql, \@binds) = $builder->update($source, $dialect, \%values, where => $where, where_builders => \@builders)
147  
148 An C<UPDATE> setting each field to its value. An C<sql> marker as a value is
149 written into the SQL with its binds.
150  
151 use DBIx::QuickORM::Value qw/sql/;
152  
153 my $h = $orm->handle('posts')->where({id => 7});
154 $h->update({views => sql('views + ?', 1)});
155  
156 # asks the SQL builder for
157 my ($sql, $binds) = $orm->sql_builder->update(
158 $h->source,
159 $orm->connection->dialect,
160 {views => sql('views + ?', 1)}, # sql and binary markers as given
161 where => {id => 7},
162 where_builders => $orm->where_builders,
163 );
164  
165 Values arrive deflated: a C<raw> marker unwrapped as it is, an C<inflated>
166 marker or an inflated value deflated by its field's type.
167  
168 =item ($sql, \@binds) = $builder->delete($source, $dialect, where => $where, where_builders => \@builders)
169  
170 A C<DELETE> of the rows matching the C<where>.
171  
172 =item $where = $builder->combine_where(op => $op, left => $left, right => $right)
173  
174 One condition, in the builder's syntax, that holds when both sides hold
175 (C<op> C<'and'>) or when either does (C<'or'>).
176  
177 my $either = $orm->handle('users')
178 ->where({name => 'Bob'})
179 ->or({name => 'Ann'});
180  
181 # asks the SQL builder for
182 my $where = $orm->sql_builder->combine_where(
183 op => 'or',
184 left => {name => 'Bob'},
185 right => {name => 'Ann'},
186 );
187 # the default builder: {-or => [{name => 'Bob'}, {name => 'Ann'}]}
188 # which becomes $either->where
189  
190 This serves a handle's C<and> and C<or>: C<$left> is the handle's C<where>,
191 C<$right> the new condition. Value markers in either side must still work in
192 the result.
193  
194 =back
195  
196 =cut
197  
198 262 requires qw/select count update delete combine_where/;
199  
200 262 1;
201  
202 __END__
« blib/lib/DBIx/QuickORM/Role.pm ↑ Summary blib/lib/DBIx/QuickORM/Role/Source.pm »