1 |
################################# |
# $Id$ |
2 |
|
# |
3 |
|
# Copyright (c) 2002 Andreas Motl <andreas.motl@ilo.de> |
4 |
|
# |
5 |
|
# See COPYRIGHT section in pod text below for usage and distribution rights. |
6 |
# |
# |
7 |
# $Id$ |
################################# |
8 |
# |
# |
9 |
# $Log$ |
# $Log$ |
10 |
|
# Revision 1.4 2002/10/27 18:35:07 joko |
11 |
|
# + added pod |
12 |
|
# |
13 |
|
# Revision 1.3 2002/10/25 11:40:37 joko |
14 |
|
# + enhanced robustness |
15 |
|
# + more logging for debug-levels |
16 |
|
# + sub dropDb |
17 |
|
# |
18 |
|
# Revision 1.2 2002/10/17 00:04:29 joko |
19 |
|
# + sub createDb |
20 |
|
# + sub isConnected |
21 |
|
# + bugfixes regarding "deep recursion" stuff |
22 |
|
# |
23 |
# Revision 1.1 2002/10/10 03:43:12 cvsjoko |
# Revision 1.1 2002/10/10 03:43:12 cvsjoko |
24 |
# + new |
# + new |
25 |
# |
# |
|
# |
|
26 |
################################# |
################################# |
27 |
|
|
28 |
|
# aim_V1: should encapsulate Tangram, DBI, DBD::CSV and LWP:: to access them in an unordinary way ;) |
29 |
|
# aim_V2: introduce a generic layered structure, refactor *SUBLAYER*-stuff, make (e.g.) this possible: |
30 |
|
# - Perl Data::Storage[DBD::CSV] -> Perl LWP:: -> Internet HTTP/FTP/* -> Host Daemon -> csv-file |
31 |
|
|
32 |
|
BEGIN { |
33 |
|
$Data::Storage::VERSION = 0.01; |
34 |
|
} |
35 |
|
|
36 |
|
=head1 NAME |
37 |
|
|
38 |
|
Data::Storage - Interface for accessing various Storage implementations for Perl in an independent way |
39 |
|
|
40 |
|
=head1 SYNOPSIS |
41 |
|
|
42 |
|
... the basic way: |
43 |
|
|
44 |
|
|
45 |
|
... via inheritance: |
46 |
|
|
47 |
|
use Data::Storage; |
48 |
|
my $proxyObj = new HttpProxy; |
49 |
|
$proxyObj->{url} = $url; |
50 |
|
$proxyObj->{payload} = $content; |
51 |
|
$self->{storage}->insert($proxyObj); |
52 |
|
|
53 |
|
use Data::Storage; |
54 |
|
my $proxyObj = HttpProxy->new( |
55 |
|
url => $url, |
56 |
|
payload => $content, |
57 |
|
); |
58 |
|
$self->{storage}->insert($proxyObj); |
59 |
|
|
60 |
|
|
61 |
|
=head2 NOTE |
62 |
|
|
63 |
|
This module heavily relies on DBI and Tangram, but adds a lot of additional bugs and quirks. |
64 |
|
Please look at their documentation and this code for additional information. |
65 |
|
|
66 |
|
|
67 |
|
=cut |
68 |
|
|
69 |
|
# The POD text continues at the end of the file. |
70 |
|
|
71 |
|
|
72 |
package Data::Storage; |
package Data::Storage; |
73 |
|
|
74 |
use strict; |
use strict; |
75 |
use warnings; |
use warnings; |
76 |
|
|
77 |
use Data::Storage::Locator; |
use Data::Storage::Locator; |
|
use Data::Storage::Handler::DBI; |
|
|
use Data::Storage::Handler::Tangram; |
|
78 |
|
|
79 |
# get logger instance |
# get logger instance |
80 |
my $logger = Log::Dispatch::Config->instance; |
my $logger = Log::Dispatch::Config->instance; |
95 |
|
|
96 |
sub AUTOLOAD { |
sub AUTOLOAD { |
97 |
|
|
98 |
|
# since this is a core function acting as dispatcher to $self->{STORAGEHANDLE}, |
99 |
|
# some sophisticated handling and filtering is needed to avoid things like |
100 |
|
# - Deep recursion on subroutine "Data::Storage::AUTOLOAD" |
101 |
|
# - Deep recursion on subroutine "Data::Storage::Handler::Abstract::AUTOLOAD" |
102 |
|
# - Deep recursion on anonymous subroutine at [...] |
103 |
|
# we also might filter log messages caused by logging itself in "advanced logging of AUTOLOAD calls" |
104 |
|
|
105 |
my $self = shift; |
my $self = shift; |
106 |
our $AUTOLOAD; |
our $AUTOLOAD; |
107 |
|
|
111 |
my $method = $AUTOLOAD; |
my $method = $AUTOLOAD; |
112 |
$method =~ s/^.*:://; |
$method =~ s/^.*:://; |
113 |
|
|
114 |
#$logger->debug( __PACKAGE__ . "[$self->{locator}->{type}]" . "->" . $method . "(@_)" . " (AUTOLOAD)" ); |
# advanced logging of AUTOLOAD calls |
115 |
|
my $logstring = ""; |
116 |
|
$logstring .= __PACKAGE__ . "[$self->{locator}->{type}]" . "->" . $method; |
117 |
|
#print "count: ", $#_, "\n"; |
118 |
|
#$logstring .= Dumper(@_) if ($#_ != -1); |
119 |
|
my $tabcount = int( (80 - length($logstring)) / 10 ); |
120 |
|
$logstring .= "\t" x $tabcount . "(AUTOLOAD)"; |
121 |
|
# TODO: only ok if logstring doesn't contain |
122 |
|
# e.g. "Data::Storage[Tangram]->insert(SystemEvent=HASH(0x5c0034c)) (AUTOLOAD)" |
123 |
|
# but that would be way too specific as long as we don't have an abstract handler for this ;) |
124 |
|
$logger->debug( $logstring ); |
125 |
|
|
126 |
|
# filtering AUTOLOAD calls |
127 |
if ($self->_filter_AUTOLOAD($method)) { |
if ($self->_filter_AUTOLOAD($method)) { |
128 |
$self->_accessStorage(); |
$self->_accessStorage(); |
129 |
$self->{STORAGEHANDLE}->$method(@_); |
$self->{STORAGEHANDLE}->$method(@_); |
155 |
sub _accessStorage { |
sub _accessStorage { |
156 |
my $self = shift; |
my $self = shift; |
157 |
# TODO: to some tracelevel! |
# TODO: to some tracelevel! |
158 |
#$logger->debug( __PACKAGE__ . "[$self->{type}]" . "->_accessStorage()" ); |
$logger->debug( __PACKAGE__ . "[$self->{locator}->{type}]" . "->_accessStorage()" ); |
159 |
if (!$self->{STORAGEHANDLE}) { |
if (!$self->{STORAGEHANDLE}) { |
160 |
$self->_createStorageHandle(); |
$self->_createStorageHandle(); |
161 |
} |
} |
172 |
# propagate args to handler |
# propagate args to handler |
173 |
# needs some more thoughts! (not only "dbi" to Tangram, when (in future) db is not more the common case) |
# needs some more thoughts! (not only "dbi" to Tangram, when (in future) db is not more the common case) |
174 |
if ($type eq 'DBI') { |
if ($type eq 'DBI') { |
175 |
|
use Data::Storage::Handler::DBI; |
176 |
#my @args = %{$self->{locator}->{dbi}}; |
#my @args = %{$self->{locator}->{dbi}}; |
177 |
my @args = %{$self->{locator}}; |
my @args = %{$self->{locator}}; |
178 |
|
# create new storage handle |
179 |
$self->{STORAGEHANDLE} = $pkg->new( @args ); |
$self->{STORAGEHANDLE} = $pkg->new( @args ); |
180 |
} |
} |
181 |
if ($type eq 'Tangram') { |
if ($type eq 'Tangram') { |
182 |
|
use Data::Storage::Handler::Tangram; |
183 |
#$self->{STORAGEHANDLE} = $pkg->new( dsn => $self->{locator}->{dbi}->{dsn} ); |
#$self->{STORAGEHANDLE} = $pkg->new( dsn => $self->{locator}->{dbi}->{dsn} ); |
184 |
#my @args = %{$self->{locator}->{dbi}}; |
#my @args = %{$self->{locator}->{dbi}}; |
185 |
my @args = %{$self->{locator}}; |
my @args = %{$self->{locator}}; |
186 |
|
# create new storage handle |
187 |
$self->{STORAGEHANDLE} = $pkg->new( @args ); |
$self->{STORAGEHANDLE} = $pkg->new( @args ); |
188 |
|
|
189 |
#$self->{STORAGEHANDLE_UNDERLYING} = $self->{STORAGEHANDLE}->getUnderlyingStorage(); |
#$self->{STORAGEHANDLE_UNDERLYING} = $self->{STORAGEHANDLE}->getUnderlyingStorage(); |
190 |
#$self->{STORAGEHANDLE_UNDERLYING}->_configureCOREHANDLE(); |
#$self->{STORAGEHANDLE_UNDERLYING}->_configureCOREHANDLE(); |
191 |
} |
} |
197 |
my $self = shift; |
my $self = shift; |
198 |
my $name = shift; |
my $name = shift; |
199 |
my $package = shift; |
my $package = shift; |
200 |
my $logger = shift; |
my $logger1 = shift; |
201 |
my $objectCreator = shift; |
my $objectCreator = shift; |
202 |
|
|
203 |
#$logger->add( Log::Dispatch::Tangram->new( name => $name, |
#$logger->add( Log::Dispatch::Tangram->new( name => $name, |
224 |
|
|
225 |
my $self = shift; |
my $self = shift; |
226 |
my $name = shift; |
my $name = shift; |
227 |
my $logger = shift; |
#my $logger = shift; |
228 |
|
|
229 |
$logger->remove($name); |
$logger->remove($name); |
230 |
|
|
248 |
$dbh->disconnect(); |
$dbh->disconnect(); |
249 |
return 1; |
return 1; |
250 |
} else { |
} else { |
251 |
$logger->error( __PACKAGE__ . "[$self->{locator}->{type}]" . "->testDsn(): " . "DBI-error: " . DBI::errstr ); |
$logger->error( __PACKAGE__ . "[$self->{locator}->{type}]" . "->testDsn(): " . "DBI-error: " . $DBI::errstr ); |
252 |
|
} |
253 |
|
} |
254 |
|
|
255 |
|
sub createDb { |
256 |
|
my $self = shift; |
257 |
|
my $dsn = $self->{locator}->{dbi}->{dsn}; |
258 |
|
|
259 |
|
$logger->debug( __PACKAGE__ . "->createDb( dsn $dsn )" ); |
260 |
|
|
261 |
|
$dsn =~ s/database=(.+?);//; |
262 |
|
my $database_name = $1; |
263 |
|
|
264 |
|
my $ok; |
265 |
|
|
266 |
|
if ( my $dbh = DBI->connect($dsn, '', '', { |
267 |
|
PrintError => 0, |
268 |
|
} ) ) { |
269 |
|
if ($database_name) { |
270 |
|
if ($dbh->do("CREATE DATABASE $database_name;")) { |
271 |
|
$ok = 1; |
272 |
|
} |
273 |
|
} |
274 |
|
$dbh->disconnect(); |
275 |
|
} |
276 |
|
|
277 |
|
return $ok; |
278 |
|
|
279 |
|
} |
280 |
|
|
281 |
|
sub dropDb { |
282 |
|
my $self = shift; |
283 |
|
my $dsn = $self->{locator}->{dbi}->{dsn}; |
284 |
|
|
285 |
|
$logger->debug( __PACKAGE__ . "->dropDb( dsn $dsn )" ); |
286 |
|
|
287 |
|
$dsn =~ s/database=(.+?);//; |
288 |
|
my $database_name = $1; |
289 |
|
|
290 |
|
my $ok; |
291 |
|
|
292 |
|
if ( my $dbh = DBI->connect($dsn, '', '', { |
293 |
|
PrintError => 0, |
294 |
|
} ) ) { |
295 |
|
if ($database_name) { |
296 |
|
if ($dbh->do("DROP DATABASE $database_name;")) { |
297 |
|
$ok = 1; |
298 |
|
} |
299 |
|
} |
300 |
|
$dbh->disconnect(); |
301 |
} |
} |
302 |
|
|
303 |
|
return $ok; |
304 |
|
} |
305 |
|
|
306 |
|
sub isConnected { |
307 |
|
my $self = shift; |
308 |
|
return 1 if $self->{STORAGEHANDLE}; |
309 |
} |
} |
310 |
|
|
|
1; |
|
311 |
|
1; |
312 |
|
__END__ |
313 |
|
|
314 |
|
|
315 |
|
=head1 DESCRIPTION |
316 |
|
|
317 |
|
Data::Storage is module for a accessing various "data structures" stored inside |
318 |
|
various "data containers". It sits on top of DBI and/or Tangram. |
319 |
|
|
320 |
|
|
321 |
|
=head1 AUTHORS / COPYRIGHT |
322 |
|
|
323 |
|
The Data::Storage module is Copyright (c) 2002 Andreas Motl. |
324 |
|
All rights reserved. |
325 |
|
|
326 |
|
You may distribute it under the terms of either the GNU General Public |
327 |
|
License or the Artistic License, as specified in the Perl README file. |
328 |
|
|
329 |
|
|
330 |
|
=head1 ACKNOWLEDGEMENTS |
331 |
|
|
332 |
|
Larry Wall and the C<perl5-porters> for Perl, |
333 |
|
Tim Bunce for DBI, Jean-Louis Leroy for Tangram and Set::Object, |
334 |
|
Sam Vilain for Class::Tangram. |
335 |
|
|
336 |
|
|
337 |
|
=head1 SUPPORT / WARRANTY |
338 |
|
|
339 |
|
Data::Storage is free software. IT COMES WITHOUT WARRANTY OF ANY KIND. |
340 |
|
|
341 |
|
|
342 |
|
=head1 TODO |
343 |
|
|
344 |
|
|
345 |
|
=head2 Handle the following errors/cases: |
346 |
|
|
347 |
|
=head3 "DBI-Error [Tangram]: DBD::mysql::st execute failed: Unknown column 't1.requestdump' in 'field list'" |
348 |
|
|
349 |
|
... occours when operating on object-attributes not introduced yet: |
350 |
|
this should be detected and appended/replaced through: |
351 |
|
"Schema-Error detected, maybe (just) an inconsistency. |
352 |
|
Please check if your declaration in schema-module "a" matches structure in database "b" or try to run" |
353 |
|
db_setup.pl --dbkey=import --action=deploy |
354 |
|
|
355 |
|
=head3 Compare schema (structure diff) with database ... |
356 |
|
|
357 |
|
... when issuing "db_setup.pl --dbkey=import --action=deploy" |
358 |
|
on a database with an already deployed schema, use an additional "--update" then |
359 |
|
to lift the schema inside the database to the current declared schema. |
360 |
|
You will have to approve removals and changes on field-level while |
361 |
|
new objects and new fields are introduced silently without any interaction needed. |
362 |
|
In future versions there may be additional options to control silent processing of |
363 |
|
removals and changes. |
364 |
|
See this CRUD-table applying to the actions occouring on Classes and Class variables when deploying schemas, |
365 |
|
don't mix this up with CRUD-actions on Objects, these are already handled by (e.g.) Tangram itself. |
366 |
|
Classes: |
367 |
|
C create -> yes, handled automatically |
368 |
|
R retrieve -> no, not subject of this aspect since it is about deployment only |
369 |
|
U update -> yes, automatically for Class meta-attributes, yes/no for Class variables (look at the rules down here) |
370 |
|
D delete -> yes, just by user-interaction |
371 |
|
Class variables: |
372 |
|
C create -> yes, handled automatically |
373 |
|
R retrieve -> no, not subject of this aspect since it is about deployment only |
374 |
|
U update -> yes, just by user-interaction; maybe automatically if it can be determined that data wouldn't be lost |
375 |
|
D delete -> yes, just by user-interaction |
376 |
|
It's all about not to be able to loose data simply while this is in alpha stage. |
377 |
|
|
378 |
|
|
379 |
|
=head2 Introduce some features: |
380 |
|
|
381 |
|
Get this stuff together with UML (Unified Modeling Language) and/or standards from ODMG. |
382 |
|
Make it possible to load/save schemas in XMI (XML Metadata Interchange), |
383 |
|
which seems to be most commonly used today, perhaps handle objects with OIFML. |
384 |
|
Integrate/bundle this with a web-/html-based UML modeling tool or |
385 |
|
some other interesting stuff like the "Co-operative UML Editor" from Uni Darmstadt. (web-/java-based) |
386 |
|
Enable Round Trip Engineering. Keep code and diagrams in sync. Don't annoy/bother the programmer. |
387 |
|
|
388 |
|
|
389 |
|
=head3 Links: |
390 |
|
|
391 |
|
UML 1.3 Spec: http://cgi.omg.org/cgi-bin/doc?ad/99-06-08.pdf |
392 |
|
XMI 1.1 Spec: http://cgi.omg.org/cgi-bin/doc?ad/99-10-02.pdf |
393 |
|
XMI 2.0 Spec: http://cgi.omg.org/docs/ad/01-06-12.pdf |
394 |
|
ODMG: http://odmg.org/ |
395 |
|
OIFML: http://odmg.org/library/readingroom/oifml.pdf |
396 |
|
Co-operative UML Editor: http://www.darmstadt.gmd.de/concert/activities/internal/umledit.html |
397 |
|
|
398 |
|
further readings: |
399 |
|
http://www.google.com/search?q=web+based+uml+editor&hl=en&lr=&ie=UTF-8&oe=UTF-8&start=10&sa=N |
400 |
|
http://www.fernuni-hagen.de/DVT/Aktuelles/01FHHeidelberg.pdf |
401 |
|
http://www.enhyper.com/src/documentation/ |
402 |
|
http://cis.cs.tu-berlin.de/Dokumente/Diplomarbeiten/2001/skinner.pdf |
403 |
|
http://citeseer.nj.nec.com/vilain00diagrammatic.html |
404 |
|
http://archive.devx.com/uml/articles/Smith01/Smith01-3.asp |
405 |
|
|
406 |
|
maybe useful for / to be integrated with: |
407 |
|
ArapXML: http://xml.coverpages.org/ni2001-09-24-b.html |