ASE Labs
Welcome Guest. Please register or log in now. There are 704 people online (0 Friends).
  • Home
  • Articles
  • News
  • Forum
  • Register/Login

Abstraction Layers And Their Importance

Author
Aron Schatz
Posted
January 22, 2006
Views
56981
Abstraction Layers And Their Importance
Abstraction layers are the foundation of all programming. Hiding the complex and maintaining portability are the prime reasons of using abstraction layers. An abstraction layer for database functions using PHP is given in this article. Read more...
Tags Software

Page 1: DBAL

<b>Intro</b>:

Abstraction layers are one of the most important concepts in computing and programming today. The idea of an abstraction layer is to provide a standard interface to lower level functions to the higher level function. Examples of this are present in nearly every single computer you use today.

<b>An Example</b>:

A simple example is the 7 layer OSI model. Remember from top to bottom... Application, Presentation, Session, Transport, Network, Data-Link, and Physical. Now that you remember something from networking class, remember the hybrid 5 layer design for TCP/IP? It melds the presentation and session into the application layer. In TCP/IP the data-link layer is an abstraction layer. It hides the underlying physical connections. This is why TCP/IP works on twisted pair, fiber, and coax...

<b>Importance</b>:

The concept of the abstraction layer has surface in many different forms. The term 'middleware' is a form of an abstraction layer. DirectX and OpenGL are also abstraction layers. HAL (Hardware Abstraction Layer) in Windows... yes. Even EVERY programming language is an abstraction layer!

<b>Concept</b>:

We already can deduce that the purpose of an abstraction layer is to hide the underlying layers so that the higher level functions can be written once and be portable. For the article, I'll be focusing on PHP and the database support. PHP5 (and PEAR) introduced a database abstraction layer, but I always like to write my own functions.

<b>The Problem</b>:

Here is a good example. You want to create a PHP program that interacts with a database and display some type of information. How do you program the database calls? Many scripts I have seen actually use direct MySQL calls. What happens if the server can only use PostgreSQL? A problem, that's what. Even if you are going to support one database, it makes it so much easier to use an abstraction layer to provide the functionality you need without the extra stuff.

<b>The Solution</b>:

Here is my code example for your enjoyment. You are free to use and modify this code, but leave the Copyright in it Wink.

Code

<?PHP
/*
/////////////////////////////////////////////////
Simple DBAL, modified from ASE (www.ase.cc) (www.aselabs.com)
Copyright 2006 Aron Schatz
There is no warranty offered with this script.
You may use and edit this script but this copywrite notice must remain.
/////////////////////////////////////////////////
*/

class simpledbal
{
     var $myconid=FALSE;
     var $myqueryid=FALSE;
     var $myquerycount = 0;
     var $myrowcount = 0;
     var $mytime=0;
     var $myrow=array();
     var $myrows=array();
     var $myuser='';
     var $mypass='';
     var $myserver='';
     var $mydb='';
     var $myqueries=array();
     
     function simpledbal($server, $user, $pass, $db)
     {
          //Constructor
          $this->myuser = $user;
          $this->mypass = $pass;
          $this->myserver = $server;
          $this->myconid=mysql_connect($server,$user,$pass);
          if($this->myconid)
          {
               if(!empty($db))
               {
                    $this->mydb=$db;
                    $dbselect = @mysql_select_db($this->mydb);
                    if(!$dbselect)
                    {
                         @mysql_close($this->myconid);
                         return 0;
                    }
                    return $this->myconid;
               }
               
          }
          return 0;
     }
     
     
     function query($string)
     {
          if(empty($string))
          {
               return 0;
          }
         
          $this->myquerycount++;
          $this->myqueryid=FALSE;
          $mtime = microtime(); //Query Times
          $mtime = explode(" ",$mtime);
          $mtime = $mtime[1] + $mtime[0];
          $starttime = $mtime;
          $this->myquerycount++;
          $this->myqueryid=@mysql_query($string,$this->myconid);
          if(!$this->myqueryid)
          {
               $mtime = microtime();
               $mtime = explode(" ",$mtime);
               $mtime = $mtime[1] + $mtime[0];
               $endtime = $mtime;
               $totaltime = ($endtime - $starttime);
               $a=array();
               $a['query']=stripslashes($string);
               $a['time']=$totaltime;
               $a['failed']=1;
               $this->myqueries[]=$a;
               return(0);
          }
               
          $mtime = microtime();
          $mtime = explode(" ",$mtime);
          $mtime = $mtime[1] + $mtime[0];
          $endtime = $mtime;
          $totaltime = ($endtime - $starttime);
          $a=array();
          $a['query']=stripslashes($string);
          $a['time']=$totaltime;
          $a['failed']=0;
          $this->myqueries[]=$a;
          return ($this->myqueryid);
     }
     
     
     function dbclose()
     {
          @mysql_close($this->myconid);
     }
         
