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 |
# Revision 1.4 2002/11/17 08:46:42 jonen |
20 |
# + wrapped eval around DBI->connect to prevent deaths |
# + wrapped eval around DBI->connect to prevent deaths |
21 |
# |
# |
42 |
|
|
43 |
use DBI; |
use DBI; |
44 |
use Data::Dumper; |
use Data::Dumper; |
45 |
use libdb; |
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 |
|
|
97 |
|
|
98 |
$logger->debug( __PACKAGE__ . "->configureCOREHANDLE" ); |
$logger->debug( __PACKAGE__ . "->configureCOREHANDLE" ); |
99 |
|
|
100 |
# apply configured modifications |
return if !$self->{COREHANDLE}; |
101 |
|
|
102 |
|
# apply configured modifications to DBI-handle |
103 |
if (exists $self->{locator}->{dbi}->{trace_level} && exists $self->{locator}->{dbi}->{trace_file}) { |
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}); |
$self->{COREHANDLE}->trace($self->{locator}->{dbi}->{trace_level}, $self->{locator}->{dbi}->{trace_file}); |
105 |
} |
} |
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 |
|
|
|
sub quoteSql { |
|
|
my $self = shift; |
|
|
my $string = shift; |
|
|
if ($string) { |
|
|
$string =~ s/'/\\'/g; |
|
|
} |
|
|
return $string; |
|
|
} |
|
|
|
|
|
|
|
150 |
sub getChildNodes { |
sub getChildNodes { |
|
|
|
151 |
my $self = shift; |
my $self = shift; |
152 |
my @nodes; |
my @nodes; |
|
|
|
153 |
$logger->debug( __PACKAGE__ . "->getChildNodes()" ); |
$logger->debug( __PACKAGE__ . "->getChildNodes()" ); |
|
|
|
154 |
if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) { |
if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) { |
155 |
my $dbname = getDbNameByDsn($self->{locator}->{dbi}->{dsn}); |
my $dbname = getDbNameByDsn($self->{locator}->{dbi}->{dsn}); |
156 |
my $key = "Tables_in_$dbname"; |
my $key = "Tables_in_$dbname"; |
157 |
while ( my $row = $result->_getNextEntry() ) { |
while ( my $row = $result->getNextEntry() ) { |
158 |
push @nodes, $row->{$key}; |
push @nodes, $row->{$key}; |
159 |
} |
} |
160 |
} |
} |
|
|
|
161 |
return \@nodes; |
return \@nodes; |
162 |
|
} |
163 |
|
|
164 |
|
sub getListUnfiltered { |
165 |
|
my $self = shift; |
166 |
|
my $nodename = shift; |
167 |
|
my @list; |
168 |
|
$logger->debug( __PACKAGE__ . "->getListUnfiltered( nodename => '" . $nodename . "' )" ); |
169 |
|
# get list of rows from rdbms by table name |
170 |
|
my $result = $self->sendCommand("SELECT * FROM $nodename"); |
171 |
|
while ( my $row = $result->getNextEntry() ) { |
172 |
|
push @list, $row; |
173 |
|
} |
174 |
|
return \@list; |
175 |
} |
} |
176 |
|
|
177 |
|
sub sendQuery { |
178 |
|
my $self = shift; |
179 |
|
my $query = shift; |
180 |
|
#my $sql = "SELECT cs FROM $self->{metainfo}->{$descent}->{node} WHERE $self->{metainfo}->{$descent}->{IdentProvider}->{arg}='$self->{entry}->{source}->{ident}';"; |
181 |
|
#my $result = $self->{metainfo}->{$descent}->{storage}->sendCommand($sql); |
182 |
|
my @crits; |
183 |
|
foreach (@{$query->{criterias}}) { |
184 |
|
my $op = ''; |
185 |
|
$op = '=' if lc $_->{op} eq 'eq'; |
186 |
|
push @crits, "$_->{key}$op'$_->{val}'"; |
187 |
|
} |
188 |
|
my $subnodes = {}; |
189 |
|
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 |
package Data::Storage::Result::DBI; |
# 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 |
use strict; |
$logger->debug( __PACKAGE__ . "->createDb( dsn $dsn )" ); |
|
use warnings; |
|
219 |
|
|
220 |
use base ("Data::Storage::Result"); |
# 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 |
sub DESTROY { |
# flag to indicate goodness |
227 |
my $self = shift; |
my $ok; |
228 |
#$logger->debug( __PACKAGE__ . "->" . "DESTROY" ); |
|
229 |
$self->{RESULTHANDLE} && $self->{RESULTHANDLE}->finish(); |
# 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 |
|
if ($database_name) { |
234 |
|
if ($dbh->do("CREATE DATABASE $database_name")) { |
235 |
|
$ok = 1; |
236 |
|
} |
237 |
|
} |
238 |
|
|
239 |
sub _getNextEntry { |
$dbh->disconnect(); |
|
my $self = shift; |
|
|
$self->{RESULTHANDLE} && return $self->{RESULTHANDLE}->fetchrow_hashref; |
|
|
} |
|
240 |
|
|
241 |
|
} |
242 |
|
|
243 |
|
return $ok; |
244 |
|
|
245 |
|
} |
246 |
|
|
247 |
1; |
1; |