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

📄 sqlapi.cpp

📁 通用的数据库中间库
💻 CPP
📖 第 1 页 / 共 5 页
字号:
	m_tm.tm_mon = nMonth - 1;
	m_tm.tm_mday = nDay;
	m_tm.tm_hour = nHour;
	m_tm.tm_min = nMinute;
	m_tm.tm_sec = nSecond;

	// complete structure
	m_tm.tm_isdst = -1;

	// Validate year and month
	if(nYear > 9999 || nMonth < 1 || nMonth > 12)
		return;

	//  Check for leap year and set the number of days in the month
	bool bLeapYear = ((nYear & 3) == 0) &&
		((nYear % 100) != 0 || (nYear % 400) == 0);

	int nDaysInMonth =
		m_saMonthDays[nMonth] - m_saMonthDays[nMonth-1] +
		((bLeapYear && nDay == 29 && nMonth == 2) ? 1 : 0);

	// Finish validating the date
	if (nDay < 1 || nDay > nDaysInMonth ||
		nHour > 23 || nMinute > 59 ||
		nSecond > 59)
	{
		return;
	}

	// Cache the date in days
	long nDate;

	//It is a valid date; make Jan 1, 1AD be 1
	nDate = nYear*365L + nYear/4 - nYear/100 + nYear/400 +
		m_saMonthDays[nMonth-1] + nDay;

	//  If leap year and it's before March, subtract 1:
	if (nMonth <= 2 && bLeapYear)
		--nDate;

	//   -1 because 1/1/0 is Sat.
	m_tm.tm_wday = (int)((nDate - 1) % 7L);
	// -1 because we want zero-based
	m_tm.tm_yday = nDate - ((nYear-1)*365L + (nYear-1)/4 - (nYear-1)/100 + (nYear-1)/400 +
		m_saMonthDays[12-1] + 31) - 1;

	m_nFraction = 0;
}

unsigned long SADateTime::Fraction() const
{
	return m_nFraction;
}
unsigned long &SADateTime::Fraction()
{
	return m_nFraction;
}

int SADateTime::GetYear() const
{
	return m_tm.tm_year + 1900;
}

int SADateTime::GetMonth() const
{
	return m_tm.tm_mon + 1;
}

int SADateTime::GetDay() const
{
	return m_tm.tm_mday;
}

int SADateTime::GetHour() const
{
	return m_tm.tm_hour;
}

int SADateTime::GetMinute() const
{
	return m_tm.tm_min;
}

int SADateTime::GetSecond() const
{
	return m_tm.tm_sec;
}

int SADateTime::GetDayOfWeek() const
{
	return m_tm.tm_wday + 1;
}

int SADateTime::GetDayOfYear() const
{
	return m_tm.tm_yday + 1;
}

/*static */
SADateTime SADateTime::GetCurrentTime()
{
	SADateTime dt;
	time_t long_time;

	time(&long_time);				// Get time as long integer
#if defined(_REENTRANT)
	localtime_r(&long_time, &(struct tm &)dt);	// Convert to local time
#else
	dt = *localtime(&long_time);	// Convert to local time
#endif

	return dt;
}

//////////////////////////////////////////////////////////////////////
// saOptions Class
//////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

saOptions::saOptions()
{
	m_nOptionCount = 0;
	m_ppOptions	= NULL;
}

/*virtual */
saOptions::~saOptions()
{
	while(m_nOptionCount)
		delete m_ppOptions[--m_nOptionCount];
	if(m_ppOptions)
	{
		free(m_ppOptions);
		m_ppOptions = NULL;
	}
}

SAString &saOptions::operator[](const SAString &sOptionName)
{
	SAParam *pRef = NULL;

	// check if option already exists
	for(int i = 0; i < m_nOptionCount; i++)
		if(m_ppOptions[i]->Name().CompareNoCase(sOptionName) == 0)
		{
			pRef = m_ppOptions[i];
			break;
		}

	if(!pRef)	// create new option
	{
		pRef = new SAParam(NULL, sOptionName, SA_dtString, -1, -1, SA_ParamInputOutput);

		m_ppOptions = (SAParam**)realloc(m_ppOptions, (m_nOptionCount+1)*sizeof(SAParam*));
		m_ppOptions[m_nOptionCount] = pRef;
		++m_nOptionCount;
	}

	return pRef->setAsString();
}

SAString saOptions::operator[](const SAString &sOptionName) const
{
	SAString s;

	for(int i = 0; i < m_nOptionCount; i++)
		if(m_ppOptions[i]->Name().CompareNoCase(sOptionName) == 0)
			return m_ppOptions[i]->asString();

	return "";
}

//////////////////////////////////////////////////////////////////////
// SAPos Class
//////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

