int retCode = CKM_API_SUCCESS;
if (0 < m_userDataMap.count(cred.uid)) {
- // check if the data exists or not
- DBCrypto::DBRowOptional row_optional;
- if (dataType == DBDataType::CERTIFICATE || dataType == DBDataType::BINARY_DATA) {
- row_optional = m_userDataMap[cred.uid].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)))
- {
- row_optional = m_userDataMap[cred.uid].database.getKeyDBRow(alias, cred.smackLabel);
- } else {
- LogError("Unknown type of requested data" << (int)dataType);
- retCode = CKM_API_ERROR_BAD_REQUEST;
- }
- if(!row_optional) {
- LogError("No row for given alias, label and type");
- retCode = CKM_API_ERROR_DB_ALIAS_UNKNOWN;
- }
-
- // remove if the data exists
- if(retCode == CKM_API_SUCCESS) {
- Try {
- m_userDataMap[cred.uid].database.deleteDBRow(alias, cred.smackLabel);
- } Catch (CKM::Exception) {
- LogError("Error in deleting row!");
- retCode = CKM_API_ERROR_DB_ERROR;
+ Try {
+ auto erased = m_userDataMap[cred.uid].database.deleteDBRow(alias, cred.smackLabel);
+ // check if the data existed or not
+ if(!erased) {
+ LogError("No row for given alias and label");
+ retCode = CKM_API_ERROR_DB_ALIAS_UNKNOWN;
}
+ } Catch (CKM::Exception) {
+ LogError("Error in deleting row!");
+ retCode = CKM_API_ERROR_DB_ERROR;
}
} else {
retCode = CKM_API_ERROR_DB_LOCKED;
// 1
"SELECT label FROM CKM_TABLE WHERE alias=? AND restricted=0;";
+ const char *select_count_rows_cmd =
+ // 1 2
+ "SELECT COUNT(alias) FROM CKM_TABLE WHERE alias=? AND label=?;";
+
const char *select_key_alias_cmd =
// 1
"SELECT * FROM CKM_TABLE WHERE alias=?"
"Couldn't get key aliases for label " << label);
}
- void DBCrypto::deleteDBRow(
+ int DBCrypto::countRows(const Alias &alias, const std::string &label) {
+ SqlConnection::DataCommandUniquePtr checkCmd =
+ m_connection->PrepareDataCommand(select_count_rows_cmd);
+ checkCmd->BindString(1, alias.c_str());
+ checkCmd->BindString(2, label.c_str());
+ if(checkCmd->Step()) {
+ return checkCmd->GetColumnInteger(0);
+ } else {
+ LogDebug("Row does not exist for alias=" << alias << "and label=" << label);
+ return 0;
+ }
+ }
+ int DBCrypto::deleteDBRow(
const Alias &alias,
const std::string &label)
{
Try {
Transaction transaction(this);
- SqlConnection::DataCommandUniquePtr deleteCommand =
- m_connection->PrepareDataCommand(delete_alias_cmd);
- deleteCommand->BindString(1, alias.c_str());
- deleteCommand->BindString(2, label.c_str());
- deleteCommand->Step();
- transaction.commit();
- return;
+ unsigned int count;
+ if((count = countRows(alias, label)) > 0) {
+ SqlConnection::DataCommandUniquePtr deleteCommand =
+ m_connection->PrepareDataCommand(delete_alias_cmd);
+ deleteCommand->BindString(1, alias.c_str());
+ deleteCommand->BindString(2, label.c_str());
+ deleteCommand->Step();
+ transaction.commit();
+ }
+ return count;
} Catch (SqlConnection::Exception::SyntaxError) {
LogError("Couldn't prepare delete statement");
} Catch (SqlConnection::Exception::InternalError) {
void getKeyAliases(
const std::string &label,
AliasVector &aliases);
- void deleteDBRow(
+ int deleteDBRow(
const Alias& alias,
const std::string &label);
const std::string &alias,
const std::string &label);
bool checkGlobalAliasExist(const std::string& alias);
+ int countRows(
+ const std::string &alias,
+ const std::string &label);
void getSingleType(
DBDataType type,
const std::string& label,
alias_duplicate.dataSize = alias_duplicate.data.size();
BOOST_REQUIRE_THROW(db.saveDBRow(alias_duplicate), DBCrypto::Exception::AliasExists);
- BOOST_REQUIRE_NO_THROW(db.deleteDBRow("alias", "label"));
+ unsigned int erased;
+ BOOST_REQUIRE_NO_THROW(erased = db.deleteDBRow("alias", "label"));
+ BOOST_REQUIRE_MESSAGE(erased > 0, "Inserted row didn't exist in db");
DBCrypto::DBRowOptional row_optional;
BOOST_REQUIRE_NO_THROW(row_optional = db.getDBRow("alias", "label", DBDataType::BINARY_DATA));