in reply to Re: Abstracting sql
in thread Abstracting sql

Well, I'm all for being logical in my requests and highly structured code. Unfortunately I can't used stored procedures as maybe fifty percent of the databases out there implement them. As to your other solution, I really like the idea of using data functions to map from perl to sql. Unfortunately it looks like using your examples would give me the worst of both worlds, as I would still end up with sql being strewn around in the middle of my code and I see no easy way to easily be "database agnostic" with those subs, other then just writing one set of subs for every single database I want to use.

I suppose in the end I'm probably going to have to do something similar, with different modules containing functions for each different database, but the idea of having large numbers of subroutines that would essentially be exactly the same, for example, postgre subs and mysql subs would probably use almost the exact same sql and so forth, but I couldn't just use the same subroutines because of the one sub where I have to use different sql for whatever reason.

I suppose that writing individual classes for each database and combining it with some sort of factory method would be the simplest way to handle it, perhaps something like:
my $db_type = defined $use_mysql?'mysql':'postgre'; # really simplifie +d my $user = new User($db_type); $user->name; $user->date; $user->stuff;

Replies are listed 'Best First'.
Re: Re: Re: Abstracting sql
by mpeppler (Vicar) on Jul 31, 2003 at 14:40 UTC
    I really like the idea of using data functions to map from perl to sql. Unfortunately it looks like using your examples would give me the worst of both worlds, as I would still end up with sql being strewn around in the middle of my code and I see no easy way to easily be "database agnostic" with those subs, other then just writing one set of subs for every single database I want to use.
    Using these data functions you at least limit the SQL to a specific module (or set of modules), and this set of modules could quite easily be a hierarchy that takes database specificities into account.

    Another possibility is to store the SQL in a separate file (text file, DB_File or something else) indexed by logical request. Load the file at startup and map perl subroutines to SQL statements.

    There are always a bunch of ways to do these things... :-)

    Michael

      Heh, actually on further thought, I decided that a classic OOP method would be the best solution. I could define the basic/default "data access class" with all the fun little members like get_user_data; or whatever I decide I need, then every database specific implementation can simply inherit from the default class and I can easily over ride any method that needs "custom sql", or even override the entire class entirely (say If I wanted to use flat files or something..)