What is a database abstraction layer?

Discussion in 'MySQL' started by pachecus, Oct 20, 2008.

  1. #1
    What is a database abstraction layer? Why we are using it? What is the different between Database abstraction layer and Database specific coding?
     
    pachecus, Oct 20, 2008 IP
  2. nirajkum

    nirajkum Active Member

    Messages:
    815
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    58
    #2
    Abstraction means ... providing interface .. so database abstraction is like u r providing interface for query like select , insert,update ... but implementation may be differ in coding layer . Please correct me if I am wrong
     
    nirajkum, Oct 20, 2008 IP
  3. ranabra

    ranabra Peon

    Messages:
    125
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    it's an interface which unifies the communication between a computer application and databases.
    in essense it enables and reduces the amount of work needed to "communicate" and interfaces with the DB.
    hope I'm clear enough :)
     
    ranabra, Oct 27, 2008 IP
  4. Pos1tron

    Pos1tron Peon

    Messages:
    95
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Sorry to bring up a week+ old thread, but in case they (the author) is still around I'll give a good explanation:

    Moving away from technobabble that means little (aka, above posts), database abstraction is where you have something (a series of classes usually, ignore that if you don't understand it) to allow you to write database queries for one type of database - eg: MySQL - but then it'll work with no changes (other than an altered configuration setting obviously) on another type of database, eg: PostreSQL or SQLite.

    Might be clearer if I explain it the other way round as well: database-specific coding is where if you write it for MySQL, but if you can't use MySQL at some point in the future (for instance a new webhost not having it installed) you have to rewrite all of the application where it uses the database to work with another engine. Not with database abstraction though - with that you just change a setting.

    Aw darn, I'm still sprouting technobabble... sorry :(

    Ok, in database specific coding, you might write this (it's not very good code atall but it's just an example):
    <?php
    mysql_connect('host', 'user', 'pass');
    mysql_select_db('name');
    mysql_query('INSERT INTO `table` VALUES(`x`, `y`, `z`');
    ?>
    PHP:
    whereas in database abstraction (not a very good way to do it but good enough as example):
    <?php
    
    $database_engine = 'mysql';
    
    function abstract_connect($host, $user, $pass) {
    	global $database_engine;
    	
    	// not actually sure the below would work, treat as pseudocode
    	switch($database_engine) {
    		case 'mysql':
    			return mysql_connect($host, $user, $pass);
    		case 'postresql':
    			return postres-connect-function-here($host, $user, $pass);
    }
    
    // AND THEN MORE FUNCTIONS FOR THE OTHER THINGS LIKE abstract_query INSTEAD OF mysql_query (etc) HERE
    
    abstract_connect('host', 'user', 'pass');
    abstract_select_db('name');
    abstract_query('INSERT INTO `table` VALUES(`x`, `y`, `z`');
    
    ?>
    PHP:
    That code's junk, and you'd probably not ever do it like that, but it's a good example of what I mean.
     
    Pos1tron, Oct 30, 2008 IP
  5. penalty

    penalty Member

    Messages:
    36
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #5
    this as a short introduction: http://en.wikipedia.org/wiki/3_tier
     
    penalty, Oct 30, 2008 IP