home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / SRCBDTO.PAK / SESSION.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  8.9 KB  |  405 lines

  1. //-----------------------------------------------------------------------------
  2. // Visual Database Tools
  3. // Copyright (c) 1996 by Borland International, All Rights Reserved
  4. //
  5. // session.cpp
  6. // TSession wrapper class
  7. //-----------------------------------------------------------------------------
  8.  
  9. #include <vdbt\bdto.h>
  10.  
  11. #pragma hdrstop
  12.  
  13. #include "wrapit.h"
  14. #include "misc.h"
  15.  
  16. //-----------------------------------------------------------------------------
  17.  
  18. TSession Session( true );        // global session variable
  19.  
  20. //-----------------------------------------------------------------------------
  21.  
  22. static void _export STDAPICALLTYPE PasswordEventCallback( int32 data, PITSession session, VARIANT_BOOL FAR* Continue )
  23. {
  24.     if (data)
  25.     {
  26.         PTSession ezSession = WrapPITSession( session );
  27.         bool ezContinue = MakeBool( *Continue );
  28.  
  29.         TPasswordSource* source = (TPasswordSource*) data;
  30.         (*source)( *ezSession, ezContinue );
  31.  
  32.         *Continue = MakeVariantBool( ezContinue );
  33.  
  34.         if (ezSession)
  35.             delete ezSession;
  36.     }
  37. }
  38.  
  39. void TSession::AttachOnPassword( const TBdtEventSourceBase&, bool attach )
  40. {
  41.     CheckGlobalSession();
  42.     if (session)
  43.     {
  44.         AttachedOnPassword = attach;
  45.         if (attach)
  46.             session->AttachOnPassword( PasswordEventCallback, (int32) &OnPasswordSource );
  47.         else
  48.             session->DetachOnPassword( PasswordEventCallback, (int32) &OnPasswordSource );
  49.     }
  50. }
  51.  
  52. void TSession::DetachEvents( void )
  53. {
  54.     if (session)
  55.     {
  56.         if (AttachedOnPassword)
  57.             session->DetachOnPassword( PasswordEventCallback, (int32) &OnPasswordSource );
  58.     }
  59. }
  60.  
  61. void TSession::ReattachEvents( void )
  62. {
  63.     if (session)
  64.     {
  65.         if (AttachedOnPassword)
  66.             session->AttachOnPassword( PasswordEventCallback, (int32) &OnPasswordSource );
  67.     }
  68. }
  69.  
  70. //-----------------------------------------------------------------------------
  71.  
  72. void TSession::SetTSession( PIUnknown p )
  73. {
  74.     session = 0;
  75.     if (p)
  76.         p->QueryInterface( IID_ITSession, (void**) &session );
  77.     ReattachEvents();
  78. }
  79.  
  80. void TSession::ClearTSession( void )
  81. {
  82.     DetachEvents();
  83.     if (session)
  84.     {
  85.         session->Release();
  86.         session = 0;
  87.     }
  88. }
  89.  
  90. TSession::TSession( bool createGlobal ) :
  91.     TBDTComponent(),
  92.     OnPasswordSource( SrcAttach_MFUNCTOR( *this, &TSession::AttachOnPassword ) ),
  93.     AttachedOnPassword( false )
  94. {
  95.     global = createGlobal;
  96.     if (global)
  97.         session = 0;
  98.     else
  99.     {
  100.         session = CreateITSession();
  101.         TBDTComponent::SetPIT( session );
  102.     }
  103. }
  104.  
  105. TSession::TSession( void ) :
  106.     TBDTComponent(),
  107.     OnPasswordSource( SrcAttach_MFUNCTOR( *this, &TSession::AttachOnPassword ) ),
  108.     AttachedOnPassword( false )
  109. {
  110.     global = false;
  111.     session = CreateITSession();
  112.     TBDTComponent::SetPIT( session );
  113. }
  114.  
  115. TSession::TSession( PITSession p ) :
  116.     TBDTComponent( p ),
  117.     OnPasswordSource( SrcAttach_MFUNCTOR( *this, &TSession::AttachOnPassword ) ),
  118.     AttachedOnPassword( false )
  119. {
  120.     global = false;
  121.     SetTSession( p );
  122. }
  123.  
  124. TSession::TSession( const TSession& p ) :
  125.     TBDTComponent( p ),
  126.     OnPasswordSource( SrcAttach_MFUNCTOR( *this, &TSession::AttachOnPassword ) ),
  127.     AttachedOnPassword( false )
  128. {
  129.     global = false;
  130.     SetTSession( p.session );
  131. }
  132.  
  133. TSession::TSession( PTSession p ) :
  134.     TBDTComponent( p ),
  135.     OnPasswordSource( SrcAttach_MFUNCTOR( *this, &TSession::AttachOnPassword ) ),
  136.     AttachedOnPassword( false )
  137. {
  138.     global = false;
  139.     SetTSession( p ? p->session : 0 );
  140. }
  141.  
  142. TSession& TSession::operator=( PITSession p )
  143. {
  144.     TBDTComponent::operator=(p);
  145.     ClearTSession();
  146.     SetTSession( p );
  147.     return *this;
  148. }
  149.  
  150. TSession& TSession::operator=( const TSession& p )
  151. {
  152.     if (this != &p)
  153.     {
  154.         TBDTComponent::operator=(p);
  155.         ClearTSession();
  156.         SetTSession( p.session );
  157.     }
  158.     return *this;
  159. }
  160.  
  161. int TSession::operator==( const TSession& p ) const
  162. {
  163.     if (this == &p)
  164.         return true;
  165.     if (session == p.session)
  166.         return true;
  167.     return false;
  168. }
  169.  
  170. int TSession::operator!=( const TSession& p ) const
  171. {
  172.     return ! operator==(p);
  173. }
  174.  
  175. TSession::~TSession()
  176. {
  177.     ClearTSession();
  178. }
  179.  
  180. void TSession::SetPIT( PIUnknown p )
  181. {
  182.     ClearTSession();
  183.     SetTSession( p );
  184.     TBDTComponent::SetPIT( p );
  185. }
  186.  
  187. void TSession::CheckGlobalSession( void )
  188. {
  189.     if (! session && global)
  190.     {
  191.         session = GetITSession();
  192.         TBDTComponent::SetPIT( session );
  193.     }
  194. }
  195.  
  196. void TSession::AddPassword( const string& p )
  197. {
  198.     CheckGlobalSession();
  199.     if (session)
  200.         session->AddPassword( AnyString(p).GetPITAnyString() );
  201. }
  202.  
  203. void TSession::CloseDatabase( TDatabase& d )
  204. {
  205.     CheckGlobalSession();
  206.     if (session)
  207.         session->CloseDatabase( d.GetPITDatabase() );
  208. }
  209.  
  210. void TSession::DropConnections( void )
  211. {
  212.     CheckGlobalSession();
  213.     if (session)
  214.         session->DropConnections();
  215. }
  216.  
  217. DEFINE_BDTO_OBJECTMETHOD1( TSession, TDatabase, PITDatabase, FindDatabase, string& );
  218.  
  219. void TSession::CallFindDatabase( string& n, TDatabase& d )
  220. {
  221.     CheckGlobalSession();
  222.     if (session)
  223.     {
  224.         PITDatabase pit = session->FindDatabase( AnyString(n).GetPITAnyString() );
  225.         if (pit)
  226.         {
  227.             d.SetPIT( pit );
  228.             pit->Release();
  229.         }
  230.     }
  231. }
  232.  
  233. void TSession::GetAliasNames( TStrings& list )
  234. {
  235.     CheckGlobalSession();
  236.     if (session)
  237.         session->GetAliasNames( list.GetPITStrings() );
  238. }
  239.  
  240. void TSession::GetAliasParams( const string& n, TStrings& list )
  241. {
  242.     CheckGlobalSession();
  243.     if (session)
  244.         session->GetAliasParams( AnyString(n).GetPITAnyString(), list.GetPITStrings() );
  245. }
  246.  
  247. void TSession::GetDatabaseNames( TStrings& list )
  248. {
  249.     CheckGlobalSession();
  250.     if (session)
  251.         session->GetDatabaseNames( list.GetPITStrings() );
  252. }
  253.  
  254. void TSession::GetDriverNames( TStrings& list )
  255. {
  256.     CheckGlobalSession();
  257.     if (session)
  258.         session->GetDriverNames( list.GetPITStrings() );
  259. }
  260.  
  261. void TSession::GetDriverParams( const string& n, TStrings& list )
  262. {
  263.     CheckGlobalSession();
  264.     if (session)
  265.         session->GetDriverParams( AnyString(n).GetPITAnyString(), list.GetPITStrings() );
  266. }
  267.  
  268. bool TSession::GetPassword( void )
  269. {
  270.     CheckGlobalSession();
  271.     if (session)
  272.         return MakeBool( session->GetPassword() );
  273.     return false;
  274. }
  275.  
  276. void TSession::GetTableNames( const string& DatabaseName, const string& Pattern, bool Extensions, bool SystemTables, TStrings& list )
  277. {
  278.     CheckGlobalSession();
  279.     if (session)
  280.         session->GetTableNames( AnyString(DatabaseName).GetPITAnyString(), AnyString(Pattern).GetPITAnyString(), MakeVariantBool( Extensions ), MakeVariantBool( SystemTables ), list.GetPITStrings() );
  281. }
  282.  
  283. void TSession::GetStoredProcNames( const string& DatabaseName, TStrings& list )
  284. {
  285.     CheckGlobalSession();
  286.     if (session)
  287.         session->GetStoredProcNames( AnyString(DatabaseName).GetPITAnyString(), list.GetPITStrings() );
  288. }
  289.  
  290. DEFINE_BDTO_OBJECTMETHOD1( TSession, TDatabase, PITDatabase, OpenDatabase, string& );
  291.  
  292. void TSession::CallOpenDatabase( string& n, TDatabase& d )
  293. {
  294.     CheckGlobalSession();
  295.     if (session)
  296.     {
  297.         PITDatabase pit = session->OpenDatabase( AnyString(n).GetPITAnyString() );
  298.         if (pit)
  299.         {
  300.             d.SetPIT( pit );
  301.             pit->Release();
  302.         }
  303.     }
  304. }
  305.  
  306. void TSession::RemoveAllPasswords( void )
  307. {
  308.     CheckGlobalSession();
  309.     if (session)
  310.         session->RemoveAllPasswords();
  311. }
  312.  
  313. void TSession::RemovePassword( const string& p )
  314. {
  315.     CheckGlobalSession();
  316.     if (session)
  317.         session->RemovePassword( AnyString(p).GetPITAnyString() );
  318. }
  319.  
  320. DEFINE_BDTO_PROP_RO( TSession, int, DatabaseCount );
  321. DEFINE_BDTO_ARRAYOBJECTPROP_RO_FAST( TSession, TDatabase, PITDatabase, Databases, int );
  322. DEFINE_BDTO_PROP_RO( TSession, hDBISes, Handle );
  323. DEFINE_BDTO_PROP_RW( TSession, bool, KeepConnections );
  324. DEFINE_BDTO_PROP_RO( TSession, TLocale, Locale );
  325. DEFINE_BDTO_OBJECTPROP_RW( TSession, string, NetFileDir );
  326. DEFINE_BDTO_OBJECTPROP_RW( TSession, string, PrivateDir );
  327.  
  328. void TSession::GetDatabaseCount( int& c )
  329. {
  330.     CheckGlobalSession();
  331.     if (session)
  332.         c = session->get_DatabaseCount();
  333. }
  334.  
  335. void TSession::GetDatabases( int i, TDatabase& d )
  336. {
  337.     CheckGlobalSession();
  338.     if (session)
  339.     {
  340.         PITDatabase pit = session->get_Databases( i );
  341.         if (pit)
  342.         {
  343.             d.SetPIT( pit );
  344.             pit->Release();
  345.         }
  346.     }
  347. }
  348.  
  349. void TSession::GetHandle( hDBISes& h )
  350. {
  351.     CheckGlobalSession();
  352.     if (session)
  353.         h = session->get_Handle();
  354. }
  355.  
  356. void TSession::GetKeepConnections( bool& k )
  357. {
  358.     CheckGlobalSession();
  359.     if (session)
  360.         k = MakeBool( session->get_KeepConnections() );
  361. }
  362.  
  363. void TSession::SetKeepConnections( bool k )
  364. {
  365.     CheckGlobalSession();
  366.     if (session)
  367.         session->put_KeepConnections( MakeVariantBool( k ) );
  368. }
  369.  
  370. void TSession::GetLocale( TLocale& l )
  371. {
  372.     CheckGlobalSession();
  373.     if (session)
  374.         l = session->get_Locale();
  375. }
  376.  
  377. void TSession::GetNetFileDir( string& s )
  378. {
  379.     CheckGlobalSession();
  380.     if (session)
  381.         AnyString( session->get_NetFileDir() ).GetString( s );
  382. }
  383.  
  384. void TSession::SetNetFileDir( const string& d )
  385. {
  386.     CheckGlobalSession();
  387.     if (session)
  388.         session->put_NetFileDir( AnyString(d).GetPITAnyString() );
  389. }
  390.  
  391. void TSession::GetPrivateDir( string& s )
  392. {
  393.     CheckGlobalSession();
  394.     if (session)
  395.         AnyString( session->get_PrivateDir() ).GetString( s );
  396. }
  397.  
  398. void TSession::SetPrivateDir( const string& d )
  399. {
  400.     CheckGlobalSession();
  401.     if (session)
  402.         session->put_PrivateDir( AnyString(d).GetPITAnyString() );
  403. }
  404.  
  405.