void test_insert_record() { int fileID; FileManager* fm = new FileManager(); fm->openFile("testfile.txt", fileID); //打开文件,fileID是返回的文件id RecordManager* test = new RecordManager(fm); test->load_table_info(fileID); vector<string> newRecord, newRecord2, newRecord3; newRecord.push_back("106001"); newRecord.push_back("'CHAD CABELLO'"); newRecord.push_back("'F'"); newRecord2.push_back("106002"); newRecord2.push_back("'CHAD CABELLO'"); newRecord2.push_back("'M'"); newRecord3.push_back("106003"); newRecord3.push_back("'CHAD CABELLO'"); newRecord3.push_back("'F'"); vector<int> nulls; nulls.push_back(1); nulls.push_back(0); nulls.push_back(1); test->insert_record(fileID, newRecord, nulls); test->insert_record(fileID, newRecord2, nulls); test->insert_record(fileID, newRecord3, nulls); // for (int i = 0; i < 50000; i++) { // if (i%100 == 0) // cout << i << endl; // test->insert_record(fileID, newRecord); // } test->print_all_record(fileID); }
void API::Insert(SQLInsert& statement) { if (current_db_.length() == 0) throw NoDatabaseSelectedException(); Database *db = catalog_m_->GetDB(current_db_); if (db == NULL) throw DatabaseNotExistException(); RecordManager *rm = new RecordManager(catalog_m_, buffer_m_, current_db_); rm->Insert(statement); delete rm; }
void API::Delete(SQLDelete& statement) { if (current_db_.length() == 0) throw NoDatabaseSelectedException(); Table *tb = catalog_m_->GetDB(current_db_)->GetTable(statement.get_tb_name()); if (tb == NULL) throw TableNotExistException(); RecordManager *rm = new RecordManager(catalog_m_, buffer_m_, current_db_); rm->Delete(statement); delete rm; }
void test_create_table() { int fileID; FileManager* fm = new FileManager(); fm->openFile("testfile.txt", fileID); //打开文件,fileID是返回的文件id RecordManager* test = new RecordManager(fm); int attr_num = 3; int attr_len[9] = {1, 10, 1, 0, 25, 0, 0, 1, 1}; int primary_key = 0; vector<string> attr_name; attr_name.push_back("id"); attr_name.push_back("name"); attr_name.push_back("gender"); test->init(fileID, attr_num, attr_len, primary_key, attr_name); test->load_table_info(fileID); }
void xyzsql_process_insert(insert_stmt *s ) { if ( s == NULL ) s = dynamic_cast<insert_stmt *>(stmt_queue.front().second); auto t = catm.exist_relation(s->table_name)->cols; if (verify_validation(s->values, t) == false) throw invalid_argument("Uncapatable values"); auto table_info = catm.exist_relation(s->table_name); Record r(*(s->values), table_info->cols); for(auto x : *(r.table_info)) { if(x->flag & (table_column::unique_attr | table_column::primary_attr)) { string filename; indexIterator cursor; int asdf = IndexManager.selectNode(cursor, s->table_name + "/index_" + x->name + ".db", condition::EQUALTO, r.get_value(x).to_str(x->data_type)); if (asdf == 0) throw invalid_argument("Unique Key already exists."); } } int blockNum, offset; RecordManager.insertRecord(s->table_name, r, blockNum, offset); table_info->inc_size(); for(auto x : *(r.table_info)) { if(x->flag & (table_column::unique_attr | table_column::primary_attr)) { IndexManager.insertNode(s->table_name + "/index_" + x->name + ".db", r.get_value(x->name).to_str(x->data_type) , blockNum, offset); } } }
void test_find_attr() { int fileID; FileManager* fm = new FileManager(); fm->openFile("testfile.txt", fileID); //打开文件,fileID是返回的文件id RecordManager* test = new RecordManager(fm); test->load_table_info(fileID); vector<string> rtn = test->find_attr(fileID, "hehe"); for (int i = 0; i < rtn.size(); i++) { cout << rtn[i] << endl; } vector<string> rtn2 = test->find_attr(fileID, "name"); for (int i = 0; i < rtn2.size(); i++) { cout << rtn2[i] << endl; } }
/*删除数据*/ void API::Delete(SQLDelete& sql_statement) { if (current_database_.length() == 0)/*判断当前数据库是否选定*/ { throw NoDatabaseSelectedException(); } Table *tb = catalog_manager_->GetDB(current_database_)->GetTable(sql_statement.get_tb_name()); if (tb == NULL)/*判断当前数据库是否存在*/ { throw TableNotExistException(); } RecordManager *rm = new RecordManager(catalog_manager_, buffer_manager_, current_database_); rm->Delete(sql_statement); delete rm; //cout << "删除数据" << endl; }
/*插入数据*/ void API::Insert(SQLInsert& sql_statement) { if (current_database_.length() == 0)/*判断当前数据库是否选定*/ { throw NoDatabaseSelectedException(); } Database *db = catalog_manager_->GetDB(current_database_); if (db == NULL)/*判断当前数据库是否存在*/ { throw DatabaseNotExistException(); } RecordManager *rm = new RecordManager(catalog_manager_, buffer_manager_, current_database_); rm->Insert(sql_statement); delete rm; //cout << "插入数据。"; }
/*join查询*/ void API::JoinSelect(SQLJoinSelect& sql_statement) { if (current_database_.length() == 0)/*判断当前数据库是否选定*/ { throw NoDatabaseSelectedException(); } int table_count = sql_statement.get_table_names().size(); for (int i = 0; i < table_count; i++) { Table *tb = catalog_manager_->GetDB(current_database_)->GetTable(sql_statement.get_table_names()[i]); if (tb == NULL) throw TableNotExistException(); } RecordManager *rm = new RecordManager(catalog_manager_, buffer_manager_, current_database_); rm->JoinSelect(sql_statement); delete rm; }
void test_update_record() { int fileID; FileManager* fm = new FileManager(); fm->openFile("testfile.txt", fileID); //打开文件,fileID是返回的文件id RecordManager* test = new RecordManager(fm); test->load_table_info(fileID); test->print_all_record(fileID); test->update_record(fileID, 0, "id", "106002", 0); test->update_record(fileID, 1, "name", "'wangsu'", 1); if (test->update_record(fileID, 2, "heheh", "21331312111", 1)) { cout << "wrong update" << endl; } else { cout << "success update" << endl; } test->print_all_record(fileID); }
void xyzsql_process_create_table(create_table_stmt *s ) { cout << "table created." << endl; if ( s == NULL ) { s = dynamic_cast<create_table_stmt *>(stmt_queue.front().second); for ( auto x : *(s->cols) ) { x->name = s->name + "." + x->name; } } catm.add_relation(s); catm.write_back(); RecordManager.createMaster(s->name); for(auto x : *(s->cols)) { if(x->flag & (table_column::unique_attr | table_column::primary_attr)) { IndexManager.createIndex(s->name + "/index_" + x->name + ".db", data_type_to_str(x->data_type), x->str_len, 0, {}, {}, {}); } } }
void calc_algric_tree(algbric_node *root) { if (root->flag == true) return; string table_name; int blockNum, offset; auto new_col_list = new vector<table_column *>, old_col_list = new_col_list, old_col_list2 = new_col_list; // auto old_col_list = catm.exist_relation((root->left->table))->cols; switch ( root->op ) { case algbric_node::DIRECT : root->flag = true; return; case algbric_node::PROJECTION : { if (!root->left->flag) calc_algric_tree(root->left); old_col_list = catm.exist_relation((root->left->table))->cols; for( auto x : *(root->projection_list) ) { auto att = catm.exist_relation(x->relation_name)->get_column(x->attribute_name); new_col_list->push_back(new table_column((root->left->op == algbric_node::DIRECT ? x->attribute_name.c_str() : x->full_name.c_str()), att->data_type, att->str_len, 0 )); } table_name = create_temp_table(new_col_list); root->table = table_name; auto cursor = RecordManager.getCursor(root->left->table, catm.calc_record_size(root->left->table)); while (cursor->next()) { Record r = cursor->getRecord(); vector<record_value> result; for(auto i = new_col_list->begin(); i != new_col_list->end(); i++) { for(auto j = old_col_list->begin(); j != old_col_list->end(); j++ ) { if ( (*i)->name == (*j)->name ) { result.push_back(r.values[j-old_col_list->begin()]); } } } RecordManager.insertRecord(table_name, Record(result, new_col_list), blockNum, offset); } if (root->left->op != algbric_node::DIRECT) catm.drop_table(root->left->table); root->flag = true; return; } case algbric_node::SELECTION : { string left_name = root->left->table; old_col_list = catm.exist_relation(left_name)->cols; for( auto x : *(old_col_list) ) { new_col_list->push_back( new table_column(x->name.c_str(), x->data_type, x->str_len, x->flag )); } table_name = create_temp_table(new_col_list); root->table = table_name; condition *p = NULL, *eq = NULL; for(auto x : (root->conditions)) { if ( catm.is_indexed(x->left_attr) ) { p = x; if (x->op == condition::EQUALTO) eq = x; } } if ( eq != NULL ) p = eq; if ( p != NULL ) { cout << "Index used: " << p->left_attr->full_name << endl; int record_size = catm.calc_record_size(root->left->table); auto t = p->left_attr; indexIterator cursor; int asdf = IndexManager.selectNode(cursor, t->relation_name + "/index_" + t->attribute_name + ".db", p->op, (p->v).to_str(catm.get_data_type(t))); if ( asdf == 0 ) { int b = 0, c = 0; while (cursor.next(b, c) == 0) { Record a = RecordManager.getRecord(t->relation_name, b, c, record_size); if (calc_conditions(&(root->conditions), a)) RecordManager.insertRecord(table_name, a, blockNum, offset); } } } else { string t = root->left->table; int record_size = catm.calc_record_size(t); indexIterator cursor; int asdf = IndexManager.getStarter(cursor, t + "/index_" + catm.get_primary(t) + ".db"); if ( asdf == 0 ) { int b = 0, c = 0; while (cursor.next(b, c) == 0) { Record a = RecordManager.getRecord(t, b, c, record_size); if (calc_conditions(&(root->conditions), a)) RecordManager.insertRecord(table_name, a, blockNum, offset); } } } root->flag = true; return; } case algbric_node::JOIN : { if (!root->left->flag) calc_algric_tree(root->left); if (!root->right->flag) calc_algric_tree(root->right); if ( catm.get_size(root->right->table) < catm.get_size(root->left->table) ) { auto tmp = root->left; root->left = root->right; root->right = tmp; } old_col_list = catm.exist_relation((root->left->table))->cols; old_col_list2= catm.exist_relation((root->right->table))->cols; for( auto x : *old_col_list ) { new_col_list->push_back(new table_column(x->name.c_str(), x->data_type, x->str_len, 0 )); } for( auto x : *old_col_list2 ) { new_col_list->push_back(new table_column(x->name.c_str(), x->data_type, x->str_len, 0 )); } table_name = create_temp_table(new_col_list); root->table = table_name; auto outter_table = catm.exist_relation(root->left->table), inner_table = catm.exist_relation(root->right->table); int outter_size = catm.calc_record_size(root->left->table), inner_size = catm.calc_record_size(root->right->table); outter_table->get_size(); condition * p = NULL; for ( auto x : root->conditions ) { if ( outter_table->get_column(x->left_attr->full_name) != NULL && inner_table->get_column(x->right_attr->full_name) != NULL) { } else if ( inner_table->get_column(x->left_attr->full_name) != NULL && outter_table->get_column(x->right_attr->full_name) != NULL) { auto tmp = x->left_attr; x->left_attr = x->right_attr; x->right_attr = tmp; } assert( outter_table->get_column(x->left_attr->full_name) != NULL && inner_table->get_column(x->right_attr->full_name) != NULL); if ( inner_table->is_indexed(x->right_attr->full_name) ) { p = x; } } auto cursor1 = RecordManager.getCursor(root->left->table, outter_size); cout << "Index used: " << p->right_attr->full_name << endl; while (cursor1->next()) { Record r1 = cursor1->getRecord(); if ( p ) { // nested-index join indexIterator a; int asdf = IndexManager.getStarter(a, root->right->table + "/index_" + p->right_attr->full_name + ".db"); if (asdf == 0) { int b = 0, c = 0; while (a.next(b, c) == 0) { Record r2 = RecordManager.getRecord(root->right->table, b, c, inner_size); if ( calc_conditions(&(root->conditions), r1, r2) ) { vector<record_value> result(r1.values); result.insert(result.end(), r2.values.begin(), r2.values.end()); RecordManager.insertRecord(table_name, Record(result, new_col_list), blockNum, offset); } } } } else { // nested-loop join auto cursor2 = RecordManager.getCursor(root->right->table, inner_size); while (cursor2->next()) { Record r2 = cursor2->getRecord(); if ( calc_conditions(&(root->conditions), r1, r2) ) { vector<record_value> result(r1.values); result.insert(result.end(), r2.values.begin(), r2.values.end()); RecordManager.insertRecord(table_name, Record(result, new_col_list), blockNum, offset); } } delete cursor2; } } delete cursor1; if (root->right->op != algbric_node::DIRECT) catm.drop_table(root->right->table); if (root->left->op != algbric_node::DIRECT) catm.drop_table(root->left->table); root->flag = true; return; } } }
void xyzsql_process_delete() { auto s = dynamic_cast<delete_stmt *>(stmt_queue.front().second); if (s->condition_list->empty()) { // delete all system(("rm " + s->table_name + "/*.db").c_str()); RecordManager.createMaster(s->table_name); auto table_info = catm.exist_relation(s->table_name); auto cols = table_info->cols; table_info->set_size(0); for(auto x : *cols) if(x->flag & (table_column::unique_attr | table_column::primary_attr)) IndexManager.createIndex(s->table_name + "/index_" + x->name + ".db", data_type_to_str(x->data_type), x->str_len, 0, {}, {}, {}); } else { auto table_info = catm.exist_relation(s->table_name); int record_size = catm.calc_record_size(s->table_name); BufferManager.newTrashCan(); // unique condition *p = NULL, *eq = NULL; for(auto x : *(s->condition_list)) { if ( catm.is_indexed(x->left_attr) ) { p = x; if (x->op == condition::EQUALTO) eq = x; } } if ( eq != NULL || p != NULL ) { cout << "Index used: " << p->left_attr->full_name << endl; auto t = eq == NULL ? p->left_attr : eq->left_attr; if (eq != NULL) p = eq; indexIterator a; int asdf = IndexManager.selectNode(a, base_addr + "/" + t->relation_name + "/index_" + t->attribute_name + ".db", p->op, (p->v).to_str(catm.get_data_type(t))); if ( asdf == 0 ) { int b = 0, c = 0; while (a.next(b, c) == 0) { Record a = RecordManager.getRecord(t->relation_name, b, c, record_size); if (calc_conditions(s->condition_list, a)) BufferManager.appendTrashCan(b, c); } } } else { indexIterator a; int asdf = IndexManager.getStarter(a, s->table_name + "/index_" + catm.get_primary(s->table_name) + ".db"); if (asdf == 0) { int b = 0, c = 0; while (a.next(b, c) == 0) { Record a = RecordManager.getRecord(s->table_name, b, c, record_size); if (calc_conditions(s->condition_list, a)) BufferManager.appendTrashCan(b, c); } } } BufferManager.beginFetchTrash(); int blockNum, offset; while(BufferManager.fetchTrash(blockNum, offset)) { auto r = RecordManager.getRecord(s->table_name, blockNum, offset, record_size); for(auto x : *(r.table_info)) { if(x->flag & (table_column::unique_attr | table_column::primary_attr)) { indexIterator cursor; IndexManager.deleteNode(s->table_name + "/index_" + x->name + ".db", r.get_value(x).to_str(x->data_type)); } } RecordManager.deleteRecord(s->table_name, blockNum, offset, record_size); table_info->dec_size(); } } cout << "records deleted." << endl; }
void xyzsql_process_select() { cout << "select parsed." << endl; auto s = dynamic_cast<select_stmt *>(stmt_queue.front().second); // checker // push selection vector< algbric_node * > leaf_nodes; for(auto x : *(s->table_list)) { // add to select queue algbric_node *direct = new algbric_node(algbric_node::DIRECT); direct->table = *x; algbric_node *select = new algbric_node(algbric_node::SELECTION); for ( auto y : *(s->condition_list) ) { if ( y->flag == false && y->left_attr->relation_name == *(x) ) { select->conditions.push_back(y); } } if (select->conditions.empty()) { direct->flag = true; leaf_nodes.push_back(direct); delete select; } else { select->left = direct; calc_algric_tree(select); leaf_nodes.push_back(select); } } algbric_node *root = NULL; set<string> rel_set; while (!leaf_nodes.empty()) { int tmp = 0xFFFFFF; auto label = leaf_nodes.begin(); for(auto i = leaf_nodes.begin(); i != leaf_nodes.end(); i++) { if ( catm.get_size(((*i)->table)) < tmp ) { tmp = catm.get_size(((*i)->table)); label = i; } } if (root == NULL) { root = *label; rel_set.insert(root->op == algbric_node::DIRECT ? (root->table) : (root->left->table)); } else { auto tmp = new algbric_node(algbric_node::JOIN); tmp->left = root; tmp->right = *label; root = tmp; string right_name = root->right->op == algbric_node::DIRECT ? (root->right->table) : (root->right->left->table); for(auto x : *(s->condition_list)) { if (x->flag == true) { if ( (rel_set.count(x->left_attr->relation_name) && right_name == x->right_attr->relation_name) || (rel_set.count(x->right_attr->relation_name) && right_name == x->left_attr->relation_name) ) { root->conditions.push_back(x); } } } rel_set.insert(right_name); } leaf_nodes.erase(label); } // set root if (!s->projection_list->empty()) { auto tmp = new algbric_node(algbric_node::PROJECTION); tmp->projection_list = s->projection_list; tmp->left = root; root = tmp; } calc_algric_tree(root); auto cursor = RecordManager.getCursor(root->table, catm.calc_record_size(root->table)); while (cursor->next()) { Record t = cursor->getRecord(); auto j = t.table_info->begin(); for (record_value x : t.values) { cout << x.to_str((*j)->data_type) << " "; j++; } cout << endl; } if (root->op != algbric_node::DIRECT) catm.drop_table(root->table); delete root; }
void Interpret::InsertParse(Command & cmd) { Attribute tempAttr; Condition tempCon; string tempRow; int indexStr = 2; m_nOperation = INSERT_ERR; if (cmd.n == indexStr) return; m_tablename = cmd.str[indexStr]; if (!catalog.ExistTable(m_tablename)) { m_nOperation = TABLE_ERR; return; } table_info = catalog.gettableinformation(m_tablename); indexStr++; if (cmd.n == indexStr) return; if (cmd.str[indexStr] != "values") return; indexStr++; if (cmd.n == indexStr) return; if (cmd.str[indexStr] != "(") return; indexStr++; int i = 0; while (cmd.n > indexStr) { int datatype = JudgeDataType(indexStr); if (table_info.attr[i].type == FLOAT && datatype == INT) datatype = FLOAT; if (datatype != table_info.attr[i].type) return; if (datatype == CHARN) { tempRow = cmd.str[indexStr - 1]; if (tempRow.length() > table_info.attr[i].length - 1) return; } else tempRow = cmd.str[indexStr]; data.columns.push_back(tempRow); indexStr++; if (cmd.n == indexStr) return; if (cmd.str[indexStr] == ")") break; if (cmd.str[indexStr] != ",") return; indexStr++; i++; } if (data.columns.size() != table_info.attriNum) { m_nOperation = INSERT_NUM_ERR; return; } for (int i = 0; i<table_info.attriNum; i++) { if (table_info.attr[i].isUnique) { if (table_info.attr[i].isPrimaryKey) { if (PrimaryKeyValue[0] != table_info.name) { Data tableValue = record.select(table_info); PrimaryKeyValue[0] = table_info.name; for (int j = 0; j < tableValue.rows.size(); j++) PrimaryKeyValue[j + 1] = tableValue.rows[j].columns[i]; } for (int j = 0; j < table_info.recordNum;j++) if (PrimaryKeyValue[j + 1] == data.columns[i]) { m_nOperation = PRIMARY_RAID; return; } PrimaryKeyValue[table_info.recordNum+1] = data.columns[i]; } if (table_info.attr[i].isPrimaryKey != 1 && table_info.attr[i].isUnique) { if (UniqueKeyValue[0] != table_info.name) { Data tableValue = record.select(table_info); table_info.recordNum = tableValue.rows.size(); UniqueKeyValue[0] = table_info.name; for (int j = 0; j < tableValue.rows.size(); j++) UniqueKeyValue[j + 1] = tableValue.rows[j].columns[i]; } for (int j = 0; j < table_info.recordNum;j++) if (UniqueKeyValue[j + 1] == data.columns[i]) { m_nOperation = UNIQUE_RAID; return; } UniqueKeyValue[table_info.recordNum+1] = data.columns[i]; } } } indexStr++; if (cmd.n == indexStr) m_nOperation = INSERT; return; }
void system_init() { cout << "System Initialized!" << endl; RecordManager.Init(&BufferManager, &catm, &IndexManager); }
int main(int argc, const char * argv[]) { RecordManager recordManager; // must initialize recordManager.Initialize(); // Table structure definition Table InsT; Attribute a; InsT.attrNumber = 4; a.attrName = "uuid"; a.dataType = Uuid; a.attrType = 1; InsT.attributes.push_back(a); a.attrName = "att1"; a.dataType = Int; a.indexName = "null"; InsT.attributes.push_back(a); a.attrName = "att2"; a.dataType = Int; a.indexName = "null"; InsT.attributes.push_back(a); a.attrName = "att3"; a.dataType = String; a.dataLength = 5; a.indexName = "null"; InsT.attributes.push_back(a); InsT.tableName = "table_test"; InsT.recordNum = 0; InsT.tableNum = 1; Record *rec = new Record; UUID *uuid = new UUID(1); int *aaa = new int(255); int *bbb = new int(0xabcd); string *str = new string("wxyz"); rec->data.push_back(static_cast<void *>(uuid)); rec->data.push_back(static_cast<void *>(aaa)); rec->data.push_back(static_cast<void *>(bbb)); rec->data.push_back(static_cast<void *>(str)); rec->next = NULL; // create a table recordManager.CreateTable(&InsT); // must load tables recordManager.LoadTable(&InsT); uint T = InsT.tableNum; #define N 4 int t; // insert record // let's do a stress testing for (int i=0; i<N; i++) { recordManager.NewQuery(); recordManager.AppendValue(i); recordManager.AppendValue(0xab); recordManager.AppendValue("ops"); //recordManager.ChooseTable(T); // NOTE: optional recordManager.InsertRecord(T); InsT.recordNum++; } cout<<"insert done "<<endl; cin>>t; // recordManager.NewQuery(); // recordManager.ChooseTable(T); // NOTE: choose is mandatory for selection // // recordManager.PushLogicOp("("); // recordManager.PushCondition(T, 1, Equal, 10); // recordManager.PushLogicOp("or"); // recordManager.PushCondition(T, 1, Greater, 11); // recordManager.PushLogicOp(")"); // recordManager.PushLogicOp("and"); // recordManager.PushLogicOp("("); // recordManager.PushCondition(T, 1, Less, 11); // recordManager.PushLogicOp("or"); // recordManager.PushCondition(T, 1, Equal, 11); // recordManager.PushLogicOp(")"); // recordManager.SelectRecord(); recordManager.NewQuery(); recordManager.ChooseTable(T); // NOTE: choose is mandatory for selection recordManager.PushCondition(T, 1, Equal, 2); recordManager.PushLogicOp("or"); recordManager.PushCondition(T, 3, Equal, "ying"); vector<vector<Record*>> results; results = recordManager.SelectRecord(); cout<<"select done, results:"<<endl; for (int i=0; i<results.at(0).size(); i++) { cout<<*((UUID*)results.at(0).at(i)->data.at(0))<<"\t"; } cin>>t; for (int i=10000; i<10002; i++) { // delete record recordManager.NewQuery(); //recordManager.ChooseTable(T); recordManager.PushCondition(T, 0, Equal, i); recordManager.DeleteRecord(T); // should be updated in the catalog InsT.recordNum--; } cout<<"delete done "<<endl; cin>>t; // select * from T recordManager.NewQuery(); recordManager.ChooseTable(T); results = recordManager.SelectRecord(); // cout<<"select * done, results:"<<endl; // for (int i=0; i<results.at(0).size(); i++) { // cout<<*((UUID*)results.at(0).at(i)->data.at(0))<<"\t"; // } // cin>>t; // drop a table //recordManager.DropTable(&InsT); // must call when quit recordManager.OnQuit(); return 0; // TODO merge select and add select from no where #if TEST recordManager.root = new Record; recordManager.root->next = NULL; recordManager.lastRecord = recordManager.root; #endif struct Table tableStruct; { tableStruct.tableName = "test"; tableStruct.attrNumber = 1; tableStruct.recordNum = 0; tableStruct.tableNum = 0; vector<Attribute> attributes; struct Attribute attr; // attr.attrName = "id"; // attr.dataType = String; // attr.dataLength = 10; // attr.attrType = 0; // attr.indexName = "null"; // attributes.push_back(attr); attr.attrName = "num"; attr.dataType = Int; attr.dataLength = 8; attr.attrType = 0; attr.indexName = "null"; attributes.push_back(attr); // attr.attrName = "name"; // attr.dataType = String; // attr.dataLength = 5; // attr.attrType = 0; // attr.indexName = "null"; // attributes.push_back(attr); tableStruct.attributes = attributes; } // uint T = tableStruct.tableNum; string id = "qwerty12"; int num = 10; string name = "abcde"; recordManager.NewQuery(); //recordManager.AppendValue(id); recordManager.AppendValue(num); //recordManager.AppendValue(name); //recordManager.ChooseTable(T); recordManager.InsertRecord(T); return 0; num = 11; recordManager.NewQuery(); //recordManager.AppendValue(id); recordManager.AppendValue(num); //recordManager.AppendValue(name); //recordManager.ChooseTable(T); recordManager.InsertRecord(T); num = 12; recordManager.NewQuery(); //recordManager.AppendValue(id); recordManager.AppendValue(num); //recordManager.AppendValue(name); //recordManager.ChooseTable(T); recordManager.InsertRecord(T); #if TEST recordManager.PrintRecord(T); #endif //recordManager.PrintSingle(recordManager.GetRecord(0, 0)); recordManager.OnQuit(); return 0; /* no longer used test codes uint T = 0; vector<DataType> dataT; dataT.push_back(String); dataT.push_back(Int); dataT.push_back(String); vector<bool> isIndexBuilt; isIndexBuilt.push_back(true); isIndexBuilt.push_back(false); isIndexBuilt.push_back(false); recordManager.SetTableDescriptions(T, dataT, isIndexBuilt); */ /* a table is created as follows: create table T ( id varchar(10) num int, name char(5) ); create table S ( key char(5) value int, ); */ /* uint T = 2; // set the data types of a table // data types can be read from catalog manager vector<DataType> dataType; dataType.clear(); dataType.push_back(String); dataType.push_back(Int); dataType.push_back(String); recordManager.SetTableAttributeDataType(T, dataType); // insert into T values ("qwerty12", 10, "abcde"); string id = "qwerty12"; int num = 10; string name = "abcde"; recordManager.NewQuery(); recordManager.ChooseTable(T); recordManager.AppendValue(id); recordManager.AppendValue(num); recordManager.AppendValue(name); // select T.num,S.key from T,S where T.num<100 and S.key!="k0001" or T.name=S.key recordManager.NewQuery(); uint T_index=2; uint S_index=3; recordManager.ChooseTable(T_index); recordManager.ChooseTable(S_index); recordManager.PushCondition(T_index, 1, Less, 100); recordManager.PushLogicOp(And); recordManager.PushCondition(S_index, 0, NotEqual, "k0001"); recordManager.PushLogicOp(Or); recordManager.PushCondition(T_index, 2, Equal, S_index, 0); */ // delete is much like select, only difference is... return 0; }
void Execute() { int i; int j; int k; Table tableinfor; Index indexinfor; string tempKeyValue; int tempPrimaryPosition=-1; int rowCount=0; Data data; switch(parsetree.m_operation) { case CRETAB: parsetree.getTableInfo.attriNum=parsetree.getTableInfo.attributes.size(); catalog.createTable(parsetree.getTableInfo); record.createTable(parsetree.getTableInfo); cout<<"Table "<<parsetree.getTableInfo.name<<" is created successfully"<<endl; break; case TABLEEXISTED: cout<<"CREATE ERROR: Table existed"<<endl; break; case DRPTAB: record.dropTable(parsetree.getTableInfo); for(int i = 0; i < parsetree.getTableInfo.attriNum; i++){//���������е�index��ɾ�� indexinfor = catalog.getIndexInformation(parsetree.getTableInfo.name, i); if(indexinfor.index_name != "") indexm.dropIndex(indexinfor); } catalog.dropTable(parsetree.getTableInfo); cout<<"Table "<<parsetree.getTableInfo.name<<" is dropped successfully"<<endl; break; case INSERT: tableinfor = parsetree.getTableInfo; if(parsetree.PrimaryKeyPosition==-1&&parsetree.UniquePostion==-1){ record.insertValue(tableinfor, parsetree.row); catalog.update(tableinfor); cout<<"Insert successfully"<<endl; break; } if(parsetree.PrimaryKeyPosition!=-1) { data=record.select(tableinfor, parsetree.condition); if(data.rows.size()>0){ cout<<"INSERT ERROR: Primary key redundancy"<<endl; break; } } if(parsetree.UniquePostion!=-1){ data=record.select(tableinfor, parsetree.UniqueCondition); if(data.rows.size()>0){ cout<<"INSERT ERROR: Unique value redundancy"<<endl; break; } } record.insertValue(tableinfor,parsetree.row); catalog.update(tableinfor); cout<<"Insert successfully"<<endl; break; case INSERTERR: cout << "Syntax ERROR: Incorrect usage of \"insert\"." << endl; break; case SELECT_NOWHERE_CAULSE: tableinfor = parsetree.getTableInfo; data=record.select(tableinfor); if(data.rows.size()!=0) ShowResult( data, tableinfor, parsetree.column); else{ cout << "No data is found." << endl; } break; case SELECT_WHERE_CAULSE: tableinfor=parsetree.getTableInfo; if(parsetree.condition.size()==1){ for(int i=0;i<parsetree.getTableInfo.attributes.size();i++){ /*��*/if((parsetree.getTableInfo.attributes[i].isPrimeryKey==true||parsetree.getTableInfo.attributes[i].isUnique==true)&&parsetree.m_colname==parsetree.getTableInfo.attributes[i].name){ tempPrimaryPosition=i; indexinfor=catalog.getIndexInformation(tableinfor.name,i); break; } } if(tempPrimaryPosition==parsetree.condition[0].columnNum&&parsetree.condition[0].op==Eq&&indexinfor.table_name!=""){ tempKeyValue=parsetree.condition[0].value; data= indexm.selectEqual(tableinfor,indexinfor,tempKeyValue); } else{ data=record.select(tableinfor,parsetree.condition); } } else{ data=record.select(tableinfor,parsetree.condition); } if(data.rows.size()!=0) ShowResult( data, tableinfor, parsetree.column); else{ cout << "No data is found." << endl; } break; case DELETE: rowCount = record.deleteValue(parsetree.getTableInfo,parsetree.condition); cout<< rowCount <<" tuples are deleted."<<endl; break; case CREIND: indexinfor = parsetree.getIndexInfo; tableinfor = parsetree.getTableInfo; if(!tableinfor.attributes[indexinfor.column].isPrimeryKey && !tableinfor.attributes[indexinfor.column].isUnique){//����primary key�������Խ�index cout << "Column " << tableinfor.attributes[indexinfor.column].name <<" is not unique."<< endl; break; } catalog.createIndex(indexinfor); indexm.createIndex(tableinfor, indexinfor); catalog.update(indexinfor); cout<<"Index "<< indexinfor.index_name << "is created successfully."<<endl; break; case INDEXERROR: cout<<"ERROR: Index existed."<<endl; break; case DRPIND: indexinfor = catalog.getIndexInformation(parsetree.m_indname); if(indexinfor.index_name == ""){ cout << "ERROR: Index" << parsetree.m_indname << "does not exist!" << endl; } indexm.dropIndex(indexinfor); catalog.dropIndex(parsetree.m_indname); cout<<"The index is dropped successfully"<<endl; break; case CREINDERR: cout << "Syntax ERROR: Incorrect usage of \"create index\" query." << endl; break; case QUIT: cout << "Bye Bye~" << endl; system("pause"); exit(0); break; case EMPTY: cout << "Query Empty." << endl; break; case UNKNOW: cout << "Syntax ERROR: Please check your query." << endl; break; case SELERR: cout << "Syntax ERROR: Incorrect usage of \"select\" query." << endl; break; case CRETABERR: cout << "Syntax ERROR: Incorrect usage of \"create table\" query." << endl; break; case DELETEERR: cout << "Syntax ERROR: Incorrect usage of \"delete from\" query." << endl; break; case DRPTABERR: cout << "Syntax ERROR: Incorrect usage of \"drop table\" query." << endl; break; case DRPINDERR: cout << "Syntax ERROR: Incorrect usage of \"drop index\" query." << endl; break; case VOIDPRI: cout << "ERROR: Invalid primary key." << endl; break; case VOIDUNI: cout << "ERROR: Invalid unique key." << endl; break; case CHARBOUD: cout << "ERROR: Too long query. Only 1~255 charactors is allowed." << endl; break; case NOPRIKEY: cout << "ERROR: Please define a primary key." << endl; break; case TABLEERROR: cout << "ERROR: Table is not existed."<<endl; break; case INDEXEROR: cout << "ERROR: Index is not existed."<<endl; break; case COLUMNERROR: cout << "ERROR: Column is not existed"<<endl; break; case INSERTNUMBERERROR: cout << "ERROR: The amount of the columns is not matched."<<endl; break; } }