#include "StdAfx.h"
#include "DataImpl.h"
#include "CharEncoding.h"
#include "Global.h"

// MID表;
#define _CREATE_MID_TABLE_ \
	"CREATE TABLE mid \
(bid  TEXT(16) NOT NULL,\
number  INTEGER NOT NULL,\
pid  INTEGER NOT NULL,\
ctype  TEXT(32) NOT NULL,\
version  TEXT(32) NOT NULL,\
purl  TEXT NOT NULL,\
psize  INTEGER NOT NULL,\
pmd5  TEXT(32) NOT NULL,\
status  INTEGER NOT NULL DEFAULT 0,\
start_date  DATETIME DEFAULT current_timestamp,\
finish_date  DATETIME DEFAULT '',\
des  TEXT NOT NULL DEFAULT \"未下载\",\
PRIMARY KEY (bid ASC));"
#define _SELECT_MID_TABLE_ "SELECT bid, number, pid, ctype, version, purl, psize, pmd5, status, start_date, finish_date, des FROM mid"
#define _INSERT_MID_TABLE_ "INSERT INTO mid(bid, number, pid, ctype, version, purl, psize, pmd5)VALUES\
(%s, %s, %s, %s, %s, %s, %s, %s);"

// Key表;
#define _CREATE_KEYS_TABLE_ \
	"CREATE TABLE keys \
(bid  TEXT(16) NOT NULL,\
sn  TEXT(32) PRIMARY KEY NOT NULL,\
pid  INTEGER NOT NULL,\
keys  TEXT NOT NULL,\
copy_date  DATETIME DEFAULT '',\
report_date  DATETIME DEFAULT '');"
#define _SELECT_KEYS_TABLE_ "SELECT bid, pid, sn, keys, copy_date, report_date FROM keys"

// Log表;
#define _CREATE_LOG_TABLE_ \
	"CREATE TABLE log \
(bid  TEXT(16) NOT NULL,\
type  TEXT(32) NOT NULL,\
sn  TEXT(32) NOT NULL,\
content  TEXT NOT NULL,\
gdate  DATETIME DEFAULT current_timestamp,\
report_date  DATETIME DEFAULT '',\
PRIMARY KEY (type, sn));"
#define _SELECT_LOG_TABLE_ "SELECT type, bid, sn, content, gdate, report_date FROM log"

#define _SELECT_BID_INFO__ \
	"select ifnull(mid.number,0) as tc, \
count(keys.bid) as ac,\
count(case when keys.copy_date <> '' then '抄写成功' end) as cc,\
count(case when keys.report_date <> '' and keys.copy_date <> '' then '上报成功' end) as rc \
from mid inner join keys on mid.bid = keys.bid where mid.bid = '%s';"

#define FREE_MSG if ( psqlite_error )  \
	printf("%s\n",psqlite_error),\
	sqlite3_free(psqlite_error),\
	psqlite_error = NULL


CDataImpl::CDataImpl(void):m_psqlite3(NULL)
{
	//sqlite3_threadsafe();
}

CDataImpl::~CDataImpl(void)
{
	Close();
}

BOOL CDataImpl::Open()
{
	Close();
	TCHAR szpath[MAX_PATH] = {0};
	_stprintf_s(szpath, _T("%sdb\\ogc.db"), GLOBAL::g_szModulePath);
	std::string strPath;
#ifdef _UNICODE
	if ( !CharEncoding::UNICODE2UTF8(szpath,strPath))
#else
	if ( !CharEncoding::ASCII2UTF8(szpath,strPath))
#endif
	{
		return FALSE;
	}

	INT nResult = sqlite3_open(strPath.c_str(), &m_psqlite3);
	if ( nResult != SQLITE_OK )
		return FALSE;

	// 创建表;
	char* psqlite_error = NULL;
	if ( !QueryTable("DebugOrder"))
	{
		sqlite3_exec(m_psqlite3, _CREATE_ORDER_, NULL, NULL, &psqlite_error);
		GLOBAL::WriteTextLog(_T("sqlite3 error:%s,%s"),_CREATE_ORDER_, psqlite_error);
		FREE_MSG;
	}

	if ( !QueryTable("DebugSN"))
	{
		sqlite3_exec(m_psqlite3, _CREATE_SN_, NULL, NULL, &psqlite_error);
		GLOBAL::WriteTextLog(_T("sqlite3 error:%s,%s"),_CREATE_SN_, psqlite_error);
		FREE_MSG;
	}

	return TRUE;
}

