Click here to Skip to main content
15,884,472 members
Articles / Desktop Programming / WTL

Shrinking SQL Server Transaction Logs with SQL-DMO

Rate me:
Please Sign up or sign in to vote.
4.99/5 (49 votes)
21 Apr 2004CPOL9 min read 204.1K   4.7K   70  
Using SQL-DMO to shrink SQL Server transaction logs.
/////////////////////////////////////////////////////////////////////////////
// #ifndef __ARMEN_H__
// #define __ARMEN_H__
//     Central Bank of Armenia, Information Security Department
//	   Armen Hakobyan, 2004. mailto:armen.h@web.am
//	   http://www.codeproject.com/script/articles/list_articles.asp?userid=25653
// #endif // __ARMEN_H__
/////////////////////////////////////////////////////////////////////////////

#include "StdAfx.h"
#include "DmoSink.h"

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

IMPLEMENT_WNDMSG( IBackupSink, WM_SQLDMO_BACKUPPERCCOMPLETE );
IMPLEMENT_WNDMSG( IBackupSink, WM_SQLDMO_BACKUPCOMPLETE     );
IMPLEMENT_WNDMSG( IBackupSink, WM_SQLDMO_BACKUPNEXTMEDIA    );

IBackupSink::IBackupSink( IN HWND hWndNotify  )
		   : m_ulRef( 0 )
		   , m_hWndNotify( hWndNotify )
{
	ASSERT( ::IsWindow( m_hWndNotify ) );
}

STDMETHODIMP IBackupSink::QueryInterface( IN  REFIID  riid, 
										  OUT LPVOID* ppvObj )
{
	ASSERT( ppvObj != NULL );
	if( !ppvObj )
		return ( E_FAIL );

	HRESULT hResult = ( E_NOINTERFACE );
	*ppvObj = NULL;

	if( IsEqualIID( riid, IID_IUnknown ) )
		*ppvObj =  static_cast< IUnknown* >( this );
	else if( IsEqualIID( riid, IID_ISQLDMOBackupSink ) )
		*ppvObj =  static_cast< ISQLDMOBackupSink* >( this );
	
	if( *ppvObj )
	{
		reinterpret_cast< IUnknown* >( *ppvObj )->AddRef();
		hResult = ( S_OK );
	}

	return ( hResult );
}

STDMETHODIMP_( ULONG ) IBackupSink::AddRef( void )
{
	return ::InterlockedIncrement( (PLONG)&m_ulRef );
}

STDMETHODIMP_( ULONG ) IBackupSink::Release( void )
{
	ULONG ulNewRef = ::InterlockedDecrement( (PLONG)&m_ulRef );
	if( ulNewRef <= 0 )
	{
		delete this;
		return ( 0 );
	}

	return ( ulNewRef );
}

STDMETHODIMP IBackupSink::PercentComplete( IN SQLDMO_LPCSTR bstrMessage, 
										   IN LONG lPercent )
{
	ASSERT( ::IsWindow( m_hWndNotify ) );

	::SendMessage( m_hWndNotify, WM_SQLDMO_BACKUPPERCCOMPLETE, 
		(WPARAM)bstrMessage, (LPARAM)lPercent );

	return ( S_OK );
}

STDMETHODIMP IBackupSink::Complete( IN SQLDMO_LPCSTR bstrMessage )
{
	ASSERT( ::IsWindow( m_hWndNotify ) );

	::SendMessage( m_hWndNotify, WM_SQLDMO_BACKUPPERCCOMPLETE, 
		(WPARAM)bstrMessage, (LPARAM)0L );

	return ( S_OK );
}

STDMETHODIMP IBackupSink::NextMedia( IN SQLDMO_LPCSTR bstrMessage )
{
	ASSERT( ::IsWindow( m_hWndNotify ) );

	::SendMessage( m_hWndNotify, WM_SQLDMO_BACKUPNEXTMEDIA, 
		(WPARAM)bstrMessage, (LPARAM)0L );

	return ( S_OK );
}

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

