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

📄 sql05doc.cpp

📁 our homework big project
💻 CPP
字号:
// sql05Doc.cpp :  Csql05Doc 类的实现
//

#include "stdafx.h"
#include "sql05.h"
#include "SplitString.h"
#include "AnalyseWords.h"
#include "CatalogManager.h"
#include "Executor.h"
#include "sql05Doc.h"
#include "MyDialog.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// Csql05Doc

IMPLEMENT_DYNCREATE(Csql05Doc, CDocument)

BEGIN_MESSAGE_MAP(Csql05Doc, CDocument)
	ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
	ON_COMMAND(ID_FILE_SAVE, OnFileSave)
	ON_COMMAND(ID_FILE_SAVE_AS, OnFileSaveAs)
	ON_COMMAND(ID_ANALYSE, OnAnalyse)
	ON_COMMAND(ID_RUN,OnExecute)
	ON_COMMAND(ID_FILE_NEW, OnFileNew)
	ON_UPDATE_COMMAND_UI(ID_ANALYSE, OnUpdateAnalyse)
	ON_UPDATE_COMMAND_UI(ID_RUN, OnUpdateRUN)
END_MESSAGE_MAP()


// Csql05Doc 构造/销毁

Csql05Doc::Csql05Doc()
:myString(""),
filename("")
{
	// TODO:在此添加一次性构造代码
	dlgReturn=true;
}

Csql05Doc::~Csql05Doc()
{
}

BOOL Csql05Doc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	reinterpret_cast<CEditView*>(m_viewList.GetHead())->SetWindowText(NULL);

	// TODO:在此添加重新初始化代码
	// (SDI 文档将重用该文档)

	return TRUE;
}




// Csql05Doc 序列化

//void Csql05Doc::Serialize(CArchive& ar)
//{
	// CEditView 包含一个处理所有序列化的编辑控件
	//reinterpret_cast<CEditView*>(m_viewList.GetHead())->SerializeRaw(ar);
//}


// Csql05Doc 诊断

#ifdef _DEBUG
void Csql05Doc::AssertValid() const
{
	CDocument::AssertValid();
}

void Csql05Doc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG


// Csql05Doc 命令

/////////

void Csql05Doc::OnFileNew()
{
	// TODO: 在此添加命令处理程序代码
	CString currentString;
	this->GetView(1)->GetWindowText(currentString);
	//AfxMessageBox(currentString);
	//AfxMessageBox(myString);
	if(currentString!=myString)
    {
		dlgReturn=true;
        CMyDialog dlg;
		 int nReturn=(int)dlg.DoModal();
		 
			if(nReturn==IDOK)
                OnFileSave();
			if(nReturn!=IDCANCEL&&dlgReturn)
			{
				 GetView(1)->SetWindowText("");
               GetView(2)->SetWindowText("");
               filename="";
			}
	}	
	else{
        GetView(1)->SetWindowText("");
        GetView(2)->SetWindowText("");
        filename="";
	}

} 


//////

void Csql05Doc::OnFileOpen()
{
	// TODO: 在此添加命令处理程序代码

	TCHAR szFilters[] =
		"Text files( *.txt)|*.tex|All files(*.*)|*.*||";
	CFileDialog dlg(TRUE,"txt","*.txt",
		OFN_FILEMUSTEXIST|OFN_HIDEREADONLY,szFilters);

	if(dlg.DoModal()==IDOK)
	{
		
		filename=dlg.GetPathName();
		CString stringLine;
		myString="";
		CStdioFile file(filename,CFile::modeRead);
		while(file.ReadString(stringLine))
		{	
			myString+=stringLine;
			if(stringLine.Right(2)!="\r\n")
				
				if(stringLine.Right(1)=="\r")
					myString+=char(0x0A);
				else{
                    myString+=char(0x0D);
                    myString+=char(0x0A);
				}

       	}
		
		GetView(1)->SetWindowText("");
		GetView(1)->SetWindowText(myString);
		file.Close();
	}
}

////////

void Csql05Doc::OnFileSave()
{
	// TODO: 在此添加命令处理程序代码

	if(!filename.IsEmpty())
	{
		CFile::Remove(filename);
       SaveFile(filename);
	}
	else
		OnFileSaveAs();
}


///////

void Csql05Doc::OnFileSaveAs()
{
	// TODO: 在此添加命令处理程序代码

	TCHAR szFilters[] =
		"Text files( *.txt)|*.tex|All files(*.*)|*.*||";

	CFileDialog dlg(FALSE,"txt","*.txt",
		OFN_FILEMUSTEXIST|OFN_HIDEREADONLY,szFilters);
	if(dlg.DoModal()==IDOK)
	{
		if(SaveFile(dlg.GetPathName()))
       filename=dlg.GetPathName();
		
	}
	else
		dlgReturn=false;
}


/////////////

