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

📄 vmsqllite.cpp

📁 TOOL (Tiny Object Oriented Language) is an easily-embedded, object-oriented, C++-like-language inter
💻 CPP
📖 第 1 页 / 共 2 页
字号:
             INPUT:  void  
            OUTPUT:  none

           RETURNS:  true if worked, false if not 
*/
bool VMSqlLiteQuery::Close( void )
{
  m_poDB->GetLock();
  if ( !m_poDBVM )
  {
    m_poDB->FreeLock();
    return( true );
  }

  m_oLastError = _T( "" );

  char* pchErrorMessage = NULL;

  int iResult = sqlite_finalize( m_poDBVM, &pchErrorMessage );
  if ( iResult != SQLITE_OK )
  {
    if ( pchErrorMessage )
    {
      m_oLastError = VMString( pchErrorMessage );
      sqlite_freemem( pchErrorMessage );
    }
  }
  m_poDBVM = NULL;
  m_poDB->FreeLock();
  return( iResult == SQLITE_OK );
}
/* End of function "VMSqlLiteQuery::Close"
/*****************************************************************************/


/*****************************************************************************/
/*

     FUNCTION NAME:  VMSqlLiteQuery::Execute

       DESCRIPTION:  execute a query against the database

             INPUT:  pchFormatString - format string
                     ... - arguments
            OUTPUT:  

           RETURNS:  true if worked, false if not
*/
bool VMSqlLiteQuery::Execute( const char* pchFormatString, ... )
{
  m_poDB->GetLock();

  m_oLastError = _T( "" );
	
  va_list args;
  va_start( args, pchFormatString );

  const char* pchSqlRemainder = NULL;
  char*       pchErrorMessage = NULL; 

  char* pchSQL = sqlite_vmprintf( pchFormatString, args );

  int iResult = sqlite_compile( m_poDB->GetDB(), pchSQL, &pchSqlRemainder, &m_poDBVM, &pchErrorMessage );
  sqlite_freemem( pchSQL );
  va_end( args );

  if ( iResult != SQLITE_OK )
  {
    if ( pchErrorMessage )
    {
      m_oLastError = VMString( pchErrorMessage );
      sqlite_freemem( pchErrorMessage );
    }
  }
  else
  {
    FetchData();
  }
  m_poDB->FreeLock();
  return( m_oLastError.Length() == 0 );
}
/* End of function "VMSqlLiteQuery::Execute"
/*****************************************************************************/


/*****************************************************************************/
/*

     FUNCTION NAME:  VMSqlLiteQuery::IsEOF

       DESCRIPTION:  is the result set exhausted?

             INPUT:  void 
            OUTPUT:  none

           RETURNS:  true if it is, false if not
*/
bool VMSqlLiteQuery::IsEOF( void ) const
{
  return( m_poDBVM == NULL );
}
/* End of function "VMSqlLiteQuery::IsEOF"
/*****************************************************************************/


/*****************************************************************************/
/*

     FUNCTION NAME:  VMSqlLiteQuery::FetchData

       DESCRIPTION:  get one row row of data into local buffers

             INPUT:  void  
            OUTPUT:  none

           RETURNS:  true if worked, false if not
*/
bool VMSqlLiteQuery::FetchData( void )
{
  m_poDB->GetLock();
  int iResult = 0;
  int iColumn = sqlite_step( m_poDBVM, &iResult, &m_ppchColumnValue, &m_ppchColumnName );
	
  if ( iColumn != m_iColumnIndex )
  {
    m_iColumnIndex = iColumn;
    if ( NULL != m_ppchColumnName && NULL != *m_ppchColumnName )
    {
      SetColumnNameAtIndex( m_ppchColumnName, m_iColumnIndex );
    }
  }

  if ( iResult == SQLITE_ROW )
  {
    m_poDB->FreeLock();
    return( true );
  }
  else
  {
    Close();
    m_poDB->FreeLock();
    return( false );
  }
}
/* End of function "VMSqlLiteQuery::FetchData"
/*****************************************************************************/


/*****************************************************************************/
/*

     FUNCTION NAME:  VMSqlLiteQuery::GetColumnIndex

       DESCRIPTION:  returns the column index for this

             INPUT:  void
            OUTPUT:  none

           RETURNS:  internal column index value 
*/
int VMSqlLiteQuery::GetColumnIndex( void ) const
{
  return( m_iColumnIndex );
}
/* End of function "VMSqlLiteQuery::GetColumnIndex"
/*****************************************************************************/


