/[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.9 by joko, Thu Dec 5 07:58:20 2002 UTC
# Line 3  Line 3 
3  #  $Id$  #  $Id$
4  #  #
5  #  $Log$  #  $Log$
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  #  Revision 1.3  2002/11/17 06:34:39  joko
27  #  + locator metadata can now be reached via ->{locator}  #  + locator metadata can now be reached via ->{locator}
28  #  - sub hash2sql now taken from libdb  #  - sub hash2sql now taken from libdb
# Line 26  use base ("Data::Storage::Handler::Abstr Line 46  use base ("Data::Storage::Handler::Abstr
46    
47  use DBI;  use DBI;
48  use Data::Dumper;  use Data::Dumper;
49  use libdb;  use libdb qw( getDbNameByDsn hash2Sql );
50    use Data::Storage::Result::DBI;
51    
52  # get logger instance  # get logger instance
53  my $logger = Log::Dispatch::Config->instance;  my $logger = Log::Dispatch::Config->instance;
54    
55    
56  our $metainfo = {  sub getMetaInfo {
57    'disconnectMethod' => 'disconnect',    my $self = shift;
58  };    $logger->debug( __PACKAGE__ . "->getMetaInfo()"  );
59      return {
60        'disconnectMethod' => 'disconnect',
61      };
62    }
63    
64  sub connect {  sub connect {
65    
# Line 52  sub connect { Line 77  sub connect {
77                    
78          #use Data::Dumper; print Dumper($self->{dbi});          #use Data::Dumper; print Dumper($self->{dbi});
79                    
80          $self->{COREHANDLE} = DBI->connect( $dsn, '', '', $self->{locator}->{dbi} );          eval {
81          if (!$self->{COREHANDLE}) {            $self->{_COREHANDLE} = DBI->connect( $dsn, '', '', $self->{locator}->{dbi} );
82            $logger->warning( __PACKAGE__ . "->connect failed: " . DBI::errstr );            if (!$self->{_COREHANDLE}) {
83            return;              $logger->warning( __PACKAGE__ . "->connect failed: " . DBI::errstr );
84          }              return;
85              }
86            };
87            $logger->warning( __PACKAGE__ . "->connect failed: " . $@ ) if $@;
88    
89        }        }
90        $self->configureCOREHANDLE();        $self->configureCOREHANDLE();
91    
# Line 72  sub configureCOREHANDLE { Line 101  sub configureCOREHANDLE {
101    
102    $logger->debug( __PACKAGE__ . "->configureCOREHANDLE" );    $logger->debug( __PACKAGE__ . "->configureCOREHANDLE" );
103    
104    # apply configured modifications    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}) {      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});        $self->{_COREHANDLE}->trace($self->{locator}->{dbi}->{trace_level}, $self->{locator}->{dbi}->{trace_file});
109      }      }
110      if (exists $self->{locator}->{dbi}->{RaiseError}) {      if (exists $self->{locator}->{dbi}->{RaiseError}) {
111        $self->{COREHANDLE}->{RaiseError} = $self->{locator}->{dbi}->{RaiseError};        $self->{_COREHANDLE}->{RaiseError} = $self->{locator}->{dbi}->{RaiseError};
112      }      }
113      if (exists $self->{locator}->{dbi}->{PrintError}) {      if (exists $self->{locator}->{dbi}->{PrintError}) {
114        $self->{COREHANDLE}->{PrintError} = $self->{locator}->{dbi}->{PrintError};        $self->{_COREHANDLE}->{PrintError} = $self->{locator}->{dbi}->{PrintError};
115      }      }
116      if (exists $self->{locator}->{dbi}->{HandleError}) {      if (exists $self->{locator}->{dbi}->{HandleError}) {
117        $self->{COREHANDLE}->{HandleError} = $self->{locator}->{dbi}->{HandleError};        $self->{_COREHANDLE}->{HandleError} = $self->{locator}->{dbi}->{HandleError};
118      }      }
119    
120  }  }
# Line 94  sub _sendSql { Line 125  sub _sendSql {
125        
126    # two-level handling for implicit connect:    # two-level handling for implicit connect:
127    # if there's no corehandle ...    # if there's no corehandle ...
128    if (!$self->{COREHANDLE}) {    if (!$self->{_COREHANDLE}) {
129      # ... try to connect, but ...      # ... try to connect, but ...
130      $self->connect();      $self->connect();
131      # ... 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
132      if (!$self->{COREHANDLE}) {      if (!$self->{_COREHANDLE}) {
133        return;        return;
134      }      }
135    }    }
136        
137    #print "prepare sql: $sql\n";    #print "prepare sql: $sql\n";
138        
139    my $sth = $self->{COREHANDLE}->prepare($sql);    my $sth = $self->{_COREHANDLE}->prepare($sql);
140    $sth->execute();    $sth->execute();
141    return $sth;    return $sth;
142  }  }
# Line 113  sub _sendSql { Line 144  sub _sendSql {
144  sub sendCommand {  sub sendCommand {
145    my $self = shift;    my $self = shift;
146    my $command = shift;    my $command = shift;
147      # TODO: when tracing: yes, do actually log this
148    #$logger->debug( __PACKAGE__ . "->sendCommand( command $command )" );    #$logger->debug( __PACKAGE__ . "->sendCommand( command $command )" );
149    my $cmdHandle = $self->_sendSql($command);    my $cmdHandle = $self->_sendSql($command);
150    my $result = Data::Storage::Result::DBI->new( RESULTHANDLE => $cmdHandle );    my $result = Data::Storage::Result::DBI->new( RESULTHANDLE => $cmdHandle );
151    return $result;    return $result;
152  }  }
153    
 sub quoteSql {  
   my $self = shift;  
   my $string = shift;  
   if ($string) {  
     $string =~ s/'/\\'/g;  
   }  
   return $string;  
 }  
   
   
