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

Revision 30343, 6.9 KB (checked in by jmt12, 4 years ago)

Minor changes to support drivers that don't support set_entry functionality. Defaults to not supported.

  • 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    # Support
50    $self->{'supports_set'} = 0;
51    bless($self, $class);
52    return $self;
53}
54## new(void) => BaseDBDriver ##
55
56
57###############################################################################
58## Protected Functions
59###############################################################################
60
61
62## @function debugPrint(string) => void
63#
64sub debugPrint
65{
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";
71    }
72}
73## debugPrint(string) => void ##
74
75
76## @function debugPrintFunctionHeader(*) => void
77#
78sub debugPrintFunctionHeader
79{
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";
95    }
96}
97## debugPrintFunctionHeader(*) => void
98
99
100## @function errorPrint(string, integer) => void
101#
102sub 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();
110    }
111}
112## errorPrint(string, integer) => void ##
113
114###############################################################################
115## Public Functions
116###############################################################################
117
118
119## @function get_infodb_file_path(string, string) => string
120#
121sub get_infodb_file_path
122{
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#
135sub supportsDatestamp
136{
137    my $self = shift(@_);
138    return 0;
139}
140## supportsDatestamp(void) => boolean ##
141
142
143## @function supportsMerge(void) => boolean
144#
145sub supportsMerge
146{
147    my $self = shift(@_);
148    return 0;
149}
150## supportsMerge(void) => boolean ##
151
152
153## @function supportsRSS(void) => boolean
154#
155sub supportsRSS
156{
157    my $self = shift(@_);
158    return 0;
159}
160## supportsRSS(void) => boolean ##
161
162
163## @function supportsSet(void) => integer
164#
165#  Not all drivers support the notion of set
166#
167sub supportsSet
168{
169    my $self = shift(@_);
170    return $self->{'supports_set'};
171}
172## supportsSet(void) => integer ##
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.