Exemplo n.º 1
0
Info API::DropTable(SqlCommandDropTable* command){
  CatalogManager catalog_manager;
  std::string table_name = command->table_name();

  if (!catalog_manager.HasTable(table_name)){
    std::string error_info;
    error_info = "Table \"" + table_name + "\" not exists.";
    return Info(error_info);
  }
  else{
    TableInfo table = catalog_manager.GetTableInfo(table_name);
    
    for (auto it : table.index_names()){
      SqlCommandDropIndex* cmd = new SqlCommandDropIndex(it);
        if(!DropIndex(cmd).is_succeed()){
          return Info("Drop table success but index \"" + it +"\" failed to be dropped");
        }
    }

    if (catalog_manager.DropTable(table_name)){
        //drop index
      return Info();
    }
    else{
      return Info("Drop table Failed");
    }
  }
}
Exemplo n.º 2
0
Info API::InsertInto(SqlCommandInsertInto* command){
  CatalogManager catalog_manager;
  RecordManager record_manager;
  IndexManager index_manager;
  int offset;
  std::string table_name = command->table_name();
  auto values = command->values();
  if (!catalog_manager.HasTable(table_name)){
    std::string error_info;
    error_info = "Table \"" + table_name + "\" not exists.";
    return Info(error_info);
  }
  TableInfo table = catalog_manager.GetTableInfo(table_name);
  if (table.attribute_names_ordered().size()!=values.size()){
    return Info("Number of values not equals to the number of attributes");
  }

  auto ano = table.attribute_names_ordered();

  //Check string length and overflow
  for (int i = 0; i<values.size(); ++i){
    int type = table.attribute(ano.at(i)).type();
    int length = table.attribute(ano.at(i)).length();
    if (type == 0){
      if (!is_int_overflow_notchecked(values.at(i))){
        std::string error_info;
        error_info = "Value \"" + values.at(i)  + "\" is not a int number.";
        return Info(error_info);
      }

      try{
        int v = std::stoi(values.at(i));
      }
      catch(const std::out_of_range& e){
        std::string error_info;
        error_info = "Value \"" + values.at(i)  + "\" is out of range of int.";
        return Info(error_info);
      }
    }
    if (type == 1){
      if (!is_float_overflow_notchecked(values.at(i))&&!is_int_overflow_notchecked(values.at(i))){
        std::string error_info;
        error_info = "Value \"" + values.at(i)  + "\" is not a float number.";
        return Info(error_info);
      }

      try{
        float v = std::stof(values.at(i));
      }
      catch(const std::out_of_range& e){
        std::string error_info;
        error_info = "Value \"" + values.at(i)  + "\" is out of range of float.";
        return Info(error_info);
      }
    }
    if (type == 2){
      if (values.at(i).length() > length){
        std::string error_info;
        error_info = "Value \"" + values.at(i)  + "\" is out of range of char("+ std::to_string(length) +").";
        return Info(error_info);
      }
    }

  }

  if (!CheckUnqiueAndPrimaryKey(table,values)){
    return Info("Violation of uniqueness");
  }


  offset = record_manager.InsertRecord(table,values);
  if (offset == -1){
    return Info("Insert failed");
  }

  //update index
  std::vector<std::string> index_names = table.index_names();

  for (auto it : index_names){
    IndexInfo index_info = catalog_manager.GetIndexInfo(it);
    if(!index_manager.AddRecord(table,index_info,values.at(table.attribute_index(index_info.attribute_name())),offset)){
      return Info("Update index failed.");
    }
  }

  return Info();
}