/[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.1 by cvsjoko, Thu Oct 10 03:44:07 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
27    #  + locator metadata can now be reached via ->{locator}
28    #  - sub hash2sql now taken from libdb
29    #
30    #  Revision 1.2  2002/10/25 11:43:27  joko
31    #  + enhanced robustness
32    #  + more logging for debug-levels
33    #
34  #  Revision 1.1  2002/10/10 03:44:07  cvsjoko  #  Revision 1.1  2002/10/10 03:44:07  cvsjoko
35  #  + new  #  + new
36  #  #
# Line 17  use warnings; Line 45  use warnings;
45  use base ("Data::Storage::Handler::Abstract");  use base ("Data::Storage::Handler::Abstract");
46    
47  use DBI;  use DBI;
48    use Data::Dumper;
49    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    
66      my $self = shift;      my $self = shift;
67            
68      # create handle      # create handle
69        if ( my $dsn = $self->{dbi}->{dsn} ) {        if ( my $dsn = $self->{locator}->{dbi}->{dsn} ) {
70          $logger->debug( __PACKAGE__ . "->connect($dsn)" );        #if ( my $dsn = $self->{locator}->{dsn} ) {
71          $self->{COREHANDLE} = DBI->connect($dsn);          $logger->debug( __PACKAGE__ . "->connect( dsn $dsn )" );
72            
73            # HACK:
74            # set errorhandler before actually calling DBI->connect
75            # in order to catch errors from the very beginning
76            #DBI->{HandleError} = $self->{dbi}->{HandleError};
77            
78            #use Data::Dumper; print Dumper($self->{dbi});
79            
80            eval {
81              $self->{_COREHANDLE} = DBI->connect( $dsn, '', '', $self->{locator}->{dbi} );
82              if (!$self->{_COREHANDLE}) {
83                $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    
92        $self->{locator}->{status}->{connected} = 1;
93    
94        return 1;
95            
96  }  }
97    
# Line 43  sub configureCOREHANDLE { Line 99  sub configureCOREHANDLE {
99    
100    my $self = shift;    my $self = shift;
101    
102    $logger->debug( __PACKAGE__ . "->_configureCOREHANDLE" );    $logger->debug( __PACKAGE__ . "->configureCOREHANDLE" );
103    
104      return if !$self->{_COREHANDLE};
105    
106    # apply configured modifications    # apply configured modifications to DBI-handle
107      if (exists $self->{dbi}->{trace_level} && exists $self->{dbi}->{trace_file}) {      if (exists $self->{locator}->{dbi}->{trace_level} && exists $self->{locator}->{dbi}->{trace_file}) {
108        $self->{COREHANDLE}->trace($self->{dbi}->{trace_level}, $self->{dbi}->{trace_file});        $self->{_COREHANDLE}->trace($self->{locator}->{dbi}->{trace_level}, $self->{locator}->{dbi}->{trace_file});
109      }      }
110      if (exists $self->{dbi}->{RaiseError}) {      if (exists $self->{locator}->{dbi}->{RaiseError}) {
111        $self->{COREHANDLE}->{RaiseError} = $self->{dbi}->{RaiseError};        $self->{_COREHANDLE}->{RaiseError} = $self->{locator}->{dbi}->{RaiseError};
112      }      }
113      if (exists $self->{dbi}->{PrintError}) {      if (exists $self->{locator}->{dbi}->{PrintError}) {
114        $self->{COREHANDLE}->{PrintError} = $self->{dbi}->{PrintError};        $self->{_COREHANDLE}->{PrintError} = $self->{locator}->{dbi}->{PrintError};
115      }      }
116      if (exists $self->{dbi}->{HandleError}) {      if (exists $self->{locator}->{dbi}->{HandleError}) {
117        $self->{COREHANDLE}->{HandleError} = $self->{dbi}->{HandleError};        $self->{_COREHANDLE}->{HandleError} = $self->{locator}->{dbi}->{HandleError};
118      }      }
119    
120  }  }
# Line 64  sub configureCOREHANDLE { Line 122  sub configureCOREHANDLE {
122  sub _sendSql {  sub _sendSql {
123    my $self = shift;    my $self = shift;
124    my $sql = shift;    my $sql = shift;
125    my $sth = $self->{COREHANDLE}->prepare($sql);    
126      # two-level handling for implicit connect:
127      # if there's no corehandle ...
128      if (!$self->{_COREHANDLE}) {
129        # ... try to connect, but ...
130        $self->connect();
131        # ... if this still fails, there's something wrong probably, so we won't continue
132        if (!$self->{_COREHANDLE}) {
133          return;
134        }
135      }
136      
137      #print "prepare sql: $sql\n";
138      
139      my $sth = $self->{_COREHANDLE}->prepare($sql);
140    $sth->execute();    $sth->execute();
141    return $sth;    return $sth;
142  }  }
# Line 72  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 )" );
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    
154  sub quoteSql {  sub getChildNodes {
155    my $self = shift;    my $self = shift;
156    my $string = shift;    my @nodes;
157    if ($string) {    $logger->debug( __PACKAGE__ . "->getChildNodes()" );
158      $string =~ s/'/\\'/g;    my $locator = $self->{locator};
159      #print Dumper($locator); exit;
160      if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) {
161        my $dbname = getDbNameByDsn($self->{locator}->{dbi}->{dsn});
162        my $key = "Tables_in_$dbname";
163        while ( my $row = $result->getNextEntry() ) {
164          push @nodes, $row->{$key};
165        }
166    }    }
167    return $string;    return \@nodes;
168  }  }
169    
170  sub hash2Sql {  sub getListUnfiltered {
   
171    my $self = shift;    my $self = shift;
172        my $nodename = shift;
173    my $table = shift;    my @list;
174    my $hash = shift;    $logger->debug( __PACKAGE__ . "->getListUnfiltered( nodename => '" . $nodename . "' )" );
175    my $mode = shift;    # get list of rows from rdbms by table name
176    my $crit = shift;    my $result = $self->sendCommand("SELECT * FROM $nodename");
177        while ( my $row = $result->getNextEntry() ) {
178    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;";  
179    }    }
180        return \@list;
   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++;  
   }  
   $fields_values = substr($fields_values, 0, -2);  
     
   $sql =~ s/#fields#/$fields/;  
   $sql =~ s/#values#/$values/;  
   $sql =~ s/#fields-values#/$fields_values/;  
     
   return $sql;  
