-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcatalog_manager.cpp
More file actions
executable file
·96 lines (74 loc) · 3.19 KB
/
catalog_manager.cpp
File metadata and controls
executable file
·96 lines (74 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include "catalog_manager.h"
void CatalogManager::createTableInfo(const TableInfo& tableInfo) {
std::ofstream tableListFile("/Users/jason/Desktop/tableList.txt", std::ios::app); // append
std::string tableName = tableInfo.getName();
tableListFile << tableName << std::endl;
tableListFile.close();
writeTableInfo(tableInfo);
}
void CatalogManager::dropTableInfo(std::string tableName) {
// delete the table from the table list first
std::ifstream tableListInFile("/Users/jason/Desktop/tableList.txt");
std::string tmpStr;
std::vector<std::string> tableList;
while( std::getline(tableListInFile, tmpStr) ) {
if( tmpStr != tableName ) {
tableList.push_back(tmpStr);
}
}
tableListInFile.close();
std::ofstream tableListOutFile("/Users/jason/Desktop/tableList.txt", std::ios::trunc);
std::vector<std::string>::iterator it;
for( it = tableList.begin(); it != tableList.end(); it++ ) {
tableListOutFile << *it;
}
tableListOutFile.close();
// delete table info
tmpStr = tableName+".table";
//system(tmpStr.c_str());
remove(tmpStr.c_str());
}
TableInfo CatalogManager::getTableInfo(const std::string& tableName) {
std::string tmpStr = "/Users/jason/Desktop/"+tableName+".table";
std::ifstream tableFile(tmpStr.c_str());
TableInfo tableInfo;
bool isFirstLine = true;
std::string attrName;
int typeId;
int strLen;
bool isUnique;
bool isPrimaryKey;
bool hasIndex;
while( std::getline(tableFile, tmpStr) ) {
std::cout<<tmpStr<<std::endl;
std::stringstream tmpLineStream(tmpStr);
if( isFirstLine ) {
tableInfo.setName(tmpStr);
isFirstLine = false;
} else {
tmpLineStream >> attrName >> typeId >> strLen >> isUnique >> isPrimaryKey >> hasIndex;
AttrInfo attrInfo(attrName, typeId, strLen, isUnique, isPrimaryKey, hasIndex);
tableInfo.addAttr(attrName, attrInfo);
}
}
tableFile.close();
return tableInfo;
}
void CatalogManager::writeTableInfo(const TableInfo& tableInfo) {
std::string tableName = tableInfo.getName();
std::string tmpStr = "/Users/jason/Desktop/"+tableName+".table";
std::ofstream tableFile(tmpStr.c_str());
// write table name
tableFile << tableInfo.getName() << std::endl;
// write attribute info
std::map<std::string, AttrInfo>::iterator it;
std::map<std::string, AttrInfo> attrs = tableInfo.getAllAttrs();
std::vector<std::string> attr_names = tableInfo.getAttrNames();
std::vector<std::string>::iterator it_names;
for( /*it = attrs.begin(); it != attrs.end(); it++ */
it_names = attr_names.begin(); it_names!=attr_names.end(); it_names++) {
it = attrs.find(it_names->data());
tableFile << it->second.getName() << " " << it->second.getTypeId() << " " << it->second.getStrLen() << " " << it->second.getIsUnique() << " " << it->second.getIsPrimaryKey() << " " << it->second.getHasIndex() << " " << std::endl;
}
tableFile.close();
}