| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | # Efetua conexao com o banco de dados de forma eficiente, | 
| 2 |  |  |  |  |  |  | # simples e rapida. | 
| 3 |  |  |  |  |  |  | # Compativel apenas com MySQL por enquanto | 
| 4 |  |  |  |  |  |  | # By: Udlei Nattis | 
| 5 |  |  |  |  |  |  |  | 
| 6 |  |  |  |  |  |  | # {type},{db},{host},{username},{passwd} | 
| 7 |  |  |  |  |  |  | # MySQL vars: host,username,passwd,db | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | package NTS::SqlLink; | 
| 10 |  |  |  |  |  |  |  | 
| 11 | 1 |  |  | 1 |  | 22516 | use vars qw($conn); | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 55 |  | 
| 12 | 1 |  |  | 1 |  | 2219 | use DBI; | 
|  | 1 |  |  |  |  | 18251 |  | 
|  | 1 |  |  |  |  | 92 |  | 
| 13 | 1 |  |  | 1 |  | 11 | use strict; | 
|  | 1 |  |  |  |  | 10 |  | 
|  | 1 |  |  |  |  | 38 |  | 
| 14 | 1 |  |  | 1 |  | 8 | use warnings FATAL => 'all'; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 887 |  | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | our $VERSION = '2.0'; | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | # Inicia conexao | 
| 19 |  |  |  |  |  |  | sub new { | 
| 20 | 0 |  |  | 0 | 1 |  | my($self,$vars) = @_; | 
| 21 | 0 |  |  |  |  |  | my ($conn); | 
| 22 |  |  |  |  |  |  |  | 
| 23 | 0 | 0 |  |  |  |  | $vars->{acommit} = 0 unless defined $vars->{acommit}; | 
| 24 | 0 |  |  |  |  |  | $vars->{acommit} = $vars->{acommit}; | 
| 25 |  |  |  |  |  |  |  | 
| 26 |  |  |  |  |  |  | # Verifica se é mysql | 
| 27 | 0 | 0 |  |  |  |  | if ($vars->{type} eq "mysql") { | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | # Faz conexao | 
| 30 | 0 | 0 |  |  |  |  | $conn = DBI->connect("DBI:$vars->{type}:$vars->{db}:$vars->{host}", | 
| 31 |  |  |  |  |  |  | $vars->{username},$vars->{passwd},{  AutoCommit => $vars->{acommit} }) | 
| 32 |  |  |  |  |  |  | or die DBI::errstr; | 
| 33 |  |  |  |  |  |  |  | 
| 34 | 0 |  |  |  |  |  | bless { | 
| 35 |  |  |  |  |  |  | conn => $conn, | 
| 36 |  |  |  |  |  |  | type => $vars->{type}, | 
| 37 |  |  |  |  |  |  | error => undef, | 
| 38 |  |  |  |  |  |  | pre => undef, | 
| 39 |  |  |  |  |  |  | arows => undef, | 
| 40 |  |  |  |  |  |  | }, $self; | 
| 41 |  |  |  |  |  |  | } | 
| 42 |  |  |  |  |  |  | } | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  | # Recupera dados do db | 
| 45 |  |  |  |  |  |  | sub return { | 
| 46 | 0 |  |  | 0 | 1 |  | my ($self,$q,$t) = @_; | 
| 47 | 0 |  |  |  |  |  | my (@array,@row,$row); | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  | # Verifica formato de dados que deve retorna | 
| 50 | 0 | 0 |  |  |  |  | $t = "scalar" if (!$t); | 
| 51 |  |  |  |  |  |  |  | 
| 52 |  |  |  |  |  |  | # Prepara query | 
| 53 | 0 |  |  |  |  |  | eval { $self->{pre} = $self->{conn}->prepare($q); }; | 
|  | 0 |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  | # Verifica se conseguiu executar a query | 
| 56 | 0 |  |  |  |  |  | eval $self->{pre}->execute; | 
| 57 | 0 | 0 |  |  |  |  | if ($@) { | 
| 58 | 0 |  |  |  |  |  | die "\n".DBI::err.": ".DBI::errstr; | 
| 59 |  |  |  |  |  |  | }; | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  | # Retorna em formato array | 
| 62 | 0 | 0 |  |  |  |  | if ($t eq "array") { | 
|  |  | 0 |  |  |  |  |  | 
| 63 | 0 |  |  |  |  |  | while (@row = $self->{pre}->fetchrow_array) { | 
| 64 | 0 |  |  |  |  |  | push(@array,[ @row ]); | 
| 65 |  |  |  |  |  |  | } | 
| 66 |  |  |  |  |  |  | } | 
| 67 |  |  |  |  |  |  |  | 
| 68 |  |  |  |  |  |  | # Retorna em formato hash | 
| 69 |  |  |  |  |  |  | elsif ($t eq "scalar") { | 
| 70 | 0 |  |  |  |  |  | eval { | 
| 71 | 0 |  |  |  |  |  | while ($row = $self->{pre}->fetchrow_hashref) { | 
| 72 | 0 |  |  |  |  |  | push(@array,$row); | 
| 73 |  |  |  |  |  |  | } | 
| 74 |  |  |  |  |  |  | }; | 
| 75 |  |  |  |  |  |  | } | 
| 76 |  |  |  |  |  |  |  | 
| 77 | 0 |  |  |  |  |  | eval { $self->{pre}->finish }; | 
|  | 0 |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | # Apaga variaveis indesejadas | 
| 80 |  |  |  |  |  |  | #print $q."\n"; | 
| 81 | 0 |  |  |  |  |  | undef $q; undef $t; | 
|  | 0 |  |  |  |  |  |  | 
| 82 |  |  |  |  |  |  |  | 
| 83 |  |  |  |  |  |  | #    print $self->{conn}->{mysql_info}."\n" if defined $self->{conn}->{mysql_info}; | 
| 84 |  |  |  |  |  |  | #    print $self->{conn}->{mysql_stat}."\n"; | 
| 85 |  |  |  |  |  |  |  | 
| 86 |  |  |  |  |  |  | # Retorna os resultados do select | 
| 87 | 0 |  |  |  |  |  | return @array; | 
| 88 |  |  |  |  |  |  | } | 
| 89 |  |  |  |  |  |  |  | 
| 90 |  |  |  |  |  |  | # executa funcao 'do' | 
| 91 |  |  |  |  |  |  | sub do { | 
| 92 | 0 |  |  | 0 | 1 |  | my ($self,$q) = @_; | 
| 93 |  |  |  |  |  |  |  | 
| 94 | 0 | 0 |  |  |  |  | eval { $self->{arows} = $self->{conn}->do($q); } | 
|  | 0 |  |  |  |  |  |  | 
| 95 |  |  |  |  |  |  | or die $q."\n".DBI::err.": ".DBI::errstr; | 
| 96 |  |  |  |  |  |  |  | 
| 97 |  |  |  |  |  |  | #if (DBI::errstr) { | 
| 98 |  |  |  |  |  |  | #    $self->{error} = DBI::errstr; | 
| 99 |  |  |  |  |  |  | #    return 0; | 
| 100 |  |  |  |  |  |  | #} | 
| 101 |  |  |  |  |  |  |  | 
| 102 | 0 |  |  |  |  |  | undef $q; | 
| 103 |  |  |  |  |  |  |  | 
| 104 | 0 |  |  |  |  |  | return 1; | 
| 105 |  |  |  |  |  |  | } | 
| 106 |  |  |  |  |  |  |  | 
| 107 |  |  |  |  |  |  | # commit | 
| 108 |  |  |  |  |  |  | sub commit { | 
| 109 | 0 |  |  | 0 | 0 |  | my ($self) = @_; | 
| 110 |  |  |  |  |  |  |  | 
| 111 | 0 |  |  |  |  |  | $self->{conn}->commit(); | 
| 112 |  |  |  |  |  |  |  | 
| 113 | 0 |  |  |  |  |  | return 1; | 
| 114 |  |  |  |  |  |  | } | 
| 115 |  |  |  |  |  |  |  | 
| 116 |  |  |  |  |  |  | # rollback | 
| 117 |  |  |  |  |  |  | sub rollback { | 
| 118 | 0 |  |  | 0 | 0 |  | my ($self) = @_; | 
| 119 |  |  |  |  |  |  |  | 
| 120 | 0 |  |  |  |  |  | $self->{conn}->rollback(); | 
| 121 |  |  |  |  |  |  |  | 
| 122 | 0 |  |  |  |  |  | return 1; | 
| 123 |  |  |  |  |  |  | } | 
| 124 |  |  |  |  |  |  |  | 
| 125 |  |  |  |  |  |  | # Adiciona quote | 
| 126 |  |  |  |  |  |  | sub qt { | 
| 127 | 0 |  |  | 0 | 1 |  | my ($self,$buf) = @_; | 
| 128 | 0 |  |  |  |  |  | my($r); | 
| 129 |  |  |  |  |  |  |  | 
| 130 | 0 |  |  |  |  |  | $r = $self->{conn}->quote($buf); | 
| 131 | 0 | 0 |  |  |  |  | $r = "''" if ($r eq "NULL"); | 
| 132 | 0 |  |  |  |  |  | return $r; | 
| 133 |  |  |  |  |  |  | } | 
| 134 |  |  |  |  |  |  |  | 
| 135 |  |  |  |  |  |  | # Desconecta do banco de dados | 
| 136 |  |  |  |  |  |  | sub disconnect { | 
| 137 | 0 |  |  | 0 | 1 |  | my ($self) = @_; | 
| 138 |  |  |  |  |  |  |  | 
| 139 | 0 |  |  |  |  |  | $self->{conn}->disconnect; | 
| 140 |  |  |  |  |  |  |  | 
| 141 |  |  |  |  |  |  | # Apaga variaveis | 
| 142 | 0 |  |  |  |  |  | delete $self->{conn}; | 
| 143 | 0 |  |  |  |  |  | delete $self->{type}; | 
| 144 | 0 |  |  |  |  |  | $self = undef; | 
| 145 |  |  |  |  |  |  | } | 
| 146 |  |  |  |  |  |  |  | 
| 147 |  |  |  |  |  |  | # Recupera numero de linhas | 
| 148 |  |  |  |  |  |  | sub rows { | 
| 149 | 0 |  |  | 0 | 0 |  | my ($self) = @_; | 
| 150 |  |  |  |  |  |  |  | 
| 151 | 0 |  |  |  |  |  | return $self->{pre}->rows; | 
| 152 |  |  |  |  |  |  | } | 
| 153 |  |  |  |  |  |  |  | 
| 154 |  |  |  |  |  |  | # Recupera numero de linhas afetadas | 
| 155 |  |  |  |  |  |  | sub arows { | 
| 156 | 0 |  |  | 0 | 0 |  | my ($self) = @_; | 
| 157 |  |  |  |  |  |  |  | 
| 158 | 0 |  |  |  |  |  | return $self->{arows}; | 
| 159 |  |  |  |  |  |  | } | 
| 160 |  |  |  |  |  |  |  | 
| 161 |  |  |  |  |  |  | # Recupera ultima linha inserida | 
| 162 |  |  |  |  |  |  | sub insertid { | 
| 163 | 0 |  |  | 0 | 1 |  | my ($self) = @_; | 
| 164 |  |  |  |  |  |  |  | 
| 165 |  |  |  |  |  |  | # mysql | 
| 166 |  |  |  |  |  |  | #if ($self->{type} eq "mysql") { $self->{conn}->{mysql_insertid}; } | 
| 167 | 0 |  |  |  |  |  | return $self->{conn}->{mysql_insertid}; | 
| 168 |  |  |  |  |  |  | } | 
| 169 |  |  |  |  |  |  |  | 
| 170 |  |  |  |  |  |  | # Retorna self | 
| 171 |  |  |  |  |  |  | sub self { | 
| 172 | 0 |  |  | 0 | 0 |  | my ($self,$field) = @_; | 
| 173 |  |  |  |  |  |  |  | 
| 174 | 0 |  |  |  |  |  | return $self->{$field}; | 
| 175 |  |  |  |  |  |  | } | 
| 176 |  |  |  |  |  |  |  | 
| 177 |  |  |  |  |  |  | # retorn erro | 
| 178 |  |  |  |  |  |  | #sub error { | 
| 179 |  |  |  |  |  |  | #    my ($self) = @_; | 
| 180 |  |  |  |  |  |  | #    return $self->{error}; | 
| 181 |  |  |  |  |  |  | #} | 
| 182 |  |  |  |  |  |  |  | 
| 183 |  |  |  |  |  |  | 1; | 
| 184 |  |  |  |  |  |  | __END__ |