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

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

(Adding new DB support) Added a dbutil::get_default_infodb_type() function, and called it from basebuilder if no 'infodbtype' value is specified in the collect.cfg file.

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_default_infodb_type
32{
33 return "gdbm";
34}
35
36
37sub get_infodb_file_path
38{
39 my $infodb_type = shift(@_);
40 my $collection_name = shift(@_);
41 my $infodb_directory_path = shift(@_);
42
43 if ($infodb_type eq "sqlite")
44 {
45 return &get_infodb_file_path_sqlite($collection_name, $infodb_directory_path);
46 }
47
48 # Use GDBM if the infodb type is empty or not one of the values above
49 return &get_infodb_file_path_gdbm($collection_name, $infodb_directory_path);
50}
51
52
53sub open_infodb_write_handle
54{
55 my $infodb_type = shift(@_);
56 my $infodb_file_path = shift(@_);
57
58 if ($infodb_type eq "sqlite")
59 {
60 return &open_infodb_write_handle_sqlite($infodb_file_path);
61 }
62
63 # Use GDBM if the infodb type is empty or not one of the values above
64 return &open_infodb_write_handle_gdbm($infodb_file_path);
65}
66
67
68sub read_infodb_file
69{
70 my $infodb_type = shift(@_);
71 my $infodb_file_path = shift(@_);
72 my $infodb_map = shift(@_);
73
74 &read_infodb_file_gdbm($infodb_file_path, $infodb_map);
75}
76
77
78sub write_infodb_entry
79{
80 my $infodb_type = shift(@_);
81 my $infodb_handle = shift(@_);
82 my $infodb_key = shift(@_);
83 my $infodb_map = shift(@_);
84
85 if ($infodb_type eq "sqlite")
86 {
87 return &write_infodb_entry_sqlite($infodb_handle, $infodb_key, $infodb_map);
88 }
89
90 # Use GDBM if the infodb type is empty or not one of the values above
91 return &write_infodb_entry_gdbm($infodb_handle, $infodb_key, $infodb_map);
92}
93
94
95
96# ----------------------------------------------------------------------------------------
97# GDBM FUNCTIONS
98# ----------------------------------------------------------------------------------------
99
100sub get_infodb_file_path_gdbm
101{
102 my $collection_name = shift(@_);
103 my $infodb_directory_path = shift(@_);
104
105 my $infodb_file_extension = (&util::is_little_endian() ? ".ldb" : ".bdb");
106 my $infodb_file_name = &util::get_dirsep_tail($collection_name) . $infodb_file_extension;
107 return &util::filename_cat($infodb_directory_path, $infodb_file_name);
108}
109
110
111sub open_infodb_write_handle_gdbm
112{
113 my $infodb_file_path = shift(@_);
114
115 my $txt2db_exe = &util::filename_cat("$ENV{'GSDLHOME'}/bin/$ENV{'GSDLOS'}", "txt2db" . &util::get_os_exe());
116 my $infodb_file_handle = undef;
117 if (!-e "$txt2db_exe" || !open($infodb_file_handle, "| $txt2db_exe \"$infodb_file_path\""))
118 {
119 return undef;
120 }
121
122 return $infodb_file_handle;
123}
124
125
126sub read_infodb_file_gdbm
127{
128 my $infodb_file_path = shift(@_);
129 my $infodb_map = shift(@_);
130
131 open (PIPEIN, "db2txt \"$infodb_file_path\" |") || die "couldn't open pipe from db2txt\n";
132 my $infodb_line = "";
133 my $infodb_key = "";
134 my $infodb_value = "";
135 while (defined ($infodb_line = <PIPEIN>))
136 {
137 if ($infodb_line =~ /^\[([^\]]+)\]$/)
138 {
139 $infodb_key = $1;
140 }
141 elsif ($infodb_line =~ /^-{70}$/)
142 {
143 $infodb_map->{$infodb_key} = $infodb_value;
144 $infodb_key = "";
145 $infodb_value = "";
146 }
147 else
148 {
149 $infodb_value .= $infodb_line;
150 }
151 }
152
153 close (PIPEIN);
154}
155
156
157sub write_infodb_entry_gdbm
158{
159 my $infodb_handle = shift(@_);
160 my $infodb_key = shift(@_);
161 my $infodb_map = shift(@_);
162
163 print $infodb_handle "[$infodb_key]\n";
164 foreach my $infodb_value_key (keys(%$infodb_map))
165 {
166 foreach my $infodb_value (@{$infodb_map->{$infodb_value_key}})
167 {
168 if ($infodb_value =~ /-{70,}/)
169 {
170 # if value contains 70 or more hyphens in a row we need to escape them
171 # to prevent txt2db from treating them as a separator
172 $infodb_value =~ s/-/&\#045;/gi;
173 }
174 print $infodb_handle "<$infodb_value_key>" . $infodb_value . "\n";
175 }
176 }
177 print $infodb_handle '-' x 70, "\n";
178}
179
180
181
182# ----------------------------------------------------------------------------------------
183# SQLITE FUNCTIONS
184# ----------------------------------------------------------------------------------------
185
186sub get_infodb_file_path_sqlite
187{
188 my $collection_name = shift(@_);
189 my $infodb_directory_path = shift(@_);
190
191 my $infodb_file_extension = ".db";
192 my $infodb_file_name = &util::get_dirsep_tail($collection_name) . $infodb_file_extension;
193 return &util::filename_cat($infodb_directory_path, $infodb_file_name);
194}
195
196
197sub open_infodb_write_handle_sqlite
198{
199 my $infodb_file_path = shift(@_);
200
201 my $sqlite3_exe = &util::filename_cat("$ENV{'GSDLHOME'}/bin/$ENV{'GSDLOS'}", "sqlite3" . &util::get_os_exe());
202 my $infodb_file_handle = undef;
203 if (!-e "$sqlite3_exe" || !open($infodb_file_handle, "| $sqlite3_exe \"$infodb_file_path\""))
204 {
205 return undef;
206 }
207
208 print $infodb_file_handle "CREATE TABLE data (key TEXT, value TEXT, PRIMARY KEY(key));\n";
209
210 return $infodb_file_handle;
211}
212
213
214sub write_infodb_entry_sqlite
215{
216 my $infodb_handle = shift(@_);
217 my $infodb_key = shift(@_);
218 my $infodb_map = shift(@_);
219
220 my $infodb_entry_value = "";
221 foreach my $infodb_value_key (keys(%$infodb_map))
222 {
223 foreach my $infodb_value (@{$infodb_map->{$infodb_value_key}})
224 {
225 $infodb_entry_value .= "<$infodb_value_key>" . $infodb_value . "\n";
226 }
227 }
228
229 # Escape any single quotes in the value
230 $infodb_entry_value =~ s/\'/\'\'/g;
231
232 print $infodb_handle "DELETE FROM data WHERE key='" . $infodb_key . "';\n";
233 print $infodb_handle "INSERT INTO data (key, value) VALUES ('" . $infodb_key . "', '" . $infodb_entry_value . "');\n";
234}
235
236
2371;
Note: See TracBrowser for help on using the repository browser.