| 1 | package # hide this package from CPAN indexer | ||
| 2 | Win32::ODBC; | ||
| 3 | |||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | use DBI; | ||
| 8 | |||
| 9 | # once we've been loaded we don't want perl to load the real Win32::ODBC | ||
| 10 | $INC{'Win32/ODBC.pm'} = $INC{'Win32/DBIODBC.pm'} || 1; | ||
| 11 | |||
| 12 | #my $db = new Win32::ODBC("DSN=$self->{'DSN'};UID=$self->{'UID'};PWD=$self->{'PWD'};"); | ||
| 13 | |||
| 14 | #EMU --- my $db = new Win32::ODBC("DSN=$DSN;UID=$login;PWD=$password;"); | ||
| 15 | sub new | ||
| 16 | { | ||
| 17 | shift; | ||
| 18 | my $connect_line= shift; | ||
| 19 | |||
| 20 | # [R] self-hack to allow empty UID and PWD | ||
| 21 | my $temp_connect_line; | ||
| 22 | $connect_line=~/DSN=\w+/; | ||
| 23 | $temp_connect_line="$&;"; | ||
| 24 | if ($connect_line=~/UID=\w?/) | ||
| 25 | {$temp_connect_line.="$&;";} | ||
| 26 | else {$temp_connect_line.="UID=;";}; | ||
| 27 | if ($connect_line=~/PWD=\w?/) | ||
| 28 | {$temp_connect_line.="$&;";} | ||
| 29 | else {$temp_connect_line.="PWD=;";}; | ||
| 30 | $connect_line=$temp_connect_line; | ||
| 31 | # -[R]- | ||
| 32 | |||
| 33 | my $self= {}; | ||
| 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 | #---- DBI CONNECTION VARIABLES | ||
| 45 | $self->{DBI_DBNAME}=$self->{ODBC_DSN}; | ||
| 46 | $self->{DBI_USER}=$self->{ODBC_UID}; | ||
| 47 | $self->{DBI_PASSWORD}=$self->{ODBC_PWD}; | ||
| 48 | $self->{DBI_DBD}='ODBC'; | ||
| 49 | |||
| 50 | #---- DBI CONNECTION | ||
| 51 | $self->{'DBI_DBH'}=DBI->connect($self->{'DBI_DBNAME'}, | ||
| 52 | $self->{'DBI_USER'},$self->{'DBI_PASSWORD'},$self->{'DBI_DBD'}); | ||
| 53 | |||
| 54 | warn "Error($DBI::err) : $DBI::errstr\n" if ! $self->{'DBI_DBH'}; | ||
| 55 | |||
| 56 | #---- RETURN | ||
| 57 | bless $self; | ||
| 58 | } | ||
| 59 | |||
| 60 | |||
| 61 | #EMU --- $db->Sql('SELECT * FROM DUAL'); | ||
| 62 | sub Sql | ||
| 63 | { | ||
| 64 | my $self= shift; | ||
| 65 | my $SQL_statment=shift; | ||
| 66 | |||
| 67 | # print " SQL : $SQL_statment \n"; | ||
| 68 | |||
| 69 | $self->{'DBI_SQL_STATMENT'}=$SQL_statment; | ||
| 70 | |||
| 71 | my $dbh=$self->{'DBI_DBH'}; | ||
| 72 | |||
| 73 | # print " DBH : $dbh \n"; | ||
| 74 | |||
| 75 | my $sth=$dbh->prepare("$SQL_statment"); | ||
| 76 | |||
| 77 | # print " STH : $sth \n"; | ||
| 78 | |||
| 79 | $self->{'DBI_STH'}=$sth; | ||
| 80 | |||
| 81 | if ($sth) | ||
| 82 | { | ||
| 83 | $sth->execute(); | ||
| 84 | } | ||
| 85 | |||
| 86 | #--- GET ERROR MESSAGES | ||
| 87 | $self->{DBI_ERR}=$DBI::err; | ||
| 88 | $self->{DBI_ERRSTR}=$DBI::errstr; | ||
| 89 | |||
| 90 | if ($sth) | ||
| 91 | { | ||
| 92 | #--- GET COLUMNS NAMES | ||
| 93 | $self->{'DBI_NAME'} = $sth->{NAME}; | ||
| 94 | } | ||
| 95 | |||
| 96 | # [R] provide compatibility with Win32::ODBC's way of identifying erroneous SQL statements | ||
| 97 | return ($self->{'DBI_ERR'})?1:undef; | ||
| 98 | # -[R]- | ||
| 99 | } | ||
| 100 | |||
| 101 | |||
| 102 | #EMU --- $db->FetchRow()) | ||
| 103 | sub FetchRow | ||
| 104 | { | ||
| 105 | my $self= shift; | ||
| 106 | |||
| 107 | my $sth=$self->{'DBI_STH'}; | ||
| 108 | if ($sth) | ||
| 109 | { | ||
| 110 | my @row=$sth->fetchrow_array; | ||
| 111 | $self->{'DBI_ROW'}=\@row; | ||
| 112 | |||
| 113 | if (scalar(@row)>0) | ||
| 114 | { | ||
| 115 | #-- the row of result is not nul | ||
| 116 | #-- return something nothing will be return else | ||
| 117 | return 1; | ||
| 118 | } | ||
| 119 | } | ||
| 120 | return undef; | ||
| 121 | } | ||
| 122 | |||
| 123 | # [R] provide compatibility with Win32::ODBC's Data() method. | ||
| 124 | sub Data | ||
| 125 | { | ||
| 126 | my $self=shift; | ||
| 127 | my @array=@{$self->{'DBI_ROW'}}; | ||
| 128 | foreach my $element (@array) | ||
| 129 | { | ||
| 130 | # remove padding of spaces by DBI | ||
| 131 | $element=~s/(\s*$)//; | ||
| 132 | }; | ||
| 133 | return (wantarray())?@array:join('', @array); | ||
| 134 | }; | ||
| 135 | # -[R]- | ||
| 136 | |||
| 137 | #EMU --- %record = $db->DataHash; | ||
| 138 | sub DataHash | ||
| 139 | { | ||
| 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 | |||
| 182 | #EMU --- $db->Error() | ||
| 183 | sub Error | ||
| 184 | { | ||
| 185 | my $self= shift; | ||
| 186 | |||
| 187 | if ($self->{'DBI_ERR'} ne '') | ||
| 188 | { | ||
| 189 | #--- Return error message | ||
| 190 | $self->{'DBI_ERRSTR'}; | ||
| 191 | } | ||
| 192 | |||
| 193 | #-- else good no error message | ||
| 194 | } | ||
| 195 | |||
| 196 | # [R] provide compatibility with Win32::ODBC's Close() method. | ||
| 197 | sub Close | ||
| 198 | { | ||
| 199 | my $self=shift; | ||
| 200 | |||
| 201 | my $dbh=$self->{'DBI_DBH'}; | ||
| 202 | $dbh->disconnect; | ||
| 203 | } | ||
| 204 | # -[R]- | ||
| 205 | |||
| 206 | 1; | ||
| 207 | |||
| 208 | __END__ |