     function fetch($resourceid=0)
     {
         
          if(!$resourceid)
          {
               $resourceid=$this->myqueryid;         
          }
          $row=@mysql_fetch_assoc($resourceid);                   
          $this->myrowcount++;
          return ($row);
     }
     
     function getrow($resourceid=0) //Alias fetch
     {
          return $this->fetch($resourceid);
     }
     
     function getallrows($resourceid=0)
     {
          if(!$resourceid)
          {
               $resourceid=$this->myqueryid;
          }
          if($resourceid)
          {
               unset($this->myrow[$resourceid]);
               while($row1=$this->fetch($resourceid))
               {
                    $ret[]=$row1;
               }
               return $ret;
          }
          return 0;
     }
     
     function freeresource($resourceid=0)
     {
          if(!$resourceid)
          {
               $resourceid=$this->myqueryid;
          }
                   
          if($resourceid)
          {
               @mysql_free_result($resourceid);
               return (1);
          }
          return (0);
     }
     
     function insertid()
     {
          if($this->myconid)
          {
               return(@mysql_insert_id($this->myconid));
          }
          return 0;
     }
     
     /////////// Special Functions
     function getarray($table,$clause,$order='')
     {
          if(@is_numeric($clause))
          {
               //id
               $q=$this->query('SELECT * FROM '.$table.' WHERE id='.$clause.' LIMIT 1');
               $row=$this->getrow($q);
               $row=(($row) ? $row : 0);
               $this->freeresource($q);
               return ($row);
          }
          else
          {
               if(!empty($order))
               {
                    $order='ORDER BY '.$order;
               }
               $q=$this->query('SELECT * FROM '.$table.' WHERE '.$clause.' '.$order.' LIMIT 1');
               $row=$this->getrow($q);
               $row=(($row) ? $row : 0);
               $this->freeresource($q);
               return ($row);
          }
     }

     function getquery($query)
     {
          $q=$this->query($query);
          $row=$this->getrow($q);
          $row=(($row) ? $row : 0);
          $this->freeresource($q);
          return($row);
         
     }

     function getcount($table,$clause='')
     {
          if(empty($clause))
          {
               $q=$this->query('SELECT count(*) as count FROM '.$table);
               $row=$this->getrow($q);
               $ret=(($row) ? $row['count'] : 0);
               $this->freeresource($q);
               return $ret;
          }
          else
          {
               $q=$this->query('SELECT count(*) as count FROM '.$table. ' WHERE '.$clause);
               $row=$this->getrow($q);
               $ret=(($row) ? $row['count'] : 0);
               $this->freeresource($q);
               return $ret;
          }
     }
}
?>


This is a modified abstraction layer that I use for my sites. I have more complex needs such as caching queries and such. This simple dbal script is a perfect starter to make a better script or it is fit to use by itself. With the use of abstraction layers, you are providing portability for your code. While this abstraction layer uses MySQL, you could easily make one that supports other databases and keep the same function names.

<b>Conclusion</b>:

Abstraction layers provide needed simplicity and portability in an ever changing and evolving world. If there were no such thing as an abstraction layer, you would need to be wiring CPUs with code instead of writing a PHP program or C++ program. Think of how much productivity you save by investing a bit more time up front. Careful planning and requirement specifications are a must for programming. Good habits is a plus and the abstraction layers that you make help to ensure that your code will stay portable.
 
