UniverseUniversity


Home Projects Jobs Clientele Contact

uu


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

Re: UU code



I understand what you mean and then imho it's better to leave it as I have
right now with using UuDbPool' Connection class.

Currently I have only 2 lines in my code that mention CODBCDatabase:

class UuDbPool
{
 public:
  class Connection: public sptk::CODBCDatabase
    {
    public:
      Connection(UuDbPool* pool)
        : sptk::CODBCDatabase(pool->m_conString)
        {}
    };
}

typedef std::list<Connection*> connectionlist_t;
}

All I need to change is CODBCDatabase to CPostgresDatabase in 2 lines if
needed, that's it. And I don't have to deal with CDatabase anywhere in my
classes and servlets, add sptk namespace etc...
There is much less room for the error in this case imho and much less
changes needed if driver will change, b/c I do create newe instances of
Connection in my pool in several cases, not only in constructor.


> 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