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

📄 script_command.cpp

📁 小型数据库源代码较多
💻 CPP
📖 第 1 页 / 共 3 页
字号:
				char rubbish;
				if ( value==-99999 )
				{

//					in >> rubbish;

					throw 5;
				}
				in >> rubbish;//','
				if ( rubbish!=','&&rubbish!=')')
					throw 5;							
				//检查特性						 
				if(att!='n')
					 for(k=0;k<t->next->num;k++) //*******************
					 {
						 iin.ignore(1000, '\n');
						 for( j=i; j; j-- )
						 iin.ignore(10000,'\t');
						 int ifile;
						 iin >> ifile;
						 if(value==ifile)
						 {
							 if(att=='p')
								 throw 6;
							 if(att=='u')
								 throw 7;
						 }
					 }
				out << value << '\t';
			}
		}
		out << '\0';
		buf[page][strlen(buf[page])-1] = '\n';
		in>>ft;
		if( ft!=';' )
			throw 4;

		t->next->num++;
		modify_index( t->next);
		modify [page] = 1;		//********************8
		 //***********************

		cout << "对表<" + filename + ">插入记录成功!" << endl;
	    Log("对表<" + filename + ">插入记录成功!");
	
	

	}
	
	
	
					
	catch( int i )
	{
		switch ( i )
		{
			case 1: cout << "语法错误!" << endl;
					break;
			case 2: cout <<filename<<"表不存在!" << endl;
					break;
			case 3: cout << "无法打开表文件!" << endl;
					break;
			case 4: cout << "参数个数不匹配!!"<< endl;
					break;
			case 5: cout << "类型不匹配(包括字符串过长)!" << endl;
					break;
			case 6: cout << "违反主键的唯一性!" << endl;
					break;
			case 7: cout << "违反unique属性的唯一性!" << endl;
					break;
			case 10:cout << "无法打开数据字典!" << endl;
					break;
			case 11:cout << "类型不匹配(包括字符串过长)!" << endl;
					break;
		}
		
		if ( i!=1&&i!=2&&i!=3 )
		{
			int current_length = strlen(buf[page]);
			for( int l=length; l<=current_length; l++ )
				buf[page][l] = '\0';//回滚。。
		}	
		
		if ( i!=5 )
			while( ft!=';' )
			{
				in >> ft;
			}
	
	    Log("对表<" + filename + ">插入记录失败!");
	}

}







