473,387 Members | 1,578 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,387 software developers and data experts.

Abstracting a database class

Hey all!

I've been using PHP for a while and I'm hearing that I should look into
using it OO. I'm looking into making changes to the program Camera Life
(http://fdcl.sf.net). I want to abstract the connection to the database
which is currently MYSQL and maybe implement another database, or just
leave it open for addition. If I get lucky and get the hang of this,
I'd also like to abstract calls to GD.

So firstly, is there a good solution to this problem? I've seen many
examples, but many of them are incomplete or simply academic. Also,
will I lose any functionality by doing it this way? I am currently
using a complicated SELECT statement with a CONCAT. Will this be
possible with the abstracted class?

Thanks for your consideration,
FD

Jan 1 '06 #1
11 1575
Have a look at this: http://adodb.sourceforge.net/ The abstraction is
already done for you. Or, you can look on pear.php.net for their DB class
too.

Balazs
"Full Decent" <fu********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Hey all!

I've been using PHP for a while and I'm hearing that I should look into
using it OO. I'm looking into making changes to the program Camera Life
(http://fdcl.sf.net). I want to abstract the connection to the database
which is currently MYSQL and maybe implement another database, or just
leave it open for addition. If I get lucky and get the hang of this,
I'd also like to abstract calls to GD.

So firstly, is there a good solution to this problem? I've seen many
examples, but many of them are incomplete or simply academic. Also,
will I lose any functionality by doing it this way? I am currently
using a complicated SELECT statement with a CONCAT. Will this be
possible with the abstracted class?

Thanks for your consideration,
FD

Jan 1 '06 #2
Full Decent wrote:
I've been using PHP for a while and I'm hearing that I should look
into using it OO. I'm looking into making changes to the program
Camera Life (http://fdcl.sf.net). I want to abstract the connection
to the database which is currently MYSQL and maybe implement another
database, or just leave it open for addition. If I get lucky and get
the hang of this, I'd also like to abstract calls to GD.


You might want to take a peek at the following:

http://www.php.net/dbx/

Or for PHP5:

http://www.php.net/pdo/
JW
Jan 1 '06 #3
Full Decent wrote:
Hey all!

I've been using PHP for a while and I'm hearing that I should look into
using it OO. I'm looking into making changes to the program Camera Life
(http://fdcl.sf.net). I want to abstract the connection to the database
which is currently MYSQL and maybe implement another database, or just
leave it open for addition. If I get lucky and get the hang of this,
I'd also like to abstract calls to GD.

So firstly, is there a good solution to this problem? I've seen many
examples, but many of them are incomplete or simply academic. Also,
will I lose any functionality by doing it this way? I am currently
using a complicated SELECT statement with a CONCAT. Will this be
possible with the abstracted class?

Thanks for your consideration,
FD

I usually implement this with a double abstraction.

The first is a database factory to provide concepts such as SQL query
support, database connection sharing, error handling, etc.

The second is a database interface implementation that isolates all the
SQL in one class. This implementation is the only class that
instantiates the factory and uses it.

This way, I can change database factories (to connect to different
databases) with ease and, since the interface implementation contains
all the SQL, there is only one file I need to scan to adjust for the
variances in the SQL languages. (e.g. limit 1 in MySQL vs. count = 1 in
Oracle)

Is abstracting the database communications (with a factory) and
abstracting the interfaces a good idea? I certainly think so. In fact,
abstracting all communications is a good idea in my book. I use it for
all my SOAP work too.

-david-

Jan 1 '06 #4
Full Decent wrote:
Hey all!

I've been using PHP for a while and I'm hearing that I should look into
using it OO. I'm looking into making changes to the program Camera Life
(http://fdcl.sf.net). I want to abstract the connection to the database
which is currently MYSQL and maybe implement another database, or just
leave it open for addition. If I get lucky and get the hang of this,
I'd also like to abstract calls to GD.

So firstly, is there a good solution to this problem? I've seen many
examples, but many of them are incomplete or simply academic. Also,
will I lose any functionality by doing it this way? I am currently
using a complicated SELECT statement with a CONCAT. Will this be
possible with the abstracted class?

Thanks for your consideration,
FD


I would definetely consider it :) For instance, if you use mysql and
then need to move the site to another server with a different dbms, I
bet you'd hate to change all those mysql_* calls :)
So, off the top of the head, I'd go with something like

class DB {
function DB($host,$db,$user,$pwd) {
$this->host = $host;
$this->db = $db;
$this->user = $user;
$this->pwd = $pwd;
}

function connect() {
// abstract
}

...

function query($query) {
// abstract
}

...
}

class DBMySql extends DB {
function connect() {
mysql_connect($this-host,$this->user,$this->pwd);
mysql_select_db($this->db);
}
...
}

class DBPostgreSql extends DB {
function connect() {
pg_connect("host=$this->host dbname=$this->db user=$this->user
password=$this->pwd");
}
...
}

and so on. Then in any place you want to use database, assume you are
using class DB:

....
/**
* @var DB
* @access private
*/
var $db;
....

Then instantiate it to whatever you use:

....
$this->db = new DBMySql("localhost","test","user","pwd");
....
$this->connect(); // works for any database now
....

Then, if you move to another database, simply change which class you
instantiate.

As for the SQL statments, I'd say there could be a problem if the syntax
differs in different dbms. For instance, in Oracle to get current date
one would call sysdate(), and in ms-sql - getdate(). In this case you
may want to consider aliases for function names which are replaced when
query() method is called.
luph
Jan 1 '06 #5

This way, I can change database factories (to connect to different
databases) with ease and, since the interface implementation contains all
the SQL, there is only one file I need to scan to adjust for the variances
in the SQL languages. (e.g. limit 1 in MySQL vs. count = 1 in Oracle)

Hmmm, ADODB already takes care of that for you. It also deals with date
formatting and other issues. Plus it provides useful features like caching
and SQL logging. Why reinvent the wheel?

Balazs
Jan 1 '06 #6
Full Decent wrote:
Hey all!

I've been using PHP for a while and I'm hearing that I should look into
using it OO. I'm looking into making changes to the program Camera Life
(http://fdcl.sf.net). I want to abstract the connection to the database
which is currently MYSQL and maybe implement another database, or just
leave it open for addition. If I get lucky and get the hang of this,
I'd also like to abstract calls to GD.


That's not a good approach. There is no point in abstracting calls to
the database, as the bulk of the differences between databases lies in
the sematics of the queries. What you really want to do is abstract the
entire data retrieval process. You application code doesn't need to
know that it's getting its data from a database. The data just needs to
be coming from somewhere: a MySQL database, a MS-SQL database, an
XML-file, or even a SOAP-compatible web-service. If your application is
agnostic in regards to data source, then it's automatically database
independent.

Jan 1 '06 #7
Balazs Wellisch wrote:
This way, I can change database factories (to connect to different
databases) with ease and, since the interface implementation contains all
the SQL, there is only one file I need to scan to adjust for the variances
in the SQL languages. (e.g. limit 1 in MySQL vs. count = 1 in Oracle)

Hmmm, ADODB already takes care of that for you. It also deals with date
formatting and other issues. Plus it provides useful features like caching
and SQL logging. Why reinvent the wheel?

Balazs

ADOdb is an example of an abstract database factory, as is PEAR DB. It
was not my intention to imply that there was a need to write the
factory, just that it was a good idea to isolate the concept of the
factory from the concept of the SQL code driving the factory, and, in
turn, to isolate the SQL code from the rest of the application space.
Sometimes, due to the databases I am talking to and the in-house
standards in place, it is necessary to write my own factory. This does
not diminish the advantages of the two abstraction layer model.

In other OO languages, Java for example, it is common to have a caching
database factory and a database bean - which is an interface
implementation - that contains the SQL code for the application.

-david-

Jan 1 '06 #8
I like this idea and the possibility of running "DB queries" against a
real DB, the filesystem, or the network. Do you know of existing
classes that provide this level of abstraction beyond ADODB?

Jan 3 '06 #9
I am going to start by porting to ADODB. Later I will see if I can
abstract further by separating the accesses as Leong suggested.

Thanks!

Jan 3 '06 #10

Full Decent wrote:
I like this idea and the possibility of running "DB queries" against a
real DB, the filesystem, or the network. Do you know of existing
classes that provide this level of abstraction beyond ADODB?


It's just a matter of separating the concerns of your code. Doesn't
really require special logic. For example, say you need to retrieve a
article from the database, instead of running the query in the
application code itself, call a function to do it:

$message = GetMessageById($id);

or

$message = GetMessageByTitle($title);

or

$messages = GetMessagesByDate($start, $end);

If you want to get fancy, use a class factory

$messages = $message_factory->Load($criteria, $offset, $length);

There are many ways to do this. The bottomline is to isolate the data
save/retrieval code, so that you can swap in a different mechanism by
simply including a different file.

Jan 4 '06 #11
Hello,

on 01/03/2006 07:38 PM Full Decent said the following:
I like this idea and the possibility of running "DB queries" against a
real DB, the filesystem, or the network. Do you know of existing
classes that provide this level of abstraction beyond ADODB?


You may want to take a look at Metastorage. This is a tool that
generates code for classes of objects that perform what is known as
object-relational mapping. In simple words, you can abstract table rows
and treat them as objects.

Besides the abstraction level that Metastorage generated code provides,
it also makes you much more productive. You just need to describe the
classes, variables, validation rules, relationships between classes and
functions that you need manipulate your objects. Then Metastorage
generates all the code of the classes that you describe in a few seconds.

Metastorage can also generate classes to install your database schema,
report data extraction classes and classes to generate and process
themed Web forms to perform common types of object manipulation operations.

A similar effort to write, test and debug equivalent code manually would
take days, weeks of months to be ready. The more complex your project
becomes, the greater are the productivity gains that Metastorage provides.

Here you may find more information about Metastorage:

http://www.metastorage.net/

Here are some screenshots of Metastorage generator Web interface and
generated application forms using different themes
http://www.meta-language.net/screenshots.html

Here is a tutorial:
http://www.meta-language.net/metastorage-tutorial.html
--

Regards,
Manuel Lemos

Metastorage - Data object relational mapping layer generator
http://www.metastorage.net/

PHP Classes - Free ready to use OOP components written in PHP
http://www.phpclasses.org/
Jan 4 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: lawrence | last post by:
I posted before, but have now narrowed my problem down to this method. At the start of the method, I test to make sure that I have a resource, a pointer to data returned from a database. This test...
15
by: Scott Auge | last post by:
I am looking for comments on something that lets me abstract database updates in an object. Lemme explain what I am thinking: Lets say I have an object Person with... SetFirstName()...
1
by: Johan | last post by:
Hi, The problem is : You have a class A with one or more data variables. You want to store the data variables in a database for example MySQL. Also you do not want to have SQL code in your...
0
by: Tim Barnes | last post by:
I am going through a revision of my product where I am trying to abstract out the data access layer, where I have traditionally just dragged/dropped data adapters and generated datasets using...
2
by: Bryan | last post by:
Hello, I'm just starting to develop in asp.net and i have a question about using a database connection globally in my app. I have set up the procedures for getting all my connection string info...
3
by: josh.kuo | last post by:
Sorry about the subject, I can't think of a better one. I recently wrote some PHP classes that I think might be of interest to this group. Since I have been reaping the benefits of reading news...
5
by: Slant | last post by:
Here's a question that most will have different answers to. I'm just dying to find a solution that seems halfway automated!! There really are two seperate issues which might be answered by the...
1
by: | last post by:
Hi. This is a a semi-newbie question about how to store arbitrary information about my apps such that I can code quickly, mimizing complexity and the number of things I have to hold in my brain. I...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...

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.