Abstraction of Database Abstraction Library

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
ynwa
Forum Newbie
Posts: 5
Joined: Wed Jan 23, 2008 8:42 am

Abstraction of Database Abstraction Library

Post by ynwa »

Hi, i'm writing some kind of cms. After reading this article http://www.patternsforphp.com/wiki/Abstract_Factory I have a question. Some one wrotes this for his projects, to use different libraries. But ADOdb and PEAR::DB has different syntax of their methods. How u solve this?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Abstraction of Database Abstraction Library

Post by pickle »

Probably your best bet is to remove one of the libraries & standardize on one or the other. Writing yet another layer to abstract above the two DB layers used is futile.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Abstraction of Database Abstraction Library

Post by Christopher »

Yes, you would write an Adapter class that makes both libraries conform to a single API.
(#10850)
ynwa
Forum Newbie
Posts: 5
Joined: Wed Jan 23, 2008 8:42 am

Re: Abstraction of Database Abstraction Library

Post by ynwa »

So I have to write Adapter for every action? Do you using such layer? And can u give me an exaple where I have to change abstraction of db?

And one more question: on different db drivers queries are differ, LIMIT for example, if there any chance to use some library which cnange query if db driver changes. as I now ADOdb not solves this problem.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Abstraction of Database Abstraction Library

Post by Christopher »

ynwa wrote:And one more question: on different db drivers queries are differ, LIMIT for example, if there any chance to use some library which cnange query if db driver changes. as I now ADOdb not solves this problem.
I think most of these libraries (and PDO) support the different LIMIT syntax that different databases use.
(#10850)
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Re: Abstraction of Database Abstraction Library

Post by Maugrim_The_Reaper »

Thanks for reading my article ;)

The point of it was really to introduce the concept of an Abstract Factory. In actual practice I've used it to switch between PDO, mysql, and mysqli (using AdoDB) depending on the deployment server (this was before the late 2007 surge to PHP5) and at the time it was more complicated than focusing on one DB or one abstraction layer. Notably this was a legacy application - legacy apps have some pretty twisted requirements at times - but in a new from-start application the Abstract Factory is unlikely to be required. I'd suggest moving towards some recognised patterns like a Data Mapper (or Active Record if it's simple enough).

To your original questions, I'd avoid PEAR DB since it's technically deprecated in favour of PEAR MDB2. ADOdb has poor support for PHP5 database access abstraction (I added mysqli support myself a while back). PDO is currently growing up and is worth a look, although it's less about abstraction and more about isolating common APIs. I say it's growing up because it's common API is not as common as the manual would like to convince you of - but it's still good enough if you're focusing only on 1-2 DBMS.

At the moment I'm using PDO predominantly since I focus only MySQL and Postgres usually. My data access layers accept plugins for custom drivers outside of PDO - so adding MSSQL is usually not too difficult if required (with some tweaking).
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Re: Abstraction of Database Abstraction Library

Post by BDKR »

Maugrim_The_Reaper wrote:Thanks for reading my article ;)
That's yours Maug? Sweet! I've been a fan of the factory pattern for some time. I just read it to make sure I'm still on track with it's usage. :D
Post Reply