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

📄 select_from.cpp

📁 小型数据库源代码较多
💻 CPP
字号:

#include "min.h"

void sql::select_from(  )
{
	Table *table;
	string scomm0[3];
	cin >> scomm0[0] >> scomm0[1] >> scomm0[2];
	int count = 0;//满足要求的记录数;
	try
	{
		if ( scomm0[2].find(';')!=-1)//select all from tablename;
		{
			if ( scomm0[0]!="*" || scomm0[1]!="from" )
				throw 1;
			filename.append(scomm0[2].c_str(),scomm0[2].size()-1);
			table = tabexist();
			
			if ( !table )
				throw 2;
			table = table->next;
			page = readtable();
			for ( int n=0; n<sbf; n++ )
				if ( buf[page][n]=='\n' )
					break;
/*---------------------ch----------------------------*/
			Col *col = table->collist->next;
			for ( ; col; col = col->next )
				cout << col->name << '\t';
			cout << endl;

			for ( n++; n<strlen(buf[page]); n++ )
			{
				while ( buf[page][n]=='*' )
				{
					for ( ; buf[page][n]!='\n'; n++ );
					n++;
				}
				cout << buf[page][n];
				if( buf[page][n] == '\n' )
					count++;
			}
			if (!count)
				cout << "找到不到符合要求的记录!" << endl;
/*---------------------ch----------------------------*/

//  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
		}
		else
		{
			filename = scomm0[2];
			string scomm1;
			cin >> scomm1;
			if ( scomm1!="where" )
				throw 1;
			table = tabexist();
			
			if ( !table )
				throw 2;
			table = table->next;
			page = readtable();

			int num = table->num; //记录数;
		//	int po = -1;     //记录当前的位置;
		//	for ( int n = 0; n < strlen(buf[page]); n++ )
			for ( int po = 0; po < strlen(buf[page]); po++ )
				if ( buf[page][po] == '\n' )
					break;
			//	{
			//		num++;
			//		if ( po==-1)
			//			po = n ;
			//	}
			
			char cbuf[subsbf];
			ostrstream out(cbuf,subsbf); 
			char type[20];
			char indextype='\0';
			string indexname;
//**********
			string indexkey = "";//= >=.....
			int indexint;
			string indexchar;


			int th[20];
			int indexe[20];//属性是否有索引,有为1,没有为0;
			string skey;
		//	string fkey;
		//	int ikey = -99999;
			for ( int n=0; ; n++ )//把所有的命令中有价值的信息放到out中;
			{
				cin >> skey;
				colname = skey;
				Col* col = colexist(table, th[n]);
				if( !col) 
					throw 5;
				type[n] = col->type;
				if ( col->indexname!="" )
							indexe[n] = 1;
						else
							indexe[n] = 0;

				cin >> skey;
				if ( col->type=='#' )
				{
					int ikey = -99999;
					if ( skey!=">" && skey!="<" && skey!="="
					 && skey!=">=" && skey!="<=" && skey!="<>" )
						throw 1;
					out << skey << ' ';
					cin >> ikey;
					if ( ikey == -99999 )
						throw 4;
					out << ikey << '\t';
					if ( (indexkey==""||(indexkey!="="&&skey=="="))&&indexe[n] )
					{
						indexkey = skey;
						indexname = col->indexname;
						indexint = ikey;
						indextype='#';
					}
			//		ikey = -99999;
				}
				else
				{
					if ( skey!="=" && skey!="<>" )
						throw 1;
					out << skey << ' ';
					string keyatt = skey;
					cin >> skey;
					string pskey;
					char k = '\'';
					if ( skey.at(0) == k && skey.at(skey.size()-1) == k )
					{
						pskey.assign(skey, 1, skey.size()-2 );
						out << pskey << '\t';
					}
					else if ( skey.at(0) == k && skey.at(skey.size()-2) == k
							  && skey.at(skey.size()-1 ) == ';' )
					{
						pskey.assign(skey, 1, skey.size()-3 );
						out << pskey << '\t';
/*---------------------ch11----------------------------*/
						if ( (indexkey==""||(indexkey!="="&&skey=="="))&&indexe[n] )
						{
							indexkey = keyatt;
							indexname = col->indexname;
							indexchar = pskey;
						    indextype='$';
						}
/*---------------------ch11----------------------------*/

						break;
					}
					else
						throw 4;
					if ( (indexkey==""||(indexkey!="="&&skey=="="))&&indexe[n] )
					{
						indexkey = keyatt;
						indexname = col->indexname;
						indexchar = pskey;
					    indextype='$';
					}
				}

				cin >> skey;
				if ( skey==";" )
					break;
				else if ( skey!="and" )
					throw 1;
			}
/*----ch11------*/
			out << '&' << ' ' << '\0';//标志out中命令已结束;

/////////////////////////////////////////////////////////////////////////
			//在表中找符合out中的记录;			
/*---------------------ch----------------------------*/
			Col *coll = table->collist->next;
			for ( ; coll; coll = coll->next )
				cout << coll->name << '\t';
			cout << endl;
/*---------------------ch----------------------------*/
			if ( indexname!="" && indexkey!="" && indextype )// 用索引查找。。。
			{
				fileload = filename + "_" + indexname + ".idx";
				ifstream indexfile;
				indexfile.open( fileload.c_str() );
				indexfile.getline(subbuf, subsbf);
				istrstream indexin(subbuf, strlen(subbuf));
				string attchar;
				int attint;
				int pos;

				while (1)
				{
					istrstream comm(cbuf,subsbf);
					attint = pos = -99999;
					attchar.erase();
					if ( indextype=='#' )
					{
						indexin >> attint >> pos;
						if( attint==-99999||pos==-99999 )
							break;
						if( !( (attint==indexint&&indexkey=="=") || (attint>indexint&&indexkey==">") 
							|| (attint<indexint&&indexkey=="<") || (attint==indexint&&indexkey>=">=") 
							|| (attint<=indexint&&indexkey=="<=") || (attint!=indexint&&indexkey=="<>") ) )
								continue;						
					}
					else
					{
						indexin >> attchar >> pos;
						if( attchar==""||pos==-99999 )
							break;
						if( !( (attchar==indexchar&&indexkey=="=") || (attchar!=indexchar&&indexkey=="<>") ) )
							continue;
					}

					int pi;
					for ( pi=pos; buf[page][pi]!='\n'; pi++ );	
					for ( n=0; ;n++ )//检查该tuple是否符合每个命令;
					{
						char *p = &buf[page][pos];
						istrstream tuple( p, pi-pos );
						comm >> skey;
						if (skey.at(0)=='&')//该记录完全符合要求;
						{
							for ( int pw=pos; pw<=pi;pw++ )
								cout << buf[page][pw];
/*----ch------*/				count++;
/*----ch12------*/				break;
						}
						for ( int thn=th[n]; thn; thn-- )
							tuple.ignore(300, '\t');
						if (type[n]=='#')//int;
						{
							int titem;	
							int iitem;
							tuple >> titem;
							comm >> iitem;
							if( !( (titem==iitem&&skey=="=") || (titem>=iitem&&skey==">=")
							 || (titem<=iitem&&skey=="<=") || (titem<iitem&&skey=="<")
							 || (titem>iitem&&skey==">") || (titem!=iitem&&skey=="<>") ) )
								break;
						}
						else
						{
							string titem;
							string iitem;
							tuple >> titem;
							comm >> iitem;
							if( !( (titem==iitem&&skey=="=") || (titem!=iitem&&skey=="<>") ) )
								break;
						}
					}//命令检查

				}
			}
			else//没有索引。。。
			{
				int pi;
				for ( ; num; num-- )//检查每个记录
				{
					istrstream comm(cbuf,subsbf);
					po++;
					for ( pi=po; buf[page][pi]!='\n'; pi++ );	
					for ( n=0; ;n++ )//检查该tuple是否符合每个命令;
					{
						char *p = &buf[page][po];
						istrstream tuple( p, strlen(p) );
						comm >> skey;
						if (skey.at(0)=='&')//该记录完全符合要求;
						{
							for ( int pw=po; pw<=pi;pw++ )
								cout << buf[page][pw];
							count++;
							break;
						}
						for ( int thn=th[n]; thn; thn-- )
							tuple.ignore(300, '\t');
						if (type[n]=='#')//int;
						{
							int titem=0;	
							int iitem;
							string c_item;
							tuple >> c_item;
/*---------------------ch----------------------------*/
							while ( c_item.at(0)=='*' )
							{
								tuple.ignore(300, '\n');
								for ( int thn=th[n]; thn; thn-- )
									tuple.ignore(300, '\t');
								po=pi+1;
								for ( pi=po; buf[page][pi]!='\n'; pi++ );
								
								tuple >> c_item;
							}
							for ( int l=0; l<c_item.size(); l++ )
								titem = titem*10 + c_item.at(l) - '0';
/*---------------------ch----------------------------*/
							comm >> iitem;
							if( !( (titem==iitem&&skey=="=") || (titem>=iitem&&skey==">=")
							 || (titem<=iitem&&skey=="<=") || (titem<iitem&&skey=="<")
							 || (titem>iitem&&skey==">") || (titem!=iitem&&skey=="<>") ) )
								break;
						}
						else
						{
							string titem;
							string iitem;
							tuple >> titem;
/*---------------------ch----------------------------*/
							while ( titem.at(0)=='*' )
							{
								tuple.ignore(300, '\n');
								for ( int thn=th[n]; thn; thn-- )
									tuple.ignore(300, '\t');
								po=pi+1;
								for ( pi=po; buf[page][pi]!='\n'; pi++ );
								tuple >> titem;
							
							}
/*---------------------ch----------------------------*/
							comm >> iitem;
							if( !( (titem==iitem&&skey=="=") || (titem!=iitem&&skey=="<>") ) )
								break;
						}
					}//命令检查
					po = pi;
				}//记录完毕
			}
/*---------------------ch----------------------------*/
			if (!count)
				cout << "找到不到符合要求的记录!" << endl;
/*---------------------ch----------------------------*/

		}//where;
	  
         Log("对表"+filename+"执行select语句成功!");//pp

	}//try;
	
	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("对表"+filename+"执行select语句失败!");
	}

}

⌨️ 快捷键说明

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