Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Fake DBI

by jimt (Chaplain)
on Sep 21, 2000 at 01:49 UTC ( #33408=perlcraft: print w/replies, xml ) Need Help??

   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
    Any reason this can't be taken one step further and built into, say, a DBD::Fake module? That would let us use precisely the same DBI setup, except instead of identifying the database as "ODBC" or something, identify it as "Fake". A minimal impact on code (for testing) with 100% integration with DBI. What do you think?
RE: Fake DBI
by japhy (Canon) on Sep 21, 2000 at 18:13 UTC
    I've been working on a module called Test::Stub, which would stub specific functions from a module, for this SPECIFIC purpose -- pretending to call them without ACTUALLY calling them. This is a perfect example.

    $_="goto+F.print+chop;\n=yhpaj";F1:eval
      I haven't got as far as thinking about a modue, I'm just doing it by hand for each method I want to override. If you finish the module perhaps you could post it, I'd like to have a look.

      Nuance

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlcraft [id://33408]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2022-05-24 15:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (84 votes). Check out past polls.

    Notices?