NAME

Teng::Plugin::SearchBySQLAbstractMore - use SQL::AbstractMore as Query Builder for Teng

VERSION

Version 0.01

SYNOPSIS

package MyApp::DB;
use parent qw/Teng/;
__PACKAGE__->load_plugin('SearchBySQLAbstractMore');

package main;
my $db = MyApp::DB->new(dbh => $dbh);
my $page = $c->req->param('page') || 1;
my ($rows, $pager) = $db->search_by_sql_abstract_more('user' => {type => 3}, {page => $page, rows => 5});

If you want to replace Teng search

package MyApp::DB;
use parent qw/Teng/;
__PACKAGE__->load_plugin('SearchBySQLAbstractMore');
__PACKAGE__->install_sql_abstract_more;
# now, search method is replaced by search_by_sql_abstract_more

If you want to load pager at the same time

# search_with_pager from SearchBySQLAbstractMore::Pager
__PACKAGE__->install_sql_abstract_more(pager => 'Pager');
# search_with_pager from SearchBySQLAbstractMore::Pager::MySQLFoundRows
__PACKAGE__->install_sql_abstract_more(pager => 'Pager::MySQLFoundRows');

Create complex SQL using SQL::Abstract::More.

Compatible usage with Teng's search method.

$teng->search_by_sql_abstract_more
   ('table1',
    { name => { like => '%perl%'},
      -and => [
               {x => 1},
               {y => [-or => {'>' => 2}, {'<' => 10}]},
              ],
    },
    {
     from     => ['-join',
                  'table1|t1',
                  't1.id=t2.id',
                  'table2|t2',
                  't1.id=t3.table1_id,t2.id=t3.table2_id',
                  'table3|t3',
                 ],
     columns  => ['x', 'y', 'min(age) as min_age', 'max(age) as max_age'],
     group_by => ['x', 'y'],
     having   => {'max_age' => {'<' => 10}},
    },
   );
# SELECT x, y, min(age) as min_age, max(age) as max_age
#   FROM table1 AS t1
#     INNER JOIN table2 AS t2 ON ( t1.id = t2.id )
#     INNER JOIN table3 AS t3 ON ( ( t1.id = t3.table1_id AND t2.id = t3.table2_id ) )
#   WHERE ( ( ( x = ? AND ( y > ? OR y < ? ) ) AND name LIKE ? ) )
#   GROUP BY x, y  HAVING ( max_age < ? );

SQL::Abstract::More original usage(as first argument, use hash ref instead of teble name):

$teng->search_by_sql_abstract_more(
  {
    -columns  => ['x', 'y', 'min(age) as min_age', 'max(age) as max_age'],
    -from     => [-join,
                  'table1|t1',
                  't1.id=t2.id',
                  'table2|t2',
                  't1.id=t3.table1_id,t2.id=t3.table2_id',
                  'table3|t3',
                ],
    -group_by => ['x', 'y'],
    -having   => {'max_age' => {'<' => 10'}},
    -where => {
       name => { like => '%perl%'},
       -and => [
           {x => 1},
           {y => [-or => {'>' => 2}, {'<' => 10}]},
       ],
     },
  },
);
# SQL is as same as the avobe code.

Using pager.

Compatible usage:

$teng->search_by_sql_abstract_more(
  'table', {
    name => 1,
    age  => 10,
  },
  {
    -columns  => ['x', 'y'],
    -from     => ['table'],
    -page     => 2,
    -rows     => 20,
  },
);

Originaly usage:

$teng->search_by_sql_abstract_more(
  {
    -columns  => ['x', 'y'],
    -from     => ['table'],
    -where    => {
         name => 1,
         age  => 10,
    },
    -page     => 2,
    -rows     => 20,
  },
);

METHODS

search_by_sql_abstract_more

see SYNOPSIS.

CLASS METHOD

If you want to replace search method of original Teng, call this.

Teng::Plugin::SearchBySQLAbstractMore->replace_teng_search;

It is useful when you wrap search method in your module and call Teng's search method in it and you want to use same usage with SQL::Abstract::More.

install_sql_abstract_more

$your_class->install_sql_abstract_more;
$your_class->install_sql_abstract_more(replace => 1); # same as the above

# use pager
$your_class->install_sql_abstract_more(pager => 1);
$your_class->install_sql_abstract_more(pager => 'mysql_pager');

It call replace_teng_search if replace option is not passed or replace option is true and loads pager plugin with alias option. search and search_with_pager are installed.

This option can take the following options.

replace

If you don't want to replace Teng's search method, pass this option with false.

$your_class->install_sql_abstract_more(replace => 0);

pager

Pass pager plugin name or 1.

$your_class->install_sql_abstract_more(pager => 1);       # load SearchBySQLAbstractMore::Pager
$your_class->install_sql_abstract_more(pager => 'Pager'); # same as the above
$your_class->install_sql_abstract_more(pager => 'pager'); # same as the above

$your_class->install_sql_abstract_more(pager => 'Pager::MySQLFoundRows');# load SearchBySQLAbstractMore::Pager::MySQLFoundRows
$your_class->install_sql_abstract_more(pager => 'mysql_pager');          # same as the above

AUTHOR

Ktat, <ktat at cpan.org>

BUGS

Please report any bugs or feature requests to bug-teng-plugin-searchbysqlabstractmore at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Teng-Plugin-SearchBySQLAbstractMore. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

You can find documentation for this module with the perldoc command.

perldoc Teng::Plugin::SearchBySQLAbstractMore

You can also look for information at:

ACKNOWLEDGEMENTS

LICENSE AND COPYRIGHT

Copyright 2012 Ktat.

This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.

See http://dev.perl.org/licenses/ for more information.