181  }  }
182    
183    sub sendQuery {
 sub getChildNodes {  
   
184    my $self = shift;    my $self = shift;
185    my @nodes;    my $query = shift;
186      #my $sql = "SELECT cs FROM $self->{metainfo}->{$descent}->{node} WHERE $self->{metainfo}->{$descent}->{IdentProvider}->{arg}='$self->{entry}->{source}->{ident}';";
187    if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) {    #my $result = $self->{metainfo}->{$descent}->{storage}->sendCommand($sql);
188      while ( my $row = $result->_getNextEntry() ) {    my @crits;
189        push @nodes, $row;    foreach (@{$query->{criterias}}) {
190      }      my $op = '';
191        $op = '=' if lc $_->{op} eq 'eq';
192        push @crits, "$_->{key}$op'$_->{val}'";
193    }    }
194        my $subnodes = {};
195    return \@nodes;    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    # 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      $logger->debug( __PACKAGE__ .  "->createDb( dsn $dsn )" );
225    
226  package Data::Storage::Result::DBI;    # 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  use strict;    # flag to indicate goodness
233  use warnings;    my $ok;
234      
235      # 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  use base ("Data::Storage::Result");      $dbh->disconnect();
246    
247  sub DESTROY {    }
248    my $self = shift;    
249    #$logger->debug( __PACKAGE__ . "->" . "DESTROY" );    return $ok;
250    $self->{RESULTHANDLE}->finish();    
251  }  }
252    
253  sub _getNextEntry {  sub getCOREHANDLE2 {
254    my $self = shift;    my $self = shift;
255    return $self->{RESULTHANDLE}->fetchrow_hashref;    return $self->{_COREHANDLE};
256  }  }
257    
   
 1;  
258    1;

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

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