void CDataImpl::Close()
{
	if ( m_psqlite3 )
		sqlite3_close(m_psqlite3);

	m_psqlite3 = NULL;
}

BOOL CDataImpl::ExecteSQL(IN LPCSTR lpSQL)
{
	if ( lpSQL == NULL || lpSQL[0] == '\0' )
	{
		GLOBAL::WriteTextLog(_T("ExecteSQL：执行语句空！"));
		return FALSE;
	}

	if(m_psqlite3 == NULL)
		return FALSE;

	char* psqlite_error = NULL;
	int sqlite_error = sqlite3_exec(m_psqlite3, lpSQL, NULL, 0, &psqlite_error);
	if(SQLITE_OK != sqlite_error)
	{
		GLOBAL::WriteTextLog(_T("sqlite3 error:%s,%s"),lpSQL, psqlite_error);
		FREE_MSG;
		return FALSE;
	}

	return TRUE;
}

BOOL CDataImpl::QueryTable(std::string table)
{
	if ( table.size() == 0 )
	{
		GLOBAL::WriteTextLog(_T("QueryTable：表名空！"));
		return FALSE;
	}

	if(m_psqlite3 == NULL)
		return FALSE;

	INT nRow = 0;
	INT nCol = 0;

	char** pazResult = NULL;
	char* psqlite_error = NULL;
	char szSql[MAX_PATH] = {0};
	sprintf_s(szSql, "select * from sqlite_master where type = 'table' and name = '%s'", table.c_str());
	int sqlite_error = sqlite3_get_table(m_psqlite3, szSql, &pazResult, &nRow, &nCol, &psqlite_error);
	if ( sqlite_error != SQLITE_OK)
	{
		GLOBAL::WriteTextLog(_T("sqlite3 error:%s,%s"),szSql, psqlite_error);
		FREE_MSG;
		return FALSE;
	}

	sqlite3_free_table(pazResult);

	return nRow != 0;
}

INT CDataImpl::InsertOrder(const TB_ORDER &order)
{
	return InsertOrder(order.strOrder, order.strDeltaEType, order.strDeltaEValue, order.nMode);
}

INT CDataImpl::InsertOrder(std::string strOrder, std::string strDeltaEType, std::string strDeltaEValue, int nMode)
{
	if(m_psqlite3 == NULL)
		return -1;

	char szMode[8] = {0};
	_itoa_s(nMode, szMode, 10);
	std::string strInsert = "INSERT INTO DebugOrders(OrderID, DEType, DEValue, Mode) VALUES ('";
	strInsert.append(strOrder);
	strInsert.append("','");
	strInsert.append(strDeltaEType);
	strInsert.append("','");
	strInsert.append(strDeltaEValue);
	strInsert.append("','");
	strInsert.append(szMode);
	strInsert.append("');");

	char* psqlite_error = NULL;
	int sqlite_error = sqlite3_exec(m_psqlite3, strInsert.c_str(), NULL, 0, &psqlite_error);
	if(SQLITE_OK != sqlite_error)
	{
		GLOBAL::WriteTextLog(_T("sqlite3 error:%s,%s"),strInsert.c_str(), psqlite_error);
		FREE_MSG;
	}

	return sqlite_error;
}

INT CDataImpl::InsertSN(const TB_SN &sn_data)
{
	return InsertSN(sn_data.strOrder, sn_data.strLine, sn_data.strSN, sn_data.strStartTime, sn_data.strFinishedTime, sn_data.nElapsed, sn_data.nTestResult, sn_data.dDeltaAVGEValue, sn_data.strDetails);
}

INT CDataImpl::InsertSN(std::string strOrder, std::string strLine, std::string strSN, std::string strStartTime, std::string strFinishTime, int nElapsed, int nTestResult, DOUBLE dDeltaAVGEValue, std::string strDetails )
{
	if(m_psqlite3 == NULL)
		return -1;

	char szValue[16] = {0};
	std::string strInsert = "INSERT INTO DebugSN(OrderID, Line, SN, StartTime, FinishTime, ElapsedTime, TestResult, DEAvgValue, Outcome) VALUES ('";
	strInsert.append(strOrder);
	strInsert.append("','");
	strInsert.append(strLine);
	strInsert.append("','");
	strInsert.append(strSN);
	strInsert.append("','");
	strInsert.append(strStartTime);
	strInsert.append("','");
	strInsert.append(strFinishTime);

	_itoa_s(nElapsed, szValue, 10);
	strInsert.append("','");
	strInsert.append(szValue);

	_itoa_s(nTestResult, szValue, 10);
	strInsert.append("','");
	strInsert.append(szValue);

	_gcvt_s(szValue, dDeltaAVGEValue, 3);
	strInsert.append("','");
	strInsert.append(szValue);

	strInsert.append("','");
	strInsert.append(strDetails);
	strInsert.append("');");

	char* psqlite_error = NULL;
	int sqlite_error = sqlite3_exec(m_psqlite3, strInsert.c_str(), NULL, 0, &psqlite_error);
	if(SQLITE_OK != sqlite_error)
	{
		GLOBAL::WriteTextLog(_T("sqlite3 error:%s,%s"),strInsert.c_str(), psqlite_error);
		FREE_MSG;
	}

	return sqlite_error;
}

