NAME
Search::QueryParser::SQL - turn free-text queries into SQL WHERE clauses
SYNOPSIS
use Search::QueryParser::SQL;
my $parser = Search::QueryParser::SQL->new(
columns => [qw( first_name last_name email )]
);
my $query = $parser->parse('joe smith', 1); # 1 for explicit AND
print $query;
# prints:
# (first_name='joe' OR last_name='joe' OR email='joe') AND \
# (first_name='smith' OR last_name='smith' OR email='smith')
# for the DBI
my $query = $parser->parse('foo');
print $query->dbi->[0];
# prints
# (first_name=? OR last_name=? OR email=?)
# wildcard support
my $query = $parser->parse('foo*');
print $query;
# prints
# (first_name ILIKE 'foo%' OR last_name ILIKE 'foo%' OR email ILIKE 'foo%')
DESCRIPTION
Search::QueryParser::SQL is a subclass of Search::QueryParser. Chiefly it extends the unparse() method to stringify free-text search queries as valid SQL WHERE clauses.
The idea is to allow you to treat your database like a free-text search index, when it really isn't.
METHODS
Only new or overridden method are documented here.
new( args )
Returns a new Parser. In addition to the args documented in Search::QueryParser, this new() method supports additional args:
- columns
-
Required
May be a hash or array ref of column names. If a hash ref, the keys should be column names and the values either the column type (e.g., int, varchar, etc.) or a hashref of attributes used to instantiate a Search::QueryParser::SQL::Column object.
The values are used for determining correct quoting in strings and for operator selection with wildcards. If passed as an array ref, all column arguments will be treated like 'char'.
See Search::QueryParser::SQL::Column for more information.
- default_column
-
Optional
The column name or names to be used when no explicit column name is used in a query string. If not present, defaults to columns.
- quote_columns
-
Optional
The default behaviour is to not quote column names, but some SQL dialects expect column names to be quoted (escaped).
Set this arg to a quote value. Example:
my $parser = Search::QueryParser::SQL->new( columns => [qw( foo bar )], quote_columns => '`' ); # query will look like `foo` and `bar`
- fuzzify
-
Optional
Treat all query keywords as if they had wildcards attached to the end. E.g.,
foo
would be treated likefoo*
. - fuzzify2
-
Optional
Like fuzzify but prepend wildcards as well. E.g.,
foo
would be treated like*foo*
. - strict
-
Optional
Croak if any of the column names in string are not among the supplied column names in columns.
- like
-
Optional
The SQL operator to use for wildcard query strings. The default is
ILIKE
. - lower
-
Optional
Wrap the
LOWER()
function around column names for case-insensitive comparison. - column_class
-
Optional
The name of the class to bless Column objects into. Default is
Search::QueryParser::SQL::Column
.
parse( string [, implicit_AND] )
Acts like parse() method in Search::QueryParser, but returns a Search::QueryParser::SQL::Query object.
If a second, true, value is passed as implicit_AND, the query is assumed to "AND" terms together. The default is to "OR" them together.
columns
Get/set the column descriptions, which is a hashref of Search::QueryParser::SQL::Column objects keyed by the column name.
get_column( name )
Returns the Column object for name or croaks if it has not been defined.
AUTHOR
Peter Karman, <karman@cpan.org>
BUGS
Please report any bugs or feature requests to bug-search-queryparser-sql@rt.cpan.org
, or through the web interface at http://rt.cpan.org. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
ACKNOWLEDGEMENTS
The Minnesota Supercomputing Institute http://www.msi.umn.edu/
sponsored the development of this software.
COPYRIGHT & LICENSE
Copyright 2008 by the Regents of the University of Minnesota.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.