/[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.3 by joko, Sun Nov 17 06:34:39 2002 UTC revision 1.8 by joko, Sun Dec 1 22:20:43 2002 UTC
# Line 3  Line 3 
3  #  $Id$  #  $Id$
4  #  #
5  #  $Log$  #  $Log$
6    #  Revision 1.8  2002/12/01 22:20:43  joko
7    #  + sub createDb (from Storage.pm)
8    #
9    #  Revision 1.7  2002/12/01 07:09:09  joko
10    #  + sub getListFiltered (dummy redirecting to getListUnfiltered)
11    #
12    #  Revision 1.6  2002/12/01 04:46:01  joko
13    #  + sub eraseAll
14    #
15    #  Revision 1.5  2002/11/29 05:00:26  joko
16    #  + sub getListUnfiltered
17    #  + sub sendQuery
18    #
19    #  Revision 1.4  2002/11/17 08:46:42  jonen
20    #  + wrapped eval around DBI->connect to prevent deaths
21    #
22  #  Revision 1.3  2002/11/17 06:34:39  joko  #  Revision 1.3  2002/11/17 06:34:39  joko
23  #  + locator metadata can now be reached via ->{locator}  #  + locator metadata can now be reached via ->{locator}
24  #  - sub hash2sql now taken from libdb  #  - sub hash2sql now taken from libdb
# Line 26  use base ("Data::Storage::Handler::Abstr Line 42  use base ("Data::Storage::Handler::Abstr
42    
43  use DBI;  use DBI;
44  use Data::Dumper;  use Data::Dumper;
45  use libdb;  use libdb qw( getDbNameByDsn hash2Sql );
46    use Data::Storage::Result::DBI;
47    
48  # get logger instance  # get logger instance
49  my $logger = Log::Dispatch::Config->instance;  my $logger = Log::Dispatch::Config->instance;
50    
51    
52  our $metainfo = {  sub getMetaInfo {
53    'disconnectMethod' => 'disconnect',    my $self = shift;
54  };    $logger->debug( __PACKAGE__ . "->getMetaInfo()"  );
55      return {
56        'disconnectMethod' => 'disconnect',
57      };
58    }
59    
60  sub connect {  sub connect {
61    
# Line 52  sub connect { Line 73  sub connect {
73                    
74          #use Data::Dumper; print Dumper($self->{dbi});          #use Data::Dumper; print Dumper($self->{dbi});
75                    
76          $self->{COREHANDLE} = DBI->connect( $dsn, '', '', $self->{locator}->{dbi} );          eval {
77          if (!$self->{COREHANDLE}) {            $self->{COREHANDLE} = DBI->connect( $dsn, '', '', $self->{locator}->{dbi} );
78            $logger->warning( __PACKAGE__ . "->connect failed: " . DBI::errstr );            if (!$self->{COREHANDLE}) {
79            return;              $logger->warning( __PACKAGE__ . "->connect failed: " . DBI::errstr );
80          }              return;
81              }
82            };
83            $logger->warning( __PACKAGE__ . "->connect failed: " . $@ ) if $@;
84    
85        }        }
86        $self->configureCOREHANDLE();        $self->configureCOREHANDLE();
87    
# Line 72  sub configureCOREHANDLE { Line 97  sub configureCOREHANDLE {
97    
98    $logger->debug( __PACKAGE__ . "->configureCOREHANDLE" );    $logger->debug( __PACKAGE__ . "->configureCOREHANDLE" );
99    
100    # apply configured modifications    return if !$self->{COREHANDLE};
101    
102      # apply configured modifications to DBI-handle
103      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}) {
104        $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});
105      }      }
# Line 113  sub _sendSql { Line 140  sub _sendSql {
140  sub sendCommand {  sub sendCommand {
141    my $self = shift;    my $self = shift;
142    my $command = shift;    my $command = shift;
143      # TODO: when tracing: yes, do actually log this
144    #$logger->debug( __PACKAGE__ . "->sendCommand( command $command )" );    #$logger->debug( __PACKAGE__ . "->sendCommand( command $command )" );
145    my $cmdHandle = $self->_sendSql($command);    my $cmdHandle = $self->_sendSql($command);
146    my $result = Data::Storage::Result::DBI->new( RESULTHANDLE => $cmdHandle );    my $result = Data::Storage::Result::DBI->new( RESULTHANDLE => $cmdHandle );
147    return $result;    return $result;
148  }  }
149    
 sub quoteSql {  
   my $self = shift;  
   my $string = shift;  
   if ($string) {  
     $string =~ s/'/\\'/g;  
   }  
   return $string;  
 }  
   
   
