/[pgestraier]/trunk/bin/pgest-index.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/bin/pgest-index.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 62 by dpavlin, Mon Aug 7 13:24:49 2006 UTC revision 67 by dpavlin, Mon Aug 7 16:38:24 2006 UTC
# Line 1  Line 1 
1  #!/usr/bin/perl -w  #!/usr/bin/perl -w
2    
3  use strict;  use strict;
4  use Search::Estraier 0.06;  use Search::Estraier 0.07;
5  use DBI;  use DBI;
6  use Data::Dumper;  use Data::Dumper;
7  use Encode qw/from_to/;  use Encode qw/from_to/;
# Line 16  pgest-index.pl - create full-text index Line 16  pgest-index.pl - create full-text index
16    
17  my $c = {  my $c = {
18          name => 'imenik',          name => 'imenik',
         node_url => 'http://localhost:1978/node/imenik',  
19          dbi => 'Pg:dbname=vip',          dbi => 'Pg:dbname=vip',
20          sql => qq{          sql => qq{
21                  select ime,tel from imenik                  select ime,tel from imenik
22          },          },
23          pk_col => 'tel',          pk => 'tel',
         db_encoding => 'iso-8859-2',  
24          debug => 1,          debug => 1,
         user => 'admin',  
         passwd => 'admin',  
25  };  };
26    
27  GetOptions($c, qw/node_url=s sql=s pk_col=s eb_encoding=s debug+ user=s passwd=s/);  =head1 SYNOPSIS
28    
29      pgest-index.pl --create movies --sql "select id,title,year from movies"
30    
31      pgsql-index.pl --drop movies
32    
33    Options:
34    
35    =over 4
36    
37    =item --create name
38    
39    Create index C<name> and create triggers on table with same name
40    
41    =item --drop name
42    
43    Remove triggers from table C<name> and node with same name
44    
45    =item --node-url http://localhost:1978/node/name
46    
47    Full URI to node. If it's not specified, it's assumed that you are using
48    Hyper Estraier on C<http://localhost:1978/>.
49    
50    =item --sql "select col1,col2 from name"
51    
52    SQL query which will return names of columns which are included in full-text
53    index. Have in mind that you can't use aliases (as I<something>) in this SQL
54    query (or triggers will be created with wrong fields).
55    
56    =item --pk id
57    
58    Specify name of primary key column in SQL query. If not specified, C<id> will be used.
59    
60    =item --user login
61    
62    =item --passwd password
63    
64    Username and password to use when connecting to Hyper Estraier. If not specified,
65    C<admin> and C<admin> will be used.
66    
67    =item --debug
68    
69    Dump debugging output. It may be specified multiple times for more verbose debugging.
70    
71    =back
72    
73    =cut
74    
75    GetOptions($c, qw/create=s drop=s node_url=s sql=s pk=s user=s passwd=s debug+/);
76    
77  warn "# c: ", Dumper($c) if ($c->{debug});  warn "# c: ", Dumper($c) if ($c->{debug});
78    
79    my $table = $c->{create} || $c->{drop} || die "$0 (--create|--drop) name [--sql='select id,foo,bar from table']\n";
80    
81    $c->{node_url} = 'http://localhost:1978/node/' . $table;
82    
83    $c->{user} ||= 'admin';
84    $c->{passwd} ||= 'admin';
85    
86  # create and configure node  # create and configure node
87  my $node = new Search::Estraier::Node(  my $node = new Search::Estraier::Node(
88          url => $c->{node_url},          url => $c->{node_url},
# Line 45  my $node = new Search::Estraier::Node( Line 96  my $node = new Search::Estraier::Node(
96  # create DBI connection  # create DBI connection
97  my $dbh = DBI->connect("DBI:$c->{dbi}","","") || die $DBI::errstr;  my $dbh = DBI->connect("DBI:$c->{dbi}","","") || die $DBI::errstr;
98    
99    # drop existing triggers
100    sub drop_triggers {
101            my $table = shift || die "no table?";
102    
103            warn "removing triggers from $table\n";
104    
105            foreach my $t (qw/UPDATE INSERT DELETE/) {
106                    my $lc_t = lc($t);
107                    $dbh->do(qq{ DROP TRIGGER pgest_trigger_${lc_t} ON ${table} });
108            }
109    }
110    
111    if ($c->{drop}) {
112            drop_triggers( $table );
113            warn "removing node $table\n";
114            $node->master(
115                    action => 'nodedel',
116                    name => $table,
117            );
118            exit;
119    }
120    
121    # clear existing node
122    $node->master(
123            action => 'nodeclr',
124            name => $table,
125    );
126    
127    # create PostgreSQL functions
128    $dbh->do(qq{
129    
130    CREATE OR REPLACE FUNCTION pgest(text, text, text, int, text, text, text, int, int, text[])
131            RETURNS setof record
132            AS 'pgest','pgest_node'
133            LANGUAGE 'C' IMMUTABLE CALLED ON NULL INPUT;
134    
135    CREATE OR REPLACE FUNCTION pgest_trigger() RETURNS TRIGGER
136            AS 'pgest', 'pgest_trigger'
137            LANGUAGE 'C' STRICT;
138    
139    }) || die $dbh->errstr();
140    
141    
142    drop_triggers( $table );
143    
144    $dbh->begin_work;
145    
146  my $sth = $dbh->prepare($c->{sql}) || die $dbh->errstr();  my $sth = $dbh->prepare($c->{sql}) || die $dbh->errstr();
147  $sth->execute() || die $sth->errstr();  $sth->execute() || die $sth->errstr();
148    
# Line 56  my $total = $sth->rows; Line 154  my $total = $sth->rows;
154  my $i = 1;  my $i = 1;
155    
156  my $t = time();  my $t = time();
157  my $pk_col = $c->{pk_col} || 'id';  my $pk = $c->{pk} || 'id';
158    
159  while (my $row = $sth->fetchrow_hashref() ) {  while (my $row = $sth->fetchrow_hashref() ) {
160    
# Line 65  while (my $row = $sth->fetchrow_hashref( Line 163  while (my $row = $sth->fetchrow_hashref(
163          # create document          # create document
164          my $doc = new Search::Estraier::Document;          my $doc = new Search::Estraier::Document;
165    
166          if (my $id = $row->{$pk_col}) {          if (my $id = $row->{$pk}) {
167                  $doc->add_attr('@uri', $id);                  $doc->add_attr('@uri', $id);
168          } else {          } else {
169                  die "can't find pk_col column '$pk_col' in results\n";                  die "can't find pk column '$pk' in results\n";
170          }          }
171    
172          printf "%4d ",$i;          printf "%4d ",$i;
# Line 76  while (my $row = $sth->fetchrow_hashref( Line 174  while (my $row = $sth->fetchrow_hashref(
174          while (my ($col,$val) = each %{$row}) {          while (my ($col,$val) = each %{$row}) {
175    
176                  if ($val) {                  if ($val) {
                         # change encoding?  
                         from_to($val, ($c->{db_encoding} || 'ISO-8859-1'), 'UTF-8');  
   
177                          # add attributes (make column usable from attribute search)                          # add attributes (make column usable from attribute search)
178                          $doc->add_attr($col, $val);                          $doc->add_attr($col, $val);
179    
# Line 100  while (my $row = $sth->fetchrow_hashref( Line 195  while (my $row = $sth->fetchrow_hashref(
195    
196  }  }
197    
198  my $table = $c->{name} || die "no name?";  my $cols = "'" . join("', '", @cols) . "'";
   
 my $cols = "'" . join("','", @cols) . "'";  
199    
200  foreach my $t (qw/UPDATE INSERT DELETE/) {  foreach my $t (qw/UPDATE INSERT DELETE/) {
201    
202          my $lc_t = lc($t);          my $lc_t = lc($t);
203    
         $dbh->do(qq{ DROP TRIGGER pgest_trigger_${lc_t} ON ${table} });  
   
204          my $sql = qq{          my $sql = qq{
205    
206                  CREATE TRIGGER pgest_trigger_${lc_t} AFTER ${t}                  CREATE TRIGGER pgest_trigger_${lc_t} AFTER ${t}
207                          ON ${table} FOR EACH ROW                          ON ${table} FOR EACH ROW
208                          EXECUTE PROCEDURE pgest_trigger('$c->{node_url}','$c->{user}','$c->{passwd}',                          EXECUTE PROCEDURE pgest_trigger('$c->{node_url}','$c->{user}','$c->{passwd}',
209                                  '$c->{pk_col}', $cols                                  '$c->{pk}', $cols
210                          )                          )
211    
212          };          };
213    
214          warn "$sql\n";          #warn "$sql\n";
215    
216          $dbh->do( $sql ) || die $dbh->errstr();          $dbh->do( $sql ) || die $dbh->errstr();
217    
218  }  }
219    
220    $dbh->commit;
221    
222    my $col_names = join(', ', @cols);
223    my $col_def = join(', ', map { "$_ text" } @cols);
224    
225    print "
226    ## example SQL search query:
227    
228    SELECT $col_names
229    FROM pgest(
230            -- node, login, passwd, depth
231            '$c->{node_url}', '$c->{user}', '$c->{passwd}', 0,
232            -- full text search
233            'foo bar',
234            -- attribute filter, order, limit, offset
235            null, null, null, null,
236            -- return columns
237            array[$cols]
238    ) as ($col_def);
239    
240    ";

Legend:
Removed from v.62  
changed lines
  Added in v.67

  ViewVC Help
Powered by ViewVC 1.1.26