INT CDataImpl::QueryOrder(std::string strOrder, TB_ORDER &order)
{
	if ( m_psqlite3 == NULL )
		return -1;

	INT nRow = 0;
	INT nCol = 0;

	char** pazResult = NULL;
	char* psqlite_error = NULL;
	std::string strSql = _SELECT_ORDER_;
	strSql.append(" WHERE OrderID = '");
	strSql.append(strOrder);
	strSql.append("';");
	int sqlite_error = sqlite3_get_table(m_psqlite3, strSql.c_str(), &pazResult, &nRow, &nCol, &psqlite_error);
	if ( sqlite_error != SQLITE_OK)
	{
		GLOBAL::WriteTextLog(_T("sqlite3 error:%s,%s"),strSql.c_str(), psqlite_error);
		FREE_MSG;
		return -1;
	}

	if( nRow == 1)
	{
#ifndef USE_UTF8
		order.strOrder = pazResult[nCol+0];
		order.strDeltaEType = pazResult[nCol+1];
		order.strDeltaEValue = pazResult[nCol+2];
		order.nMode = atoi(pazResult[nCol+3]);
		order.strCreateTime = pazResult[nCol+4];
#else
		// 由Native for SQLite3插入的数据，都是utf8格式;
		order.strOrder = CharEncoding::UTF82ASCII(pazResult[nCol+0]);
		order.strDeltaEType = CharEncoding::UTF82ASCII(pazResult[nCol+1]);
		order.strDeltaEValue = CharEncoding::UTF82ASCII(pazResult[nCol+2]);
		order.nMode = atoi(pazResult[nCol+3]);
		order.strCreateTime = CharEncoding::UTF82ASCII(pazResult[nCol+4]);
#endif
	}

	sqlite3_free_table(pazResult);

	return nRow;
}

INT CDataImpl::QueryOrders(std::vector<TB_ORDER> &vtOrders, std::string strBeginTime, std::string EndTime)
{
	if ( m_psqlite3 == NULL )
		return -1;

	INT nRow = 0;
	INT nCol = 0;

	char** pazResult = NULL;
	char* psqlite_error = NULL;
	std::string strSql = _SELECT_ORDER_;
	strSql.append(" WHERE CreateTime >= '");
	strSql.append(strBeginTime);
	strSql.append("' AND CreateTime <= '");
	strSql.append(EndTime);
	strSql.append("';");
	int sqlite_error = sqlite3_get_table(m_psqlite3, strSql.c_str(), &pazResult, &nRow, &nCol, &psqlite_error);
	if ( sqlite_error != SQLITE_OK)
	{
		GLOBAL::WriteTextLog(_T("sqlite3 error:%s,%s"),strSql.c_str(), psqlite_error);
		FREE_MSG;
		return -1;
	}

	vtOrders.resize(nRow);
	std::vector<TB_ORDER>::iterator it = vtOrders.begin();
	for(int i = 1; i <= nRow; i++, it++)
	{
#ifndef USE_UTF8
		it->strOrder = pazResult[i*nCol+0];
		it->strDeltaEType = pazResult[i*nCol+1];
		it->strDeltaEValue = pazResult[i*nCol+2];
		it->nMode = atoi(pazResult[i*nCol+3]);
		it->strCreateTime = pazResult[i*nCol+4];
#else
		// 由Native for SQLite3插入的数据，都是utf8格式;
		it->strOrder = CharEncoding::UTF82ASCII(pazResult[i*nCol+0]);
		it->strDeltaEType = CharEncoding::UTF82ASCII(pazResult[i*nCol+1]);
		it->strDeltaEValue = CharEncoding::UTF82ASCII(pazResult[i*nCol+2]);
		it->nMode = atoi(pazResult[i*nCol+3]);
		it->strCreateTime = CharEncoding::UTF82ASCII(pazResult[i*nCol+4]);
#endif
	}

	sqlite3_free_table(pazResult);

	return nRow;
}

