// testsqlite3.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "testsqlite3.h"

// #ifdef _DEBUG
// #define new DEBUG_NEW
// #endif

#include <io.h>
#include "..\\SQLite3\\sqlite3.h"
#pragma comment(lib, "SQLite3.lib")

#include "StringProcess.h"
#include "DataImpl.h"
#include "TaskInfoMessage.pb.h"
// 唯一的应用程序对象

CWinApp theApp;

using namespace std;

static int callback(void *NotUsed, int argc, char **argv, char **azColName)
{
	int i;
	for(i=0; i<argc; i++)
	{
		printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
	}
	printf("\n");
	return 0;
}

#define CHECK_RC(rc,szInfo,szErrMsg,db) if(rc!=SQLITE_OK) \
{printf("%s error!\n",szInfo);\
	printf("%s\n",szErrMsg);    \
	sqlite3_free(szErrMsg);         \
	sqlite3_close(db);              \
	system("pause");\
	return 0;}

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	int nRetCode = 0;

	// 初始化 MFC 并在失败时显示错误
	if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
	{
		// TODO: 更改错误代码以符合您的需要
		_tprintf(_T("错误: MFC 初始化失败\n"));
		nRetCode = 1;
	}
	else
	{
#if 0
		// TODO: 在此处为应用程序的行为编写代码。
		sqlite3 *db;
		char *dbPath="F:\\输出文件\\服务端\\test.db";
		char *szErrMsg = 0;

		char szpath[MAX_PATH] = {0};

		std::string ss= "F:\\输出文件\\服务端\\test.db";
		if ( StringProcess::ascii_to_utf8(dbPath, szpath) == 0)
			return 0;
		StringProcess::GB2312ToUTF_8(ss, dbPath, strlen(dbPath));

		sprintf(szpath, _T("%s"), ss.c_str());
		if (  -1 == _access(dbPath, 0 ) )
		{
			int rc= sqlite3_open(szpath, &db);
			CHECK_RC(rc,"open database",szErrMsg,db);
			char *szSql="create table UserInfo(ID int primary key , UserName char, PassWord char);";
			rc=sqlite3_exec(db,szSql,0,0,&szErrMsg);
			CHECK_RC(rc,"create table",szErrMsg,db);
			rc=sqlite3_exec(db,"insert into UserInfo(ID,UserName,PassWord) values(1,'kfqcome','123456')",0,0,&szErrMsg);
			CHECK_RC(rc,"insert info",szErrMsg,db);
			rc=sqlite3_exec(db,"insert into UserInfo(ID,UserName,PassWord) values(2,'miss wang','654321')",0,0,&szErrMsg);
			CHECK_RC(rc,"insert info",szErrMsg,db);
			szSql="select * from UserInfo";
			rc = sqlite3_exec(db,szSql, callback, 0, &szErrMsg);
			CHECK_RC(rc,"query values",szErrMsg,db);
			sqlite3_close(db);
		}
		else
		{
			int rc= sqlite3_open(szpath, &db);
			CHECK_RC(rc,"open database",szErrMsg,db);
			char *szSql="select * from UserInfo";
			rc = sqlite3_exec(db,szSql, callback, 0, &szErrMsg);
			CHECK_RC(rc,"query values",szErrMsg,db);
			sqlite3_close(db);
		}
#endif

#if 0
		CHAR *pszError = NULL;
		sqlite3 *pObj = NULL;

		// 打开;
		INT nResult = sqlite3_open("sms.db", &pObj);
		if ( SQLITE_OK != nResult )
			return -1;

		// 创建;
#define _CREATE_TABLE_ "CREATE TABLE UserInfo (Autoid  INTEGER NOT NULL,Account  TEXT(16) NOT NULL,Password  TEXT(16) NOT NULL,PRIMARY KEY (Autoid ASC),CONSTRAINT IU_UserInfo UNIQUE (Account));"
		//nResult = sqlite3_exec(pObj, _CREATE_TABLE_, NULL, NULL, &pszError);
		if ( SQLITE_OK != nResult )
		{
			printf("%s\n", pszError);
		}

		// 插入;
		CStringA strInsert = "";
		nResult = sqlite3_exec(pObj, "insert into UserInfo(Autoid, Account, Password) values (2, 'aaaa', '123456')", NULL, NULL, &pszError);
		if ( SQLITE_OK != nResult )
		{
			printf("%s\n", pszError);
		}

		// 查询;
		INT nRow = 0;
		INT nCol = 0;
		CHAR **pazResult = NULL;
		nResult = sqlite3_get_table(pObj, "select * from UserInfo", &pazResult, &nRow, &nCol, &pszError);
		if ( SQLITE_OK != nResult )
		{
			printf("%s\n", pszError);
		}
		else
		{
			// 解析表数据;
			std::vector<string> vValues;
			for ( int i = 1; i <= nRow; i++ )
			{
				for(int j = 0; j<nCol; j++)
				{
#ifdef _UNICODE
					WCHAR wszValue[256] = {0};
					StringProcess::ascii_to_unicode(pazResult[i*nCol + j], wszValue);
					vValues.push_back(wszValue);
#else
					vValues.push_back(pazResult[i*nCol + j]);
#endif
				}

			}

			// 释放;
			sqlite3_free_table(pazResult);
		}
#endif

#if 1
		if (CDataImpl::GetInstance()->Open())
		{
			if (0)
			{// 插入数据;
				satdDatabase::complieServer tagServer;
				tagServer.set_server("192.168.1.1");
				tagServer.set_user("home");
				tagServer.set_password("password99999");
				CDataImpl::GetInstance()->InsertComplieServer(tagServer);

				// 查询数据;
				satdDatabase::listServer list;
				int nRow = CDataImpl::GetInstance()->QueryComplieServer(list);
				for (int i = 0; i < list.servers_size(); i++)
				{
					satdDatabase::complieServer ser = list.servers(i);
					OutputDebugString(ser.server().c_str());  OutputDebugString("\n");
					OutputDebugString(ser.user().c_str()); OutputDebugString("\n");
					OutputDebugString(ser.password().c_str()); OutputDebugString("\n");
				}
			}

			if (0)
			{
				satdDatabase::model tagModel;
				tagModel.set_name("set_name");
				tagModel.set_softsuffix("set_softsuffix");
				
				satdDatabase::listEndFlag* pflags =  tagModel.mutable_flags();
				pflags->add_terminator("end1");
				pflags->add_terminator("end2");
				pflags->add_terminator("end3");
				pflags->add_terminator("end4");
				pflags->add_terminator("end5");
				
				satdDatabase::modelContent *pMc = tagModel.mutable_content();
				pMc->set_complietime("set_complietime");
				pMc->set_projectmodel("set_projectmodel");
				pMc->set_productname("set_productname");
				pMc->set_svndir("set_svndir");
				pMc->set_command("set_command");
				pMc->set_projectname("set_projectname");
				pMc->set_projectsn("set_projectsn");
				pMc->set_softver("set_softver");
				pMc->set_svnver("set_svnver");
				pMc->set_projectid("set_projectid");
				pMc->set_zone("set_zone");
				pMc->set_uistyle("set_uistyle");
				pMc->set_compliefiledir("set_compliefiledir");
				pMc->set_username("set_username");
				pMc->set_time("set_time");

				CDataImpl::GetInstance()->InsertAutoComplieModel(tagModel);

				// 查询;
				satdDatabase::listModel models;
				CDataImpl::GetInstance()->QueryAutoComplieModel(models);

				for ( int i = 0; i < models.models_size(); i++ )
				{
					satdDatabase::model md = models.models(i);
					OutputDebugString(md.name().c_str()); OutputDebugString("\n");
					OutputDebugString(md.softsuffix().c_str()); OutputDebugString("\n");
					
					pflags = md.mutable_flags();
					for ( int j = 0; j < md.mutable_flags()->terminator_size(); j++)
					{
						OutputDebugString(pflags->terminator(j).c_str()); OutputDebugString("\n");
					}

					pMc = md.mutable_content();
					OutputDebugString(pMc->compliefiledir().c_str()); OutputDebugString("\n");
					OutputDebugString(pMc->projectmodel().c_str()); OutputDebugString("\n");
					OutputDebugString(pMc->productname().c_str()); OutputDebugString("\n");
					OutputDebugString(pMc->svndir().c_str()); OutputDebugString("\n");
					OutputDebugString(pMc->command().c_str()); OutputDebugString("\n");
					OutputDebugString(pMc->projectname().c_str()); OutputDebugString("\n");
					OutputDebugString(pMc->projectsn().c_str()); OutputDebugString("\n");
					OutputDebugString(pMc->softver().c_str()); OutputDebugString("\n");
					OutputDebugString(pMc->projectid().c_str()); OutputDebugString("\n");

					OutputDebugString(pMc->zone().c_str()); OutputDebugString("\n");
					OutputDebugString(pMc->uistyle().c_str()); OutputDebugString("\n");
					OutputDebugString(pMc->compliefiledir().c_str()); OutputDebugString("\n");
					OutputDebugString(pMc->username().c_str()); OutputDebugString("\n");
					OutputDebugString(pMc->time().c_str()); OutputDebugString("\n");
				}
			}

			if (0)
			{
				satdDatabase::model tagModel;
				tagModel.set_name("set_name");
				tagModel.set_softsuffix("set_softsuffix__dddddd");

				satdDatabase::listEndFlag* pflags = tagModel.mutable_flags();
				pflags->add_terminator("end111");
				pflags->add_terminator("end222");
				pflags->add_terminator("end333");
				pflags->add_terminator("end444");
				pflags->add_terminator("end555");

				satdDatabase::modelContent *pMc = tagModel.mutable_content();
				pMc->set_complietime("set_complietimedddd");
				pMc->set_projectmodel("set_projectmodelddd");
				pMc->set_productname("set_productnameddd");
				pMc->set_svndir("set_svndidddr");
				pMc->set_command("set_commadddnd");
				pMc->set_projectname("set_projectnameddd");
				pMc->set_projectsn("set_projectsnddd");
				pMc->set_softver("set_softverddd");
				pMc->set_svnver("set_svnverddd");
				pMc->set_projectid("set_projectidddd");
				pMc->set_zone("set_zoneddd");
				pMc->set_uistyle("set_uistyleddd");
				pMc->set_compliefiledir("set_compliefiledirddd");
				pMc->set_username("set_username");
				pMc->set_time("set_time");

				CDataImpl::GetInstance()->UpdateAutoCompliteModel(tagModel);

				// 查询;
				satdDatabase::listModel models;
				CDataImpl::GetInstance()->QueryAutoComplieModel(models);

				for (int i = 0; i < models.models_size(); i++)
				{
					satdDatabase::model md = models.models(i);
					OutputDebugString(md.name().c_str()); OutputDebugString("\n");
					OutputDebugString(md.softsuffix().c_str()); OutputDebugString("\n");

					pflags = md.mutable_flags();
					for (int j = 0; j < md.mutable_flags()->terminator_size(); j++)
					{
						OutputDebugString(pflags->terminator(j).c_str()); OutputDebugString("\n");
					}

					pMc = md.mutable_content();
					OutputDebugString(pMc->compliefiledir().c_str()); OutputDebugString("\n");
					OutputDebugString(pMc->projectmodel().c_str()); OutputDebugString("\n");
					OutputDebugString(pMc->productname().c_str()); OutputDebugString("\n");
					OutputDebugString(pMc->svndir().c_str()); OutputDebugString("\n");
					OutputDebugString(pMc->command().c_str()); OutputDebugString("\n");
					OutputDebugString(pMc->projectname().c_str()); OutputDebugString("\n");
					OutputDebugString(pMc->projectsn().c_str()); OutputDebugString("\n");
					OutputDebugString(pMc->softver().c_str()); OutputDebugString("\n");
					OutputDebugString(pMc->projectid().c_str()); OutputDebugString("\n");

					OutputDebugString(pMc->zone().c_str()); OutputDebugString("\n");
					OutputDebugString(pMc->uistyle().c_str()); OutputDebugString("\n");
					OutputDebugString(pMc->compliefiledir().c_str()); OutputDebugString("\n");
					OutputDebugString(pMc->username().c_str()); OutputDebugString("\n");
					OutputDebugString(pMc->time().c_str()); OutputDebugString("\n");
				}
			}

			if (1)
			{
				CDataImpl::GetInstance()->ExecteSQL("delete from model where name = 'set_name'");
			}
		}
#endif

		//getchar();
		system("pause");
	}

	return nRetCode;
}
