source: gsdl/trunk/perllib/dbutil.pm@ 15725

Last change on this file since 15725 was 15725, checked in by mdewsnip, 16 years ago

(Adding new DB support) Added $infodb_type as first argument to all the dbutil functions.

File size: 6.3 KB
Line 
1###########################################################################
2#
3# dbutil.pm -- utility functions for writing to different databases
4# Copyright (C) 2008 DL Consulting Ltd
5#
6# A component of the Greenstone digital library software
7# from the New Zealand Digital Library Project at the
8# University of Waikato, New Zealand.
9#
10# This program is free software; you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation; either version 2 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program; if not, write to the Free Software
22# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23#
24###########################################################################
25
26package dbutil;
27
28use strict;
29
30
31sub get_infodb_file_path
32{
33 my $infodb_type = shift(@_);
34 my $collection_name = shift(@_);
35 my $infodb_directory_path = shift(@_);
36
37 if ($infodb_type eq "sqlite")
38 {
39 return &get_infodb_file_path_sqlite($collection_name, $infodb_directory_path);
40 }
41
42 # Use GDBM if the infodb type is empty or not one of the values above
43 return &get_infodb_file_path_gdbm($collection_name, $infodb_directory_path);
44}
45
46
47sub open_infodb_write_handle
48{
49 my $infodb_type = shift(@_);
50 my $infodb_file_path = shift(@_);
51
52 if ($infodb_type eq "sqlite")
53 {
54 return &open_infodb_write_handle_sqlite($infodb_file_path);
55 }
56
57 # Use GDBM if the infodb type is empty or not one of the values above
58 return &open_infodb_write_handle_gdbm($infodb_file_path);
59}
60
61
62sub read_infodb_file
63{
64 my $infodb_type = shift(@_);
65 my $infodb_file_path = shift(@_);
66 my $infodb_map = shift(@_);
67
68 &read_infodb_file_gdbm($infodb_file_path, $infodb_map);
69}
70
71
72sub write_infodb_entry
73{
74 my $infodb_type = shift(@_);
75 my $infodb_handle = shift(@_);
76 my $infodb_key = shift(@_);
77 my $infodb_map = shift(@_);
78
79 if ($infodb_type eq "sqlite")
80 {
81 return &write_infodb_entry_sqlite($infodb_handle, $infodb_key, $infodb_map);
82 }
83
84 # Use GDBM if the infodb type is empty or not one of the values above
85 return &write_infodb_entry_gdbm($infodb_handle, $infodb_key, $infodb_map);
86}
87
88
89
90# ----------------------------------------------------------------------------------------
91# GDBM FUNCTIONS
92# ----------------------------------------------------------------------------------------
93
94sub get_infodb_file_path_gdbm
95{
96 my $collection_name = shift(@_);
97 my $infodb_directory_path = shift(@_);
98
99 my $infodb_file_extension = (&util::is_little_endian() ? ".ldb" : ".bdb");
100 my $infodb_file_name = &util::get_dirsep_tail($collection_name) . $infodb_file_extension;
101 return &util::filename_cat($infodb_directory_path, $infodb_file_name);
102}
103
104
105sub open_infodb_write_handle_gdbm
106{
107 my $infodb_file_path = shift(@_);
108
109 my $txt2db_exe = &util::filename_cat("$ENV{'GSDLHOME'}/bin/$ENV{'GSDLOS'}", "txt2db" . &util::get_os_exe());
110 my $infodb_file_handle = undef;
111 if (!-e "$txt2db_exe" || !open($infodb_file_handle, "| $txt2db_exe \"$infodb_file_path\""))
112 {
113 return undef;
114 }
115
116 return $infodb_file_handle;
117}
118
119
120sub read_infodb_file_gdbm
121{
122 my $infodb_file_path = shift(@_);
123 my $infodb_map = shift(@_);
124
125 open (PIPEIN, "db2txt \"$infodb_file_path\" |") || die "couldn't open pipe from db2txt\n";
126 my $infodb_line = "";
127 my $infodb_key = "";
128 my $infodb_value = "";
129 while (defined ($infodb_line = <PIPEIN>))
130 {
131 if ($infodb_line =~ /^\[([^\]]+)\]$/)
132 {
133 $infodb_key = $1;
134 }
135 elsif ($infodb_line =~ /^-{70}$/)
136 {
137 $infodb_map->{$infodb_key} = $infodb_value;
138 $infodb_key = "";
139 $infodb_value = "";
140 }
141 else
142 {
143 $infodb_value .= $infodb_line;
144 }
145 }
146
147 close (PIPEIN);
148}
149
150
151sub write_infodb_entry_gdbm
152{
153 my $infodb_handle = shift(@_);
154 my $infodb_key = shift(@_);
155 my $infodb_map = shift(@_);
156
157 print $infodb_handle "[$infodb_key]\n";
158 foreach my $infodb_value_key (keys(%$infodb_map))
159 {
160 foreach my $infodb_value (@{$infodb_map->{$infodb_value_key}})
161 {
162 if ($infodb_value =~ /-{70,}/)
163 {
164 # if value contains 70 or more hyphens in a row we need to escape them
165 # to prevent txt2db from treating them as a separator
166 $infodb_value =~ s/-/&\#045;/gi;
167 }
168 print $infodb_handle "<$infodb_value_key>" . $infodb_value . "\n";
169 }
170 }
171 print $infodb_handle '-' x 70, "\n";
172}
173
174
175
176# ----------------------------------------------------------------------------------------
177# SQLITE FUNCTIONS
178# ----------------------------------------------------------------------------------------
179
180sub get_infodb_file_path_sqlite
181{
182 my $collection_name = shift(@_);
183 my $infodb_directory_path = shift(@_);
184
185 my $infodb_file_extension = ".db";
186 my $infodb_file_name = &util::get_dirsep_tail($collection_name) . $infodb_file_extension;
187 return &util::filename_cat($infodb_directory_path, $infodb_file_name);
188}
189
190
191sub open_infodb_write_handle_sqlite
192{
193 my $infodb_file_path = shift(@_);
194
195 my $sqlite3_exe = &util::filename_cat("$ENV{'GSDLHOME'}/bin/$ENV{'GSDLOS'}", "sqlite3" . &util::get_os_exe());
196 my $infodb_file_handle = undef;
197 if (!-e "$sqlite3_exe" || !open($infodb_file_handle, "| $sqlite3_exe \"$infodb_file_path\""))
198 {
199 return undef;
200 }
201
202 print $infodb_file_handle "CREATE TABLE data (key TEXT, value TEXT, PRIMARY KEY(key));\n";
203
204 return $infodb_file_handle;
205}
206
207
208sub write_infodb_entry_sqlite
209{
210 my $infodb_handle = shift(@_);
211 my $infodb_key = shift(@_);
212 my $infodb_map = shift(@_);
213
214 my $infodb_entry_value = "";
215 foreach my $infodb_value_key (keys(%$infodb_map))
216 {
217 foreach my $infodb_value (@{$infodb_map->{$infodb_value_key}})
218 {
219 $infodb_entry_value .= "<$infodb_value_key>" . $infodb_value . "\n";
220 }
221 }
222
223 # Escape any single quotes in the value
224 $infodb_entry_value =~ s/\'/\'\'/g;
225
226 print $infodb_handle "DELETE FROM data WHERE key='" . $infodb_key . "';\n";
227 print $infodb_handle "INSERT INTO data (key, value) VALUES ('" . $infodb_key . "', '" . $infodb_entry_value . "');\n";
228}
229
230
2311;
Note: See TracBrowser for help on using the repository browser.