INT CDataImpl::QuerySNData(std::string strSN, TB_SN &sn_data)
{
	if ( m_psqlite3 == NULL )
		return -1;

	INT nRow = 0;
	INT nCol = 0;

	char** pazResult = NULL;
	char* psqlite_error = NULL;
	std::string strSql = _SELECT_SN_;
	strSql.append(" WHERE SN = '");
	strSql.append(strSN);
	strSql.append("';");
	int sqlite_error = sqlite3_get_table(m_psqlite3, strSql.c_str(), &pazResult, &nRow, &nCol, &psqlite_error);
	if ( sqlite_error != SQLITE_OK)
	{
		GLOBAL::WriteTextLog(_T("sqlite3 error:%s,%s"),strSql.c_str(), psqlite_error);
		FREE_MSG;
		return -1;
	}

	if ( nRow == 1)
	{
#ifndef USE_UTF8
		sn_data.strOrder = pazResult[nCol+0];
		sn_data.strSN = pazResult[nCol+1];
		sn_data.strLine = pazResult[nCol+2];
		sn_data.strStartTime = pazResult[nCol+3];
		sn_data.strFinishedTime = pazResult[nCol+4];
		sn_data.nElapsed = atoi(pazResult[nCol+5]);
		sn_data.nTestResult = atoi(pazResult[nCol+6]);
		sn_data.dDeltaAVGEValue = atof(pazResult[nCol+7]);
		sn_data.strDetails = pazResult[nCol+8];
#else
		// 由Native for SQLite3插入的数据，都是utf8格式;
		sn_data.strOrder = CharEncoding::UTF82ASCII(pazResult[nCol+0]);
		sn_data.strSN = CharEncoding::UTF82ASCII(pazResult[nCol+1]);
		sn_data.strLine = CharEncoding::UTF82ASCII(pazResult[nCol+2]);
		sn_data.strStartTime = CharEncoding::UTF82ASCII(pazResult[nCol+3]);
		sn_data.strFinishedTime = CharEncoding::UTF82ASCII(pazResult[nCol+4]);
		sn_data.nElapsed = atoi(pazResult[nCol+5]);
		sn_data.nTestResult = atoi(pazResult[nCol+6]);
		sn_data.dDeltaAVGEValue = atof(pazResult[nCol+7]);
		sn_data.strDetails = CharEncoding::UTF82ASCII(pazResult[nCol+8]);
#endif
	}

	sqlite3_free_table(pazResult);

	return nRow;
}

INT CDataImpl::QuerySNData(std::string strOrder, std::vector<TB_SN> &vtSNdata)
{
	if ( m_psqlite3 == NULL )
		return -1;

	INT nRow = 0;
	INT nCol = 0;

	char** pazResult = NULL;
	char* psqlite_error = NULL;
	std::string strSql = _SELECT_SN_;
	strSql.append(" WHERE OrderID = '");
	strSql.append(strOrder);
	strSql.append("';");
	int sqlite_error = sqlite3_get_table(m_psqlite3, strSql.c_str(), &pazResult, &nRow, &nCol, &psqlite_error);
	if ( sqlite_error != SQLITE_OK)
	{
		GLOBAL::WriteTextLog(_T("sqlite3 error:%s,%s"),strSql.c_str(), psqlite_error);
		FREE_MSG;
		return -1;
	}

	vtSNdata.resize(nRow);
	std::vector<TB_SN>::iterator it = vtSNdata.begin();
	for(int i = 1; i <= nRow; i++, it++)
	{
#ifndef USE_UTF8
		it->strOrder = pazResult[nCol*i+0];
		it->strSN = pazResult[nCol*i+1];
		it->strLine = pazResult[nCol*i+2];
		it->strStartTime = pazResult[nCol*i+3];
		it->strFinishedTime = pazResult[nCol*i+4];
		it->nElapsed = atoi(pazResult[nCol*i+5]);
		it->nTestResult = atoi(pazResult[nCol*i+6]);
		it->dDeltaAVGEValue = atof(pazResult[nCol*i+7]);
		it->strDetails = pazResult[nCol*i+8];
#else
		// 由Native for SQLite3插入的数据，都是utf8格式;
		it->strOrder = CharEncoding::UTF82ASCII(pazResult[nCol*i+0]);
		it->strSN = CharEncoding::UTF82ASCII(pazResult[nCol*i+1]);
		it->strLine = CharEncoding::UTF82ASCII(pazResult[nCol*i+2]);
		it->strStartTime = CharEncoding::UTF82ASCII(pazResult[nCol*i+3]);
		it->strFinishedTime = CharEncoding::UTF82ASCII(pazResult[nCol*i+4]);
		it->nElapsed = atoi(pazResult[nCol*i+5]);
		it->nTestResult = atoi(pazResult[nCol*i+6]);
		it->dDeltaAVGEValue = atof(pazResult[nCol*i+7]);
		it->strDetails = CharEncoding::UTF82ASCII(pazResult[nCol*i+8]);
#endif
	}

	sqlite3_free_table(pazResult);

	return nRow;
}