BOOL Csql05Doc::SaveFile(LPCTSTR pszFile)
{
	((CEdit*)this->GetView(1))->FmtLines(TRUE);
	myString="";
	this->GetView(1)->GetWindowText(myString);
	CStdioFile file(pszFile,CFile::modeWrite|CFile::modeCreate);
    file.WriteString(myString);
	file.Close();
	return TRUE;
}


//////////
void Csql05Doc::OnAnalyse()
{
	CString cstr;
	//CString cs("");
	string str;
	this->GetView(1)->GetWindowText(cstr);
	str+=cstr;
	try
	{
		SplitString tempstr(str);
  		vector<string> tempvec;
		int num=tempstr.SplitWords(tempvec);
		
		if(num == 0)
		{
			throw ErrorPos(ID_ERROR_NOSENTENCE,-1);
		}
		
		vector<int> v_begin;
		AnalyseWords an_words(tempvec);
		an_words.InitToken();

		if(an_words.GetToken(num-1) != END )
			throw ErrorPos(ID_ERROR_END,-1);
		
		v_begin.push_back(0);
		for(int i=1; i<num; i++)
			if(an_words.GetToken(i) == END)
				v_begin.push_back(i+1);
		if(num != 1)v_begin.pop_back();

		int totalcommand=v_begin.size();
		
		for(int j=0; j<totalcommand; j++)
		{
			AnalyseWords *bp;
			
			switch(an_words.PreAnalyseSQL(v_begin[j],j+1))
			{
			case 1:
				{AnCreateTable an_c_table(tempvec);
				bp = &an_c_table;
				bp->AnalyseSQL(v_begin[j]+2,j+1);}
				break;
			case 2:
				{AnCreateIndex an_c_index(tempvec);
				bp = &an_c_index;
				bp->AnalyseSQL(v_begin[j]+2,j+1);}
				break;
			case 3:
				{AnDropTable an_d_table(tempvec);
				bp = &an_d_table;
				bp->AnalyseSQL(v_begin[j]+2,j+1);}
				break;
			case 4:
				{AnDropIndex an_d_index(tempvec);
				bp = &an_d_index;
				bp->AnalyseSQL(v_begin[j]+2,j+1);}
				break;
			case 5:
				{AnInsertValues an_i_values(tempvec);
				bp = &an_i_values;
				bp->AnalyseSQL(v_begin[j]+1,j+1);}
				break;
			case 6:
				{AnDeleteValues an_d_values(tempvec);
				bp = &an_d_values;
				bp->AnalyseSQL(v_begin[j]+1,j+1);}
				break;
			case 7:
				{AnSelectTable an_s_table(tempvec);
				bp = &an_s_table;
				bp->AnalyseSQL(v_begin[j]+1,j+1);}
				break;
			}
		}
	
	this->GetView(2)->SetWindowText("分析成功!");
	}
	catch(ErrorPos id)
	{
		this->GetView(2)->SetWindowText((Exception::HandleException(id.m_err,id.m_pos)).c_str());
	}
}



