[30318] | 1 | ###############################################################################
|
---|
| 2 | #
|
---|
| 3 | # BaseDBDriver.pm -- base class for all the database drivers
|
---|
| 4 | # A component of the Greenstone digital library software from the New Zealand
|
---|
| 5 | # Digital Library Project at the University of Waikato, New Zealand.
|
---|
| 6 | #
|
---|
| 7 | # Copyright (C) 1999-2015 New Zealand Digital Library Project
|
---|
| 8 | #
|
---|
| 9 | # This program is free software; you can redistribute it and/or modify it under
|
---|
| 10 | # the terms of the GNU General Public License as published by the Free Software
|
---|
| 11 | # Foundation; either version 2 of the License, or (at your option) any later
|
---|
| 12 | # version.
|
---|
| 13 | #
|
---|
| 14 | # This program is distributed in the hope that it will be useful, but WITHOUT
|
---|
| 15 | # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
---|
| 16 | # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
---|
| 17 | # more details.
|
---|
| 18 | #
|
---|
| 19 | # You should have received a copy of the GNU General Public License along with
|
---|
| 20 | # this program; if not, write to the Free Software Foundation, Inc., 675 Mass
|
---|
| 21 | # Ave, Cambridge, MA 02139, USA.
|
---|
| 22 | #
|
---|
| 23 | ###############################################################################
|
---|
| 24 |
|
---|
[30336] | 25 | package DBDrivers::BaseDBDriver;
|
---|
[30318] | 26 |
|
---|
| 27 | # Pragma
|
---|
| 28 | use strict;
|
---|
[30336] | 29 | no strict 'subs';
|
---|
| 30 | no strict 'refs'; # allow filehandles to be variables and viceversa
|
---|
[30318] | 31 |
|
---|
| 32 | # Libaries
|
---|
[30336] | 33 | use Time::HiRes qw( gettimeofday );
|
---|
[30318] | 34 | use gsprintf 'gsprintf';
|
---|
| 35 |
|
---|
[30336] | 36 |
|
---|
| 37 | ## @function constructor
|
---|
| 38 | #
|
---|
| 39 | sub new
|
---|
| 40 | {
|
---|
| 41 | my $class = shift(@_);
|
---|
| 42 | my $debug = shift(@_);
|
---|
| 43 | my $self = {};
|
---|
| 44 | # Debug messages for this driver
|
---|
| 45 | $self->{'debug'} = $debug; # 1 to enable
|
---|
| 46 | # Default file extension - in this case it is an error to create a DB from
|
---|
| 47 | # BaseDBDriver
|
---|
| 48 | $self->{'default_file_extension'} = 'err';
|
---|
[30343] | 49 | # Support
|
---|
| 50 | $self->{'supports_set'} = 0;
|
---|
[30336] | 51 | bless($self, $class);
|
---|
| 52 | return $self;
|
---|
| 53 | }
|
---|
| 54 | ## new(void) => BaseDBDriver ##
|
---|
| 55 |
|
---|
| 56 |
|
---|
[30318] | 57 | ###############################################################################
|
---|
[30336] | 58 | ## Protected Functions
|
---|
[30318] | 59 | ###############################################################################
|
---|
| 60 |
|
---|
| 61 |
|
---|
[30336] | 62 | ## @function debugPrint(string) => void
|
---|
[30318] | 63 | #
|
---|
[30336] | 64 | sub debugPrint
|
---|
[30318] | 65 | {
|
---|
[30336] | 66 | my $self = shift(@_);
|
---|
| 67 | my $message = shift(@_);
|
---|
| 68 | if ($self->{'debug'}) {
|
---|
| 69 | my ($seconds, $microseconds) = gettimeofday();
|
---|
| 70 | print STDERR '[DEBUG:' . $seconds . '.' . $microseconds . '] ' . (caller 1)[3] . $message . "\n";
|
---|
[30318] | 71 | }
|
---|
| 72 | }
|
---|
[30336] | 73 | ## debugPrint(string) => void ##
|
---|
[30318] | 74 |
|
---|
| 75 |
|
---|
[30336] | 76 | ## @function debugPrintFunctionHeader(*) => void
|
---|
[30318] | 77 | #
|
---|
[30336] | 78 | sub debugPrintFunctionHeader
|
---|
[30318] | 79 | {
|
---|
[30336] | 80 | my $self = shift(@_);
|
---|
| 81 | if ($self->{'debug'}) {
|
---|
| 82 | my @arguments;
|
---|
| 83 | foreach my $argument (@_) {
|
---|
| 84 | if ($argument !~ /^-?\d+(\.?\d+)?$/) {
|
---|
| 85 | push(@arguments, '"' . $argument . '"');
|
---|
| 86 | }
|
---|
| 87 | else {
|
---|
| 88 | push(@arguments, $argument);
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
| 91 | my $message = '(' . join(', ', @arguments) . ')';
|
---|
| 92 | # Would love to just call debugPrint() here, but then caller would be wrong
|
---|
| 93 | my ($seconds, $microseconds) = gettimeofday();
|
---|
| 94 | print STDERR '[DEBUG:' . $seconds . '.' . $microseconds . '] ' . (caller 1)[3] . $message . "\n";
|
---|
[30318] | 95 | }
|
---|
[30336] | 96 | }
|
---|
| 97 | ## debugPrintFunctionHeader(*) => void
|
---|
[30318] | 98 |
|
---|
[30336] | 99 |
|
---|
| 100 | ## @function errorPrint(string, integer) => void
|
---|
| 101 | #
|
---|
| 102 | sub errorPrint
|
---|
| 103 | {
|
---|
| 104 | my $self = shift(@_);
|
---|
| 105 | my $message = shift(@_);
|
---|
| 106 | my $is_fatal = shift(@_);
|
---|
| 107 | print STDERR 'Error in ' . (caller 1)[3] . '! ' . $message . "\n";
|
---|
| 108 | if ($is_fatal) {
|
---|
| 109 | exit();
|
---|
[30318] | 110 | }
|
---|
| 111 | }
|
---|
[30336] | 112 | ## errorPrint(string, integer) => void ##
|
---|
[30318] | 113 |
|
---|
| 114 | ###############################################################################
|
---|
| 115 | ## Public Functions
|
---|
| 116 | ###############################################################################
|
---|
| 117 |
|
---|
| 118 |
|
---|
[30336] | 119 | ## @function get_infodb_file_path(string, string) => string
|
---|
[30318] | 120 | #
|
---|
[30336] | 121 | sub get_infodb_file_path
|
---|
[30318] | 122 | {
|
---|
[30336] | 123 | my $self = shift(@_);
|
---|
| 124 | my $collection_name = shift(@_);
|
---|
| 125 | my $infodb_directory_path = shift(@_);
|
---|
| 126 | my $infodb_file_name = &util::get_dirsep_tail($collection_name) . '.' . $self->{'default_file_extension'};
|
---|
| 127 | my $infodb_file_path = &FileUtils::filenameConcatenate($infodb_directory_path, $infodb_file_name);
|
---|
| 128 | return $infodb_file_path;
|
---|
| 129 | }
|
---|
| 130 | ## get_infodb_file_path(string, string) => string ##
|
---|
| 131 |
|
---|
| 132 |
|
---|
| 133 | ## @function supportsDatestamp(void) => boolean
|
---|
| 134 | #
|
---|
| 135 | sub supportsDatestamp
|
---|
| 136 | {
|
---|
| 137 | my $self = shift(@_);
|
---|
[30318] | 138 | return 0;
|
---|
| 139 | }
|
---|
[30336] | 140 | ## supportsDatestamp(void) => boolean ##
|
---|
[30318] | 141 |
|
---|
| 142 |
|
---|
[30336] | 143 | ## @function supportsMerge(void) => boolean
|
---|
[30318] | 144 | #
|
---|
[30336] | 145 | sub supportsMerge
|
---|
[30318] | 146 | {
|
---|
[30336] | 147 | my $self = shift(@_);
|
---|
[30318] | 148 | return 0;
|
---|
| 149 | }
|
---|
[30336] | 150 | ## supportsMerge(void) => boolean ##
|
---|
[30318] | 151 |
|
---|
| 152 |
|
---|
[30336] | 153 | ## @function supportsRSS(void) => boolean
|
---|
| 154 | #
|
---|
| 155 | sub supportsRSS
|
---|
| 156 | {
|
---|
| 157 | my $self = shift(@_);
|
---|
| 158 | return 0;
|
---|
| 159 | }
|
---|
| 160 | ## supportsRSS(void) => boolean ##
|
---|
| 161 |
|
---|
| 162 |
|
---|
[30343] | 163 | ## @function supportsSet(void) => integer
|
---|
| 164 | #
|
---|
| 165 | # Not all drivers support the notion of set
|
---|
| 166 | #
|
---|
| 167 | sub supportsSet
|
---|
| 168 | {
|
---|
| 169 | my $self = shift(@_);
|
---|
| 170 | return $self->{'supports_set'};
|
---|
| 171 | }
|
---|
| 172 | ## supportsSet(void) => integer ##
|
---|
| 173 |
|
---|
| 174 |
|
---|
[30318] | 175 | ###############################################################################
|
---|
| 176 | ## Virtual Functions
|
---|
| 177 | ###############################################################################
|
---|
| 178 |
|
---|
| 179 |
|
---|
[30336] | 180 | ## @function close_infodb_write_handle(*) => void
|
---|
[30318] | 181 | #
|
---|
| 182 | sub close_infodb_write_handle
|
---|
| 183 | {
|
---|
[30336] | 184 | my $self = shift(@_);
|
---|
[30318] | 185 | gsprintf(STDERR, (caller(0))[3] . " {common.must_be_implemented}\n");
|
---|
| 186 | die("\n");
|
---|
| 187 | }
|
---|
[30336] | 188 | ## close_infodb_write_handle(*) => void ##
|
---|
[30318] | 189 |
|
---|
| 190 |
|
---|
[30336] | 191 | ## @function delete_infodb_entry(*) => void
|
---|
[30318] | 192 | #
|
---|
| 193 | sub delete_infodb_entry
|
---|
| 194 | {
|
---|
[30336] | 195 | my $self = shift(@_);
|
---|
[30318] | 196 | gsprintf(STDERR, (caller(0))[3] . " {common.must_be_implemented}\n");
|
---|
| 197 | die("\n");
|
---|
| 198 | }
|
---|
[30336] | 199 | ## delete_infodb_entry(*) => void ##
|
---|
[30318] | 200 |
|
---|
| 201 |
|
---|
[30336] | 202 | ## @function mergeDatabases(*) => void
|
---|
[30318] | 203 | #
|
---|
[30336] | 204 | sub mergeDatabases
|
---|
[30318] | 205 | {
|
---|
[30336] | 206 | my $self = shift(@_);
|
---|
[30318] | 207 | gsprintf(STDERR, (caller(0))[3] . " {common.must_be_implemented}\n");
|
---|
| 208 | die("\n");
|
---|
| 209 | }
|
---|
[30336] | 210 | ## mergeDatabases(*) => void ##
|
---|
[30318] | 211 |
|
---|
| 212 |
|
---|
[30336] | 213 | ## @function open_infodb_write_handle(*) => void
|
---|
[30318] | 214 | #
|
---|
[30336] | 215 | sub open_infodb_write_handle
|
---|
[30318] | 216 | {
|
---|
[30336] | 217 | my $self = shift(@_);
|
---|
[30318] | 218 | gsprintf(STDERR, (caller(0))[3] . " {common.must_be_implemented}\n");
|
---|
| 219 | die("\n");
|
---|
| 220 | }
|
---|
[30336] | 221 | ## open_infodb_write_handle(*) => void ##
|
---|
[30318] | 222 |
|
---|
| 223 |
|
---|
[30336] | 224 | ## @function set_infodb_entry(*) => void
|
---|
[30318] | 225 | #
|
---|
[30336] | 226 | sub set_infodb_entry
|
---|
[30318] | 227 | {
|
---|
[30336] | 228 | my $self = shift(@_);
|
---|
[30318] | 229 | gsprintf(STDERR, (caller(0))[3] . " {common.must_be_implemented}\n");
|
---|
| 230 | die("\n");
|
---|
| 231 | }
|
---|
[30336] | 232 | ## set_infodb_entry(*) => void ##
|
---|
[30318] | 233 |
|
---|
| 234 |
|
---|
[30336] | 235 | ## @function read_infodb_rawentry(*) => string
|
---|
[30318] | 236 | #
|
---|
[30336] | 237 | sub read_infodb_rawentry
|
---|
[30318] | 238 | {
|
---|
[30336] | 239 | my $self = shift(@_);
|
---|
[30318] | 240 | gsprintf(STDERR, (caller(0))[3] . " {common.must_be_implemented}\n");
|
---|
| 241 | die("\n");
|
---|
| 242 | }
|
---|
[30336] | 243 | ## read_infodb_rawentry(*) => string ##
|
---|
[30318] | 244 |
|
---|
| 245 |
|
---|
[30336] | 246 | ## @function read_infodb_file(*) => void
|
---|
[30318] | 247 | #
|
---|
| 248 | sub read_infodb_file
|
---|
| 249 | {
|
---|
[30336] | 250 | my $self = shift(@_);
|
---|
[30318] | 251 | gsprintf(STDERR, (caller(0))[3] . " {common.must_be_implemented}\n");
|
---|
| 252 | die("\n");
|
---|
| 253 | }
|
---|
[30336] | 254 | ## read_infodb_file(*) => void ##
|
---|
[30318] | 255 |
|
---|
| 256 |
|
---|
[30336] | 257 | ## @function read_infodb_keys(*) => void
|
---|
[30318] | 258 | #
|
---|
| 259 | sub read_infodb_keys
|
---|
| 260 | {
|
---|
[30336] | 261 | my $self = shift(@_);
|
---|
[30318] | 262 | gsprintf(STDERR, (caller(0))[3] . " {common.must_be_implemented}\n");
|
---|
| 263 | die("\n");
|
---|
| 264 | }
|
---|
[30336] | 265 | ## read_infodb_keys(*) => void ##
|
---|
[30318] | 266 |
|
---|
| 267 |
|
---|
[30336] | 268 | ## @function write_infodb_entry(*) => void
|
---|
[30318] | 269 | #
|
---|
| 270 | sub write_infodb_entry
|
---|
| 271 | {
|
---|
[30336] | 272 | my $self = shift(@_);
|
---|
[30318] | 273 | gsprintf(STDERR, (caller(0))[3] . " {common.must_be_implemented}\n");
|
---|
| 274 | die("\n");
|
---|
| 275 | }
|
---|
[30336] | 276 | ## write_infodb_entry(*) => void ##
|
---|
[30318] | 277 |
|
---|
| 278 |
|
---|
[30336] | 279 | ## @function write_infodb_rawentry(*) => void
|
---|
[30318] | 280 | #
|
---|
| 281 | sub write_infodb_rawentry
|
---|
| 282 | {
|
---|
[30336] | 283 | my $self = shift(@_);
|
---|
[30318] | 284 | gsprintf(STDERR, (caller(0))[3] . " {common.must_be_implemented}\n");
|
---|
| 285 | die("\n");
|
---|
| 286 | }
|
---|
[30336] | 287 | ## write_infodb_rawentry(*) => void ##
|
---|
[30318] | 288 |
|
---|
[30336] | 289 |
|
---|
[30318] | 290 | 1;
|
---|