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

📄 delete_from.cpp

📁 小型数据库源代码较多
💻 CPP
字号:
//日志没写。。
#include "min.h"

void sql::delete_from(  )
{	
	int count = 0;//满足要求的记录数;
	try
	{
		string from;
		cin >> from;
		if ( from.compare( "from" ) )
			throw 1;
		Table *table;
		string scomm0;
		cin >> 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 << "表<"+filename+">已经被清空!" << endl;
				Log("表<"+filename+">已经被清空!");
				return;
			}
			for ( n++; n<sbf; n++ )
				buf[page][n] = '\0';
			table->num =0;
		}

		else
		{	
			filename = scomm0;
			string scomm[3];
			cin >> 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;
/*-----------ch12----------*/
				if (col->type=='#')
				{
					int iput = -99999;
					int ifile;
					cin >> iput;
					if ( iput==-99999 )
						throw 4;
					char u[2] = {'\0','\0'};
					gets(u); //无实际意义,只是去掉命令输入中的回车;
					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;
					cin >> 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------*/			
						}
					}
				}
			}

			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;
					cin >> iput;
					if ( iput==-99999 )
						throw 4;
					char u[2] = {'\0','\0'};
					gets(u);//无实际意义,只是去掉命令输入中的回车;
					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;
					cin >> 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;
		}
/*--------------ch10--------------*/
		modify_index( table );
/*--------------ch10--------------*/
		modify [page] = 1;		//********************8
		 //***********************

		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 a[50];
		cin.getline( a, 50 );
		Log("删除表失败!");
	}

}

⌨️ 快捷键说明

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