⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 analysis.cpp

📁 使用C++编写的数据库管理系统; 拥有自定义数据文件格式
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		}
	}

	i=thisTable.addRecord(newValues);
	if(i)	
		cout<<"Data insert failed."<<endl;
	else
		cout<<"Data insert successfully."<<endl;

	
}

void select_record(char * input,int & readPointer,char * word)
{
	SYMBOL w,condition;
	int i=0;
	char tableName[128];
	FIELDNODE * head,*p,* temp;
	FIELD conditionField;
	FIELDREC expect;
	head = new FIELDNODE;
	head->next = 0;
	p=head;
	
	do{
		w=getSym(input,readPointer,word);		
		if(i==0&&w==TIMES)
		{
			w=getSym(input,readPointer,word);
			break;		
		}				
		if(w!=IDENT)
		{
			cout<<"Eligle field name."<<endl;
			return;
		}		
		temp = new FIELDNODE;
		temp->f.name = new char [246];
		strcpy(temp->f.name,word);
		temp->next = 0;
		p->next = temp;
		p=p->next;
		w=getSym(input,readPointer,word);
		i++;	
	}while(w==COMMA);

	if(w!=FROM)
	{
		cout<<"Word \'FROM\' expected."<<endl;
		return;
	}
	w=getSym(input,readPointer,word);
	if(w!=IDENT)
	{
		cout<<"Tell me which table!"<<endl;
		return;
	}
	strcpy(tableName,word);
	w=getSym(input,readPointer,word);
	if(w==END)
	{
		conditionField.valid =0;
	}
	else
	{
		if(w!=WHERE)
		{
			cout<<"Un interprut end of select clause."<<endl;
			return;
		}
		
		w=getSym(input,readPointer,word);
		if(w!=IDENT)
		{
			cout<<"Eligle field name in where."<<endl;
			return;
		}
		conditionField.valid=1;
		conditionField.name = new char[247];
		strcpy(conditionField.name,word);
		w=getSym(input,readPointer,word);
		if(w==EQL||w==NEQ||w==LSS||w==LEQ||w==GTR||w==GEQ)			
			condition=w;
		else
		{
			cout<<"Unknow  operate."<<endl;
			return;
		}
		w=getSym(input,readPointer,word);
		if(w==NUM)
		{
			expect.value = new char[40];
			strcpy(expect.value,word);
			expect.type  =NUM;
		}
		else
			if(w==TEXT)
			{
				expect.value = new char[1024];
				strcpy(expect.value,word);
				expect.type = CHAR;
			}
			else
			{
				cout<<"Only for numbers and string."<<endl;
				return;
			}
		w=getSym(input,readPointer,word);
		if(w!=END)
		{
			cout<<"Too many words."<<endl;
			return;
		}
	}

			//input analysis ok,logincal check
		int tableIndex = findTable(tableName);
		if(tableIndex ==-1)
		{
			cout<<tableName<<" do not existt."<<endl;
			return;
		}
			
		FIELDREC fr;
		FIELD node;	
		node.name = "Index_Node";
		s->findField(node);
		fr=s->getOneFieldRecord(node,tableIndex);
		Block indexNode;
		indexNode.load(atoi(fr.value));
		Table thisTable(&indexNode);
		if(conditionField.valid)
		{
			thisTable.findField(conditionField);
			if(conditionField.valid==0)
			{
				cout<<"Field "<<conditionField.name<<" can not find in table "<<tableName<<endl;
				return;
			}
			if(expect.type==NUM&&conditionField.type==CHAR)
			{
				cout<<"Field type is a NUM ,but test condition is CHAR."<<endl;
				return;
			}
			if(expect.type==CHAR&&conditionField.type!=CHAR)
			{
				cout<<"Field tyoe is CHAR ,but test condition is NUM."<<endl;
				return;
			}
		}		
		p=head;
		while(p->next)   //check fields  and read out fileds imformations
		{
			p=p->next;
			thisTable.findField(p->f);
			if(!p->f.valid)
			{
				cout<<"Table "<<tableName<<" do not have field named "<<p->f.name<<"."<<endl;
				return;
			}
		}
		if(!head->next)
			thisTable.listRecord(conditionField,condition,expect);
		else
			thisTable.listRecord(conditionField,condition,expect,head);
		
}

