|
@@ -68,20 +68,49 @@ Type
|
|
|
|
|
|
{ TSQLDBRestDataset }
|
|
{ TSQLDBRestDataset }
|
|
|
|
|
|
|
|
+ { TQueryParam }
|
|
|
|
+
|
|
|
|
+ TQueryParam = class(TParam)
|
|
|
|
+ private
|
|
|
|
+ FEnabled: Boolean;
|
|
|
|
+ Public
|
|
|
|
+ Procedure Assign(Source : TPersistent); override;
|
|
|
|
+ function AsQuery : String;
|
|
|
|
+ Published
|
|
|
|
+ Property Enabled : Boolean Read FEnabled Write FEnabled;
|
|
|
|
+ end;
|
|
|
|
+
|
|
|
|
+ { TQueryParams }
|
|
|
|
+
|
|
|
|
+ TQueryParams = Class(TParams)
|
|
|
|
+ private
|
|
|
|
+ function GetP(aIndex : Integer): TQueryParam;
|
|
|
|
+ procedure SetP(aIndex : Integer; AValue: TQueryParam);
|
|
|
|
+ Public
|
|
|
|
+ Property Params[aIndex : Integer] : TQueryParam Read GetP Write SetP; default;
|
|
|
|
+ end;
|
|
|
|
+
|
|
|
|
+ TGetQueryParamsEvent = Procedure (Sender : TDataset; IsReadURL : Boolean; var QueryString : String) of object;
|
|
|
|
+
|
|
TSQLDBRestDataset = Class(TJSONDataset)
|
|
TSQLDBRestDataset = Class(TJSONDataset)
|
|
private
|
|
private
|
|
FConnection: TSQLDBRestConnection;
|
|
FConnection: TSQLDBRestConnection;
|
|
FDatabaseConnection: String;
|
|
FDatabaseConnection: String;
|
|
|
|
+ FOnGetQueryParams: TGetQueryParamsEvent;
|
|
|
|
+ FParams: TQueryParams;
|
|
FResourceName: String;
|
|
FResourceName: String;
|
|
FSQL: TStrings;
|
|
FSQL: TStrings;
|
|
function CleanSQL: String;
|
|
function CleanSQL: String;
|
|
function CustomViewResourceName: String;
|
|
function CustomViewResourceName: String;
|
|
procedure DoSQLChange(Sender: TObject);
|
|
procedure DoSQLChange(Sender: TObject);
|
|
- function MyURL: String;
|
|
|
|
procedure SetConnection(AValue: TSQLDBRestConnection);
|
|
procedure SetConnection(AValue: TSQLDBRestConnection);
|
|
|
|
+ procedure SetParams(AValue: TQueryParams);
|
|
procedure SetResourceName(AValue: String);
|
|
procedure SetResourceName(AValue: String);
|
|
procedure SetSQL(AValue: TStrings);
|
|
procedure SetSQL(AValue: TStrings);
|
|
Protected
|
|
Protected
|
|
|
|
+ function MyURL(isRead : Boolean): String; virtual;
|
|
|
|
+ Function CreateQueryParams : TQueryParams; virtual;
|
|
|
|
+ function GetURLQueryParams(IsRead : Boolean): string; virtual;
|
|
function DataPacketReceived(ARequest: TDataRequest): Boolean; override;
|
|
function DataPacketReceived(ARequest: TDataRequest): Boolean; override;
|
|
function GetStringFieldLength(F: TJSObject; AName: String; AIndex: Integer): integer;virtual;
|
|
function GetStringFieldLength(F: TJSObject; AName: String; AIndex: Integer): integer;virtual;
|
|
function StringToFieldType(S: String): TFieldType; virtual;
|
|
function StringToFieldType(S: String): TFieldType; virtual;
|
|
@@ -91,11 +120,14 @@ Type
|
|
Constructor Create(aOwner : TComponent); override;
|
|
Constructor Create(aOwner : TComponent); override;
|
|
Destructor Destroy; override;
|
|
Destructor Destroy; override;
|
|
Class Function DefaultBlobDataToBytes(aValue : JSValue) : TBytes; override;
|
|
Class Function DefaultBlobDataToBytes(aValue : JSValue) : TBytes; override;
|
|
|
|
+ Function ParamByName(const aName : String) : TQueryParam;
|
|
Published
|
|
Published
|
|
Property Connection: TSQLDBRestConnection Read FConnection Write SetConnection;
|
|
Property Connection: TSQLDBRestConnection Read FConnection Write SetConnection;
|
|
Property ResourceName : String Read FResourceName Write SetResourceName;
|
|
Property ResourceName : String Read FResourceName Write SetResourceName;
|
|
Property SQL : TStrings Read FSQL Write SetSQL;
|
|
Property SQL : TStrings Read FSQL Write SetSQL;
|
|
property DatabaseConnection : String Read FDatabaseConnection Write FDatabaseConnection;
|
|
property DatabaseConnection : String Read FDatabaseConnection Write FDatabaseConnection;
|
|
|
|
+ Property Params : TQueryParams Read FParams Write SetParams;
|
|
|
|
+ Property OnGetQueryParams : TGetQueryParamsEvent Read FOnGetQueryParams Write FOnGetQueryParams;
|
|
end;
|
|
end;
|
|
|
|
|
|
implementation
|
|
implementation
|
|
@@ -122,6 +154,40 @@ Type
|
|
Property StatusCode : Integer Read GetStatusCode;
|
|
Property StatusCode : Integer Read GetStatusCode;
|
|
end;
|
|
end;
|
|
|
|
|
|
|
|
+{ TQueryParam }
|
|
|
|
+
|
|
|
|
+procedure TQueryParam.Assign(Source: TPersistent);
|
|
|
|
+
|
|
|
|
+Var
|
|
|
|
+ P : TQueryParam absolute Source;
|
|
|
|
+
|
|
|
|
+begin
|
|
|
|
+ if Source is TQueryParam then
|
|
|
|
+ begin
|
|
|
|
+ FEnabled:=P.Enabled;
|
|
|
|
+ end;
|
|
|
|
+ inherited Assign(Source);
|
|
|
|
+end;
|
|
|
|
+
|
|
|
|
+function TQueryParam.AsQuery: String;
|
|
|
|
+begin
|
|
|
|
+ Result:='';
|
|
|
|
+ if Enabled then
|
|
|
|
+ Result:=Name+'='+encodeURIComponent(AsString);
|
|
|
|
+end;
|
|
|
|
+
|
|
|
|
+{ TQueryParams }
|
|
|
|
+
|
|
|
|
+function TQueryParams.GetP(aIndex : Integer): TQueryParam;
|
|
|
|
+begin
|
|
|
|
+ Result:=Items[aIndex] as TQueryParam
|
|
|
|
+end;
|
|
|
|
+
|
|
|
|
+procedure TQueryParams.SetP(aIndex : Integer; AValue: TQueryParam);
|
|
|
|
+begin
|
|
|
|
+ Items[aIndex]:=aValue;
|
|
|
|
+end;
|
|
|
|
+
|
|
{ TServiceRequest }
|
|
{ TServiceRequest }
|
|
|
|
|
|
constructor TServiceRequest.Create(const aMethod,aURL, aUserName, aPassword: String; aOnDone1 : TNotifyEvent; aOnDone2 : TNotifyEvent = Nil);
|
|
constructor TServiceRequest.Create(const aMethod,aURL, aUserName, aPassword: String; aOnDone1 : TNotifyEvent; aOnDone2 : TNotifyEvent = Nil);
|
|
@@ -153,7 +219,7 @@ end;
|
|
function TServiceRequest.GetResultJSON: TJSObject;
|
|
function TServiceRequest.GetResultJSON: TJSObject;
|
|
begin
|
|
begin
|
|
if SameText(FXHR.getResponseHeader('Content-Type'),'application/json') then
|
|
if SameText(FXHR.getResponseHeader('Content-Type'),'application/json') then
|
|
- Result:=TJSJSON.parseObject(GetResult)
|
|
|
|
|
|
+ Result:=TJSJSON.parseObject(RequestResult)
|
|
else
|
|
else
|
|
Result:=nil;
|
|
Result:=nil;
|
|
end;
|
|
end;
|
|
@@ -167,6 +233,7 @@ function TServiceRequest.onLoad(Event: TEventListenerEvent): boolean;
|
|
begin
|
|
begin
|
|
if Assigned(FOnMyDone) then
|
|
if Assigned(FOnMyDone) then
|
|
FOnMyDone(Self);
|
|
FOnMyDone(Self);
|
|
|
|
+ Result:=False;
|
|
end;
|
|
end;
|
|
|
|
|
|
|
|
|
|
@@ -194,9 +261,13 @@ begin
|
|
end;
|
|
end;
|
|
|
|
|
|
function TSQLDBRestConnection.GetUpdateBaseURL(aRequest: TRecordUpdateDescriptor): String;
|
|
function TSQLDBRestConnection.GetUpdateBaseURL(aRequest: TRecordUpdateDescriptor): String;
|
|
|
|
+
|
|
|
|
+Var
|
|
|
|
+ DS : TSQLDBRestDataset;
|
|
begin
|
|
begin
|
|
Result:=inherited GetUpdateBaseURL(aRequest);
|
|
Result:=inherited GetUpdateBaseURL(aRequest);
|
|
- Result:=IncludeTrailingPathDelimiter(Result)+TSQLDBRestDataset(aRequest.Dataset).ResourceName;
|
|
|
|
|
|
+ DS:=TSQLDBRestDataset(aRequest.Dataset);
|
|
|
|
+ Result:=IncludeTrailingPathDelimiter(Result)+DS.MyURL(False);
|
|
end;
|
|
end;
|
|
|
|
|
|
function TSQLDBRestConnection.GetReadBaseURL(aRequest: TDataRequest): String;
|
|
function TSQLDBRestConnection.GetReadBaseURL(aRequest: TDataRequest): String;
|
|
@@ -207,7 +278,7 @@ Var
|
|
begin
|
|
begin
|
|
Result:=inherited GetReadBaseURL(aRequest);
|
|
Result:=inherited GetReadBaseURL(aRequest);
|
|
DS:=TSQLDBRestDataset(aRequest.Dataset);
|
|
DS:=TSQLDBRestDataset(aRequest.Dataset);
|
|
- Result:=IncludeTrailingPathDelimiter(Result)+DS.MyURL;
|
|
|
|
|
|
+ Result:=IncludeTrailingPathDelimiter(Result)+DS.MyURL(True);
|
|
end;
|
|
end;
|
|
|
|
|
|
procedure TSQLDBRestConnection.DoResources(Sender: TObject);
|
|
procedure TSQLDBRestConnection.DoResources(Sender: TObject);
|
|
@@ -283,15 +354,53 @@ begin
|
|
FConnection.FreeNotification(Self);
|
|
FConnection.FreeNotification(Self);
|
|
end;
|
|
end;
|
|
|
|
|
|
-function TSQLDBRestDataset.MyURL: String;
|
|
|
|
|
|
+procedure TSQLDBRestDataset.SetParams(AValue: TQueryParams);
|
|
|
|
+begin
|
|
|
|
+ if FParams=AValue then Exit;
|
|
|
|
+ FParams.Assign(AValue);
|
|
|
|
+end;
|
|
|
|
+
|
|
|
|
+function TSQLDBRestDataset.GetURLQueryParams(IsRead :Boolean) : string;
|
|
|
|
+
|
|
|
|
+ Procedure AddToResult(aQuery : string);
|
|
|
|
+
|
|
|
|
+ begin
|
|
|
|
+ if aQuery='' then
|
|
|
|
+ exit;
|
|
|
|
+ If Result<>'' then
|
|
|
|
+ Result:=Result+'&';
|
|
|
|
+ Result:=Result+aQuery;
|
|
|
|
+ end;
|
|
|
|
+
|
|
|
|
+Var
|
|
|
|
+ I : Integer;
|
|
|
|
+
|
|
|
|
+begin
|
|
|
|
+ Result:='';
|
|
|
|
+ if IsRead then
|
|
|
|
+ begin
|
|
|
|
+ if SameText(ResourceName,CustomViewResourceName) then
|
|
|
|
+ AddToResult('SQL'+EncodeURIComponent(CleanSQL));
|
|
|
|
+ For I:=0 to Params.Count-1 do
|
|
|
|
+ AddToResult(Params[I].AsQuery);
|
|
|
|
+ end;
|
|
|
|
+ if Assigned(FOnGetQueryParams) then
|
|
|
|
+ FOnGetQueryParams(Self,IsRead,Result);
|
|
|
|
+end;
|
|
|
|
+
|
|
|
|
+function TSQLDBRestDataset.MyURL(isRead: Boolean): String;
|
|
|
|
+
|
|
|
|
+Var
|
|
|
|
+ Qry : String;
|
|
|
|
|
|
begin
|
|
begin
|
|
Result:=DatabaseConnection;
|
|
Result:=DatabaseConnection;
|
|
if (Result<>'') and (Result[Length(Result)]<>'/') then
|
|
if (Result<>'') and (Result[Length(Result)]<>'/') then
|
|
Result:=Result+'/';
|
|
Result:=Result+'/';
|
|
Result:=Result+ResourceName;
|
|
Result:=Result+ResourceName;
|
|
- if SameText(ResourceName,CustomViewResourceName) then
|
|
|
|
- Result:=Result+'?SQL='+ EncodeURIComponent(CleanSQL);
|
|
|
|
|
|
+ Qry:=GetURLQueryParams(IsRead);
|
|
|
|
+ if Qry<>'' then
|
|
|
|
+ Result:=Result+'?'+Qry;
|
|
end;
|
|
end;
|
|
|
|
|
|
procedure TSQLDBRestDataset.DoSQLChange(Sender: TObject);
|
|
procedure TSQLDBRestDataset.DoSQLChange(Sender: TObject);
|
|
@@ -332,6 +441,11 @@ begin
|
|
FSQL.Assign(AValue);
|
|
FSQL.Assign(AValue);
|
|
end;
|
|
end;
|
|
|
|
|
|
|
|
+function TSQLDBRestDataset.CreateQueryParams: TQueryParams;
|
|
|
|
+begin
|
|
|
|
+ Result:=TQueryParams.Create(Self,TQueryParam);
|
|
|
|
+end;
|
|
|
|
+
|
|
|
|
|
|
|
|
|
|
class function TSQLDBRestDataset.DefaultBlobDataToBytes(aValue: JSValue): TBytes;
|
|
class function TSQLDBRestDataset.DefaultBlobDataToBytes(aValue: JSValue): TBytes;
|
|
@@ -339,6 +453,11 @@ begin
|
|
Result:=BytesOf(Window.atob(String(aValue)));
|
|
Result:=BytesOf(Window.atob(String(aValue)));
|
|
end;
|
|
end;
|
|
|
|
|
|
|
|
+function TSQLDBRestDataset.ParamByName(const aName: String): TQueryParam;
|
|
|
|
+begin
|
|
|
|
+ Result:=TQueryParam(Params.ParamByName(aName));
|
|
|
|
+end;
|
|
|
|
+
|
|
function TSQLDBRestDataset.DoGetDataProxy: TDataProxy;
|
|
function TSQLDBRestDataset.DoGetDataProxy: TDataProxy;
|
|
begin
|
|
begin
|
|
Result:=Connection.DataProxy;
|
|
Result:=Connection.DataProxy;
|
|
@@ -453,11 +572,13 @@ begin
|
|
inherited Create(aOwner);
|
|
inherited Create(aOwner);
|
|
FSQL:=TStringList.Create;
|
|
FSQL:=TStringList.Create;
|
|
TStringList(FSQL).OnChange:=@DoSQLChange;
|
|
TStringList(FSQL).OnChange:=@DoSQLChange;
|
|
|
|
+ FParams:=CreateQueryParams;
|
|
end;
|
|
end;
|
|
|
|
|
|
destructor TSQLDBRestDataset.Destroy;
|
|
destructor TSQLDBRestDataset.Destroy;
|
|
begin
|
|
begin
|
|
FreeAndNil(FSQL);
|
|
FreeAndNil(FSQL);
|
|
|
|
+ FreeAndnil(FParams);
|
|
inherited Destroy;
|
|
inherited Destroy;
|
|
end;
|
|
end;
|
|
|
|
|