Commit | Line | Data |
---|---|---|
86530b38 AT |
1 | package |
2 | DBI; # hide this non-DBI package from simple indexers | |
3 | ||
4 | # $Id: W32ODBC.pm,v 10.3 1999/05/06 17:29:14 timbo Exp $ | |
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 = substr(q$Revision: 10.3 $, 10); | |
44 | ||
45 | sub DBI::W32ODBC::import { } # must trick here since we're called DBI/W32ODBC.pm | |
46 | ||
47 | ||
48 | use Carp; | |
49 | ||
50 | use Win32::ODBC; | |
51 | ||
52 | @ISA = qw(Win32::ODBC); | |
53 | ||
54 | use strict; | |
55 | ||
56 | $DBI::dbi_debug = $ENV{PERL_DBI_DEBUG} || 0; | |
57 | carp "Loaded (W32ODBC) DBI.pm ${'DBI::VERSION'} (debug $DBI::dbi_debug)" | |
58 | if $DBI::dbi_debug; | |
59 | ||
60 | ||
61 | ||
62 | sub connect { | |
63 | my ($class, $dbname, $dbuser, $dbpasswd, $module, $attr) = @_; | |
64 | $dbname .= ";UID=$dbuser" if $dbuser; | |
65 | $dbname .= ";PWD=$dbpasswd" if $dbpasswd; | |
66 | my $h = new Win32::ODBC $dbname; | |
67 | warn "Error connecting to $dbname: ".Win32::ODBC::Error()."\n" unless $h; | |
68 | bless $h, $class if $h; # rebless into our class | |
69 | $h; | |
70 | } | |
71 | ||
72 | ||
73 | sub quote { | |
74 | my ($h, $string) = @_; | |
75 | return "NULL" if !defined $string; | |
76 | $string =~ s/'/''/g; # standard | |
77 | # This hack seems to be required for Access but probably breaks for | |
78 | # other databases when using \r and \n. It would be better if we could | |
79 | # use ODBC options to detect that we're actually using Access. | |
80 | $string =~ s/\r/' & chr\$(13) & '/g; | |
81 | $string =~ s/\n/' & chr\$(10) & '/g; | |
82 | "'$string'"; | |
83 | } | |
84 | ||
85 | sub do { | |
86 | my($h, $statement, $attribs, @params) = @_; | |
87 | Carp::carp "\$h->do() attribs unused" if $attribs; | |
88 | my $new_h = $h->prepare($statement) or return undef; ## | |
89 | pop @{ $h->{'___sths'} }; ## certian death assured | |
90 | $new_h->execute(@params) or return undef; ## | |
91 | my $rows = $new_h->rows; ## | |
92 | $new_h->finish; ## bang bang | |
93 | ($rows == 0) ? "0E0" : $rows; | |
94 | } | |
95 | ||
96 | # --- | |
97 | ||
98 | sub prepare { | |
99 | my ($h, $sql) = @_; | |
100 | ## opens a new connection with every prepare to allow | |
101 | ## multiple, concurrent queries | |
102 | my $new_h = new Win32::ODBC $h->{DSN}; ## | |
103 | return undef if not $new_h; ## bail if no connection | |
104 | bless $new_h; ## shouldn't be sub-classed... | |
105 | $new_h->{'__prepare'} = $sql; ## | |
106 | $new_h->{NAME} = []; ## | |
107 | $new_h->{NUM_OF_FIELDS} = -1; ## | |
108 | push @{ $h->{'___sths'} } ,$new_h; ## save sth in parent for mass destruction | |
109 | return $new_h; ## | |
110 | } | |
111 | ||
112 | sub execute { | |
113 | my ($h) = @_; | |
114 | my $rc = $h->Sql($h->{'__prepare'}); | |
115 | return undef if $rc; | |
116 | my @fields = $h->FieldNames; | |
117 | $h->{NAME} = \@fields; | |
118 | $h->{NUM_OF_FIELDS} = scalar @fields; | |
119 | $h; # return dbh as pseudo sth | |
120 | } | |
121 | ||
122 | ||
123 | sub fetchrow_hashref { ## provide DBI compatibility | |
124 | my $h = shift; | |
125 | my $NAME = shift || "NAME"; | |
126 | my $row = $h->fetchrow_arrayref or return undef; | |
127 | my %hash; | |
128 | @hash{ @{ $h->{$NAME} } } = @$row; | |
129 | return \%hash; | |
130 | } | |
131 | ||
132 | sub fetchrow { | |
133 | my $h = shift; | |
134 | return unless $h->FetchRow(); | |
135 | my $fields_r = $h->{NAME}; | |
136 | return $h->Data(@$fields_r); | |
137 | } | |
138 | sub fetch { | |
139 | my @row = shift->fetchrow; | |
140 | return undef unless @row; | |
141 | return \@row; | |
142 | } | |
143 | *fetchrow_arrayref = \&fetch; ## provide DBI compatibility | |
144 | *fetchrow_array = \&fetchrow; ## provide DBI compatibility | |
145 | ||
146 | sub rows { | |
147 | shift->RowCount; | |
148 | } | |
149 | ||
150 | sub finish { | |
151 | shift->Close; ## uncommented this line | |
152 | } | |
153 | ||
154 | # --- | |
155 | ||
156 | sub commit { | |
157 | shift->Transact(ODBC::SQL_COMMIT); | |
158 | } | |
159 | sub rollback { | |
160 | shift->Transact(ODBC::SQL_ROLLBACK); | |
161 | } | |
162 | ||
163 | sub disconnect { | |
164 | my ($h) = shift; ## this will kill all the statement handles | |
165 | foreach (@{$h->{'___sths'}}) { ## created for a specific connection | |
166 | $_->Close if $_->{DSN}; ## | |
167 | } ## | |
168 | $h->Close; ## | |
169 | } | |
170 | ||
171 | sub err { | |
172 | (shift->Error)[0]; | |
173 | } | |
174 | sub errstr { | |
175 | scalar( shift->Error ); | |
176 | } | |
177 | ||
178 | # --- | |
179 | ||
180 | 1; |