3 |
# $Id$ |
# $Id$ |
4 |
# |
# |
5 |
# $Log$ |
# $Log$ |
6 |
|
# Revision 1.8 2002/12/01 22:20:43 joko |
7 |
|
# + sub createDb (from Storage.pm) |
8 |
|
# |
9 |
|
# Revision 1.7 2002/12/01 07:09:09 joko |
10 |
|
# + sub getListFiltered (dummy redirecting to getListUnfiltered) |
11 |
|
# |
12 |
|
# Revision 1.6 2002/12/01 04:46:01 joko |
13 |
|
# + sub eraseAll |
14 |
|
# |
15 |
|
# Revision 1.5 2002/11/29 05:00:26 joko |
16 |
|
# + sub getListUnfiltered |
17 |
|
# + sub sendQuery |
18 |
|
# |
19 |
|
# Revision 1.4 2002/11/17 08:46:42 jonen |
20 |
|
# + wrapped eval around DBI->connect to prevent deaths |
21 |
|
# |
22 |
|
# Revision 1.3 2002/11/17 06:34:39 joko |
23 |
|
# + locator metadata can now be reached via ->{locator} |
24 |
|
# - sub hash2sql now taken from libdb |
25 |
|
# |
26 |
# Revision 1.2 2002/10/25 11:43:27 joko |
# Revision 1.2 2002/10/25 11:43:27 joko |
27 |
# + enhanced robustness |
# + enhanced robustness |
28 |
# + more logging for debug-levels |
# + more logging for debug-levels |
41 |
use base ("Data::Storage::Handler::Abstract"); |
use base ("Data::Storage::Handler::Abstract"); |
42 |
|
|
43 |
use DBI; |
use DBI; |
44 |
|
use Data::Dumper; |
45 |
|
use libdb qw( getDbNameByDsn hash2Sql ); |
46 |
|
use Data::Storage::Result::DBI; |
47 |
|
|
48 |
# get logger instance |
# get logger instance |
49 |
my $logger = Log::Dispatch::Config->instance; |
my $logger = Log::Dispatch::Config->instance; |
50 |
|
|
51 |
|
|
52 |
our $metainfo = { |
sub getMetaInfo { |
53 |
'disconnectMethod' => 'disconnect', |
my $self = shift; |
54 |
}; |
$logger->debug( __PACKAGE__ . "->getMetaInfo()" ); |
55 |
|
return { |
56 |
|
'disconnectMethod' => 'disconnect', |
57 |
|
}; |
58 |
|
} |
59 |
|
|
60 |
sub connect { |
sub connect { |
61 |
|
|
62 |
my $self = shift; |
my $self = shift; |
63 |
|
|
64 |
# create handle |
# create handle |
65 |
if ( my $dsn = $self->{dbi}->{dsn} ) { |
if ( my $dsn = $self->{locator}->{dbi}->{dsn} ) { |
66 |
|
#if ( my $dsn = $self->{locator}->{dsn} ) { |
67 |
$logger->debug( __PACKAGE__ . "->connect( dsn $dsn )" ); |
$logger->debug( __PACKAGE__ . "->connect( dsn $dsn )" ); |
68 |
|
|
69 |
# HACK: |
# HACK: |
73 |
|
|
74 |
#use Data::Dumper; print Dumper($self->{dbi}); |
#use Data::Dumper; print Dumper($self->{dbi}); |
75 |
|
|
76 |
$self->{COREHANDLE} = DBI->connect( |
eval { |
77 |
$dsn, '', '', { |
$self->{COREHANDLE} = DBI->connect( $dsn, '', '', $self->{locator}->{dbi} ); |
78 |
RaiseError => $self->{dbi}->{RaiseError}, |
if (!$self->{COREHANDLE}) { |
79 |
#RaiseError => 1, |
$logger->warning( __PACKAGE__ . "->connect failed: " . DBI::errstr ); |
80 |
PrintError => $self->{dbi}->{PrintError}, |
return; |
|
HandleError => $self->{dbi}->{HandleError}, |
|
81 |
} |
} |
82 |
); |
}; |
83 |
if (!$self->{COREHANDLE}) { |
$logger->warning( __PACKAGE__ . "->connect failed: " . $@ ) if $@; |
84 |
$logger->warning( __PACKAGE__ . "->connect failed: " . DBI::errstr ); |
|
|
return; |
|
|
} |
|
85 |
} |
} |
86 |
$self->configureCOREHANDLE(); |
$self->configureCOREHANDLE(); |
87 |
|
|
88 |
|
$self->{locator}->{status}->{connected} = 1; |
89 |
|
|
90 |
return 1; |
return 1; |
91 |
|
|
92 |
} |
} |
95 |
|
|
96 |
my $self = shift; |
my $self = shift; |
97 |
|
|
98 |
$logger->debug( __PACKAGE__ . "->_configureCOREHANDLE" ); |
$logger->debug( __PACKAGE__ . "->configureCOREHANDLE" ); |
99 |
|
|
100 |
# apply configured modifications |
return if !$self->{COREHANDLE}; |
101 |
if (exists $self->{dbi}->{trace_level} && exists $self->{dbi}->{trace_file}) { |
|
102 |
$self->{COREHANDLE}->trace($self->{dbi}->{trace_level}, $self->{dbi}->{trace_file}); |
# apply configured modifications to DBI-handle |
103 |
|
if (exists $self->{locator}->{dbi}->{trace_level} && exists $self->{locator}->{dbi}->{trace_file}) { |
104 |
|
$self->{COREHANDLE}->trace($self->{locator}->{dbi}->{trace_level}, $self->{locator}->{dbi}->{trace_file}); |
105 |
} |
} |
106 |
if (exists $self->{dbi}->{RaiseError}) { |
if (exists $self->{locator}->{dbi}->{RaiseError}) { |
107 |
$self->{COREHANDLE}->{RaiseError} = $self->{dbi}->{RaiseError}; |
$self->{COREHANDLE}->{RaiseError} = $self->{locator}->{dbi}->{RaiseError}; |
108 |
} |
} |
109 |
if (exists $self->{dbi}->{PrintError}) { |
if (exists $self->{locator}->{dbi}->{PrintError}) { |
110 |
$self->{COREHANDLE}->{PrintError} = $self->{dbi}->{PrintError}; |
$self->{COREHANDLE}->{PrintError} = $self->{locator}->{dbi}->{PrintError}; |
111 |
} |
} |
112 |
if (exists $self->{dbi}->{HandleError}) { |
if (exists $self->{locator}->{dbi}->{HandleError}) { |
113 |
$self->{COREHANDLE}->{HandleError} = $self->{dbi}->{HandleError}; |
$self->{COREHANDLE}->{HandleError} = $self->{locator}->{dbi}->{HandleError}; |
114 |
} |
} |
115 |
|
|
116 |
} |
} |
130 |
} |
} |
131 |
} |
} |
132 |
|
|
133 |
|
#print "prepare sql: $sql\n"; |
134 |
|
|
135 |
my $sth = $self->{COREHANDLE}->prepare($sql); |
my $sth = $self->{COREHANDLE}->prepare($sql); |
136 |
$sth->execute(); |
$sth->execute(); |
137 |
return $sth; |
return $sth; |
140 |
sub sendCommand { |
sub sendCommand { |
141 |
my $self = shift; |
my $self = shift; |
142 |
my $command = shift; |
my $command = shift; |
143 |
|
# TODO: when tracing: yes, do actually log this |
144 |
#$logger->debug( __PACKAGE__ . "->sendCommand( command $command )" ); |
#$logger->debug( __PACKAGE__ . "->sendCommand( command $command )" ); |
145 |
my $cmdHandle = $self->_sendSql($command); |
my $cmdHandle = $self->_sendSql($command); |
146 |
my $result = Data::Storage::Result::DBI->new( RESULTHANDLE => $cmdHandle ); |
my $result = Data::Storage::Result::DBI->new( RESULTHANDLE => $cmdHandle ); |
147 |
return $result; |
return $result; |
148 |
} |
} |
149 |
|
|
150 |
sub quoteSql { |
sub getChildNodes { |
151 |
my $self = shift; |
my $self = shift; |
152 |
my $string = shift; |
my @nodes; |
153 |
if ($string) { |
$logger->debug( __PACKAGE__ . "->getChildNodes()" ); |
154 |
$string =~ s/'/\\'/g; |
if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) { |
155 |
|
my $dbname = getDbNameByDsn($self->{locator}->{dbi}->{dsn}); |
156 |
|
my $key = "Tables_in_$dbname"; |
157 |
|
while ( my $row = $result->getNextEntry() ) { |
158 |
|
push @nodes, $row->{$key}; |
159 |
|
} |
160 |
} |
} |
161 |
return $string; |
return \@nodes; |
162 |
} |
} |
163 |
|
|
164 |
sub hash2Sql { |
sub getListUnfiltered { |
|
|
|
165 |
my $self = shift; |
my $self = shift; |
166 |
|
my $nodename = shift; |
167 |
my $table = shift; |
my @list; |
168 |
my $hash = shift; |
$logger->debug( __PACKAGE__ . "->getListUnfiltered( nodename => '" . $nodename . "' )" ); |
169 |
my $mode = shift; |
# get list of rows from rdbms by table name |
170 |
my $crit = shift; |
my $result = $self->sendCommand("SELECT * FROM $nodename"); |
171 |
|
while ( my $row = $result->getNextEntry() ) { |
172 |
my $sql; |
push @list, $row; |
|
if ($mode eq 'SQL_INSERT') { |
|
|
$sql = "INSERT INTO $table (#fields#) VALUES (#values#);"; |
|
173 |
} |
} |
174 |
if ($mode eq 'SQL_UPDATE') { |
return \@list; |
|
$sql = "UPDATE $table SET #fields-values# WHERE $crit;"; |
|
|
} |
|
|
|
|
|
my (@fields, @values); |
|
|
foreach my $key (keys %{$hash}) { |
|
|
push @fields, $key; |
|
|
push @values, $hash->{$key}; |
|
|
} |
|
|
# quote each element |
|
|
map { if (defined $_) { $_ = "'$_'" } else { $_ = "null" } } @values; |
|
|
|
|
|
my $fields = join(', ', @fields); |
|
|
my $values = join(', ', @values); |
|
|
my $fields_values = ''; |
|
|
my $fc = 0; |
|
|
foreach (@fields) { |
|
|
$fields_values .= $_ . '=' . $values[$fc] . ', '; |
|
|
$fc++; |
|
|
} |
|
|
$fields_values = substr($fields_values, 0, -2); |
|
|
|
|
|
$sql =~ s/#fields#/$fields/; |
|
|
$sql =~ s/#values#/$values/; |
|
|
$sql =~ s/#fields-values#/$fields_values/; |
|
|
|
|
|
return $sql; |
|
175 |
} |
} |
176 |
|
|
177 |
|
sub sendQuery { |
|
sub getChildNodes { |
|
|
|
|
178 |
my $self = shift; |
my $self = shift; |
179 |
my @nodes; |
my $query = shift; |
180 |
|
#my $sql = "SELECT cs FROM $self->{metainfo}->{$descent}->{node} WHERE $self->{metainfo}->{$descent}->{IdentProvider}->{arg}='$self->{entry}->{source}->{ident}';"; |
181 |
if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) { |
#my $result = $self->{metainfo}->{$descent}->{storage}->sendCommand($sql); |
182 |
while ( my $row = $result->_getNextEntry() ) { |
my @crits; |
183 |
push @nodes, $row; |
foreach (@{$query->{criterias}}) { |
184 |
} |
my $op = ''; |
185 |
|
$op = '=' if lc $_->{op} eq 'eq'; |
186 |
|
push @crits, "$_->{key}$op'$_->{val}'"; |
187 |
} |
} |
188 |
|
my $subnodes = {}; |
189 |
return \@nodes; |
map { $subnodes->{$_}++ } @{$query->{subnodes}}; |
190 |
|
# HACK: this is hardcoded ;( expand possibilities! |
191 |
|
my $crit = join(' AND ', @crits); |
192 |
|
my $sql = hash2Sql($query->{node}, $subnodes, 'SELECT', $crit); |
193 |
|
return $self->sendCommand($sql); |
194 |
|
} |
195 |
|
|
196 |
|
sub eraseAll { |
197 |
|
my $self = shift; |
198 |
|
my $classname = shift; |
199 |
|
my $sql = "DELETE FROM $classname"; |
200 |
|
$self->sendCommand($sql); |
201 |
} |
} |
202 |
|
|
203 |
|
# TODO: actually implement the filtering functionality using $this->sendQuery |
204 |
|
sub getListFiltered { |
205 |
|
my $self = shift; |
206 |
|
my $nodename = shift; |
207 |
|
return $self->getListUnfiltered($nodename); |
208 |
|
} |
209 |
|
|
210 |
|
# TODO: do this via a parametrized "$self->connect(<connect just to database server - don't select database>)" |
211 |
|
sub createDb { |
212 |
|
|
213 |
|
my $self = shift; |
214 |
|
|
215 |
|
# get dsn from Data::Storage::Locator instance |
216 |
|
my $dsn = $self->{locator}->{dbi}->{dsn}; |
217 |
|
|
218 |
|
$logger->debug( __PACKAGE__ . "->createDb( dsn $dsn )" ); |
219 |
|
|
220 |
package Data::Storage::Result::DBI; |
# remove database setting from dsn-string |
221 |
|
$dsn =~ s/database=(.+?);//; |
222 |
|
|
223 |
|
# remember extracted database name to know what actually to create right now |
224 |
|
my $database_name = $1; |
225 |
|
|
226 |
use strict; |
# flag to indicate goodness |
227 |
use warnings; |
my $ok; |
228 |
|
|
229 |
|
# connect to database server - don't select/use any specific database |
230 |
|
#if ( my $dbh = DBI->connect($dsn, '', '', { PrintError => 0 } ) ) { |
231 |
|
if ( my $dbh = DBI->connect($dsn, '', '', $self->{locator}->{dbi} ) ) { |
232 |
|
|
233 |
use base ("Data::Storage::Result"); |
if ($database_name) { |
234 |
|
if ($dbh->do("CREATE DATABASE $database_name")) { |
235 |
|
$ok = 1; |
236 |
|
} |
237 |
|
} |
238 |
|
|
239 |
sub DESTROY { |
$dbh->disconnect(); |
|
my $self = shift; |
|
|
#$logger->debug( __PACKAGE__ . "->" . "DESTROY" ); |
|
|
$self->{RESULTHANDLE} && $self->{RESULTHANDLE}->finish(); |
|
|
} |
|
240 |
|
|
241 |
sub _getNextEntry { |
} |
242 |
my $self = shift; |
|
243 |
$self->{RESULTHANDLE} && return $self->{RESULTHANDLE}->fetchrow_hashref; |
return $ok; |
244 |
|
|
245 |
} |
} |
246 |
|
|
|
|
|
|
1; |
|
247 |
|
1; |