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