/[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.10 by joko, Sun Dec 15 02:02:22 2002 UTC revision 1.17 by joko, Sat Jun 19 01:49:47 2004 UTC
# Line 3  Line 3 
3  #  $Id$  #  $Id$
4  #  #
5  #  $Log$  #  $Log$
6    #  Revision 1.17  2004/06/19 01:49:47  joko
7    #  (re-)activated "getCOREHANDLE"
8    #
9    #  Revision 1.16  2003/04/11 01:17:18  joko
10    #  sendQuery:
11    #  + introduced crud action 'DELETE'
12    #  + some pre-flight checks
13    #
14    #  Revision 1.15  2003/04/09 07:53:33  joko
15    #  minor namespace update
16    #
17    #  Revision 1.14  2003/04/09 06:06:04  joko
18    #  sendQuery now is capable of doing 'SELECT'- or 'INSERT'-queries
19    #
20    #  Revision 1.13  2003/04/08 23:06:45  joko
21    #  renamed core database helper functions
22    #
23    #  Revision 1.12  2003/01/30 22:28:21  joko
24    #  + implemented new concrete methods
25    #
26    #  Revision 1.11  2002/12/19 16:31:05  joko
27    #  + sub dropDb
28    #  + sub rebuildDb
29    #
30  #  Revision 1.10  2002/12/15 02:02:22  joko  #  Revision 1.10  2002/12/15 02:02:22  joko
31  #  + fixed logging-message  #  + fixed logging-message
32  #  #
# Line 47  use warnings; Line 71  use warnings;
71    
72  use base ("Data::Storage::Handler::Abstract");  use base ("Data::Storage::Handler::Abstract");
73    
74    
75  use DBI;  use DBI;
76  use Data::Dumper;  use Data::Dumper;
77  use libdb qw( getDbNameByDsn hash2Sql );  use shortcuts::database qw( hash2sql dsn2dbname );
78  use Data::Storage::Result::DBI;  use Data::Storage::Result::DBI;
79    
80    
81  # get logger instance  # get logger instance
82  my $logger = Log::Dispatch::Config->instance;  my $logger = Log::Dispatch::Config->instance;
83    
# Line 126  sub _sendSql { Line 152  sub _sendSql {
152    my $self = shift;    my $self = shift;
153    my $sql = shift;    my $sql = shift;
154        
155      # pre-flight check: is $sql defined?
156      if (!$sql) {
157        $logger->warning( __PACKAGE__ . "->_sendSql: \$sql was empty." );
158        return;
159      }
160      
161    # two-level handling for implicit connect:    # two-level handling for implicit connect:
162    # if there's no corehandle ...    # if there's no corehandle ...
163    if (!$self->{_COREHANDLE}) {    if (!$self->{_COREHANDLE}) {
# Line 161  sub getChildNodes { Line 193  sub getChildNodes {
193    my $locator = $self->{locator};    my $locator = $self->{locator};
194    #print Dumper($locator); exit;    #print Dumper($locator); exit;
195    if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) {    if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) {
196      my $dbname = getDbNameByDsn($self->{locator}->{dbi}->{dsn});      my $dbname = dsn2dbname($self->{locator}->{dbi}->{dsn});
197      my $key = "Tables_in_$dbname";      my $key = "Tables_in_$dbname";
198      while ( my $row = $result->getNextEntry() ) {      while ( my $row = $result->getNextEntry() ) {
199        push @nodes, $row->{$key};        push @nodes, $row->{$key};
# Line 189  sub sendQuery { Line 221  sub sendQuery {
221    
222    $logger->debug( __PACKAGE__ . "->sendQuery" );    $logger->debug( __PACKAGE__ . "->sendQuery" );
223    
224      my $crud = $query->{options}->{crud};
225      $crud ||= $query->{options}->{action};
226      $crud ||= 'RETRIEVE';
227    
228      if (my $guid = $query->{options}->{GUID}) {
229        $query->{criterias} = [ { key => 'guid', op => 'eq', val => $guid } ];
230      }
231    
232      # check criterias (load & save)
233    #my $sql = "SELECT cs FROM $self->{metainfo}->{$descent}->{node} WHERE $self->{metainfo}->{$descent}->{IdentProvider}->{arg}='$self->{entry}->{source}->{ident}';";    #my $sql = "SELECT cs FROM $self->{metainfo}->{$descent}->{node} WHERE $self->{metainfo}->{$descent}->{IdentProvider}->{arg}='$self->{entry}->{source}->{ident}';";
234    #my $result = $self->{metainfo}->{$descent}->{storage}->sendCommand($sql);    #my $result = $self->{metainfo}->{$descent}->{storage}->sendCommand($sql);
235    my @crits;    my @crits;
# Line 197  sub sendQuery { Line 238  sub sendQuery {
238      $op = '=' if lc $_->{op} eq 'eq';      $op = '=' if lc $_->{op} eq 'eq';
239      push @crits, "$_->{key}$op'$_->{val}'";      push @crits, "$_->{key}$op'$_->{val}'";
240    }    }
   my $subnodes = {};  
   map { $subnodes->{$_}++ } @{$query->{subnodes}};  
241    # HACK: this is hardcoded ;(    expand possibilities!    # HACK: this is hardcoded ;(    expand possibilities!
242    my $crit = join(' AND ', @crits);    my $crit = join(' AND ', @crits);
243    my $sql = hash2Sql($query->{node}, $subnodes, 'SELECT', $crit);    
244    return $self->sendCommand($sql);    # check subnodes (load)
245      my $subnodes = {};
246      map { $subnodes->{$_}++ } @{$query->{subnodes}};
247      
248      # check payload (save)
249      #print Dumper($query->{payload});
250      #$subnodes ||= $query->{payload};
251      
252      # dispatch action
253      if ($crud eq 'RETRIEVE') {
254        my $sql = hash2sql($query->{node}, $subnodes, 'SELECT', $crit);
255        return $self->sendCommand($sql);
256    
257      } elsif ($crud eq 'UPDATE') {
258        my $sql = hash2sql($query->{node}, $query->{payload}, 'UPDATE', $crit);
259        $self->sendCommand($sql);
260    
261      } elsif ($crud eq 'DELETE') {
262        my $sql = hash2sql($query->{node}, $query->{payload}, 'DELETE', $crit);
263        $self->sendCommand($sql);
264    
265      }
266    
267  }  }
268    
269  sub eraseAll {  sub eraseAll {
# Line 257  sub createDb { Line 318  sub createDb {
318        
319  }  }
320    
321  sub getCOREHANDLE2 {  sub getCOREHANDLE {
322    my $self = shift;    my $self = shift;
323    return $self->{_COREHANDLE};    return $self->{_COREHANDLE};
324  }  }
325    
326    sub dropDb {
327      my $self = shift;
328      my $dsn = $self->{locator}->{dbi}->{dsn};
329    
330      $logger->debug( __PACKAGE__ .  "->dropDb( dsn $dsn )" );
331    
332      $dsn =~ s/database=(.+?);//;
333      my $database_name = $1;
334    
335      my $ok;
336      
337      if ( my $dbh = DBI->connect($dsn, '', '', {
338                                                          PrintError => 0,
339                                                          } ) ) {
340        if ($database_name) {
341          if ($dbh->do("DROP DATABASE $database_name;")) {
342            $ok = 1;
343          }
344        }
345    
346        $dbh->disconnect();
347    
348      }
349      
350      return $ok;
351    }
352    
353    sub rebuildDb {
354      my $self = shift;
355      $logger->info( __PACKAGE__ . "->rebuildDb()" );
356      my @results;
357    
358      # sum up results (bool (0/1)) in array
359      #push @results, $self->retreatSchema();
360      push @results, $self->dropDb();
361      push @results, $self->createDb();
362      #push @results, $self->deploySchema();
363    
364      # scan array for "bad ones"
365      my $res = 1;
366      map {
367        $res = 0 if (!$_);
368      } @results;
369      
370      return $res;
371    }
372    
373    sub testAvailability {
374      my $self = shift;
375      my $status = $self->testDsn();
376      $self->{locator}->{status}->{available} = $status;
377      return $status;
378    }
379    
380    sub testDsn {
381      my $self = shift;
382      my $dsn = $self->{locator}->{dbi}->{dsn};
383      my $result;
384      if ( my $dbh = DBI->connect($dsn, '', '', {
385                                                          PrintError => 0,
386                                                          } ) ) {
387        
388        # TODO: REVIEW
389        $dbh->disconnect();
390        
391        return 1;
392      } else {
393        $logger->warning( __PACKAGE__ .  "[$self->{locator}->{type}]" . "->testDsn(): " . "DBI-error: " . $DBI::errstr );
394      }
395    }
396    
397  1;  1;
398    __END__

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

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