1unit frasqldbrestresourceedit; 2 3{$mode objfpc}{$H+} 4 5interface 6 7uses 8 Classes, SysUtils, Forms, Controls, StdCtrls, ExtCtrls, ComCtrls, ActnList, lresources, 9 sqldbrestbridge, sqldbRestSchema, SynEdit, SynHighlighterSQL, sqldbschemaedittools, frasqldbresourcefields; 10 11type 12 13 { TSQLDBRestResourceEditFrame } 14 15 TSQLDBRestResourceEditFrame = class(TBaseEditFrame) 16 AUpdateFields: TAction; 17 AValidateSQL: TAction; 18 AGenerateSQL: TAction; 19 aLResource: TActionList; 20 BFields1: TButton; 21 BGenerate: TButton; 22 BValidate: TButton; 23 BFields: TButton; 24 CBEnabled: TCheckBox; 25 CGOperations: TCheckGroup; 26 CBConnection: TComboBox; 27 CBInMetadata: TCheckBox; 28 EName: TEdit; 29 ETableName: TEdit; 30 Label1: TLabel; 31 Label2: TLabel; 32 Label3: TLabel; 33 PageControl1: TPageControl; 34 PButtons: TPanel; 35 PButtons1: TPanel; 36 fraFields: TResourceFieldsEditFrame; 37 SESelect: TSynEdit; 38 SEInsert: TSynEdit; 39 SEupdate: TSynEdit; 40 SEDelete: TSynEdit; 41 SynSQLSyn1: TSynSQLSyn; 42 TSFields: TTabSheet; 43 TSSelect: TTabSheet; 44 TSInsert: TTabSheet; 45 TabSheet3: TTabSheet; 46 TSDelete: TTabSheet; 47 procedure AGenerateSQLExecute(Sender: TObject); 48 procedure AUpdateFieldsExecute(Sender: TObject); 49 procedure AUpdateFieldsUpdate(Sender: TObject); 50 procedure AValidateSQLExecute(Sender: TObject); 51 procedure AValidateSQLUpdate(Sender: TObject); 52 procedure ETableNameEditingDone(Sender: TObject); 53 private 54 FOnFieldsChanged: TNotifyEvent; 55 FResource: TSQLDBRestResource; 56 function GetOnFieldSelected: TNotifyEvent; 57 function HaveSelectSQL: Boolean; 58 procedure SetOnFieldSelected(AValue: TNotifyEvent); 59 procedure SetResource(AValue: TSQLDBRestResource); 60 Protected 61 procedure FieldsChanged; 62 Procedure UpdateFieldList; 63 procedure SetConnections(AValue: TSQLDBRestConnectionList); override; 64 Procedure SetFrameData(aData: TObject); override; 65 public 66 Function Modified : Boolean; override; 67 Procedure SaveData; override; 68 procedure ShowConnections; 69 Procedure ShowResource; 70 Function FrameCaption: String; override; 71 Property Resource : TSQLDBRestResource Read FResource Write SetResource; 72 Property OnFieldsChanged : TNotifyEvent Read FOnFieldsChanged Write FOnFieldsChanged; 73 Property OnSelectField : TNotifyEvent Read GetOnFieldSelected Write SetOnFieldSelected; 74 end; 75 76implementation 77 78uses dialogs, sqldb; 79 80{$R *.lfm} 81 82 83{ TSQLDBRestResourceEditFrame } 84 85procedure TSQLDBRestResourceEditFrame.AGenerateSQLExecute(Sender: TObject); 86begin 87 SESelect.Lines.Text:=Resource.GenerateDefaultSQL(skSelect); 88end; 89 90procedure TSQLDBRestResourceEditFrame.AUpdateFieldsExecute(Sender: TObject); 91 92begin 93 if Resource.Fields.Count>0 then 94 if QuestionDlg(SResetFields, Format(SResetFieldsPrompt, [LineEnding, LineEnding]), mtWarning, [mrYes, SYesResetFields, mrNo, 95 SDoNotResetFields], 0) <> mrYes then exit; 96 UpdateFieldList; 97end; 98 99function TSQLDBRestResourceEditFrame.HaveSelectSQL: Boolean; 100 101begin 102 Result:=(SESelect.Lines.Count>0) and (Trim(SESelect.Lines[0])<>''); 103end; 104 105function TSQLDBRestResourceEditFrame.GetOnFieldSelected: TNotifyEvent; 106begin 107 Result:=FraFields.OnSelectField; 108end; 109 110procedure TSQLDBRestResourceEditFrame.SetOnFieldSelected(AValue: TNotifyEvent); 111begin 112 FraFields.OnSelectField:=aValue; 113end; 114 115procedure TSQLDBRestResourceEditFrame.AUpdateFieldsUpdate(Sender: TObject); 116begin 117 (Sender as Taction).Enabled:=(ETableName.Text<>'') or HaveSelectSQL; 118end; 119 120procedure TSQLDBRestResourceEditFrame.AValidateSQLExecute(Sender: TObject); 121 122begin 123 With ExecuteSelect(CBConnection.Text,Resource.ProcessSQl(SESelect.Lines.text,'(1=0)','','')) do 124 Free; 125 ShowMessage(SSQLValidatesOK); 126end; 127 128procedure TSQLDBRestResourceEditFrame.AValidateSQLUpdate(Sender: TObject); 129begin 130 (Sender as Taction).Enabled:=CanGetSQLConnection and HaveSelectSQL; 131end; 132 133procedure TSQLDBRestResourceEditFrame.ETableNameEditingDone(Sender: TObject); 134begin 135 if Not SameText(ETableName.Text,Resource.TableName) 136 and (Resource.Fields.Count>0) 137 and Not HaveSelectSQL then 138 if MessageDlg(Format(STableNameChanged, [LineEnding]), mtWarning, [mbYes, mbNo], 0) = mrYes then 139 UpdateFieldList; 140end; 141 142procedure TSQLDBRestResourceEditFrame.SetResource(AValue: TSQLDBRestResource); 143begin 144 if FResource=AValue then Exit; 145 FResource:=AValue; 146 fraFields.Resource:=Resource; 147 ShowResource; 148end; 149 150procedure TSQLDBRestResourceEditFrame.FieldsChanged; 151begin 152 FraFields.ShowResource; 153 If Assigned(FonFieldsChanged) then 154 FOnFieldsChanged(FResource); 155end; 156 157procedure TSQLDBRestResourceEditFrame.UpdateFieldList; 158 159Var 160 Q : TSQLQuery; 161 SQL : String; 162 idxFields : TStringArray; 163 164begin 165 SQL:=Trim(SESelect.Lines.Text); 166 if SQL='' then 167 SQL:=Resource.GenerateDefaultSQL(skSelect); 168 Q:=ExecuteSelect(CBConnection.Text,Resource.ProcessSQl(SQL,'(1=0)','','')); 169 try 170 Resource.Fields.Clear; 171 idxFields:=TSQLDBRestSchema.GetPrimaryIndexFields(Q); 172 Resource.PopulateFieldsFromFieldDefs(Q.FieldDefs,idxFields,Nil,MinFieldOptions); 173 FieldsChanged; 174 finally 175 Q.Free; 176 end; 177end; 178 179procedure TSQLDBRestResourceEditFrame.ShowConnections; 180 181Var 182 I : Integer; 183 184begin 185 With CBConnection.Items do 186 begin 187 BeginUpdate; 188 try 189 if Not assigned(Connections) then 190 For I:=0 to Connections.Count-1 do 191 AddObject(Connections[i].Name,Connections[i]); 192 finally 193 EndUpdate; 194 end; 195 end; 196end; 197 198procedure TSQLDBRestResourceEditFrame.SetConnections(AValue: TSQLDBRestConnectionList); 199begin 200 inherited SetConnections(AValue); 201 ShowConnections; 202end; 203 204procedure TSQLDBRestResourceEditFrame.SetFrameData(aData: TObject); 205begin 206 Resource:=aData as TMySQLDBRestResource; 207end; 208 209function TSQLDBRestResourceEditFrame.Modified: Boolean; 210 211 Function Diff(S1,S2 : TStrings) : Boolean; 212 213 begin 214 Result:=Trim(S1.Text)<>Trim(S2.Text); 215 end; 216 217 Procedure DoOperation(O : TRestOperation); 218 219 begin 220 Result:=Result or (CGOperations.Checked[Ord(O)-1] <> (O in Resource.AllowedOperations)); 221 end; 222 223Var 224 O : TRestOperation; 225 226begin 227 Result:=False; 228 With Resource do 229 begin 230 Result:=(ResourceName<>eName.Text) Or 231 (TableName<>ETableName.Text) Or 232 (Enabled<>CBEnabled.Checked) Or 233 (InMetadata<>CBInMetadata.Checked) or 234 Diff(SQLSelect, SESelect.Lines) Or 235 Diff(SQLInsert,SEInsert.Lines) Or 236 Diff(SQLUpdate,SEUpdate.Lines) Or 237 Diff(SQLDelete,SEDelete.Lines); 238 for O in TRestOperation do 239 if o<>roUnknown then 240 DoOperation(O); 241 end; 242end; 243 244procedure TSQLDBRestResourceEditFrame.SaveData; 245 246 Procedure DoOperation(O : TRestOperation); 247 248 begin 249 if CGOperations.Checked[Ord(O)-1] then 250 Resource.AllowedOperations:=Resource.AllowedOperations+[O] 251 else 252 Resource.AllowedOperations:=Resource.AllowedOperations-[O] 253 end; 254 255Var 256 O : TRestOperation; 257 258begin 259 With Resource do 260 begin 261 ResourceName := eName.Text; 262 TableName := ETableName.Text; 263 SQLSelect := SESelect.Lines; 264 SQLInsert := SEInsert.Lines; 265 SQLUpdate := SEUpdate.Lines; 266 SQLDelete := SEDelete.Lines; 267 Enabled := CBEnabled.Checked; 268 InMetadata := CBInMetadata.Checked; 269 for O in TRestOperation do 270 if o<>roUnknown then 271 DoOperation(O); 272 end; 273end; 274 275procedure TSQLDBRestResourceEditFrame.ShowResource; 276 277 Procedure DoOperation(O : TRestOperation); 278 279 begin 280 CGOperations.Checked[Ord(O)-1]:=O in Resource.AllowedOperations; 281 end; 282 283Var 284 O : TRestOperation; 285 286begin 287 With Resource do 288 begin 289 eName.Text:=ResourceName; 290 ETableName.Text:=TableName; 291 SESelect.Lines:=SQLSelect; 292 SEInsert.Lines:=SQLInsert; 293 SEUpdate.Lines:=SQLUpdate; 294 SEDelete.Lines:=SQLDelete; 295 CBEnabled.Checked:=Enabled; 296 CBInMetadata.Checked:=InMetadata; 297 for O in TRestOperation do 298 if o<>roUnknown then 299 DoOperation(O); 300 end; 301end; 302 303function TSQLDBRestResourceEditFrame.FrameCaption: String; 304begin 305 if FResource=Nil then 306 Result:=SUnknownObject 307 else 308 Result:=FResource.ResourceName; 309 Result:=Format(SEditObject,[SResource,Result]); 310end; 311 312end. 313 314