[15699] | 1 | ###########################################################################
|
---|
| 2 | #
|
---|
[21411] | 3 | # dbutil.pm -- gateway to utilities for reading/writing to different databases
|
---|
[15699] | 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 |
|
---|
| 26 | package dbutil;
|
---|
| 27 |
|
---|
| 28 | use strict;
|
---|
| 29 |
|
---|
| 30 |
|
---|
[17087] | 31 | sub open_infodb_write_handle
|
---|
| 32 | {
|
---|
[17476] | 33 | my $infodb_type = shift(@_);
|
---|
| 34 | my $infodb_file_path = shift(@_);
|
---|
| 35 |
|
---|
| 36 | if ($infodb_type eq "sqlite")
|
---|
| 37 | {
|
---|
[21544] | 38 | require dbutil::sqlite;
|
---|
[21546] | 39 | return &dbutil::sqlite::open_infodb_write_handle($infodb_file_path, @_);
|
---|
[17476] | 40 | }
|
---|
| 41 | elsif ($infodb_type eq "gdbm-txtgz")
|
---|
| 42 | {
|
---|
[21544] | 43 | require dbutil::gdbmtxtgz;
|
---|
[21546] | 44 | return &dbutil::gdbmtxtgz::open_infodb_write_handle($infodb_file_path, @_);
|
---|
[17476] | 45 | }
|
---|
[21411] | 46 | elsif ($infodb_type eq "jdbm")
|
---|
| 47 | {
|
---|
[21544] | 48 | require dbutil::jdbm;
|
---|
[21546] | 49 | return &dbutil::jdbm::open_infodb_write_handle($infodb_file_path, @_);
|
---|
[21411] | 50 | }
|
---|
[17476] | 51 | elsif ($infodb_type eq "mssql")
|
---|
| 52 | {
|
---|
[21544] | 53 | require dbutil::mssql;
|
---|
[21546] | 54 | return &dbutil::mssql::open_infodb_write_handle($infodb_file_path, @_);
|
---|
[17476] | 55 | }
|
---|
[17087] | 56 |
|
---|
[17476] | 57 | # Use GDBM if the infodb type is empty or not one of the values above
|
---|
[21544] | 58 | require dbutil::gdbm;
|
---|
[21546] | 59 | return &dbutil::gdbm::open_infodb_write_handle($infodb_file_path, @_);
|
---|
[17087] | 60 | }
|
---|
| 61 |
|
---|
| 62 |
|
---|
[16176] | 63 | sub close_infodb_write_handle
|
---|
| 64 | {
|
---|
[17476] | 65 | my $infodb_type = shift(@_);
|
---|
| 66 | my $infodb_handle = shift(@_);
|
---|
[16176] | 67 |
|
---|
[17476] | 68 | if ($infodb_type eq "sqlite")
|
---|
| 69 | {
|
---|
[21544] | 70 | require dbutil::sqlite;
|
---|
[21546] | 71 | return &dbutil::sqlite::close_infodb_write_handle($infodb_handle, @_);
|
---|
[17476] | 72 | }
|
---|
| 73 | elsif ($infodb_type eq "gdbm-txtgz")
|
---|
| 74 | {
|
---|
[21544] | 75 | require dbutil::gdbmtxtgz;
|
---|
[21546] | 76 | return &dbutil::gdbmtxtgz::close_infodb_write_handle($infodb_handle, @_);
|
---|
[17476] | 77 | }
|
---|
[21411] | 78 | elsif ($infodb_type eq "jdbm")
|
---|
| 79 | {
|
---|
[21544] | 80 | require dbutil::jdbm;
|
---|
[21546] | 81 | return &dbutil::jdbm::close_infodb_write_handle($infodb_handle, @_);
|
---|
[21411] | 82 | }
|
---|
[17476] | 83 | elsif ($infodb_type eq "mssql")
|
---|
| 84 | {
|
---|
[21544] | 85 | require dbutil::mssql;
|
---|
[21546] | 86 | return &dbutil::mssql::close_infodb_write_handle($infodb_handle, @_);
|
---|
[17476] | 87 | }
|
---|
| 88 |
|
---|
| 89 | # Use GDBM if the infodb type is empty or not one of the values above
|
---|
[21544] | 90 | require dbutil::gdbm;
|
---|
[21546] | 91 | return &dbutil::gdbm::close_infodb_write_handle($infodb_handle, @_);
|
---|
[17104] | 92 | }
|
---|
[16176] | 93 |
|
---|
| 94 |
|
---|
[15727] | 95 | sub get_default_infodb_type
|
---|
| 96 | {
|
---|
[17476] | 97 | # The default is GDBM so everything works the same for existing collections
|
---|
| 98 | # To use something else, specify the "infodbtype" in the collection's collect.cfg file
|
---|
| 99 | return "gdbm";
|
---|
[15727] | 100 | }
|
---|
| 101 |
|
---|
| 102 |
|
---|
[15710] | 103 | sub get_infodb_file_path
|
---|
| 104 | {
|
---|
[17476] | 105 | my $infodb_type = shift(@_);
|
---|
| 106 | my $collection_name = shift(@_);
|
---|
| 107 | my $infodb_directory_path = shift(@_);
|
---|
[15710] | 108 |
|
---|
[17476] | 109 | if ($infodb_type eq "sqlite")
|
---|
| 110 | {
|
---|
[21544] | 111 | require dbutil::sqlite;
|
---|
[21546] | 112 | return &dbutil::sqlite::get_infodb_file_path($collection_name, $infodb_directory_path, @_);
|
---|
[17476] | 113 | }
|
---|
| 114 | elsif ($infodb_type eq "gdbm-txtgz")
|
---|
| 115 | {
|
---|
[21544] | 116 | require dbutil::gdbmtxtgz;
|
---|
[21546] | 117 | return &dbutil::gdbmtxtgz::get_infodb_file_path($collection_name, $infodb_directory_path, @_);
|
---|
[17476] | 118 | }
|
---|
[21411] | 119 | elsif ($infodb_type eq "jdbm")
|
---|
| 120 | {
|
---|
[21544] | 121 | require dbutil::jdbm;
|
---|
[21546] | 122 | return &dbutil::jdbm::get_infodb_file_path($collection_name, $infodb_directory_path, @_);
|
---|
[21411] | 123 | }
|
---|
[17476] | 124 | elsif ($infodb_type eq "mssql")
|
---|
| 125 | {
|
---|
| 126 | #==================================================================================================#
|
---|
| 127 | # Updated by Jeffrey (2008/08/25 Monday)
|
---|
| 128 | # After look into the run-time code, it seems we should still create a database file.
|
---|
| 129 | # Since the run-time code is always try to read a database file, the easiest way here is not
|
---|
| 130 | # to change the whole structure, but to give whatever the system is looking for.
|
---|
| 131 | #==================================================================================================#
|
---|
| 132 | # Added by Jeffrey (2008/08/15 Friday)
|
---|
| 133 | # No file path required for MS SQL, it is a server-client connection.
|
---|
[21411] | 134 | # At the moment the information is hard coded in dbutil::mssql::open_infodb_write_handle
|
---|
[17476] | 135 | # the this might need some tidy up sometime.
|
---|
| 136 | #==================================================================================================#
|
---|
[21544] | 137 | require dbutil::mssql;
|
---|
[21546] | 138 | return &dbutil::mssql::get_infodb_file_path($collection_name, $infodb_directory_path, @_);
|
---|
[17476] | 139 | }
|
---|
| 140 |
|
---|
| 141 | # Use GDBM if the infodb type is empty or not one of the values above
|
---|
[21544] | 142 | require dbutil::gdbm;
|
---|
[21546] | 143 | return &dbutil::gdbm::get_infodb_file_path($collection_name, $infodb_directory_path, @_);
|
---|
[15710] | 144 | }
|
---|
| 145 |
|
---|
| 146 |
|
---|
[15705] | 147 | sub read_infodb_file
|
---|
| 148 | {
|
---|
[17476] | 149 | my $infodb_type = shift(@_);
|
---|
| 150 | my $infodb_file_path = shift(@_);
|
---|
| 151 | my $infodb_map = shift(@_);
|
---|
[15705] | 152 |
|
---|
[17476] | 153 | if ($infodb_type eq "sqlite")
|
---|
| 154 | {
|
---|
[21544] | 155 | require dbutil::sqlite;
|
---|
[21546] | 156 | return &dbutil::sqlite::read_infodb_file($infodb_file_path, $infodb_map, @_);
|
---|
[17476] | 157 | }
|
---|
| 158 | elsif ($infodb_type eq "gdbm-txtgz")
|
---|
| 159 | {
|
---|
[21544] | 160 | require dbutil::gdbmtxtgz;
|
---|
[21546] | 161 | return &dbutil::gdbmtxtgz::read_infodb_file($infodb_file_path, $infodb_map, @_);
|
---|
[17476] | 162 | }
|
---|
[21411] | 163 | elsif ($infodb_type eq "jdbm")
|
---|
| 164 | {
|
---|
[21544] | 165 | require dbutil::jdbm;
|
---|
[21546] | 166 | return &dbutil::jdbm::read_infodb_file($infodb_file_path, $infodb_map, @_);
|
---|
[21411] | 167 | }
|
---|
[17476] | 168 | elsif ($infodb_type eq "mssql")
|
---|
| 169 | {
|
---|
[21544] | 170 | require dbutil::mssql;
|
---|
[21546] | 171 | return &dbutil::mssql::read_infodb_file($infodb_file_path, $infodb_map, @_);
|
---|
[17476] | 172 | }
|
---|
| 173 |
|
---|
| 174 | # Use GDBM if the infodb type is empty or not one of the values above
|
---|
[21544] | 175 | require dbutil::gdbm;
|
---|
[21546] | 176 | return &dbutil::gdbm::read_infodb_file($infodb_file_path, $infodb_map, @_);
|
---|
[15705] | 177 | }
|
---|
| 178 |
|
---|
| 179 |
|
---|
[21547] | 180 | sub read_infodb_entry
|
---|
| 181 | {
|
---|
| 182 | my $infodb_type = shift(@_);
|
---|
| 183 | my $infodb_file_path = shift(@_);
|
---|
| 184 | my $infodb_key = shift(@_);
|
---|
| 185 |
|
---|
| 186 | # !! TEMPORARY: Slow and naive implementation that just reads the entire file and picks out the one value
|
---|
| 187 | # !! This will soon be replaced with database-specific versions that will use dbget etc.
|
---|
| 188 | my $infodb_map = {};
|
---|
| 189 | &read_infodb_file($infodb_type, $infodb_file_path, $infodb_map);
|
---|
| 190 | return $infodb_map->{$infodb_key};
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 |
|
---|
[15699] | 194 | sub write_infodb_entry
|
---|
| 195 | {
|
---|
[17476] | 196 | my $infodb_type = shift(@_);
|
---|
| 197 | my $infodb_handle = shift(@_);
|
---|
| 198 | my $infodb_key = shift(@_);
|
---|
| 199 | my $infodb_map = shift(@_);
|
---|
[15699] | 200 |
|
---|
[17476] | 201 | if ($infodb_type eq "sqlite")
|
---|
| 202 | {
|
---|
[21544] | 203 | require dbutil::sqlite;
|
---|
[21546] | 204 | return &dbutil::sqlite::write_infodb_entry($infodb_handle, $infodb_key, $infodb_map, @_);
|
---|
[17476] | 205 | }
|
---|
| 206 | elsif ($infodb_type eq "gdbm-txtgz")
|
---|
| 207 | {
|
---|
[21544] | 208 | require dbutil::gdbmtxtgz;
|
---|
[21546] | 209 | return &dbutil::gdbmtxtgz::write_infodb_entry($infodb_handle, $infodb_key, $infodb_map, @_);
|
---|
[17476] | 210 | }
|
---|
[21411] | 211 | elsif ($infodb_type eq "jdbm")
|
---|
| 212 | {
|
---|
[21544] | 213 | require dbutil::jdbm;
|
---|
[21546] | 214 | return &dbutil::jdbm::write_infodb_entry($infodb_handle, $infodb_key, $infodb_map, @_);
|
---|
[21411] | 215 | }
|
---|
[17476] | 216 | elsif ($infodb_type eq "mssql")
|
---|
| 217 | {
|
---|
[21544] | 218 | require dbutil::mssql;
|
---|
[21546] | 219 | return &dbutil::mssql::write_infodb_entry($infodb_handle, $infodb_key, $infodb_map, @_);
|
---|
[17476] | 220 | }
|
---|
[15725] | 221 |
|
---|
[17476] | 222 | # Use GDBM if the infodb type is empty or not one of the values above
|
---|
[21544] | 223 | require dbutil::gdbm;
|
---|
[21546] | 224 | return &dbutil::gdbm::write_infodb_entry($infodb_handle, $infodb_key, $infodb_map, @_);
|
---|
[15699] | 225 | }
|
---|
| 226 |
|
---|
[15705] | 227 |
|
---|
[18467] | 228 | sub delete_infodb_entry
|
---|
| 229 | {
|
---|
| 230 | my $infodb_type = shift(@_);
|
---|
| 231 | my $infodb_handle = shift(@_);
|
---|
| 232 | my $infodb_key = shift(@_);
|
---|
[15705] | 233 |
|
---|
[18467] | 234 | if ($infodb_type eq "sqlite")
|
---|
| 235 | {
|
---|
[21544] | 236 | require dbutil::sqlite;
|
---|
[21546] | 237 | return &dbutil::sqlite::delete_infodb_entry($infodb_handle, $infodb_key, @_);
|
---|
[18467] | 238 | }
|
---|
| 239 | elsif ($infodb_type eq "gdbm-txtgz")
|
---|
| 240 | {
|
---|
[21544] | 241 | require dbutil::gdbmtxtgz;
|
---|
[21546] | 242 | return &dbutil::gdbmtxtgz::delete_infodb_entry($infodb_handle, $infodb_key, @_);
|
---|
[18467] | 243 | }
|
---|
[21411] | 244 | elsif ($infodb_type eq "jdbm")
|
---|
| 245 | {
|
---|
[21544] | 246 | require dbutil::jdbm;
|
---|
[21546] | 247 | return &dbutil::jdbm::delete_infodb_entry($infodb_handle, $infodb_key, @_);
|
---|
[21411] | 248 | }
|
---|
[18467] | 249 | elsif ($infodb_type eq "mssql")
|
---|
| 250 | {
|
---|
[21544] | 251 | require dbutil::mssql;
|
---|
[21546] | 252 | return &dbutil::mssql::delete_infodb_entry($infodb_handle, $infodb_key, @_);
|
---|
[18467] | 253 | }
|
---|
| 254 |
|
---|
| 255 | # Use GDBM if the infodb type is empty or not one of the values above
|
---|
[21544] | 256 | require dbutil::gdbm;
|
---|
[21546] | 257 | return &dbutil::gdbm::delete_infodb_entry($infodb_handle, $infodb_key, @_);
|
---|
[18467] | 258 | }
|
---|
| 259 |
|
---|
| 260 |
|
---|
[21550] | 261 | # ---- GENERAL FUNCTIONS --------
|
---|
| 262 |
|
---|
| 263 | sub convert_infodb_hash_to_string
|
---|
| 264 | {
|
---|
| 265 | my $infodb_map = shift(@_);
|
---|
| 266 |
|
---|
| 267 | my $infodb_entry_value = "";
|
---|
| 268 | foreach my $infodb_value_key (keys(%$infodb_map))
|
---|
| 269 | {
|
---|
| 270 | foreach my $infodb_value (@{$infodb_map->{$infodb_value_key}})
|
---|
| 271 | {
|
---|
| 272 | $infodb_entry_value .= "<$infodb_value_key>" . $infodb_value . "\n";
|
---|
| 273 | }
|
---|
| 274 | }
|
---|
| 275 |
|
---|
| 276 | return $infodb_entry_value;
|
---|
| 277 | }
|
---|
| 278 |
|
---|
| 279 |
|
---|
| 280 | sub convert_infodb_string_to_hash
|
---|
| 281 | {
|
---|
| 282 | my $infodb_entry_value = shift(@_);
|
---|
| 283 |
|
---|
| 284 | my $infodb_map = ();
|
---|
| 285 | while ($infodb_entry_value =~ /^<(.*?)>(.*)$/mg)
|
---|
| 286 | {
|
---|
| 287 | my $infodb_value_key = $1;
|
---|
| 288 | my $infodb_value = $2;
|
---|
| 289 |
|
---|
| 290 | if (!defined($infodb_map->{$infodb_value_key}))
|
---|
| 291 | {
|
---|
| 292 | $infodb_map->{$infodb_value_key} = [ $infodb_value ];
|
---|
| 293 | }
|
---|
| 294 | else
|
---|
| 295 | {
|
---|
| 296 | push(@{$infodb_map->{$infodb_value_key}}, $infodb_value);
|
---|
| 297 | }
|
---|
| 298 | }
|
---|
| 299 |
|
---|
| 300 | return $infodb_map;
|
---|
| 301 | }
|
---|
| 302 |
|
---|
| 303 |
|
---|
[15699] | 304 | 1;
|
---|