1 |
## --------------------------------------------------------------------------- |
2 |
## $Id: files.pm,v 1.3 2003/03/31 05:47:01 janosch Exp $ |
3 |
## --------------------------------------------------------------------------- |
4 |
## $Log: files.pm,v $ |
5 |
## --------------------------------------------------------------------------- |
6 |
|
7 |
=pod |
8 |
|
9 |
=head1 Background |
10 |
|
11 |
Databases = Everything is sql |
12 |
Perl ~ Everything is a hash ;-) |
13 |
|
14 |
|
15 |
=cut |
16 |
|
17 |
|
18 |
|
19 |
package shortcuts::db; |
20 |
|
21 |
use strict; |
22 |
use warnings; |
23 |
|
24 |
require Exporter; |
25 |
our @ISA = qw( Exporter ); |
26 |
our @EXPORT_OK = qw( |
27 |
SQL_INSERT SQL_UPDATE SQL_SELECT |
28 |
hash2sql |
29 |
quotesql |
30 |
dsn2dbname |
31 |
); |
32 |
|
33 |
|
34 |
use constant SQL_INSERT => 10; |
35 |
use constant SQL_UPDATE => 11; |
36 |
use constant SQL_SELECT => 12; |
37 |
|
38 |
|
39 |
use Data::Dumper; |
40 |
|
41 |
|
42 |
# TODO: handle usage of "$crit" in an abstract way somehow |
43 |
sub hash2sql { |
44 |
|
45 |
my $table = shift; |
46 |
my $hash = shift; |
47 |
my $mode = shift; |
48 |
my $crit = shift; |
49 |
|
50 |
my $sql; |
51 |
$mode = SQL_SELECT if ($mode eq 'SQL_SELECT' || $mode eq 'SELECT'); |
52 |
$mode = SQL_INSERT if ($mode eq 'SQL_INSERT' || $mode eq 'INSERT'); |
53 |
$mode = SQL_UPDATE if ($mode eq 'SQL_UPDATE' || $mode eq 'UPDATE'); |
54 |
|
55 |
if ($mode == SQL_SELECT) { |
56 |
$sql = "SELECT #fields# FROM $table"; |
57 |
} elsif ($mode == SQL_INSERT) { |
58 |
$sql = "INSERT INTO $table (#fields#) VALUES (#values#)"; |
59 |
} elsif ($mode == SQL_UPDATE) { |
60 |
$sql = "UPDATE $table SET #fields-values#"; |
61 |
} |
62 |
|
63 |
# FIXME: this wouldn't be valid for 'INSERT' queries |
64 |
if ($crit) { |
65 |
$sql .= " WHERE $crit"; |
66 |
} |
67 |
|
68 |
my (@fields, @values); |
69 |
foreach my $key (keys %{$hash}) { |
70 |
push @fields, $key; |
71 |
push @values, $hash->{$key}; |
72 |
} |
73 |
# quote each element |
74 |
map { if (defined $_) { $_ = "'$_'" } else { $_ = "null" } } @values; |
75 |
|
76 |
my $fields = join(', ', @fields); |
77 |
my $values = join(', ', @values); |
78 |
my $fields_values = ''; |
79 |
my $fc = 0; |
80 |
foreach (@fields) { |
81 |
$fields_values .= $_ . '=' . $values[$fc] . ', '; |
82 |
$fc++; |
83 |
} |
84 |
$fields_values = substr($fields_values, 0, -2); |
85 |
|
86 |
# FIXME: this should only be valid for 'SELECT' queries |
87 |
$fields ||= '*'; |
88 |
|
89 |
$sql =~ s/#fields#/$fields/; |
90 |
$sql =~ s/#values#/$values/; |
91 |
$sql =~ s/#fields-values#/$fields_values/; |
92 |
|
93 |
print "shortcuts::db::hash2sql: \$sql=$sql", "\n"; |
94 |
|
95 |
return $sql; |
96 |
} |
97 |
|
98 |
|
99 |
sub quotesql { |
100 |
my $string = shift; |
101 |
if ($string) { |
102 |
$string =~ s/'/\\'/g; |
103 |
} |
104 |
return $string; |
105 |
} |
106 |
|
107 |
sub dsn2dbname { |
108 |
my $dsn = shift; |
109 |
$dsn =~ m/database=(.+?);/; |
110 |
my $database_name = $1; |
111 |
return $database_name; |
112 |
} |
113 |
|
114 |
1; |
115 |
__END__ |