/[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.8 - (hide annotations)
Thu Dec 12 02:48:58 2002 UTC (21 years, 10 months ago) by joko
Branch: MAIN
Changes since 1.7: +11 -3 lines
+ played with fixing disconnectTarget

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

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