历史第一人乔帮主 發表於 2022-3-13 07:51:00

lazarus CRUD

<p>lazarus CRUD</p>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:true;">unit Persistence;

{$MODE DELPHI}

interface

uses
SysUtils,
Classes,
BufDataset,
SQLdb,
SQLite3Conn;

function ListPersons: TStream;
procedure SavePersons(const ABytes: TBytes);

implementation

const
SQL_SELECT_PERSONS = 'SELECT * FROM persons';
SQL_UDPATE_PERSONS = 'UPDATE persons SET name = :name WHERE id = :id';

var
DBConnection: TSQLConnector;

procedure CreateAndConfigureDBConnection;
begin
DBConnection := TSQLConnector.Create(nil);
DBConnection.Transaction := TSQLTransaction.Create(DBConnection);
DBConnection.ConnectorType := 'SQLite3';
DBConnection.DatabaseName := '../../../DB/DataBase.sqlite3';
end;

procedure DestroyDBConnection;
begin
FreeAndNil(DBConnection);
end;

function CreateQuery(const ASQL: string): TSQLQuery;
begin
Result := TSQLQuery.Create(nil);
Result.SQLConnection := DBConnection;
Result.SQLTransaction := DBConnection.Transaction;
Result.SQL.Text := ASQL;
end;

function ListPersons: TStream;
var
VQuery: TSQLQuery;
begin
Result := TBytesStream.Create;
VQuery := CreateQuery(SQL_SELECT_PERSONS);
try
    VQuery.Open;
    VQuery.SaveToStream(Result, dfBinary);
    Result.Seek(0, TSeekOrigin.soBeginning);
finally
    VQuery.Destroy;
end;
end;

procedure SavePersons(const ABytes: TBytes);
var
VQuery: TSQLQuery;
VData: TBytesStream;
begin
VQuery := CreateQuery(SQL_SELECT_PERSONS);
VData := TBytesStream.Create(ABytes);
try
    VQuery.UpdateSQL.Text := SQL_UDPATE_PERSONS;
    VQuery.Prepare;
    VQuery.LoadFromStream(VData, dfBinary);
    VQuery.ApplyUpdates;
    DBConnection.Transaction.Commit;
finally
    VQuery.Destroy;
    VData.Free;
end;
end;

initialization
CreateAndConfigureDBConnection;

finalization
DestroyDBConnection;

end.
</pre>
</div>
<p> </p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">uses
BrookHTTPRequest,
BrookHTTPResponse,
BrookHTTPServer,
Persistence;

type
THTTPServer </span>= <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">(TBrookHTTPServer)
</span><span style="color: rgba(0, 0, 255, 1)">protected</span><span style="color: rgba(0, 0, 0, 1)">
    procedure DoRequest(ASender: TObject; ARequest: TBrookHTTPRequest;
      AResponse: TBrookHTTPResponse); </span><span style="color: rgba(0, 0, 255, 1)">override</span><span style="color: rgba(0, 0, 0, 1)">;
end;

procedure THTTPServer.DoRequest(ASender: TObject; ARequest: TBrookHTTPRequest;
AResponse: TBrookHTTPResponse);
begin
</span><span style="color: rgba(0, 0, 255, 1)">if</span> ARequest.Payload.Length &gt; <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)"> then
    SavePersons(ARequest.Payload.Content)
</span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">
    AResponse.SendStream(ListPersons, </span><span style="color: rgba(128, 0, 128, 1)">200</span><span style="color: rgba(0, 0, 0, 1)">);
end;

