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 |
use Encode qw/encode decode/; |
32 |
|
33 |
my $pqf = { |
34 |
isbn => '@attr 1=7 @attr 4=1 "%s"', |
35 |
title => '@attr 1=4 @attr 4=1 "%s"', |
36 |
author => '@attr 1=1003 @attr 4=1 "%s"', |
37 |
issn => '@attr 1=8 @attr 4=1 "%s"', |
38 |
}; |
39 |
|
40 |
sub q2cqf { |
41 |
my $q = shift; |
42 |
if ($q =~ m/^(\w+):\s*(.*)$/) { |
43 |
my ( $k,$v ) = ( $1,$2 ); |
44 |
return sprintf( $pqf->{ $k }, $v ) if ( defined( $pqf->{ $k } ) ); |
45 |
} |
46 |
return $q; |
47 |
} |
48 |
|
49 |
my $conn = new ZOOM::Connection($host, $port, |
50 |
databaseName => $database) or |
51 |
die "can't connect to ${host}:${port}/${database}\n"; |
52 |
|
53 |
$conn->option(preferredRecordSyntax => "usmarc"); |
54 |
|
55 |
my $cqf = q2cqf( $query ); |
56 |
|
57 |
my $rs = $conn->search_pqf( $cqf ); |
58 |
|
59 |
my $n = $rs->size(); |
60 |
# fetch all results |
61 |
$rs->records(0, $n - 1, 0); |
62 |
|
63 |
warn "$n results for '$query' [$cqf]\n"; |
64 |
|
65 |
sub strip_non_digit { |
66 |
my $d = shift; |
67 |
$d =~ s/^\D+//; |
68 |
$d =~ s/\D+$//; |
69 |
return $d; |
70 |
} |
71 |
|
72 |
# fix encoding |
73 |
sub e { |
74 |
my $t = shift; |
75 |
$t =~ s/éc/è/g; |
76 |
$t =~ s/âc/æ/g; |
77 |
$t =~ s/és/¹/g; |
78 |
$t =~ s/éz/¾/g; |
79 |
$t =~ s/éC/È/g; |
80 |
$t =~ s/âC/Æ/g; |
81 |
$t =~ s/éS/©/g; |
82 |
$t =~ s/éZ/®/g; |
83 |
$t =~ s/£/Ð/g; |
84 |
warn "## $t\n"; |
85 |
# $t = decode('iso-8859-2', $t); |
86 |
# return encode('utf-8',$t); |
87 |
return $t; |
88 |
} |
89 |
|
90 |
foreach my $i ( 1 .. $n ) { |
91 |
my $marc = new_from_usmarc MARC::Record( $rs->record( $i - 1 )->raw() ); |
92 |
|
93 |
return_next({ |
94 |
title => e( $marc->title ), |
95 |
author => e( $marc->author ), |
96 |
edition => e( $marc->edition ), |
97 |
date => strip_non_digit( $marc->publication_date ), |
98 |
}); |
99 |
} |
100 |
|
101 |
return undef; |
102 |
|
103 |
$$; |
104 |
|
105 |
-- if your terminal isn't iso-8859-2, change this! |
106 |
-- set client_encoding = 'iso-8859-2'; |
107 |
|
108 |
-- example |
109 |
-- select * from search('title:djece'); |
110 |
-- select * from search('osman'); |
111 |
|
112 |
select * from search('title:mor'); |