File: frasqldbrestresourceedit.pp

package info (click to toggle)
lazarus 2.2.6%2Bdfsg2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 219,980 kB
  • sloc: pascal: 1,944,919; xml: 357,634; makefile: 270,608; cpp: 57,115; sh: 3,249; java: 609; perl: 297; sql: 222; ansic: 137
file content (313 lines) | stat: -rw-r--r-- 8,173 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
unit frasqldbrestresourceedit;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Forms, Controls, StdCtrls, ExtCtrls, ComCtrls, ActnList, lresources,
  sqldbrestbridge, sqldbRestSchema, SynEdit, SynHighlighterSQL, sqldbschemaedittools, frasqldbresourcefields;

type

  { TSQLDBRestResourceEditFrame }

  TSQLDBRestResourceEditFrame = class(TBaseEditFrame)
    AUpdateFields: TAction;
    AValidateSQL: TAction;
    AGenerateSQL: TAction;
    aLResource: TActionList;
    BFields1: TButton;
    BGenerate: TButton;
    BValidate: TButton;
    BFields: TButton;
    CBEnabled: TCheckBox;
    CGOperations: TCheckGroup;
    CBConnection: TComboBox;
    CBInMetadata: TCheckBox;
    EName: TEdit;
    ETableName: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    PageControl1: TPageControl;
    PButtons: TPanel;
    PButtons1: TPanel;
    fraFields: TResourceFieldsEditFrame;
    SESelect: TSynEdit;
    SEInsert: TSynEdit;
    SEupdate: TSynEdit;
    SEDelete: TSynEdit;
    SynSQLSyn1: TSynSQLSyn;
    TSFields: TTabSheet;
    TSSelect: TTabSheet;
    TSInsert: TTabSheet;
    TabSheet3: TTabSheet;
    TSDelete: TTabSheet;
    procedure AGenerateSQLExecute(Sender: TObject);
    procedure AUpdateFieldsExecute(Sender: TObject);
    procedure AUpdateFieldsUpdate(Sender: TObject);
    procedure AValidateSQLExecute(Sender: TObject);
    procedure AValidateSQLUpdate(Sender: TObject);
    procedure ETableNameEditingDone(Sender: TObject);
  private
    FOnFieldsChanged: TNotifyEvent;
    FResource: TSQLDBRestResource;
    function GetOnFieldSelected: TNotifyEvent;
    function HaveSelectSQL: Boolean;
    procedure SetOnFieldSelected(AValue: TNotifyEvent);
    procedure SetResource(AValue: TSQLDBRestResource);
  Protected
    procedure FieldsChanged;
    Procedure UpdateFieldList;
    procedure SetConnections(AValue: TSQLDBRestConnectionList); override;
    Procedure SetFrameData(aData: TObject); override;
  public
    Function Modified : Boolean; override;
    Procedure SaveData; override;
    procedure ShowConnections;
    Procedure ShowResource;
    Function FrameCaption: String; override;
    Property Resource : TSQLDBRestResource Read FResource Write SetResource;
    Property OnFieldsChanged : TNotifyEvent Read FOnFieldsChanged Write FOnFieldsChanged;
    Property OnSelectField : TNotifyEvent Read GetOnFieldSelected Write SetOnFieldSelected;
  end;

implementation

uses dialogs, sqldb;

{$R *.lfm}


{ TSQLDBRestResourceEditFrame }

procedure TSQLDBRestResourceEditFrame.AGenerateSQLExecute(Sender: TObject);
begin
  SESelect.Lines.Text:=Resource.GenerateDefaultSQL(skSelect);
end;

procedure TSQLDBRestResourceEditFrame.AUpdateFieldsExecute(Sender: TObject);

begin
  if Resource.Fields.Count>0 then
     if QuestionDlg(SResetFields, Format(SResetFieldsPrompt, [LineEnding, LineEnding]), mtWarning, [mrYes, SYesResetFields, mrNo,
       SDoNotResetFields], 0) <> mrYes then exit;
  UpdateFieldList;
end;

function TSQLDBRestResourceEditFrame.HaveSelectSQL: Boolean;

begin
  Result:=(SESelect.Lines.Count>0) and (Trim(SESelect.Lines[0])<>'');
end;

function TSQLDBRestResourceEditFrame.GetOnFieldSelected: TNotifyEvent;
begin
  Result:=FraFields.OnSelectField;
end;

procedure TSQLDBRestResourceEditFrame.SetOnFieldSelected(AValue: TNotifyEvent);
begin
  FraFields.OnSelectField:=aValue;
end;

procedure TSQLDBRestResourceEditFrame.AUpdateFieldsUpdate(Sender: TObject);
begin
  (Sender as Taction).Enabled:=(ETableName.Text<>'') or HaveSelectSQL;
end;

procedure TSQLDBRestResourceEditFrame.AValidateSQLExecute(Sender: TObject);

begin
  With ExecuteSelect(CBConnection.Text,Resource.ProcessSQl(SESelect.Lines.text,'(1=0)','','')) do
    Free;
  ShowMessage(SSQLValidatesOK);
end;

procedure TSQLDBRestResourceEditFrame.AValidateSQLUpdate(Sender: TObject);
begin
  (Sender as Taction).Enabled:=CanGetSQLConnection and HaveSelectSQL;
end;

