/[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.11 by joko, Thu Dec 19 16:31:05 2002 UTC revision 1.14 by joko, Wed Apr 9 06:06:04 2003 UTC
# Line 3  Line 3 
3  #  $Id$  #  $Id$
4  #  #
5  #  $Log$  #  $Log$
6    #  Revision 1.14  2003/04/09 06:06:04  joko
7    #  sendQuery now is capable of doing 'SELECT'- or 'INSERT'-queries
8    #
9    #  Revision 1.13  2003/04/08 23:06:45  joko
10    #  renamed core database helper functions
11    #
12    #  Revision 1.12  2003/01/30 22:28:21  joko
13    #  + implemented new concrete methods
14    #
15  #  Revision 1.11  2002/12/19 16:31:05  joko  #  Revision 1.11  2002/12/19 16:31:05  joko
16  #  + sub dropDb  #  + sub dropDb
17  #  + sub rebuildDb  #  + sub rebuildDb
# Line 51  use warnings; Line 60  use warnings;
60    
61  use base ("Data::Storage::Handler::Abstract");  use base ("Data::Storage::Handler::Abstract");
62    
63    
64  use DBI;  use DBI;
65  use Data::Dumper;  use Data::Dumper;
66  use libdb qw( getDbNameByDsn hash2Sql );  use shortcuts::db qw( hash2sql dsn2dbname );
67  use Data::Storage::Result::DBI;  use Data::Storage::Result::DBI;
68    
69    
70  # get logger instance  # get logger instance
71  my $logger = Log::Dispatch::Config->instance;  my $logger = Log::Dispatch::Config->instance;
72    
# Line 165  sub getChildNodes { Line 176  sub getChildNodes {
176    my $locator = $self->{locator};    my $locator = $self->{locator};
177    #print Dumper($locator); exit;    #print Dumper($locator); exit;
178    if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) {    if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) {
179      my $dbname = getDbNameByDsn($self->{locator}->{dbi}->{dsn});      my $dbname = dsn2dbname($self->{locator}->{dbi}->{dsn});
180      my $key = "Tables_in_$dbname";      my $key = "Tables_in_$dbname";
181      while ( my $row = $result->getNextEntry() ) {      while ( my $row = $result->getNextEntry() ) {
182        push @nodes, $row->{$key};        push @nodes, $row->{$key};
# Line 193  sub sendQuery { Line 204  sub sendQuery {
204    
205    $logger->debug( __PACKAGE__ . "->sendQuery" );    $logger->debug( __PACKAGE__ . "->sendQuery" );
206    
207      my $action = $query->{options}->{action};
208      $action ||= 'load';
209    
210      if (my $guid = $query->{options}->{GUID}) {
211        $query->{criterias} = [ { key => 'guid', op => 'eq', val => $guid } ];
212      }
213    
214      # check criterias (load & save)
215    #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}';";
216    #my $result = $self->{metainfo}->{$descent}->{storage}->sendCommand($sql);    #my $result = $self->{metainfo}->{$descent}->{storage}->sendCommand($sql);
217    my @crits;    my @crits;
# Line 201  sub sendQuery { Line 220  sub sendQuery {
220      $op = '=' if lc $_->{op} eq 'eq';      $op = '=' if lc $_->{op} eq 'eq';
221      push @crits, "$_->{key}$op'$_->{val}'";      push @crits, "$_->{key}$op'$_->{val}'";
222    }    }
   my $subnodes = {};  
   map { $subnodes->{$_}++ } @{$query->{subnodes}};  
223    # HACK: this is hardcoded ;(    expand possibilities!    # HACK: this is hardcoded ;(    expand possibilities!
224    my $crit = join(' AND ', @crits);    my $crit = join(' AND ', @crits);
225    my $sql = hash2Sql($query->{node}, $subnodes, 'SELECT', $crit);    
226    return $self->sendCommand($sql);    # check subnodes (load)
227      my $subnodes = {};
228      map { $subnodes->{$_}++ } @{$query->{subnodes}};
229      
230      # check payload (save)
231      #print Dumper($query->{payload});
232      #$subnodes ||= $query->{payload};
233      
234      # dispatch action
235      if ($action eq 'load') {
236        my $sql = hash2sql($query->{node}, $subnodes, 'SELECT', $crit);
237        return $self->sendCommand($sql);
238      } elsif ($action eq 'save') {
239        my $sql = hash2sql($query->{node}, $query->{payload}, 'UPDATE', $crit);
240        $self->sendCommand($sql);
241      }
242    
243  }  }
244    
245  sub eraseAll {  sub eraseAll {
# Line 313  sub rebuildDb { Line 346  sub rebuildDb {
346    return $res;    return $res;
347  }  }
348    
349    sub testAvailability {
350      my $self = shift;
351      my $status = $self->testDsn();
352      $self->{locator}->{status}->{available} = $status;
353      return $status;
354    }
355    
356    sub testDsn {
357      my $self = shift;
358      my $dsn = $self->{locator}->{dbi}->{dsn};
359      my $result;
360      if ( my $dbh = DBI->connect($dsn, '', '', {
361                                                          PrintError => 0,
362                                                          } ) ) {
363        
364        # TODO: REVIEW
365        $dbh->disconnect();
366        
367        return 1;
368      } else {
369        $logger->warning( __PACKAGE__ .  "[$self->{locator}->{type}]" . "->testDsn(): " . "DBI-error: " . $DBI::errstr );
370      }
371    }
372    
373  1;  1;
374    __END__

Legend:
Removed from v.1.11  
changed lines
  Added in v.1.14

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