SAPos::SAPos(int nByID)
{
	m_sName.Format(_SA("%d"), nByID);
}

SAPos::SAPos(const SAString& sByName) :
	m_sName(sByName)
{
}

//////////////////////////////////////////////////////////////////////
// SAConnection Class

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

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

SAConnection::SAConnection()
{
	m_eSAClient			= SA_Client_NotSpecified;
	m_pISAConnection	= NULL;
	m_pCommands			= NULL;

	m_eIsolationLevel	= SA_LevelUnknown;
	m_eAutoCommit		= SA_AutoCommitUnknown;
}

SAConnection::~SAConnection()
{
	// we are in destructor, so no exception throwing
	try
	{
		setClient(SA_Client_NotSpecified);

		// walk through registered commands and reregister them
		sa_Commands **pp = &m_pCommands;
		while(*pp)
		{
			SACommand *pCommand = (*pp)->pCommand;
			assert(pCommand);

			pCommand->m_pConnection = NULL;

			pp = &(*pp)->Next;
		}
	}
	catch(SAException &)
	{
		delete m_pISAConnection;
	}
}

void SAConnection::EnumCursors(EnumCursors_t fn, void *pAddlData)
{
	sa_Commands *p = m_pCommands;
	while(p)
	{
		fn(p->pISACursor, pAddlData);
		p = p->Next;
	}
}

void SAConnection::RegisterCommand(SACommand *pCommand)
{
	sa_Commands **pp = &m_pCommands;
	while(*pp)
	{
		assert((*pp)->pCommand != pCommand);
		pp = &(*pp)->Next;
	}

	*pp = new sa_Commands;
	(*pp)->pCommand = pCommand;
	(*pp)->pISACursor = m_pISAConnection? m_pISAConnection->NewCursor(pCommand) : NULL;
	(*pp)->Next = NULL;
}

void SAConnection::UnRegisterCommand(SACommand *pCommand)
{
	sa_Commands **pp = &m_pCommands;
	while(*pp)
	{
		if((*pp)->pCommand == pCommand)
		{
			sa_Commands *pNext = (*pp)->Next;
			delete (*pp)->pISACursor;
			delete *pp;
			*pp = pNext;
			return;
		}
		pp = &(*pp)->Next;
	}

	assert(false);
}

ISACursor *SAConnection::GetISACursor(SACommand *pCommand)
{
	sa_Commands *p = m_pCommands;
	while(p)
	{
		if(p->pCommand == pCommand)
			return p->pISACursor;
		p = p->Next;
	}

	assert(false);
	return NULL;
}

void SAConnection::setClient(SAClient_t eSAClient)
{
	// nothing to do
	if(eSAClient == m_eSAClient)
		return;

	// dispose previous  connection and release client interface
	if(m_eSAClient != SA_Client_NotSpecified)
	{
		if(isConnected())
			Disconnect();
		m_pISAConnection->UnInitializeClient();
		delete m_pISAConnection;
		m_pISAConnection = 0;
		m_eSAClient = SA_Client_NotSpecified;
	}

	// set new client interface
	if(eSAClient != SA_Client_NotSpecified)
	{
		ISAClient *pISAClient = stat_SAClients[eSAClient];
		if(!pISAClient)
			throw SAException(SA_Library_Error, -1, -1, IDS_CLIENT_NOT_SUPPORTED);

		ISAConnection *pISAConnection = pISAClient->QueryConnectionInterface(this);
		try
		{
			pISAConnection->InitializeClient();
		}
		catch(SAException &)	// clean up on error
		{
			delete pISAConnection;
			throw;
		}
		m_pISAConnection = pISAConnection;
		m_eSAClient = eSAClient;
	}

	// walk through registered commands and reregister them
	sa_Commands **pp = &m_pCommands;
	while(*pp)
	{
		SACommand *pCommand = (*pp)->pCommand;
		assert(pCommand);

		delete (*pp)->pISACursor;
		(*pp)->pISACursor = NULL;
		(*pp)->pISACursor = m_pISAConnection? m_pISAConnection->NewCursor(pCommand) : NULL;
		pp = &(*pp)->Next;
	}
}

SAClient_t SAConnection::Client() const
{
	return m_eSAClient;
}

long SAConnection::ClientVersion() const
{
	if(!m_pISAConnection)
		throw SAException(SA_Library_Error, -1, -1, IDS_CLIENT_NOT_SET);

	return m_pISAConnection->GetClientVersion();
}

long SAConnection::ServerVersion() const
{
	if(!m_pISAConnection)
		throw SAException(SA_Library_Error, -1, -1, IDS_CLIENT_NOT_SET);

	return m_pISAConnection->GetServerVersion();
}

