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