Dynamic marshaling from sql

Home Forums kbmMW Dynamic marshaling from sql

Viewing 2 reply threads
  • Author
    Posts
    • #55803
      Ismet Sonmez
      Participant

      Hi,

      I tested this code, work good.For dynamic object creation and marshaling.

      Is that true way or is there any other way?

      unit MainObject;

      interface
      uses SysUtils, Generics.Collections, Rtti,
      kbmMemTable,
      kbmMWRTTI,
      kbmMWObjectMarshal,
      kbmMWJSONMarshal,
      kbmMWXMLMarshal,
      kbmMWYAMLMarshal,
      MainDbModule;

      type

      TBaseObjectClass = class of TBaseObject;
      TBaseObject = class
      end;

      [kbmMW_Root(‘Result’,[mwrfIncludeOnlyTagged])]
      TResult = class(TBaseObject)
      private
      FCode : integer;
      FDesc : string;
      public
      [kbmMW_Attribute(‘Code’)]
      property Code : integer read FCode write FCode;
      [kbmMW_Attribute(‘Desc’)]
      property Desc : string read FDesc write FDesc;
      end;

      [kbmMW_Root(‘SimpleObject’,[mwrfIncludeOnlyTagged])]
      TSimpleObject = class(TBaseObject)
      private
      FObjId: Int64;
      FCode : string;
      FDesc : string;
      public
      [kbmMW_Attribute(‘ObjId’)]
      property ObjId : Int64 read FObjId write FObjId;
      [kbmMW_Attribute(‘Code’)]
      property Code : string read FCode write FCode;
      [kbmMW_Attribute(‘Desc’)]
      property Desc : string read FDesc write FDesc;
      end;

      [kbmMW_Root(‘SimpleObjectResult’,[mwrfIncludeOnlyTagged])]
      TSimpleObjectResult = class(TResult)
      private
      FData : TSimpleObject;
      public
      destructor Destroy(); override;
      [kbmMW_Attribute(‘Data’)]
      property Data : TSimpleObject read FData write FData;
      end;

      function ObjectMarshal(AClass:TBaseObject; AType: Integer=1; AAnonymousRoot: Boolean=False; AModifiedStatus: Boolean=False; AJQueryEscapes: Boolean=False; AAlignChildren: Boolean=False): string;
      function ObjectUnMarshal(ABaseObjectClass:TBaseObjectClass; AObjectString: string; AType: Integer=1; AAnonymousRoot: Boolean=False; AModifiedStatus: Boolean=False; AJQueryEscapes: Boolean=False):TBaseObject;
      function GetSimpleObjectBySql(const ASql: string):TSimpleObject;
      function GetSimpleObjectResultBySql(const ASql: string):TSimpleObjectResult;

      implementation

      function ObjectMarshal(AClass:TBaseObject; AType: Integer=1; AAnonymousRoot: Boolean=False; AModifiedStatus: Boolean=False; AJQueryEscapes: Boolean=False; AAlignChildren: Boolean=False): string;
      var
      m:TkbmMWCustomRTTIMarshal;
      begin
      Result := ”;
      case AType of
      1 : begin // JSON
      m := TkbmMWJSONMarshal.Create;
      try
      TkbmMWJSONMarshal(m).Typed := false;
      TkbmMWJSONMarshal(m).AutoIndent := true;
      TkbmMWJSONMarshal(m).AutoLineFeed := true;
      TkbmMWJSONMarshal(m).JQueryEscaping := AJQueryEscapes;
      TkbmMWJSONMarshal(m).AnonymousRoot := AAnonymousRoot;
      m.IncludeModifiedStatus := AModifiedStatus;
      m.ExceptOnUnknownType := true;
      Result := TkbmMWJSONMarshal(m).ValueToString(AClass);
      finally
      m.Free;
      end;
      end;
      2 : begin // XML
      m := TkbmMWXMLMarshal.Create;
      try
      TkbmMWXMLMarshal(m).Typed := false;
      TkbmMWXMLMarshal(m).AutoIndent := true;
      TkbmMWXMLMarshal(m).AutoLineFeed := true;
      TkbmMWXMLMarshal(m).PreserveWhiteSpace := false;
      m.IncludeModifiedStatus := AModifiedStatus;
      m.ExceptOnUnknownType := true;
      Result := TkbmMWXMLMarshal(m).ValueToString(AClass);
      finally
      m.Free;
      end;
      end;
      3 : begin // YAML
      m := TkbmMWYAMLMarshal.Create;
      try
      TkbmMWYAMLMarshal(m).Typed := false;
      TkbmMWYAMLMarshal(m).Indentation := 2;
      TkbmMWYAMLMarshal(m).AnonymousRoot := AAnonymousRoot;
      TkbmMWYAMLMarshal(m).AlignChildren := AAlignChildren;
      m.IncludeModifiedStatus := AModifiedStatus;
      m.ExceptOnUnknownType := true;
      Result := TkbmMWYAMLMarshal(m).ValueToString(AClass);
      finally
      m.Free;
      end;
      end;
      end;
      end;

      function ObjectUnMarshal(ABaseObjectClass:TBaseObjectClass; AObjectString: string; AType: Integer=1; AAnonymousRoot: Boolean=False; AModifiedStatus: Boolean=False; AJQueryEscapes: Boolean=False):TBaseObject;
      var
      m:TkbmMWCustomRTTIMarshal;
      begin
      Result := nil;
      case AType of
      1 : begin // JSON
      m:=TkbmMWJSONMarshal.Create;
      try
      m.ExceptOnUnknownType := True;
      m.IncludeModifiedStatus := AModifiedStatus;
      TkbmMWJSONMarshal(m).JQueryEscaping := AJQueryEscapes;
      TkbmMWJSONMarshal(m).AnonymousRoot := AAnonymousRoot;
      Result := TBaseObject(TkbmMWJSONMarshal(m).ValueFromString(ABaseObjectClass,AObjectString));
      finally
      m.Free;
      end;
      end;
      2 : begin // XML
      m:=TkbmMWXMLMarshal.Create;
      try
      m.ExceptOnUnknownType := True;
      m.IncludeModifiedStatus := AModifiedStatus;
      Result := TBaseObject(TkbmMWXMLMarshal(m).ValueFromString(ABaseObjectClass,AObjectString));
      finally
      m.Free;
      end;
      end;
      3 : begin // YAML
      m:=TkbmMWYAMLMarshal.Create;
      try
      m.ExceptOnUnknownType:=true;
      m.IncludeModifiedStatus := AModifiedStatus;
      Result := TBaseObject(TkbmMWYAMLMarshal(m).ValueFromString(ABaseObjectClass,AObjectString));
      finally
      m.Free;
      end;
      end;
      end;
      end;

      function GetSimpleObjectBySql(const ASql: string):TSimpleObject;
      var
      mtTemp:TKbmMemTable;
      begin
      Result:=TSimpleObject.Create();
      Result.ObjId := -1;
      Result.Code := ”;
      Result.Desc := ”;
      mtTemp := TKbmMemTable.Create(nil);
      try
      ModuleMainDB.LoadQuery(mtTemp, ASql);
      if (mtTemp.Active) and (mtTemp.RecordCount = 1) then
      begin
      Result.ObjId := mtTemp.FieldByName(‘OBJID’).AsInteger;
      Result.Code := mtTemp.FieldByName(‘CODE’).AsString;
      Result.Desc := mtTemp.FieldByName(‘DESCRIPTION’).AsString;
      end;
      mtTemp.Close;
      finally
      mtTemp.Free;
      end;
      end;

      function GetSimpleObjectResultBySql(const ASql: string):TSimpleObjectResult;
      begin
      Result := TSimpleObjectResult.Create();
      Result.Code := 1001 ;
      Result.Desc := ‘NA’ ;
      try
      Result.Data := GetSimpleObjectBySql(ASql);
      if Result.Data.ObjId = -1 then
      begin
      Result.Code := 1003;
      Result.Desc := ‘Kayıt bulunamadı’;
      FreeAndNil(Result.Data);
      end else
      begin
      Result.Code := 1000;
      Result.Desc := ‘SUCC’;
      end;
      except
      On E :Exception do begin
      Result.Code := 1002;
      Result.Desc := ‘Kod bloğunda hata oluştu’; // E.Message;
      end;
      end;
      end;

      destructor TSimpleObjectResult.Destroy;
      begin
      if FData<>nil then
      FData.Free;
      inherited;
      end;

      initialization

      kbmMWRegisterKnownTypes([
      typeinfo(TBaseObject),
      typeinfo(TResult),
      typeinfo(TSimpleObject),
      typeinfo(TSimpleObjectResult)
      ]);
      end.

       

       

       

    • #55804
      Ismet Sonmez
      Participant

      test code;

      procedure TFormMain.Button1Click(Sender: TObject);
      var
      o:TSimpleObjectResult;
      s:TSimpleObjectResult;
      str:string;
      begin
      s := GetSimpleObjectResultBySql(‘SELECT * FROM SYSFIRM’);

      str := ObjectMarshal(S,1,True);
      showmessage(str);

      o:=TSimpleObjectResult(ObjectUnMarshal(TSimpleObjectResult,str,1,True));
      showmessage(o.Data.Desc);
      end;

    • #55838
      kimbomadsen
      Keymaster

      Hi,

      Well there are many ways to enter data into objects.

      Marshalling is one of them. The rulebook for marshalling is usually a Delphi class as you have done it.
      The Delphi class can be handcoded as you did, or autogenerated from an XSD if you prefer that method. kbmMW supports both.

      If your data comes from a SQL statement, you could use kbmMW’s ORM to access the data, then you will automatically have Delphi objects rather than a dataset, and you will not need to do any manual copying of data from the SQL dataset to new object instances.

      Further you could choose to use kbmMW’s SmartService REST features, then you also do not need to do any marshalling yourself. It will automatically be done for you, both for data provided to the REST function, and the resulting data returned from the REST function.

      Check the blog series REST easy which explains that in quite some detail.
      /Kim

Viewing 2 reply threads
  • You must be logged in to reply to this topic.