IMPLEMENT_WNDMSG( IServerSink, WM_SQLDMO_CONNECTIONBROKEN  );
IMPLEMENT_WNDMSG( IServerSink, WM_SQLDMO_REMOTELOGINFAILED );
IMPLEMENT_WNDMSG( IServerSink, WM_SQLDMO_SERVERMESSAGE     );

IServerSink::IServerSink( IN HWND hWndNotify  )
			: m_ulRef( 0 )
			, m_hWndNotify( hWndNotify )
{
	ASSERT( ::IsWindow( m_hWndNotify ) );
}

STDMETHODIMP IServerSink::QueryInterface( IN  REFIID  riid, 
										  OUT LPVOID* ppvObj )
{
	ASSERT( ppvObj != NULL );
	if( !ppvObj )
		return ( E_FAIL );

	HRESULT hResult = ( E_NOINTERFACE );
	*ppvObj = NULL;

	if( IsEqualIID( riid, IID_IUnknown ) )
		*ppvObj =  static_cast< IUnknown* >( this );
	else if( IsEqualIID( riid, IID_ISQLDMOServerSink ) )
		*ppvObj =  static_cast< ISQLDMOServerSink* >( this );

	if( *ppvObj )
	{
		reinterpret_cast< IUnknown* >( *ppvObj )->AddRef();
		hResult = ( S_OK );
	}

	return ( hResult );
}

STDMETHODIMP_( ULONG ) IServerSink::AddRef( void )
{
	return ::InterlockedIncrement( (PLONG)&m_ulRef );
}

STDMETHODIMP_( ULONG ) IServerSink::Release( void )
{
	ULONG ulNewRef = ::InterlockedDecrement( (PLONG)&m_ulRef );
	if( ulNewRef <= 0 )
	{
		delete this;
		return ( 0 );
	}

	return ( ulNewRef );
}

STDMETHODIMP IServerSink::CommandSent( IN SQLDMO_LPCSTR /*bstrSQL*/ )
{
	return ( E_NOTIMPL );
}

STDMETHODIMP IServerSink::ConnectionBroken( IN  SQLDMO_LPCSTR bstrMsg, 
											OUT LPBOOL pbRetry )
{
	ASSERT( ::IsWindow( m_hWndNotify ) );
	ASSERT( pbRetry != NULL );

	::SendMessage( m_hWndNotify, WM_SQLDMO_CONNECTIONBROKEN, 
		(WPARAM)bstrMsg, (LPARAM)0L );

	*pbRetry = ( TRUE );
	return ( S_OK );
}

// The QueryTimeout event is reserved for future use

STDMETHODIMP IServerSink::QueryTimeout( IN  SQLDMO_LPCSTR /*bstrMsg*/, 
										OUT LPBOOL /*pbCont*/ )
{
	return ( E_NOTIMPL );
}

STDMETHODIMP IServerSink::RemoteLoginFailed( IN LONG /*lSev*/, 
											 IN LONG /*lMsgNum*/,
											 IN LONG /*lMsgState*/,
											 IN SQLDMO_LPCSTR bstrMsg )
{
	ASSERT( ::IsWindow( m_hWndNotify ) );

	::SendMessage( m_hWndNotify, WM_SQLDMO_REMOTELOGINFAILED, 
		(WPARAM)bstrMsg, (LPARAM)0L );

	return ( S_OK );
}

STDMETHODIMP IServerSink::ServerMessage( IN LONG /*lSev*/, 
										 IN LONG /*lMsgNum*/,
										 IN LONG /*lMsgState*/,
										 IN SQLDMO_LPCSTR bstrMsg )
{
	ASSERT( ::IsWindow( m_hWndNotify ) );

	::SendMessage( m_hWndNotify, WM_SQLDMO_SERVERMESSAGE, 
		(WPARAM)bstrMsg, (LPARAM)0L );

	return ( S_OK );
}

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

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) SafeNet Inc
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions