4 |
# |
# |
5 |
# See COPYRIGHT section in pod text below for usage and distribution rights. |
# See COPYRIGHT section in pod text below for usage and distribution rights. |
6 |
# |
# |
7 |
################################# |
############################################ |
8 |
# |
# |
9 |
# $Log$ |
# $Log$ |
10 |
|
# Revision 1.12 2002/12/12 02:50:15 joko |
11 |
|
# + this now (unfortunately) needs DBI for some helper functions |
12 |
|
# + TODO: these have to be refactored to another scope! (soon!) |
13 |
|
# |
14 |
|
# Revision 1.11 2002/12/11 06:53:19 joko |
15 |
|
# + updated pod |
16 |
|
# |
17 |
|
# Revision 1.10 2002/12/07 03:37:23 joko |
18 |
|
# + updated pod |
19 |
|
# |
20 |
|
# Revision 1.9 2002/12/01 22:15:45 joko |
21 |
|
# - sub createDb: moved to handler |
22 |
|
# |
23 |
|
# Revision 1.8 2002/11/29 04:48:23 joko |
24 |
|
# + updated pod |
25 |
|
# |
26 |
# Revision 1.7 2002/11/17 06:07:18 joko |
# Revision 1.7 2002/11/17 06:07:18 joko |
27 |
# + creating the handler is easier than proposed first - for now :-) |
# + creating the handler is easier than proposed first - for now :-) |
28 |
# + sub testAvailability |
# + sub testAvailability |
50 |
# Revision 1.1 2002/10/10 03:43:12 cvsjoko |
# Revision 1.1 2002/10/10 03:43:12 cvsjoko |
51 |
# + new |
# + new |
52 |
# |
# |
53 |
################################# |
############################################ |
54 |
|
|
|
# aim_V1: should encapsulate Tangram, DBI, DBD::CSV and LWP:: to access them in an unordinary way ;) |
|
|
# aim_V2: introduce a generic layered structure, refactor *SUBLAYER*-stuff, make (e.g.) this possible: |
|
|
# - Perl Data::Storage[DBD::CSV] -> Perl LWP:: -> Internet HTTP/FTP/* -> Host Daemon -> csv-file |
|
55 |
|
|
56 |
BEGIN { |
BEGIN { |
57 |
$Data::Storage::VERSION = 0.01; |
$Data::Storage::VERSION = 0.02; |
58 |
} |
} |
59 |
|
|
60 |
|
|
61 |
=head1 NAME |
=head1 NAME |
62 |
|
|
63 |
Data::Storage - Interface for accessing various Storage implementations for Perl in an independent way |
Data::Storage - Interface for accessing various Storage implementations for Perl in an independent way |
64 |
|
|
65 |
|
|
66 |
|
=head1 AIMS |
67 |
|
|
68 |
|
- should encapsulate Tangram, DBI, DBD::CSV and LWP:: to access them in an unordinary (more convenient) way ;) |
69 |
|
- introduce a generic layered structure, refactor *SUBLAYER*-stuff, make (e.g.) this possible: |
70 |
|
Perl Data::Storage[DBD::CSV] -> Perl LWP:: -> Internet HTTP/FTP/* -> Host Daemon -> csv-file |
71 |
|
- provide generic synchronization mechanisms across arbitrary/multiple storages based on ident/checksum |
72 |
|
maybe it's possible to have schema-, structural- and semantical modifications synchronized??? |
73 |
|
|
74 |
|
|
75 |
=head1 SYNOPSIS |
=head1 SYNOPSIS |
76 |
|
|
77 |
... the basic way: |
=head2 BASIC ACCESS |
78 |
|
|
79 |
|
=head2 ADVANCED ACCESS |
80 |
|
|
81 |
... via inheritance: |
... via inheritance: |
82 |
|
|
94 |
$self->{storage}->insert($proxyObj); |
$self->{storage}->insert($proxyObj); |
95 |
|
|
96 |
|
|
97 |
|
=head2 SYNCHRONIZATION |
98 |
|
|
99 |
|
my $nodemapping = { |
100 |
|
'LangText' => 'langtexts.csv', |
101 |
|
'Currency' => 'currencies.csv', |
102 |
|
'Country' => 'countries.csv', |
103 |
|
}; |
104 |
|
|
105 |
|
my $propmapping = { |
106 |
|
'LangText' => [ |
107 |
|
[ 'source:lcountrykey' => 'target:country' ], |
108 |
|
[ 'source:lkey' => 'target:key' ], |
109 |
|
[ 'source:lvalue' => 'target:text' ], |
110 |
|
], |
111 |
|
'Currency' => [ |
112 |
|
[ 'source:ckey' => 'target:key' ], |
113 |
|
[ 'source:cname' => 'target:text' ], |
114 |
|
], |
115 |
|
'Country' => [ |
116 |
|
[ 'source:ckey' => 'target:key' ], |
117 |
|
[ 'source:cname' => 'target:text' ], |
118 |
|
], |
119 |
|
}; |
120 |
|
|
121 |
|
sub syncResource { |
122 |
|
|
123 |
|
my $self = shift; |
124 |
|
my $node_source = shift; |
125 |
|
my $mode = shift; |
126 |
|
my $opts = shift; |
127 |
|
|
128 |
|
$mode ||= ''; |
129 |
|
$opts->{erase} ||= 0; |
130 |
|
|
131 |
|
$logger->info( __PACKAGE__ . "->syncResource( node_source $node_source mode $mode erase $opts->{erase} )"); |
132 |
|
|
133 |
|
# resolve metadata for syncing requested resource |
134 |
|
my $node_target = $nodemapping->{$node_source}; |
135 |
|
my $mapping = $propmapping->{$node_source}; |
136 |
|
|
137 |
|
if (!$node_target || !$mapping) { |
138 |
|
# loggger.... "no target, sorry!" |
139 |
|
print "error while resolving resource metadata", "\n"; |
140 |
|
return; |
141 |
|
} |
142 |
|
|
143 |
|
if ($opts->{erase}) { |
144 |
|
$self->_erase_all($node_source); |
145 |
|
} |
146 |
|
|
147 |
|
# create new sync object |
148 |
|
my $sync = Data::Transfer::Sync->new( |
149 |
|
storages => { |
150 |
|
L => $self->{bizWorks}->{backend}, |
151 |
|
R => $self->{bizWorks}->{resources}, |
152 |
|
}, |
153 |
|
id_authorities => [qw( L ) ], |
154 |
|
checksum_authorities => [qw( L ) ], |
155 |
|
write_protected => [qw( R ) ], |
156 |
|
verbose => 1, |
157 |
|
); |
158 |
|
|
159 |
|
# sync |
160 |
|
# todo: filter!? |
161 |
|
$sync->syncNodes( { |
162 |
|
direction => $mode, # | +PUSH | +PULL | -FULL | +IMPORT | -EXPORT |
163 |
|
method => 'checksum', # | -timestamp | -manual |
164 |
|
source => "L:$node_source", |
165 |
|
source_ident => 'storage_method:id', |
166 |
|
source_exclude => [qw( id cs )], |
167 |
|
target => "R:$node_target", |
168 |
|
target_ident => 'property:oid', |
169 |
|
mapping => $mapping, |
170 |
|
} ); |
171 |
|
|
172 |
|
} |
173 |
|
|
174 |
|
|
175 |
=head2 NOTE |
=head2 NOTE |
176 |
|
|
177 |
This module heavily relies on DBI and Tangram, but adds a lot of additional bugs and quirks. |
This module heavily relies on DBI and Tangram, but adds a lot of additional bugs and quirks. |
178 |
Please look at their documentation and/or this code for additional information. |
Please look at their documentation and/or this code for additional information. |
179 |
|
|
180 |
|
|
181 |
=head1 REQUIREMENTS |
=head1 REQUIREMENTS |
182 |
|
|
183 |
For full functionality: |
For full functionality: |
184 |
DBI from CPAN |
DBI from CPAN |
185 |
Tangram from CPAN |
DBD::mysql from CPAN |
186 |
Class::Tangram from CPAN |
Tangram 2.04 from CPAN (hmmm, 2.04 won't do in some cases) |
187 |
MySQL::Diff from http://adamspiers.org/computing/mysqldiff/ |
Tangram 2.05 from http://... (2.05 seems okay but there are also additional patches from our side) |
188 |
... and all their dependencies |
Class::Tangram from CPAN |
189 |
|
DBD::CSV from CPAN |
190 |
|
MySQL::Diff from http://adamspiers.org/computing/mysqldiff/ |
191 |
|
... and all their dependencies |
192 |
|
|
193 |
=cut |
=cut |
194 |
|
|
203 |
use Data::Storage::Locator; |
use Data::Storage::Locator; |
204 |
use Data::Dumper; |
use Data::Dumper; |
205 |
|
|
206 |
|
# TODO: wipe out! |
207 |
|
use DBI; |
208 |
|
|
209 |
# TODO: actually implement level (integrate with Log::Dispatch) |
# TODO: actually implement level (integrate with Log::Dispatch) |
210 |
my $TRACELEVEL = 0; |
my $TRACELEVEL = 0; |
211 |
|
|
234 |
# - Deep recursion on subroutine "Data::Storage::AUTOLOAD" |
# - Deep recursion on subroutine "Data::Storage::AUTOLOAD" |
235 |
# - Deep recursion on subroutine "Data::Storage::Handler::Abstract::AUTOLOAD" |
# - Deep recursion on subroutine "Data::Storage::Handler::Abstract::AUTOLOAD" |
236 |
# - Deep recursion on anonymous subroutine at [...] |
# - Deep recursion on anonymous subroutine at [...] |
237 |
# we also might filter log messages caused by logging itself in "advanced logging of AUTOLOAD calls" |
# we also might filter log messages caused by logging to itself in "advanced logging of AUTOLOAD calls" |
238 |
|
|
239 |
my $self = shift; |
my $self = shift; |
240 |
our $AUTOLOAD; |
our $AUTOLOAD; |
256 |
$logstring .= "\t" x $tabcount . "(AUTOLOAD)"; |
$logstring .= "\t" x $tabcount . "(AUTOLOAD)"; |
257 |
# TODO: only ok if logstring doesn't contain |
# TODO: only ok if logstring doesn't contain |
258 |
# e.g. "Data::Storage[Tangram]->insert(SystemEvent=HASH(0x5c0034c)) (AUTOLOAD)" |
# e.g. "Data::Storage[Tangram]->insert(SystemEvent=HASH(0x5c0034c)) (AUTOLOAD)" |
259 |
# but that would be way too specific as long as we don't have an abstract handler for this ;) |
# but that would be _way_ too specific as long as we don't have an abstract handler for this ;) |
260 |
$logger->debug( $logstring ); |
$logger->debug( $logstring ); |
261 |
#print join('; ', @_); |
#print join('; ', @_); |
262 |
} |
} |
263 |
|
|
264 |
# filtering AUTOLOAD calls |
# filtering AUTOLOAD calls and first-time-touch of the actual storage impl |
265 |
if ($self->_filter_AUTOLOAD($method)) { |
if ($self->_filter_AUTOLOAD($method)) { |
266 |
#print "_accessStorage\n"; |
#print "_accessStorage\n"; |
267 |
$self->_accessStorage(); |
$self->_accessStorage(); |
321 |
#my @args = %{$self->{locator}}; |
#my @args = %{$self->{locator}}; |
322 |
my @args = (); |
my @args = (); |
323 |
|
|
324 |
# create new storage handle object, propagate arguments to handler |
# - create new storage handle object |
325 |
# pass locator by reference to be able to store status information in it |
# - propagate arguments to handler |
326 |
|
# - pass locator by reference to be able to store status- or meta-information in it |
327 |
$self->{STORAGEHANDLE} = $pkg->new( locator => $self->{locator}, @args ); |
$self->{STORAGEHANDLE} = $pkg->new( locator => $self->{locator}, @args ); |
328 |
|
|
329 |
} |
} |
357 |
} |
} |
358 |
|
|
359 |
sub removeLogDispatchHandler { |
sub removeLogDispatchHandler { |
360 |
|
my $self = shift; |
361 |
my $self = shift; |
my $name = shift; |
362 |
my $name = shift; |
#my $logger = shift; |
363 |
#my $logger = shift; |
$logger->remove($name); |
|
|
|
|
$logger->remove($name); |
|
|
|
|
364 |
} |
} |
365 |
|
|
366 |
sub getDbName { |
sub getDbName { |
371 |
return $database_name; |
return $database_name; |
372 |
} |
} |
373 |
|
|
|
sub testDsn { |
|
|
my $self = shift; |
|
|
my $dsn = $self->{locator}->{dbi}->{dsn}; |
|
|
my $result; |
|
|
if ( my $dbh = DBI->connect($dsn, '', '', { |
|
|
PrintError => 0, |
|
|
} ) ) { |
|
|
$dbh->disconnect(); |
|
|
return 1; |
|
|
} else { |
|
|
$logger->warning( __PACKAGE__ . "[$self->{locator}->{type}]" . "->testDsn(): " . "DBI-error: " . $DBI::errstr ); |
|
|
} |
|
|
} |
|
|
|
|
374 |
sub testAvailability { |
sub testAvailability { |
375 |
my $self = shift; |
my $self = shift; |
376 |
my $status = $self->testDsn(); |
my $status = $self->testDsn(); |
378 |
return $status; |
return $status; |
379 |
} |
} |
380 |
|
|
381 |
sub createDb { |
sub isConnected { |
382 |
my $self = shift; |
my $self = shift; |
383 |
my $dsn = $self->{locator}->{dbi}->{dsn}; |
# TODO: REVIEW! |
384 |
|
return 1 if $self->{STORAGEHANDLE}; |
385 |
$logger->debug( __PACKAGE__ . "->createDb( dsn $dsn )" ); |
} |
|
|
|
|
$dsn =~ s/database=(.+?);//; |
|
|
my $database_name = $1; |
|
386 |
|
|
387 |
my $ok; |
sub testDsn { |
388 |
|
my $self = shift; |
389 |
|
my $dsn = $self->{locator}->{dbi}->{dsn}; |
390 |
|
my $result; |
391 |
if ( my $dbh = DBI->connect($dsn, '', '', { |
if ( my $dbh = DBI->connect($dsn, '', '', { |
392 |
PrintError => 0, |
PrintError => 0, |
393 |
} ) ) { |
} ) ) { |
394 |
if ($database_name) { |
|
395 |
if ($dbh->do("CREATE DATABASE $database_name;")) { |
# TODO: REVIEW |
|
$ok = 1; |
|
|
} |
|
|
} |
|
396 |
$dbh->disconnect(); |
$dbh->disconnect(); |
397 |
|
|
398 |
|
return 1; |
399 |
|
} else { |
400 |
|
$logger->warning( __PACKAGE__ . "[$self->{locator}->{type}]" . "->testDsn(): " . "DBI-error: " . $DBI::errstr ); |
401 |
} |
} |
|
|
|
|
return $ok; |
|
|
|
|
402 |
} |
} |
403 |
|
|
404 |
sub dropDb { |
sub dropDb { |
420 |
$ok = 1; |
$ok = 1; |
421 |
} |
} |
422 |
} |
} |
423 |
|
|
424 |
$dbh->disconnect(); |
$dbh->disconnect(); |
425 |
|
|
426 |
} |
} |
427 |
|
|
428 |
return $ok; |
return $ok; |
429 |
} |
} |
430 |
|
|
|
sub isConnected { |
|
|
my $self = shift; |
|
|
return 1 if $self->{STORAGEHANDLE}; |
|
|
} |
|
|
|
|
431 |
1; |
1; |
432 |
__END__ |
__END__ |
433 |
|
|
434 |
|
|
435 |
=head1 DESCRIPTION |
=head1 DESCRIPTION |
436 |
|
|
437 |
Data::Storage is module for a accessing various "data structures" stored inside |
=head2 Data::Storage |
|
various "data containers". It sits on top of DBI and/or Tangram. |
|
438 |
|
|
439 |
|
Data::Storage is a module for accessing various "data structures / kinds of structured data" stored inside |
440 |
|
various "data containers". |
441 |
|
We tried to use the AdapterPattern (http://c2.com/cgi/wiki?AdapterPattern) to implement a wrapper-layer |
442 |
|
around core CPAN modules (Tangram, DBI). |
443 |
|
|
444 |
|
=head2 Why? |
445 |
|
|
446 |
|
You will get a better code-structure (not bad for later maintenance) in growing Perl code projects, |
447 |
|
especially when using multiple database connections at the same time. |
448 |
|
You will be able to switch between different _kinds_ of implementations used for storing data. |
449 |
|
Your code will use the very same API to access these storage layers. |
450 |
|
... implementation has to be changed for now |
451 |
|
Maybe you will be able to switch "on-the-fly" without changing any bits in code in the future.... |
452 |
|
... but that's not the focus |
453 |
|
|
454 |
=head1 AUTHORS / COPYRIGHT |
=head2 What else? |
455 |
|
|
456 |
The Data::Storage module is Copyright (c) 2002 Andreas Motl. |
Having this, we were able to do implement a generic data synchronization module more easy, |
457 |
All rights reserved. |
please look at Data::Transfer. |
458 |
|
|
459 |
You may distribute it under the terms of either the GNU General Public |
|
460 |
License or the Artistic License, as specified in the Perl README file. |
=head1 AUTHORS / COPYRIGHT |
461 |
|
|
462 |
|
The Data::Storage module is Copyright (c) 2002 Andreas Motl. |
463 |
|
All rights reserved. |
464 |
|
You may distribute it under the terms of either the GNU General Public |
465 |
|
License or the Artistic License, as specified in the Perl README file. |
466 |
|
|
467 |
|
|
468 |
=head1 ACKNOWLEDGEMENTS |
=head1 ACKNOWLEDGEMENTS |
469 |
|
|
470 |
Larry Wall for Perl, Tim Bunce for DBI, Jean-Louis Leroy for Tangram and Set::Object, |
Larry Wall for Perl, Tim Bunce for DBI, Jean-Louis Leroy for Tangram and Set::Object, |
471 |
Sam Vilain for Class::Tangram, Adam Spiers for MySQL::Diff and all contributors. |
Sam Vilain for Class::Tangram, Jochen Wiedmann and Jeff Zucker for DBD::CSV & Co., |
472 |
|
Adam Spiers for MySQL::Diff and all contributors. |
473 |
|
|
474 |
|
|
475 |
=head1 SUPPORT / WARRANTY |
=head1 SUPPORT / WARRANTY |
476 |
|
|
477 |
Data::Storage is free software. IT COMES WITHOUT WARRANTY OF ANY KIND. |
Data::Storage is free software. IT COMES WITHOUT WARRANTY OF ANY KIND. |
478 |
|
|
479 |
|
|
480 |
=head1 TODO |
=head1 TODO |
481 |
|
|
482 |
|
|
483 |
=head2 Handle the following errors/cases: |
=head2 BUGS |
484 |
|
|
485 |
=head3 "DBI-Error [Tangram]: DBD::mysql::st execute failed: Unknown column 't1.requestdump' in 'field list'" |
"DBI-Error [Tangram]: DBD::mysql::st execute failed: Unknown column 't1.requestdump' in 'field list'" |
486 |
|
|
487 |
... occours when operating on object-attributes not introduced yet: |
... occours when operating on object-attributes not introduced yet: |
488 |
this should be detected and appended/replaced through: |
this should be detected and appended/replaced through: |
489 |
"Schema-Error detected, maybe (just) an inconsistency. |
"Schema-Error detected, maybe (just) an inconsistency. |
490 |
Please check if your declaration in schema-module "a" matches structure in database "b" or try to run" |
Please check if your declaration in schema-module "a" matches structure in database "b" or try to run" |
491 |
db_setup.pl --dbkey=import --action=deploy |
db_setup.pl --dbkey=import --action=deploy |
492 |
|
|
493 |
=head3 Compare schema (structure diff) with database ... |
|
494 |
|
Compare schema (structure diff) with database ... |
495 |
|
|
496 |
... when issuing "db_setup.pl --dbkey=import --action=deploy" |
... when issuing "db_setup.pl --dbkey=import --action=deploy" |
497 |
on a database with an already deployed schema, use an additional "--update" then |
on a database with an already deployed schema, use an additional "--update" then |
520 |
automatically and this is believed to be the most common case under normal circumstances. |
automatically and this is believed to be the most common case under normal circumstances. |
521 |
|
|
522 |
|
|
523 |
=head2 Introduce some features: |
=head2 FEATURES |
524 |
|
|
525 |
- Get this stuff together with UML (Unified Modeling Language) and/or standards from ODMG. |
- Get this stuff together with UML (Unified Modeling Language) and/or standards from ODMG. |
526 |
- Make it possible to load/save schemas in XMI (XML Metadata Interchange), |
- Make it possible to load/save schemas in XMI (XML Metadata Interchange), |
528 |
Integrate/bundle this with a web-/html-based UML modeling tool or |
Integrate/bundle this with a web-/html-based UML modeling tool or |
529 |
some other interesting stuff like the "Co-operative UML Editor" from Uni Darmstadt. (web-/java-based) |
some other interesting stuff like the "Co-operative UML Editor" from Uni Darmstadt. (web-/java-based) |
530 |
- Enable Round Trip Engineering. Keep code and diagrams in sync. Don't annoy/bother the programmers. |
- Enable Round Trip Engineering. Keep code and diagrams in sync. Don't annoy/bother the programmers. |
531 |
- Add some more handlers: |
- Add support for some more handlers/locators to be able to |
532 |
- look at DBD::CSV, Text::CSV, XML::CSV, XML::Excel |
access the following standards/protocols/interfaces/programs/apis transparently: |
533 |
- Add some more locations/locators: |
+ DBD::CSV (via Data::Storage::Handler::DBI) |
534 |
- PerlDAV: http://www.webdav.org/perldav/ |
(-) Text::CSV, XML::CSV, XML::Excel |
535 |
- Move to t3, use InCASE |
- MAPI |
536 |
|
- LDAP |
537 |
|
- DAV (look at PerlDAV: http://www.webdav.org/perldav/) |
538 |
|
- Mbox (use formail for seperating/splitting entries/nodes) |
539 |
|
- Cyrus (cyrdeliver - what about cyrretrieve (export)???) |
540 |
|
- use File::DiffTree, use File::Compare |
541 |
|
- Hibernate |
542 |
|
- "Win32::UserAccountDb" |
543 |
|
- "*nix::UserAccountDb" |
544 |
|
- .wab - files (Windows Address Book) |
545 |
|
- .pst - files (Outlook Post Storage?) |
546 |
|
- XML (e.g. via XML::Simple?) |
547 |
|
- Move to t3, look at InCASE |
548 |
|
- some kind of security layer for methods/objects |
549 |
|
- acls (stored via tangram/ldap?) for functions, methods and objects (entity- & data!?) |
550 |
|
- where are the hooks needed then? |
551 |
|
- is Data::Storage & Co. okay, or do we have to touch the innards of DBI and/or Tangram? |
552 |
|
- an attempt to start could be: |
553 |
|
- 'sub getACLByObjectId($id, $context)' |
554 |
|
- 'sub getACLByMethodname($id, $context)' |
555 |
|
- 'sub getACLByName($id, $context)' |
556 |
|
( would require a kinda registry to look up these very names pointing to arbitrary locations (code, data, ...) ) |
557 |
|
|
558 |
|
|
559 |
|
|
560 |
=head3 Links: |
=head3 LINKS / REFERENCES |
561 |
|
|
562 |
Specs: |
Specs: |
563 |
UML 1.3 Spec: http://cgi.omg.org/cgi-bin/doc?ad/99-06-08.pdf |
UML 1.3 Spec: http://cgi.omg.org/cgi-bin/doc?ad/99-06-08.pdf |