Home Projects Jobs Clientele Contact


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

My little comments to CPP code


I'm not sure how far we are from the working prototype, but the following code would never work correctly:

+bool User::setSessionInfo(string login, string password, int server, string sessionid)
+  CODBCDatabase db("DSN=PostgreSQL;UID=sergey;PWD=;DATABASE=uu");
+  try {
+    CQuery qryInsert(&db,"select login('"+login+"', '"+password+"', "+lexical_cast<string>(server)+")");
+    qryInsert.exec();
+  }
+  catch (exception& e) {
+    cout<<"\nError: " <<e.what();
+    return false;
+  }
+  return true;

Here is how it works now:

1) The database object is created.
2) Query is executed. Since the connection wasn't open yet, query makes database object to open a connection. Execution of the query creates temporary table session_info and fills it with user information.
3) Query and database objects go out the scope with the return. Connection is closed in database object destructor. Temporary table that is local to connection is destroyed by Postgres server.

As result, we have no effect on the database.

The workaround may be the following (for instance): We make CDatabase* a member of User class. It's created in User ctor and destroyed in user dtor. All the time in between it must stay open.

Also, I'd do the code within try a little bit different, like this:

try {
   CQuery qryInsert(&db,"select login(:login,:password,:server)");
   qryInsert.param("login") = login;
   qryInsert.param("password") = password;
   qryInsert.param("server") = server; // this should be an integer parameter, use course_server table as a source

Now, in this particular approach it just looks longer than original version. However, as we get to slightly more complex queries, it makes the query syntax much more clear.
Also, in most of the cases, SPTK uses binary data with no need for conversion to string. In many cases, it's faster, too.

Alexey Parshin,

Authoright © Total Knowledge: 2001-2008