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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.4 - (hide annotations)
Sun Nov 17 08:46:42 2002 UTC (21 years, 7 months ago) by jonen
Branch: MAIN
Changes since 1.3: +15 -7 lines
+ wrapped eval around DBI->connect to prevent deaths

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

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