| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
# Author: I.Plisco |
|
2
|
|
|
|
|
|
|
# $Id: Composer.pm,v 1.2 2003/10/29 16:55:23 plisco Exp $ |
|
3
|
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
package DBIx::Composer; |
|
5
|
2
|
|
|
2
|
|
43737
|
use strict; |
|
|
2
|
|
|
|
|
4
|
|
|
|
2
|
|
|
|
|
74
|
|
|
6
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
BEGIN { |
|
8
|
2
|
|
|
2
|
|
10
|
use Exporter (); |
|
|
2
|
|
|
|
|
3
|
|
|
|
2
|
|
|
|
|
36
|
|
|
9
|
2
|
|
|
2
|
|
10
|
use vars qw ($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); |
|
|
2
|
|
|
|
|
7
|
|
|
|
2
|
|
|
|
|
219
|
|
|
10
|
2
|
|
|
2
|
|
4
|
$VERSION = '1.00'; |
|
11
|
2
|
|
|
|
|
24
|
@ISA = qw (Exporter); |
|
12
|
2
|
|
|
|
|
5
|
@EXPORT = qw (); |
|
13
|
2
|
|
|
|
|
2
|
@EXPORT_OK = qw (); |
|
14
|
2
|
|
|
|
|
3686
|
%EXPORT_TAGS = (); |
|
15
|
|
|
|
|
|
|
} |
|
16
|
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
# Default global values |
|
18
|
|
|
|
|
|
|
our $DEBUG = 0; |
|
19
|
|
|
|
|
|
|
our $DBH; |
|
20
|
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
########################################### main pod documentation begin ## |
|
22
|
|
|
|
|
|
|
# Below is documentation for module. |
|
23
|
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
=head1 NAME |
|
26
|
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
DBIx::Composer - Composes and runs SQL statement. |
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
30
|
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
use DBIx::Composer |
|
32
|
|
|
|
|
|
|
$cmd = new DBIx::Composer(); |
|
33
|
|
|
|
|
|
|
$cmd->{table} = 'table1'; |
|
34
|
|
|
|
|
|
|
$cmd->{fields} = 'name, email'; |
|
35
|
|
|
|
|
|
|
$cmd->{where} = "where login = 'peter'"; |
|
36
|
|
|
|
|
|
|
print $cmd->compose_select; |
|
37
|
|
|
|
|
|
|
# Prints "select name, surname from table1 where login = 'peter'" |
|
38
|
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
$dbh = open_database(); # Open database yourself |
|
40
|
|
|
|
|
|
|
use DBIx::Composer |
|
41
|
|
|
|
|
|
|
$cmd = new DBIx::Composer(dbh=>$dbh, debug=> 1); |
|
42
|
|
|
|
|
|
|
$cmd->{table} = 'table1'; |
|
43
|
|
|
|
|
|
|
$cmd->{fields} = 'login, name, email'; |
|
44
|
|
|
|
|
|
|
$cmd->{values} = "'john', 'John Smith', 'john@smith.com'"; |
|
45
|
|
|
|
|
|
|
$cmd->insert(); |
|
46
|
|
|
|
|
|
|
# Executes command "insert into table1 (login, name, email) values |
|
47
|
|
|
|
|
|
|
# ('john', 'John Smith', 'john@smith.com')" |
|
48
|
|
|
|
|
|
|
# Prints this command on STDERR before execution. |
|
49
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
51
|
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
This module helps you to compose and run SQL statements. First you |
|
53
|
|
|
|
|
|
|
create new object and fill its hash for common parts of SQL |
|
54
|
|
|
|
|
|
|
statements. Then you may either compose SQL statement from these parts |
|
55
|
|
|
|
|
|
|
or both compose and execute it. |
|
56
|
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
=head1 USAGE |
|
58
|
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
You connect to database using your favorite |
|
60
|
|
|
|
|
|
|
method of connection and supply DBIx::Composer object with standard database |
|
61
|
|
|
|
|
|
|
handler $dbh. If you don't plan to execute statements, you may omit |
|
62
|
|
|
|
|
|
|
connection to database. |
|
63
|
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
So, after creating new object you set its parameters, or SQL command parts. |
|
65
|
|
|
|
|
|
|
Modifiers for command, such as "where ...", "order ...", "limit ..." |
|
66
|
|
|
|
|
|
|
must be full modifiers like "where a=b", not only "a=b". |
|
67
|
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
You don't need to prepare() SQL fetch statements - they are prepared |
|
69
|
|
|
|
|
|
|
internally. You cant execute statements right after setting their parts - the |
|
70
|
|
|
|
|
|
|
module checks whether command has been composed, prepared and executed. Because |
|
71
|
|
|
|
|
|
|
of such behaviour don't try to reset command parts after executing, but better |
|
72
|
|
|
|
|
|
|
create new DBIx::Composer object. |
|
73
|
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=head2 Command parts |
|
75
|
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
Valid command parts are: |
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
table - table name. |
|
79
|
|
|
|
|
|
|
Examples: |
|
80
|
|
|
|
|
|
|
$cmd->{table} = 'table1' |
|
81
|
|
|
|
|
|
|
$cmd->{table} = 'db left join user using (host)'; |
|
82
|
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
fields - fields to select, insert, update, etc. |
|
84
|
|
|
|
|
|
|
Examples: |
|
85
|
|
|
|
|
|
|
$cmd->{fields} = 'login, email, tries + 5'; |
|
86
|
|
|
|
|
|
|
$cmd->{fields} = 'curdate(), now()'; |
|
87
|
|
|
|
|
|
|
$cmd->{fields} = 'ip, traf_in+thaf_out as sum'; |
|
88
|
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
where |
|
90
|
|
|
|
|
|
|
Examples: |
|
91
|
|
|
|
|
|
|
$cmd->{where} = 'login = peter'; |
|
92
|
|
|
|
|
|
|
$cmd->{where} = 'tries > limit + 2'; |
|
93
|
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
order |
|
95
|
|
|
|
|
|
|
Examples: |
|
96
|
|
|
|
|
|
|
$cmd->{order} = "order by ip desc"; |
|
97
|
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
limit |
|
99
|
|
|
|
|
|
|
Examples: |
|
100
|
|
|
|
|
|
|
$cmd->{limit} = "limit 20"; |
|
101
|
|
|
|
|
|
|
$cmd->{limit} = "limit 100, 20"; |
|
102
|
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=head2 Opening database |
|
104
|
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
DBIx::Composer doesn't touch opening database. You should open it |
|
106
|
|
|
|
|
|
|
yourself somewhere else in your program. As for me, I use special function |
|
107
|
|
|
|
|
|
|
open_database() like this: |
|
108
|
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
#==================== |
|
110
|
|
|
|
|
|
|
sub open_database { |
|
111
|
|
|
|
|
|
|
#==================== |
|
112
|
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
# Read config from some file |
|
114
|
|
|
|
|
|
|
my ($db_db, $db_login, $db_passwd, $debug) = read_config(); |
|
115
|
|
|
|
|
|
|
$driver_name = "dbi:mysql:$db_db"; |
|
116
|
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
# Connect to database |
|
118
|
|
|
|
|
|
|
$dbh = DBI->connect |
|
119
|
|
|
|
|
|
|
($driver_name, $db_login, $db_passwd)|| |
|
120
|
|
|
|
|
|
|
die $DBI::errstr; |
|
121
|
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
# Initialize DBIx::Composer |
|
123
|
|
|
|
|
|
|
$DBIx::Composer::DBH = $dbh; |
|
124
|
|
|
|
|
|
|
$DBIx::Composer::DEBUG = $debug; |
|
125
|
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
return $dbh; # returns true on success |
|
127
|
|
|
|
|
|
|
} |
|
128
|
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
Then in your program you don't need to set $dbh anymore, so you can simply |
|
130
|
|
|
|
|
|
|
write: |
|
131
|
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
$cmd = new $DBIx::Composer; |
|
133
|
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
and $cmd knows yet about dbh handler and debug level. |
|
135
|
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
You have to set $dbh in new() explicitly only if you want to make two or more |
|
137
|
|
|
|
|
|
|
database connections in one program, e.g. on migrating from old database to new |
|
138
|
|
|
|
|
|
|
one. |
|
139
|
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=head2 Debug levels |
|
141
|
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
If debug level is set to 1 or more, the module prints SQL commands |
|
143
|
|
|
|
|
|
|
to STDERR (for cgi scripts it's error_log of web server). |
|
144
|
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
If debug level >= 1, it prints executed statements before executing them. |
|
146
|
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
If debug level >= 2, it prints composed statements after composing them. So |
|
148
|
|
|
|
|
|
|
usually (not always) on level 2 you have 2 lines in STDERR - of composing and |
|
149
|
|
|
|
|
|
|
of executing. |
|
150
|
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
Debug line starts from prefix ("SQL run" or "SQL debug") and program name (it's |
|
152
|
|
|
|
|
|
|
convenient for cgi-bin's). |
|
153
|
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=head2 Things you must pay attention to |
|
155
|
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=item * |
|
157
|
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
In order to avoid confusing, don't reuse DBIx::Composer objects after executing |
|
159
|
|
|
|
|
|
|
them, but simply create new object. It's because the module remembers its state |
|
160
|
|
|
|
|
|
|
and don't composes statement again. Probably such behaviour will change |
|
161
|
|
|
|
|
|
|
partially in future. |
|
162
|
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=item * |
|
164
|
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
Command parts 'where', 'limit', 'order' must be started from these words, that |
|
166
|
|
|
|
|
|
|
is, 'where a=b', not 'a=b'; 'order by c desc', not 'c desc' or 'by c desc'. You |
|
167
|
|
|
|
|
|
|
may have in your mind the string "select $fields from $table $where". |
|
168
|
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
=head1 REQUIRES |
|
170
|
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
Perl 5.6.1 (for lvalue functions) |
|
172
|
|
|
|
|
|
|
ExtUtils::MakeMaker - for installation |
|
173
|
|
|
|
|
|
|
Test::More - for installation tests |
|
174
|
|
|
|
|
|
|
DBI etc. - if you want to execute statements |
|
175
|
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
=head1 BUGS |
|
178
|
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
I have tested it only in environment with MySQL database and localhost as |
|
180
|
|
|
|
|
|
|
database host. |
|
181
|
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
Installation tests doesn't check that it works on real database - they only |
|
183
|
|
|
|
|
|
|
check that the module composes statements. |
|
184
|
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=head1 AUTHOR |
|
187
|
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
Igor Plisco |
|
189
|
|
|
|
|
|
|
igor at plisco dot ru |
|
190
|
|
|
|
|
|
|
http://plisco.ru/soft |
|
191
|
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
=head1 COPYRIGHT |
|
193
|
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
This program is free software; you can redistribute |
|
195
|
|
|
|
|
|
|
it and/or modify it under the same terms as Perl itself. |
|
196
|
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
The full text of the license can be found in the |
|
198
|
|
|
|
|
|
|
LICENSE file included with this module. |
|
199
|
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
202
|
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
perl(1). |
|
204
|
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
=head1 FUNCTION DESCRIPTIONS |
|
206
|
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
=cut |
|
209
|
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
############################################# main pod documentation end ## |
|
211
|
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
################################################ subroutine header begin ## |
|
214
|
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
#============================================================ |
|
216
|
|
|
|
|
|
|
sub new { |
|
217
|
|
|
|
|
|
|
#============================================================ |
|
218
|
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
=head2 new |
|
220
|
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
Purpose : Creates new DBIx::Object object. |
|
222
|
|
|
|
|
|
|
Usage : DBIx::Composer->new() |
|
223
|
|
|
|
|
|
|
Returns : Object handler. |
|
224
|
|
|
|
|
|
|
Argument : Nothing or dbh handler or config. |
|
225
|
|
|
|
|
|
|
Throws : None. |
|
226
|
|
|
|
|
|
|
Comments : No. |
|
227
|
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
See Also : N/A |
|
229
|
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
=cut |
|
231
|
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
#------------------------------------------------------------ |
|
233
|
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
# my ($class, %parameters) = @_; |
|
235
|
2
|
|
|
2
|
1
|
17
|
my $class = shift; |
|
236
|
|
|
|
|
|
|
|
|
237
|
2
|
|
|
|
|
11
|
my $self = { |
|
238
|
|
|
|
|
|
|
'_config' => { |
|
239
|
|
|
|
|
|
|
debug => $DEBUG, # default debug level |
|
240
|
|
|
|
|
|
|
dbh => $DBH # default database handler |
|
241
|
|
|
|
|
|
|
} |
|
242
|
|
|
|
|
|
|
}; |
|
243
|
|
|
|
|
|
|
|
|
244
|
2
|
50
|
|
|
|
9
|
$self->{'_config'}->{'dbh'} = shift if @_ == 1; |
|
245
|
2
|
50
|
|
|
|
7
|
%{ $self->{'_config'} } = @_ if @_ >= 2; |
|
|
0
|
|
|
|
|
0
|
|
|
246
|
|
|
|
|
|
|
|
|
247
|
2
|
|
33
|
|
|
17
|
bless ($self, ref ($class) || $class); |
|
248
|
2
|
|
|
|
|
6
|
return $self; |
|
249
|
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
} |
|
251
|
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
#============================================================ |
|
253
|
|
|
|
|
|
|
sub debug_level { |
|
254
|
|
|
|
|
|
|
#============================================================ |
|
255
|
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
=head2 debug_level |
|
257
|
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
Usage : DBIx::Composer->debug_level() |
|
259
|
|
|
|
|
|
|
Purpose : Sets default debug level for all newly created objects. |
|
260
|
|
|
|
|
|
|
Returns : current value of debug level |
|
261
|
|
|
|
|
|
|
Argument : Debug level as integer. |
|
262
|
|
|
|
|
|
|
Throws : None. |
|
263
|
|
|
|
|
|
|
Comments : Warning: now returns global $dbh, not local for object. |
|
264
|
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
See Also : |
|
266
|
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
=cut |
|
268
|
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
#------------------------------------------------------------ |
|
270
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
|
271
|
|
|
|
|
|
|
|
|
272
|
|
|
|
|
|
|
# Set it |
|
273
|
0
|
0
|
|
|
|
0
|
$DEBUG = shift if(@_); |
|
274
|
0
|
|
|
|
|
0
|
return $DEBUG; |
|
275
|
|
|
|
|
|
|
|
|
276
|
|
|
|
|
|
|
} |
|
277
|
|
|
|
|
|
|
|
|
278
|
|
|
|
|
|
|
#============================================================ |
|
279
|
|
|
|
|
|
|
sub dbh { |
|
280
|
|
|
|
|
|
|
#============================================================ |
|
281
|
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
=head2 dbh |
|
283
|
|
|
|
|
|
|
|
|
284
|
|
|
|
|
|
|
Usage : DBIx::Composer->dbh() |
|
285
|
|
|
|
|
|
|
Purpose : Sets default database handler for all newly created objects. |
|
286
|
|
|
|
|
|
|
Returns : current value of $dbh |
|
287
|
|
|
|
|
|
|
Argument : database handler |
|
288
|
|
|
|
|
|
|
Throws : None. |
|
289
|
|
|
|
|
|
|
Comments : Warning: now returns global $dbh, not local for object. |
|
290
|
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
See Also : |
|
292
|
|
|
|
|
|
|
|
|
293
|
|
|
|
|
|
|
=cut |
|
294
|
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
#------------------------------------------------------------ |
|
296
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
|
297
|
|
|
|
|
|
|
|
|
298
|
|
|
|
|
|
|
# Set it |
|
299
|
0
|
0
|
|
|
|
0
|
$DBH = shift if(@_); |
|
300
|
0
|
|
|
|
|
0
|
return $DBH; |
|
301
|
|
|
|
|
|
|
|
|
302
|
|
|
|
|
|
|
} |
|
303
|
|
|
|
|
|
|
|
|
304
|
|
|
|
|
|
|
#============================================================ |
|
305
|
|
|
|
|
|
|
sub quote { |
|
306
|
|
|
|
|
|
|
#============================================================ |
|
307
|
|
|
|
|
|
|
|
|
308
|
|
|
|
|
|
|
=head2 quote |
|
309
|
|
|
|
|
|
|
|
|
310
|
|
|
|
|
|
|
Usage : DBIx::Composer->quote() |
|
311
|
|
|
|
|
|
|
Purpose : Quotes its value by calling $dbh->quote. |
|
312
|
|
|
|
|
|
|
Returns : Quoted string |
|
313
|
|
|
|
|
|
|
Argument : String or number to quote |
|
314
|
|
|
|
|
|
|
Throws : None. |
|
315
|
|
|
|
|
|
|
Comments : None. |
|
316
|
|
|
|
|
|
|
|
|
317
|
|
|
|
|
|
|
See Also : |
|
318
|
|
|
|
|
|
|
|
|
319
|
|
|
|
|
|
|
=cut |
|
320
|
|
|
|
|
|
|
|
|
321
|
|
|
|
|
|
|
#------------------------------------------------------------ |
|
322
|
|
|
|
|
|
|
# my ($self, $str) = @_; |
|
323
|
|
|
|
|
|
|
# my $dbh; |
|
324
|
|
|
|
|
|
|
# |
|
325
|
|
|
|
|
|
|
# $dbh = $self->{_config}->{dbh}; |
|
326
|
|
|
|
|
|
|
# return $dbh->quote($str); |
|
327
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
|
328
|
0
|
|
|
|
|
0
|
return $self->{_config}->{dbh}->quote(shift); |
|
329
|
|
|
|
|
|
|
|
|
330
|
|
|
|
|
|
|
} |
|
331
|
|
|
|
|
|
|
|
|
332
|
|
|
|
|
|
|
#============================================================ |
|
333
|
|
|
|
|
|
|
sub compose_select { |
|
334
|
|
|
|
|
|
|
#============================================================ |
|
335
|
|
|
|
|
|
|
|
|
336
|
|
|
|
|
|
|
=head2 compose_select |
|
337
|
|
|
|
|
|
|
|
|
338
|
|
|
|
|
|
|
Usage : $cmd->compose_select() |
|
339
|
|
|
|
|
|
|
Purpose : Composes select statement for given object. |
|
340
|
|
|
|
|
|
|
Returns : Composed statement |
|
341
|
|
|
|
|
|
|
Argument : None. |
|
342
|
|
|
|
|
|
|
Throws : Returns undef, if required fields are missed. |
|
343
|
|
|
|
|
|
|
Comments : Composes "select $fields from $table $where $order $limit" |
|
344
|
|
|
|
|
|
|
statement where $fields stands for $cmd->fields and so on. |
|
345
|
|
|
|
|
|
|
$where, $group, $order and $limit are optional. |
|
346
|
|
|
|
|
|
|
|
|
347
|
|
|
|
|
|
|
|
|
348
|
|
|
|
|
|
|
See Also : |
|
349
|
|
|
|
|
|
|
|
|
350
|
|
|
|
|
|
|
=cut |
|
351
|
|
|
|
|
|
|
|
|
352
|
|
|
|
|
|
|
#------------------------------------------------------------ |
|
353
|
1
|
|
|
1
|
1
|
1034
|
my ($self, %parameters) = @_; |
|
354
|
|
|
|
|
|
|
|
|
355
|
|
|
|
|
|
|
# Check required keys |
|
356
|
1
|
50
|
|
|
|
7
|
return undef unless ($self->{fields}); |
|
357
|
|
|
|
|
|
|
|
|
358
|
|
|
|
|
|
|
# Compose |
|
359
|
1
|
|
|
|
|
6
|
$self->{cmd} = "select $self->{fields}"; |
|
360
|
1
|
50
|
|
|
|
8
|
$self->{cmd} .= " from $self->{table}" if $self->{table}; |
|
361
|
1
|
50
|
|
|
|
6
|
$self->{cmd} .= " $self->{where}" if $self->{where}; |
|
362
|
1
|
50
|
|
|
|
13
|
$self->{cmd} .= " $self->{group}" if $self->{group}; |
|
363
|
1
|
50
|
|
|
|
6
|
$self->{cmd} .= " $self->{order}" if $self->{order}; |
|
364
|
1
|
50
|
|
|
|
5
|
$self->{cmd} .= " $self->{limit}" if $self->{limit}; |
|
365
|
|
|
|
|
|
|
|
|
366
|
|
|
|
|
|
|
# Print debug, if needed |
|
367
|
1
|
|
|
|
|
7
|
$self->debug; |
|
368
|
|
|
|
|
|
|
|
|
369
|
1
|
|
|
|
|
4
|
return $self->{cmd}; |
|
370
|
|
|
|
|
|
|
} |
|
371
|
|
|
|
|
|
|
|
|
372
|
|
|
|
|
|
|
#============================================================ |
|
373
|
|
|
|
|
|
|
sub compose_insert { |
|
374
|
|
|
|
|
|
|
#============================================================ |
|
375
|
|
|
|
|
|
|
|
|
376
|
|
|
|
|
|
|
=head2 compose_insert |
|
377
|
|
|
|
|
|
|
|
|
378
|
|
|
|
|
|
|
Usage : $cmd->compose_insert() |
|
379
|
|
|
|
|
|
|
Purpose : Composes insert statement for given object. |
|
380
|
|
|
|
|
|
|
Returns : Composed statement |
|
381
|
|
|
|
|
|
|
Argument : None. |
|
382
|
|
|
|
|
|
|
Throws : Returns undef, if required fields are missed. |
|
383
|
|
|
|
|
|
|
Comments : Composes "insert into $table ($fields) values ($values)" |
|
384
|
|
|
|
|
|
|
or "insert into $table values ($values)" if $fields omitted. |
|
385
|
|
|
|
|
|
|
|
|
386
|
|
|
|
|
|
|
See Also : |
|
387
|
|
|
|
|
|
|
|
|
388
|
|
|
|
|
|
|
=cut |
|
389
|
|
|
|
|
|
|
|
|
390
|
|
|
|
|
|
|
#------------------------------------------------------------ |
|
391
|
2
|
|
|
2
|
1
|
3
|
my ($self, %parameters) = @_; |
|
392
|
|
|
|
|
|
|
|
|
393
|
|
|
|
|
|
|
# Check required keys |
|
394
|
2
|
50
|
|
|
|
7
|
return undef unless ($self->{table}); |
|
395
|
2
|
50
|
|
|
|
6
|
return undef unless ($self->{values}); |
|
396
|
|
|
|
|
|
|
|
|
397
|
|
|
|
|
|
|
# Compose |
|
398
|
2
|
|
|
|
|
5
|
$self->{cmd} = "insert into $self->{table}"; |
|
399
|
2
|
100
|
|
|
|
7
|
$self->{cmd} .= " ($self->{fields})" if $self->{fields}; |
|
400
|
2
|
|
|
|
|
4
|
$self->{cmd} .= " values ($self->{values})"; |
|
401
|
|
|
|
|
|
|
|
|
402
|
|
|
|
|
|
|
# Print debug, if needed |
|
403
|
2
|
|
|
|
|
5
|
$self->debug; |
|
404
|
|
|
|
|
|
|
|
|
405
|
2
|
|
|
|
|
9
|
return $self->{cmd}; |
|
406
|
|
|
|
|
|
|
} |
|
407
|
|
|
|
|
|
|
|
|
408
|
|
|
|
|
|
|
#============================================================ |
|
409
|
|
|
|
|
|
|
sub compose_replace { |
|
410
|
|
|
|
|
|
|
#============================================================ |
|
411
|
|
|
|
|
|
|
|
|
412
|
|
|
|
|
|
|
=head2 compose_replace |
|
413
|
|
|
|
|
|
|
|
|
414
|
|
|
|
|
|
|
Usage : $cmd->compose_replace() |
|
415
|
|
|
|
|
|
|
Purpose : Composes replace statement for given object. |
|
416
|
|
|
|
|
|
|
Returns : Composed statement |
|
417
|
|
|
|
|
|
|
Argument : None. |
|
418
|
|
|
|
|
|
|
Throws : Returns undef, if required fields are missed. |
|
419
|
|
|
|
|
|
|
Comments : Composes "replace into $table ($fields) values ($values)" |
|
420
|
|
|
|
|
|
|
or "replace into $table values ($values)" if $fields omitted. |
|
421
|
|
|
|
|
|
|
|
|
422
|
|
|
|
|
|
|
See Also : |
|
423
|
|
|
|
|
|
|
|
|
424
|
|
|
|
|
|
|
=cut |
|
425
|
|
|
|
|
|
|
|
|
426
|
|
|
|
|
|
|
#------------------------------------------------------------ |
|
427
|
3
|
|
|
3
|
1
|
6
|
my ($self, %parameters) = @_; |
|
428
|
|
|
|
|
|
|
|
|
429
|
|
|
|
|
|
|
# Check required keys |
|
430
|
3
|
50
|
|
|
|
11
|
return undef unless ($self->{table}); |
|
431
|
3
|
50
|
66
|
|
|
13
|
return undef unless ($self->{values} or $self->{set}); |
|
432
|
|
|
|
|
|
|
|
|
433
|
|
|
|
|
|
|
# Compose |
|
434
|
3
|
|
|
|
|
8
|
$self->{cmd} = "replace into $self->{table}"; |
|
435
|
|
|
|
|
|
|
|
|
436
|
|
|
|
|
|
|
# Insert-like syntax |
|
437
|
3
|
100
|
|
|
|
8
|
if($self->{values}) { |
|
438
|
2
|
100
|
|
|
|
6
|
$self->{cmd} .= " ($self->{fields})" if $self->{fields}; |
|
439
|
2
|
|
|
|
|
24
|
$self->{cmd} .= " values ($self->{values})"; |
|
440
|
|
|
|
|
|
|
|
|
441
|
|
|
|
|
|
|
# Update-like syntax |
|
442
|
|
|
|
|
|
|
} else { |
|
443
|
1
|
|
|
|
|
4
|
$self->{cmd} .= " $self->{set}"; |
|
444
|
|
|
|
|
|
|
} |
|
445
|
|
|
|
|
|
|
|
|
446
|
|
|
|
|
|
|
# Print debug, if needed |
|
447
|
3
|
|
|
|
|
8
|
$self->debug; |
|
448
|
|
|
|
|
|
|
|
|
449
|
3
|
|
|
|
|
14
|
return $self->{cmd}; |
|
450
|
|
|
|
|
|
|
} |
|
451
|
|
|
|
|
|
|
|
|
452
|
|
|
|
|
|
|
|
|
453
|
|
|
|
|
|
|
#============================================================ |
|
454
|
|
|
|
|
|
|
sub compose_delete { |
|
455
|
|
|
|
|
|
|
#============================================================ |
|
456
|
|
|
|
|
|
|
|
|
457
|
|
|
|
|
|
|
=head2 compose_delete |
|
458
|
|
|
|
|
|
|
|
|
459
|
|
|
|
|
|
|
Usage : $cmd->compose_delete() |
|
460
|
|
|
|
|
|
|
Purpose : Composes delete statement for given object. |
|
461
|
|
|
|
|
|
|
Returns : Composed statement |
|
462
|
|
|
|
|
|
|
Argument : None. |
|
463
|
|
|
|
|
|
|
Throws : Returns undef, if required fields are missed. |
|
464
|
|
|
|
|
|
|
Comments : Composes "delete from $table $where" |
|
465
|
|
|
|
|
|
|
or "delete from $table" if $where omitted. |
|
466
|
|
|
|
|
|
|
|
|
467
|
|
|
|
|
|
|
|
|
468
|
|
|
|
|
|
|
See Also : |
|
469
|
|
|
|
|
|
|
|
|
470
|
|
|
|
|
|
|
=cut |
|
471
|
|
|
|
|
|
|
|
|
472
|
|
|
|
|
|
|
#------------------------------------------------------------ |
|
473
|
2
|
|
|
2
|
1
|
4
|
my ($self, %parameters) = @_; |
|
474
|
|
|
|
|
|
|
|
|
475
|
|
|
|
|
|
|
# Check required keys |
|
476
|
2
|
50
|
|
|
|
7
|
return undef unless ($self->{table}); |
|
477
|
|
|
|
|
|
|
|
|
478
|
|
|
|
|
|
|
# Compose |
|
479
|
2
|
|
|
|
|
6
|
$self->{cmd} = "delete from $self->{table}"; |
|
480
|
2
|
100
|
|
|
|
11
|
$self->{cmd} .= " $self->{where}" if $self->{where}; |
|
481
|
|
|
|
|
|
|
|
|
482
|
|
|
|
|
|
|
# Print debug, if needed |
|
483
|
2
|
|
|
|
|
8
|
$self->debug; |
|
484
|
|
|
|
|
|
|
|
|
485
|
2
|
|
|
|
|
10
|
return $self->{cmd}; |
|
486
|
|
|
|
|
|
|
} |
|
487
|
|
|
|
|
|
|
|
|
488
|
|
|
|
|
|
|
#============================================================ |
|
489
|
|
|
|
|
|
|
sub compose_update { |
|
490
|
|
|
|
|
|
|
#============================================================ |
|
491
|
|
|
|
|
|
|
|
|
492
|
|
|
|
|
|
|
=head2 compose_update |
|
493
|
|
|
|
|
|
|
|
|
494
|
|
|
|
|
|
|
Usage : $cmd->compose_update() |
|
495
|
|
|
|
|
|
|
Purpose : Composes update statement for given object. |
|
496
|
|
|
|
|
|
|
Returns : Composed statement |
|
497
|
|
|
|
|
|
|
Argument : None. |
|
498
|
|
|
|
|
|
|
Throws : Returns undef, if required fields are missed. |
|
499
|
|
|
|
|
|
|
Comments : Composes "update $table $set $where" |
|
500
|
|
|
|
|
|
|
or "update $table $set" if $where omitted. |
|
501
|
|
|
|
|
|
|
|
|
502
|
|
|
|
|
|
|
See Also : |
|
503
|
|
|
|
|
|
|
|
|
504
|
|
|
|
|
|
|
=cut |
|
505
|
|
|
|
|
|
|
|
|
506
|
|
|
|
|
|
|
#------------------------------------------------------------ |
|
507
|
2
|
|
|
2
|
1
|
5
|
my ($self, %parameters) = @_; |
|
508
|
|
|
|
|
|
|
|
|
509
|
|
|
|
|
|
|
# Check required keys |
|
510
|
2
|
50
|
|
|
|
7
|
return undef unless ($self->{table}); |
|
511
|
2
|
50
|
|
|
|
5
|
return undef unless ($self->{set}); |
|
512
|
|
|
|
|
|
|
|
|
513
|
|
|
|
|
|
|
# Compose |
|
514
|
2
|
|
|
|
|
6
|
$self->{cmd} = "update $self->{table} $self->{set}"; |
|
515
|
2
|
100
|
|
|
|
6
|
$self->{cmd} .= " $self->{where}" if $self->{where}; |
|
516
|
|
|
|
|
|
|
|
|
517
|
|
|
|
|
|
|
# Print debug, if needed |
|
518
|
2
|
|
|
|
|
5
|
$self->debug; |
|
519
|
|
|
|
|
|
|
|
|
520
|
2
|
|
|
|
|
8
|
return $self->{cmd}; |
|
521
|
|
|
|
|
|
|
} |
|
522
|
|
|
|
|
|
|
|
|
523
|
|
|
|
|
|
|
#============================================================ |
|
524
|
|
|
|
|
|
|
sub selectrow_array { |
|
525
|
|
|
|
|
|
|
#============================================================ |
|
526
|
|
|
|
|
|
|
|
|
527
|
|
|
|
|
|
|
=head2 selectrow_array |
|
528
|
|
|
|
|
|
|
|
|
529
|
|
|
|
|
|
|
Usage : $cmd->selectrow_array() |
|
530
|
|
|
|
|
|
|
Purpose : Makes DBI call of selectrow_array |
|
531
|
|
|
|
|
|
|
Returns : Array or scalar |
|
532
|
|
|
|
|
|
|
Argument : None. |
|
533
|
|
|
|
|
|
|
Throws : Returns undef, if required fields are missed. |
|
534
|
|
|
|
|
|
|
Comments : |
|
535
|
|
|
|
|
|
|
|
|
536
|
|
|
|
|
|
|
See Also : |
|
537
|
|
|
|
|
|
|
|
|
538
|
|
|
|
|
|
|
=cut |
|
539
|
|
|
|
|
|
|
|
|
540
|
|
|
|
|
|
|
#------------------------------------------------------------ |
|
541
|
0
|
|
|
0
|
1
|
0
|
my ($self, %parameters) = @_; |
|
542
|
|
|
|
|
|
|
|
|
543
|
|
|
|
|
|
|
# Check required keys |
|
544
|
0
|
0
|
|
|
|
0
|
return undef unless ($self->{_config}->{dbh}); |
|
545
|
|
|
|
|
|
|
|
|
546
|
|
|
|
|
|
|
# Compose |
|
547
|
0
|
0
|
|
|
|
0
|
$self->compose_select unless $self->{cmd}; |
|
548
|
0
|
0
|
|
|
|
0
|
return undef unless ($self->{cmd}); |
|
549
|
|
|
|
|
|
|
|
|
550
|
|
|
|
|
|
|
# Make DBI call |
|
551
|
0
|
|
|
|
|
0
|
$self->log; |
|
552
|
|
|
|
|
|
|
# return $self->{_config}->{dbh}->selectrow_array($self->{cmd}); |
|
553
|
|
|
|
|
|
|
|
|
554
|
|
|
|
|
|
|
# if(wantarray()) { |
|
555
|
|
|
|
|
|
|
# my @arr = $self->{_config}->{dbh}->selectrow_array($self->{cmd}); |
|
556
|
|
|
|
|
|
|
# return @arr; |
|
557
|
|
|
|
|
|
|
# } else { |
|
558
|
|
|
|
|
|
|
# return $self->{_config}->{dbh}->selectrow_array($self->{cmd}); |
|
559
|
|
|
|
|
|
|
# } |
|
560
|
0
|
|
|
|
|
0
|
return $self->{_config}->{dbh}->selectrow_array($self->{cmd}); |
|
561
|
|
|
|
|
|
|
|
|
562
|
|
|
|
|
|
|
} |
|
563
|
|
|
|
|
|
|
|
|
564
|
|
|
|
|
|
|
#============================================================ |
|
565
|
|
|
|
|
|
|
sub fetch { |
|
566
|
|
|
|
|
|
|
#============================================================ |
|
567
|
|
|
|
|
|
|
|
|
568
|
|
|
|
|
|
|
=head2 fetch |
|
569
|
|
|
|
|
|
|
|
|
570
|
|
|
|
|
|
|
Usage : $cmd->fetch() |
|
571
|
|
|
|
|
|
|
Purpose : Makes DBI call of fetch |
|
572
|
|
|
|
|
|
|
Returns : Array of data |
|
573
|
|
|
|
|
|
|
Argument : None. |
|
574
|
|
|
|
|
|
|
Throws : Returns undef, if error occured. See $dbh->errstr for errors. |
|
575
|
|
|
|
|
|
|
Comments : After last row returns undef too. |
|
576
|
|
|
|
|
|
|
|
|
577
|
|
|
|
|
|
|
See Also : |
|
578
|
|
|
|
|
|
|
|
|
579
|
|
|
|
|
|
|
=cut |
|
580
|
|
|
|
|
|
|
|
|
581
|
|
|
|
|
|
|
#------------------------------------------------------------ |
|
582
|
0
|
|
|
0
|
1
|
0
|
my ($self, %parameters) = @_; |
|
583
|
|
|
|
|
|
|
|
|
584
|
|
|
|
|
|
|
# execute() called yet? |
|
585
|
0
|
0
|
|
|
|
0
|
unless($self->{_executed}) { |
|
586
|
|
|
|
|
|
|
|
|
587
|
|
|
|
|
|
|
# Is $sth ready? |
|
588
|
0
|
0
|
|
|
|
0
|
unless($self->{sth}) { |
|
589
|
|
|
|
|
|
|
|
|
590
|
|
|
|
|
|
|
# Was command prepared yet? |
|
591
|
0
|
0
|
|
|
|
0
|
unless ($self->{cmd}) { |
|
592
|
0
|
0
|
|
|
|
0
|
$self->compose_select unless $self->{cmd}; |
|
593
|
0
|
0
|
|
|
|
0
|
return undef unless ($self->{cmd}); |
|
594
|
|
|
|
|
|
|
} |
|
595
|
|
|
|
|
|
|
|
|
596
|
|
|
|
|
|
|
# Prepare |
|
597
|
0
|
|
|
|
|
0
|
$self->{sth} = $self->{_config}->{dbh}->prepare($self->{cmd}); |
|
598
|
0
|
0
|
|
|
|
0
|
return undef unless ($self->{sth}); |
|
599
|
|
|
|
|
|
|
} |
|
600
|
|
|
|
|
|
|
|
|
601
|
|
|
|
|
|
|
# Execute |
|
602
|
0
|
|
|
|
|
0
|
$self->log; |
|
603
|
0
|
0
|
|
|
|
0
|
$self->{sth}->execute || return undef; |
|
604
|
0
|
|
|
|
|
0
|
$self->{_executed} = 1; |
|
605
|
|
|
|
|
|
|
} |
|
606
|
|
|
|
|
|
|
|
|
607
|
0
|
|
|
|
|
0
|
return $self->{sth}->fetch(); |
|
608
|
|
|
|
|
|
|
|
|
609
|
|
|
|
|
|
|
} |
|
610
|
|
|
|
|
|
|
|
|
611
|
|
|
|
|
|
|
#============================================================ |
|
612
|
|
|
|
|
|
|
sub fetchrow_hashref { |
|
613
|
|
|
|
|
|
|
#============================================================ |
|
614
|
|
|
|
|
|
|
|
|
615
|
|
|
|
|
|
|
=head2 fetchrow_hashref |
|
616
|
|
|
|
|
|
|
|
|
617
|
|
|
|
|
|
|
Usage : $cmd->fetchrow_hashref() |
|
618
|
|
|
|
|
|
|
Purpose : Makes DBI call of fetchrow_hashref |
|
619
|
|
|
|
|
|
|
Returns : Array of data |
|
620
|
|
|
|
|
|
|
Argument : None. |
|
621
|
|
|
|
|
|
|
Throws : Returns undef, if error occured. See $dbh->errstr for errors. |
|
622
|
|
|
|
|
|
|
Comments : After last row returns undef too. |
|
623
|
|
|
|
|
|
|
|
|
624
|
|
|
|
|
|
|
See Also : |
|
625
|
|
|
|
|
|
|
|
|
626
|
|
|
|
|
|
|
=cut |
|
627
|
|
|
|
|
|
|
|
|
628
|
|
|
|
|
|
|
#------------------------------------------------------------ |
|
629
|
0
|
|
|
0
|
1
|
0
|
my ($self, %parameters) = @_; |
|
630
|
|
|
|
|
|
|
|
|
631
|
|
|
|
|
|
|
# execute() called yet? |
|
632
|
0
|
0
|
|
|
|
0
|
unless($self->{_executed}) { |
|
633
|
|
|
|
|
|
|
|
|
634
|
|
|
|
|
|
|
# Is $sth ready? |
|
635
|
0
|
0
|
|
|
|
0
|
unless($self->{sth}) { |
|
636
|
|
|
|
|
|
|
|
|
637
|
|
|
|
|
|
|
# Was command prepared yet? |
|
638
|
0
|
0
|
|
|
|
0
|
unless ($self->{cmd}) { |
|
639
|
0
|
0
|
|
|
|
0
|
$self->compose_select unless $self->{cmd}; |
|
640
|
0
|
0
|
|
|
|
0
|
return undef unless ($self->{cmd}); |
|
641
|
|
|
|
|
|
|
} |
|
642
|
|
|
|
|
|
|
|
|
643
|
|
|
|
|
|
|
# Prepare |
|
644
|
0
|
|
|
|
|
0
|
$self->{sth} = $self->{_config}->{dbh}->prepare($self->{cmd}); |
|
645
|
0
|
0
|
|
|
|
0
|
return undef unless ($self->{sth}); |
|
646
|
|
|
|
|
|
|
} |
|
647
|
|
|
|
|
|
|
|
|
648
|
|
|
|
|
|
|
# Execute |
|
649
|
0
|
0
|
|
|
|
0
|
$self->{sth}->execute || return undef; |
|
650
|
0
|
|
|
|
|
0
|
$self->{_executed} = 1; |
|
651
|
|
|
|
|
|
|
} |
|
652
|
|
|
|
|
|
|
|
|
653
|
0
|
|
|
|
|
0
|
return $self->{sth}->fetchrow_hashref(); |
|
654
|
|
|
|
|
|
|
|
|
655
|
|
|
|
|
|
|
} |
|
656
|
|
|
|
|
|
|
|
|
657
|
|
|
|
|
|
|
#============================================================ |
|
658
|
|
|
|
|
|
|
sub insert { |
|
659
|
|
|
|
|
|
|
#============================================================ |
|
660
|
|
|
|
|
|
|
|
|
661
|
|
|
|
|
|
|
=head2 insert |
|
662
|
|
|
|
|
|
|
|
|
663
|
|
|
|
|
|
|
Usage : $cmd->insert() |
|
664
|
|
|
|
|
|
|
Purpose : Makes DBI call of insert |
|
665
|
|
|
|
|
|
|
Returns : ID of inserted row (as "last insert id"). |
|
666
|
|
|
|
|
|
|
Argument : None. |
|
667
|
|
|
|
|
|
|
Throws : Returns undef, if required fields are missed. |
|
668
|
|
|
|
|
|
|
Comments : |
|
669
|
|
|
|
|
|
|
|
|
670
|
|
|
|
|
|
|
See Also : |
|
671
|
|
|
|
|
|
|
|
|
672
|
|
|
|
|
|
|
=cut |
|
673
|
|
|
|
|
|
|
|
|
674
|
|
|
|
|
|
|
#------------------------------------------------------------ |
|
675
|
0
|
|
|
0
|
1
|
0
|
my ($self, %parameters) = @_; |
|
676
|
|
|
|
|
|
|
|
|
677
|
|
|
|
|
|
|
# Check required keys |
|
678
|
0
|
0
|
|
|
|
0
|
return undef unless ($self->{table}); |
|
679
|
|
|
|
|
|
|
|
|
680
|
|
|
|
|
|
|
# Compose |
|
681
|
0
|
0
|
|
|
|
0
|
$self->compose_insert unless $self->{cmd}; |
|
682
|
0
|
0
|
|
|
|
0
|
return undef unless ($self->{cmd}); |
|
683
|
|
|
|
|
|
|
|
|
684
|
|
|
|
|
|
|
# Make DBI call |
|
685
|
0
|
|
|
|
|
0
|
$self->log; |
|
686
|
0
|
|
|
|
|
0
|
$self->{_config}->{dbh}->do($self->{cmd}); |
|
687
|
|
|
|
|
|
|
|
|
688
|
0
|
|
|
|
|
0
|
return $self->{_config}->{dbh}->{'mysql_insertid'}; |
|
689
|
|
|
|
|
|
|
|
|
690
|
|
|
|
|
|
|
} |
|
691
|
|
|
|
|
|
|
|
|
692
|
|
|
|
|
|
|
#============================================================ |
|
693
|
|
|
|
|
|
|
sub replace { |
|
694
|
|
|
|
|
|
|
#============================================================ |
|
695
|
|
|
|
|
|
|
|
|
696
|
|
|
|
|
|
|
=head2 replace |
|
697
|
|
|
|
|
|
|
|
|
698
|
|
|
|
|
|
|
Usage : $cmd->replace() |
|
699
|
|
|
|
|
|
|
Purpose : Makes DBI call of replace |
|
700
|
|
|
|
|
|
|
Returns : ID of replaced row (as "last insert id"). |
|
701
|
|
|
|
|
|
|
Argument : None. |
|
702
|
|
|
|
|
|
|
Throws : Returns undef, if required fields are missed. |
|
703
|
|
|
|
|
|
|
Comments : |
|
704
|
|
|
|
|
|
|
|
|
705
|
|
|
|
|
|
|
See Also : |
|
706
|
|
|
|
|
|
|
|
|
707
|
|
|
|
|
|
|
=cut |
|
708
|
|
|
|
|
|
|
|
|
709
|
|
|
|
|
|
|
#------------------------------------------------------------ |
|
710
|
0
|
|
|
0
|
1
|
0
|
my ($self, %parameters) = @_; |
|
711
|
|
|
|
|
|
|
|
|
712
|
|
|
|
|
|
|
# Check required keys |
|
713
|
0
|
0
|
|
|
|
0
|
return undef unless ($self->{table}); |
|
714
|
|
|
|
|
|
|
|
|
715
|
|
|
|
|
|
|
# Compose |
|
716
|
0
|
0
|
|
|
|
0
|
$self->compose_replace unless $self->{cmd}; |
|
717
|
0
|
0
|
|
|
|
0
|
return undef unless ($self->{cmd}); |
|
718
|
|
|
|
|
|
|
|
|
719
|
|
|
|
|
|
|
# Make DBI call |
|
720
|
0
|
|
|
|
|
0
|
$self->log; |
|
721
|
0
|
|
|
|
|
0
|
$self->{_config}->{dbh}->do($self->{cmd}); |
|
722
|
|
|
|
|
|
|
|
|
723
|
0
|
|
|
|
|
0
|
return $self->{_config}->{dbh}->{'mysql_insertid'}; |
|
724
|
|
|
|
|
|
|
|
|
725
|
|
|
|
|
|
|
} |
|
726
|
|
|
|
|
|
|
|
|
727
|
|
|
|
|
|
|
#============================================================ |
|
728
|
|
|
|
|
|
|
sub delete { |
|
729
|
|
|
|
|
|
|
#============================================================ |
|
730
|
|
|
|
|
|
|
|
|
731
|
|
|
|
|
|
|
=head2 delete |
|
732
|
|
|
|
|
|
|
|
|
733
|
|
|
|
|
|
|
Usage : $cmd->delete() |
|
734
|
|
|
|
|
|
|
Purpose : Makes DBI call of delete |
|
735
|
|
|
|
|
|
|
Returns : 1 if OK, false otherwise. |
|
736
|
|
|
|
|
|
|
Argument : None. |
|
737
|
|
|
|
|
|
|
Throws : Returns undef, if required fields are missed. |
|
738
|
|
|
|
|
|
|
Comments : |
|
739
|
|
|
|
|
|
|
|
|
740
|
|
|
|
|
|
|
See Also : |
|
741
|
|
|
|
|
|
|
|
|
742
|
|
|
|
|
|
|
=cut |
|
743
|
|
|
|
|
|
|
|
|
744
|
|
|
|
|
|
|
#------------------------------------------------------------ |
|
745
|
0
|
|
|
0
|
1
|
0
|
my ($self, %parameters) = @_; |
|
746
|
|
|
|
|
|
|
|
|
747
|
|
|
|
|
|
|
# Check required keys |
|
748
|
0
|
0
|
|
|
|
0
|
return undef unless ($self->{table}); |
|
749
|
|
|
|
|
|
|
|
|
750
|
|
|
|
|
|
|
# Compose |
|
751
|
0
|
0
|
|
|
|
0
|
$self->compose_delete unless $self->{cmd}; |
|
752
|
0
|
0
|
|
|
|
0
|
return undef unless ($self->{cmd}); |
|
753
|
|
|
|
|
|
|
|
|
754
|
|
|
|
|
|
|
# Make DBI call |
|
755
|
0
|
|
|
|
|
0
|
$self->log; |
|
756
|
0
|
0
|
|
|
|
0
|
unless ($self->{_config}->{dbh}->do($self->{cmd})) { |
|
757
|
0
|
|
|
|
|
0
|
warn $self->{_config}->{dbh}->errstr; |
|
758
|
0
|
|
|
|
|
0
|
return 0; |
|
759
|
|
|
|
|
|
|
} |
|
760
|
|
|
|
|
|
|
|
|
761
|
0
|
|
|
|
|
0
|
return 1; |
|
762
|
|
|
|
|
|
|
|
|
763
|
|
|
|
|
|
|
} |
|
764
|
|
|
|
|
|
|
|
|
765
|
|
|
|
|
|
|
#============================================================ |
|
766
|
|
|
|
|
|
|
sub update { |
|
767
|
|
|
|
|
|
|
#============================================================ |
|
768
|
|
|
|
|
|
|
|
|
769
|
|
|
|
|
|
|
=head2 update |
|
770
|
|
|
|
|
|
|
|
|
771
|
|
|
|
|
|
|
Usage : $cmd->update() |
|
772
|
|
|
|
|
|
|
Purpose : Makes DBI call of update |
|
773
|
|
|
|
|
|
|
Returns : 1 if OK, false otherwise. |
|
774
|
|
|
|
|
|
|
Argument : None. |
|
775
|
|
|
|
|
|
|
Throws : Returns undef, if required fields are missed. |
|
776
|
|
|
|
|
|
|
Comments : |
|
777
|
|
|
|
|
|
|
|
|
778
|
|
|
|
|
|
|
See Also : |
|
779
|
|
|
|
|
|
|
|
|
780
|
|
|
|
|
|
|
=cut |
|
781
|
|
|
|
|
|
|
|
|
782
|
|
|
|
|
|
|
#------------------------------------------------------------ |
|
783
|
0
|
|
|
0
|
1
|
0
|
my ($self, %parameters) = @_; |
|
784
|
|
|
|
|
|
|
|
|
785
|
|
|
|
|
|
|
# Check required keys |
|
786
|
0
|
0
|
|
|
|
0
|
return undef unless ($self->{table}); |
|
787
|
|
|
|
|
|
|
|
|
788
|
|
|
|
|
|
|
# Compose |
|
789
|
0
|
0
|
|
|
|
0
|
$self->compose_update unless $self->{cmd}; |
|
790
|
0
|
0
|
|
|
|
0
|
return undef unless ($self->{cmd}); |
|
791
|
|
|
|
|
|
|
|
|
792
|
|
|
|
|
|
|
# Make DBI call |
|
793
|
0
|
|
|
|
|
0
|
$self->log; |
|
794
|
0
|
0
|
|
|
|
0
|
unless ($self->{_config}->{dbh}->do($self->{cmd})) { |
|
795
|
0
|
|
|
|
|
0
|
warn $self->{_config}->{dbh}->errstr; |
|
796
|
0
|
|
|
|
|
0
|
return 0; |
|
797
|
|
|
|
|
|
|
} |
|
798
|
|
|
|
|
|
|
|
|
799
|
0
|
|
|
|
|
0
|
return 1; |
|
800
|
|
|
|
|
|
|
|
|
801
|
|
|
|
|
|
|
} |
|
802
|
|
|
|
|
|
|
|
|
803
|
|
|
|
|
|
|
#============================================================ |
|
804
|
|
|
|
|
|
|
sub log_cmd { |
|
805
|
|
|
|
|
|
|
#============================================================ |
|
806
|
|
|
|
|
|
|
|
|
807
|
|
|
|
|
|
|
=head2 log_cmd |
|
808
|
|
|
|
|
|
|
|
|
809
|
|
|
|
|
|
|
Usage : $cmd->log_cmd() |
|
810
|
|
|
|
|
|
|
Purpose : Logs SQL command to STDERR. |
|
811
|
|
|
|
|
|
|
Returns : Nothing. |
|
812
|
|
|
|
|
|
|
Argument : 1 - output format |
|
813
|
|
|
|
|
|
|
Throws : Returns undef, if required fields are missed. |
|
814
|
|
|
|
|
|
|
Comments : Should't be called directly. Set flag debug |
|
815
|
|
|
|
|
|
|
instead when called new(). |
|
816
|
|
|
|
|
|
|
|
|
817
|
|
|
|
|
|
|
See Also : |
|
818
|
|
|
|
|
|
|
|
|
819
|
|
|
|
|
|
|
=cut |
|
820
|
|
|
|
|
|
|
|
|
821
|
|
|
|
|
|
|
#------------------------------------------------------------ |
|
822
|
0
|
|
|
0
|
1
|
0
|
my ($self, $fmt) = @_; |
|
823
|
|
|
|
|
|
|
|
|
824
|
|
|
|
|
|
|
# Check format |
|
825
|
0
|
0
|
|
|
|
0
|
$fmt = "%s\n" unless $fmt; |
|
826
|
0
|
|
|
|
|
0
|
printf STDERR $fmt, $self->{cmd}; |
|
827
|
|
|
|
|
|
|
|
|
828
|
|
|
|
|
|
|
# Log command into STDERR |
|
829
|
|
|
|
|
|
|
|
|
830
|
|
|
|
|
|
|
} |
|
831
|
|
|
|
|
|
|
|
|
832
|
|
|
|
|
|
|
#============================================================ |
|
833
|
|
|
|
|
|
|
sub debug { |
|
834
|
|
|
|
|
|
|
#============================================================ |
|
835
|
|
|
|
|
|
|
|
|
836
|
|
|
|
|
|
|
=head2 debug |
|
837
|
|
|
|
|
|
|
|
|
838
|
|
|
|
|
|
|
Usage : $cmd->debug() |
|
839
|
|
|
|
|
|
|
Purpose : Logs SQL command to STDERR. |
|
840
|
|
|
|
|
|
|
Returns : Nothing. |
|
841
|
|
|
|
|
|
|
Argument : None. |
|
842
|
|
|
|
|
|
|
Throws : Returns undef, if required fields are missed. |
|
843
|
|
|
|
|
|
|
Comments : Should't be called directly. Set flag debug > 0 |
|
844
|
|
|
|
|
|
|
instead when called new(). |
|
845
|
|
|
|
|
|
|
|
|
846
|
|
|
|
|
|
|
See Also : |
|
847
|
|
|
|
|
|
|
|
|
848
|
|
|
|
|
|
|
=cut |
|
849
|
|
|
|
|
|
|
|
|
850
|
|
|
|
|
|
|
#------------------------------------------------------------ |
|
851
|
10
|
|
|
10
|
1
|
15
|
my ($self, %parameters) = @_; |
|
852
|
|
|
|
|
|
|
|
|
853
|
|
|
|
|
|
|
# If debug level < 1 - do nothing |
|
854
|
10
|
50
|
|
|
|
29
|
return unless($self->{_config}->{debug} > 1); |
|
855
|
|
|
|
|
|
|
|
|
856
|
|
|
|
|
|
|
# Log command into STDERR |
|
857
|
0
|
|
|
|
|
|
$self->log_cmd("SQL debug: $0 -> [%s]\n"); |
|
858
|
|
|
|
|
|
|
|
|
859
|
|
|
|
|
|
|
} |
|
860
|
|
|
|
|
|
|
|
|
861
|
|
|
|
|
|
|
#============================================================ |
|
862
|
|
|
|
|
|
|
sub log { |
|
863
|
|
|
|
|
|
|
#============================================================ |
|
864
|
|
|
|
|
|
|
|
|
865
|
|
|
|
|
|
|
=head2 log |
|
866
|
|
|
|
|
|
|
|
|
867
|
|
|
|
|
|
|
Usage : $cmd->log() |
|
868
|
|
|
|
|
|
|
Purpose : Logs SQL command to STDERR. |
|
869
|
|
|
|
|
|
|
Returns : Nothing. |
|
870
|
|
|
|
|
|
|
Argument : None. |
|
871
|
|
|
|
|
|
|
Throws : Returns undef, if required fields are missed. |
|
872
|
|
|
|
|
|
|
Comments : Should't be called directly. Set flag debug > 1 |
|
873
|
|
|
|
|
|
|
instead when called new(). |
|
874
|
|
|
|
|
|
|
|
|
875
|
|
|
|
|
|
|
See Also : |
|
876
|
|
|
|
|
|
|
|
|
877
|
|
|
|
|
|
|
=cut |
|
878
|
|
|
|
|
|
|
|
|
879
|
|
|
|
|
|
|
#------------------------------------------------------------ |
|
880
|
0
|
|
|
0
|
1
|
|
my ($self, %parameters) = @_; |
|
881
|
|
|
|
|
|
|
|
|
882
|
|
|
|
|
|
|
# If debug level < 0 - do nothing |
|
883
|
0
|
0
|
|
|
|
|
return unless($self->{_config}->{debug} > 0); |
|
884
|
|
|
|
|
|
|
|
|
885
|
|
|
|
|
|
|
# Log command into STDERR |
|
886
|
0
|
|
|
|
|
|
$self->log_cmd("SQL run: $0 -> [%s]\n"); |
|
887
|
|
|
|
|
|
|
|
|
888
|
|
|
|
|
|
|
} |
|
889
|
|
|
|
|
|
|
|
|
890
|
|
|
|
|
|
|
=head2 Functions for access to inner object data as lvalue |
|
891
|
|
|
|
|
|
|
|
|
892
|
|
|
|
|
|
|
Usage : $cmd->table = "users"; or: $table_sav = $cmd->table; |
|
893
|
|
|
|
|
|
|
Purpose : Make access to inner variables without hash curlies. |
|
894
|
|
|
|
|
|
|
Comments : Warning: don't work in Perl < 5.6. Use form $cmd->{table} instead. |
|
895
|
|
|
|
|
|
|
|
|
896
|
|
|
|
|
|
|
Currently supported functions: |
|
897
|
|
|
|
|
|
|
table() |
|
898
|
|
|
|
|
|
|
fields() |
|
899
|
|
|
|
|
|
|
values() |
|
900
|
|
|
|
|
|
|
where() |
|
901
|
|
|
|
|
|
|
set() |
|
902
|
|
|
|
|
|
|
order() |
|
903
|
|
|
|
|
|
|
group() |
|
904
|
|
|
|
|
|
|
limit() |
|
905
|
|
|
|
|
|
|
|
|
906
|
|
|
|
|
|
|
=cut |
|
907
|
|
|
|
|
|
|
|
|
908
|
|
|
|
|
|
|
# use 5.6.1; # for lvalues |
|
909
|
|
|
|
|
|
|
|
|
910
|
|
|
|
|
|
|
#============================================================ |
|
911
|
|
|
|
|
|
|
sub table : lvalue { |
|
912
|
|
|
|
|
|
|
#------------------------------------------------------------ |
|
913
|
0
|
|
|
0
|
0
|
|
shift()->{table}; |
|
914
|
|
|
|
|
|
|
} |
|
915
|
|
|
|
|
|
|
|
|
916
|
|
|
|
|
|
|
#============================================================ |
|
917
|
|
|
|
|
|
|
sub fields : lvalue { |
|
918
|
|
|
|
|
|
|
#------------------------------------------------------------ |
|
919
|
0
|
|
|
0
|
0
|
|
shift()->{fields}; |
|
920
|
|
|
|
|
|
|
} |
|
921
|
|
|
|
|
|
|
|
|
922
|
|
|
|
|
|
|
#============================================================ |
|
923
|
|
|
|
|
|
|
sub values : lvalue { |
|
924
|
|
|
|
|
|
|
#------------------------------------------------------------ |
|
925
|
0
|
|
|
0
|
0
|
|
shift()->{values}; |
|
926
|
|
|
|
|
|
|
} |
|
927
|
|
|
|
|
|
|
|
|
928
|
|
|
|
|
|
|
#============================================================ |
|
929
|
|
|
|
|
|
|
sub where : lvalue { |
|
930
|
|
|
|
|
|
|
#------------------------------------------------------------ |
|
931
|
0
|
|
|
0
|
0
|
|
shift()->{where}; |
|
932
|
|
|
|
|
|
|
} |
|
933
|
|
|
|
|
|
|
|
|
934
|
|
|
|
|
|
|
#============================================================ |
|
935
|
|
|
|
|
|
|
sub set : lvalue { |
|
936
|
|
|
|
|
|
|
#------------------------------------------------------------ |
|
937
|
0
|
|
|
0
|
0
|
|
shift()->{set}; |
|
938
|
|
|
|
|
|
|
} |
|
939
|
|
|
|
|
|
|
|
|
940
|
|
|
|
|
|
|
#============================================================ |
|
941
|
|
|
|
|
|
|
sub order : lvalue { |
|
942
|
|
|
|
|
|
|
#------------------------------------------------------------ |
|
943
|
0
|
|
|
0
|
0
|
|
shift()->{order}; |
|
944
|
|
|
|
|
|
|
} |
|
945
|
|
|
|
|
|
|
|
|
946
|
|
|
|
|
|
|
#============================================================ |
|
947
|
|
|
|
|
|
|
sub group : lvalue { |
|
948
|
|
|
|
|
|
|
#------------------------------------------------------------ |
|
949
|
0
|
|
|
0
|
0
|
|
shift()->{group}; |
|
950
|
|
|
|
|
|
|
} |
|
951
|
|
|
|
|
|
|
|
|
952
|
|
|
|
|
|
|
#============================================================ |
|
953
|
|
|
|
|
|
|
sub limit : lvalue { |
|
954
|
|
|
|
|
|
|
#------------------------------------------------------------ |
|
955
|
0
|
|
|
0
|
0
|
|
shift()->{limit}; |
|
956
|
|
|
|
|
|
|
} |
|
957
|
|
|
|
|
|
|
|
|
958
|
|
|
|
|
|
|
|
|
959
|
|
|
|
|
|
|
1; #this line is important and will help the module return a true value |
|
960
|
|
|
|
|
|
|
__END__ |