SAString SAConnection::ServerVersionString() const
{
	if(!m_pISAConnection)
		throw SAException(SA_Library_Error, -1, -1, IDS_CLIENT_NOT_SET);

	return m_pISAConnection->GetServerVersionString();
}

bool SAConnection::isConnected() const
{
	if(!m_pISAConnection)
		return false;

	return m_pISAConnection->IsConnected();
}

void SAConnection::Connect(
	const SAString &sDBString,
	const SAString &sUserID,
	const SAString &sPassword,
	SAClient_t eSAClient)
{
	if(eSAClient != SA_Client_NotSpecified)
		setClient(eSAClient);

	if(!m_pISAConnection)
		throw SAException(SA_Library_Error, -1, -1, IDS_CLIENT_NOT_SET);

	// now actually connect
	m_pISAConnection->Connect(sDBString, sUserID, sPassword);

#if !defined(SA_NO_TRIAL)
	CheckTrial();
#endif	// !defined(SA_NO_TRIAL)
}

void SAConnection::Disconnect()
{
	if(!m_pISAConnection)
		throw SAException(SA_Library_Error, -1, -1, IDS_CLIENT_NOT_SET);

	// walk through all commands and close them
	sa_Commands *p = m_pCommands;
	while(p)
	{
		SACommand *pCommand = p->pCommand;
		assert(pCommand);

		if(pCommand->isOpened())
			pCommand->Close();

		p = p->Next;
	}

	// now actually disconnect
	m_pISAConnection->Disconnect();
}

void SAConnection::Commit()
{
	if(!m_pISAConnection)
		throw SAException(SA_Library_Error, -1, -1, IDS_CLIENT_NOT_SET);

	if(m_pISAConnection->IsConnected())
		m_pISAConnection->Commit();
}

void SAConnection::Rollback()
{
	if(!m_pISAConnection)
		throw SAException(SA_Library_Error, -1, -1, IDS_CLIENT_NOT_SET);

	if(m_pISAConnection->IsConnected())
		m_pISAConnection->Rollback();
}

void SAConnection::setIsolationLevel(
	SAIsolationLevel_t eIsolationLevel)
{
	if(!m_pISAConnection)
		throw SAException(SA_Library_Error, -1, -1, IDS_CLIENT_NOT_SET);

	if(eIsolationLevel == m_eIsolationLevel)
		return;
	if(eIsolationLevel == SA_LevelUnknown)
		return;

	m_pISAConnection->setIsolationLevel(eIsolationLevel);
	m_eIsolationLevel = eIsolationLevel;
}

SAIsolationLevel_t SAConnection::IsolationLevel() const
{
	return m_eIsolationLevel;
}

void SAConnection::setAutoCommit(SAAutoCommit_t eAutoCommit)
{
	if(!m_pISAConnection)
		throw SAException(SA_Library_Error, -1, -1, IDS_CLIENT_NOT_SET);

	if(eAutoCommit == m_eAutoCommit)
		return;
	if(eAutoCommit == SA_AutoCommitUnknown)
		return;

	m_pISAConnection->setAutoCommit(eAutoCommit);
	m_eAutoCommit = eAutoCommit;

	// notify commands
	sa_Commands *p = m_pCommands;
	while(p)
	{
		p->pISACursor->OnConnectionAutoCommitChanged();
		p = p->Next;
	}
}

SAAutoCommit_t SAConnection::AutoCommit() const
{
	return m_eAutoCommit;
}

SAString &SAConnection::setOption(const SAString &sOptionName)
{
	SAString &s = m_Options[sOptionName];
	return s;
}

SAString SAConnection::Option(const SAString &sOptionName) const
{
	SAString s = m_Options[sOptionName];
	return s;
}

saAPI *SAConnection::NativeAPI() const
{
	if(!m_pISAConnection)
		throw SAException(SA_Library_Error, -1, -1, IDS_CLIENT_NOT_SET);

	return m_pISAConnection->NativeAPI();
}

saConnectionHandles *SAConnection::NativeHandles()
{
	if(!m_pISAConnection)
		throw SAException(SA_Library_Error, -1, -1, IDS_CLIENT_NOT_SET);

	return m_pISAConnection->NativeHandles();
}

//////////////////////////////////////////////////////////////////////
// SACommand Class
//////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

void SACommand::Init()
{
	m_pConnection = NULL;

	m_eCmdType = SA_CmdUnknown;
	m_bPrepared = false;
	m_bExecuted = false;
	m_bFieldsDescribed = false;
	m_bSelectBuffersSet = false;

	m_bParamsKnown = false;
	m_nPlaceHolderCount = 0;
	m_ppPlaceHolders = NULL;
	m_nParamCount = 0;
	m_ppParams	= NULL;
	m_nCurParamID = 0;

	m_nFieldCount = 0;
	m_ppFields = 

⌨️ 快捷键说明

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