void delete_record(char * input,int & readPointer,char * word)
{
	SYMBOL w,condition;
	FIELD conditionField;
	FIELDREC expect;
	char tableName[128];

	w=getSym(input,readPointer,word);
	if(w!=FROM)
	{
		cout<<"\'FROM\' expected."<<endl;
		return;
	}
	w=getSym(input,readPointer,word);
	if(w!=IDENT)
	{
		cout<<"Tell me which talbe."<<endl;
		return;
	}
	
	strcpy(tableName,word);
	w=getSym(input,readPointer,word);
	if(w!=WHERE)
	{
		cout<<"Tell me which record."<<endl;
		return;
	}		
	w=getSym(input,readPointer,word);
	if(w!=IDENT)
	{
		cout<<"Eligle field name in where."<<endl;
		return;
	}
	conditionField.valid=1;
	conditionField.name = new char[247];
	strcpy(conditionField.name,word);
	w=getSym(input,readPointer,word);
	if(w==EQL||w==NEQ||w==LSS||w==LEQ||w==GTR||w==GEQ)			
		condition=w;
	else
	{
		cout<<"Unknow  operate."<<endl;
		return;
	}
	w=getSym(input,readPointer,word);
	if(w==NUM)
	{
		expect.value = new char[40];
		strcpy(expect.value,word);
		expect.type  =NUM;
	}
	else
		if(w==TEXT)
		{
			expect.value = new char[1024];
			strcpy(expect.value,word);
			expect.type = CHAR;
		}
		else
		{
			cout<<"Only for numbers and string."<<endl;
			return;
		}
	w=getSym(input,readPointer,word);
	if(w!=END)
	{
		cout<<"Too many words."<<endl;
		return;
	}

	int tableIndex = findTable(tableName);
	if(tableIndex ==-1)
	{
		cout<<tableName<<" do not existt."<<endl;
		return;
	}
			
	FIELDREC fr;         //create talbe object;
	FIELD node;	
	node.name = "Index_Node";
	s->findField(node);
	fr=s->getOneFieldRecord(node,tableIndex);
	Block indexNode;
	indexNode.load(atoi(fr.value));
	Table thisTable(&indexNode);
	thisTable.findField(conditionField);
	if(!conditionField.valid)
	{
		cout<<"Table "<<tableName<<" do not have field named "<<conditionField.name<<"."<<endl;
		return;
	}
	thisTable.deleteRecord(conditionField,condition,expect);
	cout<<"Delete successfully."<<endl;


}

void update_table(char * input,int & readPointer,char * word)
{
	SYMBOL w,condition;
	FIELD conditionField,setField;
	FIELDREC expect,setValue;
	char tableName[128];

	w=getSym(input,readPointer,word);
	if(w!=IDENT)
	{
		cout<<"Tell me which table."<<endl;
		return;
	}
	strcpy(tableName,word);
	w=getSym(input,readPointer,word);
	if(w!=SET)
	{
		cout<<"\'SET\' expect."<<endl;
		return;
	}	
	w=getSym(input,readPointer,word);   //set field name
	if(w!=IDENT)
	{
		cout<<"Tell me which field."<<endl;
		return;
	}
	setField.name = new char[247];
	strcpy(setField.name,word);
	w=getSym(input,readPointer,word);
	if(w!=EQL)
	{
		cout<<"\'=\' expected."<<endl;
		return;
	}
	w=getSym(input,readPointer,word);  //set value
	if(w==NUM)
	{
		setValue.value = new char[40];
		strcpy(setValue.value,word);
		setValue.type  =NUM;
	}
	else
		if(w==TEXT)
		{
			setValue.value = new char[1024];
			strcpy(setValue.value,word);
			setValue.type = CHAR;
		}
		else
		{
			cout<<"Only for numbers and string."<<endl;
			return;
		}
	w=getSym(input,readPointer,word);   //where clause
	if(w==END)
	{
		conditionField.valid =0; //no where clause
	}
	else
	{
		if(w!=WHERE)
		{
			cout<<"Un interprut end of select clause."<<endl;
			return;
		}
		
		w=getSym(input,readPointer,word);
		if(w!=IDENT)
		{
			cout<<"Elligle field name in where."<<endl;
			return;
		}
		conditionField.valid=1;
		conditionField.name = new char[247];
		strcpy(conditionField.name,word);
		w=getSym(input,readPointer,word);
		if(w==EQL||w==NEQ||w==LSS||w==LEQ||w==GTR||w==GEQ)			
			condition=w;
		else
		{
			cout<<"Unknow  operate."<<endl;
			return;
		}
		w=getSym(input,readPointer,word);
		if(w==NUM)
		{
			expect.value = new char[40];
			strcpy(expect.value,word);
			expect.type  =NUM;
		}
		else
			if(w==TEXT)
			{
				expect.value = new char[1024];
				strcpy(expect.value,word);
				expect.type = CHAR;
			}
			else
			{
				cout<<"Only for numbers and string."<<endl;
				return;
			}
		w=getSym(input,readPointer,word);
		if(w!=END)
		{
			cout<<"Too many words."<<endl;
			return;
		}
	}

	//input analysis ok,begin check
	int tableIndex = findTable(tableName);
	if(tableIndex ==-1)
	{
		cout<<tableName<<" do not existt."<<endl;
		return;
	}
	FIELDREC fr;         //create talbe object;
	FIELD node;	
	node.name = "Index_Node";
	s->findField(node);
	fr=s->getOneFieldRecord(node,tableIndex);
	Block indexNode;
	indexNode.load(atoi(fr.value));
	Table thisTable(&indexNode);

	thisTable.findField(setField);  //check fields
	if(setField.valid==0)
	{
		cout<<"Can not find "<<setField.name<<". No such field in table "<<tableName<<"."<<endl;
		return;
	}
	if(conditionField.valid)
		{
			thisTable.findField(conditionField);
			if(conditionField.valid==0)
			{
				cout<<"Field "<<conditionField.name<<" can not find in table "<<tableName<<endl;
				return;
			}
			if(expect.type==NUM&&conditionField.type==CHAR)
			{
				cout<<"Field type is a NUM ,but test condition is CHAR."<<endl;
				return;
			}
			if(expect.type==CHAR&&conditionField.type!=CHAR)
			{
				cout<<"Field tyoe is CHAR ,but test condition is NUM."<<endl;
				return;
			}
		}	
	


	//basic check ok,call table fuction
	int result=thisTable.update(setField,setValue,conditionField,condition,expect);
	if(result==2)
		cout<<"Primary key's value must be unique."<<endl;
	else
		if(result==1)
			cout<<"Update failed."<<endl;
		else
			cout<<"Update successfully."<<endl;
	
	


	
}

