added "poll" flag to surveys; split up driver so we can use the CDBI stuff
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Storage / Driver / Pg / fts.pm
1 { # Every driver needs to provide a 'compile()' method to OpenILS::Application::Storage::FTS.
2   # If that driver wants to support FTI, that is...
3         #-------------------------------------------------------------------------------
4         package OpenILS::Application::Storage::FTS;
5         use OpenSRF::Utils::Logger qw/:level/;
6         my $log = 'OpenSRF::Utils::Logger';
7
8         sub compile {
9                 my $self = shift;
10                 my $term = shift;
11
12                 $self = ref($self) || $self;
13                 $self = bless {} => $self;
14
15                 $self->decompose($term);
16
17                 my $newterm = join('&', $self->words);
18
19                 if (@{$self->nots}) {
20                         $newterm = '('.$newterm.')&('. join('|', $self->nots) . ')';
21                 }
22
23                 $log->debug("Compiled term is [$newterm]", DEBUG);
24                 $newterm = OpenILS::Application::Storage::Driver::Pg->quote($newterm);
25                 $log->debug("Quoted term is [$newterm]", DEBUG);
26
27                 $self->{fts_query} = ["to_tsquery('default',$newterm)"];
28                 $self->{fts_query_nots} = [];
29                 $self->{fts_op} = '@@';
30                 $self->{text_col} = shift;
31                 $self->{fts_col} = shift;
32
33                 return $self;
34         }
35
36         sub sql_where_clause {
37                 my $self = shift;
38                 my $column = $self->fts_col;
39                 my @output;
40         
41                 my @ranks;
42                 for my $fts ( $self->fts_query ) {
43                         push @output, join(' ', $self->fts_col, $self->{fts_op}, $fts);
44                         push @ranks, "rank($column, $fts)";
45                 }
46                 $self->{fts_rank} = \@ranks;
47         
48                 my $phrase_match = $self->sql_exact_phrase_match();
49                 return join(' AND ', @output) . $phrase_match;
50         }
51
52 }
53
54 1;