1 |
################################# |
2 |
# |
3 |
# $Id: DBI.pm,v 1.4 2002/11/17 08:46:42 jonen Exp $ |
4 |
# |
5 |
# $Log: DBI.pm,v $ |
6 |
# Revision 1.4 2002/11/17 08:46:42 jonen |
7 |
# + wrapped eval around DBI->connect to prevent deaths |
8 |
# |
9 |
# Revision 1.3 2002/11/17 06:34:39 joko |
10 |
# + locator metadata can now be reached via ->{locator} |
11 |
# - sub hash2sql now taken from libdb |
12 |
# |
13 |
# Revision 1.2 2002/10/25 11:43:27 joko |
14 |
# + enhanced robustness |
15 |
# + more logging for debug-levels |
16 |
# |
17 |
# Revision 1.1 2002/10/10 03:44:07 cvsjoko |
18 |
# + new |
19 |
# |
20 |
# |
21 |
################################# |
22 |
|
23 |
package Data::Storage::Handler::DBI; |
24 |
|
25 |
use strict; |
26 |
use warnings; |
27 |
|
28 |
use base ("Data::Storage::Handler::Abstract"); |
29 |
|
30 |
use DBI; |
31 |
use Data::Dumper; |
32 |
use libdb qw( getDbNameByDsn hash2Sql ); |
33 |
use Data::Storage::Result::DBI; |
34 |
|
35 |
# get logger instance |
36 |
my $logger = Log::Dispatch::Config->instance; |
37 |
|
38 |
|
39 |
sub getMetaInfo { |
40 |
my $self = shift; |
41 |
$logger->debug( __PACKAGE__ . "->getMetaInfo()" ); |
42 |
return { |
43 |
'disconnectMethod' => 'disconnect', |
44 |
}; |
45 |
} |
46 |
|
47 |
sub connect { |
48 |
|
49 |
my $self = shift; |
50 |
|
51 |
# create handle |
52 |
if ( my $dsn = $self->{locator}->{dbi}->{dsn} ) { |
53 |
#if ( my $dsn = $self->{locator}->{dsn} ) { |
54 |
$logger->debug( __PACKAGE__ . "->connect( dsn $dsn )" ); |
55 |
|
56 |
# HACK: |
57 |
# set errorhandler before actually calling DBI->connect |
58 |
# in order to catch errors from the very beginning |
59 |
#DBI->{HandleError} = $self->{dbi}->{HandleError}; |
60 |
|
61 |
#use Data::Dumper; print Dumper($self->{dbi}); |
62 |
|
63 |
eval { |
64 |
$self->{COREHANDLE} = DBI->connect( $dsn, '', '', $self->{locator}->{dbi} ); |
65 |
if (!$self->{COREHANDLE}) { |
66 |
$logger->warning( __PACKAGE__ . "->connect failed: " . DBI::errstr ); |
67 |
return; |
68 |
} |
69 |
}; |
70 |
$logger->warning( __PACKAGE__ . "->connect failed: " . $@ ) if $@; |
71 |
|
72 |
} |
73 |
$self->configureCOREHANDLE(); |
74 |
|
75 |
$self->{locator}->{status}->{connected} = 1; |
76 |
|
77 |
return 1; |
78 |
|
79 |
} |
80 |
|
81 |
sub configureCOREHANDLE { |
82 |
|
83 |
my $self = shift; |
84 |
|
85 |
$logger->debug( __PACKAGE__ . "->configureCOREHANDLE" ); |
86 |
|
87 |
# apply configured modifications to DBI-handle |
88 |
if (exists $self->{locator}->{dbi}->{trace_level} && exists $self->{locator}->{dbi}->{trace_file}) { |
89 |
$self->{COREHANDLE}->trace($self->{locator}->{dbi}->{trace_level}, $self->{locator}->{dbi}->{trace_file}); |
90 |
} |
91 |
if (exists $self->{locator}->{dbi}->{RaiseError}) { |
92 |
$self->{COREHANDLE}->{RaiseError} = $self->{locator}->{dbi}->{RaiseError}; |
93 |
} |
94 |
if (exists $self->{locator}->{dbi}->{PrintError}) { |
95 |
$self->{COREHANDLE}->{PrintError} = $self->{locator}->{dbi}->{PrintError}; |
96 |
} |
97 |
if (exists $self->{locator}->{dbi}->{HandleError}) { |
98 |
$self->{COREHANDLE}->{HandleError} = $self->{locator}->{dbi}->{HandleError}; |
99 |
} |
100 |
|
101 |
} |
102 |
|
103 |
sub _sendSql { |
104 |
my $self = shift; |
105 |
my $sql = shift; |
106 |
|
107 |
# two-level handling for implicit connect: |
108 |
# if there's no corehandle ... |
109 |
if (!$self->{COREHANDLE}) { |
110 |
# ... try to connect, but ... |
111 |
$self->connect(); |
112 |
# ... if this still fails, there's something wrong probably, so we won't continue |
113 |
if (!$self->{COREHANDLE}) { |
114 |
return; |
115 |
} |
116 |
} |
117 |
|
118 |
#print "prepare sql: $sql\n"; |
119 |
|
120 |
my $sth = $self->{COREHANDLE}->prepare($sql); |
121 |
$sth->execute(); |
122 |
return $sth; |
123 |
} |
124 |
|
125 |
sub sendCommand { |
126 |
my $self = shift; |
127 |
my $command = shift; |
128 |
# TODO: when tracing: yes, do actually log this |
129 |
#$logger->debug( __PACKAGE__ . "->sendCommand( command $command )" ); |
130 |
my $cmdHandle = $self->_sendSql($command); |
131 |
my $result = Data::Storage::Result::DBI->new( RESULTHANDLE => $cmdHandle ); |
132 |
return $result; |
133 |
} |
134 |
|
135 |
sub getChildNodes { |
136 |
my $self = shift; |
137 |
my @nodes; |
138 |
$logger->debug( __PACKAGE__ . "->getChildNodes()" ); |
139 |
if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) { |
140 |
my $dbname = getDbNameByDsn($self->{locator}->{dbi}->{dsn}); |
141 |
my $key = "Tables_in_$dbname"; |
142 |
while ( my $row = $result->getNextEntry() ) { |
143 |
push @nodes, $row->{$key}; |
144 |
} |
145 |
} |
146 |
return \@nodes; |
147 |
} |
148 |
|
149 |
sub getListUnfiltered { |
150 |
my $self = shift; |
151 |
my $nodename = shift; |
152 |
my @list; |
153 |
$logger->debug( __PACKAGE__ . "->getListUnfiltered( nodename => '" . $nodename . "' )" ); |
154 |
# get list of rows from rdbms by table name |
155 |
my $result = $self->sendCommand("SELECT * FROM $nodename"); |
156 |
while ( my $row = $result->getNextEntry() ) { |
157 |
push @list, $row; |
158 |
} |
159 |
return \@list; |
160 |
} |
161 |
|
162 |
sub sendQuery { |
163 |
my $self = shift; |
164 |
my $query = shift; |
165 |
#my $sql = "SELECT cs FROM $self->{metainfo}->{$descent}->{node} WHERE $self->{metainfo}->{$descent}->{IdentProvider}->{arg}='$self->{entry}->{source}->{ident}';"; |
166 |
#my $result = $self->{metainfo}->{$descent}->{storage}->sendCommand($sql); |
167 |
my @crits; |
168 |
foreach (@{$query->{criterias}}) { |
169 |
my $op = ''; |
170 |
$op = '=' if lc $_->{op} eq 'eq'; |
171 |
push @crits, "$_->{key}$op'$_->{val}'"; |
172 |
} |
173 |
my $subnodes = {}; |
174 |
map { $subnodes->{$_}++ } @{$query->{subnodes}}; |
175 |
# HACK: this is hardcoded ;( expand possibilities! |
176 |
my $crit = join(' AND ', @crits); |
177 |
my $sql = hash2Sql($query->{node}, $subnodes, 'SELECT', $crit); |
178 |
return $self->sendCommand($sql); |
179 |
} |
180 |
|
181 |
1; |