=head1 NAME

Mad::Mapper::Guides::Custom - How to do custom queries

=head1 OVERVIEW

This guide will show how to run custom queries for full control.

=head1 GUIDE

=head2 Half control

It is possible to override the private methods C<_find_sst()>,
C<_insert_sst()>, C<_update_sst()> and C<_delete_sst()> for more
control. The benefit of overriding these methods is if you want
to use optional columns for doing queries. The example below can
find a row base on both the "id" and "email" column.

  sub _find_sst {
    my $self = shift;

    if ($self->email) {
      return $self->expand_sst("SELECT %pc FROM %t WHERE email=?"), $self->email;
    }
    else {
      return $self->expand_sst("SELECT %pc FROM %t WHERE id=?"), $self->id;
    }
  }

=head2 Full control

Instead of using the automatic generated methods from simple SQL statements,
it is possible to do the complete query yourself. Below is the example of how
a completely custom C<_insert()>:

  package MyApp::Model::User;
  use Mad::Mapper -base;

  sub _insert {
    my ($self, $cb) = @_;

    Mojo::IOLoop->delay(
      sub {
        my ($delay) = @_;
        $self->db->query("INSERT INTO users (email) VALUES (?)", $self->email, $delay->begin);
      },
      sub {
        my ($delay, $err, $res) = @_;
        return $self->$cb($err) if $err;
        $self->in_storage(1);
        $self->id($db->dbh->last_insert_id(undef, undef, $self->table, $self->pk));
        $self->$cb("");
      },
    );
  }

You can also override C<_find()>, C<_update()> and C<_delete()>.

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2014, Jan Henning Thorsen

This program is free software, you can redistribute it and/or modify it under
the terms of the Artistic License version 2.0.

=head1 AUTHOR

Jan Henning Thorsen - C<jhthorsen@cpan.org>

=cut
