/[cvs]/nfo/perl/libs/Data/Storage/Handler/DBI.pm
ViewVC logotype

Contents of /nfo/perl/libs/Data/Storage/Handler/DBI.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.10 - (show annotations)
Sun Dec 15 02:02:22 2002 UTC (21 years, 6 months ago) by joko
Branch: MAIN
Changes since 1.9: +9 -1 lines
+ fixed logging-message

1 #################################
2 #
3 # $Id: DBI.pm,v 1.9 2002/12/05 07:58:20 joko Exp $
4 #
5 # $Log: DBI.pm,v $
6 # Revision 1.9 2002/12/05 07:58:20 joko
7 # + now using Tie::SecureHash as a base for the COREHANDLE
8 # + former public COREHANDLE becomes private _COREHANDLE now
9 #
10 # Revision 1.8 2002/12/01 22:20:43 joko
11 # + sub createDb (from Storage.pm)
12 #
13 # Revision 1.7 2002/12/01 07:09:09 joko
14 # + sub getListFiltered (dummy redirecting to getListUnfiltered)
15 #
16 # Revision 1.6 2002/12/01 04:46:01 joko
17 # + sub eraseAll
18 #
19 # Revision 1.5 2002/11/29 05:00:26 joko
20 # + sub getListUnfiltered
21 # + sub sendQuery
22 #
23 # Revision 1.4 2002/11/17 08:46:42 jonen
24 # + wrapped eval around DBI->connect to prevent deaths
25 #
26 # Revision 1.3 2002/11/17 06:34:39 joko
27 # + locator metadata can now be reached via ->{locator}
28 # - sub hash2sql now taken from libdb
29 #
30 # Revision 1.2 2002/10/25 11:43:27 joko
31 # + enhanced robustness
32 # + more logging for debug-levels
33 #
34 # Revision 1.1 2002/10/10 03:44:07 cvsjoko
35 # + new
36 #
37 #
38 #################################
39
40 package Data::Storage::Handler::DBI;
41
42 use strict;
43 use warnings;
44
45 use base ("Data::Storage::Handler::Abstract");
46
47 use DBI;
48 use Data::Dumper;
49 use libdb qw( getDbNameByDsn hash2Sql );
50 use Data::Storage::Result::DBI;
51
52 # get logger instance
53 my $logger = Log::Dispatch::Config->instance;
54
55
56 sub getMetaInfo {
57 my $self = shift;
58 $logger->debug( __PACKAGE__ . "->getMetaInfo()" );
59 return {
60 'disconnectMethod' => 'disconnect',
61 };
62 }
63
64 sub connect {
65
66 my $self = shift;
67
68 # create handle
69 if ( my $dsn = $self->{locator}->{dbi}->{dsn} ) {
70 #if ( my $dsn = $self->{locator}->{dsn} ) {
71 $logger->debug( __PACKAGE__ . "->connect( dsn $dsn )" );
72
73 # HACK:
74 # set errorhandler before actually calling DBI->connect
75 # in order to catch errors from the very beginning
76 #DBI->{HandleError} = $self->{dbi}->{HandleError};
77
78 #use Data::Dumper; print Dumper($self->{dbi});
79
80 eval {
81 $self->{_COREHANDLE} = DBI->connect( $dsn, '', '', $self->{locator}->{dbi} );
82 if (!$self->{_COREHANDLE}) {
83 $logger->warning( __PACKAGE__ . "->connect failed: " . DBI::errstr );
84 return;
85 }
86 };
87 $logger->warning( __PACKAGE__ . "->connect failed: " . $@ ) if $@;
88
89 }
90 $self->configureCOREHANDLE();
91
92 $self->{locator}->{status}->{connected} = 1;
93
94 return 1;
95
96 }
97
98 sub configureCOREHANDLE {
99
100 my $self = shift;
101
102 $logger->debug( __PACKAGE__ . "->configureCOREHANDLE" );
103
104 return if !$self->{_COREHANDLE};
105
106 # apply configured modifications to DBI-handle
107 if (exists $self->{locator}->{dbi}->{trace_level} && exists $self->{locator}->{dbi}->{trace_file}) {
108 $self->{_COREHANDLE}->trace($self->{locator}->{dbi}->{trace_level}, $self->{locator}->{dbi}->{trace_file});
109 }
110 if (exists $self->{locator}->{dbi}->{RaiseError}) {
111 $self->{_COREHANDLE}->{RaiseError} = $self->{locator}->{dbi}->{RaiseError};
112 }
113 if (exists $self->{locator}->{dbi}->{PrintError}) {
114 $self->{_COREHANDLE}->{PrintError} = $self->{locator}->{dbi}->{PrintError};
115 }
116 if (exists $self->{locator}->{dbi}->{HandleError}) {
117 $self->{_COREHANDLE}->{HandleError} = $self->{locator}->{dbi}->{HandleError};
118 }
119
120 }
121
122 sub _sendSql {
123 my $self = shift;
124 my $sql = shift;
125
126 # two-level handling for implicit connect:
127 # if there's no corehandle ...
128 if (!$self->{_COREHANDLE}) {
129 # ... try to connect, but ...
130 $self->connect();
131 # ... if this still fails, there's something wrong probably, so we won't continue
132 if (!$self->{_COREHANDLE}) {
133 return;
134 }
135 }
136
137 #print "prepare sql: $sql\n";
138
139 my $sth = $self->{_COREHANDLE}->prepare($sql);
140 $sth->execute();
141 return $sth;
142 }
143
144 sub sendCommand {
145 my $self = shift;
146 my $command = shift;
147 # TODO: when tracing: yes, do actually log this
148 #$logger->debug( __PACKAGE__ . "->sendCommand( command $command )" );
149 my $cmdHandle = $self->_sendSql($command);
150 my $result = Data::Storage::Result::DBI->new( RESULTHANDLE => $cmdHandle );
151 return $result;
152 }
153
154 sub getChildNodes {
155 my $self = shift;
156 my @nodes;
157 $logger->debug( __PACKAGE__ . "->getChildNodes()" );
158 my $locator = $self->{locator};
159 #print Dumper($locator); exit;
160 if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) {
161 my $dbname = getDbNameByDsn($self->{locator}->{dbi}->{dsn});
162 my $key = "Tables_in_$dbname";
163 while ( my $row = $result->getNextEntry() ) {
164 push @nodes, $row->{$key};
165 }
166 }
167 return \@nodes;
168 }
169
170 sub getListUnfiltered {
171 my $self = shift;
172 my $nodename = shift;
173 my @list;
174 $logger->debug( __PACKAGE__ . "->getListUnfiltered( nodename => '" . $nodename . "' )" );
175 # get list of rows from rdbms by table name
176 my $result = $self->sendCommand("SELECT * FROM $nodename");
177 while ( my $row = $result->getNextEntry() ) {
178 push @list, $row;
179 }
180 return \@list;
181 }
182
183 sub sendQuery {
184 my $self = shift;
185 my $query = shift;
186
187 $logger->debug( __PACKAGE__ . "->sendQuery" );
188
189 #my $sql = "SELECT cs FROM $self->{metainfo}->{$descent}->{node} WHERE $self->{metainfo}->{$descent}->{IdentProvider}->{arg}='$self->{entry}->{source}->{ident}';";
190 #my $result = $self->{metainfo}->{$descent}->{storage}->sendCommand($sql);
191 my @crits;
192 foreach (@{$query->{criterias}}) {
193 my $op = '';
194 $op = '=' if lc $_->{op} eq 'eq';
195 push @crits, "$_->{key}$op'$_->{val}'";
196 }
197 my $subnodes = {};
198 map { $subnodes->{$_}++ } @{$query->{subnodes}};
199 # HACK: this is hardcoded ;( expand possibilities!
200 my $crit = join(' AND ', @crits);
201 my $sql = hash2Sql($query->{node}, $subnodes, 'SELECT', $crit);
202 return $self->sendCommand($sql);
203 }
204
205 sub eraseAll {
206 my $self = shift;
207 my $classname = shift;
208 $logger->debug( __PACKAGE__ . "->eraseAll" );
209 my $sql = "DELETE FROM $classname";
210 $self->sendCommand($sql);
211 }
212
213 # TODO: actually implement the filtering functionality using $this->sendQuery
214 sub getListFiltered {
215 my $self = shift;
216 my $nodename = shift;
217 return $self->getListUnfiltered($nodename);
218 }
219
220 # TODO: do this via a parametrized "$self->connect(<connect just to database server - don't select database>)"
221 sub createDb {
222
223 my $self = shift;
224
225 # get dsn from Data::Storage::Locator instance
226 my $dsn = $self->{locator}->{dbi}->{dsn};
227
228 $logger->debug( __PACKAGE__ . "->createDb( dsn $dsn )" );
229
230 # remove database setting from dsn-string
231 $dsn =~ s/database=(.+?);//;
232
233 # remember extracted database name to know what actually to create right now
234 my $database_name = $1;
235
236 # flag to indicate goodness
237 my $ok;
238
239 # connect to database server - don't select/use any specific database
240 #if ( my $dbh = DBI->connect($dsn, '', '', { PrintError => 0 } ) ) {
241 if ( my $dbh = DBI->connect($dsn, '', '', $self->{locator}->{dbi} ) ) {
242
243 if ($database_name) {
244 if ($dbh->do("CREATE DATABASE $database_name")) {
245 $ok = 1;
246 }
247 }
248
249 $dbh->disconnect();
250
251 }
252
253 return $ok;
254
255 }
256
257 sub getCOREHANDLE2 {
258 my $self = shift;
259 return $self->{_COREHANDLE};
260 }
261
262 1;

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed