1 |
################################# |
2 |
# |
3 |
# $Id: Storage.pm,v 1.2 2002/10/17 00:04:29 joko Exp $ |
4 |
# |
5 |
# $Log: Storage.pm,v $ |
6 |
# Revision 1.2 2002/10/17 00:04:29 joko |
7 |
# + sub createDb |
8 |
# + sub isConnected |
9 |
# + bugfixes regarding "deep recursion" stuff |
10 |
# |
11 |
# Revision 1.1 2002/10/10 03:43:12 cvsjoko |
12 |
# + new |
13 |
# |
14 |
# |
15 |
################################# |
16 |
|
17 |
# aim_V1: should encapsulate Tangram, DBI, DBD::CSV and LWP:: to access them in an unordinary way ;) |
18 |
# aim_V2: introduce a generic layered structure, refactor *SUBLAYER*-stuff, make (e.g.) this possible: |
19 |
# - Perl Data::Storage[DBD::CSV] -> Perl LWP:: -> Internet HTTP/FTP/* -> Host Daemon -> csv-file |
20 |
|
21 |
package Data::Storage; |
22 |
|
23 |
use strict; |
24 |
use warnings; |
25 |
|
26 |
use Data::Storage::Locator; |
27 |
|
28 |
# get logger instance |
29 |
my $logger = Log::Dispatch::Config->instance; |
30 |
|
31 |
sub new { |
32 |
my $invocant = shift; |
33 |
my $class = ref($invocant) || $invocant; |
34 |
#my @args = normalizeArgs(@_); |
35 |
|
36 |
my $arg_locator = shift; |
37 |
my $arg_options = shift; |
38 |
|
39 |
#my $self = { STORAGEHANDLE => undef, @_ }; |
40 |
my $self = { STORAGEHANDLE => undef, locator => $arg_locator, options => $arg_options }; |
41 |
$logger->debug( __PACKAGE__ . "[$self->{locator}->{type}]" . "->new(@_)" ); |
42 |
return bless $self, $class; |
43 |
} |
44 |
|
45 |
sub AUTOLOAD { |
46 |
|
47 |
# since this is a core function acting as dispatcher to $self->{STORAGEHANDLE}, |
48 |
# some sophisticated handling and filtering is needed to avoid things like |
49 |
# - Deep recursion on subroutine "Data::Storage::AUTOLOAD" |
50 |
# - Deep recursion on subroutine "Data::Storage::Handler::Abstract::AUTOLOAD" |
51 |
# - Deep recursion on anonymous subroutine at [...] |
52 |
# we also might filter log messages caused by logging itself in "advanced logging of AUTOLOAD calls" |
53 |
|
54 |
my $self = shift; |
55 |
our $AUTOLOAD; |
56 |
|
57 |
# ->DESTROY would - if not declared - trigger an AUTOLOAD also |
58 |
return if $AUTOLOAD =~ m/::DESTROY$/; |
59 |
|
60 |
my $method = $AUTOLOAD; |
61 |
$method =~ s/^.*:://; |
62 |
|
63 |
# advanced logging of AUTOLOAD calls |
64 |
my $logstring = ""; |
65 |
$logstring .= __PACKAGE__ . "[$self->{locator}->{type}]" . "->" . $method; |
66 |
#print "count: ", $#_, "\n"; |
67 |
#$logstring .= Dumper(@_) if ($#_ != -1); |
68 |
my $tabcount = int( (80 - length($logstring)) / 10 ); |
69 |
$logstring .= "\t" x $tabcount . "(AUTOLOAD)"; |
70 |
# TODO: only ok if logstring doesn't contain |
71 |
# e.g. "Data::Storage[Tangram]->insert(SystemEvent=HASH(0x5c0034c)) (AUTOLOAD)" |
72 |
# but that would be way too specific as long as we don't have an abstract handler for this ;) |
73 |
$logger->debug( $logstring ); |
74 |
|
75 |
# filtering AUTOLOAD calls |
76 |
if ($self->_filter_AUTOLOAD($method)) { |
77 |
$self->_accessStorage(); |
78 |
$self->{STORAGEHANDLE}->$method(@_); |
79 |
} |
80 |
|
81 |
} |
82 |
|
83 |
sub _filter_AUTOLOAD { |
84 |
my $self = shift; |
85 |
my $method = shift; |
86 |
if ($self->{options}->{protected}) { |
87 |
if ($method eq 'disconnect') { |
88 |
return; |
89 |
} |
90 |
} |
91 |
return 1; |
92 |
} |
93 |
|
94 |
|
95 |
sub normalizeArgs { |
96 |
my %args = @_; |
97 |
if (!$args{dsn} && $args{meta}{dsn}) { |
98 |
$args{dsn} = $args{meta}{dsn}; |
99 |
} |
100 |
my @result = %args; |
101 |
return @result; |
102 |
} |
103 |
|
104 |
sub _accessStorage { |
105 |
my $self = shift; |
106 |
# TODO: to some tracelevel! |
107 |
$logger->debug( __PACKAGE__ . "[$self->{locator}->{type}]" . "->_accessStorage()" ); |
108 |
if (!$self->{STORAGEHANDLE}) { |
109 |
$self->_createStorageHandle(); |
110 |
} |
111 |
} |
112 |
|
113 |
sub _createStorageHandle { |
114 |
my $self = shift; |
115 |
|
116 |
my $type = $self->{locator}->{type}; |
117 |
$logger->debug( __PACKAGE__ . "[$type]" . "->_createStorageHandle()" ); |
118 |
|
119 |
my $pkg = "Data::Storage::Handler::" . $type . ""; |
120 |
|
121 |
# propagate args to handler |
122 |
# needs some more thoughts! (not only "dbi" to Tangram, when (in future) db is not more the common case) |
123 |
if ($type eq 'DBI') { |
124 |
use Data::Storage::Handler::DBI; |
125 |
#my @args = %{$self->{locator}->{dbi}}; |
126 |
my @args = %{$self->{locator}}; |
127 |
# create new storage handle |
128 |
$self->{STORAGEHANDLE} = $pkg->new( @args ); |
129 |
} |
130 |
if ($type eq 'Tangram') { |
131 |
use Data::Storage::Handler::Tangram; |
132 |
#$self->{STORAGEHANDLE} = $pkg->new( dsn => $self->{locator}->{dbi}->{dsn} ); |
133 |
#my @args = %{$self->{locator}->{dbi}}; |
134 |
my @args = %{$self->{locator}}; |
135 |
# create new storage handle |
136 |
$self->{STORAGEHANDLE} = $pkg->new( @args ); |
137 |
|
138 |
#$self->{STORAGEHANDLE_UNDERLYING} = $self->{STORAGEHANDLE}->getUnderlyingStorage(); |
139 |
#$self->{STORAGEHANDLE_UNDERLYING}->_configureCOREHANDLE(); |
140 |
} |
141 |
|
142 |
} |
143 |
|
144 |
sub addLogDispatchHandler { |
145 |
|
146 |
my $self = shift; |
147 |
my $name = shift; |
148 |
my $package = shift; |
149 |
my $logger1 = shift; |
150 |
my $objectCreator = shift; |
151 |
|
152 |
#$logger->add( Log::Dispatch::Tangram->new( name => $name, |
153 |
$logger->add( $package->new( name => $name, |
154 |
#min_level => 'debug', |
155 |
min_level => 'info', |
156 |
storage => $self, |
157 |
objectCreator => $objectCreator, |
158 |
fields => { |
159 |
message => 'usermsg', |
160 |
timestamp => 'stamp', |
161 |
level => 'level', |
162 |
name => 'code', |
163 |
}, |
164 |
filter_patterns => [ '->insert\(SystemEvent=' ], |
165 |
#filter_patterns => [ 'SystemEvent' ], |
166 |
|
167 |
#format => '[%d] [%p] %m%n', |
168 |
) ); |
169 |
|
170 |
} |
171 |
|
172 |
sub removeLogDispatchHandler { |
173 |
|
174 |
my $self = shift; |
175 |
my $name = shift; |
176 |
#my $logger = shift; |
177 |
|
178 |
$logger->remove($name); |
179 |
|
180 |
} |
181 |
|
182 |
sub getDbName { |
183 |
my $self = shift; |
184 |
my $dsn = $self->{locator}->{dbi}->{dsn}; |
185 |
$dsn =~ m/database=(.+?);/; |
186 |
my $database_name = $1; |
187 |
return $database_name; |
188 |
} |
189 |
|
190 |
sub testDsn { |
191 |
my $self = shift; |
192 |
my $dsn = $self->{locator}->{dbi}->{dsn}; |
193 |
my $result; |
194 |
if ( my $dbh = DBI->connect($dsn, '', '', { |
195 |
PrintError => 0, |
196 |
} ) ) { |
197 |
$dbh->disconnect(); |
198 |
return 1; |
199 |
} else { |
200 |
$logger->error( __PACKAGE__ . "[$self->{locator}->{type}]" . "->testDsn(): " . "DBI-error: " . $DBI::errstr ); |
201 |
} |
202 |
} |
203 |
|
204 |
sub createDb { |
205 |
my $self = shift; |
206 |
my $dsn = $self->{locator}->{dbi}->{dsn}; |
207 |
|
208 |
$logger->debug( __PACKAGE__ . "->createDb( dsn $dsn )" ); |
209 |
|
210 |
$dsn =~ s/database=(.+?);//; |
211 |
my $database_name = $1; |
212 |
|
213 |
my $ok; |
214 |
|
215 |
if ( my $dbh = DBI->connect($dsn, '', '', { |
216 |
PrintError => 0, |
217 |
} ) ) { |
218 |
if ($database_name) { |
219 |
if ($dbh->do("CREATE DATABASE $database_name;")) { |
220 |
$ok = 1; |
221 |
} |
222 |
} |
223 |
$dbh->disconnect(); |
224 |
} |
225 |
|
226 |
return $ok; |
227 |
|
228 |
} |
229 |
|
230 |
sub dropDb { |
231 |
my $self = shift; |
232 |
my $dsn = $self->{locator}->{dbi}->{dsn}; |
233 |
|
234 |
$logger->debug( __PACKAGE__ . "->dropDb( dsn $dsn )" ); |
235 |
|
236 |
$dsn =~ s/database=(.+?);//; |
237 |
my $database_name = $1; |
238 |
|
239 |
my $ok; |
240 |
|
241 |
if ( my $dbh = DBI->connect($dsn, '', '', { |
242 |
PrintError => 0, |
243 |
} ) ) { |
244 |
if ($database_name) { |
245 |
if ($dbh->do("DROP DATABASE $database_name;")) { |
246 |
$ok = 1; |
247 |
} |
248 |
} |
249 |
$dbh->disconnect(); |
250 |
} |
251 |
|
252 |
return $ok; |
253 |
} |
254 |
|
255 |
sub isConnected { |
256 |
my $self = shift; |
257 |
return 1 if $self->{STORAGEHANDLE}; |
258 |
} |
259 |
|
260 |
1; |