procedure TSQLDBRestResourceEditFrame.ETableNameEditingDone(Sender: TObject);
begin
  if Not SameText(ETableName.Text,Resource.TableName)
     and (Resource.Fields.Count>0)
     and Not HaveSelectSQL then
    if MessageDlg(Format(STableNameChanged, [LineEnding]), mtWarning, [mbYes, mbNo], 0) = mrYes then
      UpdateFieldList;
end;

procedure TSQLDBRestResourceEditFrame.SetResource(AValue: TSQLDBRestResource);
begin
  if FResource=AValue then Exit;
  FResource:=AValue;
  fraFields.Resource:=Resource;
  ShowResource;
end;

procedure TSQLDBRestResourceEditFrame.FieldsChanged;
begin
  FraFields.ShowResource;
  If Assigned(FonFieldsChanged) then
    FOnFieldsChanged(FResource);
end;

procedure TSQLDBRestResourceEditFrame.UpdateFieldList;

Var
  Q : TSQLQuery;
  SQL : String;
  idxFields : TStringArray;

begin
  SQL:=Trim(SESelect.Lines.Text);
  if SQL='' then
    SQL:=Resource.GenerateDefaultSQL(skSelect);
  Q:=ExecuteSelect(CBConnection.Text,Resource.ProcessSQl(SQL,'(1=0)','',''));
  try
    Resource.Fields.Clear;
    idxFields:=TSQLDBRestSchema.GetPrimaryIndexFields(Q);
    Resource.PopulateFieldsFromFieldDefs(Q.FieldDefs,idxFields,Nil,MinFieldOptions);
    FieldsChanged;
  finally
    Q.Free;
  end;
end;

procedure TSQLDBRestResourceEditFrame.ShowConnections;

Var
  I : Integer;

begin
  With CBConnection.Items do
      begin
      BeginUpdate;
      try
        if Not assigned(Connections) then
          For I:=0 to Connections.Count-1 do
            AddObject(Connections[i].Name,Connections[i]);
      finally
        EndUpdate;
      end;
      end;
end;

procedure TSQLDBRestResourceEditFrame.SetConnections(AValue: TSQLDBRestConnectionList);
begin
  inherited SetConnections(AValue);
  ShowConnections;
end;

procedure TSQLDBRestResourceEditFrame.SetFrameData(aData: TObject);
begin
  Resource:=aData as TMySQLDBRestResource;
end;

function TSQLDBRestResourceEditFrame.Modified: Boolean;

  Function Diff(S1,S2 : TStrings) : Boolean;

  begin
    Result:=Trim(S1.Text)<>Trim(S2.Text);
  end;

  Procedure DoOperation(O : TRestOperation);

  begin
    Result:=Result or (CGOperations.Checked[Ord(O)-1] <> (O in Resource.AllowedOperations));
  end;

Var
  O : TRestOperation;

begin
  Result:=False;
  With Resource do
    begin
    Result:=(ResourceName<>eName.Text) Or
            (TableName<>ETableName.Text) Or
            (Enabled<>CBEnabled.Checked) Or
            (InMetadata<>CBInMetadata.Checked) or
            Diff(SQLSelect, SESelect.Lines) Or
            Diff(SQLInsert,SEInsert.Lines) Or
            Diff(SQLUpdate,SEUpdate.Lines) Or
            Diff(SQLDelete,SEDelete.Lines);
    for O in TRestOperation do
      if o<>roUnknown then
        DoOperation(O);
    end;
end;

procedure TSQLDBRestResourceEditFrame.SaveData;

  Procedure DoOperation(O : TRestOperation);

  begin
    if CGOperations.Checked[Ord(O)-1] then
      Resource.AllowedOperations:=Resource.AllowedOperations+[O]
    else
      Resource.AllowedOperations:=Resource.AllowedOperations-[O]
  end;

Var
  O : TRestOperation;

begin
  With Resource do
    begin
    ResourceName := eName.Text;
    TableName    := ETableName.Text;
    SQLSelect    := SESelect.Lines;
    SQLInsert    := SEInsert.Lines;
    SQLUpdate    := SEUpdate.Lines;
    SQLDelete    := SEDelete.Lines;
    Enabled      := CBEnabled.Checked;
    InMetadata   := CBInMetadata.Checked;
    for O in TRestOperation do
      if o<>roUnknown then
        DoOperation(O);
    end;
end;

procedure TSQLDBRestResourceEditFrame.ShowResource;

  Procedure DoOperation(O : TRestOperation);

  begin
    CGOperations.Checked[Ord(O)-1]:=O in Resource.AllowedOperations;
  end;

Var
  O : TRestOperation;

begin
  With Resource do
    begin
    eName.Text:=ResourceName;
    ETableName.Text:=TableName;
    SESelect.Lines:=SQLSelect;
    SEInsert.Lines:=SQLInsert;
    SEUpdate.Lines:=SQLUpdate;
    SEDelete.Lines:=SQLDelete;
    CBEnabled.Checked:=Enabled;
    CBInMetadata.Checked:=InMetadata;
    for O in TRestOperation do
      if o<>roUnknown then
        DoOperation(O);
    end;
end;

function TSQLDBRestResourceEditFrame.FrameCaption: String;
begin
  if FResource=Nil then
    Result:=SUnknownObject
  else
    Result:=FResource.ResourceName;
  Result:=Format(SEditObject,[SResource,Result]);
end;

end.