| 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 <unattis@nattis.com> | ||||||||||||||
| 5 | |||||||||||||||
| 6 | # {type},{db},{host},{username},{passwd} | ||||||||||||||
| 7 | # MySQL vars: host,username,passwd,db | ||||||||||||||
| 8 | |||||||||||||||
| 9 | package NTS::SqlLink; | ||||||||||||||
| 10 | |||||||||||||||
| 11 | 1 | ▶ | use vars qw($conn); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | 1 | ▶ | use DBI; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | 1 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 14 | 1 | ▶ | use warnings FATAL => 'all'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 15 | |||||||||||||||
| 16 | 1 | our $VERSION = '2.0'; | |||||||||||||
| 17 | |||||||||||||||
| 18 | # Inicia conexao | ||||||||||||||
| 19 | 0 | ▶ | sub new { | ||||||||||||
|
Subroutine
new: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||
| 20 | 0 | my($self,$vars) = @_; | |||||||||||||
| 21 | 0 | my ($conn); | |||||||||||||
| 22 | |||||||||||||||
| 23 | 0 | ▶ | $vars->{acommit} = 0 unless defined $vars->{acommit}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 24 | 0 | $vars->{acommit} = $vars->{acommit}; | |||||||||||||
| 25 | |||||||||||||||
| 26 | # Verifica se é mysql | ||||||||||||||
| 27 | 0 | ▶ | if ($vars->{type} eq "mysql") { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 28 | |||||||||||||||
| 29 | # Faz conexao | ||||||||||||||
| 30 | $conn = DBI->connect("DBI:$vars->{type}:$vars->{db}:$vars->{host}", | ||||||||||||||
| 31 | $vars->{username},$vars->{passwd},{ AutoCommit => $vars->{acommit} }) | ||||||||||||||
| 32 | 0 | ▶ | or die DBI::errstr; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 33 | |||||||||||||||
| 34 | bless { | ||||||||||||||
| 35 | conn => $conn, | ||||||||||||||
| 36 | type => $vars->{type}, | ||||||||||||||
| 37 | 0 | error => undef, | |||||||||||||
| 38 | pre => undef, | ||||||||||||||
| 39 | arows => undef, | ||||||||||||||
| 40 | }, $self; | ||||||||||||||
| 41 | } | ||||||||||||||
| 42 | } | ||||||||||||||
| 43 | |||||||||||||||
| 44 | # Recupera dados do db | ||||||||||||||
| 45 | 0 | ▶ | sub return { | ||||||||||||
|
Subroutine
return: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||
| 46 | 0 | my ($self,$q,$t) = @_; | |||||||||||||
| 47 | 0 | my (@array,@row,$row); | |||||||||||||
| 48 | |||||||||||||||
| 49 | # Verifica formato de dados que deve retorna | ||||||||||||||
| 50 | 0 | ▶ | $t = "scalar" if (!$t); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 51 | |||||||||||||||
| 52 | # Prepara query | ||||||||||||||
| 53 | 0 | eval { $self->{pre} = $self->{conn}->prepare($q); }; | |||||||||||||
| 54 | |||||||||||||||
| 55 | # Verifica se conseguiu executar a query | ||||||||||||||
| 56 | 0 | eval $self->{pre}->execute; | |||||||||||||
| 57 | 0 | ▶ | if ($@) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 58 | 0 | die "\n".DBI::err.": ".DBI::errstr; | |||||||||||||
| 59 | }; | ||||||||||||||
| 60 | |||||||||||||||
| 61 | # Retorna em formato array | ||||||||||||||
| 62 | 0 | ▶ | if ($t eq "array") { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 63 | 0 | ▶ | while (@row = $self->{pre}->fetchrow_array) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 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) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 72 | 0 | push(@array,$row); | |||||||||||||
| 73 | } | ||||||||||||||
| 74 | }; | ||||||||||||||
| 75 | } | ||||||||||||||
| 76 | |||||||||||||||
| 77 | 0 | eval { $self->{pre}->finish }; | |||||||||||||
| 78 | |||||||||||||||
| 79 | # Apaga variaveis indesejadas | ||||||||||||||
| 80 | #print $q."\n"; | ||||||||||||||
| 81 | 0 | undef $q; undef $t; | |||||||||||||
| 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 | 0 | ▶ | sub do { | ||||||||||||
|
Subroutine
do: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||
| 92 | 0 | my ($self,$q) = @_; | |||||||||||||
| 93 | |||||||||||||||
| 94 | 0 | ▶ | eval { $self->{arows} = $self->{conn}->do($q); } | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 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 | 0 | ▶ | sub commit { | ||||||||||||
|
Subroutine
commit: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 109 | 0 | my ($self) = @_; | |||||||||||||
| 110 | |||||||||||||||
| 111 | 0 | $self->{conn}->commit(); | |||||||||||||
| 112 | |||||||||||||||
| 113 | 0 | return 1; | |||||||||||||
| 114 | } | ||||||||||||||
| 115 | |||||||||||||||
| 116 | # rollback | ||||||||||||||
| 117 | 0 | ▶ | sub rollback { | ||||||||||||
|
Subroutine
rollback: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 118 | 0 | my ($self) = @_; | |||||||||||||
| 119 | |||||||||||||||
| 120 | 0 | $self->{conn}->rollback(); | |||||||||||||
| 121 | |||||||||||||||
| 122 | 0 | return 1; | |||||||||||||
| 123 | } | ||||||||||||||
| 124 | |||||||||||||||
| 125 | # Adiciona quote | ||||||||||||||
| 126 | 0 | ▶ | sub qt { | ||||||||||||
|
Subroutine
qt: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||
| 127 | 0 | my ($self,$buf) = @_; | |||||||||||||
| 128 | 0 | my($r); | |||||||||||||
| 129 | |||||||||||||||
| 130 | 0 | $r = $self->{conn}->quote($buf); | |||||||||||||
| 131 | 0 | ▶ | $r = "''" if ($r eq "NULL"); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 132 | 0 | return $r; | |||||||||||||
| 133 | } | ||||||||||||||
| 134 | |||||||||||||||
| 135 | # Desconecta do banco de dados | ||||||||||||||
| 136 | 0 | ▶ | sub disconnect { | ||||||||||||
|
Subroutine
disconnect: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 137 | 0 | 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 | 0 | ▶ | sub rows { | ||||||||||||
|
Subroutine
rows: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 149 | 0 | my ($self) = @_; | |||||||||||||
| 150 | |||||||||||||||
| 151 | 0 | return $self->{pre}->rows; | |||||||||||||
| 152 | } | ||||||||||||||
| 153 | |||||||||||||||
| 154 | # Recupera numero de linhas afetadas | ||||||||||||||
| 155 | 0 | ▶ | sub arows { | ||||||||||||
|
Subroutine
arows: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 156 | 0 | my ($self) = @_; | |||||||||||||
| 157 | |||||||||||||||
| 158 | 0 | return $self->{arows}; | |||||||||||||
| 159 | } | ||||||||||||||
| 160 | |||||||||||||||
| 161 | # Recupera ultima linha inserida | ||||||||||||||
| 162 | 0 | ▶ | sub insertid { | ||||||||||||
|
Subroutine
insertid: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 163 | 0 | 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 | 0 | ▶ | sub self { | ||||||||||||
|
Subroutine
self: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 172 | 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 | 1; | |||||||||||||
| 184 | __END__ | ||||||||||||||