void sql::delete_from(ifstream& in )
{	
	int count = 0;//满足要求的记录数;
	try
	{
		string from;
		in >> from;
		if ( from.compare( "from" ) )
			throw 1;
		Table *table;
		string scomm0;
		in >> scomm0;
		
		if ( scomm0.find(';')!=-1)//删除所有记录
		{
			filename.append(scomm0.c_str(),scomm0.size()-1);
			table = tabexist();			
			if ( !table )
				throw 2;
			table = table->next;
			count = table->num;
			page = readtable();			
			for ( int n=0; n<sbf; n++ )
				if ( buf[page][n]=='\n' )
					break;
			if ( n == strlen(buf[page]) )
			{
				cout << "表已经被清空!" << endl;
				Log("表<"+filename+">已经被清空!");
				return;
			}
			for ( n++; n<sbf; n++ )
				buf[page][n] = '\0';
			table->num =0;
		}

        
		else
		{	
			filename = scomm0;
			string scomm[3];
			in >> scomm[0] >> scomm[1] >> scomm[2];
			if ( scomm[0] != "where" )
				throw 1;
			table = tabexist();
			
			if ( !table )
				throw 2;
			table = table->next;
			colname = scomm[1];
			int cth = -1;
			Col* col = colexist ( table, cth );
			if ( !col )
				throw 5;
			index = col->indexname;
			page = readtable();
			istrstream inb( buf[page], sbf);	
		
			if ( col->indexname!="" )
			{
				index = filename+"_" + index + ".idx";
				ifstream indexfile;
				indexfile.open( index.c_str() );
				indexfile.getline( subbuf, subsbf  );
				istrstream buff_in( subbuf, subsbf );
				int pos;
				if (col->type=='#')
				{
					int iput = -99999;
					int ifile;
					in >> iput;
					if ( iput==-99999 )
						throw 4;
					char u[2] = {'\0','\0'};
			
					in.getline(u,2);
					if ( scomm[2]!=">" && scomm[2]!="<" && scomm[2]!="="
						 && scomm[2]!=">=" && scomm[2]!="<=" && scomm[2]!="<>" )
						 throw 1;
					if( u[0]!=';'||u[1]!='\0' )
						throw 1; 
					while(1)
					{
						ifile = pos = -99999;
						buff_in >> ifile >> pos;
						if ( ifile==-99999||pos==-99999)
							break;
						if ( (ifile==iput&&scomm[2]=="=") || (ifile>=iput&&scomm[2]==">=")
							 || (ifile<=iput&&scomm[2]=="<=") || (ifile<iput&&scomm[2]=="<")
							 || (ifile>iput&&scomm[2]==">") || (ifile!=iput&&scomm[2]=="<>") )
						{
							for ( int dele=pos; buf[page][dele]!='\n'; dele++ )
								if( buf[page][dele]!='\t' )
									buf[page][dele] = '*';
							count++;
/*----ch11------*/			break;

						}
					}
				}

				else//type is char;
				{
					string sput;
					in >> sput;

					if( sput.size()-2 >= col->len )
						throw 4;
					char k = '\'';
					if( sput.at(sput.size()-1)!=';' || sput.at(0)!=k || sput.at(sput.size()-2)!=k )
						throw 1;
	
					string skey;
					string sfile;
					int pos;
					skey.assign(sput, 1, sput.size()-3);
					if( scomm[2]!="=" && scomm[2]!="<>" )
						throw 1; 
					while(1)
					{
						sfile.erase();
						pos = -99999;
						buff_in >> sfile >> pos;
						if ( sfile=="" || pos==-99999 )
							break;
						if ( (sfile==skey&&scomm[2]=="=") || (sfile!=skey&&scomm[2]=="<>") )
						{
							for ( int dele=pos; buf[page][dele]!='\n'; dele++ )
								if( buf[page][dele]!='\t' )
									buf[page][dele] = '*';
							count++;
/*----ch11------*/			break;
						}
					}
				}
			}

			else
			{
			
				int num = table->num;
				int po = -1;     //记录当前的位置;
				for (int  n=0; n<strlen(buf[page]); n++ )
					if ( buf[page][n] == '\n' )
						{
				
							if ( po==-1)
								po = n ;
							break;
						}
					
				int thn;
				int dele;
				if( col->type == '#' )//属性为int型;
				{
				
					int iput = -99999;
					int ifile;
					in >> iput;
					if ( iput==-99999 )
						throw 4;
					char u[2] = {'\0','\0'};
			
				
					in.getline(u,2);
					if ( scomm[2]!=">" && scomm[2]!="<" && scomm[2]!="="
						 && scomm[2]!=">=" && scomm[2]!="<=" && scomm[2]!="<>" )
						 throw 1;
					if( u[0]!=';'||u[1]!='\0' )
						throw 1; 
					for ( ; num; num-- )
					{
						po++;
						inb.ignore(1000,'\n');
						for ( thn=cth; thn; thn-- )
							inb.ignore(1000,'\t' ); 
						inb >> ifile;

						if ( (ifile==iput&&scomm[2]=="=") || (ifile>=iput&&scomm[2]==">=")
							 || (ifile<=iput&&scomm[2]=="<=") || (ifile<iput&&scomm[2]=="<")
							 || (ifile>iput&&scomm[2]==">") || (ifile!=iput&&scomm[2]=="<>") )
						{
							for ( dele=po; buf[page][dele]!='\n'; dele++ )
								if( buf[page][dele]!='\t' )
									buf[page][dele] = '*';
							count++;

						}
				
						for ( po; buf[page][po]!='\n'; po++ );
						ifile = 0;
					}

				}
	
				else//char型;
				{
					string sput;
					in >> sput;

					if( sput.size()-2 > col->len )
						throw 4;
					char k = '\'';
					if( sput.at(sput.size()-1)!=';' || sput.at(0)!=k || sput.at(sput.size()-2)!=k )
						throw 1;
	
					string skey;
					string sfile;
					skey.assign(sput, 1, sput.size()-3);

					if( scomm[2]!="=" && scomm[2]!="<>" )
						throw 1;



					for ( ; num; num-- )
					{
						po++;
						inb.ignore(1000,'\n');
						for ( thn=cth; thn; thn-- )
							inb.ignore(1000,'\t' ); 
						inb >> sfile;

						if ( (sfile==skey&&scomm[2]=="=") || (sfile!=skey&&scomm[2]=="<>") )
						{
							for ( dele=po; buf[page][dele]!='\n'; dele++ )
								if( buf[page][dele]!='\t' )
									buf[page][dele] = '*';
							count++;
						}
						
						for ( po; buf[page][po]!='\n'; po++ );
						sfile.erase();
					}


				}
			}
			table->num -= count;
		}
		modify_index( table );
		modify [page] = 1;		
		 

		cout << "对表<" + filename + ">删除 " << count << " 条记录成功!" << endl;
	    Log("对表<" + filename + ">删除记录成功!");
 }
	
	catch ( int i )
	{
		switch(i)
		{
		case 1:	cout << "语法错误!" << endl;
				break;
		case 2:	cout << "表名不存在!" << endl;
				break;
		case 3: cout << "该表不能打开!" << endl;
				break;
		case 4: cout << "类型不匹配!" << endl;
				break;
		case 5: cout << "列名不存在!" << endl;
				break;
		}
	
	        char ft;
		    in>>ft;
         	while(ft!=';')
			   in >> ft;
	        Log("删除表失败!");
	
	}

}





