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

  1. //----------------------------------------------------------------------------
  2. //Borland C++ Builder
  3. //Copyright (c) 1987 Borland International Inc. All Rights Reserved.
  4. //----------------------------------------------------------------------------
  5. //---------------------------------------------------------------------------
  6. //    Demostrates how to execute a query in a background thread.  This
  7. //  files contains the main user interface for this program.  The background
  8. //  query code is in ResltFrm.
  9. //---------------------------------------------------------------------------
  10.  
  11. #include <vcl\vcl.h>
  12. #pragma hdrstop
  13.  
  14. #include "QueryFrm.h"
  15. #include "bkstring.rc"
  16. //---------------------------------------------------------------------------
  17. #pragma resource "*.dfm"
  18. TAdhocForm *AdhocForm;
  19.  
  20. AnsiString StrToIniStr(const AnsiString Str)
  21. {
  22.   char Buffer[4096];
  23.   PChar B, S;
  24.  
  25.   if (Str.Length() > sizeof(Buffer))
  26.       throw Exception("AnsiString too large to save in INI file");
  27.   S = Str.c_str();
  28.   B = Buffer;
  29.   while (*S != '\0')
  30.   {
  31.       switch (*S)
  32.      {
  33.          case 13:
  34.         case 10:
  35.            if ((*S == 13) && (S[1] == 10)) S++;
  36.            else if ((*S == 10) && (S[1] == 13)) S++;
  37.            *B = '\\';
  38.            B++;
  39.            *B = 'n';
  40.            B++;
  41.            S++;
  42.            break;
  43.         default:
  44.               *B = *S;
  45.               B++;
  46.               S++;
  47.      }
  48.   }
  49.   *B = '\0';
  50.   return String(Buffer);
  51. }
  52.  
  53. String IniStrToStr(const String Str)
  54. {
  55.   char Buffer[4096];
  56.   PChar B, S;
  57.  
  58.   if (Str.Length() > sizeof(Buffer))
  59.       throw Exception("String to read from an INI file");
  60.   S = Str.c_str();
  61.   B = Buffer;
  62.   while (*S != '\0')
  63.   {
  64.     if ((S[0] == '\\') && (S[1] == 'n'))
  65.     {
  66.       *B = 13;
  67.       B++;
  68.       *B = 10;
  69.       B++;
  70.       S++;
  71.       S++;
  72.     }
  73.     else
  74.     {
  75.       *B = *S;
  76.       B++;
  77.       S++;
  78.     }
  79.   }
  80.   *B = '\0';
  81.   return String(Buffer);
  82. }
  83.  
  84. //---------------------------------------------------------------------------
  85. __fastcall TAdhocForm::TAdhocForm(TComponent* Owner)
  86.   : TForm(Owner)
  87. {
  88. }
  89. //---------------------------------------------------------------------------
  90. void __fastcall TAdhocForm::FormCloseQuery(TObject *Sender,
  91.       Boolean &CanClose)
  92. {
  93.   CanClose = CheckModified();
  94. }
  95. //---------------------------------------------------------------------
  96.  
  97. Boolean __fastcall TAdhocForm::IsModified(void)
  98. {
  99.   return ((AliasCombo->Text != OldAlias) || NameEdit->Modified ||
  100.     QueryEdit->Modified);
  101. }
  102.  
  103. //---------------------------------------------------------------------
  104. Boolean  __fastcall TAdhocForm::CheckModified(void)
  105. {
  106.   char szTemp[256];
  107.   Boolean Result = true;
  108.  
  109.   sprintf(szTemp, "Query %s has been modified. Save?", QueryName);
  110.   if (IsModified())
  111.       switch (MessageDlg(szTemp,
  112.                        mtConfirmation,
  113.                         TMsgDlgButtons() << mbYes << mbNo << mbCancel,
  114.                        0)){
  115.       case mrOk:
  116.         SaveQuery();
  117.         break;
  118.       case mrCancel:
  119.         Result = false;
  120.         break;
  121.     }
  122.   return Result;
  123. }
  124.  
  125. //---------------------------------------------------------------------
  126. void __fastcall TAdhocForm::Unmodify(void)
  127. {
  128.   OldAlias = AliasCombo->Text;
  129.   NameEdit->Modified = false;
  130.   QueryEdit->Modified = false;
  131. }
  132.  
  133. //---------------------------------------------------------------------
  134. void __fastcall TAdhocForm::ReadQuery(void)
  135. {
  136.   if (!CheckModified()) return;
  137.   QueryName = SavedQueryCombo->Items->Strings[SavedQueryCombo->ItemIndex];
  138.   QueryEdit->Text = IniStrToStr(SavedQueries->ReadString(QueryName, "Query", ""));
  139.   AliasCombo->Text = SavedQueries->ReadString(QueryName, "Alias", "");
  140.   NameEdit->Text = SavedQueries->ReadString(QueryName, "Name", "");
  141.      Unmodify();
  142.   if (Showing)
  143.     if (NameEdit->Text != "")
  144.       PasswordEdit->SetFocus();
  145.     else
  146.       QueryEdit->SetFocus();
  147. }
  148.  
  149. //---------------------------------------------------------------------
  150. void __fastcall TAdhocForm::SaveQuery(void)
  151. {
  152.   if (Unnamed)
  153.     SaveQueryAs();
  154.   else
  155.   {
  156.       SavedQueries->WriteString(QueryName, "Query", StrToIniStr(QueryEdit->Text));
  157.       SavedQueries->WriteString(QueryName, "Alias", AliasCombo->Text);
  158.       SavedQueries->WriteString(QueryName, "Name", NameEdit->Text);
  159.       Unmodify();
  160.   }
  161. }
  162.  
  163. void __fastcall TAdhocForm::SaveQueryAs(void)
  164. {
  165.   if (GetNewName(QueryName)){
  166.      Unnamed = false;
  167.      SaveQuery();
  168.      if (SavedQueryCombo->Items->IndexOf(QueryName) < 0)
  169.          SavedQueryCombo->Items->Add(QueryName);
  170.      SavedQueryCombo->ItemIndex = SavedQueryCombo->Items->IndexOf(QueryName);
  171.   }
  172. }
  173.  
  174. //---------------------------------------------------------------------
  175. void __fastcall TAdhocForm::FormDestroy(TObject *Sender)
  176. {
  177.   delete SavedQueries;
  178. }
  179.  
  180. //---------------------------------------------------------------------
  181. void __fastcall TAdhocForm::FormCreate(TObject *Sender)
  182. {
  183.   int I;
  184.  
  185.   // Grab session aliases
  186.   Session->GetAliasNames(AliasCombo->Items);
  187.  
  188.   // Load in saved queries
  189.   SavedQueries = new TIniFile(".\\BKQUERY.INI");
  190.   SavedQueries->ReadSections(SavedQueryCombo->Items);
  191.   if (SavedQueryCombo->Items->Count <= 0)
  192.      CreateInitialIni();
  193.   SavedQueryCombo->ItemIndex = 0;
  194.   QueryName = SavedQueryCombo->Items->Strings[0];
  195.   Unmodify();
  196.   ReadQuery();
  197. }
  198. //---------------------------------------------------------------------
  199. // CreateInitialIni
  200. //Creates initial INI file when one doesn't already exist
  201. void __fastcall TAdhocForm::CreateInitialIni()
  202. {
  203.   //Load first SQL query text and add it to the combo box
  204.   char szQueryName[64];
  205.   LoadString((HINSTANCE)HInstance,
  206.              IDS_VeryInefficientName,
  207.              szQueryName,
  208.              sizeof(szQueryName));
  209.   HGLOBAL hSQLQuery =  LoadResource((HINSTANCE)HInstance,
  210.                                     FindResource((HINSTANCE)HInstance,
  211.                                                  MAKEINTRESOURCE(IDSQLQUERY_VeryInefficientQuery),
  212.                                                  "SQLQUERY"));
  213.   char* pszSQLQuery = (char*)LockResource(hSQLQuery);
  214.   SavedQueries->WriteString(szQueryName, "Query", StrToIniStr(pszSQLQuery));
  215.   SavedQueries->WriteString(szQueryName, "Alias", "IBLOCAL");
  216.   SavedQueries->WriteString(szQueryName, "Name", "SYSDBA");
  217.   SavedQueryCombo->Items->Add(szQueryName);
  218.  
  219.   //Load next SQL query text and add it to the combo box
  220.   LoadString((HINSTANCE)HInstance, IDS_AmountDueName, szQueryName, sizeof(szQueryName));
  221.   hSQLQuery =  LoadResource((HINSTANCE)HInstance,
  222.                             FindResource((HINSTANCE)HInstance,
  223.                                          MAKEINTRESOURCE(IDSQLQUERY_AmountDueByCustomer),
  224.                                          "SQLQUERY"));
  225.   pszSQLQuery = (char*)LockResource(hSQLQuery);
  226.   SavedQueries->WriteString(szQueryName, "Query", StrToIniStr(pszSQLQuery));
  227.   SavedQueries->WriteString(szQueryName, "Alias", "BCDEMOS");
  228.   SavedQueries->WriteString(szQueryName, "Name", "");
  229.   SavedQueryCombo->Items->Add(szQueryName);
  230. }
  231. //---------------------------------------------------------------------
  232.  
  233. void __fastcall TAdhocForm::ExecuteBtnClick(TObject *Sender)
  234. {
  235.   BackgroundQuery(QueryName,
  236.                   AliasCombo->Text,
  237.                   NameEdit->Text,
  238.                   PasswordEdit->Text,
  239.                   QueryEdit->Text);
  240.   BringToFront();
  241. }
  242. //---------------------------------------------------------------------
  243.  
  244. String __fastcall TAdhocForm::UniqueName()
  245. {
  246.   int I;
  247.   String result;
  248.   I = 1;
  249.   do
  250.     result.Format("Query%d", ARRAYOFCONST((I)));
  251.   while(SavedQueryCombo->Items->IndexOf(result) >= 0);
  252. }
  253. //---------------------------------------------------------------------
  254.  
  255. void __fastcall TAdhocForm::NewBtnClick(TObject *Sender)
  256. {
  257.   AliasCombo->Text = "BCDEMOS";
  258.   NameEdit->Text = "";
  259.   PasswordEdit->Text = "";
  260.   QueryEdit->Text = "";
  261.   QueryEdit->SetFocus();
  262.   QueryName = UniqueName();
  263.   SavedQueryCombo->ItemIndex = -1;
  264.   Unnamed = true;
  265. }
  266. //---------------------------------------------------------------------
  267. void __fastcall TAdhocForm::SaveBtnClick(TObject *Sender)
  268. {
  269.   SaveQuery();
  270. }
  271. //---------------------------------------------------------------------
  272. void __fastcall TAdhocForm::SaveAsBtnClick(TObject *Sender)
  273. {
  274.   SaveQueryAs();
  275. }
  276. //---------------------------------------------------------------------
  277. void __fastcall TAdhocForm::CloseBtnClick(TObject *Sender)
  278. {
  279.   Close();
  280. }
  281. //---------------------------------------------------------------------
  282. void __fastcall TAdhocForm::SavedQueryComboChange(TObject *Sender)
  283. {
  284.   ReadQuery();
  285. }
  286. //---------------------------------------------------------------------
  287.