154  sub getChildNodes {  sub getChildNodes {
   
155    my $self = shift;    my $self = shift;
156    my @nodes;    my @nodes;
   
157    $logger->debug( __PACKAGE__ . "->getChildNodes()" );    $logger->debug( __PACKAGE__ . "->getChildNodes()" );
158      my $locator = $self->{locator};
159      #print Dumper($locator); exit;
160    if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) {    if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) {
161      my $dbname = getDbNameByDsn($self->{locator}->{dbi}->{dsn});      my $dbname = getDbNameByDsn($self->{locator}->{dbi}->{dsn});
162      my $key = "Tables_in_$dbname";      my $key = "Tables_in_$dbname";
163      while ( my $row = $result->_getNextEntry() ) {      while ( my $row = $result->getNextEntry() ) {
164        push @nodes, $row->{$key};        push @nodes, $row->{$key};
165      }      }
166    }    }
     
167    return \@nodes;    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      #my $sql = "SELECT cs FROM $self->{metainfo}->{$descent}->{node} WHERE $self->{metainfo}->{$descent}->{IdentProvider}->{arg}='$self->{entry}->{source}->{ident}';";
187      #my $result = $self->{metainfo}->{$descent}->{storage}->sendCommand($sql);
188      my @crits;
189      foreach (@{$query->{criterias}}) {
190        my $op = '';
191        $op = '=' if lc $_->{op} eq 'eq';
192        push @crits, "$_->{key}$op'$_->{val}'";
193      }
194      my $subnodes = {};
195      map { $subnodes->{$_}++ } @{$query->{subnodes}};
196      # HACK: this is hardcoded ;(    expand possibilities!
197      my $crit = join(' AND ', @crits);
198      my $sql = hash2Sql($query->{node}, $subnodes, 'SELECT', $crit);
199      return $self->sendCommand($sql);
200    }
201    
202    sub eraseAll {
203      my $self = shift;
204      my $classname = shift;
205      my $sql = "DELETE FROM $classname";
206      $self->sendCommand($sql);
207    }
208    
209    # TODO: actually implement the filtering functionality using $this->sendQuery
210    sub getListFiltered {
211      my $self = shift;
212      my $nodename = shift;
213      return $self->getListUnfiltered($nodename);
214    }
215    
216  package Data::Storage::Result::DBI;  # TODO: do this via a parametrized "$self->connect(<connect just to database server - don't select database>)"
217    sub createDb {
218      
219      my $self = shift;
220      
221      # get dsn from Data::Storage::Locator instance
222      my $dsn = $self->{locator}->{dbi}->{dsn};
223    
224  use strict;    $logger->debug( __PACKAGE__ .  "->createDb( dsn $dsn )" );
 use warnings;  
225    
226  use base ("Data::Storage::Result");    # remove database setting from dsn-string
227      $dsn =~ s/database=(.+?);//;
228      
229      # remember extracted database name to know what actually to create right now
230      my $database_name = $1;
231    
232  sub DESTROY {    # flag to indicate goodness
233    my $self = shift;    my $ok;
234    #$logger->debug( __PACKAGE__ . "->" . "DESTROY" );    
235    $self->{RESULTHANDLE} && $self->{RESULTHANDLE}->finish();    # connect to database server - don't select/use any specific database
236      #if ( my $dbh = DBI->connect($dsn, '', '', { PrintError => 0 } ) ) {
237      if ( my $dbh = DBI->connect($dsn, '', '', $self->{locator}->{dbi} ) ) {
238    
239        if ($database_name) {
240          if ($dbh->do("CREATE DATABASE $database_name")) {
241            $ok = 1;
242          }
243        }
244    
245        $dbh->disconnect();
246    
247      }
248      
249      return $ok;
250      
251  }  }
252    
253  sub _getNextEntry {  sub getCOREHANDLE2 {
254    my $self = shift;    my $self = shift;
255    $self->{RESULTHANDLE} && return $self->{RESULTHANDLE}->fetchrow_hashref;    return $self->{_COREHANDLE};
256  }  }
257    
   
 1;  
258    1;

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

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