INT CDataImpl::QuerySNData(std::vector<TB_SN> &vtSNData, std::string strBeginTime, std::string EndTime)
{
	if ( m_psqlite3 == NULL )
		return -1;

	INT nRow = 0;
	INT nCol = 0;

	char** pazResult = NULL;
	char* psqlite_error = NULL;
	std::string strSql = _SELECT_SN_;
	strSql.append(" WHERE StartTime >= '");
	strSql.append(strBeginTime);
	strSql.append("' AND StartTime <= '");
	strSql.append(EndTime);
	strSql.append("';");
	int sqlite_error = sqlite3_get_table(m_psqlite3, strSql.c_str(), &pazResult, &nRow, &nCol, &psqlite_error);
	if ( sqlite_error != SQLITE_OK)
	{
		GLOBAL::WriteTextLog(_T("sqlite3 error:%s,%s"),strSql.c_str(), psqlite_error);
		FREE_MSG;
		return -1;
	}

	vtSNData.resize(nRow);
	std::vector<TB_SN>::iterator it = vtSNData.begin();
	for(int i = 1; i <= nRow; i++, it++)
	{
#ifndef USE_UTF8
		it->strOrder = pazResult[nCol*i+0];
		it->strSN = pazResult[nCol*i+1];
		it->strLine = pazResult[nCol*i+2];
		it->strStartTime = pazResult[nCol*i+3];
		it->strFinishedTime = pazResult[nCol*i+4];
		it->nElapsed = atoi(pazResult[nCol*i+5]);
		it->nTestResult = atoi(pazResult[nCol*i+6]);
		it->dDeltaAVGEValue = atof(pazResult[nCol*i+7]);
		it->strDetails = pazResult[nCol*i+8];
#else
		// 由Native for SQLite3插入的数据，都是utf8格式;
		it->strOrder = CharEncoding::UTF82ASCII(pazResult[nCol*i+0]);
		it->strSN = CharEncoding::UTF82ASCII(pazResult[nCol*i+1]);
		it->strLine = CharEncoding::UTF82ASCII(pazResult[nCol*i+2]);
		it->strStartTime = CharEncoding::UTF82ASCII(pazResult[nCol*i+3]);
		it->strFinishedTime = CharEncoding::UTF82ASCII(pazResult[nCol*i+4]);
		it->nElapsed = atoi(pazResult[nCol*i+5]);
		it->nTestResult = atoi(pazResult[nCol*i+6]);
		it->dDeltaAVGEValue = atof(pazResult[nCol*i+7]);
		it->strDetails = CharEncoding::UTF82ASCII(pazResult[nCol*i+8]);
#endif
	}

	sqlite3_free_table(pazResult);

	return nRow;
}

BOOL CDataImpl::UpdateOrder(TB_ORDER &order)
{
	return UpdateOrder(order.strOrder, order.strDeltaEType, order.strDeltaEValue, order.nMode);
}

BOOL CDataImpl::UpdateOrder(std::string strOrder, std::string strDeltaEType, std::string strDeltaEValue, int nMode)
{
	if(m_psqlite3 == NULL)
		return FALSE;

	char szValue[8]={0};
	std::string strInsert = "UPDATE DebugOrders SET DEType ='";
	strInsert.append(strDeltaEType);
	strInsert.append("', DEValue='");
	strInsert.append(strDeltaEValue);
	strInsert.append("', Mode='");
	_itoa_s(nMode, szValue, 10);
	strInsert.append(szValue);
	strInsert.append("'");
	strInsert.append(" WHERE OrderID ='");
	strInsert.append(strOrder);
	strInsert.append("';");

	char* psqlite_error = NULL;
	int sqlite_error = sqlite3_exec(m_psqlite3, strInsert.c_str(), NULL, 0, &psqlite_error);
	if(SQLITE_OK != sqlite_error)
	{
		GLOBAL::WriteTextLog(_T("sqlite3 error:%s,%s"),strInsert.c_str(), psqlite_error);
		FREE_MSG;
		return FALSE;
	}

	return TRUE;
}

