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 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
|
unit reglazsqldbrest;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, PropEdits, ComponentEditors, ProjectIntf, sqldb, sqldbrestschema,
sqldbrestcsv ,sqldbrestxml, sqldbrestcds, sqldbrestado,
sqldbrestio, sqldbrestauth, sqldbrestbridge, sqldbrestmodule;
Type
{ ---------------------------------------------------------------------
Property editors
---------------------------------------------------------------------}
{ TSQLDBConnectionTypePropertyEditor }
TSQLDBConnectionTypePropertyEditor = class(TStringPropertyEditor)
public
function GetAttributes: TPropertyAttributes; override;
procedure GetValues(Proc: TGetStrProc); override;
procedure SetValue(const NewValue: ansistring); override;
end;
{ TSQLDBRestDefaultConnectionPropertyEditor }
TSQLDBRestDefaultConnectionPropertyEditor = class(TStringPropertyEditor)
public
function GetAttributes: TPropertyAttributes; override;
procedure GetValues(Proc: TGetStrProc); override;
procedure SetValue(const NewValue: ansistring); override;
end;
{ TSQLDBRestInOutFormatPropertyEditor }
TSQLDBRestInOutFormatPropertyEditor = class(TStringPropertyEditor)
public
Class Function StreamType : TRestStreamerType; virtual; abstract;
function GetAttributes: TPropertyAttributes; override;
procedure GetValues(Proc: TGetStrProc); override;
procedure SetValue(const NewValue: ansistring); override;
end;
{ TSQLDBRestInputFormatPropertyEditor }
TSQLDBRestInputFormatPropertyEditor = class(TSQLDBRestInOutFormatPropertyEditor)
Class Function StreamType : TRestStreamerType; override;
end;
{ TSQLDBRestOutputFormatPropertyEditor }
TSQLDBRestOutputFormatPropertyEditor = class(TSQLDBRestInOutFormatPropertyEditor)
Class Function StreamType : TRestStreamerType; override;
end;
{ TSQLDBRestResourceNamePropertyEditor }
TSQLDBRestResourceNamePropertyEditor = class(TStringPropertyEditor)
protected
function GetResourceList: TSQLDBRestResourceList;virtual;
public
function GetAttributes: TPropertyAttributes; override;
procedure GetValues(Proc: TGetStrProc); override;
end;
{ ---------------------------------------------------------------------
Component editors
---------------------------------------------------------------------}
{ TSQLDBRestSchemaComponentEditor }
TSQLDBRestSchemaComponentEditor = class(TComponentEditor)
private
procedure DoLoadSchemaFromConnection(S: TSQLDBRestSchema);
procedure EditSchema(S: TSQLDBRestSchema);
procedure FillSchema(S: TSQLDBRestSchema; Conn: TSQLDBRestConnection; Opts: TRestFieldOptions; AllTables: Boolean);
function GetSchemaConnections(S: TSQLDBRestSchema): TSQLDBRestConnectionList;
function GetTableList(aConn: TSQLConnection; aList: TStrings): Boolean;
public
procedure ExecuteVerb(Index: Integer); override;
function GetVerb(Index: Integer): string; override;
function GetVerbCount: Integer; override;
end;
{ TSQLDBRestDispatcherComponentEditor }
TSQLDBRestDispatcherComponentEditor = class(TComponentEditor)
private
procedure ExposeConnection(D: TSQLDBRestDispatcher);
public
procedure ExecuteVerb(Index: Integer); override;
function GetVerb(Index: Integer): string; override;
function GetVerbCount: Integer; override;
end;
{ ---------------------------------------------------------------------
Rest Module
---------------------------------------------------------------------}
TFileRestModule = class(TFileDescPascalUnitWithResource)
public
constructor Create; override;
function GetInterfaceUsesSection: string; override;
function GetLocalizedName: string; override;
function GetLocalizedDescription: string; override;
function GetImplementationSource(const Filename, SourceName, ResourceName: string): string;override;
end;
Procedure Register;
implementation
uses
// Lazarus
LResources, codecache, Controls, Forms, Dialogs, SrcEditorIntf, lazideintf, FormEditingIntf, codetoolmanager,
// FPC
sqldbrestini,
// This package
frmsqldbrestselectconn,
frmsqldbrestdispatchini,
frmsqldbrestselecttables,
dlgeditsqldbrestschema,
reslazsqldbrest;
Var
FileDescriptorRestModule: TFileRestModule;
Procedure Register;
begin
RegisterComponents('fpWeb',[
TSQLDBRESTDispatcher,
TSQLDBRESTSchema,
TRESTBasicAuthenticator,
TSQLDBRestBusinessProcessor
]);
RegisterPropertyEditor(TypeInfo(AnsiString),
TSQLDBRestConnection, 'ConnectionType', TSQLDBConnectionTypePropertyEditor);
RegisterPropertyEditor(TypeInfo(AnsiString),
TSQLDBRestDispatcher, 'OutputFormat', TSQLDBRestOutputFormatPropertyEditor);
RegisterPropertyEditor(TypeInfo(AnsiString),
TSQLDBRestDispatcher, 'InputFormat', TSQLDBRestInPutFormatPropertyEditor);
RegisterPropertyEditor(TypeInfo(UTF8String),
TSQLDBRestDispatcher, 'DefaultConnection', TSQLDBRestDefaultConnectionPropertyEditor);
RegisterPropertyEditor(TypeInfo(UTF8String),
TSQLDBRestBusinessProcessor,'ResourceName',TSQLDBRestResourceNamePropertyEditor);
RegisterComponentEditor(TSQLDBRESTSchema,TSQLDBRESTSchemaComponentEditor);
RegisterComponentEditor(TSQLDBRestDispatcher,TSQLDBRestDispatcherComponentEditor);
FileDescriptorRestModule:=TFileRestModule.Create;
RegisterProjectFileDescriptor(FileDescriptorRestModule);
FormEditingHook.RegisterDesignerBaseClass(TSQLDBRestModule);
end;
{ TSQLDBRestSchemaComponentEditor }
Function GetFileName(var aFileName : UTF8String; aFilters : UTF8String; ForSave : Boolean = False) : Boolean;
Var
Dlg : TFileDialog;
begin
if forSave then
begin
DLG:=TSaveDialog.Create(Application);
With TSaveDialog(DLG) do
Options:=Options+[ofPathMustExist,ofOverwritePrompt];
end
else
begin
DLG:=TOpenDialog.Create(Application);
With TOpenDialog(DLG) do
Options:=Options+[ofFileMustExist];
end;
try
DLG.FileName:=aFileName;
DLG.Filter:=aFilters;
DLG.DefaultExt:=ExtractFileExt(aFileName);
Result:=Dlg.Execute;
if Result then
aFileName:=DLG.FileName;
finally
Dlg.Free;
end;
end;
{ TSQLDBRestResourceNamePropertyEditor }
function TSQLDBRestResourceNamePropertyEditor.GetAttributes: TPropertyAttributes;
begin
Result := [paSortList, paValueList, paRevertable];
end;
function TSQLDBRestResourceNamePropertyEditor.GetResourceList : TSQLDBRestResourceList;
Var
S : TSQLDBRestSchema;
C : TPersistent;
begin
Result:=Nil;
C:=TPersistent(GetComponent(0));
if not (Assigned(C) and (C is TSQLDBRestBusinessProcessor)) then
exit;
S:=TSQLDBRestBusinessProcessor(C).Schema;
if Assigned(S) then
Result:=S.Resources;
end;
procedure TSQLDBRestResourceNamePropertyEditor.GetValues(Proc: TGetStrProc);
Var
L : TSQLDBRestResourceList;
i : Integer;
begin
L:=GetResourceList;
if Not Assigned(L) then
exit;
For I:=0 to L.Count-1 do
Proc(L[i].ResourceName);
end;
{ TSQLDBRestDefaultConnectionPropertyEditor }
function TSQLDBRestDefaultConnectionPropertyEditor.GetAttributes: TPropertyAttributes;
begin
Result := [paSortList, paValueList, paRevertable];
end;
procedure TSQLDBRestDefaultConnectionPropertyEditor.GetValues(Proc: TGetStrProc);
Var
I : Integer;
D : TSQLDBRestDispatcher;
begin
D:=GetComponent(0) as TSQLDBRestDispatcher;
for I:=0 to D.Connections.Count-1 do
Proc(D.Connections[i].Name);
end;
procedure TSQLDBRestDefaultConnectionPropertyEditor.SetValue(const NewValue: ansistring
);
begin
inherited SetValue(NewValue);
end;
{ TSQLDBRestOutputFormatPropertyEditor }
class function TSQLDBRestOutputFormatPropertyEditor.StreamType: TRestStreamerType;
begin
Result:=rstOutput;
end;
{ TSQLDBRestInputFormatPropertyEditor }
class function TSQLDBRestInputFormatPropertyEditor.StreamType: TRestStreamerType;
begin
Result:=rstInput;
end;
{ TSQLDBRestInOutFormatPropertyEditor }
function TSQLDBRestInOutFormatPropertyEditor.GetAttributes: TPropertyAttributes;
begin
Result := [paMultiSelect, paSortList, paValueList, paRevertable];
end;
procedure TSQLDBRestInOutFormatPropertyEditor.GetValues(Proc: TGetStrProc);
Var
L : TStringList;
I : Integer;
begin
L:=TStringList.Create;
try
TStreamerFactory.GetStreamerList(L,StreamType);
for I:=0 to L.Count-1 do
Proc(L[i]);
finally
L.Free;
end;
end;
procedure TSQLDBRestInOutFormatPropertyEditor.SetValue(
const NewValue: ansistring);
begin
inherited SetValue(NewValue);
end;
{ TSQLDBRestDispatcherComponentEditor }
procedure TSQLDBRestDispatcherComponentEditor.ExposeConnection(D : TSQLDBRestDispatcher);
Var
C : TSQLDBRestConnection;
aSchema : TSQLDBRestSchema;
aOptions : TRestFieldOptions;
allTables : Boolean;
begin
C:=SelectRestConnection(D.Connections,aOptions,allTables);
if C=Nil then
exit;
aSchema:=D.ExposeConnection(Designer.LookupRoot,C,Nil,aOptions);
aSchema.Name:=Designer.CreateUniqueComponentName(aSchema.ClassName);
Designer.Modified;
// Todo: add Aschema to form.
end;
procedure TSQLDBRestDispatcherComponentEditor.ExecuteVerb(Index: Integer);
Var
FN : UTF8String;
D : TSQLDBRestDispatcher;
OI : TDispatcherIniOptions;
begin
D:=Component as TSQLDBRestDispatcher;
Case Index of
0 :
begin
ExposeConnection(D);
Designer.Modified;
end;
1,2 :
begin
FN:=Component.Name+'.ini';
if GetDispatchLoadSaveOptions(Index=1,OI) then
if GetFileName(FN,'INI Files|*.ini|All files|'+allFilesMask,Index=1) then
if Index=0 then
D.SaveToFile(FN,OI)
else
begin
D.LoadFromFile(FN,OI);
Designer.Modified;
end;
end;
end;
end;
function TSQLDBRestDispatcherComponentEditor.GetVerb(Index: Integer): string;
begin
Case Index of
0 : Result:=SExposeAConnection;
1 : Result:=SSaveSettingsToIni;
2 : Result:=SLoadSettingsFromIni;
end;
end;
function TSQLDBRestDispatcherComponentEditor.GetVerbCount: Integer;
begin
Result:=3;
end;
Function TSQLDBRestSchemaComponentEditor.GetTableList(aConn : TSQLConnection; aList : TStrings) : Boolean;
Var
L : TStringList;
F : TSQLDBRestSelectTablesForm;
begin
Result:=False;
F:=Nil;
L:=TStringList.Create;
try
L.Sorted:=true;
aConn.GetTableNames(L,False);
if L.Count>0 then
begin
F:=TSQLDBRestSelectTablesForm.Create(Application);
F.Tables:=L;
Result:=F.ShowModal=mrOK;
if Result then
F.GetSelectedTables(aList);
end;
finally
L.Free;
F.Free;
end;
end;
procedure TSQLDBRestSchemaComponentEditor.FillSchema(S : TSQLDBRestSchema; Conn : TSQLDBRestConnection; Opts : TRestFieldOptions; AllTables : Boolean);
Var
SQLC : TSQLConnection;
T : TSQLTransaction;
sTables : TStrings;
aCount : Integer;
begin
T:=Nil;
sTables:=nil;
SQLC:=Conn.SingleConnection;
if SQLC=Nil then
SQLC:=TSQLConnector.Create(Nil);
try
if (SQLC.Transaction=Nil) then
begin
T:=TSQLTransaction.Create(SQLC);
SQLC.Transaction:=T;
Conn.ConfigConnection(SQLC);
end;
SQLC.Connected:=true;
if not AllTables then
begin
sTables:=TStringList.Create;
if not GetTableList(SQLC,sTables) then
exit;
end;
aCount:=S.Resources.Count;
S.PopulateResources(SQLC,sTables,Opts);
ShowMessage(Format(SAddedNTables,[S.Resources.Count-aCount]));
Designer.Modified;
finally
T.Free;
if SQLC<>Conn.SingleConnection then
SQLC.Free;
end;
end;
procedure TSQLDBRestSchemaComponentEditor.DoLoadSchemaFromConnection(S : TSQLDBRestSchema);
Var
L : TStringList;
O : TComponent;
D : TSQLDBRestDispatcher;
I,J : Integer;
Opts : TRestFieldOptions;
Conn : TSQLDBRestConnection;
AllTables : Boolean;
begin
L:=TStringList.Create;
try
O:=S.Owner;
While O<>Nil do
begin
for I:=0 to O.ComponentCount-1 do
if O.Components[i] is TSQLDBRestDispatcher then
begin
D:=O.Components[i] as TSQLDBRestDispatcher;
For J:=0 To D.Connections.Count-1 do
L.AddObject(D.Name+'.'+D.Connections[J].Name,D.Connections[J]);
end;
O:=O.Owner;
end;
if L.Count=0 then
ShowMessage(SErrNoConnectionsFound)
else
begin
Conn:=SelectRestConnection(L,Opts,allTables);
if Conn<>Nil then
FillSchema(S,Conn,Opts,AllTables);
end;
finally
L.Free;
end;
end;
Function TSQLDBRestSchemaComponentEditor.GetSchemaConnections(S : TSQLDBRestSchema) : TSQLDBRestConnectionList;
Var
C : TComponent;
I : Integer;
D : TSQLDBRestDispatcher;
begin
C:=S.Owner;
Result:=Nil;
While (Result=Nil) and (C<>Nil) do
begin
I:=0;
While (Result=Nil) and (I<C.ComponentCount-1) do
begin
if C.Components[i] is TSQLDBRestDispatcher then
begin
D:=C.Components[i] as TSQLDBRestDispatcher;
if D.Schemas.IndexOfSchema(S.Name)<>-1 then
Result:=D.Connections;
end;
Inc(I)
end;
C:=C.Owner;
end;
end;
procedure TSQLDBRestSchemaComponentEditor.EditSchema(S : TSQLDBRestSchema);
Var
Frm : TSQLDBRestSchemaEditorForm;
cList : TSQLDBRestConnectionList;
begin
Frm:=TSQLDBRestSchemaEditorForm.Create(Application);
try
Frm.Schema:=S;
cList:=GetSchemaConnections(S);
if Assigned(cList) then
Frm.Connections:=cList;
if Frm.ShowModal=mrOK then
begin
if Frm.SchemaModified then
S.Resources.Assign(frm.Schema.Resources);
if Frm.ConnectionsModified then
if MessageDlg(Format(SConnectionsChangedUpdateDispatcher, [LineEnding]), mtInformation, [mbYes, mbNo], 0) = mrYes then
cList.Assign(frm.Connections);
Designer.Modified;
end;
finally
frm.Free;
end;
end;
procedure TSQLDBRestSchemaComponentEditor.ExecuteVerb(Index: Integer);
Const
idxSave = 2;
Var
FN : UTF8String;
S : TSQLDBRestSchema;
begin
S:=Component as TSQLDBRestSchema;
Case Index of
0 : EditSchema(S);
1,2 :
begin
FN:=Component.Name+'.json';
if GetFileName(FN, Format(SJSONFilesFilter, [allFilesMask]), Index=idxSave) then
if idxSave=2 then
S.SaveToFile(FN)
else
begin
S.LoadFromFile(FN);
Designer.Modified;
end;
end;
3 :
begin
DoLoadSchemaFromConnection(S);
Designer.Modified;
end;
4 :
begin
S.Resources.Clear;
Designer.Modified;
end;
end;
end;
function TSQLDBRestSchemaComponentEditor.GetVerb(Index: Integer): string;
begin
Case Index of
0 : Result:=SEditSchema;
1 : Result:=SSaveSchemaToJSONFile;
2 : Result:=SLoadSchemaFromJSONFile;
3 : Result:=SLoadSchemaFromConnection;
4 : Result:=SClearSchema;
end;
end;
function TSQLDBRestSchemaComponentEditor.GetVerbCount: Integer;
begin
Result:=5;
end;
function TSQLDBConnectionTypePropertyEditor.GetAttributes: TPropertyAttributes;
begin
Result := [paMultiSelect, paSortList, paValueList, paRevertable];
end;
procedure TSQLDBConnectionTypePropertyEditor.GetValues(Proc: TGetStrProc);
Var
L : TStringList;
I : Integer;
begin
L:=TStringList.Create;
try
GetConnectionList(L);
for I:=0 to L.Count-1 do
Proc(L[i]);
finally
L.Free;
end;
end;
procedure TSQLDBConnectionTypePropertyEditor.SetValue(const NewValue: ansistring);
var
Comp: TPersistent;
Code: TCodeBuffer;
ConnDef: TConnectionDef;
SrcEdit: TSourceEditorInterface;
begin
if not LazarusIDE.BeginCodeTools then
Exit;
SrcEdit := SourceEditorManagerIntf.ActiveEditor;
if SrcEdit=nil then
Exit;
Code := TCodeBuffer(SrcEdit.CodeToolsBuffer);
if Code = nil then
Exit;
Comp := GetComponent(0);
if Comp is TSQLConnector then
begin
ConnDef := GetConnectionDef(NewValue);
if Assigned(ConnDef) then
CodeToolBoss.AddUnitToMainUsesSection(Code, ConnDef.UnitName, '');
end;
inherited;
end;
{ TFileDescHTMLModule }
constructor TFileRestModule.Create;
begin
inherited Create;
Name:='SQLDB REST Bridge Module';
ResourceClass:=TSQLDBRestModule;
UseCreateFormStatements:=true;
end;
function TFileRestModule.GetInterfaceUsesSection: string;
begin
Result:='SysUtils, Classes ';
if (GetResourceType = rtLRS) then
Result := Result+ ', LResources, ';
Result:=Result+',HTTPDefs, fpHTTP, sqldbrestmodule, sqldbrestbridge';
end;
function TFileRestModule.GetLocalizedName: string;
begin
Result:=SSQLDBRESTModule;
end;
function TFileRestModule.GetLocalizedDescription: string;
begin
Result:=SSQLDBRESTModuleDesc;
end;
function TFileRestModule.GetImplementationSource(const Filename, SourceName, ResourceName: string): string;
begin
Result:=Inherited GetImplementationSource(FileName,SourceName,ResourceName);
if GetResourceType = rtRes then
Result:=Result+'initialization'+LineEnding;
Result:=Result+' T'+ResourceName+'.RegisterModule(''REST'');'+LineEnding;
end;
initialization
{$i lazsqldbrest_images.inc}
end.
|