2 |
## $Id$ |
## $Id$ |
3 |
## --------------------------------------------------------------------------- |
## --------------------------------------------------------------------------- |
4 |
## $Log$ |
## $Log$ |
5 |
|
## Revision 1.3 2003/04/11 01:06:44 joko |
6 |
|
## enhanced hash2sql: |
7 |
|
## + revamped crud action tokenizer |
8 |
|
## + introduced SQL_DELETE |
9 |
|
## |
10 |
## Revision 1.2 2003/04/09 07:51:11 joko |
## Revision 1.2 2003/04/09 07:51:11 joko |
11 |
## renamed from db.pm |
## renamed from db.pm |
12 |
## |
## |
27 |
|
|
28 |
|
|
29 |
|
|
30 |
package shortcuts::db; |
package shortcuts::database; |
31 |
|
|
32 |
use strict; |
use strict; |
33 |
use warnings; |
use warnings; |
45 |
use constant SQL_INSERT => 10; |
use constant SQL_INSERT => 10; |
46 |
use constant SQL_UPDATE => 11; |
use constant SQL_UPDATE => 11; |
47 |
use constant SQL_SELECT => 12; |
use constant SQL_SELECT => 12; |
48 |
|
use constant SQL_DELETE => 13; |
49 |
|
|
50 |
|
|
51 |
use Data::Dumper; |
use Data::Dumper; |
54 |
# TODO: handle usage of "$crit" in an abstract way somehow |
# TODO: handle usage of "$crit" in an abstract way somehow |
55 |
sub hash2sql { |
sub hash2sql { |
56 |
|
|
57 |
|
# the arguments |
58 |
my $table = shift; |
my $table = shift; |
59 |
my $hash = shift; |
my $hash = shift; |
60 |
my $mode = shift; |
my $mode = shift; |
61 |
my $crit = shift; |
my $crit = shift; |
62 |
|
|
63 |
|
# our result |
64 |
my $sql; |
my $sql; |
65 |
$mode = SQL_SELECT if ($mode eq 'SQL_SELECT' || $mode eq 'SELECT'); |
|
66 |
$mode = SQL_INSERT if ($mode eq 'SQL_INSERT' || $mode eq 'INSERT'); |
# declare commands and associated tokens |
67 |
$mode = SQL_UPDATE if ($mode eq 'SQL_UPDATE' || $mode eq 'UPDATE'); |
my @command = qw( SELECT INSERT UPDATE DELETE ); |
68 |
|
my @token = ( SQL_SELECT, SQL_INSERT, SQL_UPDATE, SQL_DELETE ); |
69 |
|
|
70 |
|
# translate stringified mode to token |
71 |
|
my $c = 0; |
72 |
|
foreach (@command) { |
73 |
|
$mode = $token[$c] if ($mode eq 'SQL_' . $_ || $mode eq $_); |
74 |
|
$c++; |
75 |
|
} |
76 |
|
|
77 |
|
# pre-flight check: has mode been resolved into token? |
78 |
|
if ($mode !~ m/\d+/) { |
79 |
|
print __PACKAGE__ . "::hash2sql: no mode, no token, no way!", "\n"; |
80 |
|
return; |
81 |
|
} |
82 |
|
|
83 |
|
# dispatch mode |
84 |
if ($mode == SQL_SELECT) { |
if ($mode == SQL_SELECT) { |
85 |
$sql = "SELECT #fields# FROM $table"; |
$sql = "SELECT #fields# FROM $table"; |
86 |
} elsif ($mode == SQL_INSERT) { |
} elsif ($mode == SQL_INSERT) { |
87 |
$sql = "INSERT INTO $table (#fields#) VALUES (#values#)"; |
$sql = "INSERT INTO $table (#fields#) VALUES (#values#)"; |
88 |
} elsif ($mode == SQL_UPDATE) { |
} elsif ($mode == SQL_UPDATE) { |
89 |
$sql = "UPDATE $table SET #fields-values#"; |
$sql = "UPDATE $table SET #fields-values#"; |
90 |
|
} elsif ($mode == SQL_DELETE) { |
91 |
|
if (!$crit) { |
92 |
|
print __PACKAGE__ . "::hash2sql: Criteria required for mode 'SQL_DELETE' (\$crit must not be empty!).", "\n"; |
93 |
|
return; |
94 |
|
} |
95 |
|
$sql = "DELETE FROM $table"; |
96 |
} |
} |
97 |
|
|
98 |
# FIXME: this wouldn't be valid for 'INSERT' queries |
# apply filter? |
99 |
if ($crit) { |
if ($mode != SQL_INSERT && $crit) { |
100 |
$sql .= " WHERE $crit"; |
$sql .= " WHERE $crit" if $sql; |
101 |
} |
} |
102 |
|
|
103 |
my (@fields, @values); |
my (@fields, @values); |
126 |
$sql =~ s/#values#/$values/; |
$sql =~ s/#values#/$values/; |
127 |
$sql =~ s/#fields-values#/$fields_values/; |
$sql =~ s/#fields-values#/$fields_values/; |
128 |
|
|
129 |
print "shortcuts::db::hash2sql: \$sql=$sql", "\n"; |
print __PACKAGE__ . "::hash2sql: \$sql=$sql", "\n"; |
130 |
|
|
131 |
return $sql; |
return $sql; |
132 |
} |
} |