/[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.2 by joko, Fri Oct 25 11:43:27 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
27    #  + wrapped eval around DBI->connect to prevent deaths
28    #
29    #  Revision 1.3  2002/11/17 06:34:39  joko
30    #  + locator metadata can now be reached via ->{locator}
31    #  - sub hash2sql now taken from libdb
32    #
33  #  Revision 1.2  2002/10/25 11:43:27  joko  #  Revision 1.2  2002/10/25 11:43:27  joko
34  #  + enhanced robustness  #  + enhanced robustness
35  #  + more logging for debug-levels  #  + more logging for debug-levels
# Line 21  use warnings; Line 48  use warnings;
48  use base ("Data::Storage::Handler::Abstract");  use base ("Data::Storage::Handler::Abstract");
49    
50  use DBI;  use DBI;
51    use Data::Dumper;
52    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    
69      my $self = shift;      my $self = shift;
70            
71      # create handle      # create handle
72        if ( my $dsn = $self->{dbi}->{dsn} ) {        if ( my $dsn = $self->{locator}->{dbi}->{dsn} ) {
73          #if ( my $dsn = $self->{locator}->{dsn} ) {
74          $logger->debug( __PACKAGE__ . "->connect( dsn $dsn )" );          $logger->debug( __PACKAGE__ . "->connect( dsn $dsn )" );
75                    
76          # HACK:          # HACK:
# Line 45  sub connect { Line 80  sub connect {
80                    
81          #use Data::Dumper; print Dumper($self->{dbi});          #use Data::Dumper; print Dumper($self->{dbi});
82                    
83          $self->{COREHANDLE} = DBI->connect(          eval {
84            $dsn, '', '', {            $self->{_COREHANDLE} = DBI->connect( $dsn, '', '', $self->{locator}->{dbi} );
85              RaiseError => $self->{dbi}->{RaiseError},            if (!$self->{_COREHANDLE}) {
86              #RaiseError => 1,              $logger->warning( __PACKAGE__ . "->connect failed: " . DBI::errstr );
87              PrintError => $self->{dbi}->{PrintError},              return;
             HandleError => $self->{dbi}->{HandleError},  
88            }            }
89          );          };
90          if (!$self->{COREHANDLE}) {          $logger->warning( __PACKAGE__ . "->connect failed: " . $@ ) if $@;
91            $logger->warning( __PACKAGE__ . "->connect failed: " . DBI::errstr );  
           return;  
         }  
92        }        }
93        $self->configureCOREHANDLE();        $self->configureCOREHANDLE();
94        
95        $self->{locator}->{status}->{connected} = 1;
96    
97      return 1;      return 1;
98            
99  }  }
# Line 68  sub configureCOREHANDLE { Line 102  sub configureCOREHANDLE {
102    
103    my $self = shift;    my $self = shift;
104    
105    $logger->debug( __PACKAGE__ . "->_configureCOREHANDLE" );    $logger->debug( __PACKAGE__ . "->configureCOREHANDLE" );
106    
107      return if !$self->{_COREHANDLE};
108    
109    # apply configured modifications    # apply configured modifications to DBI-handle
110      if (exists $self->{dbi}->{trace_level} && exists $self->{dbi}->{trace_file}) {      if (exists $self->{locator}->{dbi}->{trace_level} && exists $self->{locator}->{dbi}->{trace_file}) {
111        $self->{COREHANDLE}->trace($self->{dbi}->{trace_level}, $self->{dbi}->{trace_file});        $self->{_COREHANDLE}->trace($self->{locator}->{dbi}->{trace_level}, $self->{locator}->{dbi}->{trace_file});
112      }      }
113      if (exists $self->{dbi}->{RaiseError}) {      if (exists $self->{locator}->{dbi}->{RaiseError}) {
114        $self->{COREHANDLE}->{RaiseError} = $self->{dbi}->{RaiseError};        $self->{_COREHANDLE}->{RaiseError} = $self->{locator}->{dbi}->{RaiseError};
115      }      }
116      if (exists $self->{dbi}->{PrintError}) {      if (exists $self->{locator}->{dbi}->{PrintError}) {
117        $self->{COREHANDLE}->{PrintError} = $self->{dbi}->{PrintError};        $self->{_COREHANDLE}->{PrintError} = $self->{locator}->{dbi}->{PrintError};
118      }      }
119      if (exists $self->{dbi}->{HandleError}) {      if (exists $self->{locator}->{dbi}->{HandleError}) {
120        $self->{COREHANDLE}->{HandleError} = $self->{dbi}->{HandleError};        $self->{_COREHANDLE}->{HandleError} = $self->{locator}->{dbi}->{HandleError};
121      }      }
122    
123  }  }
# Line 92  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    my $sth = $self->{COREHANDLE}->prepare($sql);    #print "prepare sql: $sql\n";
141      
142      my $sth = $self->{_COREHANDLE}->prepare($sql);
143    $sth->execute();    $sth->execute();
144    return $sth;    return $sth;
145  }  }
# Line 109  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    
157  sub quoteSql {  sub getChildNodes {
158    my $self = shift;    my $self = shift;
159    my $string = shift;    my @nodes;
160    if ($string) {    $logger->debug( __PACKAGE__ . "->getChildNodes()" );
161      $string =~ s/'/\\'/g;    my $locator = $self->{locator};
162      #print Dumper($locator); exit;
163      if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) {
164        my $dbname = getDbNameByDsn($self->{locator}->{dbi}->{dsn});
165        my $key = "Tables_in_$dbname";
166        while ( my $row = $result->getNextEntry() ) {
167          push @nodes, $row->{$key};
168        }
169    }    }
170    return $string;    return \@nodes;
171  }  }
172    
173  sub hash2Sql {  sub getListUnfiltered {
   
174    my $self = shift;    my $self = shift;
175        my $nodename = shift;
176    my $table = shift;    my @list;
177    my $hash = shift;    $logger->debug( __PACKAGE__ . "->getListUnfiltered( nodename => '" . $nodename . "' )" );
178    my $mode = shift;    # get list of rows from rdbms by table name
179    my $crit = shift;    my $result = $self->sendCommand("SELECT * FROM $nodename");
180        while ( my $row = $result->getNextEntry() ) {
181    my $sql;      push @list, $row;
   if ($mode eq 'SQL_INSERT') {  
     $sql = "INSERT INTO $table (#fields#) VALUES (#values#);";  
   }  
   if ($mode eq 'SQL_UPDATE') {  
     $sql = "UPDATE $table SET #fields-values# WHERE $crit;";  
   }  
     
   my (@fields, @values);  
   foreach my $key (keys %{$hash}) {  
     push @fields, $key;  
     push @values, $hash->{$key};  
   }  
   # quote each element  
   map { if (defined $_) { $_ = "'$_'" } else { $_ = "null" } } @values;  
     
   my $fields = join(', ', @fields);  
   my $values = join(', ', @values);  
   my $fields_values = '';  
   my $fc = 0;  
   foreach (@fields) {  
     $fields_values .= $_ . '=' . $values[$fc] . ', ';  
     $fc++;  
182    }    }
183    $fields_values = substr($fields_values, 0, -2);    return \@list;
     
   $sql =~ s/#fields#/$fields/;  
   $sql =~ s/#values#/$values/;  
   $sql =~ s/#fields-values#/$fields_values/;  
     
   return $sql;  
184  }  }
185    
186    sub sendQuery {
 sub getChildNodes {  
   
187    my $self = shift;    my $self = shift;
188    my @nodes;    my $query = shift;
189    
190    if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) {    $logger->debug( __PACKAGE__ . "->sendQuery" );
191      while ( my $row = $result->_getNextEntry() ) {  
192        push @nodes, $row;    #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      my @crits;
195      foreach (@{$query->{criterias}}) {
196        my $op = '';
197        $op = '=' if lc $_->{op} eq 'eq';
198        push @crits, "$_->{key}$op'$_->{val}'";
199    }    }
200        my $subnodes = {};
201    return \@nodes;    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    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    # TODO: actually implement the filtering functionality using $this->sendQuery
217    sub getListFiltered {
218      my $self = shift;
219      my $nodename = shift;
220      return $self->getListUnfiltered($nodename);
221    }
222    
223    # 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;
227      
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  package Data::Storage::Result::DBI;    # 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  use strict;    # flag to indicate goodness
240  use warnings;    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  use base ("Data::Storage::Result");      $dbh->disconnect();
253    
254  sub DESTROY {    }
255    my $self = shift;    
256    #$logger->debug( __PACKAGE__ . "->" . "DESTROY" );    return $ok;
257    $self->{RESULTHANDLE} && $self->{RESULTHANDLE}->finish();    
258  }  }
259    
260  sub _getNextEntry {  sub getCOREHANDLE2 {
261    my $self = shift;    my $self = shift;
262    $self->{RESULTHANDLE} && return $self->{RESULTHANDLE}->fetchrow_hashref;    return $self->{_COREHANDLE};
263  }  }
264    
   
 1;  
265    1;

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

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