0: =pod
1: So I'm cranking away on a database script, when I realize "OOPS,
2: I'm trying to make a query to a database that I can't connect to
3: yet" (dang difficulties with DBD::ODBC). Well, what I'm doing is
4: correct and all, but I obviously can't test it. The only way to
5: connect to the DB currently is to use a seperate ODBC program. I
6: didn't want to go rip out my DBI code and put in stuff to display
7: the queries since it was just temporary.
8:
9: So polymorphism to the rescue. :) And the FakeDB package was
10: born. It does one thing, it recognizes prepares, executes and
11: finishes (trivially). So you can prepare your query, and then
12: instead of executing it, dump those queries out to a file to be
13: imported elsewhere.
14:
15: You can use it with just:
16:
17: use FakeDB;
18: $dbh = "FakeDB"; #or whatever your database handle is
19:
20: and everything else works the same as before, it just now dumps
21: your queries to a file upon executions.
22:
23: Useful to you? Dunno, but it sure was to me. :)
24:
25:
26: =end
27:
28: package FakeDB;
29:
30: open QUERIES, ">./query.txt";
31:
32: sub prepare {
33: my ($class, $query) = @_;
34: return bless \$query, $class;
35: };
36:
37: sub execute {
38: my $self = shift;
39: my @values = @_;
40:
41: my $tpl = $$self;
42:
43: foreach (@values){
44: $tpl =~ s/\?/$_/;
45: };
46:
47: $tpl =~ s/\n//g;
48: $tpl =~ s/\s+/ /g;
49: print QUERIES "$tpl\n";
50: return 1;
51: };
52:
53: sub finish {return 1};
54:
55: 1; | For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |