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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.1.4  
changed lines
  Added in v.1.10

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