| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
#!/usr/bin/perl -w |
|
2
|
|
|
|
|
|
|
# Efetua conexao com o banco de dados |
|
3
|
|
|
|
|
|
|
# By: Udlei Nattis |
|
4
|
|
|
|
|
|
|
# Date: Tue Nov 27 13:56:34 BRST 2001 |
|
5
|
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
# {type},{db},{host},{username},{passwd} |
|
7
|
|
|
|
|
|
|
# MySQL vars: host,username,passwd,db |
|
8
|
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
package Ananke::SqlLink; |
|
10
|
|
|
|
|
|
|
|
|
11
|
1
|
|
|
1
|
|
640
|
use vars qw($conn); |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
48
|
|
|
12
|
1
|
|
|
1
|
|
2129
|
use DBI; |
|
|
1
|
|
|
|
|
18205
|
|
|
|
1
|
|
|
|
|
61
|
|
|
13
|
1
|
|
|
1
|
|
9
|
use strict; |
|
|
1
|
|
|
|
|
6
|
|
|
|
1
|
|
|
|
|
600
|
|
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
our $VERSION = '1.1.1'; |
|
16
|
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
# Inicia conexao |
|
18
|
|
|
|
|
|
|
sub new { |
|
19
|
0
|
|
|
0
|
1
|
|
my($self,$vars) = @_; |
|
20
|
0
|
|
|
|
|
|
my ($conn); |
|
21
|
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
# Verifica se é mysql |
|
23
|
0
|
0
|
|
|
|
|
if ($vars->{type} eq "mysql") { |
|
24
|
0
|
0
|
|
|
|
|
$conn = DBI->connect("DBI:$vars->{type}:$vars->{db}:$vars->{host}", |
|
25
|
|
|
|
|
|
|
$vars->{username},$vars->{passwd}) |
|
26
|
|
|
|
|
|
|
or die DBI::errstr; |
|
27
|
|
|
|
|
|
|
|
|
28
|
0
|
|
|
|
|
|
bless { |
|
29
|
|
|
|
|
|
|
conn => $conn, |
|
30
|
|
|
|
|
|
|
type => $vars->{type}, |
|
31
|
|
|
|
|
|
|
error => undef, |
|
32
|
|
|
|
|
|
|
pre => undef, |
|
33
|
|
|
|
|
|
|
}, $self; |
|
34
|
|
|
|
|
|
|
} |
|
35
|
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
} |
|
37
|
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
# Recupera dados do db |
|
39
|
|
|
|
|
|
|
sub return { |
|
40
|
0
|
|
|
0
|
1
|
|
my ($self,$q,$t) = @_; |
|
41
|
0
|
|
|
|
|
|
my (@array,@row,$row); |
|
42
|
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
# Verifica formato de dados que deve retorna |
|
44
|
0
|
0
|
|
|
|
|
$t = "scalar" if (!$t); |
|
45
|
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
# Prepara query |
|
47
|
0
|
|
|
|
|
|
eval { $self->{pre} = $self->{conn}->prepare($q); }; |
|
|
0
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
# Verifica se conseguiu executar a query |
|
50
|
0
|
0
|
|
|
|
|
eval { $self->{pre}->execute } or die DBI::errstr; |
|
|
0
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
# Retorna em formato array |
|
53
|
0
|
0
|
|
|
|
|
if ($t eq "array") { |
|
|
|
0
|
|
|
|
|
|
|
54
|
0
|
|
|
|
|
|
while (@row = $self->{pre}->fetchrow_array) { |
|
55
|
0
|
|
|
|
|
|
push(@array,[ @row ]); |
|
56
|
|
|
|
|
|
|
} |
|
57
|
|
|
|
|
|
|
} |
|
58
|
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
# Retorna em formato hash |
|
60
|
|
|
|
|
|
|
elsif ($t eq "scalar") { |
|
61
|
0
|
|
|
|
|
|
eval { |
|
62
|
0
|
|
|
|
|
|
while ($row = $self->{pre}->fetchrow_hashref) { |
|
63
|
0
|
|
|
|
|
|
push(@array,$row); |
|
64
|
|
|
|
|
|
|
} |
|
65
|
|
|
|
|
|
|
}; |
|
66
|
|
|
|
|
|
|
} |
|
67
|
|
|
|
|
|
|
|
|
68
|
0
|
|
|
|
|
|
eval { $self->{pre}->finish }; |
|
|
0
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
# Apaga variaveis indesejadas |
|
71
|
0
|
|
|
|
|
|
undef $q; undef $t; |
|
|
0
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
# Retorna os resultados do select |
|
74
|
0
|
|
|
|
|
|
return @array; |
|
75
|
|
|
|
|
|
|
} |
|
76
|
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
# executa funcao 'do' |
|
78
|
|
|
|
|
|
|
sub do { |
|
79
|
0
|
|
|
0
|
1
|
|
my ($self,$q) = @_; |
|
80
|
|
|
|
|
|
|
|
|
81
|
0
|
|
|
|
|
|
$self->{conn}->do($q); |
|
82
|
|
|
|
|
|
|
|
|
83
|
0
|
0
|
|
|
|
|
if (DBI::errstr) { |
|
84
|
0
|
|
|
|
|
|
$self->{error} = DBI::errstr; |
|
85
|
0
|
|
|
|
|
|
return 0; |
|
86
|
|
|
|
|
|
|
} |
|
87
|
|
|
|
|
|
|
|
|
88
|
0
|
|
|
|
|
|
undef $q; |
|
89
|
|
|
|
|
|
|
|
|
90
|
0
|
|
|
|
|
|
return 1; |
|
91
|
|
|
|
|
|
|
} |
|
92
|
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
# Adiciona quote |
|
94
|
|
|
|
|
|
|
sub quote { |
|
95
|
0
|
|
|
0
|
1
|
|
my ($self,$buf) = @_; |
|
96
|
0
|
|
|
|
|
|
my($r); |
|
97
|
|
|
|
|
|
|
|
|
98
|
0
|
|
|
|
|
|
$r = $self->{conn}->quote($buf); |
|
99
|
0
|
0
|
|
|
|
|
$r = "''" if ($r eq "NULL"); |
|
100
|
0
|
|
|
|
|
|
return $r; |
|
101
|
|
|
|
|
|
|
} |
|
102
|
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
# Desconecta do banco de dados |
|
104
|
|
|
|
|
|
|
sub disconnect { |
|
105
|
0
|
|
|
0
|
1
|
|
my ($self) = @_; |
|
106
|
|
|
|
|
|
|
|
|
107
|
0
|
|
|
|
|
|
$self->{conn}->disconnect; |
|
108
|
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
# Apaga variaveis |
|
110
|
0
|
|
|
|
|
|
delete $self->{conn}; |
|
111
|
0
|
|
|
|
|
|
delete $self->{type}; |
|
112
|
0
|
|
|
|
|
|
$self = undef; |
|
113
|
|
|
|
|
|
|
} |
|
114
|
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
# Recupera numero de linhas |
|
116
|
|
|
|
|
|
|
sub rows { |
|
117
|
0
|
|
|
0
|
0
|
|
my ($self) = @_; |
|
118
|
|
|
|
|
|
|
|
|
119
|
0
|
|
|
|
|
|
return $self->{pre}->rows; |
|
120
|
|
|
|
|
|
|
} |
|
121
|
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
# Recupera ultima linha inserida |
|
123
|
|
|
|
|
|
|
sub insertid { |
|
124
|
0
|
|
|
0
|
1
|
|
my ($self) = @_; |
|
125
|
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
# mysql |
|
127
|
0
|
0
|
|
|
|
|
if ($self->{type} eq "mysql") { $self->{conn}->{mysql_insertid}; } |
|
|
0
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
} |
|
129
|
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
# retorn erro |
|
131
|
|
|
|
|
|
|
sub error { |
|
132
|
0
|
|
|
0
|
0
|
|
my ($self) = @_; |
|
133
|
0
|
|
|
|
|
|
return $self->{error}; |
|
134
|
|
|
|
|
|
|
} |
|
135
|
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
1; |
|
137
|
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=head1 NAME |
|
139
|
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
Ananke::SqlLink - Front-end module to MySQL |
|
141
|
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
143
|
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
MySQL easy access |
|
145
|
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
147
|
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
#!/usr/bin/perl] |
|
149
|
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
use strict; |
|
151
|
|
|
|
|
|
|
use Ananke::SqlLink; |
|
152
|
|
|
|
|
|
|
my(@r,$c,$q,$i); |
|
153
|
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
# Open DB |
|
155
|
|
|
|
|
|
|
$c = new Ananke::SqlLink({ |
|
156
|
|
|
|
|
|
|
'type' => 'mysql', |
|
157
|
|
|
|
|
|
|
'db' => 'test', |
|
158
|
|
|
|
|
|
|
'host' => 'localhost', |
|
159
|
|
|
|
|
|
|
'username' => 'root', |
|
160
|
|
|
|
|
|
|
'passwd' => '', |
|
161
|
|
|
|
|
|
|
}); |
|
162
|
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
# Query Insert |
|
164
|
|
|
|
|
|
|
$q = "INSERT INTO test (id,name) VALUES (null,'user')"; |
|
165
|
|
|
|
|
|
|
$c->do($q); undef $q; |
|
166
|
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
# Query Select |
|
168
|
|
|
|
|
|
|
$q = "SELECT id,name FROM test"; |
|
169
|
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
# Result 1 |
|
171
|
|
|
|
|
|
|
print "- Scalar\n"; |
|
172
|
|
|
|
|
|
|
@r = $c->return($q,'scalar'); |
|
173
|
|
|
|
|
|
|
foreach $i (@r) { |
|
174
|
|
|
|
|
|
|
print "ID: ".$i->{id}." - Name: ".$i->{name}."\n"; |
|
175
|
|
|
|
|
|
|
} |
|
176
|
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
# Result 2 |
|
178
|
|
|
|
|
|
|
print "- Array\n"; |
|
179
|
|
|
|
|
|
|
@r = $c->return($q,'array'); |
|
180
|
|
|
|
|
|
|
foreach $i (@r) { |
|
181
|
|
|
|
|
|
|
print "ID: ".${$i}[0]." - Name: ".${$i}[1]."\n"; |
|
182
|
|
|
|
|
|
|
} |
|
183
|
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
# Close DB |
|
185
|
|
|
|
|
|
|
$c->disconnect; |
|
186
|
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
=head1 METHODS |
|
188
|
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
=head2 new({type,db,host,username,passwd}) |
|
190
|
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
Create a new SqlLink object. |
|
192
|
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
my $c = new Ananke::SqlLink({ |
|
194
|
|
|
|
|
|
|
'type' => 'mysql', |
|
195
|
|
|
|
|
|
|
'db' => 'test', |
|
196
|
|
|
|
|
|
|
'host' => 'localhost', |
|
197
|
|
|
|
|
|
|
'username' => 'root', |
|
198
|
|
|
|
|
|
|
'passwd' => '', |
|
199
|
|
|
|
|
|
|
}); |
|
200
|
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
=head2 $c->return(type,query) |
|
202
|
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
only for select |
|
204
|
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
=head3 scalar type |
|
206
|
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
@r = $c->return($q,'scalar'); |
|
208
|
|
|
|
|
|
|
foreach $i (@r) { |
|
209
|
|
|
|
|
|
|
print "ID: ".$i->{id}." - Name: ".$i->{name}."\n"; |
|
210
|
|
|
|
|
|
|
} |
|
211
|
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
=head3 array type |
|
213
|
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
@r = $c->return($q,'array'); |
|
215
|
|
|
|
|
|
|
foreach $i (@r) { |
|
216
|
|
|
|
|
|
|
print "ID: ".${$i}[0]." - Name: ".${$i}[1]."\n"; |
|
217
|
|
|
|
|
|
|
} |
|
218
|
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
=head2 $c->do(query) |
|
220
|
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
to insert,update,replace,etc... |
|
222
|
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
$q = "INSERT INTO test (id,name) VALUES (null,'user')"; |
|
224
|
|
|
|
|
|
|
$c->do($q); undef $q; |
|
225
|
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
=head2 $c->disconnect() |
|
228
|
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
disconnect |
|
230
|
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
$c->disconnect(); |
|
232
|
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
=head2 $c->insertid() |
|
234
|
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
return last insert id |
|
236
|
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
=head2 $c->quote(string) |
|
238
|
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
AddSlashes |
|
240
|
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
$q = "INSERT INTO test (id,name) VALUES (null,'".$c->quote($user)."')"; |
|
242
|
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
=head1 AUTHOR |
|
244
|
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
Udlei D. R. Nattis |
|
246
|
|
|
|
|
|
|
nattis@anankeit.com.br |
|
247
|
|
|
|
|
|
|
http://www.nobol.com.br |
|
248
|
|
|
|
|
|
|
http://www.anankeit.com.br |
|
249
|
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
=cut |