NAME

DBIx::QuickORM::Role::SQLBuilder - The interface a handle builds SQL through.

DESCRIPTION

A DBIx::QuickORM::Handle turns its request into SQL through the ORM's sql_builder. DBIx::QuickORM::SQLBuilder::SQLAbstract is the default. Any class composing this role can replace it. The ORM's CRUD operations write their own SQL and do not use the builder.

Each method but combine_where returns new SQL and a new arrayref of binds, and executes nothing. combine_where returns a condition. A bind is [$field, $value]: $field names the field of the source's table the value is compared with or written to, and decides how the value is bound. It is undef when there is none.

A value may be a raw, inflated, or binary marker (DBIx::QuickORM::Value), which the Connection resolves when it runs the SQL. Identifiers are quoted through the dialect.

A where and an order are opaque to everything but the builder, which documents the syntax it accepts. Every builder must accept a hashref of field names to plain values, meaning each field equals its value (undef: IS NULL), since the ORM builds that form itself. It must also accept value markers as values in a where.

A handle sends its where with where_builders: the where builders (DBIx::QuickORM::Role::WhereBuilder) to render it through, the first that can build it winning. A builder should use them for the WHERE. One that ignores them renders every where itself.

SYNOPSIS

package My::Builder;
use Role::Tiny::With qw/with/;
with 'DBIx::QuickORM::Role::SQLBuilder';

sub new { bless {}, shift }    # the ORM constructs a class it is given

sub select { my ($self, $source, $dialect, %request) = @_; ... return ($sql, \@binds) }
sub count  { ... }
sub update { ... }
sub delete { ... }
sub combine_where { my ($self, %params) = @_; ... return $where }

REQUIRED METHODS

$source is a DBIx::QuickORM::Role::Source and $dialect a DBIx::QuickORM::Dialect object. Without a where, update and delete write every row.

($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)

A SELECT. Each request key is optional. A field name anywhere may be qualified as name.field.

  • fields: an arrayref of field names, or a hashref of output names to what each selects: a field name, an sql marker, or [$FUNC, $field] for FUNC(field) with the field quoted (the handle has checked $FUNC is a word). Without it every column is selected.

  • group_by: field names.

  • having: a condition in the where syntax.

  • order_by: may name output names.

($sql, \@binds) = $builder->count($source, $dialect, where => $where, where_builders => \@builders)

SELECT COUNT(*), its one column named count.

($sql, \@binds) = $builder->update($source, $dialect, \%values, where => $where, where_builders => \@builders)

An UPDATE setting each field to its value. An sql marker as a value is written into the SQL with its binds.

($sql, \@binds) = $builder->delete($source, $dialect, where => $where, where_builders => \@builders)

A DELETE of the rows matching the where.

$where = $builder->combine_where(op => $op, left => $left, right => $right)

One condition, in the builder's syntax, that holds when both sides hold (op 'and') or when either does ('or').

This serves a handle's and and or: $left is the handle's where, $right the new condition. Value markers in either side must still work in the result.

SOURCE

The source code repository for DBIx-QuickORM can be found at https://github.com/exodist/DBIx-QuickORM.

MAINTAINERS

Chad Granum <exodist7@gmail.com>

AUTHORS

Chad Granum <exodist7@gmail.com>

COPYRIGHT

Copyright Chad Granum <exodist7@gmail.com>.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

See https://dev.perl.org/licenses/