/*****************************************************************************/
/*

     FUNCTION NAME:  VMSqlLiteQuery::GetColumnNameAtIndex

       DESCRIPTION:  the column name for the given index

             INPUT:  iFieldIndex - the index to get the name for
            OUTPUT:  none

           RETURNS:  pointer to column name
*/
const char* VMSqlLiteQuery::GetColumnNameAtIndex( int iFieldIndex )
{
  return( m_ppchColumnName[ iFieldIndex ] );
}
/* End of function "VMSqlLiteQuery::GetColumnNameAtIndex"
/*****************************************************************************/


/*****************************************************************************/
/*

     FUNCTION NAME:  VMSqlLiteQuery::GetColumnValueByIndex

       DESCRIPTION:  gets the column value at the given column index (for the
                     current row)

             INPUT:  iFieldIndex - the column index
            OUTPUT:  none

           RETURNS:  VMSqlLiteQueryBase::CDBField value object
*/
VMSqlLiteQueryBase::CDBField VMSqlLiteQuery::GetColumnValueByIndex( int iFieldIndex )
{
  return( CDBField( m_ppchColumnValue[ iFieldIndex ] ) );
}
/* End of function "VMSqlLiteQuery::GetColumnValueByIndex"
/*****************************************************************************/


/*****************************************************************************/
/*

     FUNCTION NAME:  VMSqlLiteQuery::GetColumnValueByName

       DESCRIPTION:  get the value for the column by column name (for the current
                     row)

             INPUT:  oFieldName - the name of the field to get the value for
            OUTPUT:  none

           RETURNS:  VMSqlLiteQueryBase::CDBField value object 
*/
VMSqlLiteQueryBase::CDBField VMSqlLiteQuery::GetColumnValueByName( VMString oFieldName )
{
  int iIndex = GetColumnIndexForName( oFieldName );
  if ( iIndex < 0 )
  {
    VMString oError;
    oError.Format( "Unknown field name <%s>", oFieldName.Buffer() );
    throw VMSqlLiteException( oError );
  }
  return GetColumnValueByIndex( iIndex );
}
/* End of function "VMSqlLiteQuery::GetColumnValueByName"
/*****************************************************************************/



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



/*****************************************************************************/
/*

     FUNCTION NAME:  VMSqlLiteQueryResult::VMSqlLiteQueryResult

       DESCRIPTION:  ctor

             INPUT:  poDB - pointer to connection object
            OUTPUT:  none

           RETURNS:  none
*/
VMSqlLiteQueryResult::VMSqlLiteQueryResult( VMSqlLiteDatabase* poDB )
: VMSqlLiteQueryBase( poDB ),
  m_ppchResult( NULL ), 
  m_iRowCount( 0 ), 
  m_iColumnCount( 0 ),
  m_iCursor( 0 )
{
}
/* End of function "VMSqlLiteQueryResult::VMSqlLiteQueryResult"
/*****************************************************************************/


/*****************************************************************************/
/*

     FUNCTION NAME:  VMSqlLiteQueryResult::~VMSqlLiteQueryResult

       DESCRIPTION:  dtor

             INPUT:  void 
            OUTPUT:  none

           RETURNS:  none
*/
VMSqlLiteQueryResult::~VMSqlLiteQueryResult( void )
{
  m_poDB->GetLock();
  sqlite_free_table( m_ppchResult );
  m_poDB->FreeLock();
}
/* End of function "VMSqlLiteQueryResult::~VMSqlLiteQueryResult"
/*****************************************************************************/


/*****************************************************************************/
/*

     FUNCTION NAME:  VMSqlLiteQueryResult::Execute

       DESCRIPTION:  executes a query against the database and caches the result

             INPUT:  pchFormatText - format string for query
                     ... - arguments
            OUTPUT:  

           RETURNS:  true if worked, false if not
*/
bool VMSqlLiteQueryResult::Execute( const char* pchFormatText, ... )
{
  m_poDB->GetLock();
  sqlite_free_table( m_ppchResult );
  m_oLastError = _T( "" );

  char* pchErrorMessage = NULL;

  va_list args;
  va_start( args, pchFormatText );
  int iResult = SQLITE_ERROR;
    
  iResult = sqlite_get_table_vprintf( m_poDB->GetDB(), 
                                      pchFormatText, 
                                      &m_ppchResult, 
                                      &m_iRowCount, 
                                      &m_iColumnCount, 
                                      &pchErrorMessage, 
                                      args );
  va_end( args );
    
  if ( iResult != SQLITE_OK )
  {
    if ( pchErrorMessage )
    {
      m_oLastError = VMString( pchErrorMessage );
      sqlite_freemem( pchErrorMessage );
    }
    m_poDB->FreeLock();
    return( false );
  }
  SetColumnNameAtIndex( (const char**)m_ppchResult, m_iColumnCount );
  m_poDB->FreeLock();
  return( true );
}
/* End of function "VMSqlLiteQueryResult::Execute"
/*****************************************************************************/