void expression(char * input)
{
	int readPointer=0;
	char word[1024];
	SYMBOL w;
	w=getSym(input,readPointer,word);
	switch(w)
	{
	 
	case OPEN:
				w=getSym(input,readPointer,word);
				if(w==IDENT||w==TEXT)
				{
					f->open(word,ios::in|ios::out|ios::nocreate|ios::binary);
					if(f->fail())
					{
						cout<<"File \""<<word<<"\" can not open.";	
					}
					else{
						//initial Block * super and Block * systemTable;
						super = new Block();
						super->load(0ul);
						
						systemTableBlock=new Block();
						systemTableBlock->load(super->read(4,4));
						s=new Table(systemTableBlock);

					}
					
				}
				break;
	case CREATE:
				w=getSym(input,readPointer,word);
				switch(w)
				{
				case DATABASE:
					w=getSym(input,readPointer,word);
					if(w==IDENT||w==TEXT)
						create_databse(word);
					break;
				case TABLE:					
						create_table(input,readPointer,word);						


					break;
				case USER:
					cout<<"Do not finish yet."<<endl;
					break;
				default:
					cout<<"Syntax error.Unexpected word after create"<<endl;
				}				
				break;
	case DESCRIBE:
				w=getSym(input,readPointer,word);
				if(w==IDENT||w==TEXT)
				{
					describe_table(word);
				}
				else
				{
					cout<<"Must a table name!";
				}
				break;
	case ALTER:
				alter_table(input,readPointer,word);
				break;
	case DELETE:
				delete_record(input,readPointer,word);
				break;
	case DROP:
				w=getSym(input,readPointer,word);
				switch(w)
				{
				case TABLE:
						drop_table(input,readPointer,word);
						break;
				case USER:
						cout<<"Do not finish yet."<<endl;
						break; 
				default:
						cout<<"Unknow command."<<endl;

				}
				break;
	case INSERT:
				insert_into(input,readPointer,word);
				break;
	case SELECT:
				select_record(input,readPointer,word);
				break;
	case UPDATE:
				update_table(input,readPointer,word);
				break;
	case QUIT:				
				if(f)
					f->close();
				exit(0);
				break;
	default:
		cout<<"Unrecogonized command"<<endl;


		
	}
}

int main()
{
    	
	for(int i=0;i<RESERVE;i++)
		lowerString(RSYMS[i],lowerRSYMS[i]);

	char input_buffer[BUFFER_SIZE]="";	
	f=new fstream();

	cout<<"###Please input sql language,end with ';'###";
	while(true)
	{
				
		input_buffer[0]=0;
		cout<<"\n>>>>";
		getinput(input_buffer);
		expression(input_buffer);
		
	}
	return 0;
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -