2023년 10월 15일 일요일

How to dramatically reduce code conversion work when moving from Tier 2 to Tier 3.

 When converting a 2-tier project to 3-tier, you often have to move the SQL queries used in the 2-tier app to the 3-tier middleware server, which increases the workload and makes the project on the server huge.

In this case, there is a way to send the SQL statement itself as a String parameter without moving the query statement used in the second-tier project to the server.

In this case, the middleware server receives the sql string, processes it, and makes the result visible to the client app.

Depending on the SQL query statement, the column names retrieved from the database or the field names replaced with AS in the SQL query statement are all different, so you can output the field names like below.

       for i := 0 to FDQueryI.FieldCount - 1 do

           JsubObj.AddPair( FDQueryI.Fields[ i ].FullName, FDQueryI.Fields[ i ].AsString );

In some cases, you may know the field names of the queried results in the database, but FireDAC can show the field names of the queried results in the same way as the sample source, so you can use it.

The sample source outputs the result as Json, so you can utilize Rad server or Datasnap Rest method.


function TRTestResource1.QueryText_sql( sqlText : string ) : String;

var

  JTopObj, JsubObj : TJSONObject;

  JArr  : TJSONArray;

  JPair : TJSONPair;

  i : integer;


begin

  JTopObj := TJSONObject.Create;  


  try

    FDConnection1.Open;


    try

      FDQueryI.Close;

      FDQueryI.SQL.Clear;

      FDQueryI.SQL.Add( sqlText );


      FDQueryI.Open;

      FDQueryI.First;


      JArr :=  TJSONArray.Create;   

      while Not FDQueryI.EOF do

      begin

        JsubObj := TJSONObject.Create; 


        for i := 0 to FDQueryI.FieldCount - 1 do

           JsubObj.AddPair( FDQueryI.Fields[ i ].FullName,  FDQueryI.Fields[ i ].AsString );  // 조회 결과값의 필드명과 데이터를 같이 출력 하는 방법


        JArr.AddElement( JsubObj ); 

        FDQueryI.Next;

      end;


      JPair := TJSONPair.Create( 'Items', JArr );                           

      JTopObj.AddPair( 'Count', TJSONNumber.Create( FDQueryI.RecordCount ) );  

      JTopObj.AddPair( JPair );                                                


    except

      on e: Exception do begin

            result := 'Error';

            Exit;

      end;

    end;



  finally

      FDConnection1.Close;


      result := JTopObj.ToString;   // 결과값 전달.

      JTopObj.Free;

  end;

end;

2023년 7월 12일 수요일

FireMonkey TLine Demo

This video is created by connecting two planets orbiting the sun with different orbital periods with a line.

You can visually see the distance between two destination lines by the length of the line.



The length between the two points was calculated in the following way.

procedure TMForm.Draw_Line2P( x1,y1, x2,y2 : single;  setColor : cardinal );

var

  d, xtemp, ytemp, rAngle : single;

  drawLine : TLine;

begin

  if x1 > x2 then

  begin

    xtemp := x1;     ytemp := y1;

    x1 := x2;          y1 := y2;

    x2 := xtemp;     y2 := ytemp;

  end;


  d := SQRT( Power( x2-x1, 2 ) + Power( y2-y1, 2 ) );   // Uses System.Math

  rAngle := RadToDeg( ArcSin( (y2-y1)/d ));


  drawLine := TLine.Create( BLayout );

  drawLine.Parent := BLayout;

  drawLine.LineLocation := TLineLocation.Inner;

  drawLine.LineType := TLineType.Bottom;

  drawLine.RotationCenter.X := 0;

  drawLine.RotationCenter.Y := 0;

  drawLine.Stroke.Thickness := 1;

  drawLine.Stroke.Color := setColor;

  drawLine.Height := 1;

  drawLine.Width  := d;

  drawLine.Position.X := x1;

  drawLine.Position.Y := y1;

  drawLine.RotationAngle := rAngle;

end;

2023년 5월 3일 수요일