/*****************************************************************************/
/*

     FUNCTION NAME:  VMSqlLiteQueryResult::NumChanges

       DESCRIPTION:  return the number of rows changed by a query

             INPUT:  void  
            OUTPUT:  none

           RETURNS:  row count 
*/
int VMSqlLiteQueryResult::NumChanges( void )
{
  return( sqlite_changes( m_poDB->GetDB() ) );
}
/* End of function "VMSqlLiteQueryResult::NumChanges"
/*****************************************************************************/


/*****************************************************************************/
/*

     FUNCTION NAME:  VMSqlLiteQueryResult::GetRowCount

       DESCRIPTION:  returns the number of rows in the result set

             INPUT:  void 
            OUTPUT:  none

           RETURNS:  row count
*/
int VMSqlLiteQueryResult::GetRowCount( void )
{
  return( m_iRowCount );
}
/* End of function "VMSqlLiteQueryResult::GetRowCount"
/*****************************************************************************/


/*****************************************************************************/
/*

     FUNCTION NAME:  VMSqlLiteQueryResult::GetColumnCount

       DESCRIPTION:  returns the number of columns in the result set

             INPUT:  void  
            OUTPUT:  none

           RETURNS:  column count 
*/
int VMSqlLiteQueryResult::GetColumnCount( void )
{
  return( m_iColumnCount );
}
/* End of function "VMSqlLiteQueryResult::GetColumnCount"
/*****************************************************************************/


/*****************************************************************************/
/*

     FUNCTION NAME:  VMSqlLiteQueryResult::GetFieldNameByIndex

       DESCRIPTION:  returns the name of the column at the given colum index

             INPUT:  iFieldIndex - column index
            OUTPUT:  none

           RETURNS:  pointer to column name
*/
char* VMSqlLiteQueryResult::GetFieldNameByIndex( int iFieldIndex )
{
  return( m_ppchResult[ iFieldIndex ] );
}
/* End of function "VMSqlLiteQueryResult::GetFieldNameByIndex"
/*****************************************************************************/


/*****************************************************************************/
/*

     FUNCTION NAME:  VMSqlLiteQueryResult::GetValueAt

       DESCRIPTION:  return the value object at the given row/column location

             INPUT:  iRowIndex - 
                     iFieldIndex - 
            OUTPUT:  

           RETURNS:  VMSqlLiteQueryBase::CDBField value object
*/
VMSqlLiteQueryBase::CDBField VMSqlLiteQueryResult::GetValueAt( int iRowIndex, int iFieldIndex )
{
  return( CDBField( m_ppchResult[ ( iRowIndex + 1 ) * m_iColumnCount + iFieldIndex ] ) );
}
/* End of function "VMSqlLiteQueryResult::GetValueAt"
/*****************************************************************************/


/*****************************************************************************/
/*

     FUNCTION NAME:  VMSqlLiteQueryResult::FieldByName

       DESCRIPTION:  return the value object at the given row index for the
                     given column name

             INPUT:  iRowIndex - 
                     oColumnName - 
            OUTPUT:  

           RETURNS:  VMSqlLiteQueryBase::CDBField  value object
*/
VMSqlLiteQueryBase::CDBField VMSqlLiteQueryResult::FieldByName( int iRowIndex, VMString oColumnName )
{
  int iColumnIndex = GetColumnIndexForName( oColumnName );
  if ( iColumnIndex < 0 )
  {
    VMString oError;
    oError.Format( "Unknown field name <%s>", oColumnName.Buffer() );
    throw VMSqlLiteException( oError );
  }
  return( GetValueAt( iRowIndex, iColumnIndex ) );
}
/* End of function "VMSqlLiteQueryResult::FieldByName"
/*****************************************************************************/


VMSqlLiteQueryBase::CDBField VMSqlLiteQueryResult::GetValueAt( int fieldIdx )
{
  return( GetValueAt( m_iCursor, fieldIdx ) );
}


VMSqlLiteQueryBase::CDBField VMSqlLiteQueryResult::FieldByName( VMString fieldName )
{
  return( FieldByName( m_iCursor, fieldName ) );
}


/*****************************************************************************/
/* Check-in history */
/*
 *$Log:  $
*/
/*****************************************************************************/


⌨️ 快捷键说明

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