Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re: Re: How to override methods which use my() variables?

by Mur (Pilgrim)
on Nov 01, 2002 at 14:26 UTC ( #209722=note: print w/replies, xml ) Need Help??

in reply to Re: How to override methods which use my() variables?
in thread How to override methods which use my() variables?

Sure. DBIx::TextIndex ships with MySQL support (only); I'm trying to adapt it to work with PostgreSQL. However, the way the author implemented the database-specific support was to accept a parameter, "db => $dbname", and construct a partial file path, essentially via
$db = "DBIx/TextIndex/${db}.pm" require $db;
Thus, any db-specific code must reside in that file. In particular, it's intended that functions defined in that file return valid SQL statements as text for certain operations like CREATE TABLE, UPDATE, SELECT, etc.

The problem comes from the MySQL-centric code ... several of the internal operations involve putting a row in a table via "REPLACE INTO", which has no corresponding statement in most other RDBMS, including PostgreSQL. I really have to perform a DELETE and INSERT at that point, or a SELECT followed by an UPDATE (if successful) or an INSERT (if not). And I can't do the delete in the function that returns the SQL code, because the caller doesn't provide any info about parameters at that point; instead, it takes the SQL and executes it in a loop.

The code in TextIndex expects only a single SQL statement to be returned. Thus, I need to provide a function to override the TextIndex function, so that it gets both the DELETE statement and the INSERT statement, prepares both, and executes both in a loop.

The problem is that the function I'm overriding makes use of a lexical defined elsewhere in the package. At this point I think my only options are to rewrite the whole package or writing a new class that inherits from the old.
Jeff Boes
Database Engineer
Nexcerpt, Inc.
vox 269.226.9550 ext 24
fax 269.349.9076
...Nexcerpt...Connecting People With Expertise

Replies are listed 'Best First'.
Re^3: How to override methods which use my() variables?
by adrianh (Chancellor) on Nov 01, 2002 at 18:05 UTC

    Ahhh... understand now. You're right, the DB specific module is a bit hard to extend to anything apart from MySQL. Not even a proper factory class which is what I assumed. Eww...

    If you're feeling nice (and have the time) I'd patch the main module to cope with multiple SQL statements and send the patch to the module author along with your new and funky DBIx::TextIndex::pg extension.

    If you can't spare the time, drop a line to the author anyway so they know there's a problem (it's the only way it they get fixed :-)

    If you're into a evil hack type solution take a look at PadWalker, which can access lexical vars in calling subroutines.

    Alternatively I think you could subvert the intent of the DBIx::TextIndex::whatever methods and actually run the SQL there. You're passed $self so you have access to the database handle. This would probably be excessively evil tho :-)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://209722]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2017-01-21 02:48 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (180 votes). Check out past polls.