void Csql05Doc::OnExecute()
{
	CString cstr;
	string str;
	string tempOut="..............................................................................................................................\r\n\r\n";
	long stime,etime;
	double ftemp;
	this->GetView(1)->GetWindowText(cstr);
	str+=cstr;

	try
	{
		SplitString tempstr(str);
  		vector<string> tempvec;
		int num=tempstr.SplitWords(tempvec);
		vector<int> v_begin;
		AnalyseWords an_words(tempvec);
		an_words.InitToken();
		if(num == 0)
		{
			throw ErrorPos(ID_ERROR_NOSENTENCE,-1);
		}

		if(an_words.GetToken(num-1) != END)
			throw ErrorPos(ID_ERROR_END,-1);
		
		v_begin.push_back(0);
		for(int i=1; i<num; i++)
			if(an_words.GetToken(i) == END)
				v_begin.push_back(i+1);
		if(num != 1)v_begin.pop_back();

		int totalcommand=v_begin.size();
		
		stime=clock();
		for(int j=0; j<totalcommand; j++)
		{
			AnalyseWords *bp;
			Executor *ep;
			
			switch(an_words.PreAnalyseSQL(v_begin[j],j+1))
			{
			case 1:
				{AnCreateTable an_c_table(tempvec);
				bp = &an_c_table;
				bp->AnalyseSQL(v_begin[j]+2,j+1);
				ExCreateTable ex_c_table(an_c_table);
				ep=&ex_c_table;
				tempOut+=ep->ExecuteSQL(j+1);
				tempOut+="\r\n";
				tempOut+="..............................................................................................................................";
				tempOut+="\r\n";
				tempOut+="\r\n";
				break;}
			case 2:
				{AnCreateIndex an_c_index(tempvec);
				bp = &an_c_index;
				bp->AnalyseSQL(v_begin[j]+2,j+1);
				ExCreateIndex ex_c_index(an_c_index);
				ep=&ex_c_index;
				tempOut+=ep->ExecuteSQL(j+1);
				tempOut+="\r\n";
				tempOut+="..............................................................................................................................";
				tempOut+="\r\n";
				tempOut+="\r\n";
				break;}
			case 3:
				{AnDropTable an_d_table(tempvec);
				bp = &an_d_table;
				bp->AnalyseSQL(v_begin[j]+2,j+1);
				ExDropTable ex_d_table(an_d_table);
				ep=&ex_d_table;
				tempOut+=ep->ExecuteSQL(j+1);
				tempOut+="\r\n";
				tempOut+="..............................................................................................................................";
				tempOut+="\r\n";
				tempOut+="\r\n";
				break;}
			case 4:
				{AnDropIndex an_d_index(tempvec);
				bp = &an_d_index;
				bp->AnalyseSQL(v_begin[j]+2,j+1);
				ExDropIndex ex_d_index(an_d_index);
				ep=&ex_d_index;
				tempOut+=ep->ExecuteSQL(j+1);
				tempOut+="\r\n";
				tempOut+="..............................................................................................................................";
				tempOut+="\r\n";
				tempOut+="\r\n";
				break;}
			case 5:
				{AnInsertValues an_i_values(tempvec);
				bp = &an_i_values;
				bp->AnalyseSQL(v_begin[j]+1,j+1);
				ExInsertValues ex_i_values(an_i_values);
				ep=&ex_i_values;
				tempOut+=ep->ExecuteSQL(j+1);
				tempOut+="\r\n";
				tempOut+="..............................................................................................................................";
				tempOut+="\r\n";
				tempOut+="\r\n";
				break;}
			case 6:
				{
				AnDeleteValues an_d_values(tempvec);
				bp = &an_d_values;
				bp->AnalyseSQL(v_begin[j]+1,j+1);
				ExDeleteValues ex_d_values(an_d_values);
				ep=&ex_d_values;
				tempOut+=ep->ExecuteSQL(j+1);
				tempOut+="\r\n";
				tempOut+="..............................................................................................................................";
				tempOut+="\r\n";
				tempOut+="\r\n";
				
				break;}
			case 7:
				{AnSelectTable an_s_table(tempvec);
				bp = &an_s_table;
				bp->AnalyseSQL(v_begin[j]+1,j+1);
				ExSelectTable ex_s_table(an_s_table);
				ep=&ex_s_table;
				tempOut+=ep->ExecuteSQL(j+1);
				tempOut+="\r\n";
				tempOut+="..............................................................................................................................";
				tempOut+="\r\n";
				tempOut+="\r\n";
				break;}
			}
			
		}
		etime=clock();
		tempOut+="*****共耗时 ";
		
		ftemp=double((etime-stime))/CLK_TCK;
		if(int(ftemp)==0)
			tempOut+="0.000";
		else
		{
			ftemp=int(ftemp*1000);
			for(j=0 ;;j++) 
			{
				tempOut.insert(tempOut.end()-j, int(ftemp)%10+'0'); 
				ftemp=int(ftemp/10);
				if(int(ftemp)==0) break;
			}
			tempOut.insert(tempOut.size()-3,"."); 
		}
				
		tempOut+=" 秒!*****";
		this->GetView(2)->SetWindowText(tempOut.c_str());
	}

	catch(ErrorPos id)
	{
		this->GetView(2)->SetWindowText((Exception::HandleException(id.m_err,id.m_pos)).c_str());
	}

	
}

/////////

CView* Csql05Doc::GetView(int nIndex)
{
	POSITION pos=NULL;
	pos=m_viewList.GetHeadPosition();
	int nCount=-1;
	while(pos!=NULL)
	{
		nCount++;
		if( nCount==nIndex)
			return(CEditView*)(m_viewList.GetAt(pos));
		m_viewList.GetNext(pos);
	}
	return NULL;
}


/////////
BOOL Csql05Doc::SaveModified()
{
	// TODO: 在此添加专用代码和/或调用基类
	CString currentString;
	this->GetView(1)->GetWindowText(currentString);
	if(currentString!=myString)
    {
        CMyDialog dlg;
		dlgReturn=true;
		 int nReturn=(int)dlg.DoModal();
		if(nReturn == IDOK)
			OnFileSave();
		if(nReturn==IDCANCEL||!dlgReturn)
			 return FALSE;
   }  
	this->SetModifiedFlag(FALSE);
	return TRUE;
}

void Csql05Doc::OnUpdateAnalyse(CCmdUI *pCmdUI)
{
	// TODO: 在此添加命令更新用户界面处理程序代码
	CString testString;
	this->GetView(1)->GetWindowText(testString);
	if(testString=="")
	pCmdUI->Enable(false);
}

void Csql05Doc::OnUpdateRUN(CCmdUI *pCmdUI)
{
	// TODO: 在此添加命令更新用户界面处理程序代码
	CString testString;
	this->GetView(1)->GetWindowText(testString);
	if(testString=="")
	pCmdUI->Enable(false);
}

⌨️ 快捷键说明

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