unit ServerMethodsUnit1;

interface

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

type



  TServerMethods1 = class(TDSServerModule)
    SQLConnection1: TSQLConnection;
    SimpleDataSet1: TSimpleDataSet;
    DataSetProvider1: TDataSetProvider;
  private
    { Private declarations }
  public
    { Public declarations }

    procedure ZeroGlobalBuffer;
    procedure GlobalBufferToMySQL;
    procedure GetstreamByPackets(stream:tmemorystream; packetsize:int64);

    procedure ApplyUpdates;
    procedure Hello (s:string);
    procedure DataInsert(V:Olevariant);
    procedure ZeroGlobalString;
    procedure BuildGlobalString(V:Olevariant);
    procedure GlobalStringToMySQL;
    procedure GetBytes(s:string);

  end;


  var Globalbuffer:TBytes; //Массив байт для получения файлов, изображений...
      Globalstring:string;

implementation


{$R *.dfm}


uses System.StrUtils;

procedure TServerMethods1.ApplyUpdates;
begin
  simpledataset1.ApplyUpdates(-1);
end;



function UnPackVarArray(V : Variant) : AnsiString;
var pData : PByteArray;
    S : String;
begin
  if V = NULL then begin
    Result := ''; Exit;
  end;
  pData := VarArrayLock(V);
  try
    SetLength(S, VarArrayHighBound(V, 1)+1);
    Move(pData^, S[1], Length(S));
  finally
    VarArrayUnlock(V);
  end;
  Result := S;
end;


function OleVariantToString(const Value: OleVariant): string;
  var ss: TStringStream;
      Size: integer;
      Data: PByteArray;
begin
  Result:='';
  if Length(Value) = 0 then Exit;
  ss:=TStringStream.Create;
  try
    Size := VarArrayHighBound (Value, 1) - VarArrayLowBound(Value, 1) + 1;
    Data := VarArrayLock(Value);
    try
      ss.Position := 0;
      ss.WriteBuffer(Data^, Size);
      ss.Position := 0;
      Result:=ss.DataString;
    finally
      VarArrayUnlock(Value);
    end;
  finally
    ss.Free;
  end;
end;





procedure TServerMethods1.DataInsert(V: Olevariant);

var s:string; ss:tstringstream;
begin


s:=OleVariantToString(V);


ss:=tstringstream.Create(s);
ss.Position:=0;



   simpledataset1.insert;
   simpledataset1.fieldbyname('Primary_key').AsInteger:=simpledataset1.RecordCount+1;
   tblobfield( simpledataset1.fieldbyname('Picture')).LoadFromStream(ss);
   simpledataset1.post;
   simpledataset1.applyUpdates(-1);



end;

procedure TServerMethods1.BuildGlobalString(V: Olevariant);
var s:string;
begin
s:=OleVariantToString(V);
Globalstring:=Globalstring+s;
s:='';
end;

procedure TServerMethods1.GetBytes(s:string);


var ms:tmemorystream;
begin
 ms:=tmemorystream.Create;
{ ms.Write(bytes,length(bytes));

   simpledataset1.insert;
   simpledataset1.fieldbyname('Primary_key').AsInteger:=simpledataset1.RecordCount+1;
   tblobfield( simpledataset1.fieldbyname('Picture')).LoadFromStream(ms);
   simpledataset1.post;
   simpledataset1.applyUpdates(-1);

   ms.Free;
}
end;

procedure TServerMethods1.GetstreamByPackets(stream: tmemorystream;PacketSize:int64);
var ms:tmemorystream;  bmp:tbitmap; bufferstream:tmemorystream;
    localbuffer:tbytes;

begin

//showmessage('Поток на сервере ' +inttostr(stream.Size));
//if stream.Size=0 then exit;


     ms:=tmemorystream.Create;
     ms.LoadFromStream(stream);
     ms.Position:=0;


     setlength(localbuffer,PacketSize) ;
     ms.ReadBuffer(localbuffer,PacketSize);

     globalbuffer:=globalbuffer+localbuffer;


     setlength(localbuffer,0) ;
     ms.Free;

  //   stream.Clear;
 //    stream.Position:=0;
 //    stream:=tmemorystream.Create;

end;


procedure TServerMethods1.GlobalBufferToMySQL;
var ms:tmemorystream;
begin
  if globalbuffer=nil then exit;  // Вышли, если нет данных

  ms:=tmemorystream.Create;
  ms.WriteBuffer(GlobalBuffer,length(Globalbuffer));

   simpledataset1.insert;
   simpledataset1.fieldbyname('Primary_key').AsInteger:=simpledataset1.RecordCount+1;
   tblobfield( simpledataset1.fieldbyname('Picture')).LoadFromStream(ms);
   simpledataset1.post;
   simpledataset1.applyUpdates(-1);

   ms.Free;
end;



procedure TServerMethods1.GlobalStringToMySQL;
var ss:tstringstream;
begin
if GlobalString='' then exit;

ss:=tstringstream.Create(GlobalString);


// Запись в СУБД
   simpledataset1.insert;
   simpledataset1.fieldbyname('Primary_key').AsInteger:=simpledataset1.RecordCount+1;
   tblobfield( simpledataset1.fieldbyname('Picture')).LoadFromStream(ss);
   simpledataset1.post;
   simpledataset1.applyUpdates(-1);

Globalstring:='';

end;

procedure TServerMethods1.Hello(s:string);
var ss:tstringstream; ms:tmemorystream;
begin

   ss:=tstringstream.create(s);
//  showmessage('Строка на сервере '+inttostr(length(ss.DataString)));

   ms:=tmemorystream.create;

   ms.LoadFromStream(ss);


   simpledataset1.insert;
   simpledataset1.fieldbyname('Primary_key').AsInteger:=simpledataset1.RecordCount+1;
   tblobfield( simpledataset1.fieldbyname('Picture')).LoadFromStream(ms);
   simpledataset1.post;
   simpledataset1.applyUpdates(-1);

 //  if ms.Size>0 then
  // showmessage('hello');


end;

procedure TServerMethods1.ZeroGlobalBuffer;
begin
 if GlobalBuffer<>nil then GlobalBuffer:=nil;
 Setlength(Globalbuffer,0);
end;

procedure TServerMethods1.ZeroGlobalString;
begin
Globalstring:='';
end;

end.

