File Coverage

blib/lib/Search/QueryParser/SQL/Column.pm
Criterion Covered Total %
statement 15 30 50.0
branch 0 4 0.0
condition 0 10 0.0
subroutine 5 8 62.5
pod 2 2 100.0
total 22 54 40.7


line stmt bran cond sub pod time code
1             package Search::QueryParser::SQL::Column;
2 1     1   15 use strict;
  1         18  
  1         77  
3 1     1   10 use warnings;
  1         3  
  1         63  
4 1     1   9 use base qw( Class::Accessor::Fast );
  1         3  
  1         2196  
5 1     1   4148 use Carp;
  1         2  
  1         215  
6              
7             __PACKAGE__->mk_accessors(
8             qw( type name alias fuzzy_op fuzzy_not_op callback orm_callback is_int ));
9              
10 1     1   6 use overload '""' => 'stringify', 'fallback' => 1;
  1         3  
  1         9  
11              
12             our $VERSION = '0.010';
13              
14             my $debug = $ENV{PERL_DEBUG} || 0;
15              
16             =head1 NAME
17              
18             Search::QueryParser::SQL::Column - SQL column object
19              
20             =head1 SYNOPSIS
21              
22             my $column = Search::QueryParser::SQL::Column->new(
23             name => 'foo',
24             type => 'char',
25             alias => 'bar',
26             fuzzy_op => 'ILIKE',
27             fuzzy_not_op => 'NOT ILIKE',
28             callback => sub {
29             my ($col, $op, $val) = @_;
30             return "$col $op $val";
31             },
32             orm_callback => sub {
33             my ($col, $op, $val) = @_;
34             return( $col => { $op => $val } );
35             },
36             );
37              
38             =head1 DESCRIPTION
39              
40             This class represents a column in a database table,
41             and is used for rendering SQL correctly.
42            
43             =head1 METHODS
44              
45             Only new or overridden method are documented here.
46              
47             =cut
48              
49             =head2 new( I )
50              
51             Instantiate a new Column object. I may be a hash or hashref.
52              
53             I keys are also accessor methods:
54              
55             =over
56              
57             =item name
58              
59             The column name. Stringifies to this value.
60              
61             =item type
62              
63             SQL column type: char, varchar, integer, datetime, timestamp, etc.
64              
65             =item alias
66              
67             Alternate names for the column. May be a scalar string or array ref.
68              
69             =item fuzzy_op
70              
71             The operator to use when a column value has a wildcard attached.
72             For text columns this defaults to C. For numeric columns
73             this defaults to ">=".
74              
75             =item fuzzy_not_op
76              
77             The operator to use when a column value has a wildcard attached
78             and is negated. For text columns this defaults to C.
79             For numeric columns this defaults to "! >=".
80              
81             =item callback
82              
83             Should be a CODE reference expecting three arguments: the Column object,
84             the operator in play, and the value. Should return a string to be
85             pushed onto the Query buffer.
86              
87             =item orm_callback
88              
89             Like callback but should return a pair of values: the column name
90             and either a value or a hashref with the operator as the key.
91              
92             =back
93              
94             =cut
95              
96             sub new {
97 0     0 1   my $class = shift;
98 0 0         my $args = ref( $_[0] ) ? $_[0] : {@_};
99 0           my $self = $class->SUPER::new($args);
100 0           $self->__setup;
101 0           return $self;
102             }
103              
104             sub __setup {
105 0     0     my $self = shift;
106 0   0       $self->{type} ||= 'char';
107              
108             # numeric types
109 0 0         if ( $self->{type} =~ m/int|float|bool|time|date/ ) {
110 0   0       $self->{fuzzy_op} ||= '>=';
111 0   0       $self->{fuzzy_not_op} ||= '! >=';
112 0           $self->{is_int} = 1;
113             }
114              
115             # text types
116             else {
117 0   0       $self->{fuzzy_op} ||= 'ILIKE';
118 0   0       $self->{fuzzy_not_op} ||= 'NOT ILIKE';
119 0           $self->{is_int} = 0;
120             }
121              
122             }
123              
124             =head2 stringify
125              
126             Returns Column name. Column objects overload to this method.
127              
128             =cut
129              
130             sub stringify {
131 0     0 1   return shift->name;
132             }
133              
134             1;
135              
136             __END__