1 |
dpavlin |
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 |
|
|
|
12 |
|
|
create type item as ( |
13 |
|
|
title text, |
14 |
|
|
author text, |
15 |
|
|
edition text, |
16 |
|
|
date text |
17 |
|
|
); |
18 |
|
|
|
19 |
|
|
create or replace function search(text) |
20 |
|
|
returns setof item |
21 |
|
|
language plperlu |
22 |
|
|
as $$ |
23 |
|
|
|
24 |
|
|
my ( $query ) = @_; |
25 |
|
|
|
26 |
|
|
my ( $host, $port, $database ) = |
27 |
|
|
( '161.53.240.27', 8090, 'voyager' ); |
28 |
|
|
|
29 |
|
|
use ZOOM; |
30 |
|
|
use MARC::Record; |
31 |
|
|
|
32 |
|
|
my $pqf = { |
33 |
|
|
isbn => '@attr 1=7 @attr 4=1 "%s"', |
34 |
|
|
title => '@attr 1=4 @attr 4=1 "%s"', |
35 |
|
|
author => '@attr 1=1003 @attr 4=1 "%s"', |
36 |
|
|
issn => '@attr 1=8 @attr 4=1 "%s"', |
37 |
|
|
}; |
38 |
|
|
|
39 |
|
|
sub q2cqf { |
40 |
|
|
my $q = shift; |
41 |
|
|
if ($q =~ m/^(\w+):\s*(.*)$/) { |
42 |
|
|
my ( $k,$v ) = ( $1,$2 ); |
43 |
|
|
return sprintf( $pqf->{ $k }, $v ) if ( defined( $pqf->{ $k } ) ); |
44 |
|
|
} |
45 |
|
|
return $q; |
46 |
|
|
} |
47 |
|
|
|
48 |
|
|
my $conn = new ZOOM::Connection($host, $port, |
49 |
|
|
databaseName => $database) or |
50 |
|
|
die "can't connect to ${host}:${port}/${database}\n"; |
51 |
|
|
|
52 |
|
|
$conn->option(preferredRecordSyntax => "usmarc"); |
53 |
|
|
|
54 |
|
|
my $rs = $conn->search_pqf( q2cqf( $query ) ); |
55 |
|
|
|
56 |
|
|
my $n = $rs->size(); |
57 |
|
|
# fetch all results |
58 |
|
|
$rs->records(0, $n - 1, 0); |
59 |
|
|
|
60 |
|
|
warn "found $n results for $query"; |
61 |
|
|
|
62 |
|
|
foreach my $i ( 1 .. $n ) { |
63 |
|
|
my $marc = new_from_usmarc MARC::Record( $rs->record( $i - 1 )->raw() ); |
64 |
|
|
|
65 |
|
|
return_next({ |
66 |
|
|
title => $marc->title, |
67 |
|
|
author => $marc->author, |
68 |
|
|
edition => $marc->edition, |
69 |
|
|
date => $marc->publication_date, |
70 |
|
|
}); |
71 |
|
|
} |
72 |
|
|
|
73 |
|
|
return undef; |
74 |
|
|
|
75 |
|
|
$$; |
76 |
|
|
|
77 |
|
|
-- if your terminal isn't iso-8859-2, change this! |
78 |
|
|
set client_encoding = 'iso-8859-2'; |
79 |
|
|
|