home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / Share / Editores / Perl5 / perl / lib / site / DBD / ExampleP.pm next >
Encoding:
Text File  |  1997-08-10  |  4.9 KB  |  200 lines

  1. {
  2.     package DBD::ExampleP;
  3.  
  4.     require DBI;
  5.  
  6.     @EXPORT = qw(); # Do NOT @EXPORT anything.
  7.  
  8. #   $Id: ExampleP.pm,v 1.6 1997/07/22 23:17:50 timbo Exp $
  9. #
  10. #   Copyright (c) 1994, Tim Bunce
  11. #
  12. #   You may distribute under the terms of either the GNU General Public
  13. #   License or the Artistic License, as specified in the Perl README file.
  14.  
  15.     @statnames = qw(dev ino mode nlink uid gid
  16.     rdev size atime mtime ctime blksize blocks name);
  17.     @statnames{@statnames} = (0 .. @statnames-1);
  18.     @stattypes = qw(1 1 1 1 1 1 1 1 3 3 3 1 1 2);
  19.     @stattypes{@statnames} = @stattypes;
  20.  
  21.     $drh = undef;    # holds driver handle once initialised
  22.     $err = 0;        # The $DBI::err value
  23.     $gensym = "SYM000"; # used by st::execute() for filehandles
  24.  
  25.     sub driver{
  26.     return $drh if $drh;
  27.     my($class, $attr) = @_;
  28.     $class .= "::dr";
  29.     ($drh) = DBI::_new_drh($class, {
  30.         'Name' => 'ExampleP',
  31.         'Version' => '$Revision: 1.6 $',
  32.         'Attribution' => 'DBD Example Perl stub by Tim Bunce',
  33.         }, ['example implementors private data']);
  34.     $drh;
  35.     }
  36.  
  37.     1;
  38. }
  39.  
  40.  
  41. {   package DBD::ExampleP::dr; # ====== DRIVER ======
  42.     $imp_data_size = 0;
  43.     use strict;
  44.  
  45.     sub my_handler {
  46.     my($self, $type, @args) = @_;
  47.     return 0 unless $type eq 'ERROR';
  48.     ${$self->{Err}}    = $args[0];
  49.     ${$self->{Errstr}} = $args[1];
  50.     1;    # handled
  51.     }
  52.  
  53.     sub connect { # normally overridden, but a handy default
  54.         my($drh, $dbname, $user, $auth)= @_;
  55.         my($this) = DBI::_new_dbh($drh, {
  56.         'Name' => $dbname,
  57.         'User' => $user,
  58.         'Handlers' => [ \&my_handler ],    # deprecated, don't do this
  59.         });
  60.         $this;
  61.     }
  62.  
  63.     sub disconnect_all {
  64.     # we don't need to tidy up anything
  65.     }
  66.     sub DESTROY { undef }
  67. }
  68.  
  69.  
  70. {   package DBD::ExampleP::db; # ====== DATABASE ======
  71.     $imp_data_size = 0;
  72.     use strict;
  73.  
  74.     sub prepare {
  75.     my($dbh, $statement)= @_;
  76.     my($fields, $param)
  77.         = $statement =~ m/^select ([\w,\s]+)\s+from\s+(.*?)/i;
  78.     my(@fields) = split(/\s*,\s*/, $fields);
  79.  
  80.     my(@bad) = map($DBD::ExampleP::statnames{$_} ? () : $_, @fields);
  81.     if (@bad) {
  82.         $dbh->event("ERROR", 1, "Unknown field names: @bad");
  83.         return undef;
  84.     }
  85.  
  86.     my($outer, $sth) = DBI::_new_sth($dbh, {
  87.         'Statement'     => $statement,
  88.         'fields'        => \@fields,
  89.         }, ['example implementors private data']);
  90.  
  91.     $outer->{NAME} = \@fields;
  92.     $outer->{NULLABLE} = (0) x @fields;
  93.     $outer->{NUM_OF_FIELDS} = @fields;
  94.     $outer->{NUM_OF_PARAMS} = 1;
  95.  
  96.     $outer;
  97.     }
  98.  
  99.     sub disconnect {
  100.     return 1;
  101.     }
  102.  
  103.     sub FETCH {
  104.     my ($dbh, $attrib) = @_;
  105.     # In reality this would interrogate the database engine to
  106.     # either return dynamic values that cannot be precomputed
  107.     # or fetch and cache attribute values too expensive to prefetch.
  108.     return 1 if $attrib eq 'AutoCommit';
  109.     # else pass up to DBI to handle
  110.     return $dbh->DBD::_::st::FETCH($attrib);
  111.     }
  112.  
  113.     sub STORE {
  114.     my ($dbh, $attrib, $value) = @_;
  115.     # would normally validate and only store known attributes
  116.     # else pass up to DBI to handle
  117.     if ($attrib eq 'AutoCommit') {
  118.         return 1 if $value;    # is already set
  119.         croak("Can't disable AutoCommit");
  120.     }
  121.     return $dbh->DBD::_::st::STORE($attrib, $value);
  122.     }
  123.     sub DESTROY { undef }
  124. }
  125.  
  126.  
  127. {   package DBD::ExampleP::st; # ====== STATEMENT ======
  128.     $imp_data_size = 0;
  129.     use strict; no strict 'refs'; # cause problems with filehandles
  130.  
  131.     sub bind_param {
  132.     my($sth, $param, $value, $attribs) = @_;
  133.     $sth->{'param'}->[$param] = $value;
  134.     }
  135.     
  136.     sub execute {
  137.     my($sth, @dir) = @_;
  138.     my $dir;
  139.     if (@dir) {
  140.         $dir = $dir[0];
  141.     } else {
  142.         $dir = $sth->{'param'}->[1] || die "No bind_param";
  143.     }
  144.     $sth->finish;
  145.     $sth->{'datahandle'} = "DBD::ExampleP::".++$DBD::ExampleP::gensym;
  146.     opendir($sth->{'datahandle'}, $dir)
  147.         or ($sth->event("ERROR", 2, "opendir($dir): $!"), return undef);
  148.     $sth->{'dir'} = $dir;
  149.     1;
  150.     }
  151.  
  152.     sub fetch {
  153.     my($sth) = @_;
  154.     my $f = readdir($sth->{'datahandle'});
  155.     unless($f){
  156.         $sth->finish;     # no more data so finish
  157.         return ();
  158.     }
  159.     my(%s); # fancy a slice of a hash?
  160.     # put in all the data fields
  161.     @s{@DBD::ExampleP::statnames} = (stat("$sth->{'dir'}/$f"), $f);
  162.     # return just what fields the query asks for
  163.     [ @s{ @{$sth->{'fields'}} } ];
  164.     }
  165.  
  166.     sub finish {
  167.     my($sth) = @_;
  168.     return undef unless $sth->{'datahandle'};
  169.     closedir($sth->{'datahandle'});
  170.     $sth->{'datahandle'} = undef;
  171.     return 1;
  172.     }
  173.  
  174.     sub FETCH {
  175.     my ($sth, $attrib) = @_;
  176.     # In reality this would interrogate the database engine to
  177.     # either return dynamic values that cannot be precomputed
  178.     # or fetch and cache attribute values too expensive to prefetch.
  179.     if ($attrib eq 'DATA_TYPE'){
  180.         my(@t) = @DBD::ExampleP::stattypes{@{$sth->{'fields'}}};
  181.         return \@t;
  182.     }
  183.     # else pass up to DBI to handle
  184.     return $sth->DBD::_::st::FETCH($attrib);
  185.     }
  186.  
  187.     sub STORE {
  188.     my ($sth, $attrib, $value) = @_;
  189.     # would normally validate and only store known attributes
  190.     # else pass up to DBI to handle
  191.     return $sth->{$attrib}=$value
  192.         if $attrib eq 'NAME' or $attrib eq 'NULLABLE';
  193.     return $sth->DBD::_::st::STORE($attrib, $value);
  194.     }
  195.  
  196.     sub DESTROY { undef }
  197. }
  198.  
  199. 1;
  200.