blib/lib/DBI/W32ODBC.pm untested

filter: Statementstmt 0.0% 0 / 84 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 16 totaltotal 0.0% 0 / 161 CC 13 SCAR 21.8
CC
13
Cov
0%
connect
30.0
do
12.0
quote
6.0
CRAP 8.8
1 package
2 DBI; # hide this non-DBI package from simple indexers
3  
4 # $Id: W32ODBC.pm 8696 2007-01-24 23:12:38Z Tim $
5 #
6 # Copyright (c) 1997,1999 Tim Bunce
7 # With many thanks to Patrick Hollins for polishing.
8 #
9 # You may distribute under the terms of either the GNU General Public
10 # License or the Artistic License, as specified in the Perl README file.
11  
12 =head1 NAME
13  
14 DBI::W32ODBC - An experimental DBI emulation layer for Win32::ODBC
15  
16 =head1 SYNOPSIS
17  
18 use DBI::W32ODBC;
19  
20 # apart from the line above everything is just the same as with
21 # the real DBI when using a basic driver with few features.
22  
23 =head1 DESCRIPTION
24  
25 This is an experimental pure perl DBI emulation layer for Win32::ODBC
26  
27 If you can improve this code I'd be interested in hearing about it. If
28 you are having trouble using it please respect the fact that it's very
29 experimental. Ideally fix it yourself and send me the details.
30  
31 =head2 Some Things Not Yet Implemented
32  
33 Most attributes including PrintError & RaiseError.
34 type_info and table_info
35  
36 Volunteers welcome!
37  
38 =cut
39  
40 ${'DBI::VERSION'} # hide version from PAUSE indexer
41 = "0.01";
42  
43 my $Revision = sprintf("12.%06d", q$Revision: 8696 $ =~ /(\d+)/o);
44  
45  
46 sub DBI::W32ODBC::import { } # must trick here since we're called DBI/W32ODBC.pm
47  
48  
49 use Carp;
50  
51 use Win32::ODBC;
52  
53 @ISA = qw(Win32::ODBC);
54  
55 use strict;
56 use warnings;
57  
58 $DBI::dbi_debug = $ENV{PERL_DBI_DEBUG} || 0;
59 carp "Loaded (W32ODBC) DBI.pm ${'DBI::VERSION'} (debug $DBI::dbi_debug)"
60 if $DBI::dbi_debug;
61  
62  
63  
64 sub connect {
65 my ($class, $dbname, $dbuser, $dbpasswd, $module, $attr) = @_;
66 $dbname .= ";UID=$dbuser" if $dbuser;
67 $dbname .= ";PWD=$dbpasswd" if $dbpasswd;
68 my $h = new Win32::ODBC $dbname;
69 warn "Error connecting to $dbname: ".Win32::ODBC::Error()."\n" unless $h;
70 bless $h, $class if $h; # rebless into our class
71 $h;
72 }
73  
74  
75 sub quote {
76 my ($h, $string) = @_;
77 return "NULL" if !defined $string;
78 $string =~ s/'/''/g; # standard
79 # This hack seems to be required for Access but probably breaks for
80 # other databases when using \r and \n. It would be better if we could
81 # use ODBC options to detect that we're actually using Access.
82 $string =~ s/\r/' & chr\$(13) & '/g;
83 $string =~ s/\n/' & chr\$(10) & '/g;
84 "'$string'";
85 }
86  
87 sub do {
88 my($h, $statement, $attribs, @params) = @_;
89 Carp::carp "\$h->do() attribs unused" if $attribs;
90 my $new_h = $h->prepare($statement) or return undef; ##
91 pop @{ $h->{'___sths'} }; ## certain death assured
92 $new_h->execute(@params) or return undef; ##
93 my $rows = $new_h->rows; ##
94 $new_h->finish; ## bang bang
95 ($rows == 0) ? "0E0" : $rows;
96 }
97  
98 # ---
99  
100 sub prepare {
101 my ($h, $sql) = @_;
102 ## opens a new connection with every prepare to allow
103 ## multiple, concurrent queries
104 my $new_h = new Win32::ODBC $h->{DSN}; ##
105 return undef if not $new_h; ## bail if no connection
106 bless $new_h; ## shouldn't be sub-classed...
107 $new_h->{'__prepare'} = $sql; ##
108 $new_h->{NAME} = []; ##
109 $new_h->{NUM_OF_FIELDS} = -1; ##
110 push @{ $h->{'___sths'} } ,$new_h; ## save sth in parent for mass destruction
111 return $new_h; ##
112 }
113  
114 sub execute {
115 my ($h) = @_;
116 my $rc = $h->Sql($h->{'__prepare'});
117 return undef if $rc;
118 my @fields = $h->FieldNames;
119 $h->{NAME} = \@fields;
120 $h->{NUM_OF_FIELDS} = scalar @fields;
121 $h; # return dbh as pseudo sth
122 }
123  
124  
125 sub fetchrow_hashref { ## provide DBI compatibility
126 my $h = shift;
127 my $NAME = shift || "NAME";
128 my $row = $h->fetchrow_arrayref or return undef;
129 my %hash;
130 @hash{ @{ $h->{$NAME} } } = @$row;
131 return \%hash;
132 }
133  
134 sub fetchrow {
135 my $h = shift;
136 return unless $h->FetchRow();
137 my $fields_r = $h->{NAME};
138 return $h->Data(@$fields_r);
139 }
140 sub fetch {
141 my @row = shift->fetchrow;
142 return undef unless @row;
143 return \@row;
144 }
145 *fetchrow_arrayref = \&fetch; ## provide DBI compatibility
146 *fetchrow_array = \&fetchrow; ## provide DBI compatibility
147  
148 sub rows {
149 shift->RowCount;
150 }
151  
152 sub finish {
153 shift->Close; ## uncommented this line
154 }
155  
156 # ---
157  
158 sub commit {
159 shift->Transact(ODBC::SQL_COMMIT);
160 }
161 sub rollback {
162 shift->Transact(ODBC::SQL_ROLLBACK);
163 }
164  
165 sub disconnect {
166 my ($h) = shift; ## this will kill all the statement handles
167 foreach (@{$h->{'___sths'}}) { ## created for a specific connection
168 $_->Close if $_->{DSN}; ##
169 } ##
170 $h->Close; ##
171 }
172  
173 sub err {
174 (shift->Error)[0];
175 }
176 sub errstr {
177 scalar( shift->Error );
178 }
179  
180 # ---
181  
182 1;