/[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 7 - (show annotations)
Thu Apr 26 21:33:46 2007 UTC (17 years ago) by dpavlin
File size: 2960 byte(s)
better error message, removed more non-working servers
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 $cqf = q2cqf( $query );
72
73 my $rs = $conn->search_pqf( $cqf );
74
75 my $n = $rs->size();
76 if ( $n > $max_res ) {
77 warn "query returned $n results, fetching first $max_res\n";
78 $n = $max_res;
79 }
80 # fetch all results
81 $rs->records(0, $n - 1, 0);
82
83 warn "$n results for '$query' [$cqf]\n";
84
85 sub strip_non_digit {
86 my $d = shift;
87 $d =~ s/^\D+//;
88 $d =~ s/\D+$//;
89 return $d;
90 }
91
92 # fix encoding
93 sub e {
94 my $t = shift;
95 $t =~ s/éc/è/g;
96 $t =~ s/âc/æ/g;
97 $t =~ s/éz/¾/g;
98 $t =~ s/és/¹/g;
99 $t =~ s/³/ð/g;
100 $t =~ s/éC/È/g;
101 $t =~ s/âC/Æ/g;
102 $t =~ s/éZ/®/g;
103 $t =~ s/éS/©/g;
104 $t =~ s/£/Ð/g;
105 warn "## $t\n" if $debug;
106 # $t = decode('iso-8859-2', $t);
107 # return encode('utf-8',$t);
108 return $t;
109 }
110
111 foreach my $i ( 1 .. $n ) {
112 my $marc = new_from_usmarc MARC::Record( $rs->record( $i - 1 )->raw() );
113
114 return_next({
115 title => e( $marc->title ),
116 author => e( $marc->author ),
117 edition => e( $marc->edition ),
118 date => strip_non_digit( $marc->publication_date ),
119 });
120 }
121
122 return undef;
123
124 $$;
125
126 -- if your terminal isn't iso-8859-2, change this!
127 -- set client_encoding = 'iso-8859-2';
128
129 -- example
130 -- select * from search('title:djece');
131 -- select * from search('osman');
132
133 --select * from search('nsk','title:mor');
134 --select * from search('nsk','grada');
135 --select * from search('nsk-en','restrictions');
136
137 select * from search('ucs','human');

  ViewVC Help
Powered by ViewVC 1.1.26