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"); } } }
//--------------------------------------------------------------------------- void Table::DropIndexByName(char *desc) { IndexField *indx = GetIndexByName(desc); if (!stricmp(desc, "None")) return; if (indx) DropIndex(indx); }
//--------------------------------------------------------------------------- void Table::DropIndexById(unsigned char Id) { if (!IndexList) return; if (Id == (unsigned char)-1) return; IndexField *indx=(IndexField *)IndexList->GetField(Id); if (indx) DropIndex(indx); }
void API::Switcher(SqlCommand* command){ SqlCommandType command_type = command->command_type(); Info info; // typedef std::chrono::high_resolution_clock Clock; // typedef std::chrono::milliseconds milliseconds; // Clock::time_point t0 = Clock::now(); switch(command_type){ case kSqlInvalid: { info = Info("Invalid Command, Please check your syntax."); break; } case kSqlCreateTable: { info = CreateTable(dynamic_cast<SqlCommandCreateTable *>(command)); break; } case kSqlCreateIndex: { info = CreateIndex(dynamic_cast<SqlCommandCreateIndex *>(command)); break; } case kSqlDeleteFrom: { info = DeleteFrom(dynamic_cast<SqlCommandDeleteFrom *>(command)); break; } case kSqlDropTable: { info = DropTable(dynamic_cast<SqlCommandDropTable *>(command)); break; } case kSqlDropIndex: { info = DropIndex(dynamic_cast<SqlCommandDropIndex *>(command)); break; } case kSqlInsertInto: { info = InsertInto(dynamic_cast<SqlCommandInsertInto *>(command)); break; } case kSqlSelectFrom: { info = SelectFrom(dynamic_cast<SqlCommandSelectFrom *>(command));break; } } info.PrintInfo(); // Clock::time_point t1 = Clock::now(); // milliseconds ms = std::chrono::duration_cast<milliseconds>(t1 - t0); // std::cout<<"time used:"<<ms.count()<<" ms."<<std::endl; }
int main(void){ int i, j, tmp; const int size = 10; vector<int>::iterator vi; Address a; TKey ans; BaseNode *p = NULL; int data[size] = {5, 2, 7, 1, 9, 4, 0, 6, 3, 8}; vector<int> v(data, data+size); vector<int> vn; time_t t_start, t_end; // Initialize for(i = 0; i < BlockNum; i++) blockUsed[i] = false; for(i = 0; i < IndexNum; i++) indexRootBlockId[i] = -1; for(i = 0; i < IndexNum; i++) indexDataBlockId[i] = -1; for(i = 0; i < IndexNum; i++) indexType[i] = TYPE_INT; for(i = 0; i < DATA_NUM; i++) vn.push_back(i); srand((unsigned int)time(0)); for(i = 0; i < DATA_NUM; i++){ j = rand() % DATA_NUM; tmp = vn.at(j); vn.erase(vn.begin() + j); vn.push_back(tmp); } /*for(i = 0; i < DATA_NUM; i++){ cout << vn.at(i) << ' '; } system("pause"); return 0;*/ // test try{ cout << "----------------insert " << DATA_NUM << " numbers----------------------" << endl; time(&t_start); //for(i = 0; i < 15; i++){ //for(i = DATA_NUM; i >= 0; i--){ for(vi = vn.begin(); vi != vn.end(); vi++){ i = *vi; if(!(i % (DATA_NUM / PRINT_NUM))) cout << "-------------insert " << i << "---------------" << endl; try{ a = InsertIndex(0, 0, TKey(i)); } catch(ErKeyNotFound &e){ cout << e.what() << e.key() << endl; } catch(DbEr &e){ cout << e.what() << endl; } //p = (BaseNode *)GetBlock(a.blockId); //cout << "Insert Data Block: " << a.blockId << endl; //p->data[a.offset] = i; //PrintIndex(GetRootBlockId(0), p); //PrintCatalog(); } time(&t_end); cout << "Run time for insert " << DATA_NUM << " data: " << t_end - t_start << "s" << endl; //PrintData(0, p); //PrintIndex(GetRootBlockId(0), p); cout << "----------------query " << DATA_NUM << " numbers----------------------" << endl; time(&t_start); //for(i = 0; i <= DATA_NUM; i++){ for(vi = vn.begin(); vi != vn.end(); vi++){ i = *vi; if(!(i % (DATA_NUM / PRINT_NUM))) cout << "-------------query " << i << "----------------" << endl; try{ a = FindIndex(0, 0, TKey(i)); p = (BaseNode *)GetBlock(a.blockId); ans = TKey(p->Key(a.offset), p->type); if(ans.iKey != i){ throw ErKeyNotFound(TKey(i)); } } catch(ErKeyNotFound &e){ cout << e.what() << e.key() << endl; } catch(DbEr &e){ cout << e.what() << endl; } //PrintIndex(GetRootBlockId(0), p); //PrintCatalog(); } time(&t_end); cout << "Run time for query " << DATA_NUM << " data: " << t_end - t_start << "s" << endl; PrintCatalog(); cout << "----------------delete " << DATA_NUM << " numbers----------------------" << endl; time(&t_start); //for(i = 0; i <= DATA_NUM; i++){ for(vi = vn.begin(); vi != vn.end(); vi++){ i = *vi; if(!(i % (DATA_NUM / PRINT_NUM))) cout << "-------------delete " << i << "---------------" << endl; try{ DeleteIndex(0, 0, TKey(i)); } catch(ErKeyNotFound &e){ cout << e.what() << e.key() << endl; } catch(DbEr &e){ cout << e.what() << endl; } //PrintIndex(GetRootBlockId(0), p); //PrintData(GetDataBlockId(0), p); //PrintCatalog(); } time(&t_end); cout << "Run time for delete " << DATA_NUM << " data: " << t_end - t_start << "s" << endl; PrintData(GetDataBlockId(0), p); DropIndex(0, 0); PrintCatalog(); /*for(i = 0; i < 15; i++){ a = FindIndex(0, i); cout << "Find \"" << i << "\" in offset " << a.offset << " of Block " << a.blockId << endl; }*/ } catch(ErKeyNotFound &e){ cout << e.what() << e.key() << endl; } catch(DbEr &e){ cout << e.what() << endl; } system("pause"); }