Throw instead of return value
authorZofia Abramowska <z.abramowska@samsung.com>
Wed, 18 Jun 2014 13:50:19 +0000 (15:50 +0200)
committerBartlomiej Grzelewski <b.grzelewski@samsung.com>
Fri, 12 Sep 2014 12:58:32 +0000 (14:58 +0200)
All public methods of DBCrypto throw instead of returning
value from key-manager api error codes.
Rewritten also ckm-logic to handle exceptions from DBCrypto.

Change-Id: Ie85cb8b4e448e23aa26782d8a5451a5fcfed81e8

src/manager/service/ckm-logic.cpp
src/manager/service/db-crypto.cpp
src/manager/service/db-crypto.h

index 3b933fe..f120954 100644 (file)
@@ -146,27 +146,29 @@ int CKMLogic::saveDataHelper(
     if (0 == m_userDataMap.count(cred.uid))
         return KEY_MANAGER_API_ERROR_DB_LOCKED;
 
-    DBRow row = {   alias,  cred.smackLabel, policy.restricted,
+    DBRow row = { alias, cred.smackLabel, policy.restricted,
          policy.extractable, dataType, DBCMAlgType::NONE,
          0, RawBuffer(10, 'c'), key.size(), key };
 
     auto &handler = m_userDataMap[cred.uid];
     if (!handler.crypto.haveKey(cred.smackLabel)) {
         RawBuffer key;
-        int status = handler.database.getKey(cred.smackLabel, key);
-        if (KEY_MANAGER_API_ERROR_DB_ALIAS_UNKNOWN == status) {
-            LogDebug("No Key in database found. Generating new one for label: " << cred.smackLabel);
+        auto key_optional = handler.database.getKey(cred.smackLabel);
+        if(!key_optional) {
+            LogDebug("No Key in database found. Generating new one for label: "
+                    << cred.smackLabel);
             key = handler.keyProvider.generateDEK(cred.smackLabel);
-            if (KEY_MANAGER_API_SUCCESS != handler.database.saveKey(cred.smackLabel, key)) {
-                LogError("Failed to save key for smack label: " << cred.smackLabel);
-                return KEY_MANAGER_API_ERROR_DB_ERROR;
-            }
+        } else {
+            key = *key_optional;
         }
+
         key = handler.keyProvider.getPureDEK(key);
         handler.crypto.pushKey(cred.smackLabel, key);
+        handler.database.saveKey(cred.smackLabel, key);
     }
     handler.crypto.encryptRow(policy.password, row);
-    return handler.database.saveDBRow(row);
+    handler.database.saveDBRow(row);
+    return KEY_MANAGER_API_SUCCESS;
 }
 
 RawBuffer CKMLogic::saveData(
@@ -178,7 +180,6 @@ RawBuffer CKMLogic::saveData(
     const PolicySerializable &policy)
 {
     int retCode = KEY_MANAGER_API_SUCCESS;
-
     try {
         retCode = saveDataHelper(cred, dataType, alias, key, policy);
         LogDebug("SaveDataHelper returned: " << retCode);
@@ -188,6 +189,12 @@ RawBuffer CKMLogic::saveData(
     } catch (const DBCryptoModule::Exception::Base &e) {
         LogError("DBCryptoModule failed with message: " << e.GetMessage());
         retCode = KEY_MANAGER_API_ERROR_SERVER_ERROR;
+    } catch (const DBCrypto::Exception::InternalError &e) {
+        LogError("DBCrypto failed with message: " << e.GetMessage());
+        retCode = KEY_MANAGER_API_ERROR_DB_ERROR;
+    } catch (const DBCrypto::Exception::AliasExists &e) {
+        LogError("DBCrypto couldn't save duplicate alias");
+        retCode = KEY_MANAGER_API_ERROR_DB_ALIAS_EXISTS;
     }
 
     MessageBuffer response;
@@ -208,7 +215,12 @@ RawBuffer CKMLogic::removeData(
     int retCode = KEY_MANAGER_API_SUCCESS;
 
     if (0 < m_userDataMap.count(cred.uid)) {
-        retCode = m_userDataMap[cred.uid].database.deleteDBRow(alias, cred.smackLabel);
+        Try {
+            m_userDataMap[cred.uid].database.deleteDBRow(alias, cred.smackLabel);
+        } Catch (CKM::Exception) {
+            LogError("Error in deleting row!");
+            retCode = KEY_MANAGER_API_ERROR_DB_ERROR;
+        }
     } else {
         retCode = KEY_MANAGER_API_ERROR_DB_LOCKED;
     }
@@ -229,36 +241,38 @@ int CKMLogic::getDataHelper(
     const std::string &password,
     DBRow &row)
 {
-    int retCode = KEY_MANAGER_API_SUCCESS;
 
     if (0 == m_userDataMap.count(cred.uid))
         return KEY_MANAGER_API_ERROR_DB_LOCKED;
 
     auto &handler = m_userDataMap[cred.uid];
 
+    DBCrypto::DBRowOptional row_optional;
     if (dataType == DBDataType::CERTIFICATE || dataType == DBDataType::BINARY_DATA) {
-        retCode = handler.database.getDBRow(alias, cred.smackLabel, dataType, row);
+        row_optional = handler.database.getDBRow(alias, cred.smackLabel, dataType);
     } else if ((static_cast<int>(dataType) >= static_cast<int>(DBDataType::DB_KEY_FIRST))
             && (static_cast<int>(dataType) <= static_cast<int>(DBDataType::DB_KEY_LAST)))
     {
-        retCode = handler.database.getKeyDBRow(alias, cred.smackLabel, row);
+        row_optional = handler.database.getKeyDBRow(alias, cred.smackLabel);
     } else {
         LogError("Unknown type of requested data" << (int)dataType);
         return KEY_MANAGER_API_ERROR_BAD_REQUEST;
     }
-
-    if (KEY_MANAGER_API_SUCCESS != retCode){
-        LogDebug("DBCrypto::getDBRow failed with code: " << retCode);
-        return retCode;
+    if(!row_optional) {
+        LogError("No row for given alias, label and type");
+        return KEY_MANAGER_API_ERROR_DB_ALIAS_UNKNOWN;
+    } else {
+        row = *row_optional;
     }
 
     if (!handler.crypto.haveKey(row.smackLabel)) {
         RawBuffer key;
-        retCode = handler.database.getKey(row.smackLabel, key);
-        if (KEY_MANAGER_API_SUCCESS != retCode) {
-            LogDebug("DBCrypto::getKey failed with: " << retCode);
-            return retCode;
+        auto key_optional = handler.database.getKey(row.smackLabel);
+        if(!key_optional) {
+            LogError("No key for given label in database");
+            return KEY_MANAGER_API_ERROR_DB_ERROR;
         }
+        key = *key_optional;
         key = handler.keyProvider.getPureDEK(key);
         handler.crypto.pushKey(cred.smackLabel, key);
     }
@@ -287,6 +301,9 @@ RawBuffer CKMLogic::getData(
     } catch (const DBCryptoModule::Exception::Base &e) {
         LogError("DBCryptoModule failed with message: " << e.GetMessage());
         retCode = KEY_MANAGER_API_ERROR_SERVER_ERROR;
+    } catch (const DBCrypto::Exception::Base &e) {
+        LogError("DBCrypto failed with message: " << e.GetMessage());
+        retCode = KEY_MANAGER_API_ERROR_DB_ERROR;
     }
 
     if (KEY_MANAGER_API_SUCCESS != retCode) {
@@ -315,10 +332,15 @@ RawBuffer CKMLogic::getDataList(
 
     if (0 < m_userDataMap.count(cred.uid)) {
         auto &handler = m_userDataMap[cred.uid];
-        if (dataType == DBDataType::CERTIFICATE || dataType == DBDataType::BINARY_DATA) {
-            retCode = handler.database.getAliases(dataType, cred.smackLabel, aliasVector);
-        } else {
-            retCode = handler.database.getKeyAliases(cred.smackLabel, aliasVector);
+        Try {
+            if (dataType == DBDataType::CERTIFICATE || dataType == DBDataType::BINARY_DATA) {
+                handler.database.getAliases(dataType, cred.smackLabel, aliasVector);
+            } else {
+                handler.database.getKeyAliases(cred.smackLabel, aliasVector);
+            }
+        } Catch (CKM::Exception) {
+            LogError("Failed to get aliases");
+            retCode = KEY_MANAGER_API_ERROR_DB_ERROR;
         }
     } else {
         retCode = KEY_MANAGER_API_ERROR_DB_LOCKED;
index 21a12c1..f560153 100644 (file)
@@ -127,26 +127,28 @@ using namespace DB;
     DBCrypto::DBCrypto(const std::string& path,
                          const RawBuffer &rawPass) {
         m_connection = NULL;
-        m_init = false;
         Try {
             m_connection = new SqlConnection(path, SqlConnection::Flag::Option::CRW);
             m_connection->SetKey(rawPass);
             initDatabase();
-            m_init = true;
         } Catch(SqlConnection::Exception::ConnectionBroken) {
             LogError("Couldn't connect to database: " << path);
+            ReThrow(DBCrypto::Exception::InternalError);
         } Catch(SqlConnection::Exception::InvalidArguments) {
             LogError("Couldn't set the key for database");
+            ReThrow(DBCrypto::Exception::InternalError);
         } Catch(SqlConnection::Exception::SyntaxError) {
             LogError("Couldn't initiate the database");
+            ReThrow(DBCrypto::Exception::InternalError);
+        } Catch(SqlConnection::Exception::InternalError) {
+            LogError("Couldn't create the database");
+            ReThrow(DBCrypto::Exception::InternalError);
         }
     }
 
     DBCrypto::DBCrypto(DBCrypto &&other) :
-            m_connection(other.m_connection),
-            m_init(other.m_init) {
+            m_connection(other.m_connection) {
         other.m_connection = NULL;
-        other.m_init = false;
     }
 
     DBCrypto::~DBCrypto() {
@@ -161,27 +163,27 @@ using namespace DB;
         m_connection = other.m_connection;
         other.m_connection = NULL;
 
-        m_init = other.m_init;
-        other.m_init = false;
-
         return *this;
     }
 
-    void DBCrypto::createTable(const char* create_cmd) {
+    void DBCrypto::createTable(
+            const char* create_cmd,
+            const char *table_name)
+    {
         Try {
             m_connection->ExecCommand(create_cmd);
         } Catch(SqlConnection::Exception::SyntaxError) {
-            LogError("Couldn't create the main table!");
+            LogError("Couldn't create table : " << table_name << "!");
             throw;
         }
     }
 
     void DBCrypto::initDatabase() {
         if(!m_connection->CheckTableExist(main_table)) {
-            createTable(db_create_main_cmd);
+            createTable(db_create_main_cmd, main_table);
         }
         if(!m_connection->CheckTableExist(key_table)) {
-            createTable(db_create_key_cmd);
+            createTable(db_create_key_cmd, key_table);
         }
     }
 
@@ -212,16 +214,16 @@ using namespace DB;
             return false;
     }
 
-    int DBCrypto::saveDBRow(const DBRow &row){
-        if(!m_init)
-            return KEY_MANAGER_API_ERROR_DB_ERROR;
+    void DBCrypto::saveDBRow(const DBRow &row){
         Try {
 
             //Sqlite does not support partial index in our version,
             //so we do it by hand
             if((row.restricted == 1 && checkAliasExist(row.alias, row.smackLabel)) ||
                     (row.restricted == 0 && checkGlobalAliasExist(row.alias))) {
-                return KEY_MANAGER_API_ERROR_DB_ALIAS_EXISTS;
+                ThrowMsg(DBCrypto::Exception::AliasExists,
+                        "Alias exists for alias: " << row.alias
+                        << ", label: " << row.smackLabel);
             }
 
             SqlConnection::DataCommandAutoPtr insertCommand =
@@ -238,16 +240,15 @@ using namespace DB;
             insertCommand->BindBlob(10, row.data);
 
             insertCommand->Step();
+            return;
 
         } Catch(SqlConnection::Exception::SyntaxError) {
             LogError("Couldn't prepare insert statement");
-            return KEY_MANAGER_API_ERROR_DB_ERROR;
         } Catch(SqlConnection::Exception::InternalError) {
             LogError("Couldn't execute insert statement");
-            return KEY_MANAGER_API_ERROR_DB_ERROR;
         }
-
-        return KEY_MANAGER_API_SUCCESS;
+        ThrowMsg(DBCrypto::Exception::InternalError,
+                "Couldn't save DBRow");
     }
 
     DBRow DBCrypto::getRow(const SqlConnection::DataCommandAutoPtr &selectCommand) {
@@ -265,51 +266,41 @@ using namespace DB;
         return row;
     }
 
-    int DBCrypto::getDBRow(
+    DBCrypto::DBRowOptional DBCrypto::getDBRow(
         const Alias &alias,
         const std::string &label,
-        DBDataType type,
-        DBRow &row)
+        DBDataType type)
     {
-        if(!m_init)
-            return KEY_MANAGER_API_ERROR_DB_ERROR;
         Try {
-        SqlConnection::DataCommandAutoPtr selectCommand =
-                m_connection->PrepareDataCommand(select_alias_cmd);
-        selectCommand->BindString(1, alias.c_str());
-        selectCommand->BindInteger(2, static_cast<int>(type));
-        selectCommand->BindString(3, label.c_str());
-        selectCommand->BindString(4, alias.c_str());
-        selectCommand->BindInteger(5, static_cast<int>(type));
-
-
-        if(selectCommand->Step()) {
-            row = getRow(selectCommand);
-        } else {
-            return KEY_MANAGER_API_ERROR_DB_ALIAS_UNKNOWN;
-        }
+            SqlConnection::DataCommandAutoPtr selectCommand =
+                    m_connection->PrepareDataCommand(select_alias_cmd);
+            selectCommand->BindString(1, alias.c_str());
+            selectCommand->BindInteger(2, static_cast<int>(type));
+            selectCommand->BindString(3, label.c_str());
+            selectCommand->BindString(4, alias.c_str());
+            selectCommand->BindInteger(5, static_cast<int>(type));
 
-        AssertMsg(!selectCommand->Step(),
-                "Select returned multiple rows for unique column.");
+            if(selectCommand->Step()) {
+                return DBRowOptional(getRow(selectCommand));
+            } else {
+                return DBRowOptional();
+            }
         } Catch (SqlConnection::Exception::InvalidColumn) {
             LogError("Select statement invalid column error");
-            return KEY_MANAGER_API_ERROR_DB_ERROR;
         } Catch (SqlConnection::Exception::SyntaxError) {
             LogError("Couldn't prepare select statement");
-            return KEY_MANAGER_API_ERROR_DB_ERROR;
         } Catch (SqlConnection::Exception::InternalError) {
             LogError("Couldn't execute select statement");
-            return KEY_MANAGER_API_ERROR_DB_ERROR;
         }
-        return KEY_MANAGER_API_SUCCESS;
+        ThrowMsg(DBCrypto::Exception::InternalError,
+                "Couldn't get row for type " << static_cast<int>(type) <<
+                " alias " << alias << " and label " << label);
     }
 
-    int DBCrypto::getKeyDBRow(
+    DBCrypto::DBRowOptional DBCrypto::getKeyDBRow(
         const Alias &alias,
-        const std::string &label,
-        DBRow &row) {
-        if (!m_init)
-            return KEY_MANAGER_API_ERROR_DB_ERROR;
+        const std::string &label)
+    {
         Try{
             SqlConnection::DataCommandAutoPtr selectCommand =
                     m_connection->PrepareDataCommand(select_key_alias_cmd);
@@ -318,25 +309,24 @@ using namespace DB;
             selectCommand->BindInteger(3, static_cast<int>(DBDataType::DB_KEY_LAST));
             selectCommand->BindString(4, label.c_str());
 
-            if(selectCommand->Step()){
-                row = getRow(selectCommand);
+            if(selectCommand->Step()) {
+                return DBRowOptional(getRow(selectCommand));
             } else {
-                return KEY_MANAGER_API_ERROR_DB_ALIAS_UNKNOWN;
+                return DBRowOptional();
             }
         } Catch (SqlConnection::Exception::InvalidColumn) {
             LogError("Select statement invalid column error");
-            return KEY_MANAGER_API_ERROR_DB_ERROR;
         } Catch (SqlConnection::Exception::SyntaxError) {
             LogError("Couldn't prepare select statement");
-            return KEY_MANAGER_API_ERROR_DB_ERROR;
         } Catch (SqlConnection::Exception::InternalError) {
             LogError("Couldn't execute select statement");
-            return KEY_MANAGER_API_ERROR_DB_ERROR;
         }
-        return KEY_MANAGER_API_SUCCESS;
+        ThrowMsg(DBCrypto::Exception::InternalError,
+                "Couldn't get Key for alias " << alias
+                << " and label " << label);
     }
 
-    int DBCrypto::getSingleType(
+    void DBCrypto::getSingleType(
             DBDataType type,
             const std::string& label,
             AliasVector& aliases)
@@ -353,38 +343,32 @@ using namespace DB;
                 alias = selectCommand->GetColumnString(0);
                 aliases.push_back(alias);
             }
+            return;
         } Catch (SqlConnection::Exception::InvalidColumn) {
             LogError("Select statement invalid column error");
-            return KEY_MANAGER_API_ERROR_DB_ERROR;
         } Catch (SqlConnection::Exception::SyntaxError) {
             LogError("Couldn't prepare select statement");
-            return KEY_MANAGER_API_ERROR_DB_ERROR;
         } Catch (SqlConnection::Exception::InternalError) {
             LogError("Couldn't execute select statement");
-            return KEY_MANAGER_API_ERROR_DB_ERROR;
         }
-        return KEY_MANAGER_API_SUCCESS;
+        ThrowMsg(DBCrypto::Exception::InternalError,
+                "Couldn't get type " << static_cast<int>(type)
+                << " for label " << label);
     }
 
-    int DBCrypto::getAliases(
+    void DBCrypto::getAliases(
         DBDataType type,
         const std::string& label,
         AliasVector& aliases)
     {
-        if(!m_init)
-            return KEY_MANAGER_API_ERROR_DB_ERROR;
-
-        return getSingleType(type, label, aliases);
+        getSingleType(type, label, aliases);
     }
 
 
-    int DBCrypto::getKeyAliases(
+    void DBCrypto::getKeyAliases(
         const std::string &label,
         AliasVector &aliases)
     {
-        if (!m_init)
-            return KEY_MANAGER_API_ERROR_DB_ERROR;
-
         Try{
             SqlConnection::DataCommandAutoPtr selectCommand =
                             m_connection->PrepareDataCommand(select_key_type_cmd);
@@ -397,20 +381,19 @@ using namespace DB;
                 alias = selectCommand->GetColumnString(0);
                 aliases.push_back(alias);
             }
+            return;
         } Catch (SqlConnection::Exception::InvalidColumn) {
             LogError("Select statement invalid column error");
-            return KEY_MANAGER_API_ERROR_DB_ERROR;
         } Catch (SqlConnection::Exception::SyntaxError) {
             LogError("Couldn't prepare select statement");
-            return KEY_MANAGER_API_ERROR_DB_ERROR;
         } Catch (SqlConnection::Exception::InternalError) {
             LogError("Couldn't execute select statement");
-            return KEY_MANAGER_API_ERROR_DB_ERROR;
         }
-        return KEY_MANAGER_API_SUCCESS;
+        ThrowMsg(DBCrypto::Exception::InternalError,
+                "Couldn't get key aliases for label " << label);
     }
 
-    int DBCrypto::deleteDBRow(
+    void DBCrypto::deleteDBRow(
             const Alias &alias,
             const std::string &label)
     {
@@ -420,93 +403,77 @@ using namespace DB;
             deleteCommand->BindString(1, alias.c_str());
             deleteCommand->BindString(2, label.c_str());
             deleteCommand->Step();
+            return;
         } Catch (SqlConnection::Exception::SyntaxError) {
             LogError("Couldn't prepare delete statement");
-            return KEY_MANAGER_API_ERROR_DB_ERROR;
         } Catch (SqlConnection::Exception::InternalError) {
             LogError("Couldn't execute delete statement");
-            return KEY_MANAGER_API_ERROR_DB_ERROR;
         }
-        return KEY_MANAGER_API_SUCCESS;
+        ThrowMsg(DBCrypto::Exception::InternalError,
+                "Couldn't delete DBRow for alias " << alias
+                << " and label " << label);
     }
 
-    int DBCrypto::saveKey(
+    void DBCrypto::saveKey(
             const std::string& label,
             const RawBuffer &key)
     {
-        if (!m_init)
-            return KEY_MANAGER_API_ERROR_DB_ERROR;
-
         Try {
             SqlConnection::DataCommandAutoPtr insertCommand =
                     m_connection->PrepareDataCommand(insert_key_cmd);
             insertCommand->BindString(1, label.c_str());
             insertCommand->BindBlob(2, key);
-
             insertCommand->Step();
-
+            return;
         } Catch (SqlConnection::Exception::SyntaxError) {
             LogError("Couldn't prepare insert key statement");
-            return KEY_MANAGER_API_ERROR_DB_ERROR;
         } Catch (SqlConnection::Exception::InternalError) {
             LogError("Couldn't execute insert statement");
-            return KEY_MANAGER_API_ERROR_DB_ERROR;
         }
-
-        return KEY_MANAGER_API_SUCCESS;
+        ThrowMsg(DBCrypto::Exception::InternalError,
+                "Couldn't save key for label " << label);
     }
-    int DBCrypto::getKey(
-            const std::string& label,
-            RawBuffer &key)
-    {
-        if (!m_init)
-            return KEY_MANAGER_API_ERROR_DB_ERROR;
 
+    DBCrypto::RawBufferOptional DBCrypto::getKey(
+            const std::string& label)
+    {
         Try {
             SqlConnection::DataCommandAutoPtr selectCommand =
                     m_connection->PrepareDataCommand(select_key_cmd);
             selectCommand->BindString(1, label.c_str());
 
             if (selectCommand->Step()) {
-                key = selectCommand->GetColumnBlob(0);
+                return RawBufferOptional(
+                        selectCommand->GetColumnBlob(0));
             } else {
-                return KEY_MANAGER_API_ERROR_DB_ALIAS_UNKNOWN;
+                return RawBufferOptional();
             }
 
-            AssertMsg(!selectCommand->Step(),
-                "Select returned multiple rows for unique column.");
-
         } Catch (SqlConnection::Exception::InvalidColumn) {
             LogError("Select statement invalid column error");
-            return KEY_MANAGER_API_ERROR_DB_ERROR;
         } Catch (SqlConnection::Exception::SyntaxError) {
             LogError("Couldn't prepare insert key statement");
-            return KEY_MANAGER_API_ERROR_DB_ERROR;
         } Catch (SqlConnection::Exception::InternalError) {
             LogError("Couldn't execute insert statement");
-            return KEY_MANAGER_API_ERROR_DB_ERROR;
         }
-        return KEY_MANAGER_API_SUCCESS;
+        ThrowMsg(DBCrypto::Exception::InternalError,
+                "Couldn't get key for label " << label);
     }
-    int DBCrypto::deleteKey(const std::string& label) {
-        if (!m_init)
-            return KEY_MANAGER_API_ERROR_DB_ERROR;
 
+    void DBCrypto::deleteKey(const std::string& label) {
         Try {
             SqlConnection::DataCommandAutoPtr deleteCommand =
                     m_connection->PrepareDataCommand(delete_key_cmd);
             deleteCommand->BindString(1, label.c_str());
-
             deleteCommand->Step();
-
+            return;
         } Catch (SqlConnection::Exception::SyntaxError) {
             LogError("Couldn't prepare insert key statement");
-            return KEY_MANAGER_API_ERROR_DB_ERROR;
         } Catch (SqlConnection::Exception::InternalError) {
             LogError("Couldn't execute insert statement");
-            return KEY_MANAGER_API_ERROR_DB_ERROR;
         }
-        return KEY_MANAGER_API_SUCCESS;
+        ThrowMsg(DBCrypto::Exception::InternalError,
+                "Couldn't delete key for label " << label);
     }
 
 } // CKM
index 6eaaf26..79af190 100644 (file)
 namespace CKM {
     class DBCrypto {
          public:
-            DBCrypto() : m_connection(NULL), m_init(false) {};
+            typedef boost::optional<DBRow> DBRowOptional;
+            typedef boost::optional<RawBuffer> RawBufferOptional;
+            class Exception
+            {
+              public:
+                DECLARE_EXCEPTION_TYPE(CKM::Exception, Base)
+                DECLARE_EXCEPTION_TYPE(Base, AliasExists)
+                DECLARE_EXCEPTION_TYPE(Base, InternalError)
+            };
+            DBCrypto() : m_connection(NULL) {};
             //user name instead of path?
             DBCrypto(const std::string &path, const RawBuffer &rawPass);
             DBCrypto(const DBCrypto &other) = delete;
@@ -47,44 +56,43 @@ namespace CKM {
 
             virtual ~DBCrypto();
 
-            bool isInit() {return m_init;};
-            int saveDBRow(const DBRow &row);
-            int getDBRow(
+            void saveDBRow(const DBRow &row);
+            DBRowOptional getDBRow(
                     const Alias &alias,
                     const std::string &label,
-                    DBDataType type,
-                    DBRow &row);
-            int getKeyDBRow(
+                    DBDataType type);
+            DBRowOptional getKeyDBRow(
                     const Alias &alias,
-                    const std::string &label,
-                    DBRow &row);
-            int getAliases(
+                    const std::string &label);
+            void getAliases(
                     DBDataType dataType,
                     const std::string &label,
                     AliasVector &aliases);
-            int getKeyAliases(
+            void getKeyAliases(
                     const std::string &label,
                     AliasVector &aliases);
-            int deleteDBRow(
+            void deleteDBRow(
                     const Alias& alias,
                     const std::string &label);
 
-            int saveKey(const std::string& label, const RawBuffer &key);
-            int getKey(const std::string& label, RawBuffer &key);
-            int deleteKey(const std::string& label);
+            void saveKey(const std::string& label, const RawBuffer &key);
+            RawBufferOptional getKey(
+                    const std::string& label);
+            void deleteKey(const std::string& label);
 
          private:
             DB::SqlConnection* m_connection;
-            bool m_init;
 
             void initDatabase();
             DBRow getRow(const DB::SqlConnection::DataCommandAutoPtr &selectCommand);
-            void createTable(const char* create_cmd);
+            void createTable(
+                    const char *create_cmd,
+                    const char *table_name);
             bool checkAliasExist(
                     const std::string &alias,
                     const std::string &label);
             bool checkGlobalAliasExist(const std::string& alias);
-            int getSingleType(
+            void getSingleType(
                     DBDataType type,
                     const std::string& label,
                     AliasVector& aliases);