Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2002-07

Common database access in Pascal

7 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Thomas Schatzl

Hi,

I was wondering whether someone here knows if there's some sort of unit which provides some (basic) methods for common database access across different database systems for (free-)pascal/delphi using win32/linux.

I know that there are several header translations for Interbase, MySQL etc. out there, but I didn't succeed in finding a unit/package providing a common interface to do connection management, updates, selects and the likes. I.e. something like the JDBC interface (java) or PEAR library (php) for other (script-)languages which provide imo quite easy to use classes/objects without having to worry much about specific implementation details.
Some googling didn't yield much results, so I wanted to ask here whether I'm simply looking in the wrong places (ignorant of obvious solutions) or if there's really nothing available in this respect. Or even better, one of you has already implemented such a unit and wants to share it....

This package I'm currently searching for should provide, if there exists something like this, the following functionality:
- connecting/disconnecting to database
- transaction handling (basically turning on/off, commiting and rollback)
- execute updates/selects (e.g. sending a query to the database)

Regards,
Thomas

Reply by email

Florian Klaempfl
to Thomas Schatzl
you wrote 26 lines
Hi, I was wondering whether someone here knows if there's some sort of unit which provides some (basic) methods for common database access across different database systems for (free-)pascal/delphi using win32/linux. I know that there are several header translations for Interbase, MySQL etc. out there, but I didn't succeed in finding a unit/package providing a common interface to do connection management, updates, selects and the likes. I.e. something like the JDBC interface (java) or PEAR library (php) for other (script-)languages which provide imo quite easy to use classes/objects without having to worry much about specific implementation details. Some googling didn't yield much results, so I wanted to ask here whether I'm simply looking in the wrong places (ignorant of obvious solutions) or if there's really nothing available in this respect. Or even better, one of you has already implemented such a unit and wants to share it.... This package I'm currently searching for should provide, if there exists something like this, the following functionality: - connecting/disconnecting to database - transaction handling (basically turning on/off, commiting and rollback) - execute updates/selects (e.g. sending a query to the database)

What about the db components of the vcl/fcl? Though I don't know if they support transactions and how far the fcl implementation is, I don't know exactly either.

Just check fcl/db

Reply by email

Thomas Schatzl
to Florian Klaempfl

Subject: Re: [fpc-pascal] Common database access in Pascal

Hi,

[Common DB access]

Show quoted text 5 lines
What about the db components of the vcl/fcl? Though I don't know if they support transactions and how far the fcl implementation is, I don't know exactly either. Just check fcl/db

Thanks for the hint.... I had a look at them, but I think that it's not perfectly suitable. Although both the Interbase and MySQL use some common database objects provided in db.pp (which doesn't supply any documentation, is there something I'm not aware of ?), but their actual implementation, e.g. interface to access the database is quite different...

So the MySQL part uses the given TDataSet classes approach but the Interbase part doesn't. It splits the TDataSet class into three objects (It uses some classes of db.pp, but doesn't implement an interbase specific TDataSet. Well, I'm not going further into detail)....