void sql::create_index(ifstream& in)
{
	string scomm[6];
	for( int i = 0; i < 6; i++ )
		in >> scomm[i];
	char u[2];

	in.getline(u,2);

	try
	{
		//语法分析
		if( scomm[1] != "on" || scomm[3] != "(" || scomm[5] != ");" )
			throw 1;
		//检查表,属性及唯一性,索引是否存在
		index = scomm[0].data();
		filename = scomm[2].data();	
		colname = scomm[4].data();
		Table*  table = tabexist()->next;
		if( !table )
			throw 2;
		int num = table->num;
		int cth=-1;
		Col* col = colexist ( table, cth );
		if ( !col )
			throw 4;
		if ( col->att == 'n')
			throw 6;
		if ( col->indexname.compare(""))
			throw 5;
		//读入表文件
		page =  readtable ();			
		istrstream inb(buf[page],sbf);

		inb.ignore(1000,'\n');
		int thn;
		int head = 0 ;
		ostrstream out( subbuf,subsbf );
		if ( col->type=='#' ) //属性为int
				{
					int idx = 0;
					while (num-- )
					{
						for ( ; head<=strlen(buf[page]) ; head++)
						if( buf[page][head]=='\n' )
							break;
						head++;
						for ( thn=cth; thn; thn-- )
							inb.ignore(1000,'\t' );
						string c_idx;
						inb >> c_idx;
						while ( c_idx.at(0)=='*' )
						{
							inb.ignore(1000,'\n');
							for ( thn=cth; thn; thn-- )
								inb.ignore(1000,'\t' );							
							inb >> c_idx;
							for ( ; head<=strlen(buf[page]) ; head++)
								if( buf[page][head]=='\n' )
									break;
							head++;
						}
						for ( int p=0; p<c_idx.size(); p++ )
							idx = idx*10 + c_idx.at(p) - '0';

						out << idx << ' ' << head << '\t';
						idx = 0;
						inb.ignore(1000,'\n');
					}		
				}
				else//属性为char;
				{
					char ind[256];
					while ( num-- )
					{			
						for ( ; head <= strlen(buf[page]) ; head++)
							if( buf[page][head]=='\n' )
								break;
						head++;
						for ( thn=cth; thn; thn-- )
							inb.ignore(1000,'\t' ); 	
						inb >> ind;
						while ( ind[0]=='*' )
						{
							inb.ignore(1000,'\n');
							for ( thn=cth; thn; thn-- )
								inb.ignore(1000,'\t' );
/*---------ch---------*/		inb >> ind;
							for ( ; head<=strlen(buf[page]) ; head++)
								if( buf[page][head]=='\n' )
									break;
							head++;
						}
						out << ind << ' ' << head << '\t';

⌨️ 快捷键说明

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