home *** CD-ROM | disk | FTP | other *** search
/ Chip 1997 April / Chip_1997-04_cd.bin / prezent / cb / data.z / QBFFORM.CPP < prev    next >
C/C++ Source or Header  |  1997-01-16  |  11KB  |  276 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 <memory>     //for std::auto_ptr
  10. #include "QBFForm.h"
  11. //---------------------------------------------------------------------------
  12. #pragma resource "*.dfm"
  13. TQueryForm *QueryForm;
  14. //---------------------------------------------------------------------------
  15. __fastcall TQueryForm::TQueryForm(TComponent* Owner)
  16.   : TForm(Owner)
  17. {
  18. }
  19. //---------------------------------------------------------------------------
  20. void __fastcall TQueryForm::FormCreate(TObject *Sender)
  21. {
  22.   Screen->Cursor = Controls::TCursor(crHourGlass);
  23.  
  24.   // Populate the alias list
  25.   ListBox1->Items->Clear();
  26.   Session->GetAliasNames(ListBox1->Items);
  27.  
  28.   // Make sure there are aliases defined
  29.   Screen->Cursor = Controls::TCursor(crDefault);
  30.   if (ListBox1->Items->Count < 1)
  31.     MessageDlg( "There are no database aliases currently defined.  You " \
  32.                 "need at least one alias to use this demonstration.",
  33.                  mtError, TMsgDlgButtons() << mbOK, 0 );
  34.  
  35.   // Default the drop-down list to the first value in the list
  36.   ComboBox1->ItemIndex = 0;
  37.  
  38. }
  39. //---------------------------------------------------------------------
  40. void __fastcall TQueryForm::BitBtn2Click(TObject *Sender)
  41. {
  42.   AnsiString
  43.     strAlias,            // Alias name selected by the user
  44.     strTable,            // Table name selected by the user
  45.     strField,            // Field name selected by the user
  46.     strValue,            // Field Value entered by the user
  47.     strWhere,            // WHERE clause for the user's query
  48.     strQuote,            // Holds quotes is the query field is text
  49.     strQuery;               // String used to construct the query
  50.   std::auto_ptr<TResultForm> frmQuery; // The Results form
  51.   char szTemp[1024];
  52.  
  53.   /*
  54.       The following type is used with the Type drop-down
  55.         list.  The text values corresponding with each item is
  56.         described in comments, along with the relevant SQL operators.
  57.     */
  58.  
  59.   typedef enum {
  60.               soNoCondition,  // not field conditions: no WHERE clause
  61.             soEqual,        // equals: =
  62.             soNotEqual,     // is not equal to: <>
  63.             soLessThan,     // is less than: <
  64.             soLessEqual,    // is less than or equal to: <=
  65.             soMoreThan,     // is greater than: >
  66.             soMoreEqual,    // is greater than or equal to: >=
  67.             soStartsWith,   // starts with: LIKE xx%
  68.             soNoStartsWith, // doesn't start with: NOT LIKE xx%
  69.             soEndsWith,     // ends with: LIKE %xx
  70.             soNoEndsWith,   // doesn't end with: NOT LIKE %xx
  71.             soContains,     // contains: LIKE %xx%
  72.             soNoContains,   // doesn't contain: NOT LIKE %xx%
  73.             soBlank,        // is blank:
  74.             soNotBlank,     // is not blank:
  75.             soInside,       // contains only: IN ( xx, yy, zz )
  76.             soOutside       // doesn't contain: NOT IN (xx, yy, zz)
  77.             } etSQLOps;
  78.  
  79.     // Initialize the variables needed to run the query
  80.   if (ListBox1->ItemIndex == -1)
  81.       throw Exception("Can't Run Query: No Alias Selected");
  82.   else
  83.       strAlias = ListBox1->Items->Strings[ListBox1->ItemIndex];
  84.  
  85.   if (ListBox2->ItemIndex == -1)
  86.     throw Exception("Can't Run Query: No Table Selected");
  87.   else
  88.     strTable = ListBox2->Items->Strings[ListBox2->ItemIndex];
  89.  
  90.   if (ListBox3->ItemIndex == -1)
  91.   {
  92.     if (ComboBox1->ItemIndex > soNoCondition)
  93.       throw Exception("Can't Run Query: No Field Selected");
  94.     else
  95.       strField = "";
  96.   }
  97.   else
  98.     strField = ListBox3->Items->Strings[ListBox3->ItemIndex];
  99.  
  100.   if ((Edit1->Text.Length() == 0) && (ComboBox1->ItemIndex > soNoCondition) &&
  101.     (ComboBox1->ItemIndex < soBlank))
  102.     throw Exception("Can't Run Query: No Search Value Entered");
  103.   else
  104.     strValue = Edit1->Text;
  105.  
  106.   /*
  107.       See if the field being search is a string field.  If so, then pad the
  108.         quote string with quotation marks; otherwise, set it to a null value.
  109.     */
  110.   if (strField.Length() != 0)
  111.   {
  112.       if ((Table1->FieldByName(strField)->DataType == ftString) ||
  113.          (Table1->FieldByName(strField)->DataType == ftMemo))
  114.          strQuote = "\"";
  115.      else
  116.         strQuote = " ";
  117.   }
  118.  
  119.   /*
  120.       Construct the WHERE clause of the query based on the user's choice
  121.         in Type.
  122.   */
  123.  
  124.   szTemp[0] = '\0';
  125.   switch (etSQLOps(ComboBox1->ItemIndex))
  126.   {
  127.     case soNoCondition:
  128.          szTemp[0] = '\0';
  129.         break;
  130.     case soEqual:
  131.          wsprintf(szTemp, "%s = %s%s%s", strField, strQuote, strValue, strQuote);
  132.          break;
  133.     case soNotEqual:
  134.          wsprintf(szTemp, "%s <> %s%s%s", strField, strQuote, strValue, strQuote);
  135.          break;
  136.     case soLessThan:
  137.          wsprintf(szTemp, "%s < %s%s%s", strField, strQuote, strValue, strQuote);
  138.          break;
  139.      case soLessEqual:
  140.          wsprintf(szTemp, "%s <= %s%s%s", strField, strQuote, strValue, strQuote);
  141.          break;
  142.      case soMoreThan:
  143.          wsprintf(szTemp, "%s > %s%s%s", strField, strQuote, strValue, strQuote);
  144.          break;
  145.      case soMoreEqual:
  146.          wsprintf(szTemp, "%s >= %s%s%s", strField, strQuote, strValue, strQuote);
  147.          break;
  148.      case soStartsWith:
  149.          wsprintf(szTemp, "%s LIKE %s%s%%%s", strField, strQuote, strValue, strQuote);
  150.          break;
  151.      case soNoStartsWith:
  152.          wsprintf(szTemp, "%s NOT LIKE %s%s%%%s", strField, strQuote, strValue, strQuote);
  153.          break;
  154.      case soEndsWith:
  155.          wsprintf(szTemp, "%s LIKE %s%%%s%s", strField, strQuote, strValue, strQuote);
  156.          break;
  157.      case soNoEndsWith:
  158.          wsprintf(szTemp, "%s NOT LIKE %s%%%s%s", strField, strQuote, strValue, strQuote);
  159.          break;
  160.      case soContains:
  161.          wsprintf(szTemp, "%s LIKE %s%%%s%%%s", strField, strQuote, strValue, strQuote);
  162.          break;
  163.      case soNoContains:
  164.          wsprintf(szTemp, "%s NOT LIKE %s%%%s%%%s", strField, strQuote, strValue, strQuote);
  165.          break;
  166.      case soBlank:
  167.          wsprintf(szTemp, "%s IS NULL", strField);
  168.          break;
  169.      case soNotBlank:
  170.          wsprintf(szTemp, "%s IS NOT NULL", strField);
  171.          break;
  172.      default:
  173.          szTemp[0] = '\0';
  174.   }
  175.   strWhere = szTemp;
  176.  
  177.     szTemp[0] = '\0';
  178.   if (ComboBox1->ItemIndex == soNoCondition)
  179.       wsprintf(szTemp, "SELECT * FROM \"%s\"", strTable);
  180.   else if (Table1->FieldByName(strField)->DataType == ftString)
  181.       wsprintf(szTemp, "SELECT * FROM \"%s\" t WHERE t.%s", strTable, strWhere);
  182.   else
  183.       wsprintf(szTemp, "SELECT * FROM \"%s\" t WHERE t.%s", strTable, strWhere);
  184.   strQuery = szTemp;
  185.  
  186.   // Create an instance of the browser form.
  187.   frmQuery.reset(new TResultForm(Application));
  188.  
  189.   szTemp[0] = '\0';
  190.  
  191.   Screen->Cursor = Controls::TCursor(crHourGlass);
  192.   if (frmQuery->Query1->Active)
  193.     frmQuery->Query1->Close();
  194.   frmQuery->Query1->DatabaseName = strAlias; // set the alias the query poitns to
  195.   frmQuery->Query1->SQL->Clear();            // empty existing SQL in the query
  196.   frmQuery->Query1->SQL->Add(strQuery);      // add query string to query object
  197.   frmQuery->Query1->Active = True;           // try to run the query
  198.   Screen->Cursor = Controls::TCursor(crDefault);
  199.  
  200.   if (frmQuery->Query1->Active){
  201.     /* If the query didn't return any records, there's no point in
  202.        displaying the form.  In that event, raise an exception. */
  203.     if (frmQuery->Query1->RecordCount < 1)
  204.       throw Exception("No records matched your criteria.  " \
  205.                       "Please try again.");
  206.  
  207.     // write a message to the browse form's status line
  208.     if (strField.Length() == 0){
  209.        wsprintf(szTemp, "Now showing all records from %s ...", strTable);
  210.        frmQuery->Panel3->Caption = szTemp;
  211.     }
  212.     else {
  213.        wsprintf(szTemp, "Now showing %s where %s contains values equal to ..."
  214.           , strTable, strField, strValue);
  215.        frmQuery->Panel3->Caption = szTemp;
  216.     }
  217.     // show the form
  218.     frmQuery->ShowModal();
  219.   }
  220. }
  221. //---------------------------------------------------------------------
  222. void __fastcall TQueryForm::ListBox1Click(TObject *Sender)
  223. {
  224.   AnsiString strValue;       // Holds the alias selected by the user
  225.   bool bIsLocal;      // Indicates whether or not an alias is local
  226.   TStringList *slParams; // Holds the parameters of the selected alias
  227.   int iCounter;      // An integer counter variable for loops
  228.  
  229.   // Determine the alias name selected by the user }
  230.   strValue = ListBox1->Items->Strings[ListBox1->ItemIndex];
  231.  
  232.   /*
  233.       Get the names of the tables in the alias and put them in the
  234.         appropriate list box, making sure the user's choices are reflected
  235.         in the list.
  236.   */
  237.   ListBox2->Items->Clear();
  238.   Session->GetTableNames(strValue,          // alias to enumerate
  239.                         "",                 // pattern to match
  240.                         CheckBox1->Checked, // show extensions flag
  241.                         CheckBox2->Checked, // show system tables flag
  242.                         ListBox2->Items);   // target for table list
  243.  
  244.   /*
  245.       Make sure there are tables defined in the alias.  If not, show an
  246.         error; otherwise, clear the list box.
  247.   */
  248.   Screen->Cursor = Controls::TCursor(crDefault);
  249.   if (ListBox2->Items->Count < 1)
  250.     MessageDlg("There are no tables in the alias you selected.  Please " \
  251.                "choose another", mtError, TMsgDlgButtons() << mbOK, 0 );
  252.  
  253.   ListBox3->Items->Clear();
  254. }
  255. //---------------------------------------------------------------------
  256. void __fastcall TQueryForm::ListBox2Click(TObject *Sender)
  257. {
  258.   Screen->Cursor = Controls::TCursor(crHourGlass);
  259.   try
  260.   {
  261.     // First, disable the TTable object.
  262.     if (Table1->Active) Table1->Close();
  263.  
  264.     // Open the selected table
  265.     Table1->DatabaseName = ListBox1->Items->Strings[ListBox1->ItemIndex];
  266.     Table1->TableName = ListBox2->Items->Strings[ListBox2->ItemIndex];
  267.  
  268.     // Open the table and put a list of the field names in the Fields list box. }
  269.     Table1->Open();
  270.     if (Table1->Active) Table1->GetFieldNames(ListBox3->Items);
  271.   }
  272.   catch (...) {}
  273.   Screen->Cursor = Controls::TCursor(crDefault);
  274. }
  275. //---------------------------------------------------------------------
  276.