Page 1
View As Single Page Print This Page Print Entire Article
Related Articles
  • KDE4: How It Looks
  • BackupPC (Open Source)
  • Open Source Alternatives
  • Feisty And Like A Fawn: Ubuntu 7.04
  • Switch To Open Source Today

Title

Medium Image View Large
Login
Welcome Guest. Please register or log in now.
Forgot your password?
Navigation
  • Home
  • Articles
  • News
  • Register/Login
  • Shopping
  • ASE Forums
  • Anime Threads
  • HardwareLogic
  • ASE Adnet
Latest News
  • Kingston HyperX Cloud 2 Pro Gaming Headset Unboxing
  • Synology DS415+ Unboxing
  • D-Link DCS-5020L Wireless IP Pan/Tilt IP Camera
  • Actiontec WiFi Powerline Network Extender Kit Unboxing
  • Durovis Dive Unboxing
  • Bass Egg Verb Unboxing
  • Welcome to the new server
  • Gmail Gets Optional Preview Pane
  • HBO Go on Consoles
  • HP Touchpad Update
Latest Articles
  • D-Link Exo AC2600 Smart Mesh Wi-Fi Router DIR-2660-US
  • HyperX Double Shot PBT Keys
  • Avantree ANC032 Wireless Active Noise Cancelling Headphones
  • ScharkSpark Beginner Drones
  • HyperX Alloy FPS RGB Mechanical Gaming Keyboard
  • D-Link DCS-8300LH Full HD 2-Way Audio Camera
  • Contour Unimouse Wireless Ergonomic Mouse
  • HyperX Cloud Alpha Pro Gaming Headset
  • Linksys Wemo Smart Home Suite
  • Fully Jarvis Adjustable Standing Desk
Latest Topics
  • Hello
  • Welcome to the new server at ASE Labs
  • Evercool Royal NP-901 Notebook Cooler at ASE Labs
  • HyperX Double Shot PBT Keys at ASE Labs
  • Avantree ANC032 Wireless Active Noise Cancelling Headphones at ASE Labs
  • ScharkSpark Beginner Drones at ASE Labs
  • HyperX Alloy FPS RGB Mechanical Gaming Keyboard at ASE Labs
  • D-Link DCS-8300LH Full HD 2-Way Audio Camera at ASE Labs
  • Kingston SDX10V/128GB SDXC Memory at ASE Labs
  • What are you listening to now?
  • Antec Six Hundred v2 Gaming Case at HardwareLogic
  • Sans Digital TR5UTP 5-Bay RAID Tower at HardwareLogic
  • Crucial Ballistix Smart Tracer 6GB PC3-12800 BL3KIT25664ST1608OB at HardwareLogic
  • Cooler Master Storm Enforcer Mid-Tower Gaming Case at HardwareLogic
  • Arctic M571-L Gaming Laser Mouse at ASE Labs
  • Contour Unimouse Wireless Ergonomic Mouse at ASE Labs
Press Release
  • Huntkey Has Launched Its New Power Strips with USB Chargers on Amazon US
  • Inspur Releases TensorFlow-Supported FPGA Compute Acceleration Engine TF2
  • Hot Pepper Introduces Spicy New Smartphones in US Markets
  • Sharp Introduces New Desktop Printers For The Advanced Office
  • DJI Introduces Mavic 2 Pro And Mavic 2 Zoom: A New Era For Camera Drones
  • DJI Introduces Mavic 2 Pro And Mavic 2 Zoom: A New Era For Camera Drones
  • Fujifilm launches "instax SQUARE SQ6 Taylor Swift Edition", designed by instax global partner Taylor Swift
  • Huawei nova 3 With Best-in-class AI Capabilities Goes on Sale Today
  • Rand McNally Introduces Its Most Advanced Dashboard Camera
  • =?UTF-8?Q?My_Size_to_Showcase_Its_MySizeId=E2=84=A2_Mobil?= =?UTF-8?Q?e_Measurement_Technology_at_CurvyCon_NYC?=
Home - ASE Publishing - About Us
© 2010 Aron Schatz (ASE Publishing) [Queries: 18 (8 Cached)] [Rows: 294 Fetched: 37] [Page Generation time: 0.34477615356445]