Abstraction of Database Abstraction Library
Moderator: General Moderators
Abstraction of Database Abstraction Library
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?
Re: Abstraction of Database Abstraction Library
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.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Abstraction of Database Abstraction Library
Yes, you would write an Adapter class that makes both libraries conform to a single API.
(#10850)
Re: Abstraction of Database Abstraction Library
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.
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.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Abstraction of Database Abstraction Library
I think most of these libraries (and PDO) support the different LIMIT syntax that different databases use.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.
(#10850)
- Maugrim_The_Reaper
- DevNet Master
- Posts: 2704
- Joined: Tue Nov 02, 2004 5:43 am
- Location: Ireland
Re: Abstraction of Database Abstraction Library
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).
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).
Re: Abstraction of Database Abstraction Library
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.Maugrim_The_Reaper wrote:Thanks for reading my article