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

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

(Adding new DB support) Added sqlite versions of the three important infodb functions -- so you now can build an sqlite database automatically!

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