home *** CD-ROM | disk | FTP | other *** search
/ Chip 1997 April / Chip_1997-04_cd.bin / prezent / cb / data.z / RESLTFRM.CPP < prev    next >
C/C++ Source or Header  |  1997-01-16  |  5KB  |  141 lines

  1. //----------------------------------------------------------------------------
  2. //Borland C++ Builder
  3. //Copyright (c) 1987 Borland International Inc. All Rights Reserved.
  4. //----------------------------------------------------------------------------
  5. //---------------------------------------------------------------------------
  6. #include <vcl\vcl.h>
  7. #pragma hdrstop
  8.  
  9. #include "ResltFrm.h"
  10. #pragma resource "*.dfm"
  11. //---------------------------------------------------------------------------
  12.  
  13. __fastcall TQueryForm::TQueryForm(TComponent* AOwner) : TForm(AOwner)
  14. {
  15. }
  16. __fastcall TQueryForm::TQueryForm(TComponent* AOwner, int Dummy) : TForm(AOwner, Dummy)
  17. {
  18. }
  19. __fastcall TQueryForm::TQueryForm(HWND ParentWindow) : TForm(ParentWindow)
  20. {
  21. }
  22. __fastcall TQueryForm::~TQueryForm(void)
  23. {
  24. }
  25. //---------------------------------------------------------------------------
  26.  
  27. __fastcall TQueryThread::TQueryThread(TQueryForm* AQueryForm)
  28.                         :TThread(false)
  29. {
  30.   QueryForm = AQueryForm;
  31.   FreeOnTerminate = true;
  32. }
  33.  
  34. __fastcall TQueryThread::~TQueryThread(void)
  35. {
  36.   //delete QueryForm;
  37. }
  38. //---------------------------------------------------------------------------
  39.  
  40. void __fastcall TQueryThread::Execute(void)
  41. {
  42.   static long UniqueNumber;
  43.   char buf[512];
  44.  
  45.   try{
  46.     // Ensure the Query has a unique session and database.  A unique session
  47.     // is required for each thread.  Since databases are session specific
  48.     // it must be unique as well
  49.     InterlockedIncrement(&UniqueNumber);
  50.  
  51.     sprintf(buf, "%s%d",
  52.             QueryForm->BKSession->Name,
  53.             UniqueNumber);
  54.     QueryForm->BKSession->SessionName = buf;
  55.     QueryForm->BKDatabase->SessionName = QueryForm->BKSession->SessionName;
  56.  
  57.     sprintf(buf, "%s%d",
  58.             QueryForm->BKDatabase->Name,
  59.             UniqueNumber);
  60.     QueryForm->BKDatabase->DatabaseName = buf;
  61.  
  62.     QueryForm->Query->SessionName = QueryForm->BKDatabase->SessionName;
  63.     QueryForm->Query->DatabaseName = QueryForm->BKDatabase->DatabaseName;
  64.  
  65.     // Open the query
  66.     QueryForm->Query->Open();
  67.  
  68.     // Connect the query to the grid.  This must be done in a synchronzied
  69.     // method since assigning the query to the DataSource will modify the
  70.     // contents of the grid and the grid can only be modified from the main
  71.     // VCL thread
  72.     Synchronize(ConnectQuery);
  73.  
  74.     // Update the status line.  Since the label is a VCL control this must
  75.     // also be done in the main VCL thread }
  76.     MessageText = "Query opened";
  77.     Synchronize(DisplayMessage);
  78.   }
  79.  
  80.   catch(EDatabaseError& e){
  81.     sprintf(buf,
  82.            "%s\n\n"
  83.            "1. Be sure Local Interbase and SQL Links are installed.\n"
  84.            "2. Run the BDE Configuration utility, BDECFG32.EXE.\n"
  85.            "3. Create a new Alias by the name 'IBLOCAL' of type INTRBASE.\n"
  86.            "4. Set the Server Name to <Interbase path>\\EXAMPLES\\EMPLOYEE.GDB.\n",
  87.            e.Message.c_str());
  88.  
  89.     MessageText = buf;
  90.  
  91.     // Display error on the status line
  92.     Synchronize(DisplayMessage);
  93.   }
  94.  
  95.   catch(Exception& e){
  96.     sprintf(buf, "%s: %s", e.ClassName(), e.Message.c_str());
  97.  
  98.     MessageText = buf;
  99.  
  100.     // Display error on the status line
  101.     Synchronize(DisplayMessage);
  102.   }
  103. }
  104. //---------------------------------------------------------------------------
  105.  
  106. void __fastcall TQueryThread::ConnectQuery(void)
  107. {
  108.   QueryForm->DataSource->DataSet = QueryForm->Query;
  109. }
  110. //---------------------------------------------------------------------------
  111.  
  112. void __fastcall TQueryThread::DisplayMessage(void)
  113. {
  114.   QueryForm->StatusLine->Caption = MessageText;
  115. }
  116. //---------------------------------------------------------------------------
  117.  
  118. void __fastcall BackgroundQuery(const AnsiString QueryName,
  119.                                 const AnsiString Alias,
  120.                                 const AnsiString User,
  121.                                 const AnsiString Password,
  122.                                 const AnsiString QueryText)
  123. {
  124.   //The QueryForm object is deleted in TQueryThread's destructor
  125.   TQueryForm* QueryForm = new TQueryForm(Application);
  126.  
  127.   QueryForm->Caption = QueryName;
  128.   QueryForm->QueryLabel->Caption = QueryText;
  129.   QueryForm->Show();
  130.   QueryForm->BKDatabase->AliasName = Alias;
  131.   QueryForm->BKDatabase->Params->Values["USER"] = User;
  132.   QueryForm->BKDatabase->Params->Values["PASSWORD"] = Password;
  133.   QueryForm->Query->SQL->Text = QueryText;
  134.  
  135.   // Create the background thread to execute the query. Since the thread
  136.   // will free itself on termination we do not need to maintain a reference
  137.   // to it.  Creating it is enough
  138.   new TQueryThread(QueryForm);
  139. }
  140.  
  141.