/[pg-zoom]/zoom.sql
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /zoom.sql

Parent Directory Parent Directory | Revision Log Revision Log


Revision 8 - (show annotations)
Thu Apr 26 21:45:09 2007 UTC (16 years, 11 months ago) by dpavlin
File size: 3134 byte(s)
added experimental support for CQF, but no servers seem to support it
1 create table servers (
2 name text not null,
3 host text not null,
4 port int not null default 210,
5 database text default 'Default',
6 primary key(name)
7 );
8
9 -- insert sample
10 insert into servers values ( 'nsk', '161.53.240.27', 8090, 'voyager' );
11 insert into servers values ( 'nsk-en', '161.53.240.27', 8190, 'voyager' );
12 insert into servers values ( 'loc', 'z3950.loc.gov', 7090, 'Voyager' );
13 insert into servers values ( 'ucs', 'library.usc.edu', 2200, 'unicorn' );
14 --insert into servers values ( '', '', , '' );
15
16 create type item as (
17 title text,
18 author text,
19 edition text,
20 date text
21 );
22
23 create or replace function search(text,text)
24 returns setof item
25 language plperlu
26 as $$
27
28 my $debug = 0;
29 my $max_res = 100;
30
31 my ( $server, $query ) = @_;
32
33 my $rv = spi_exec_query(qq{
34 select host,port,database from servers where name ilike '$server'
35 },1);
36
37 die "can't find server $server in table servers\n" unless ( $rv->{processed} == 1 );
38
39 my ( $host, $port, $database ) = (
40 $rv->{rows}[0]->{host},
41 $rv->{rows}[0]->{port},
42 $rv->{rows}[0]->{database},
43 );
44
45 use ZOOM;
46 use MARC::Record;
47 use Encode qw/encode decode/;
48
49 my $pqf = {
50 isbn => '@attr 1=7 @attr 4=1 "%s"',
51 title => '@attr 1=4 @attr 4=1 "%s"',
52 author => '@attr 1=1003 @attr 4=1 "%s"',
53 issn => '@attr 1=8 @attr 4=1 "%s"',
54 };
55
56 sub q2cqf {
57 my $q = shift;
58 if ($q =~ m/^(\w+):\s*(.*)$/) {
59 my ( $k,$v ) = ( $1,$2 );
60 return sprintf( $pqf->{ $k }, $v ) if ( defined( $pqf->{ $k } ) );
61 }
62 return $q;
63 }
64
65 my $conn = new ZOOM::Connection($host, $port,
66 databaseName => $database) or
67 die "can't connect to ${host}:${port}/${database}\n";
68
69 $conn->option(preferredRecordSyntax => "usmarc");
70
71 my $rs;
72
73 my $notice;
74
75 if ( $query =~ m/[\s="]|(and|or|not)/ ) {
76 $rs = $conn->search( new ZOOM::Query::CQL( $query ) );
77 $notice = 'CQL';
78 } else {
79 my $cqf = q2cqf( $query );
80 $notice = "CQF: $cqf";
81 $rs = $conn->search_pqf( $cqf );
82 }
83
84 my $n = $rs->size();
85 if ( $n > $max_res ) {
86 warn "query returned $n results, fetching first $max_res\n";
87 $n = $max_res;
88 }
89 # fetch all results
90 $rs->records(0, $n - 1, 0);
91
92 warn "$n results for '$query' $notice\n";
93
94 sub strip_non_digit {
95 my $d = shift;
96 $d =~ s/^\D+//;
97 $d =~ s/\D+$//;
98 return $d;
99 }
100
101 # fix encoding
102 sub e {
103 my $t = shift;
104 $t =~ s/éc/è/g;
105 $t =~ s/âc/æ/g;
106 $t =~ s/éz/¾/g;
107 $t =~ s/és/¹/g;
108 $t =~ s/³/ð/g;
109 $t =~ s/éC/È/g;
110 $t =~ s/âC/Æ/g;
111 $t =~ s/éZ/®/g;
112 $t =~ s/éS/©/g;
113 $t =~ s/£/Ð/g;
114 warn "## $t\n" if $debug;
115 # $t = decode('iso-8859-2', $t);
116 # return encode('utf-8',$t);
117 return $t;
118 }
119
120 foreach my $i ( 1 .. $n ) {
121 my $marc = new_from_usmarc MARC::Record( $rs->record( $i - 1 )->raw() );
122
123 return_next({
124 title => e( $marc->title ),
125 author => e( $marc->author ),
126 edition => e( $marc->edition ),
127 date => strip_non_digit( $marc->publication_date ),
128 });
129 }
130
131 return undef;
132
133 $$;
134
135 -- if your terminal isn't iso-8859-2, change this!
136 -- set client_encoding = 'iso-8859-2';
137
138 -- example
139 -- select * from search('title:djece');
140 -- select * from search('osman');
141
142 --select * from search('nsk','title:mor');
143 --select * from search('nsk','grada');
144 --select * from search('nsk-en','restrictions');
145
146 --select * from search('ucs','human');

  ViewVC Help
Powered by ViewVC 1.1.26