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 |
my $max_res = 100; |
34 |
|
35 |
my ( $server, $query ) = @_; |
36 |
|
37 |
my $rv = spi_exec_query(qq{ |
38 |
select host,port,database from servers where name ilike '$server' |
39 |
},1); |
40 |
|
41 |
die "can't find database $database\n" unless ( $rv->{processed} == 1 ); |
42 |
|
43 |
my ( $host, $port, $database ) = ( |
44 |
$rv->{rows}[0]->{host}, |
45 |
$rv->{rows}[0]->{port}, |
46 |
$rv->{rows}[0]->{database}, |
47 |
); |
48 |
|
49 |
use ZOOM; |
50 |
use MARC::Record; |
51 |
use Encode qw/encode decode/; |
52 |
|
53 |
my $pqf = { |
54 |
isbn => '@attr 1=7 @attr 4=1 "%s"', |
55 |
title => '@attr 1=4 @attr 4=1 "%s"', |
56 |
author => '@attr 1=1003 @attr 4=1 "%s"', |
57 |
issn => '@attr 1=8 @attr 4=1 "%s"', |
58 |
}; |
59 |
|
60 |
sub q2cqf { |
61 |
my $q = shift; |
62 |
if ($q =~ m/^(\w+):\s*(.*)$/) { |
63 |
my ( $k,$v ) = ( $1,$2 ); |
64 |
return sprintf( $pqf->{ $k }, $v ) if ( defined( $pqf->{ $k } ) ); |
65 |
} |
66 |
return $q; |
67 |
} |
68 |
|
69 |
my $conn = new ZOOM::Connection($host, $port, |
70 |
databaseName => $database) or |
71 |
die "can't connect to ${host}:${port}/${database}\n"; |
72 |
|
73 |
$conn->option(preferredRecordSyntax => "usmarc"); |
74 |
|
75 |
my $cqf = q2cqf( $query ); |
76 |
|
77 |
my $rs = $conn->search_pqf( $cqf ); |
78 |
|
79 |
my $n = $rs->size(); |
80 |
if ( $n > $max_res ) { |
81 |
warn "query returned $n results, fetching first $max_res\n"; |
82 |
$n = $max_res; |
83 |
} |
84 |
# fetch all results |
85 |
$rs->records(0, $n - 1, 0); |
86 |
|
87 |
warn "$n results for '$query' [$cqf]\n"; |
88 |
|
89 |
sub strip_non_digit { |
90 |
my $d = shift; |
91 |
$d =~ s/^\D+//; |
92 |
$d =~ s/\D+$//; |
93 |
return $d; |
94 |
} |
95 |
|
96 |
# fix encoding |
97 |
sub e { |
98 |
my $t = shift; |
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 |
$t =~ s/éC/È/g; |
105 |
$t =~ s/âC/Æ/g; |
106 |
$t =~ s/éZ/®/g; |
107 |
$t =~ s/éS/©/g; |
108 |
$t =~ s/£/Ð/g; |
109 |
warn "## $t\n" if $debug; |
110 |
# $t = decode('iso-8859-2', $t); |
111 |
# return encode('utf-8',$t); |
112 |
return $t; |
113 |
} |
114 |
|
115 |
foreach my $i ( 1 .. $n ) { |
116 |
my $marc = new_from_usmarc MARC::Record( $rs->record( $i - 1 )->raw() ); |
117 |
|
118 |
return_next({ |
119 |
title => e( $marc->title ), |
120 |
author => e( $marc->author ), |
121 |
edition => e( $marc->edition ), |
122 |
date => strip_non_digit( $marc->publication_date ), |
123 |
}); |
124 |
} |
125 |
|
126 |
return undef; |
127 |
|
128 |
$$; |
129 |
|
130 |
-- if your terminal isn't iso-8859-2, change this! |
131 |
-- set client_encoding = 'iso-8859-2'; |
132 |
|
133 |
-- example |
134 |
-- select * from search('title:djece'); |
135 |
-- select * from search('osman'); |
136 |
|
137 |
select * from search('NSK','title:mor'); |
138 |
select * from search('NSK','grada'); |
139 |
select * from search('nsk-en','restrictions'); |