begin
with THTTPServer.Create(nil) </span><span style="color: rgba(0, 0, 255, 1)">do</span>
<span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)">
    Port :</span>= <span style="color: rgba(128, 0, 128, 1)">8080</span><span style="color: rgba(0, 0, 0, 1)">;
    Open;
    </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> not Active then
      Exit;
    WriteLn(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Server running at http://localhost:</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">, Port);
    ReadLn;
</span><span style="color: rgba(0, 0, 255, 1)">finally</span><span style="color: rgba(0, 0, 0, 1)">
    Free;
end;
end.</span></pre>
</div>
<p>&nbsp;</p>
<p> </p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">unit Client;

{$MODE DELPHI}

</span><span style="color: rgba(0, 0, 255, 1)">interface</span><span style="color: rgba(0, 0, 0, 1)">

uses
SysUtils,
Classes,
DB,
BufDataset,
FPHTTPClient;

function NewGuid: </span><span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">;
function ListPersons(</span><span style="color: rgba(0, 0, 255, 1)">const</span> AURL: <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">): TDataSet;
procedure SavePersons(</span><span style="color: rgba(0, 0, 255, 1)">const</span> AURL: <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">; ADataSet: TDataSet);
function CreatePersonsDataSet: TDataSet;

implementation

function NewGuid: </span><span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">;
begin
Result :</span>=<span style="color: rgba(0, 0, 0, 1)"> TGuid.NewGuid.ToString(True);
end;

function ListPersons(</span><span style="color: rgba(0, 0, 255, 1)">const</span> AURL: <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">): TDataSet;
</span><span style="color: rgba(0, 0, 255, 1)">var</span><span style="color: rgba(0, 0, 0, 1)">
VData: TStream;
begin
Result :</span>=<span style="color: rgba(0, 0, 0, 1)"> TBufDataset.Create(nil);
VData :</span>=<span style="color: rgba(0, 0, 0, 1)"> TBytesStream.Create;
</span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)">
    TFPHTTPClient.SimpleGet(AURL, VData);
    TBufDataset(Result).LoadFromStream(VData, dfBinary);
</span><span style="color: rgba(0, 0, 255, 1)">finally</span><span style="color: rgba(0, 0, 0, 1)">
    VData.Free;
end;
end;

procedure SavePersons(</span><span style="color: rgba(0, 0, 255, 1)">const</span> AURL: <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">; ADataSet: TDataSet);
</span><span style="color: rgba(0, 0, 255, 1)">var</span><span style="color: rgba(0, 0, 0, 1)">
VClient: TFPHTTPClient;
begin
</span><span style="color: rgba(0, 0, 255, 1)">if</span> ADataSet.State <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> dsEditModes then
    ADataSet.Post;
</span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)">
    VClient :</span>=<span style="color: rgba(0, 0, 0, 1)"> TFPHTTPClient.Create(nil);
    VClient.RequestBody :</span>=<span style="color: rgba(0, 0, 0, 1)"> TBytesStream.Create;
    </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)">
      TBufDataset(ADataSet).SaveToStream(VClient.RequestBody, dfBinary);
      VClient.RequestBody.Seek(</span><span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">, TSeekOrigin.soBeginning);
      VClient.Post(AURL);
    </span><span style="color: rgba(0, 0, 255, 1)">finally</span><span style="color: rgba(0, 0, 0, 1)">
      VClient.RequestBody.Free;
      VClient.Free;
    end;
</span><span style="color: rgba(0, 0, 255, 1)">finally</span><span style="color: rgba(0, 0, 0, 1)">
    FreeAndNil(ADataSet);
end;
end;

function CreatePersonsDataSet: TDataSet;
begin
Result :</span>=<span style="color: rgba(0, 0, 0, 1)"> TBufDataset.Create(nil);
Result.FieldDefs.Add(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">name</span><span style="color: rgba(128, 0, 0, 1)">'</span>, ftString, <span style="color: rgba(128, 0, 128, 1)">100</span><span style="color: rgba(0, 0, 0, 1)">);
TBufDataset(Result).CreateDataSet;
end;

end.</span></pre>
</div>
<p>&nbsp;</p>

</div>
<div id="MySignature" role="contentinfo">
    <p>本文来自博客园,作者:{咏南中间件},转载请注明原文链接:https://www.cnblogs.com/hnxxcxg/p/15999453.html</p><br><br>
来源:https://www.cnblogs.com/hnxxcxg/p/15999453.html
頁: [1]
查看完整版本: lazarus CRUD