#include "kmsAdapter/dek_manager.hpp"


void DekManager::initialize(const std::string& user_name, const std::string& db_name){
    user_name_ = user_name;
    db_name_ = db_name;
}


// 数据库信息设置

void DekManager::setInfoUser(const std::string &user_name) {
    user_name_ = user_name;
}

void DekManager::setInfoDb(const std::string &db_name) {
    db_name_ = db_name;
}

void DekManager::setInfoTable(const std::string &table_name) {
    table_name_ = table_name;
}

void DekManager::setInfoCol(const std::string &col_name) {
    col_name_.push(col_name);
}


std::string DekManager::getInfoUser() {
    return user_name_;
}

std::string DekManager::getInfoDb() {
    return db_name_;
}

std::string DekManager::getInfoTable() {
    return table_name_;
}

std::string DekManager::getInfoCol() {
    if (!col_name_.empty()) {
        auto tmp = col_name_.front();
        col_name_.pop();
        return tmp;
    }
    return "";
}

void DekManager::setDekTableLevel(const std::string &dek) {
    table_dek_ = dek;
}

void DekManager::getDekTableLevel(std::string &dek) {
    dek = table_dek_;
}


void DekManager::setDekTableLevelForUpdate(const std::string &dek) {
    table_dek_update_ = dek;
}

void DekManager::getDekTableLevelForUpdate(std::string &dek) {
    dek = table_dek_update_;
}

// DEK 缓存管理
void DekManager::setDekColLevel(const std::string &col_name, const std::string &dek_tmp) {
    col_dek_[col_name] = dek_tmp;
}

void DekManager::getDekColLevel(const std::string &col_name, std::string &dek_tmp) {
    if(col_dek_.find(col_name) != col_dek_.end()){
    dek_tmp = col_dek_[col_name];
    }else{
        dek_tmp = table_dek_;
    }

}

void DekManager::setDekColLevelForUpdate(const std::string &col_name, const std::string &dek_tmp) {
    col_dek_update_[col_name] = dek_tmp;
}

void DekManager::getDekColLevelForUpdate(const std::string &col_name, std::string &dek_tmp) {
    if(col_dek_update_.find(col_name) != col_dek_update_.end()){
        dek_tmp = col_dek_update_[col_name];
    }else{
        dek_tmp = table_dek_update_;
    }
}

void DekManager::getAllDekColLevelForUpdate(std::unordered_map<std::string, std::string> &column_deks) {
    column_deks = col_dek_update_;
}

void DekManager::clearDek() {
    col_dek_.clear();
    table_dek_.clear();
    col_name_ = std::queue<std::string>();
}

void DekManager::clearDekForUpdate() {
    col_dek_update_.clear();
    table_dek_update_.clear();
}