473,395 Members | 1,941 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

OO DataBase Abstraction Layer, what for?

bilibytes
128 100+
Hi there,

I am wondering what are the advantages of having a database abstraction layer like the one explained Here (in Devshed)

Don't you get more efficient queries writing them down in SQL?

As far as i can understand, i just see an advantage whenever the underlying database were to change (and even then, SQL isn't the same on each RDBMS?)

I actually wrap my queries into methods like this:
Expand|Select|Wrap|Line Numbers
  1. public function getUserBm()
  2.     { 
  3.         $sql =<<<_SQL
  4.         SELECT * FROM bm LEFT JOIN bm_sections
  5.         ON bm.section_id = bm_sections.id
  6.         WHERE user_id = '$this->_userId'
  7. _SQL;
  8.         $result = $this->_db->query($sql);
  9.  
  10.         if($this->_db->numRows($result) > 0){    
  11.             while($row = $this->_db->fetchArray($result)){
  12.                 foreach($row as $key => $val){
  13.                     $dataArray[][$key] = $val;
  14.                 }
  15.             }
  16.             return $dataArray;
  17.         }
  18.     }

-I make a class to hide the RDBMS specific functions

-then i instantiate that class into a property of another class that will use the abstracted functions. (here: $this->_db)

-i try to create a layer that keeps SQL code separate from the objects that use the information fetched from db. (the code above is from one of the classes that are used for separating SQL from rest)

is this a very bad practice?

If someone can bring light on this topic, i would be very greatful.
Thank you very much

bilibytes
Apr 19 '09 #1
2 2227
Dormilich
8,658 Expert Mod 8TB
@bilibytes
only did a quick reading, but I think it's to leave the SQL composition and DB handling to the class (i.e. no manual SQL writing)

@bilibytes
probably

@bilibytes
that's why DALs were done in the first place (I assume). the script just fetches all fields to work in every case (although that may not be the best choice in some cases)

…and the user has still to take care of userland data validation himself.

@bilibytes
I find it a very good practice

@bilibytes
there are certainly more ways to use the DAL, it mostly depends on how you like to do it.

(currently my favorite is:
abstract DB handler class:
Expand|Select|Wrap|Line Numbers
  1. (void) public static function connect()
  2. (bool) public static function prepare((string) $index, (string) $sql)
  3. (object) public static function execute((string) $index, (array) $params)
SQL handling class
Expand|Select|Wrap|Line Numbers
  1. // prepare all non-dynamic SQL statements
  2. (bool) protected function load()
  3. // do stuff like triggering statement execution or processing the results
  4. protected/public function doSomething()
output class [extends SQL handling class]
(loads the methods from parent according to needs, fetches user input, formats DB results for output, …)
)
Apr 19 '09 #2
Markus
6,050 Expert 4TB
I think the main reason is to (obvious) abstract away any programmer errors that may arise from having to manually rewrite data access scripts when a database is changed from, say MySQL to one with different syntax. Using a (good) DAL will know which syntax to use based on your configuration of the DAL.

CodeIgniter (and most of the big frameworks, I can assume) uses an awesome DAL.

For someone who isn't very knowledgeable with SQL, it's easier to read the query when it's written like this:

Expand|Select|Wrap|Line Numbers
  1. $val = "some 'value";
  2. $val2 = "ano'ther value";
  3. $this->db->from('tbl1');
  4. $this->db->where('col1', $val);
  5. $this->db->where('col2', $val2);
  6. $this->db->limit(1);
  7. $this->db->order_by('col3', 'desc');
  8.  
  9. // Would produce (depending on the database driver you have specified)
  10. SELECT * FROM `tbl1` WHERE `col1` = 'some \'value' AND `col2` = 'ano\'ther value' ORDER BY `col3` DESC LIMIT 1
  11. // Notice all values are escaped and the how the SQL is arranged properly.
  12.  
Another cool thing for the CI library is chaining (PHP5 only).
Expand|Select|Wrap|Line Numbers
  1. $this->db
  2.      ->from('table')
  3.      ->where('x', 1)
  4.      ->limit(1);
  5.  
Apr 20 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: lawrence | last post by:
A user writes this sentence: "It was the New Urbanist's nightmare of sprawl run amok." They input that and my PHP script hits it with addslashes() and then the sentence gets put in the database....
3
by: Rainer Collet | last post by:
hi! i tested several php database abstraction layers (db, mdb(2), creole, adodb, etc), but i always missed one really important feature: i need a method for a limited select which gives me the...
13
by: SectorUnknown | last post by:
I've written a database (Access mdb) front-end using Python/wxpython/and ADO. However, the scope of the project has changed and I need to access the same data on an MSSQL server. Also, the...
2
by: David | last post by:
Is there a good DataBase Abstraction layer that will handle the mySql, Sql Server, and MS Access engines? I do not wish to write the same functions with the three different PHP db functions. ...
27
by: Brett | last post by:
If I want to easily swap the database I'm using, what is the best method for developing that tier in my application? I'll have basically a 4 tier app: 1. presentation 2. business logic 3. data...
25
by: Colin McKinnon | last post by:
Hi all, There's lots of DB abstraction layers out there, but a quick look around them hasn't turned up anything which seems to met my requirements. Before I go off and write one I thought I'd...
1
by: BJ Dierkes | last post by:
Hello all, I am looking for opinions on preferred methods of Database Abstraction Layer or Object Relation Mapper (I'm using Python 2.5). I have found a number of options such as those listed...
39
by: alex | last post by:
I've converted a latin1 database I have to utf8. The process has been: # mysqldump -u root -p --default-character-set=latin1 -c --insert-ignore --skip-set-charset mydb mydb.sql # iconv -f...
8
by: Ivan S | last post by:
What are your recommendations for lightweight database abstraction library (Oracle/MySQL)? I prefer OOP. :) Tnx, Ivan.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.