DIOCP open source project -DEMO (how to operate the remote database)

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

After two days of open source, and puts forward some suggestions for the netizen, especially in XE, as part of the optimization.


You can download the latest code in SVN


https://code.google.com/p/diocp/

Today made a database operation demonstration of <here I come up with methods to operate the database in the actual project, integrated in Demo>


Database operation is relatively simple, is divided into two parts, the first part of open SQL

procedure TfrmMain.btnOpenSQLClick(Sender: TObject);
var
  lvJSonStream, lvRecvObject:TJsonStream;
  lvStream:TStream;
  lvData:AnsiString;
  l, j, x:Integer;
begin
  lvJSonStream := TJsonStream.Create;
  try
    lvJSonStream.JSon := SO();
    lvJSonStream.JSon.I['cmdIndex'] := 1001;   //Open a SQL script, data acquisition
    lvJSonStream.Json.S['sql'] := mmoSQL.Lines.Text;

    FClientSocket.sendObject(lvJSonStream);
  finally
    lvJSonStream.Free;
  end;

  //Read data
  lvRecvObject := TJsonStream.Create;
  try
    FClientSocket.recvObject(lvRecvObject);

    if not lvRecvObject.getResult then
    begin
      raise Exception.Create(lvRecvObject.getResultMsg);
    end;

    SetLength(lvData, lvRecvObject.Stream.Size);
    lvRecvObject.Stream.Position := 0;
    lvRecvObject.Stream.ReadBuffer(lvData[1], lvRecvObject.Stream.Size);

    cdsMain.XMLData := lvData;
  finally
     lvRecvObject.Free;
  end;
end;

Processing server in ClientContext

procedure TClientContext.dataReceived(const pvDataObject:TObject);
var
  lvJsonStream:TJSonStream;
  lvFile:String;
  lvCmdIndex:Cardinal;
  lvXMLData, lvEncodeData:AnsiString;
  lvSQL:String;
begin
  lvJsonStream := TJSonStream(pvDataObject);

  lvCmdIndex := lvJsonStream.JSon.I['cmdIndex'];

  //Echo test
  if lvCmdIndex= 1000 then
  begin
    InterlockedIncrement(TesterINfo.__RecvTimes);
    //Write back data
    writeObject(lvJsonStream);
  end else if lvCmdIndex = 1001 then
  begin  //According to SQL, a data acquisition, in Stream
    try
      lvSQL := lvJsonStream.Json.S['sql'];

      lvXMLData := dmMain.CDSProvider.QueryXMLData(lvSQL);

      lvJsonStream.Clear();
      lvJsonStream.Stream.WriteBuffer(lvXMLData[1], Length(lvXMLData));
      lvJsonStream.setResult(True);
    except
      on e:Exception do
      begin
        lvJsonStream.Clear();
        lvJsonStream.setResult(False);
        lvJsonStream.setResultMsg(e.Message);
      end;
    end;
    
    //Write back data
    writeObject(lvJsonStream);

Save data to a very nice DLL

The client:

procedure TfrmMain.btnPostClick(Sender: TObject);
var
  lvJSonStream, lvRecvObject:TJsonStream;
  lvStream:TStream;
  lvData:AnsiString;
  l, j, x:Integer;
begin
  if cdsMain.State in [dsInsert, dsEdit] then cdsMain.Post;
  
  if cdsMain.ChangeCount = 0 then
  begin
    ShowMessage('Without any modification!');
    exit;
  end;
  lvJSonStream := TJsonStream.Create;
  try
    lvJSonStream.JSon := SO();
    lvJSonStream.JSon.I['cmdIndex'] := 1002;   //Open a SQL script, data acquisition

    //Package amendment record
    with TCDSOperatorWrapper.createCDSEncode do
    begin
      setTableINfo(PAnsiChar(AnsiString(edtUpdateTable.Text)), PAnsiChar(AnsiString(edtKeyFields.Text)));
      setData(cdsMain.Data, cdsMain.Delta);
      //Execute code
      Execute;
      lvData := getPackageData;
    end;

    mmoData.Clear;
    mmoData.Lines.Add(lvData);

    lvJSonStream.Stream.Write(lvData[1], Length(lvData));

    FClientSocket.sendObject(lvJSonStream);
  finally
    lvJSonStream.Free;
  end;

  //Read data
  lvRecvObject := TJsonStream.Create;
  try
    FClientSocket.recvObject(lvRecvObject);

    if not lvRecvObject.getResult then
    begin
      raise Exception.Create(lvRecvObject.getResultMsg);
    end else
    begin
      ShowMessage('Successfully saved!');
    end;
  finally
     lvRecvObject.Free;
  end;

  cdsMain.MergeChangeLog();

end;

The server:

if lvCmdIndex = 1002 then  //Save the data to the familiar
  begin
    try
      lvJsonStream.Stream.Position := 0;
      SetLength(lvEncodeData, lvJSonStream.Stream.Size);
      lvJsonStream.Stream.ReadBuffer(lvEnCodeData[1], lvJSonStream.Stream.Size);

      dmMain.ExecuteApplyUpdate(lvEncodeData);

      lvJsonStream.Clear();
      lvJsonStream.setResult(True);
    except
      on e:Exception do
      begin
        lvJsonStream.Clear();
        lvJsonStream.setResult(False);
        lvJsonStream.setResultMsg(e.Message);
      end;
    end;
    
    //Write back data
    writeObject(lvJsonStream);
  end


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Paul at November 26, 2013 - 11:26 PM