home *** CD-ROM | disk | FTP | other *** search
- { FormulaBuilder Demo }
- { YGB Software, Inc. }
- { Copyright 1995 Clayton Collie }
- { All rights reserved }
-
- {* *}
- {* This unit implements a Database *}
- {* Expression Builder, somewhat like *}
- {* the one included in dBase 5.0 *}
- {* *}
- unit Dbexprfm;
- interface
- uses
- fbCalc,FBComp,FBDBComp,
- WinTypes, WinProcs, Classes, Graphics, Forms, Controls,
- StdCtrls, DBTables, DB, Buttons, ExtCtrls;
-
- type
- TDBExprBuilder = class(TForm)
- BitBtn1: TBitBtn;
- BitBtn2: TBitBtn;
- GroupButton: TBitBtn;
- SpeedButton1: TSpeedButton;
- NeBtn: TSpeedButton;
- LtBtn: TSpeedButton;
- SpeedButton4: TSpeedButton;
- LEBtn: TSpeedButton;
- GeBtn: TSpeedButton;
- PlusBtn: TSpeedButton;
- MinusBtn: TSpeedButton;
- SpeedButton9: TSpeedButton;
- DivideBtn: TSpeedButton;
- SpeedButton11: TSpeedButton;
- QuoteBtn: TSpeedButton;
- AndBtn: TSpeedButton;
- OrBtn: TSpeedButton;
- NotBtn: TSpeedButton;
- SpeedButton16: TSpeedButton;
- Bevel1: TBevel;
- ExpressionMemo: TMemo;
- Label1: TLabel;
- Bevel4: TBevel;
- Calcbtn: TBitBtn;
- StatusPanel: TPanel;
- GroupBox1: TGroupBox;
- Resultmemo: TMemo;
- Label4: TLabel;
- TableGroup: TGroupBox;
- TableListbox: TListBox;
- FieldsGroup: TGroupBox;
- FieldListbox: TListBox;
- FunctionGroup: TGroupBox;
- FunctionListBox: TListBox;
- procedure FieldListboxDblClick(Sender: TObject);
- procedure SpeedButton1Click(Sender: TObject);
- procedure GroupButtonClick(Sender: TObject);
- procedure TableListboxClick(Sender: TObject);
- procedure FormCreate(Sender: TObject);
- procedure CalcbtnClick(Sender: TObject);
- procedure FunctionListBoxDblClick(Sender: TObject);
- procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
- procedure FormActivate(Sender: TObject);
- procedure ExpressionMemoMouseUp(Sender: TObject; Button: TMouseButton;
- Shift: TShiftState; X, Y: Integer);
- Private
- fExpression : TDBExpression;
- fDatabase : TDatabase;
- Procedure setDatabase(db : TDatabase);
- procedure setExpression(const S : TDBExpression);
- function getDatabase : TDatabase;
- Procedure LoadFieldListBox;
- Function Evaluate(var vtype : datatypes;var res : integer): String;
- public
- property Database : TDatabase read getDatabase write setDatabase;
- property Expression : TDBExpression read fExpression write setExpression;
- end;
-
- var
- DBExprBuilder: TDBExprBuilder;
-
-
- {* Allows the user to visually construct an expression based on *}
- {* a BDE database. Returns TRUE if a valid expression was entered, *}
- {* FALSE otherwise. If false, the original expression text is *}
- {* restored to the TDBExpression instance. *}
- {* *}
- {* Possible improvement - Pass in a set to limit the types of *}
- {* expressions permitted. *}
- {* *}
-
- Function BuildDBExpression(const theTitle : string;
- var Expr : TDBExpression):boolean;
-
- implementation
- uses sysutils,fbMisc;
- {$R *.DFM}
-
-
-
- Function BuildDBExpression(const theTitle : string;
- var Expr : TDBExpression):boolean;
- Var Form1 : TDbExprBuilder;
- origExpr : pchar;
- begin
- result := False;
- Application.CreateForm(TDBExprBuilder,Form1);
- origExpr := Expr.StrFormula;
- Try
- form1.Expression := Expr;
- form1.Caption := theTitle;
- Result := form1.ShowModal = mrOk;
- if not result then
- Expr.StrFormula := OrigExpr;
- finally
- Form1.Free;
- StrDispose(OrigExpr);
- end;
- end;
-
-
- Function TDBExprBuilder.Evaluate(var vtype : datatypes;var res : integer) : String;
- var tmp : string;
- tptr : pchar;
- begin
- {}tmp := ExpressionMemo.Text;
- fExpression.Lines := ExpressionMemo.Lines;
- if fExpression.Status = EXPR_SUCCESS then
- begin
- Result := fExpression.asString;
- res := fExpression.Status;
- if res = EXPR_SUCCESS then
- vtype := fExpression.ReturnType;
- end;
- end; {}
-
-
- Procedure TDBExprBuilder.setExpression(const s : TDBExpression);
- begin
- if not Assigned(s) then exit;
- fExpression := s;
- SetDatabase(S.Database);
- ExpressionMemo.Lines.Clear;
- ExpressionMemo.Lines := S.Lines;
- end;
-
-
- Function TDBExprBuilder.getDatabase : TDatabase;
- begin
- result := fDatabase;
- end;
-
- Procedure TDBExprBuilder.SetDatabase( db : TDatabase);
- var i : integer;
- fdataset : TDataset;
- tblname : TFilename;
-
- begin
- FieldListbox.Clear;
- TableListBox.Clear;
- fDatabase := db;
- if db = NIL then exit; {?????}
- for i := 0 to db.datasetCount-1 do
- begin
- fDataset := db.datasets[i];
- if (fDataset is TQuery) then
- tblName := (fDataset as TQuery).Name
- else
- if (fDataset is TTable) then
- tblName := JustFilename( TTable(fDataset).TableName)
- else
- tblName := '';
- if (tblName <> '') then
- TableListBox.Items.Add(TblName);
- end;
- if (TableListBox.Items.Count > 0) then
- begin
- TableListBox.ItemIndex := 0;
- LoadFieldListbox;
- end;
- end;
-
-
- procedure TDBExprBuilder.FieldListboxDblClick(Sender: TObject);
- var
- tblname,fldname : string[50];
- indx : integer;
-
- begin
- indx := TableListBox.ItemIndex;
- if indx = -1 then exit;
- tblName := JustFilename(TableListBox.Items[Indx]);
- indx := FieldListBox.ItemIndex;
- if indx = -1 then exit;
- FldName := FieldListBox.Items[Indx];
- ExpressionMemo.SelText := '['+tblname+'->'+fldname+']';
- end;
-
- procedure TDBExprBuilder.SpeedButton1Click(Sender: TObject);
- var s : string[15];
- begin
- if (sender is TSpeedButton) then
- begin
- s := (sender as TSpeedbutton).Caption;
- if (S[1] = '&') then
- Delete(s,1,1);
- ExpressionMemo.selText := ' '+s+' ';
- end;
- end;
-
- procedure TDBExprBuilder.GroupButtonClick(Sender: TObject);
- var txt : string;
- begin
- txt := ExpressionMemo.SelText;
- if txt <> '' then
- ExpressionMemo.Seltext := '(' + txt + ')';
- end;
-
- Procedure TDBExprBuilder.LoadFieldListBox;
- var fDataset : TDataset;
- i : integer;
- begin
- FieldListbox.Clear;
- i := TableListBox.ItemIndex;
- if (i < 0) or (Database = NIL) then exit;
- fDataset := Database.Datasets[i];
- fDataset.GetFieldNames(FieldListbox.Items);
- end;
-
- procedure TDBExprBuilder.TableListboxClick(Sender: TObject);
- begin
- LoadFieldListBox;
- end;
-
-
- procedure TDBExprBuilder.FormCreate(Sender: TObject);
- var thelist : TStringList;
- begin
- thelist := getFunctionPrototypes(false);
- FunctionListBox.Items.AddStrings(thelist);
- thelist.free;
- end;
-
- procedure TDBExprBuilder.CalcbtnClick(Sender: TObject);
- var s : string;
- vtype : Datatypes;
- res : integer;
-
- begin
- s := Evaluate(vType,res);
- resultMemo.Text := s;
- if res <> EXPR_SUCCESS then
- begin
- StatusPanel.Caption := 'Expression Error : '+FExpression.StatusText;
- MessageBeep(MB_ICONHAND);
- end
- else
- StatusPanel.Caption := DataTypeName(vtype);
- end;
-
-
-
- procedure TDBExprBuilder.FunctionListBoxDblClick(Sender: TObject);
- var fnName : string;
- sel : string;
- indx : integer;
- p : byte;
- begin
- indx := FunctionListBox.ItemIndex;
- if indx = -1 then exit;
- FnName := FunctionListBox.Items[Indx];
- p := Pos('(',fnName);
- if p > 0 then
- fnName := Copy(fnName,1,p-1);
- fnName := fnName + '( ';
- sel := ExpressionMemo.SelText;
- if sel <> '' then
- ExpressionMemo.SelText := fnName + Sel + ' )'
- else
- ExpressionMemo.SelText := fnName+' )';
- end;
-
- procedure TDBExprBuilder.FormCloseQuery(Sender: TObject;var CanClose: Boolean);
- begin
- if (modalResult = mrCancel) then
- canclose := true
- else
- if (ModalResult = mrOk) then
- begin
- fExpression.Lines := ExpressionMemo.Lines;
- canClose := fExpression.Status = EXPR_SUCCESS;
- if not CanClose then
- begin
- StatusPanel.Caption := 'Error : '+FExpression.StatusText;
- MessageBeep(mb_iconHand);
- end;
- end;
- end; { FormCloseQuery }
-
-
- procedure TDBExprBuilder.FormActivate(Sender: TObject);
- begin
- GroupButton.Enabled := False;
- end;
-
-
- {* Enable the Group Button only if text is highlighted *}
- procedure TDBExprBuilder.ExpressionMemoMouseUp(Sender: TObject;
- Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
- begin
- GroupButton.Enabled := ExpressionMemo.selText <> '';
- end;
-
- end.
-