BOOL CDataImpl::UpdateSNData(TB_SN &sn_data)
{
	return UpdateSNData(sn_data.strOrder, sn_data.strSN, sn_data.strLine, sn_data.strStartTime, sn_data.strFinishedTime, sn_data.nElapsed, sn_data.nTestResult, sn_data.dDeltaAVGEValue, sn_data.strDetails);
}

BOOL CDataImpl::UpdateSNData(std::string strOrder,std::string strSN, std::string strLine, std::string strTestTime, std::string strFinishedTime, int nElapsed, int nTestResult, double dAVGDeltaEVlaue, std::string strDetails)
{
	if(m_psqlite3 == NULL)
		return FALSE;

	char szValue[8]={0};
	std::string strInsert = "UPDATE DebugSN SET OrderID ='";
	strInsert.append(strOrder);
	strInsert.append("', Line='");
	strInsert.append(strLine);
	strInsert.append("', StartTime='");
	strInsert.append(strTestTime);
	strInsert.append("', FinishTime='");
	strInsert.append(strFinishedTime);
	_itoa_s(nElapsed, szValue, 10);
	strInsert.append("', ElapsedTime='");
	strInsert.append(szValue);
	_itoa_s(nTestResult, szValue, 10);
	strInsert.append("', TestResult='");
	strInsert.append(szValue);
	_gcvt_s(szValue, dAVGDeltaEVlaue, 3);
	strInsert.append("', DEAvgValue='");
	strInsert.append(szValue);
	strInsert.append("', Outcome='");
	strInsert.append(strDetails);
	strInsert.append("'");
	strInsert.append(" WHERE SN ='");
	strInsert.append(strOrder);
	strInsert.append("';");

	char* psqlite_error = NULL;
	int sqlite_error = sqlite3_exec(m_psqlite3, strInsert.c_str(), NULL, 0, &psqlite_error);
	if(SQLITE_OK != sqlite_error)
	{
		GLOBAL::WriteTextLog(_T("sqlite3 error:%s,%s"),strInsert.c_str(), psqlite_error);
		FREE_MSG;
		return FALSE;
	}

	return TRUE;
}






BOOL CDataImpl::DeleteOrder(std::string strOrder)
{
	if(m_psqlite3 == NULL)
		return FALSE;

	if ( TransactionBegin() != 0 )
		return FALSE;

	CHAR szSQL[1024] = {0};
	sprintf_s(szSQL, "delete from DebugOrders where OrderID = '%s'; delete from DebugSN where OrderID = '%s';", strOrder.c_str(), strOrder.c_str());
	char* psqlite_error = NULL;
	int sqlite_error = sqlite3_exec(m_psqlite3, szSQL, NULL, 0, &psqlite_error);
	if(SQLITE_OK != sqlite_error)
	{
		GLOBAL::WriteTextLog(_T("sqlite3 error:%s,%s"),szSQL, psqlite_error);
		FREE_MSG;
		// 回滚事务;
		sqlite3_exec(m_psqlite3, "rollback;", 0, 0, &psqlite_error);
		FREE_MSG;
		return FALSE;
	}

	if ( TransactionCommit() != 0 )
		return FALSE;

	return TRUE;
}

BOOL CDataImpl::DeleteSNData(std::string strSN)
{
	if(m_psqlite3 == NULL)
		return FALSE;

	if ( TransactionBegin() != 0 )
		return FALSE;

	CHAR szSQL[1024] = {0};
	sprintf_s(szSQL, "delete from DebugSN where SN = '%s';", strSN.c_str());
	char* psqlite_error = NULL;
	int sqlite_error = sqlite3_exec(m_psqlite3, szSQL, NULL, 0, &psqlite_error);
	if(SQLITE_OK != sqlite_error)
	{
		GLOBAL::WriteTextLog(_T("sqlite3 error:%s,%s"),szSQL, psqlite_error);
		FREE_MSG;
		// 回滚事务;
		sqlite3_exec(m_psqlite3, "rollback;", 0, 0, &psqlite_error);
		FREE_MSG;
		return FALSE;
	}

	if ( TransactionCommit() != 0 )
		return FALSE;

	return TRUE;
}