source: gs2-extensions/tdb/trunk/perllib/DBDrivers/BaseDBDriver.pm@ 30336

Last change on this file since 30336 was 30336, checked in by jmt12, 8 years ago

The base class of all database drivers, providing the 'interface' for all drivers as well as some common functionality (debug printing etc)

  • Property svn:executable set to *
File size: 7.0 KB
RevLine 
[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]25package DBDrivers::BaseDBDriver;
[30318]26
27# Pragma
28use strict;
[30336]29no strict 'subs';
30no strict 'refs'; # allow filehandles to be variables and viceversa
[30318]31
32# Libaries
[30336]33use Time::HiRes qw( gettimeofday );
[30318]34use gsprintf 'gsprintf';
35
[30336]36
37## @function constructor
38#
39sub 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';
49 bless($self, $class);
50 return $self;
51}
52## new(void) => BaseDBDriver ##
53
54
[30318]55###############################################################################
[30336]56## Protected Functions
[30318]57###############################################################################
58
59
[30336]60## @function debugPrint(string) => void
[30318]61#
[30336]62sub debugPrint
[30318]63{
[30336]64 my $self = shift(@_);
65 my $message = shift(@_);
66 if ($self->{'debug'}) {
67 my ($seconds, $microseconds) = gettimeofday();
68 print STDERR '[DEBUG:' . $seconds . '.' . $microseconds . '] ' . (caller 1)[3] . $message . "\n";
[30318]69 }
70}
[30336]71## debugPrint(string) => void ##
[30318]72
73
[30336]74## @function debugPrintFunctionHeader(*) => void
[30318]75#
[30336]76sub debugPrintFunctionHeader
[30318]77{
[30336]78 my $self = shift(@_);
79 if ($self->{'debug'}) {
80 my @arguments;
81 foreach my $argument (@_) {
82 if ($argument !~ /^-?\d+(\.?\d+)?$/) {
83 push(@arguments, '"' . $argument . '"');
84 }
85 else {
86 push(@arguments, $argument);
87 }
88 }
89 my $message = '(' . join(', ', @arguments) . ')';
90 # Would love to just call debugPrint() here, but then caller would be wrong
91 my ($seconds, $microseconds) = gettimeofday();
92 print STDERR '[DEBUG:' . $seconds . '.' . $microseconds . '] ' . (caller 1)[3] . $message . "\n";
[30318]93 }
[30336]94}
95## debugPrintFunctionHeader(*) => void
[30318]96
[30336]97
98## @function errorPrint(string, integer) => void
99#
100sub errorPrint
101{
102 my $self = shift(@_);
103 my $message = shift(@_);
104 my $is_fatal = shift(@_);
105 print STDERR 'Error in ' . (caller 1)[3] . '! ' . $message . "\n";
106 if ($is_fatal) {
107 exit();
[30318]108 }
109}
[30336]110## errorPrint(string, integer) => void ##
[30318]111
112###############################################################################
113## Public Functions
114###############################################################################
115
116
[30336]117## @function canInstantiate(void) => integer
[30318]118#
[30336]119# Called to determine if this driver implementation can actually be created
120# given its purpose and the current system (i.e. Windows only drivers on
121# Linux systems can't be instantiated...)
122#
123sub canInstantiate
[30318]124{
[30336]125 # Can't ever instantiate the base driver
[30318]126 return 0;
127}
[30336]128## canInstantiate(void) => integer ##
[30318]129
130
[30336]131## @function get_infodb_file_path(string, string) => string
[30318]132#
[30336]133sub get_infodb_file_path
[30318]134{
[30336]135 my $self = shift(@_);
136 my $collection_name = shift(@_);
137 my $infodb_directory_path = shift(@_);
138 my $infodb_file_name = &util::get_dirsep_tail($collection_name) . '.' . $self->{'default_file_extension'};
139 my $infodb_file_path = &FileUtils::filenameConcatenate($infodb_directory_path, $infodb_file_name);
140 return $infodb_file_path;
141}
142## get_infodb_file_path(string, string) => string ##
143
144
145## @function supportsDatestamp(void) => boolean
146#
147sub supportsDatestamp
148{
149 my $self = shift(@_);
[30318]150 return 0;
151}
[30336]152## supportsDatestamp(void) => boolean ##
[30318]153
154
[30336]155## @function supportsMerge(void) => boolean
[30318]156#
[30336]157sub supportsMerge
[30318]158{
[30336]159 my $self = shift(@_);
[30318]160 return 0;
161}
[30336]162## supportsMerge(void) => boolean ##
[30318]163
164
[30336]165## @function supportsRSS(void) => boolean
166#
167sub supportsRSS
168{
169 my $self = shift(@_);
170 return 0;
171}
172## supportsRSS(void) => boolean ##
173
174
[30318]175###############################################################################
176## Virtual Functions
177###############################################################################
178
179
[30336]180## @function close_infodb_write_handle(*) => void
[30318]181#
182sub 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#
193sub 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]204sub 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]215sub 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]226sub 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]237sub 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#
248sub 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#
259sub 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#
270sub 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#
281sub 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]2901;
Note: See TracBrowser for help on using the repository browser.