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

Revision 30336, 7.0 KB (checked in by jmt12, 5 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 *
Line 
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
25package DBDrivers::BaseDBDriver;
26
27# Pragma
28use strict;
29no strict 'subs';
30no strict 'refs'; # allow filehandles to be variables and viceversa
31
32# Libaries
33use Time::HiRes qw( gettimeofday );
34use gsprintf 'gsprintf';
35
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
55###############################################################################
56## Protected Functions
57###############################################################################
58
59
60## @function debugPrint(string) => void
61#
62sub debugPrint
63{
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";
69    }
70}
71## debugPrint(string) => void ##
72
73
74## @function debugPrintFunctionHeader(*) => void
75#
76sub debugPrintFunctionHeader
77{
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";
93    }
94}
95## debugPrintFunctionHeader(*) => void
96
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();
108    }
109}
110## errorPrint(string, integer) => void ##
111
112###############################################################################
113## Public Functions
114###############################################################################
115
116
117## @function canInstantiate(void) => integer
118#
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
124{
125    # Can't ever instantiate the base driver
126    return 0;
127}
128## canInstantiate(void) => integer  ##
129
130
131## @function get_infodb_file_path(string, string) => string
132#
133sub get_infodb_file_path
134{
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(@_);
150    return 0;
151}
152## supportsDatestamp(void) => boolean ##
153
154
155## @function supportsMerge(void) => boolean
156#
157sub supportsMerge
158{
159    my $self = shift(@_);
160    return 0;
161}
162## supportsMerge(void) => boolean ##
163
164
165## @function supportsRSS(void) => boolean
166#
167sub supportsRSS
168{
169    my $self = shift(@_);
170    return 0;
171}
172## supportsRSS(void) => boolean ##
173
174
175###############################################################################
176## Virtual Functions
177###############################################################################
178
179
180## @function close_infodb_write_handle(*) => void
181#
182sub close_infodb_write_handle
183{
184    my $self = shift(@_);
185    gsprintf(STDERR, (caller(0))[3] . " {common.must_be_implemented}\n");
186    die("\n");
187}
188## close_infodb_write_handle(*) => void ##
189
190
191## @function delete_infodb_entry(*) => void
192#
193sub delete_infodb_entry
194{
195    my $self = shift(@_);
196    gsprintf(STDERR, (caller(0))[3] . " {common.must_be_implemented}\n");
197    die("\n");
198}
199## delete_infodb_entry(*) => void ##
200
201
202## @function mergeDatabases(*) => void
203#
204sub mergeDatabases
205{
206    my $self = shift(@_);
207    gsprintf(STDERR, (caller(0))[3] . " {common.must_be_implemented}\n");
208    die("\n");
209}
210## mergeDatabases(*) => void ##
211
212
213## @function open_infodb_write_handle(*) => void
214#
215sub open_infodb_write_handle
216{
217    my $self = shift(@_);
218    gsprintf(STDERR, (caller(0))[3] . " {common.must_be_implemented}\n");
219    die("\n");
220}
221## open_infodb_write_handle(*) => void ##
222
223
224## @function set_infodb_entry(*) => void
225#
226sub set_infodb_entry
227{
228    my $self = shift(@_);
229    gsprintf(STDERR, (caller(0))[3] . " {common.must_be_implemented}\n");
230    die("\n");
231}
232## set_infodb_entry(*) => void ##
233
234
235## @function read_infodb_rawentry(*) => string
236#
237sub read_infodb_rawentry
238{
239    my $self = shift(@_);
240    gsprintf(STDERR, (caller(0))[3] . " {common.must_be_implemented}\n");
241    die("\n");
242}
243## read_infodb_rawentry(*) => string ##
244
245
246## @function read_infodb_file(*) => void
247#
248sub read_infodb_file
249{
250    my $self = shift(@_);
251    gsprintf(STDERR, (caller(0))[3] . " {common.must_be_implemented}\n");
252    die("\n");
253}
254## read_infodb_file(*) => void ##
255
256
257## @function read_infodb_keys(*) => void
258#
259sub read_infodb_keys
260{
261    my $self = shift(@_);
262    gsprintf(STDERR, (caller(0))[3] . " {common.must_be_implemented}\n");
263    die("\n");
264}
265## read_infodb_keys(*) => void ##
266
267
268## @function write_infodb_entry(*) => void
269#
270sub write_infodb_entry
271{
272    my $self = shift(@_);
273    gsprintf(STDERR, (caller(0))[3] . " {common.must_be_implemented}\n");
274    die("\n");
275}
276## write_infodb_entry(*) => void ##
277
278
279## @function write_infodb_rawentry(*) => void
280#
281sub write_infodb_rawentry
282{
283    my $self = shift(@_);
284    gsprintf(STDERR, (caller(0))[3] . " {common.must_be_implemented}\n");
285    die("\n");
286}
287## write_infodb_rawentry(*) => void ##
288
289
2901;
Note: See TracBrowser for help on using the browser.