ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/public/ibx/trunk/design/ibgeneratoreditor.pas
Revision: 5
Committed: Fri Feb 18 16:26:16 2011 UTC (13 years, 8 months ago) by tony
Content type: text/x-pascal
File size: 4457 byte(s)
Log Message:
Committing updates for Release pre-release

File Contents

# User Rev Content
1 tony 5 unit IBGeneratorEditor;
2    
3     {$mode objfpc}{$H+}
4    
5     interface
6    
7     uses
8     Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
9     ExtCtrls, StdCtrls, ComCtrls, IBDatabase, IBCustomDataSet, IBSystemTables;
10    
11     type
12    
13     { TGeneratorEditor }
14    
15     TGeneratorEditor = class(TForm)
16     Bevel1: TBevel;
17     Button1: TButton;
18     Button2: TButton;
19     GeneratorNames: TComboBox;
20     FieldNames: TComboBox;
21     IncrementBy: TEdit;
22     Label1: TLabel;
23     Label2: TLabel;
24     Label3: TLabel;
25     OnNewRecord: TRadioButton;
26     OnPost: TRadioButton;
27     UpDown1: TUpDown;
28     procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
29     procedure FormShow(Sender: TObject);
30     private
31     FGenerator: TIBGenerator;
32     FTableName: string;
33     FIBSystemTables: TIBSystemTables;
34     { private declarations }
35     procedure LoadGenerators;
36     procedure LoadFieldNames;
37     function GetPrimaryKey: string;
38     procedure SetGenerator(const AValue: TIBGenerator);
39     procedure SetDatabase(ADatabase: TIBDatabase; ATransaction: TIBTransaction);
40     public
41     { public declarations }
42     constructor Create(TheOwner: TComponent); override;
43     destructor Destroy; override;
44     property Generator: TIBGenerator read FGenerator write SetGenerator;
45     end;
46    
47     function EditGenerator(AGenerator: TIBGenerator): boolean;
48    
49     implementation
50    
51     function EditGenerator(AGenerator: TIBGenerator): boolean;
52     var Database: TIBDatabase;
53     begin
54     Result := false;
55     if AGenerator.SelectSQL = '' then
56     begin
57     ShowMessage('No Select SQL Found!');
58     Exit
59     end;
60     if assigned(Database) then
61     begin
62     Database := AGenerator.Owner.Database;
63     if not assigned(AGenerator.Owner.Transaction)then
64     begin
65     ShowMessage('No Transaction assigned to DataSet!');
66     Exit
67     end;
68     Database.Connected := true;
69    
70     with TGeneratorEditor.Create(Application) do
71     try
72     Generator := AGenerator;
73     Result := ShowModal = mrOK
74     finally
75     Free
76     end;
77     end;
78     end;
79    
80     { TGeneratorEditor }
81    
82     procedure TGeneratorEditor.FormShow(Sender: TObject);
83     begin
84     LoadGenerators;
85     LoadFieldNames;
86     if Generator.GeneratorName <> '' then
87     GeneratorNames.ItemIndex := GeneratorNames.Items.IndexOf(Generator.GeneratorName);
88     if Generator.FieldName <> '' then
89     FieldNames.ItemIndex := FieldNames.Items.IndexOf(Generator.FieldName)
90     else
91     FieldNames.ItemIndex := FieldNames.Items.IndexOf(GetPrimaryKey);
92    
93     if Generator.ApplyOnEvent = gaeOnNewRecord then
94     OnNewRecord.Checked := true
95     else
96     OnPost.Checked := true;
97     IncrementBy.Text := IntToStr(Generator.Increment);
98     end;
99    
100     procedure TGeneratorEditor.FormClose(Sender: TObject;
101     var CloseAction: TCloseAction);
102     begin
103     if ModalResult = mrOK then
104     begin
105     Generator.GeneratorName := GeneratorNames.Text;
106     Generator.FieldName := FieldNames.Text;
107     if OnNewRecord.Checked then
108     Generator.ApplyOnEvent := gaeOnNewRecord
109     else
110     Generator.ApplyOnEvent := gaeOnPostRecord;
111     Generator.Increment := StrToInt(IncrementBy.Text)
112    
113     end;
114     end;
115    
116     procedure TGeneratorEditor.LoadGenerators;
117     begin
118     FIBSystemTables.GetGenerators(GeneratorNames.Items);
119     if GeneratorNames.Items.Count > 0 then
120     GeneratorNames.ItemIndex := 0
121     end;
122    
123     procedure TGeneratorEditor.LoadFieldNames;
124     begin
125     FIBSystemTables.GetTableAndColumns(FGenerator.SelectSQL,FTableName,FieldNames.Items)
126     end;
127    
128     function TGeneratorEditor.GetPrimaryKey: string;
129     var Keys: TStringList;
130     begin
131     Result := '';
132     Keys := TStringList.Create;
133     try
134     FIBSystemTables.GetPrimaryKeys(FTableName,Keys);
135     if Keys.Count > 0 then
136     Result := Keys[0];
137     finally
138     Keys.Free
139     end;
140     end;
141    
142     procedure TGeneratorEditor.SetGenerator(const AValue: TIBGenerator);
143     begin
144     FGenerator := AValue;
145     SetDatabase(Generator.Owner.Database,Generator.Owner.Transaction);
146     end;
147    
148     procedure TGeneratorEditor.SetDatabase(ADatabase: TIBDatabase; ATransaction: TIBTransaction);
149     begin
150     if not assigned(ADatabase) then
151     raise Exception.Create('A Database must be assigned');
152     if not assigned(ATransaction) then
153     raise Exception.Create('A Transaction must be assigned');
154     FIBSystemTables.SelectDatabase( ADatabase,ATransaction)
155     end;
156    
157     constructor TGeneratorEditor.Create(TheOwner: TComponent);
158     begin
159     inherited Create(TheOwner);
160     FIBSystemTables := TIBSystemTables.Create
161     end;
162    
163     destructor TGeneratorEditor.Destroy;
164     begin
165     if assigned(FIBSystemTables) then FIBSystemTables.Free;
166     inherited Destroy;
167     end;
168    
169     initialization
170     {$I ibgeneratoreditor.lrs}
171    
172     end.
173