📄 vmodbc.h
字号:
#ifndef VM_ODBC_DYNAMIC_BINDING_RECORDSET_H_INCLUDED
#define VM_ODBC_DYNAMIC_BINDING_RECORDSET_H_INCLUDED
/*****************************************************************************/
/* HEADER FILE */
/*****************************************************************************/
/*
$Archive: $
$Revision: $
$Date: $
$Author: $
Description: Declaration of a class that can dynamically bind to and
read any sql result set.
TOOL And XML FORMS License
==========================
Except where otherwise noted, all of the documentation
and software included in the TOOL package is
copyrighted by Michael Swartzendruber.
Copyright (C) 2005 Michael John Swartzendruber.
All rights reserved.
Access to this code, whether intentional or accidental,
does NOT IMPLY any transfer of rights.
This software is provided "as-is," without any express
or implied warranty. In no event shall the author be held
liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for
any purpose, including commercial applications, and to
alter and redistribute it, provided that the following
conditions are met:
1. All redistributions of source code files must retain
all copyright notices that are currently in place,
and this list of conditions without modification.
2. The origin of this software must not be misrepresented;
you must not claim that you wrote the original software.
3. If you use this software in another product, an acknowledgment
in the product documentation would be appreciated but is
not required.
4. Modified versions in source or binary form must be plainly
marked as such, and must not be misrepresented as being
the original software.
*/
/*****************************************************************************/
#include <sql.h>
#include <sqlext.h>
#include <vector>
#include <map>
#include <string>
#include "VMString.h"
#include "VMODBCConnection.h"
#pragma comment( lib, "odbc32.lib" )
#pragma comment( lib, "odbccp32.lib" )
///////////////////////////////////////////////////////////////////////////////
//
// forward declarations
//
class VMODBC;
///////////////////////////////////////////////////////////////////////////////
//
// First: An SQL Column descriptor class that will be filled out for each column
// in the result set
//
//
class VMColumnInfo
{
#define MAX_TYPE 65
public:
// Following is information from the source column
//
SWORD m_iSqlDataType; // SQL_xxxx type of column
SWORD m_iCDataType; // Mapped C type
UDWORD m_dwPrecision; // Column precision
SWORD m_iScale; // Scale of the column
char m_achNamedType[ MAX_TYPE ]; // named SQL type of the column
char m_achFromTable[ MAX_TYPE ]; // Name from original table
SDWORD m_iDisplaySize; // Display size if converted to character
UWORD m_iColumnIndex; // Which col in the results list
char m_achColumnName[ MAX_TYPE ]; // Name of this column
void* m_pvData; // Memory for the transfer
SWORD m_iIsNullable; // Can column be null?
int m_iParamCount; // number of creation parameters for this column type
};
///////////////////////////////////////////////////////////////////////////////
//
// Next: Define collections for the column information structures
//
//
typedef std::vector< VMColumnInfo* > COLUMN_INFO_BY_INDEX;
typedef COLUMN_INFO_BY_INDEX::iterator COLUMN_INFO_BY_INDEX_ITER;
typedef std::map< std::string, VMColumnInfo* > COLUMN_INFO_BY_NAME;
typedef COLUMN_INFO_BY_NAME::iterator COLUMN_INFO_BY_NAME_ITER;
///////////////////////////////////////////////////////////////////////////////
//
// Next: define a data exchange interface object. The VMODBC will load
// data into an a set of VMColumnInfo objects, the VMDataExchanger will
// translate/transport that data into the application context
//
class VMDataExchanger
{
public:
VMDataExchanger( void ){;};
virtual ~VMDataExchanger( void ){;};
virtual void ExchangeData( VMODBC* poDataSource ) = 0;
};
///////////////////////////////////////////////////////////////////////////////
//
// Finally: The class which does the work of dynamically binding to and getting
// the data from any query.
//
// Warn: Will not likely work for parameters bound to a stored procedure. This
// class is not really intended to be used like that. But if I can make
// that work here, then this comment will be removed.
//
class VMODBC : public VMODBCConnection
{
friend VMDataExchanger;
public:
VMODBC( void );
~VMODBC( void );
bool ExecuteQuery( const char* pchToExecute );
bool IsEmptySet( void )
{
return( m_bIsEmpty );
};
bool IsEOF( void )
{
return( m_bIsEOF );
};
bool IsBOF( void )
{
return( m_bIsBOF );
};
virtual void Close( void );
bool MoveNext( void );
bool MovePrev( void );
bool MoveToStart( void );
bool MoveToEnd( void );
bool MoveForward( int iMoveAmount );
bool MoveBackward( int iMoveAmount );
bool MoveToIndexFromStart( int iIndex );
bool MoveToIndexFromEnd( int iIndex );
void ExchangeData( VMDataExchanger* poDataExchange );
COLUMN_INFO_BY_INDEX* GetColumnsByIndex( void ) { return( &m_oColumnsByIndex ); };
COLUMN_INFO_BY_NAME* GetColumnsByName( void ) { return( &m_oColumnsByName ); };
protected:
bool MakeColumnListFromQuery( void );
bool AllocStorageForColumn( VMColumnInfo* poColumnInfo );
void AddColumnInfoToList( VMColumnInfo* poColumnInfo );
bool MapColumnSQLTypeToCType( VMColumnInfo* poColumnInfo );
bool BindColumn( VMColumnInfo* poColumnInfo );
void DecodeSQLError( RETCODE rc,
HDBC hdbc,
HSTMT hstmt,
LPSTR lpstrFile,
UINT uiLine );
void ReleaseColumns( void );
bool m_bIsEOF;
bool m_bIsBOF;
bool m_bIsEmpty;
bool m_bIsBound;
bool m_bShouldBind;
SQLHANDLE m_hstmt;
COLUMN_INFO_BY_INDEX m_oColumnsByIndex;
COLUMN_INFO_BY_NAME m_oColumnsByName;
int m_iColumnCount;
};
#endif
/*****************************************************************************/
/* Check-in history */
/*
*$Log: $
*/
/*****************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -