/[cvs]/nfo/perl/libs/Data/Storage/Handler/Abstract.pm
ViewVC logotype

Contents of /nfo/perl/libs/Data/Storage/Handler/Abstract.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.5 - (show annotations)
Sun Dec 1 22:19:33 2002 UTC (21 years, 7 months ago) by joko
Branch: MAIN
Changes since 1.4: +6 -2 lines
+ just disconnect if COREHANDLE exists

1 #################################
2 #
3 # $Id: Abstract.pm,v 1.4 2002/12/01 04:45:38 joko Exp $
4 #
5 # $Log: Abstract.pm,v $
6 # Revision 1.4 2002/12/01 04:45:38 joko
7 # + sub eraseAll
8 # + sub createDb
9 #
10 # Revision 1.3 2002/11/29 04:58:20 joko
11 # + Storage::Result now uses the same dispatching mechanism like Storage::Handler
12 #
13 # Revision 1.2 2002/10/17 00:08:07 joko
14 # + bugfixes regarding "deep recursion" stuff
15 #
16 # Revision 1.1 2002/10/10 03:44:07 cvsjoko
17 # + new
18 #
19 #
20 #################################
21
22 package Data::Storage::Handler::Abstract;
23
24 use strict;
25 use warnings;
26
27 use Data::Dumper;
28
29 # get logger instance
30 my $logger = Log::Dispatch::Config->instance;
31
32 #our $lock_info;
33
34 sub new {
35 my $invocant = shift;
36 my $class = ref($invocant) || $invocant;
37
38 # logging info about the actual handler called
39 $logger->debug( "$invocant->new( @_ )" );
40 #$logger->debug( __PACKAGE__ . "->" . "new()" );
41
42 # arguments become properties
43 my $self = { @_ };
44 # create object from blessed hash-reference
45 bless $self, $class;
46
47 # handle meta data
48 #my $metainfo = $self->getMetaInfo($class);
49 my $metainfo = $self->getMetaInfo();
50 if (!$metainfo->{disconnectMethod}) { $metainfo->{disconnectMethod} = 'disconnect'; }
51 # type?
52 $invocant =~ s/Data::Storage::Handler:://;
53 $metainfo->{type} = $invocant;
54
55 # direct accessible (non-protected) properties ( was: my $self = { }; )
56 $self->{metainfo} = $metainfo;
57
58 return $self;
59 }
60
61
62 sub AUTOLOAD {
63
64 # recursion problem to be avoided here!!!
65
66 # - problem: we get recursion-hangs!!! => perl stops with ...
67 # "Deep recursion on subroutine "Data::Storage::Handler::Abstract::AUTOLOAD" at [...]/libs/Data/Storage.pm line 38."
68
69 # - when: if we log to ourselves as a storage-implementation (e.g. via Log::Dispatch::Tangram)
70 # - why: debug-messages are on a very low level including every data-operation to "Data::Storage(::Handler::X)",
71 # so e.g. a "$storage->insert" would trigger another "$storage->insert" itself
72 # which leads to a infinite recursion loop (deep recursion)
73 # - solution: locks! (by Hack or (maybe) via Perl "local"s)
74
75 my $self = shift;
76
77 our $AUTOLOAD;
78 #return if $AUTOLOAD =~ m/::DESTROY$/;
79
80 # find out methodname
81 my $methodname = $AUTOLOAD;
82 $methodname =~ s/^.*:://;
83
84 # handle locking (hack)
85 if ($self->{lock_info}->{last_method} && $methodname eq $self->{lock_info}->{last_method}) {
86 $self->{lock_info}->{log_lock} = 1;
87 } else {
88 $self->{lock_info}->{log_lock} = 0;
89 }
90 $self->{lock_info}->{last_method} = $methodname;
91
92 # test for COREHANDLE
93 if (!$self->{COREHANDLE}) {
94 #print "no COREHANDLE", "\n";
95 if (!$self->{lock_info}->{log_lock}) {
96 $logger->error( __PACKAGE__ . "[$self->{metainfo}->{type}]" . ": " . "COREHANDLE is undefined while trying to execute method \"$methodname\"" );
97 }
98 return;
99 }
100
101 # try to dispatch method-call to Storage::Handler::*
102 #if ($self->can($methodname)) {
103 #$logger->debug( __PACKAGE__ . "[$self->{metainfo}->{type}]" . "->" . $methodname . "(@_)" );
104 #my $res = $self->$methodname(@_);
105 #if ($res) { return $res; }
106 #}
107
108 # try to dispatch method-call to COREHANDLE
109 if ($self->{COREHANDLE}->can($methodname) || $self->{COREHANDLE}->can("AUTOLOAD")) {
110 #$logger->debug( __PACKAGE__ . "->" . $methodname . " (AUTOLOAD)" );
111 #$lock_AUTOLOAD = 1 if ($methodname eq 'insert');
112 if (!$self->{lock_info}->{log_lock}) {
113 #print "method: $methodname", "\n";
114 $logger->debug( __PACKAGE__ . "[$self->{metainfo}->{type}]" . "->" . $methodname . "(@_)" );
115 } else {
116 # AUTOLOAD - sub is locked to prevent deep recursions if (e.g.) db-inserts cause log-actions to same db itself
117 }
118 #$lock_AUTOLOAD = 0;
119 #$logger->log( level => 'debug', message => __PACKAGE__ . "->" . $methodname . " (AUTOLOAD)" );
120
121 # method calls doing it until here will get dispatched to the proper handler
122 return $self->{COREHANDLE}->$methodname(@_);
123 }
124
125 }
126
127 sub DESTROY {
128 my $self = shift;
129 if ($self->{COREHANDLE}) {
130 $logger->debug( __PACKAGE__ . "[$self->{metainfo}->{type}]" . "->DESTROY" );
131
132 my $disconnectMethod = $self->{metainfo}->{disconnectMethod};
133
134 # call "disconnect" or alike on COREHANDLE
135 # was: $self->{COREHANDLE}->disconnect();
136 $disconnectMethod && $self->{COREHANDLE} && ( $self->{COREHANDLE}->$disconnectMethod() );
137
138 undef $self->{COREHANDLE};
139 }
140 }
141
142
143 sub _abstract_function {
144 my $self = shift;
145 my $fName = shift;
146 my $class = ref($self);
147 $logger->error( __PACKAGE__ . ": function \"$fName\" is an abstract method, please implement it in \"$class\"");
148 #exit;
149 }
150
151 sub _typeCheck {
152 my $type = shift;
153 print "type: $type";
154 eval("use Data::Storage::$type;");
155 }
156
157
158 # ====================================================
159 # PUBLIC METHODS
160 # ====================================================
161
162 sub existsChildNode {
163 my $self = shift;
164 my $nodename = shift;
165 #$nodename = 'TransactionRoutingTable';
166 $logger->debug( __PACKAGE__ . "->getChildNode( nodename $nodename )" );
167 $self->getChildNodes() unless $self->{meta}->{childnodes};
168 my $result = grep(m/$nodename/i, @{$self->{meta}->{childnodes}}); # TODO: use "/i" only on win32-systems!
169 return $result;
170 }
171
172 sub connect_tmp {
173
174 # my $self = shift;
175 # my $connectionString = shift;
176 # # todo: patch connectionString to $args
177 # _typeCheck($self->{args}{type});
178
179 }
180
181
182 # ====================================================
183 # CONCRETE METHOD DUMMIES (croaking via "$logger" by calling "_abstract_function")
184 # ====================================================
185
186 # TODO:
187 # - abstract "abstract methods" to list/hash to be used in AUTOLOAD
188 # e.g.: my @ABSTRACT_METHODS = (qw( connect sendCommand getChildNodes ));
189 # use Class::XYZ (Construct)
190 # - build them via anonymous subs
191 # - introduce them via symbols
192
193 sub sendCommand {
194 my $self = shift;
195 $self->_abstract_function('sendCommand');
196 }
197
198 sub getChildNodes {
199 my $self = shift;
200 $self->_abstract_function('getChildNodes');
201 }
202
203 sub configureCOREHANDLE {
204 my $self = shift;
205 $self->_abstract_function('configureCOREHANDLE');
206 }
207
208 sub getMetaInfo {
209 my $self = shift;
210 $self->_abstract_function('getMetaInfo');
211 return;
212 }
213
214 sub getListUnfiltered {
215 my $self = shift;
216 $self->_abstract_function('getListUnfiltered');
217 return;
218 }
219
220 sub getListFiltered {
221 my $self = shift;
222 $self->_abstract_function('getListFiltered');
223 return;
224 }
225
226 sub sendQuery {
227 my $self = shift;
228 $self->_abstract_function('sendQuery');
229 return;
230 }
231
232 sub connect {
233 my $self = shift;
234 $self->_abstract_function('connect');
235 return;
236 }
237
238 sub testIntegrity {
239 my $self = shift;
240 $self->_abstract_function('testIntegrity');
241 return;
242 }
243
244 sub quoteSql {
245 my $self = shift;
246 $self->_abstract_function('quoteSql');
247 return;
248 }
249
250 sub eraseAll {
251 my $self = shift;
252 $self->_abstract_function('eraseAll');
253 return;
254 }
255
256 sub createDb {
257 my $self = shift;
258 $self->_abstract_function('createDb');
259 return;
260 }
261
262 1;

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed