elegent way to handle "pluggable" backend servers?

From: Jon Lapham (lapham_at_jandr.org)
Date: 02/18/05


Date: Fri, 18 Feb 2005 16:37:16 -0200
To: dbi-users@perl.org

My DBI perl application needs to support many database servers and
versions, but will only use one database server per installation. IE:
one customer may be using MySQL v3.xx, while another has PgSQL v7.4,
while another has Oracle... etc. My application needs to work for them
all, with minimal fuss.

As an example of why this is necessary, MySQL v<4 does not support
boolean variables, and date handling between databases (and different
versions of the same database) are often different.

I would like to ask your opinion(s) on which approach to take for coding
this backend server "plugablility". It seems to me that there are a
number of approaches.

1) The first idea would be to simple wrap all SQL in some conditional in
the main body of the code, [or pseudocode in this case :) ]

if ($dbserver eq 'MySQL' and $dbversion == 3.23) {
   $SQL = 'SELECT * FROM blah WHERE active=1';
} elsif ($dbserver eq 'PgSQL' and $dbversion == 7.4) {
   $SQL = 'SELECT * FROM blah WHERE active';
} elsif (etc...) {
   ...
}
$result = $conn->prepare($SQL);
$result->execute();

Okay, this has the advantage that all the SQL for all the supported
backends sits easy to see in the main code body. Disadvantage is that
it is ugly as sin making your main body code harder to follow, imagine
if you wanted to support 20 database backend-version combinations?

2) But the SQL into a subroutine, held in an external module. One
module for each database-version combo you want to support. Then, your
main body code would look like:

$SQL = &db_some_query();
$result = $conn->prepare($SQL);
$result->execute();

...where the subroutine &db_some_query() exists in a bunch of modules
(ie: SQLpgsql_7_4.pm, SQLmysql_3_23.pm, SQLoracle_x_xx, etc) with only
the appropriate on loaded earier in the app.

The advantage here is that it is extensible, more database-versions
supported simply means creating more SQL* modules. The disadvantage is
that these modules could become easily out of sync... nightmare in terms
of maintainablity. Another disadvantage is that since the main body
code contains a subroutine call rather than the actual SQL code, it is
hard to see what is happening in the main body code, you would
continually need to refer to the appropriate module.

Any opinions? Is there another approach that I should think about? Has
anyone encountered this situation before?

Thanks!!!!
-Jon

-- 
-**-*-*---*-*---*-*---*-----*-*-----*---*-*---*-----*-----*-*-----*---
  Jon Lapham  <lapham@jandr.org>                Rio de Janeiro, Brasil
  Personal: http://www.jandr.org/
***-*--*----*-------*------------*--------------------*---------------


Relevant Pages

  • Re: sqlstring -- a library to build a SELECT statement
    ... > short-circuiting behaviors regarding them (the resultant bytecode will ... How to best add further sql function support? ... > database does not support a given function (or operator, ... > case-insensitive in MS SQL Server but case-sensitive in PostgreSQL). ...
    (comp.lang.python)
  • Re: What so special about PostgreSQL and other RDBMS?
    ... That's exactly the link the licence agreement for the database points to when it ... comes to what wecan expect for paying support. ... > "Oracle may provide additional releases or versions of its programs ... If the requirements are volatile I'd do a long term contract detailing what ...
    (comp.lang.php)
  • RE: How to relate a SQL based entity with an Object based entity in Entity Framework
    ... As for "mixing the SQL and Non-SQL. ... The ADO.NET entity framework and EDM mainly focus on the object mapping ... between code level and backend database source. ... support some class/objects mapping to non-database source, ...
    (microsoft.public.dotnet.framework.aspnet)
  • RE: Create new publication error
    ... This occurs when you have sql 2000 as publisher and 2005 ... This issue could occur if the user might be orphaned on the database you ... Microsoft Online Community Support ... where an initial response from the community or a Microsoft Support ...
    (microsoft.public.sqlserver.replication)
  • RE: Adding Full Text Search to SBS 2003 - Yes I have searched and Read Everything
    ... You receive a "The database this server is using does not support search" ... SQL Enterprise Manager. ...
    (microsoft.public.windows.server.sbs)

Quantcast