UniverseUniversity


Home Projects Jobs Clientele Contact

uu


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: UU code





2007/4/12, sergey@total-knowledge.com <sergey@total-knowledge.com>:
I have my code working with CODBCDatabase and I'm trying to change it to
CDatabase as you suggested.
I can't create objects using keyword 'new' with sptk::CDatabase, what's
the right way to create new connection?

You should have CDatabase* data members in your classes. But, when you create a new connection object, you should use something like:

  my_connection = new CODBCDatabase(.....);

Since my connection is CDatabase*, we can later replace this with any other database driver, for instance:

  my_connection = new CPostgresDatabase(.....);

If all of your code uses only CDatabase objects, there would not be a significant change in your code. This is possible because both CODBCDatabase and (not yet existing) CPostgresDatabase are derived from CDatabase and have identical methods.

Also CDatabase does not have active() method implemented, but I use it in
my DB pool, what should I do?

If you create a connection as described above, then virtual method active() would be called from CODBCDatabase.

Thanks.


>
> 2) We should never use CODBCDatabase in our code besides creating the
> connection object. Any code that needs a database object should use
> CDatabase. That allows us to change a single line of code if we need to
> switch to totally different database driver. SPTK allows to create a
> database driver for anything that supports basic database functions. If we
> don't like Postgres ODBC driver, for instance, we can create
> CPostgresDatabase class, and replace:
>
> CDatabase* db;
> ...
>
> db = new CODBCDatabase("DSN=.....");
> with
> db = new CPostgresDatabase(".....");
>
> It would not require any corrections in C++ code besides the difference in
> SQL syntax.
>
>
> --
> Alexey Parshin,
> http://www.sptk.net
>





--
Alexey Parshin,
http://www.sptk.net

Authoright © Total Knowledge: 2001-2008