/[cvs]/nfo/perl/libs/libdb.pm
ViewVC logotype

Annotation of /nfo/perl/libs/libdb.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.6 - (hide annotations)
Fri Nov 29 04:53:39 2002 UTC (21 years, 11 months ago) by joko
Branch: MAIN
Changes since 1.5: +22 -10 lines
+ hash2Sql now knows about SQL_SELECT

1 cvsjoko 1.1 ## --------------------------------------------------------------------------------
2 joko 1.6 ## $Id: libdb.pm,v 1.5 2002/11/17 07:18:38 joko Exp $
3 cvsjoko 1.1 ## --------------------------------------------------------------------------------
4 cvsjoko 1.2 ## $Log: libdb.pm,v $
5 joko 1.6 ## Revision 1.5 2002/11/17 07:18:38 joko
6     ## + small modification in hash2sql
7     ##
8 joko 1.5 ## Revision 1.4 2002/10/16 22:36:42 joko
9     ## + sub testDbServer
10     ##
11 joko 1.4 ## Revision 1.3 2002/07/27 00:28:20 cvsjoko
12     ## bugfixes
13     ##
14 cvsjoko 1.3 ## Revision 1.2 2002/07/20 11:09:58 cvsjoko
15     ## + bugfixes
16     ## + dont' print sql-errors
17     ##
18 cvsjoko 1.2 ## Revision 1.1 2002/07/19 18:14:03 cvsjoko
19     ## no message
20     ##
21 cvsjoko 1.1 ##
22     ## --------------------------------------------------------------------------------
23    
24     package libdb;
25    
26     use strict;
27     use warnings;
28    
29     require Exporter;
30     our @ISA = qw( Exporter );
31 joko 1.6 our @EXPORT_OK = qw(
32 cvsjoko 1.1 testDsn hash2Sql
33     SQL_INSERT SQL_UPDATE
34     connectTarget disconnectTarget sendSql
35     dbNow
36     getDbNameByDsn sqlDbAction createSqlDb dropSqlDb
37     quotesql
38 joko 1.4 testDsnForTables testDbServer
39 cvsjoko 1.1 );
40    
41 joko 1.6
42     use libp qw( croak );
43     use DBI;
44    
45 cvsjoko 1.1 use constant SQL_INSERT => 10;
46     use constant SQL_UPDATE => 11;
47 joko 1.6 use constant SQL_SELECT => 12;
48 cvsjoko 1.1
49     my $dbmeta_ref_cache;
50    
51     sub testDsn {
52     my $dsn = shift;
53     my $result;
54     if ( my $dbh = DBI->connect($dsn, '', '', {
55     PrintError => 0,
56     } ) ) {
57     $dbh->disconnect();
58     return 1;
59     }
60     }
61    
62 joko 1.4 sub testDbServer {
63     my $dsn = shift;
64     $dsn =~ s/database=(\w+)//;
65    
66     #print "testDbServer: $dsn", "\n";
67    
68     my $result;
69     if ( my $dbh = DBI->connect($dsn, '', '', {
70     PrintError => 0,
71     } ) ) {
72     $dbh->disconnect();
73     return 1;
74     }
75     }
76    
77 joko 1.6
78     # TODO: handle usage of "$crit" in an abstract way somehow
79 cvsjoko 1.1 sub hash2Sql {
80    
81     my $table = shift;
82     my $hash = shift;
83     my $mode = shift;
84     my $crit = shift;
85    
86     my $sql;
87 joko 1.6 $mode = SQL_SELECT if ($mode eq 'SQL_SELECT' || $mode eq 'SELECT');
88     $mode = SQL_INSERT if ($mode eq 'SQL_INSERT' || $mode eq 'INSERT');
89     $mode = SQL_UPDATE if ($mode eq 'SQL_UPDATE' || $mode eq 'UPDATE');
90    
91     if ($mode == SQL_SELECT) {
92     $sql = "SELECT #fields# FROM $table WHERE $crit";
93     } elsif ($mode == SQL_INSERT) {
94     $sql = "INSERT INTO $table (#fields#) VALUES (#values#)";
95     } elsif ($mode == SQL_UPDATE) {
96     $sql = "UPDATE $table SET #fields-values# WHERE $crit";
97 cvsjoko 1.1 }
98    
99     my (@fields, @values);
100     foreach my $key (keys %{$hash}) {
101     push @fields, $key;
102     push @values, $hash->{$key};
103     }
104     # quote each element
105     map { if (defined $_) { $_ = "'$_'" } else { $_ = "null" } } @values;
106    
107     my $fields = join(', ', @fields);
108     my $values = join(', ', @values);
109     my $fields_values = '';
110     my $fc = 0;
111     foreach (@fields) {
112     $fields_values .= $_ . '=' . $values[$fc] . ', ';
113     $fc++;
114     }
115     $fields_values = substr($fields_values, 0, -2);
116    
117     $sql =~ s/#fields#/$fields/;
118     $sql =~ s/#values#/$values/;
119     $sql =~ s/#fields-values#/$fields_values/;
120    
121     return $sql;
122     }
123    
124     sub patch_dbmeta {
125    
126     my $dbmeta = shift;
127    
128     if ($dbmeta =~ m/^dbi:/) {
129     $dbmeta = {
130     dsn => $dbmeta,
131     trace_level => 0,
132     trace_file => undef,
133     };
134     }
135    
136     return $dbmeta;
137    
138     }
139    
140     sub connectTarget {
141     my $dbmeta = shift;
142     croak "please supply a dsn or a \"dbmeta\"-hash" if (!$dbmeta);
143     $dbmeta = patch_dbmeta($dbmeta);
144     if (!$dbmeta->{connected}) {
145 cvsjoko 1.2 if ($dbmeta->{dbh} = DBI->connect($dbmeta->{dsn}, '', '', { PrintError => 0 } )) {
146 cvsjoko 1.1 $dbmeta->{dbh}->trace($dbmeta->{trace_level}, $dbmeta->{trace_file});
147     $dbmeta->{dbh}->{PrintError} = 0;
148     $dbmeta->{connected} = 1;
149     $dbmeta_ref_cache = $dbmeta;
150 cvsjoko 1.2 return 1;
151 cvsjoko 1.1 }
152     }
153     }
154    
155     sub disconnectTarget {
156     #my $dbmeta = shift;
157     #croak "please supply a \"dbmeta\"-hash" if (!$dbmeta);
158     #$dbmeta = patch_dbmeta($dbmeta);
159     #$dbmeta->{dbh} && $dbmeta->{dbh}->disconnect();
160     my $dbmeta = $dbmeta_ref_cache;
161     $dbmeta->{dbh} && $dbmeta->{dbh}->disconnect();
162     undef($dbmeta_ref_cache);
163     }
164    
165     sub sendSql {
166     my $sql = shift;
167     my $dbmeta_ref = shift;
168     if (!$dbmeta_ref) {
169     $dbmeta_ref = $dbmeta_ref_cache;
170     }
171     #print "sql: $sql", "\n";
172     if (!$dbmeta_ref->{connected}) {
173     print "not connected!", "\n";
174     return 0;
175     }
176     if (my $result = $dbmeta_ref->{dbh}->prepare($sql)) {
177 joko 1.4 #if ($result->execute()) {
178     $result->execute();
179 cvsjoko 1.1 return $result;
180 joko 1.4 #}
181 cvsjoko 1.1 }
182     }
183    
184     sub dbNow {
185     #my $now_string = strftime "%a %b %e %H:%M:%S %Y", localtime;
186     my $now_string = strftime("%Y-%m-%d %H:%M:%S", localtime);
187     return $now_string;
188     }
189    
190    
191     sub getDbNameByDsn {
192     my $dsn = shift;
193     $dsn =~ m/database=(.+?);/;
194     my $database_name = $1;
195     return $database_name;
196     }
197    
198     sub sqlDbAction {
199     my $dsn = shift;
200     $dsn =~ s/database=.+?;//;
201     my $sql = shift;
202     my $dbmeta = {
203     dsn => $dsn,
204     trace_level => 1,
205     trace_file => 'dbitrace',
206     };
207 cvsjoko 1.2 my $bool_ok;
208     if (connectTarget($dbmeta)) {
209     sendSql($sql);
210 cvsjoko 1.3 #print "state: ", $dbmeta->{dbh}->state, "\n";
211     #print "err ", $dbmeta->{dbh}->err, "\n";
212     #$bool_ok = ($dbmeta->{dbh} && $dbmeta->{dbh}->state && !$dbmeta->{dbh}->err);
213     $bool_ok = ($dbmeta->{dbh} && !$dbmeta->{dbh}->err);
214 cvsjoko 1.2 disconnectTarget($dbmeta);
215     }
216     return $bool_ok;
217 cvsjoko 1.1 }
218    
219     sub createSqlDb {
220     my $dsn = shift;
221     my $dbname = getDbNameByDsn($dsn);
222 cvsjoko 1.2 print " - creating rdbms-database $dbname ($dsn) ...";
223 cvsjoko 1.1 my $sql;
224     $sql = "CREATE DATABASE $dbname;";
225 cvsjoko 1.2 if ( sqlDbAction($dsn, $sql) ) {
226 cvsjoko 1.3 print "ok", "\n";
227     return 1;
228 cvsjoko 1.2 } else {
229 cvsjoko 1.3 print "failed", "\n";
230     return 0;
231 cvsjoko 1.2 }
232 cvsjoko 1.1 }
233 cvsjoko 1.2
234 cvsjoko 1.1 sub dropSqlDb {
235     my $dsn = shift;
236     my $dbname = getDbNameByDsn($dsn);
237     print " - dropping database $dbname", "\n";
238     my $sql;
239     $sql = "DROP DATABASE $dbname;";
240     sqlDbAction($dsn, $sql);
241     }
242    
243     sub quotesql {
244     my $string = shift;
245 joko 1.4 if ($string) {
246     $string =~ s/'/\\'/g;
247     }
248 cvsjoko 1.1 return $string;
249     }
250    
251     sub testDsnForTables {
252     my $dsn = shift;
253     connectTarget($dsn);
254     my $result = sendSql('SHOW TABLES;');
255     my $bool_tablesHere = $result->fetchrow_hashref();
256     $result->finish();
257     disconnectTarget($dsn);
258     return 1 if ($bool_tablesHere);
259     }
260    
261 cvsjoko 1.3 1;

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed