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