reasons of its existance. So why to use abstraction layers?
Answer is quite straightforward - to make complex things
simple.
Indeed when you are working with database (vendor is
irrelevant) tasks you perform are similar (at least if we are
talking about same db model, here relational one). You open
connection, manipulate data or db structure, close connection.
So by abstracting from actual database or vendor you can
write code which would handle all this tasks on any db.
What I present below is not a *real* db abstraction layer but
merely db wrapper ie I wrapped mysql functions with my own generic
ones so that if one day I would have to make my code work with
PostgreSQL, for example, I would have to make changes
in one file only (wrapper file).
So here is the code:
Note that several constants must be declared.
mysql_db.inc
<?
/*
+------------------------------------------------------------------------------+
| Authors: Victor Farazdagi (Kuulest) |
+------------------------------------------------------------------------------+
*/
/**
*MySQL wrapper functions:
*/
function db_connect() {
global $db_link;
if (isset($db_link)) return $db_link;
if (USE_PCONNECT) @$db_link = mysql_pconnect(DB_HOST, DB_LOGIN, DB_PASS);
else @$db_link = mysql_connect(DB_SERVER, DB_SERVER_USERNAME, DB_SERVER_PASSWORD);
if ($db_link) @mysql_select_db(DB_DATABASE);
return $db_link;
}
function db_close() {
global $db_link;
$result = mysql_close($db_link);
return $result;
}
function db_error() {
global $db_link;
return mysql_error();
}
function db_query($db_query) {
global $db_link;
$result = mysql_query($db_query, $db_link);
return $result;
}
function db_fetch_array($db_query) {
$result = mysql_fetch_array($db_query);
return $result;
}
function db_fetch_row($db_query) {
$result = mysql_fetch_row($db_query);
return $result;
}
function db_num_rows($db_query) {
$result = mysql_num_rows($db_query);
return $result;
}
function db_data_seek($db_query, $row_number) {
$result = mysql_data_seek($db_query, $row_number);
return $result;
}
function db_insert_id() {
$result = mysql_insert_id();
return $result;
}
function db_free_result($db_query) {
$result = mysql_free_result($db_query);
return $result;
}
?>
So, since I use db_connect() instead of mysql_connect(),
for example, I would be able to connect to another
kind of db simply by changing code in my wrapper.
Therefore, shifting from one db to another will be merely
reduced to including different wrappers for different
databases(mysql_db.inc for MySQL, postgre_db.inc for
PostgreSQL etc)
Hope this gives you an idea,
Any comments are welcome,
Yours Kuulest