Probably I'm a little mistaken about this, but it seems as if I'm still nearly at the point where I started :(

Btw, if you put a {$IFNDEF WIN32} around the {$LINKLIB}statements of the interbase test program testib.pp (and set up the example database according to the supplied script), it works with Interbase/FireBird 1.0 under the win32 platform. (I noticed a "don't know if it works under windows" notice in the example program)

Regards,
Thomas

Reply by email

Michael Van Canneyt
to Thomas Schatzl
Thomas Schatzl wrote 27 lines
Subject: Re: [fpc-pascal] Common database access in Pascal Hi, [Common DB access] >What about the db components of the vcl/fcl? Though I don't know >if they support transactions and how far the fcl implementation is, >I don't know exactly either. > >Just check fcl/db Thanks for the hint.... I had a look at them, but I think that it's not perfectly suitable. Although both the Interbase and MySQL use some common database objects provided in db.pp (which doesn't supply any documentation, is there something I'm not aware of ?), but their actual implementation, e.g. interface to access the database is quite different... So the MySQL part uses the given TDataSet classes approach but the Interbase part doesn't. It splits the TDataSet class into three objects (It uses some classes of db.pp, but doesn't implement an interbase specific TDataSet. Well, I'm not going further into detail).... Probably I'm a little mistaken about this, but it seems as if I'm still nearly at the point where I started :(

Your remarks are to the point. I have started to code something like

TDatabase
TTransaction
TDBDataset

Which would cooperate together much like you would expect them to. From TDatabase/TDBdataset etc, a specific descendent should be made for each type of database. The Interbase components at this point do this. unfortunately, the mysql components don't (not yet). It is my idea to finish this work, but due to lack of time, I cannot start working on this yet. (3 zillion other things requiring attention...)

If you're looking for a unified approach in the sense of Borland's BDE or even DBExpress, I have no plans for implementing this for the very simple reason that I don't believe in this approach: If you want unified database access, then I see more sense in making a set of TODBCDatabase/TODBCTransaction/TODBCDataset
descendents which can then be used to access any needed database.

You're of course welcome to help out in this matter. I'll guide and provide explanations wherever possible.

I you need a quick solution, you can use the low-level ODBC components I implemented. They resemble TDataset and so on, but are simply an OOP wrapper around the ODBC API. I successfully connected to MySQL, Interbase, MS-Access, MS-EXcel, from both windows and Linux.

Michael.

Reply by email

Fernando Lozano

Hi Michael,

Show quoted text 22 lines
> [Common DB access] > Your remarks are to the point. I have started to code something like TDatabase TTransaction TDBDataset Which would cooperate together much like you would expect them to. >From TDatabase/TDBdataset etc, a specific descendent should be made for each type of database. The Interbase components at this point do this. unfortunately, the mysql components don't (not yet). It is my idea to finish this work, but due to lack of time, I cannot start working on this yet. (3 zillion other things requiring attention...) If you're looking for a unified approach in the sense of Borland's BDE or even DBExpress, I have no plans for implementing this for the very simple reason that I don't believe in this approach: If you want unified database access, then I see more sense in making a set of TODBCDatabase/TODBCTransaction/TODBCDataset descendents which can then be used to access any needed database.

Well, having a unified DB API like Perl DBI, PHP PEAR, Java JDBC and others saves much development time. It's not nice to change lots of lines of code just to move from one database to another.

I don't know how much work is involved for the designer of the API and the developer of the drivers, but I guess that with so many sucessfull free software DB APIs availabe it shouldn't be so hard to pick one of them and mimic its structure and implementation.

Each Database class could have its own unique methods for its unique capabilites, but the standard connect/query/iterate should work the same way for any database, ideally using dynamic loading of libraries so the application don't need even to be recompiled to use a different DB.

About using ODBC this does not look like a good idea except because it won't need a lot of custom drivers. No one uses ODBC to provide drivers although everyone can use ODBC when they have no driver availabe. It's much easier, for example, to use Perl DBD::Oracle or DBD::MySQL directly than to configure the unix ODBC (or Windows ODBC) support for these databases.

[]s, Fernando Lozano

Reply by email

Michael Van Canneyt
to Fernando Lozano
Fernando Lozano wrote 28 lines
Hi Michael, > > [Common DB access] > > > Your remarks are to the point. I have started to code something like > > TDatabase > TTransaction > TDBDataset > > Which would cooperate together much like you would expect them to. > > >From TDatabase/TDBdataset etc, a specific descendent should be made for > each type of database. The Interbase components at this point do this. > unfortunately, the mysql components don't (not yet). It is my idea to > finish this work, but due to lack of time, I cannot start working on > this yet. (3 zillion other things requiring attention...) > > If you're looking for a unified approach in the sense of Borland's BDE > or even DBExpress, I have no plans for implementing this for the very > simple reason that I don't believe in this approach: If you want unified > database access, then I see more sense in making a set of > TODBCDatabase/TODBCTransaction/TODBCDataset > descendents which can then be used to access any needed database. Well, having a unified DB API like Perl DBI, PHP PEAR, Java JDBC and others saves much development time. It's not nice to change lots of lines of code just to move from one database to another.

Depending on how you would work, you'd normally have only one or two lines of code to change:

Something like

DB:=TMYSQLDatabase.Create(Nil);
DS:=TMySQLDataset.Create(Nil);

Would become

DB:=TIBDatabase.Create(Nil);
DS:=TIBDataset.Create(Nil);

All the rest would remain 100% the same...

Show quoted text 5 lines
I don't know how much work is involved for the designer of the API and the developer of the drivers, but I guess that with so many sucessfull free software DB APIs availabe it shouldn't be so hard to pick one of them and mimic its structure and implementation.

Sure. Note that I am not against a 'unified' approach - the only thing is that I don't think we should repeat the BDE or DBExpress setup.

Show quoted text 5 lines
Each Database class could have its own unique methods for its unique capabilites, but the standard connect/query/iterate should work the same way for any database, ideally using dynamic loading of libraries so the application don't need even to be recompiled to use a different DB.

The 'ideally using dynamic loading of libraries' is where I disagree. I think that recompiling the application is not really a hassle. See below for more argumentation.

For the rest I agree, but the OOP approach and the TDataset/TDBDataset/ TDatabase/TTransaction class tree already ensure that 99% of the code will remain exactly the same when changing databases.

It is one of the reasons why I went one step further as Delphi and introduced TDBDataset/TDatabase/TTransaction already in DB.pas: to stimulate a uniform approach of databases.

Show quoted text 7 lines
About using ODBC this does not look like a good idea except because it won't need a lot of custom drivers. No one uses ODBC to provide drivers although everyone can use ODBC when they have no driver availabe. It's much easier, for example, to use Perl DBD::Oracle or DBD::MySQL directly than to configure the unix ODBC (or Windows ODBC) support for these databases.

I understand. But I think a recompilation is usually not really a problem. I work with DB apps since many years, and

1. It certainly isn't so that most apps are required to run on a variety of different databases. If they do, usually they DO use ODBC :-) The average application is designed to work with 1 fixed kind of database. As a consequence, many queries will be geared towards this particular database for performance reasons.

2. We've seldom had to switch databases. We did it only once (Access->Interbase).
If you switch databases, I think the problem of different SQL syntaxes for many queries is more of a hassle than the fact that you would have to replace 2 lines of code as indicated above.

That is why I don't really see the need for this 'dynamic switching' and would prefer to leave that kind of stuff to an ODBC layer implementation.

That said, everyone is free to implement a BDE or dbExpress type of TDatabase/TDBDataset/etc descendent, which can handle dynamic switching of databases. It's not high on *my* priority list however.

Michael.

Reply by email

Fernando Lozano
to Michael Van Canneyt

Hi Michael,

Show quoted text 20 lines
>Well, having a unified DB API like Perl DBI, PHP PEAR, Java JDBC and >others saves much development time. It's not nice to change lots of >lines of code just to move from one database to another. > > Depending on how you would work, you'd normally have only one or two lines of code to change: Something like DB:=TMYSQLDatabase.Create(Nil); DS:=TMySQLDataset.Create(Nil); Would become DB:=TIBDatabase.Create(Nil); DS:=TIBDataset.Create(Nil); All the rest would remain 100% the same...

Could please send a complete example on this? The test and sample programs on the FCL provided by FPC 1.0.6 don't show this. I Could not figure, for example, how a Database and a Dataset would interact (I do not want each Dataset to have it's own connection to the database server).

By the way, have you seen my message about a possible memmory allocation bug on the DB classes from the FCL?

[]s, Fernando Lozano

Reply by email