150  sub getChildNodes {  sub getChildNodes {
   
151    my $self = shift;    my $self = shift;
152    my @nodes;    my @nodes;
   
153    $logger->debug( __PACKAGE__ . "->getChildNodes()" );    $logger->debug( __PACKAGE__ . "->getChildNodes()" );
   
154    if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) {    if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) {
155      my $dbname = getDbNameByDsn($self->{locator}->{dbi}->{dsn});      my $dbname = getDbNameByDsn($self->{locator}->{dbi}->{dsn});
156      my $key = "Tables_in_$dbname";      my $key = "Tables_in_$dbname";
157      while ( my $row = $result->_getNextEntry() ) {      while ( my $row = $result->getNextEntry() ) {
158        push @nodes, $row->{$key};        push @nodes, $row->{$key};
159      }      }
160    }    }
     
161    return \@nodes;    return \@nodes;
162    }
163    
164    sub getListUnfiltered {
165      my $self = shift;
166      my $nodename = shift;
167      my @list;
168      $logger->debug( __PACKAGE__ . "->getListUnfiltered( nodename => '" . $nodename . "' )" );
169      # get list of rows from rdbms by table name
170      my $result = $self->sendCommand("SELECT * FROM $nodename");
171      while ( my $row = $result->getNextEntry() ) {
172        push @list, $row;
173      }
174      return \@list;
175  }  }
176    
177    sub sendQuery {
178      my $self = shift;
179      my $query = shift;
180      #my $sql = "SELECT cs FROM $self->{metainfo}->{$descent}->{node} WHERE $self->{metainfo}->{$descent}->{IdentProvider}->{arg}='$self->{entry}->{source}->{ident}';";
181      #my $result = $self->{metainfo}->{$descent}->{storage}->sendCommand($sql);
182      my @crits;
183      foreach (@{$query->{criterias}}) {
184        my $op = '';
185        $op = '=' if lc $_->{op} eq 'eq';
186        push @crits, "$_->{key}$op'$_->{val}'";
187      }
188      my $subnodes = {};
189      map { $subnodes->{$_}++ } @{$query->{subnodes}};
190      # HACK: this is hardcoded ;(    expand possibilities!
191      my $crit = join(' AND ', @crits);
192      my $sql = hash2Sql($query->{node}, $subnodes, 'SELECT', $crit);
193      return $self->sendCommand($sql);
194    }
195    
196    sub eraseAll {
197      my $self = shift;
198      my $classname = shift;
199      my $sql = "DELETE FROM $classname";
200      $self->sendCommand($sql);
201    }
202    
203    # TODO: actually implement the filtering functionality using $this->sendQuery
204    sub getListFiltered {
205      my $self = shift;
206      my $nodename = shift;
207      return $self->getListUnfiltered($nodename);
208    }
209    
210  package Data::Storage::Result::DBI;  # TODO: do this via a parametrized "$self->connect(<connect just to database server - don't select database>)"
211    sub createDb {
212      
213      my $self = shift;
214      
215      # get dsn from Data::Storage::Locator instance
216      my $dsn = $self->{locator}->{dbi}->{dsn};
217    
218  use strict;    $logger->debug( __PACKAGE__ .  "->createDb( dsn $dsn )" );
219  use warnings;  
220      # remove database setting from dsn-string
221      $dsn =~ s/database=(.+?);//;
222      
223      # remember extracted database name to know what actually to create right now
224      my $database_name = $1;
225    
226  use base ("Data::Storage::Result");    # flag to indicate goodness
227      my $ok;
228      
229      # connect to database server - don't select/use any specific database
230      #if ( my $dbh = DBI->connect($dsn, '', '', { PrintError => 0 } ) ) {
231      if ( my $dbh = DBI->connect($dsn, '', '', $self->{locator}->{dbi} ) ) {
232    
233        if ($database_name) {
234          if ($dbh->do("CREATE DATABASE $database_name")) {
235            $ok = 1;
236          }
237        }
238    
239  sub DESTROY {      $dbh->disconnect();
   my $self = shift;  
   #$logger->debug( __PACKAGE__ . "->" . "DESTROY" );  
   $self->{RESULTHANDLE} && $self->{RESULTHANDLE}->finish();  
 }  
240    
241  sub _getNextEntry {    }
242    my $self = shift;    
243    $self->{RESULTHANDLE} && return $self->{RESULTHANDLE}->fetchrow_hashref;    return $ok;
244      
245  }  }
246    
   
 1;  
247    1;

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.8

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