[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
Re: My little comments to CPP code
I wonder, what do you actually store as the attribute in the following code:
ctx.setAttribute<uudb>("handle", *db);
other than that, I'd suggest you add 'm_' prefix to any member variable in any class. That makes it easier to distinguish class members from anything else.
2007/3/21, sergey@total-knowledge.com <sergey@total-knowledge.com>:
Ok, finally I have my version of DB pool working. However, since the pool
should somehow take care of the login credentials, I have a question
regarding passing login/password to it.
I'm not going to send diffs since the code is far from decent even imho.
Here is the pool(I still use CODBCDatabase, but I remember that it should
be replaced with CDatabase):
-----------------------
uudb::uudb() : handle("DSN=PostgreSQL;UID=sergey;PWD=;DATABASE=uu")
{
handle.open();
string login = "sergey";
string password = "password12";
int server = 1;
string sessionid = "test";
connect(login, password, server, sessionid);
}
uudb::~uudb()
{
disconnect();
handle.close();
}
void uudb::connect(string login, string password, int server, string
sessionid)
{
try {
CQuery loginQuery(&handle,"select login(:login,:password,:server)");
loginQuery.param("login") = login;
loginQuery.param("password") = password;
loginQuery.param("server") = server;
loginQuery.exec();
}
catch (exception& e) {
cout<<"\nError: " <<e.what();
}
}
void uudb::disconnect()
{
try {
CQuery logoutQuery(&handle,"select logout()");
logoutQuery.exec();
}
catch (exception& e) {
cout<<"\nError: " <<e.what();
}
}
----------------------------
The question is how do I pass those login, password, server parameters to
connect()? Where do I get them, from request context? But since it's first
called in init() we don't have those params yet.
Here is how handle saved in context:
---------------
void InitServlet::init()
{
ServletContext& ctx = getServletConfig().getServletContext();
uudb* db = new uudb();
ctx.setAttribute<uudb>("handle", *db);
}
-----------------
Here is how I retrieve handle(or better say uudb object) from context in
UUServlet:
------------------
UUServlet::handle_t UUServlet::getHandle() const
{
ServletContext& ctx = getServletConfig().getServletContext();
handle_t handle;
handle = ctx.getAttribute
<uudb>("handle");
return handle;
}
-------------------
This handle passed to User model class:
--------------------
User::User(uudb& pool) : m_pool(pool)
{}
CQuery qrySelect(&(m_pool.handle),"select pl_id from person_list where
pl_login=:login and pl_password=:password");
---------------
The application does the job except handling login credentials in uudb().
--
Alexey Parshin,
http://www.sptk.net