📄 vmodbcconnection.cpp
字号:
//
::SQLFreeStmt( hstmt, SQL_DROP );
m_hstmt = SQL_NULL_HSTMT;
::SQLDisconnect( m_hdbc );
::SQLFreeConnect( m_hdbc );
m_hdbc = SQL_NULL_HDBC;
// start the "reconnect timer"
//
m_bReconnectTimer = true;
}
throw new VMException( __FILE__, __LINE__, "SQLExecDirect Failed.", VMException::fatal );
}
else
{
nRetCode = ::SQLRowCount( hstmt, &iResult );
if ( !CheckHstmt( nRetCode, hstmt ) )
{
GetErrorMessages( SQL_HANDLE_STMT, hstmt );
}
}
}
catch( VMException* poError )
{
::SQLCancel( hstmt );
nRetCode = ::SQLFreeStmt( hstmt, SQL_DROP );
throw poError;
}
nRetCode = ::SQLFreeStmt( hstmt, SQL_DROP );
return( iResult );
}
/* End of function "VMODBCConnection::ExecuteSQLGetRowCount"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMODBCConnection::ExecuteSQL
DESCRIPTION: used to execute a specific sql command againts the dbms
INPUT: lpszSQL - pointer to string containing the command to exec
OUTPUT: none
RETURNS: void
*/
void VMODBCConnection::ExecuteSQL( LPCTSTR lpszSQL )
{
VMPerfTimer oTimer( "ExecuteSQL" );
RETCODE nRetCode;
HSTMT hstmt;
nRetCode = ::SQLAllocStmt( m_hdbc, &hstmt );
if ( !CheckHstmt( nRetCode, hstmt ) )
{
GetErrorMessages( SQL_HANDLE_STMT, hstmt );
throw new VMException( __FILE__, __LINE__, "SQLAllocStatement Failed.", VMException::fatal );
}
try
{
OnSetOptions( hstmt );
// Give derived VMODBCConnection classes option to use parameters
//
BindParameters( hstmt );
VMPerfTimer oExecTime( "SQLExecDirect", false );
oExecTime.StartTimer();
ODBC_CALL_WAIT( ::SQLExecDirect( hstmt,
(unsigned char*) lpszSQL,
SQL_NTS ) );
oExecTime.StopTimer();
if ( !CheckHstmt( nRetCode, hstmt ) )
{
GetErrorMessages( SQL_HANDLE_STMT, hstmt );
// test for a lost database connection
//
char* pcTest = strstr( (const char*)m_auchErrorMsg, COMM_LINK_FAILED );
if( pcTest != NULL )
{
// connection has been broken
//
::SQLFreeStmt( hstmt, SQL_DROP );
m_hstmt = SQL_NULL_HSTMT;
::SQLDisconnect( m_hdbc );
::SQLFreeConnect( m_hdbc );
m_hdbc = SQL_NULL_HDBC;
// start the "reconnect timer"
//
m_bReconnectTimer = true;
}
throw new VMException( __FILE__, __LINE__, "SQLExecDirect Failed.", VMException::fatal );
}
else
{
do
{
SWORD nResultColumns;
ODBC_CALL_WAIT( ::SQLNumResultCols( hstmt, &nResultColumns ) );
if ( nResultColumns != 0 )
{
do
{
ODBC_CALL_WAIT( ::SQLFetch( hstmt ) );
}
while ( CheckHstmt( nRetCode, hstmt ) && nRetCode != SQL_NO_DATA_FOUND );
}
ODBC_CALL_WAIT( ::SQLMoreResults( hstmt ) );
}
while ( CheckHstmt( nRetCode, hstmt ) && nRetCode != SQL_NO_DATA_FOUND );
}
}
catch( VMException* poError )
{
::SQLCancel( hstmt );
nRetCode = ::SQLFreeStmt( hstmt, SQL_DROP );
throw poError;
}
nRetCode = ::SQLFreeStmt( hstmt, SQL_DROP );
}
/* End of function "VMODBCConnection::ExecuteSQL"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMODBCConnection::Cancel
DESCRIPTION: used to cancel the last command issued against the dbms
INPUT: void
OUTPUT: none
RETURNS: void
*/
void VMODBCConnection::Cancel( void )
{
assert( m_hdbc != SQL_NULL_HDBC );
::SQLCancel( m_hstmt );
}
/* End of function "VMODBCConnection::Cancel"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMODBCConnection::Close
DESCRIPTION: closes the dbms connection
INPUT: void
OUTPUT: none
RETURNS: void
*/
void VMODBCConnection::Close( void )
{
if ( m_hdbc != SQL_NULL_HDBC )
{
RETCODE nRetCode;
if ( SQL_NULL_HSTMT != m_hstmt )
{
nRetCode = ::SQLFreeStmt( m_hstmt, SQL_DROP );
}
m_hstmt = SQL_NULL_HSTMT;
nRetCode = ::SQLDisconnect( m_hdbc );
nRetCode = ::SQLFreeConnect( m_hdbc );
m_hdbc = SQL_NULL_HDBC;
}
}
/* End of function "VMODBCConnection::Close"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMODBCConnection::Free
DESCRIPTION: frees the static HENV if this is the last user of
that resource, if not, then simply dec's the use
counter for that resource
INPUT: void
OUTPUT: none
RETURNS: void
*/
void VMODBCConnection::Free( void )
{
Close();
// mjs make a static environment and when this goes to zip,
// then shut it down
//
// free henv if refcount goes to 0
//
if ( m_henvAllConnections != SQL_NULL_HENV )
{
assert ( m_nAllocatedConnections >= 0 );
if ( m_nAllocatedConnections == 0 )
{
// free last connection - release HENV
//
RETCODE nRetCodeEnv = ::SQLFreeEnv( m_henvAllConnections );
m_henvAllConnections = SQL_NULL_HENV;
}
}
}
/* End of function "VMODBCConnection::Free"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMODBCConnection::SetLoginTimeout
DESCRIPTION: sets the login time out
INPUT: dwSeconds - the time out value
hstmt - the statement handle to apply the settings to
OUTPUT: none
RETURNS: void
*/
void VMODBCConnection::SetLoginTimeout( DWORD dwSeconds, HSTMT hstmt )
{
if ( NULL != hstmt )
{
RETCODE nRetCode;
assert( m_hdbc != SQL_NULL_HDBC );
if ( m_dwLoginTimeout != -1 )
{
// Attempt to set query timeout. Ignore failure
//
nRetCode = ::SQLSetStmtOption( hstmt,
SQL_LOGIN_TIMEOUT,
dwSeconds );
if ( !Check( nRetCode ) )
{
// don't attempt it again
//
GetErrorMessages( SQL_HANDLE_STMT, hstmt );
m_dwLoginTimeout = (DWORD)-1;
}
else
{
m_dwLoginTimeout = dwSeconds;
}
}
}
else
{
m_dwLoginTimeout = dwSeconds;
}
}
/* End of function "VMODBCConnection::SetLoginTimeout"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMODBCConnection::SetQueryTimeout
DESCRIPTION: sets the query time out
INPUT: dwSeconds - the time out value
hstmt - the statement handle to apply the settings to
OUTPUT: none
RETURNS: void
*/
void VMODBCConnection::SetQueryTimeout( DWORD dwSeconds, HSTMT hstmt )
{
if ( NULL != hstmt )
{
RETCODE nRetCode;
assert( m_hdbc != SQL_NULL_HDBC );
if ( m_dwQueryTimeout != -1 )
{
// Attempt to set query timeout. Ignore failure
//
nRetCode = ::SQLSetStmtOption( hstmt,
SQL_QUERY_TIMEOUT,
dwSeconds );
if ( !Check( nRetCode ) )
{
// don't attempt it again
//
GetErrorMessages( SQL_HANDLE_STMT, hstmt );
m_dwQueryTimeout = (DWORD)-1;
}
else
{
m_dwQueryTimeout = dwSeconds;
}
}
}
else
{
m_dwQueryTimeout = dwSeconds;
}
}
/* End of function "VMODBCConnection::SetQueryTimeout"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMODBCConnection::OnSetOptions
DESCRIPTION: sets the query options that can be used by the connection/
query
INPUT: hstmt - the statement handle to apply the settings to
OUTPUT: none
RETURNS: void
*/
void VMODBCConnection::OnSetOptions( HSTMT hstmt )
{
RETCODE nRetCode;
assert( m_hdbc != SQL_NULL_HDBC );
if ( m_dwQueryTimeout != -1 )
{
// Attempt to set query timeout. Ignore failure
//
nRetCode = ::SQLSetStmtOption( hstmt,
SQL_QUERY_TIMEOUT,
m_dwQueryTimeout );
if ( !Check( nRetCode ) )
{
// don't attempt it again
//
GetErrorMessages( SQL_HANDLE_STMT, hstmt );
m_dwQueryTimeout = (DWORD)-1;
}
}
}
/* End of function "VMODBCConnection::OnSetOptions"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMODBCConnection::GetDatabaseName
DESCRIPTION: returns the database name to the caller
INPUT: void
OUTPUT: none
RETURNS: VMString containing the name of the connected database
*/
VMString VMODBCConnection::GetDatabaseName( void )
{
assert( m_hdbc != SQL_NULL_HDBC );
char szName[ MAX_TNAME_LEN ];
SWORD nResult;
RETCODE nRetCode;
nRetCode = ::SQLGetInfo( m_hdbc,
SQL_DATABASE_NAME,
szName,
sizeof( szName ),
&nResult );
if ( !Check( nRetCode ) )
{
GetErrorMessages( SQL_HANDLE_DBC, m_hdbc );
szName[ 0 ] = '\0';
}
return( szName );
}
/* End of function "VMODBCConnection::GetDatabaseName"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMODBCConnection::BeginTrans
DESCRIPTION: If the driver is capable of doing so, starts a transaction
INPUT: void
OUTPUT: none
RETURNS: true if driver is capable, false otherwise
*/
bool VMODBCConnection::BeginTrans( void )
{
assert( m_hdbc != SQL_NULL_HDBC );
if ( !m_bTransactions )
return( false );
RETCODE nRetCode;
nRetCode = ::SQLSetConnectOption( m_hdbc,
SQL_AUTOCOMMIT,
SQL_AUTOCOMMIT_OFF );
return( Check( nRetCode ) );
}
/* End of function "VMODBCConnection::BeginTrans"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMODBCConnection::CommitTrans
DESCRIPTION: if the driver is capable, and if a transaction is opened,
this call will commit the transaction
INPUT: void
OUTPUT: none
RETURNS: true if worked, false otherwise
*/
bool VMODBCConnection::CommitTrans( void )
{
assert( m_hdbc != SQL_NULL_HDBC );
if ( !m_bTransactions )
return( false );
RETCODE nRetCode;
nRetCode = ::SQLTransact( m_henvAllConnections, m_hdbc, SQL_COMMIT );
bool bSuccess = Check( nRetCode );
// Turn back on auto commit
//
nRetCode = ::SQLSetConnectOption( m_hdbc,
SQL_AUTOCOMMIT,
SQL_AUTOCOMMIT_ON );
return( bSuccess );
}
/* End of function "VMODBCConnection::CommitTrans"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMODBCConnection::Rollback
DESCRIPTION: if the driver is capable, and if a transaction is ongoing,
this will cancel/roll-back the transaction
INPUT: void
OUTPUT: none
RETURNS: true if worked, false if not
*/
bool VMODBCConnection::Rollback( void )
{
assert( m_hdbc != SQL_NULL_HDBC );
if ( !m_bTransactions )
return( false );
RETCODE nRetCode;
nRetCode = ::SQLTransact( m_henvAllConnections, m_hdbc, SQL_ROLLBACK );
bool bSuccess = Check( nRetCode );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -