line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package DBIx::Fast; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
12597
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
49
|
|
4
|
1
|
|
|
1
|
|
5
|
use warnings FATAL => 'all'; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
46
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '0.06'; |
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
410
|
use Moo; |
|
1
|
|
|
|
|
9232
|
|
|
1
|
|
|
|
|
6
|
|
9
|
1
|
|
|
1
|
|
1201
|
use DBIx::Connector; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
use DateTime::Format::MySQL; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
has db => ( is => 'rw' ); |
13
|
|
|
|
|
|
|
has sql => ( is => 'rw' ); |
14
|
|
|
|
|
|
|
has p => ( is => 'rw' ); |
15
|
|
|
|
|
|
|
has last_id => ( is => 'rw'); |
16
|
|
|
|
|
|
|
has results => ( is => 'rw'); |
17
|
|
|
|
|
|
|
has errors => ( is => 'rwp'); |
18
|
|
|
|
|
|
|
has dbd => ( is => 'rwp'); |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub set_error { |
21
|
|
|
|
|
|
|
my $self = shift; |
22
|
|
|
|
|
|
|
my $error = { |
23
|
|
|
|
|
|
|
id => shift, |
24
|
|
|
|
|
|
|
error => shift |
25
|
|
|
|
|
|
|
}; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
my $Errors = $self->errors; |
28
|
|
|
|
|
|
|
push @{$Errors} ,$error; |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
$self->_set_errors($Errors); |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub BUILD { |
34
|
|
|
|
|
|
|
my ($self,$args) = @_; |
35
|
|
|
|
|
|
|
my $dsn; |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
my $dbi_args = { |
38
|
|
|
|
|
|
|
RaiseError => 0, |
39
|
|
|
|
|
|
|
PrintError => 0, |
40
|
|
|
|
|
|
|
AutoCommit => 1, |
41
|
|
|
|
|
|
|
}; |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
$args->{host} = '127.0.0.1' unless $args->{host}; |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
$args->{host} eq 'sqlite' ? $self->_set_dbd('sqlite') : |
46
|
|
|
|
|
|
|
$self->_set_dbd('mysql'); |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
if ( $self->dbd eq 'sqlite' ) { |
49
|
|
|
|
|
|
|
$dsn = 'dbi:SQLite:'.$args->{db}; |
50
|
|
|
|
|
|
|
$self->db(DBIx::Connector->new( $dsn, $args->{user}, $args->{passwd}, |
51
|
|
|
|
|
|
|
$dbi_args )); |
52
|
|
|
|
|
|
|
} else { |
53
|
|
|
|
|
|
|
$dsn= 'dbi:mysql:database='.$args->{db}.':'.$args->{host}; |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
$self->db(DBIx::Connector->new( $dsn, $args->{user}, $args->{passwd}, |
56
|
|
|
|
|
|
|
$dbi_args )); |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
$self->db->mode('ping'); |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
$self->db->dbh->{HandleError} = sub { |
62
|
|
|
|
|
|
|
$self->set_error($DBI::err,$DBI::errstr); |
63
|
|
|
|
|
|
|
}; |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
$self->db->dbh->trace($args->{trace},'dbix-fast-trace') if $args->{trace}; |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
$self->profile($args->{profile}) if $args->{profile}; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
=head2 profile |
71
|
|
|
|
|
|
|
Save profile with the PID |
72
|
|
|
|
|
|
|
=cut |
73
|
|
|
|
|
|
|
sub profile { |
74
|
|
|
|
|
|
|
my $self = shift; |
75
|
|
|
|
|
|
|
my $stat = shift."/DBI::ProfileDumper/"; |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
$stat .= qq{File:dbix-fast-$$.prof}; |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
$self->db->dbh->{Profile} = $stat; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=head2 Compatibility |
83
|
|
|
|
|
|
|
scalar @_ > 1 ? $self->execute(@_,'arrayref') : |
84
|
|
|
|
|
|
|
$self->execute(@_,undef,'arrayref'); |
85
|
|
|
|
|
|
|
=cut |
86
|
|
|
|
|
|
|
sub all { |
87
|
|
|
|
|
|
|
my $self = shift; |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
$self->q(@_); |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
my $res = $self->db->dbh->selectall_arrayref($self->sql, |
92
|
|
|
|
|
|
|
{ Slice => {} },@{$self->p}); |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
$self->results($res) unless $DBI::err; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub hash { |
98
|
|
|
|
|
|
|
my $self = shift; |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
$self->q(@_); |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
my $sth = $self->db->dbh->prepare($self->sql); |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
$sth->execute(@{$self->p}); |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
my $res = $sth->fetchrow_hashref; |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
$self->results($res) unless $DBI::err; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=head2 val |
112
|
|
|
|
|
|
|
Return one value |
113
|
|
|
|
|
|
|
=cut |
114
|
|
|
|
|
|
|
sub val { |
115
|
|
|
|
|
|
|
my $self = shift; |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
$self->q(@_); |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
return $self->db->dbh->selectrow_array($self->sql, undef, @{$self->p}); |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=head2 array |
123
|
|
|
|
|
|
|
Return array |
124
|
|
|
|
|
|
|
=cut |
125
|
|
|
|
|
|
|
sub array { |
126
|
|
|
|
|
|
|
my $self = shift; |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
$self->q(@_); |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
my $sth = $self->db->dbh->prepare($self->sql); |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
$sth->execute(@{$self->p}); |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
unless ( $DBI::err ) { |
135
|
|
|
|
|
|
|
my @rows = @{ $self->db->dbh->selectcol_arrayref( |
136
|
|
|
|
|
|
|
$self->sql, undef, @{ $self->p } ) }; |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
$self->results(\@rows); |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
sub count { |
143
|
|
|
|
|
|
|
my $self = shift; |
144
|
|
|
|
|
|
|
my $table = shift; |
145
|
|
|
|
|
|
|
my $skeel = shift; |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
$self->sql("SELECT COUNT(*) FROM $table"); |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
unless ( $skeel ) { |
150
|
|
|
|
|
|
|
return $self->db->dbh->selectrow_array($self->sql); |
151
|
|
|
|
|
|
|
} |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
$self->_make_where($skeel); |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
return $self->db->dbh->selectrow_array($self->sql, undef, @{$self->p}); |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
sub _make_where { |
159
|
|
|
|
|
|
|
my $self = shift; |
160
|
|
|
|
|
|
|
my $skeel = shift; |
161
|
|
|
|
|
|
|
my @p; |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
my $sql = " WHERE "; |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
for my $K ( keys %{$skeel} ) { |
166
|
|
|
|
|
|
|
my $key; |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
if ( ref $skeel->{$K} eq 'HASH' ) { |
169
|
|
|
|
|
|
|
$key = (keys %{$skeel->{$K}})[0]; |
170
|
|
|
|
|
|
|
push @p,$skeel->{$K}->{$key}; |
171
|
|
|
|
|
|
|
} else { |
172
|
|
|
|
|
|
|
$key = '='; |
173
|
|
|
|
|
|
|
push @p,$skeel->{$K}; |
174
|
|
|
|
|
|
|
} |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
$sql .= qq{$K $key ? }; |
177
|
|
|
|
|
|
|
} |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
$sql =~ s/,$//; |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
$self->sql($self->sql.$sql); |
182
|
|
|
|
|
|
|
$self->p(\@p); |
183
|
|
|
|
|
|
|
} |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
sub execute { |
186
|
|
|
|
|
|
|
my $self = shift; |
187
|
|
|
|
|
|
|
my $sql = shift; |
188
|
|
|
|
|
|
|
my $extra = shift; |
189
|
|
|
|
|
|
|
my $type = shift // 'arrayref'; |
190
|
|
|
|
|
|
|
my $res; |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
$self->sql($sql); |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
## Extra Arguments |
195
|
|
|
|
|
|
|
$self->make_sen($extra) if $extra; |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
if ( $type eq 'hash' ) { |
198
|
|
|
|
|
|
|
my $sth = $self->db->dbh->prepare($self->sql); |
199
|
|
|
|
|
|
|
if ( $self->p ) { |
200
|
|
|
|
|
|
|
$sth->execute(@{$self->p}); |
201
|
|
|
|
|
|
|
} else { |
202
|
|
|
|
|
|
|
$sth->execute; |
203
|
|
|
|
|
|
|
} |
204
|
|
|
|
|
|
|
$res = $sth->fetchrow_hashref; |
205
|
|
|
|
|
|
|
} else { |
206
|
|
|
|
|
|
|
if ($self->p ) { |
207
|
|
|
|
|
|
|
$res = $self->db->dbh->selectall_arrayref($self->sql, |
208
|
|
|
|
|
|
|
{ Slice => {} },@{$self->p}); |
209
|
|
|
|
|
|
|
} else { |
210
|
|
|
|
|
|
|
$res = $self->db->dbh->selectall_arrayref($self->sql, |
211
|
|
|
|
|
|
|
{ Slice => {} } ); |
212
|
|
|
|
|
|
|
} |
213
|
|
|
|
|
|
|
} |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
unless ( $DBI::err ) { |
216
|
|
|
|
|
|
|
$self->results($res); |
217
|
|
|
|
|
|
|
} |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
} |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
sub update { |
222
|
|
|
|
|
|
|
my $self = shift; |
223
|
|
|
|
|
|
|
my $table = shift; |
224
|
|
|
|
|
|
|
my $skeel = shift; |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
$skeel->{sen} = $self->extra_args($skeel->{sen},@_) if scalar @_ > 0; |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
my @p; |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
my $sql = "UPDATE $table SET "; |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
for ( keys %{$skeel->{sen}} ) { |
233
|
|
|
|
|
|
|
push @p,$skeel->{sen}->{$_}; |
234
|
|
|
|
|
|
|
$sql .= $_.' = ? ,'; |
235
|
|
|
|
|
|
|
} |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
$sql =~ s/,$//; |
238
|
|
|
|
|
|
|
$sql .= 'WHERE '; |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
for my $K ( keys %{$skeel->{where}} ) { |
241
|
|
|
|
|
|
|
push @p,$skeel->{where}->{$K}; |
242
|
|
|
|
|
|
|
$sql .= $K.' = ? ,'; |
243
|
|
|
|
|
|
|
} |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
$sql =~ s/,$//; |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
$self->sql($sql); |
248
|
|
|
|
|
|
|
$self->execute_prepare(@p); |
249
|
|
|
|
|
|
|
} |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
sub insert { |
252
|
|
|
|
|
|
|
my $self = shift; |
253
|
|
|
|
|
|
|
my $table = shift; |
254
|
|
|
|
|
|
|
my $skeel = shift; |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
$skeel = $self->extra_args($skeel,@_) if scalar @_ > 0; |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
my @p; |
259
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
my $sql= "INSERT INTO $table ( "; |
261
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
for ( keys %{$skeel} ) { |
263
|
|
|
|
|
|
|
push @p,$skeel->{$_}; |
264
|
|
|
|
|
|
|
$sql .= $_.','; |
265
|
|
|
|
|
|
|
} |
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
$sql =~ s/,$/ )/; |
268
|
|
|
|
|
|
|
$sql .= ' VALUES ( '.join(',', ('?') x @p).' )'; |
269
|
|
|
|
|
|
|
|
270
|
|
|
|
|
|
|
$self->sql($sql); |
271
|
|
|
|
|
|
|
$self->execute_prepare(@p); |
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
if ( $self->dbd eq 'mysql' ) { |
274
|
|
|
|
|
|
|
$self->last_id($self->db->dbh->{mysql_insertid}); |
275
|
|
|
|
|
|
|
} elsif ( $self->dbd eq 'sqlite' ) { |
276
|
|
|
|
|
|
|
$self->last_id($self->db->dbh->sqlite_last_insert_rowid()); |
277
|
|
|
|
|
|
|
} |
278
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
} |
280
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
sub delete { |
282
|
|
|
|
|
|
|
my $self = shift; |
283
|
|
|
|
|
|
|
my $table = shift; |
284
|
|
|
|
|
|
|
my $skeel = shift; |
285
|
|
|
|
|
|
|
|
286
|
|
|
|
|
|
|
$self->sql("DELETE FROM $table"); |
287
|
|
|
|
|
|
|
|
288
|
|
|
|
|
|
|
#unless ( $skeel ) { |
289
|
|
|
|
|
|
|
# return $self->db->dbh->selectrow_array($self->sql); |
290
|
|
|
|
|
|
|
#} |
291
|
|
|
|
|
|
|
|
292
|
|
|
|
|
|
|
$self->_make_where($skeel); |
293
|
|
|
|
|
|
|
|
294
|
|
|
|
|
|
|
my $sth = $self->db->dbh->prepare($self->sql); |
295
|
|
|
|
|
|
|
$sth->execute(@{$self->p}); |
296
|
|
|
|
|
|
|
} |
297
|
|
|
|
|
|
|
|
298
|
|
|
|
|
|
|
=head2 function |
299
|
|
|
|
|
|
|
Extra Args : |
300
|
|
|
|
|
|
|
|
301
|
|
|
|
|
|
|
time : NOW() |
302
|
|
|
|
|
|
|
=cut |
303
|
|
|
|
|
|
|
sub extra_args { |
304
|
|
|
|
|
|
|
my $self = shift; |
305
|
|
|
|
|
|
|
my $skeel = shift; |
306
|
|
|
|
|
|
|
my %args = @_; |
307
|
|
|
|
|
|
|
|
308
|
|
|
|
|
|
|
$skeel->{$args{time}} = DateTime::Format::MySQL->format_datetime(DateTime->now) |
309
|
|
|
|
|
|
|
if $args{time}; |
310
|
|
|
|
|
|
|
|
311
|
|
|
|
|
|
|
return $skeel; |
312
|
|
|
|
|
|
|
} |
313
|
|
|
|
|
|
|
|
314
|
|
|
|
|
|
|
## FIXME : Hacer con execute_prepare |
315
|
|
|
|
|
|
|
sub make_sen { |
316
|
|
|
|
|
|
|
my $self = shift; |
317
|
|
|
|
|
|
|
my $skeel = shift; |
318
|
|
|
|
|
|
|
my $sql = $self->sql(); |
319
|
|
|
|
|
|
|
my @p; |
320
|
|
|
|
|
|
|
|
321
|
|
|
|
|
|
|
## Ha de encontrar resultados por el orden de entrada parsear debidamente |
322
|
|
|
|
|
|
|
for ( keys %{$skeel} ) { |
323
|
|
|
|
|
|
|
my $arg = ':'.$_; |
324
|
|
|
|
|
|
|
push @p,$skeel->{$_}; |
325
|
|
|
|
|
|
|
$sql =~ s/$arg/\?/; |
326
|
|
|
|
|
|
|
} |
327
|
|
|
|
|
|
|
|
328
|
|
|
|
|
|
|
$sql =~ s/,$//; |
329
|
|
|
|
|
|
|
|
330
|
|
|
|
|
|
|
$self->sql($sql); |
331
|
|
|
|
|
|
|
$self->p(\@p); |
332
|
|
|
|
|
|
|
} |
333
|
|
|
|
|
|
|
|
334
|
|
|
|
|
|
|
sub q { |
335
|
|
|
|
|
|
|
my $self = shift; |
336
|
|
|
|
|
|
|
my $sql = shift; |
337
|
|
|
|
|
|
|
my @p; |
338
|
|
|
|
|
|
|
|
339
|
|
|
|
|
|
|
map { push @p,$_ } @_; |
340
|
|
|
|
|
|
|
|
341
|
|
|
|
|
|
|
$self->sql($sql); |
342
|
|
|
|
|
|
|
$self->p(\@p); |
343
|
|
|
|
|
|
|
} |
344
|
|
|
|
|
|
|
|
345
|
|
|
|
|
|
|
sub execute_prepare { |
346
|
|
|
|
|
|
|
my $self = shift; |
347
|
|
|
|
|
|
|
my @p = @_; |
348
|
|
|
|
|
|
|
|
349
|
|
|
|
|
|
|
my $sth = $self->db->dbh->prepare($self->sql); |
350
|
|
|
|
|
|
|
|
351
|
|
|
|
|
|
|
$sth->execute(@p); |
352
|
|
|
|
|
|
|
} |
353
|
|
|
|
|
|
|
|
354
|
|
|
|
|
|
|
=head1 NAME |
355
|
|
|
|
|
|
|
|
356
|
|
|
|
|
|
|
DBIx::Fast |
357
|
|
|
|
|
|
|
|
358
|
|
|
|
|
|
|
=head1 SYNOPSIS |
359
|
|
|
|
|
|
|
|
360
|
|
|
|
|
|
|
$db = DBIx::Fast->new( db => 'test' , user => 'test' , passwd => 'test'); |
361
|
|
|
|
|
|
|
|
362
|
|
|
|
|
|
|
$db = DBIx::Fast->new( db => 'test' , user => 'test' , passwd => 'test', |
363
|
|
|
|
|
|
|
trace => '1' , profile => '!Statement:!MethodName' ); |
364
|
|
|
|
|
|
|
|
365
|
|
|
|
|
|
|
=head1 DESCRIPTION |
366
|
|
|
|
|
|
|
|
367
|
|
|
|
|
|
|
=head1 SUBROUTINES/METHODS |
368
|
|
|
|
|
|
|
|
369
|
|
|
|
|
|
|
=cut |
370
|
|
|
|
|
|
|
|
371
|
|
|
|
|
|
|
1; |