This repository has been archived by the owner on May 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDBSQL.pm
315 lines (264 loc) · 7.4 KB
/
DBSQL.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package DBSQL;
use strict;
use warnings;
use DBI;
my $dbh;
my $DEBUG_SQL = 0 || $ENV{SQL_DEBUG};
1;
sub IsOpen {
return defined($dbh);
}
sub Open { # ($dbname, $username, $password)
# The first argument is the name of the database
# or if it's on another computer, database@host.
my $dbname = shift;
my $db_user_name = shift;
my $db_password = shift;
if (defined($dbh)) { die "Database already open."; }
my $dsn = "DBI:mysql:$dbname";
$dbh = DBI->connect($dsn, $db_user_name, $db_password) || die "Connection to database failed: $DBI::errstr";
$dbh->{'mysql_enable_utf8'} = 1;
my $sth = $dbh->prepare('SET NAMES "UTF8"');
$sth->execute();
$sth->finish();
}
sub Close {
if (!defined($dbh)) { die "Database not open."; }
$dbh->disconnect();
undef $dbh;
}
sub Execute {
my $sth = $dbh->prepare($_[0]);
$sth->execute();
if ($sth->{NUM_OF_FIELDS}) {
my @ret = @{ $sth->fetchall_arrayref() };
$sth->finish();
return @ret;
} else {
$sth->finish();
my @ret = ();
return @ret;
}
}
sub SelectByID { # ($table, $id, \@fields, [other select options]) => array
my $table = shift;
my $id = shift;
my $fields = shift;
return Select("first", @_, $table, $fields, [SpecEQ('id', $id)]);
}
sub SelectFirst { # ([options], $table, \@fields, \@specs) => array
unshift @_, "first";
return &Select;
}
sub SelectAll { # ([options], $table, \@fields, \@specs) => array of arrayrefs
unshift @_, "all";
return &Select;
}
sub SelectVector { # ($table, \@fields, \@specs) => array
unshift @_, "all";
my @all = &Select;
my @ret;
foreach my $x (@all) {
push @ret, @{ $x };
}
return @ret;
}
sub SelectVectorDistinct { # ($table, \@fields, \@specs) => array
unshift @_, "all";
unshift @_, "distinct";
my @all = &Select;
my @ret;
foreach my $x (@all) {
push @ret, @{ $x };
}
return @ret;
}
sub Select { # ([DISTINCT], [FIRST|ALL], [HASH], $table, \@fields, \@specs, $limits) => array or arrayref
if (!defined($dbh)) { die "Database not open."; }
my $first = 0;
my $hash = 0;
my $distinct = "";
if (lc($_[0]) eq "distinct") { $distinct = "distinct"; shift; }
if (lc($_[0]) eq "first") { $first = 1; shift; }
if (lc($_[0]) eq "all") { $first = 0; shift; }
if (lc($_[0]) eq "hash") { $hash = 1; shift; }
my $dbname = shift;
my $fieldlist = shift;
my $speclist = shift;
my $limits = shift;
my @fieldarray = @{ $fieldlist };
my $fields = join(", ", @fieldarray);
my $specs = (defined($speclist) ? join(" and ", @{ $speclist }) : "");
if (!defined($limits)) { $limits = ''; }
if ($specs ne "") { $specs = "where $specs"; } else { $specs = ''; }
my $sql = "select $distinct $fields from $dbname $specs $limits";
if ($DEBUG_SQL) { warn $sql; }
my $sth = $dbh->prepare($sql);
$sth->execute();
if ($first) {
my @ret = $sth->fetchrow_array();
for my $v (@ret) { utf8::decode($v); } # set the UTF8 flag on the strings
$sth->finish();
if (!$hash) { return @ret; }
my %ret2;
for (my $i = 0; $i < scalar(@fieldarray); $i++) {
$ret2{$fieldarray[$i]} = $ret[$i];
}
return %ret2;
} else {
my @ret = @{ $sth->fetchall_arrayref() };
$sth->finish();
for my $r (@ret) {
for my $v (@$r) { utf8::decode($v); } # set the UTF8 flag on the strings
}
if (!$hash) { return @ret; }
my @ret2;
foreach my $r (@ret) {
my @ra = @{ $r };
my %rr;
for (my $i = 0; $i < scalar(@fieldarray); $i++) {
$rr{$fieldarray[$i]} = $ra[$i];
}
push @ret2, { %rr };
}
return @ret2;
}
}
sub Delete { # ($table, \@specs)
if (!defined($dbh)) { die "Database not open."; }
my $dbname = shift;
my $speclist = shift;
my $specs = "";
if ($speclist ne "all") {
$specs = join(" and ", @{ $speclist });
if ($specs ne "") { $specs = "where $specs"; }
else { die "No specs given to Delete"; }
}
my $sth = $dbh->prepare(qq{delete from $dbname $specs});
$sth->execute();
$sth->finish();
}
sub DeleteByID { # ($table, $id)
Delete($_[0], [SpecEQ('id', $_[1])]);
}
sub Insert { # (['LOW_PRIORITY', 'DELAYED', 'IGNORE'], $table, %values) => inserted id
my @opts;
while ($_[0] eq 'LOW PRIORITY' || $_[0] eq 'DELAYED' || $_[0] eq 'IGNORE') {
push @opts, $_[0]; shift;
}
my $table = shift;
return InsertUpdate('insert', $table, \@opts, [], @_);
}
sub Update { # (['LOW PRIORITY', 'IGNORE'], $table, \@specs, %values)
my @opts;
while ($_[0] eq 'LOW_PRIORITY' || $_[0] eq 'IGNORE') {
push @opts, $_[0]; shift;
}
my $table = shift;
my $specs = shift;
InsertUpdate('update', $table, \@opts, $specs, @_);
}
sub UpdateByID { # ($table, $id, %values)
my $table = shift;
my $id = shift;
return Update($table, [SpecEQ('id', $id)], @_);
}
sub InsertUpdate { # (insert/update, $table, \@opts, \@specs, %values) => inserted id
if (!defined($dbh)) { die "Database not open."; }
my $command = shift;
my $dbname = shift;
my $optlist = shift;
my $speclist = shift;
my %values = @_;
my @valuelist;
my $valuestr;
foreach my $k (keys(%values)) {
if (defined($values{$k})) {
my $v = $values{$k};
$v = Escape($v);
push @valuelist, "$k = '$v'";
} else {
push @valuelist, "$k = NULL";
}
}
$valuestr = join(", ", @valuelist);
my $opts = join(" ", @{ $optlist });
my $specs = join(" and ", @{ $speclist });
if ($specs ne "") { $specs = "where $specs"; }
my $cmd = qq{$command $opts $dbname set $valuestr $specs};
if ($DEBUG_SQL) { warn $cmd; }
my $sth = $dbh->prepare($cmd);
my $n = $sth->execute() or die "Row insertion had error: " . $DBI::errstr;
$sth->finish();
if ($command eq "update") { return $n; }
if ($n == 0) { return -1; }
return $dbh->{'mysql_insertid'};
}
sub Escape {
my $v = shift;
$v =~ s/\\/\\\\/g;
$v =~ s/'/\\'/g;
return $v;
}
sub SpecEQ { return Spec($_[0], '=', $_[1]); }
sub SpecNE { return SpecNot(SpecEQ(@_)); }
sub SpecLE { return Spec($_[0], '<=', $_[1]); }
sub SpecGE { return Spec($_[0], '>=', $_[1]); }
sub SpecLT { return Spec($_[0], '<', $_[1]); }
sub SpecGT { return Spec($_[0], '>', $_[1]); }
sub SpecContains { return Spec($_[0], 'like', '%' . Escape($_[1]) . '%', 1); }
sub SpecStartsWith { return Spec($_[0], 'like', Escape($_[1]) . '%', 1); }
sub SpecEndsWith { return Spec($_[0], 'like', '%' . Escape($_[1]), 1); }
sub Spec {
my $field = shift; my $cmp = shift; my $value = shift;
my $noescape = shift;
if (!$value) { $value = ''; }
if (!$noescape) { $value = Escape($value); }
return "$field $cmp '$value'";
}
sub SpecNot {
my $spec = shift;
return "not($spec)";
}
sub SpecOrNull {
my $field = shift; my $cmp = shift; my $value = shift;
return "(" . Spec($field, $cmp, $value) . " or $field IS NULL)";
}
sub SpecIn {
my $field = shift;
my @values = @_;
if (scalar(@values) == 0) { die "Cannot pass empty array to SpecIn."; }
foreach my $v (@values) { $v = Escape($v); }
my $j = join(", ", @values);
return "($field IN ($j))";
}
sub MakeDBDate {
my $date = shift;
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)
= localtime($date);
$year += 1900;
$mon++;
return sprintf("%04d%02d%02d%02d%02d%02d", $year, $mon, $mday, $hour, $min, $sec);
}
sub GetDBDate {
$_[0] =~ /(\d\d\d\d)-(\d\d)-(\d\d)( (\d\d):(\d\d):(\d\d))?/;
return timelocal($7,$6,$5,$3,$2-1,$1);
}
sub GetDBTimestamp {
if ($_[0] eq "00000000000000") { return undef; }
$_[0] =~ /(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)/;
return timelocal($6,$5,$4,$3,$2-1,$1);
}
sub SpecFromHash {
my @ret;
while (scalar(@_)) {
my $k = shift;
my $v = shift;
if (defined($v)) {
push @ret, SpecEQ($k, $v);
} else {
push @ret, "($k IS NULL)";
}
}
return @ret;
}