unit ServerMethodsUnit1;

interface

uses System.SysUtils,Messages,Vcl.Dialogs, System.Classes, System.Json,vcl.Graphics,
    Datasnap.DSServer, Datasnap.DSAuth, DataSnap.DSProviderDataModuleAdapter, Data.DBXMySQL,
  Datasnap.Provider, Data.DB, Datasnap.DBClient, SimpleDS, Data.SqlExpr;

type
  TServerMethods1 = class(TDSServerModule)
    SQLConnection1: TSQLConnection;
    SimpleDataSet1: TSimpleDataSet;
    DataSetProvider1: TDataSetProvider;
    procedure DataSetProvider1GetTableName(Sender: TObject; DataSet: TDataSet;
      var TableName: string);
    procedure DSServerModuleCreate(Sender: TObject);
    procedure DataSetProvider1BeforeGetRecords(Sender: TObject; var OwnerData: OleVariant);
    procedure DataSetProvider1AfterGetRecords(Sender: TObject; var OwnerData: OleVariant);
  private
    { Private declarations }
  public
    { Public declarations }
    function EchoString(Value: string): string;
    function ReverseString(Value: string): string;
    procedure GetPicture(filename:TStream);
    procedure GetString(s:widestring);
  end;

implementation


{$R *.dfm}


uses System.StrUtils;

procedure TServerMethods1.DataSetProvider1AfterGetRecords(Sender: TObject;
  var OwnerData: OleVariant);
begin
simpledataset1.Open;
end;

procedure TServerMethods1.DataSetProvider1BeforeGetRecords(Sender: TObject;
  var OwnerData: OleVariant);
begin
simpledataset1.Close;
end;

procedure TServerMethods1.DataSetProvider1GetTableName(Sender: TObject; DataSet: TDataSet;
  var TableName: string);
begin
TableName:='Pictures';
end;

procedure TServerMethods1.DSServerModuleCreate(Sender: TObject);
begin
simpledataset1.Open;
end;

function TServerMethods1.EchoString(Value: string): string;
begin
  Result := Value;
end;

procedure TServerMethods1.GetPicture(filename: TStream);
var ms:tmemorystream; bmp:tbitmap;
begin
  bmp:=tbitmap.Create;
  ms:=tmemorystream.Create;
  ms.LoadFromStream(filename);

  bmp.LoadFromStream(filename);

  if ms<>nil then showmessage('not empty on server');


  simpledataset1.Insert;
  simpledataset1.FieldByName('Primary_key').AsInteger:=simpledataset1.RecordCount+1;
  Tblobfield(simpledataset1.FieldByName('Picture')).Assign(bmp);
//  simpledataset1.Post;
  simpledataset1.ApplyUpdates(-1);

end;

procedure TServerMethods1.GetString(s: widestring);
var ss:Tstringstream; ms:tmemorystream; bmp:tbitmap;
begin
//ss:=tstringstream.Create(s);
//bmp:=tbitmap.Create;

//ms.LoadFromStream(ss);
//bmp.LoadFromStream(ms);

showmessage(inttostr(length(s)));

simpledataset1.Insert;
simpledataset1.FieldByName('Primary_key').AsInteger:=simpledataset1.RecordCount+1;
TBlobfield(simpledataset1.FieldByName('Picture')).AsString:=s;
simpledataset1.Post;
simpledataset1.ApplyUpdates(-1);




end;

function TServerMethods1.ReverseString(Value: string): string;
begin
  Result := System.StrUtils.ReverseString(Value);
end;

end.

