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

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

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