3 |
# $Id$ |
# $Id$ |
4 |
# |
# |
5 |
# $Log$ |
# $Log$ |
6 |
|
# Revision 1.5 2002/11/29 05:00:26 joko |
7 |
|
# + sub getListUnfiltered |
8 |
|
# + sub sendQuery |
9 |
|
# |
10 |
|
# Revision 1.4 2002/11/17 08:46:42 jonen |
11 |
|
# + wrapped eval around DBI->connect to prevent deaths |
12 |
|
# |
13 |
|
# Revision 1.3 2002/11/17 06:34:39 joko |
14 |
|
# + locator metadata can now be reached via ->{locator} |
15 |
|
# - sub hash2sql now taken from libdb |
16 |
|
# |
17 |
# Revision 1.2 2002/10/25 11:43:27 joko |
# Revision 1.2 2002/10/25 11:43:27 joko |
18 |
# + enhanced robustness |
# + enhanced robustness |
19 |
# + more logging for debug-levels |
# + more logging for debug-levels |
32 |
use base ("Data::Storage::Handler::Abstract"); |
use base ("Data::Storage::Handler::Abstract"); |
33 |
|
|
34 |
use DBI; |
use DBI; |
35 |
|
use Data::Dumper; |
36 |
|
use libdb qw( getDbNameByDsn hash2Sql ); |
37 |
|
use Data::Storage::Result::DBI; |
38 |
|
|
39 |
# get logger instance |
# get logger instance |
40 |
my $logger = Log::Dispatch::Config->instance; |
my $logger = Log::Dispatch::Config->instance; |
41 |
|
|
42 |
|
|
43 |
our $metainfo = { |
sub getMetaInfo { |
44 |
'disconnectMethod' => 'disconnect', |
my $self = shift; |
45 |
}; |
$logger->debug( __PACKAGE__ . "->getMetaInfo()" ); |
46 |
|
return { |
47 |
|
'disconnectMethod' => 'disconnect', |
48 |
|
}; |
49 |
|
} |
50 |
|
|
51 |
sub connect { |
sub connect { |
52 |
|
|
53 |
my $self = shift; |
my $self = shift; |
54 |
|
|
55 |
# create handle |
# create handle |
56 |
if ( my $dsn = $self->{dbi}->{dsn} ) { |
if ( my $dsn = $self->{locator}->{dbi}->{dsn} ) { |
57 |
|
#if ( my $dsn = $self->{locator}->{dsn} ) { |
58 |
$logger->debug( __PACKAGE__ . "->connect( dsn $dsn )" ); |
$logger->debug( __PACKAGE__ . "->connect( dsn $dsn )" ); |
59 |
|
|
60 |
# HACK: |
# HACK: |
64 |
|
|
65 |
#use Data::Dumper; print Dumper($self->{dbi}); |
#use Data::Dumper; print Dumper($self->{dbi}); |
66 |
|
|
67 |
$self->{COREHANDLE} = DBI->connect( |
eval { |
68 |
$dsn, '', '', { |
$self->{COREHANDLE} = DBI->connect( $dsn, '', '', $self->{locator}->{dbi} ); |
69 |
RaiseError => $self->{dbi}->{RaiseError}, |
if (!$self->{COREHANDLE}) { |
70 |
#RaiseError => 1, |
$logger->warning( __PACKAGE__ . "->connect failed: " . DBI::errstr ); |
71 |
PrintError => $self->{dbi}->{PrintError}, |
return; |
|
HandleError => $self->{dbi}->{HandleError}, |
|
72 |
} |
} |
73 |
); |
}; |
74 |
if (!$self->{COREHANDLE}) { |
$logger->warning( __PACKAGE__ . "->connect failed: " . $@ ) if $@; |
75 |
$logger->warning( __PACKAGE__ . "->connect failed: " . DBI::errstr ); |
|
|
return; |
|
|
} |
|
76 |
} |
} |
77 |
$self->configureCOREHANDLE(); |
$self->configureCOREHANDLE(); |
78 |
|
|
79 |
|
$self->{locator}->{status}->{connected} = 1; |
80 |
|
|
81 |
return 1; |
return 1; |
82 |
|
|
83 |
} |
} |
86 |
|
|
87 |
my $self = shift; |
my $self = shift; |
88 |
|
|
89 |
$logger->debug( __PACKAGE__ . "->_configureCOREHANDLE" ); |
$logger->debug( __PACKAGE__ . "->configureCOREHANDLE" ); |
90 |
|
|
91 |
# apply configured modifications |
# apply configured modifications to DBI-handle |
92 |
if (exists $self->{dbi}->{trace_level} && exists $self->{dbi}->{trace_file}) { |
if (exists $self->{locator}->{dbi}->{trace_level} && exists $self->{locator}->{dbi}->{trace_file}) { |
93 |
$self->{COREHANDLE}->trace($self->{dbi}->{trace_level}, $self->{dbi}->{trace_file}); |
$self->{COREHANDLE}->trace($self->{locator}->{dbi}->{trace_level}, $self->{locator}->{dbi}->{trace_file}); |
94 |
} |
} |
95 |
if (exists $self->{dbi}->{RaiseError}) { |
if (exists $self->{locator}->{dbi}->{RaiseError}) { |
96 |
$self->{COREHANDLE}->{RaiseError} = $self->{dbi}->{RaiseError}; |
$self->{COREHANDLE}->{RaiseError} = $self->{locator}->{dbi}->{RaiseError}; |
97 |
} |
} |
98 |
if (exists $self->{dbi}->{PrintError}) { |
if (exists $self->{locator}->{dbi}->{PrintError}) { |
99 |
$self->{COREHANDLE}->{PrintError} = $self->{dbi}->{PrintError}; |
$self->{COREHANDLE}->{PrintError} = $self->{locator}->{dbi}->{PrintError}; |
100 |
} |
} |
101 |
if (exists $self->{dbi}->{HandleError}) { |
if (exists $self->{locator}->{dbi}->{HandleError}) { |
102 |
$self->{COREHANDLE}->{HandleError} = $self->{dbi}->{HandleError}; |
$self->{COREHANDLE}->{HandleError} = $self->{locator}->{dbi}->{HandleError}; |
103 |
} |
} |
104 |
|
|
105 |
} |
} |
119 |
} |
} |
120 |
} |
} |
121 |
|
|
122 |
|
#print "prepare sql: $sql\n"; |
123 |
|
|
124 |
my $sth = $self->{COREHANDLE}->prepare($sql); |
my $sth = $self->{COREHANDLE}->prepare($sql); |
125 |
$sth->execute(); |
$sth->execute(); |
126 |
return $sth; |
return $sth; |
129 |
sub sendCommand { |
sub sendCommand { |
130 |
my $self = shift; |
my $self = shift; |
131 |
my $command = shift; |
my $command = shift; |
132 |
|
# TODO: when tracing: yes, do actually log this |
133 |
#$logger->debug( __PACKAGE__ . "->sendCommand( command $command )" ); |
#$logger->debug( __PACKAGE__ . "->sendCommand( command $command )" ); |
134 |
my $cmdHandle = $self->_sendSql($command); |
my $cmdHandle = $self->_sendSql($command); |
135 |
my $result = Data::Storage::Result::DBI->new( RESULTHANDLE => $cmdHandle ); |
my $result = Data::Storage::Result::DBI->new( RESULTHANDLE => $cmdHandle ); |
136 |
return $result; |
return $result; |
137 |
} |
} |
138 |
|
|
|
sub quoteSql { |
|
|
my $self = shift; |
|
|
my $string = shift; |
|
|
if ($string) { |
|
|
$string =~ s/'/\\'/g; |
|
|
} |
|
|
return $string; |
|
|
} |
|
|
|
|
|
sub hash2Sql { |
|
|
|
|
|
my $self = shift; |
|
|
|
|
|
my $table = shift; |
|
|
my $hash = shift; |
|
|
my $mode = shift; |
|
|
my $crit = shift; |
|
|
|
|
|
my $sql; |
|
|
if ($mode eq 'SQL_INSERT') { |
|
|
$sql = "INSERT INTO $table (#fields#) VALUES (#values#);"; |
|
|
} |
|
|
if ($mode eq 'SQL_UPDATE') { |
|
|
$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; |
|
|
} |
|
|
|
|
|
|
|
139 |
sub getChildNodes { |
sub getChildNodes { |
|
|
|
140 |
my $self = shift; |
my $self = shift; |
141 |
my @nodes; |
my @nodes; |
142 |
|
$logger->debug( __PACKAGE__ . "->getChildNodes()" ); |
143 |
if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) { |
if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) { |
144 |
while ( my $row = $result->_getNextEntry() ) { |
my $dbname = getDbNameByDsn($self->{locator}->{dbi}->{dsn}); |
145 |
push @nodes, $row; |
my $key = "Tables_in_$dbname"; |
146 |
|
while ( my $row = $result->getNextEntry() ) { |
147 |
|
push @nodes, $row->{$key}; |
148 |
} |
} |
149 |
} |
} |
|
|
|
150 |
return \@nodes; |
return \@nodes; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
package Data::Storage::Result::DBI; |
|
|
|
|
|
use strict; |
|
|
use warnings; |
|
|
|
|
|
use base ("Data::Storage::Result"); |
|
|
|
|
|
sub DESTROY { |
|
|
my $self = shift; |
|
|
#$logger->debug( __PACKAGE__ . "->" . "DESTROY" ); |
|
|
$self->{RESULTHANDLE} && $self->{RESULTHANDLE}->finish(); |
|
151 |
} |
} |
152 |
|
|
153 |
sub _getNextEntry { |
sub getListUnfiltered { |
154 |
my $self = shift; |
my $self = shift; |
155 |
$self->{RESULTHANDLE} && return $self->{RESULTHANDLE}->fetchrow_hashref; |
my $nodename = shift; |
156 |
|
my @list; |
157 |
|
$logger->debug( __PACKAGE__ . "->getListUnfiltered( nodename => '" . $nodename . "' )" ); |
158 |
|
# get list of rows from rdbms by table name |
159 |
|
my $result = $self->sendCommand("SELECT * FROM $nodename"); |
160 |
|
while ( my $row = $result->getNextEntry() ) { |
161 |
|
push @list, $row; |
162 |
|
} |
163 |
|
return \@list; |
164 |
|
} |
165 |
|
|
166 |
|
sub sendQuery { |
167 |
|
my $self = shift; |
168 |
|
my $query = shift; |
169 |
|
#my $sql = "SELECT cs FROM $self->{metainfo}->{$descent}->{node} WHERE $self->{metainfo}->{$descent}->{IdentProvider}->{arg}='$self->{entry}->{source}->{ident}';"; |
170 |
|
#my $result = $self->{metainfo}->{$descent}->{storage}->sendCommand($sql); |
171 |
|
my @crits; |
172 |
|
foreach (@{$query->{criterias}}) { |
173 |
|
my $op = ''; |
174 |
|
$op = '=' if lc $_->{op} eq 'eq'; |
175 |
|
push @crits, "$_->{key}$op'$_->{val}'"; |
176 |
|
} |
177 |
|
my $subnodes = {}; |
178 |
|
map { $subnodes->{$_}++ } @{$query->{subnodes}}; |
179 |
|
# HACK: this is hardcoded ;( expand possibilities! |
180 |
|
my $crit = join(' AND ', @crits); |
181 |
|
my $sql = hash2Sql($query->{node}, $subnodes, 'SELECT', $crit); |
182 |
|
return $self->sendCommand($sql); |
183 |
} |
} |
184 |
|
|
|
|
|
|
1; |
|
185 |
|
1; |