/[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 5 - (show annotations)
Thu Apr 26 20:43:00 2007 UTC (16 years, 11 months ago) by dpavlin
File size: 3093 byte(s)
added some sample 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 ( 'tera', 'z3950.inquirion.com', 210, 'Marc' );
15 insert into servers values ( 'cosmo', 'rlw.savba.sk', 8888, 'test_un_cat' );
16 insert into servers values ( 'copac', 'z3950.copac.ac.uk', 2020, 'xxdefault' );
17 --insert into servers values ( 'wcat', 'tikal.dev.oclc.org', 210, 'WorldCat' );
18 --insert into servers values ( '', '', , '' );
19
20 create type item as (
21 title text,
22 author text,
23 edition text,
24 date text
25 );
26
27 create or replace function search(text,text)
28 returns setof item
29 language plperlu
30 as $$
31
32 my $debug = 0;
33
34 my ( $server, $query ) = @_;
35
36 my $rv = spi_exec_query(qq{
37 select host,port,database from servers where name ilike '$server'
38 },1);
39
40 die "can't find database $database\n" unless ( $rv->{processed} == 1 );
41
42 my ( $host, $port, $database ) = (
43 $rv->{rows}[0]->{host},
44 $rv->{rows}[0]->{port},
45 $rv->{rows}[0]->{database},
46 );
47
48 use ZOOM;
49 use MARC::Record;
50 use Encode qw/encode decode/;
51
52 my $pqf = {
53 isbn => '@attr 1=7 @attr 4=1 "%s"',
54 title => '@attr 1=4 @attr 4=1 "%s"',
55 author => '@attr 1=1003 @attr 4=1 "%s"',
56 issn => '@attr 1=8 @attr 4=1 "%s"',
57 };
58
59 sub q2cqf {
60 my $q = shift;
61 if ($q =~ m/^(\w+):\s*(.*)$/) {
62 my ( $k,$v ) = ( $1,$2 );
63 return sprintf( $pqf->{ $k }, $v ) if ( defined( $pqf->{ $k } ) );
64 }
65 return $q;
66 }
67
68 my $conn = new ZOOM::Connection($host, $port,
69 databaseName => $database) or
70 die "can't connect to ${host}:${port}/${database}\n";
71
72 $conn->option(preferredRecordSyntax => "usmarc");
73
74 my $cqf = q2cqf( $query );
75
76 my $rs = $conn->search_pqf( $cqf );
77
78 my $n = $rs->size();
79 # fetch all results
80 $rs->records(0, $n - 1, 0);
81
82 warn "$n results for '$query' [$cqf]\n";
83
84 sub strip_non_digit {
85 my $d = shift;
86 $d =~ s/^\D+//;
87 $d =~ s/\D+$//;
88 return $d;
89 }
90
91 # fix encoding
92 sub e {
93 my $t = shift;
94 $t =~ s/éc/è/g;
95 $t =~ s/âc/æ/g;
96 $t =~ s/éz/¾/g;
97 $t =~ s/és/¹/g;
98 $t =~ s/³/ð/g;
99 $t =~ s/éC/È/g;
100 $t =~ s/âC/Æ/g;
101 $t =~ s/éZ/®/g;
102 $t =~ s/éS/©/g;
103 $t =~ s/£/Ð/g;
104 warn "## $t\n" if $debug;
105 # $t = decode('iso-8859-2', $t);
106 # return encode('utf-8',$t);
107 return $t;
108 }
109
110 foreach my $i ( 1 .. $n ) {
111 my $marc = new_from_usmarc MARC::Record( $rs->record( $i - 1 )->raw() );
112
113 return_next({
114 title => e( $marc->title ),
115 author => e( $marc->author ),
116 edition => e( $marc->edition ),
117 date => strip_non_digit( $marc->publication_date ),
118 });
119 }
120
121 return undef;
122
123 $$;
124
125 -- if your terminal isn't iso-8859-2, change this!
126 -- set client_encoding = 'iso-8859-2';
127
128 -- example
129 -- select * from search('title:djece');
130 -- select * from search('osman');
131
132 select * from search('NSK','title:mor');
133 select * from search('NSK','grada');
134 select * from search('nsk-en','restrictions');

  ViewVC Help
Powered by ViewVC 1.1.26