home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-perl-addon-1.4.9-installer.exe / DBIODBC.pm < prev    next >
Encoding:
Perl POD Document  |  2001-07-13  |  4.7 KB  |  249 lines

  1. package            # hide this package from CPAN indexer
  2.     Win32::ODBC;
  3.  
  4. #use strict;
  5.  
  6. use DBI;
  7.  
  8. # once we've been loaded we don't want perl to load the real Win32::ODBC
  9. $INC{'Win32/ODBC.pm'} = $INC{'Win32/DBIODBC.pm'} || 1;
  10.  
  11. #my $db = new Win32::ODBC("DSN=$self->{'DSN'};UID=$self->{'UID'};PWD=$self->{'PWD'};");
  12.  
  13. #EMU --- my $db = new Win32::ODBC("DSN=$DSN;UID=$login;PWD=$password;");
  14. sub new
  15. {
  16.     shift;
  17.     my $connect_line= shift;
  18.  
  19. # [R] self-hack to allow empty UID and PWD
  20.     my $temp_connect_line;
  21.     $connect_line=~/DSN=\w+/;
  22.     $temp_connect_line="$&;";
  23.     if ($connect_line=~/UID=\w?/)
  24.         {$temp_connect_line.="$&;";}
  25.     else    {$temp_connect_line.="UID=;";};
  26.     if ($connect_line=~/PWD=\w?/)
  27.         {$temp_connect_line.="$&;";}
  28.     else    {$temp_connect_line.="PWD=;";};
  29.     $connect_line=$temp_connect_line;
  30. # -[R]-
  31.     
  32.     my $self= {};
  33.         
  34.     
  35.     $_=$connect_line;
  36.      /^(DSN=)(.*)(;UID=)(.*)(;PWD=)(.*)(;)$/;
  37.  
  38.      #---- DBI CONNECTION VARIABLES
  39.  
  40.      $self->{ODBC_DSN}=$2;
  41.      $self->{ODBC_UID}=$4;
  42.      $self->{ODBC_PWD}=$6;
  43.     
  44.     
  45.     #---- DBI CONNECTION VARIABLES    
  46.     $self->{DBI_DBNAME}=$self->{ODBC_DSN};
  47.     $self->{DBI_USER}=$self->{ODBC_UID};
  48.     $self->{DBI_PASSWORD}=$self->{ODBC_PWD};
  49.     $self->{DBI_DBD}='ODBC';
  50.             
  51.     #---- DBI CONNECTION
  52.     $self->{'DBI_DBH'}=DBI->connect($self->{'DBI_DBNAME'},
  53.             $self->{'DBI_USER'},$self->{'DBI_PASSWORD'},$self->{'DBI_DBD'});
  54.  
  55.     warn "Error($DBI::err) : $DBI::errstr\n" if ! $self->{'DBI_DBH'}; 
  56.  
  57.         
  58.     #---- RETURN 
  59.     
  60.     bless $self;
  61. }
  62.  
  63.  
  64. #EMU --- $db->Sql('SELECT * FROM DUAL');
  65. sub Sql
  66. {
  67.      my $self= shift;
  68.      my $SQL_statment=shift;
  69.  
  70.  #    print " SQL : $SQL_statment \n";
  71.     
  72.     $self->{'DBI_SQL_STATMENT'}=$SQL_statment;
  73.     
  74.     my $dbh=$self->{'DBI_DBH'};
  75.  
  76.  #    print " DBH : $dbh \n";
  77.     
  78.     my $sth=$dbh->prepare("$SQL_statment");
  79.     
  80.  #    print " STH : $sth \n";
  81.     
  82.     $self->{'DBI_STH'}=$sth;
  83.     
  84.     if ($sth)
  85.     {
  86.         $sth->execute();
  87.     }
  88.     
  89.     #--- GET ERROR MESSAGES
  90.     $self->{DBI_ERR}=$DBI::err;
  91.     $self->{DBI_ERRSTR}=$DBI::errstr;
  92.  
  93.     if ($sth)
  94.     {
  95.         #--- GET COLUMNS NAMES
  96.         $self->{'DBI_NAME'} = $sth->{NAME};
  97.     }
  98.  
  99. # [R] provide compatibility with Win32::ODBC's way of identifying erraneous SQL statements
  100.      return ($self->{'DBI_ERR'})?1:undef;
  101. # -[R]-
  102. }
  103.  
  104.  
  105. #EMU --- $db->FetchRow())
  106. sub FetchRow
  107.      my $self= shift;
  108.      
  109.      my $sth=$self->{'DBI_STH'};
  110.      if ($sth)
  111.     {
  112.          my @row=$sth->fetchrow_array;
  113.          $self->{'DBI_ROW'}=\@row;
  114.  
  115.          if (scalar(@row)>0)
  116.          {
  117.             #-- the row of result is not nul
  118.             #-- return somthing nothing will be return else
  119.             return 1;
  120.          }     
  121.     }
  122.     return undef;
  123.  
  124. # [R] provide compatibility with Win32::ODBC's Data() method.
  125. sub Data
  126. {
  127.     my $self=shift;
  128.     my @array=@{$self->{'DBI_ROW'}};
  129.     foreach my $element (@array)
  130.     {
  131.         # remove padding of spaces by DBI
  132.         $element=~s/(\s*$)//;
  133.     };
  134.     return (wantarray())?@array:join('', @array);
  135. };
  136. # -[R]-
  137.  
  138. #EMU --- %record = $db->DataHash;
  139. sub DataHash
  140.      my $self= shift;
  141.           
  142.      my $p_name=$self->{'DBI_NAME'};
  143.      my $p_row=$self->{'DBI_ROW'};
  144.  
  145.      my @name=@$p_name;
  146.      my @row=@$p_row;
  147.  
  148.      my %DataHash;
  149. #print @name; print "\n"; print @row;
  150. # [R] new code that seems to work consistent with Win32::ODBC
  151.     while (@name)
  152.     {
  153.         my $name=shift(@name);
  154.         my $value=shift(@row);
  155.  
  156.         # remove padding of spaces by DBI
  157.         $name=~s/(\s*$)//;
  158.         $value=~s/(\s*$)//;
  159.  
  160.         $DataHash{$name}=$value;
  161.     };
  162. # -[R]-
  163.  
  164. # [R] old code that didn't appear to work
  165. #    foreach my $name (@name)
  166. #    {
  167. #        $name=~s/(^\s*)|(\s*$)//;
  168. #        my @arr=@$name;
  169. #        foreach (@arr)
  170. #        {
  171. #            print "lot $name  name  col $_   or ROW= 0 $row[0]  1 $row[1] 2 $row[2] \n ";
  172. #            $DataHash{$name}=shift(@row);
  173. #        }
  174. #    }
  175. # -[R]-
  176.  
  177.      #--- Return Hash
  178.      return %DataHash;     
  179.  
  180.  
  181. #EMU --- $db->Error()
  182. sub Error
  183.      my $self= shift;
  184.           
  185.      if ($self->{'DBI_ERR'} ne '')
  186.      {
  187.         #--- Return error message
  188.         $self->{'DBI_ERRSTR'};
  189.      }
  190.  
  191.      #-- else good no error message     
  192.      
  193. }
  194.  
  195. # [R] provide compatibility with Win32::ODBC's Close() method.
  196. sub Close
  197. {
  198.     my $self=shift;
  199.  
  200.     my $dbh=$self->{'DBI_DBH'};
  201.     $dbh->disconnect;
  202. }
  203. # -[R]-
  204.  
  205. 1;
  206.  
  207. __END__
  208.  
  209. # [R] to -[R]- indicate sections edited by me, Roy Lee
  210.  
  211. =head1 NAME
  212.  
  213. Win32::DBIODBC - Win32::ODBC emulation layer for the DBI
  214.  
  215. =head1 SYNOPSIS
  216.  
  217.   use Win32::DBIODBC;     # instead of use Win32::ODBC
  218.  
  219. =head1 DESCRIPTION
  220.  
  221. This is a I<very> basic I<very> alpha quality Win32::ODBC emulation
  222. for the DBI. To use it just replace
  223.  
  224.     use Win32::ODBC;
  225.  
  226. in your scripts with
  227.  
  228.     use Win32::DBIODBC;
  229.  
  230. or, while experimenting, you can pre-load this module without changing your
  231. scripts by doing
  232.  
  233.     perl -MWin32::DBIODBC your_script_name
  234.  
  235. =head1 TO DO
  236.  
  237. Error handling is virtually non-existant.
  238.  
  239. =head1 AUTHOR
  240.  
  241. Tom Horen <tho@melexis.com>
  242.  
  243. =cut
  244.