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 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 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; } }