mORMOT---Samples\13 - StandAlone JSON SQL server

前言:从DEMO开始学习mORMOT。

注意一下作者在DEMO中的说明:

使用HTTP服务器从SQL提供SQLite3 JSON结果。
它将期望传入的SQL语句作为HTTP主体发布,即
将被执行并作为JSON返回。
这个默认的实现将只服务于测试。生成的db3文件
通过回归测试。
安装注意:确保您首先将测试复制到sample exe文件夹中。db4文件
由TestSQL3.exe生成。
但这是一个非常粗糙的机制:
-不包括安全机制;
-如果请求返回太多行,可能会使进程耗尽内存;
-不会检查所有输入资料;
-不使用语句缓存;
-未进行任何试验;
-考虑使用SynDBRemote单元,而不是远程SQL访问。
因此,与mORMot实现的方法相比,这种方法的效率要低得多。
这只是一个粗略的样品-不要在生产中使用-你应该更好
而是使用mORMot框架。
使用SynDB类而不是直接使用SynSQLite3将允许使用任何其他DB,
不仅SQlite3。
program JSONSQLServer;

{
$APPTYPE CONSOLE} uses {$I SynDprUses.inc} // use FastMM4 on older Delphi, or set FPC threads SysUtils, Classes, SynCommons, SynZip, SynDB, SynDBSQLite3, SynSQLite3Static, SynCrtSock; type TJSONServer = class protected fProps: TSQLDBConnectionProperties; fServer: THttpApiServer; function Process(Ctxt: THttpServerRequest): cardinal; public constructor Create(Props: TSQLDBConnectionProperties); destructor Destroy; override; end; { TJSONServer } const DEFAULT_PORT = {$ifdef LINUX} '8888' {$else} '888' {$endif}; constructor TJSONServer.Create(Props: TSQLDBConnectionProperties); var Conn: TSQLDBConnection; begin fProps := Props; Conn := fProps.ThreadSafeConnection; if not Conn.Connected then Conn.Connect; // ensure we can connect to the DB fServer := THttpApiServer.Create(false); fServer.AddUrl('root',DEFAULT_PORT,false,'+',true); fServer.RegisterCompress(CompressDeflate); // our server will deflate JSON :) fServer.OnRequest := Process; fServer.Clone(31); // will use a thread pool of 32 threads in total end; destructor TJSONServer.Destroy; begin fServer.Free; inherited; end; function TJSONServer.Process(Ctxt: THttpServerRequest): cardinal; begin try if length(Ctxt.InContent)<5 then raise ESynException.CreateUTF8('Invalid request % %',[Ctxt.Method,Ctxt.URL]); Ctxt.OutContentType := JSON_CONTENT_TYPE; Ctxt.OutContent := fProps.Execute(Ctxt.InContent,[]).FetchAllAsJSON(true); result := 200; except on E: Exception do begin Ctxt.OutContentType := TEXT_CONTENT_TYPE; Ctxt.OutContent := StringToUTF8(E.ClassName+': '+E.Message)+#13#10+Ctxt.InContent; result := 504; end; end; end; var Props: TSQLDBConnectionProperties; begin // copy in the sample exe folder the test.db3 file as generated by TestSQL3.exe Props := TSQLDBSQLite3ConnectionProperties.Create('test.db3','','',''); try with TJSONServer.Create(Props) do try write('Server is now running on http://localhost:', DEFAULT_PORT,'/root'#13#10'and will serve ', ExpandFileName(UTF8ToString(Props.ServerName)), ' content'#13#10#13#10'Press [Enter] to quit'); readln; finally Free; end; finally Props.Free; end; end.

从JSON/SQL HTTP服务器接收SQLite3结果

program JSONSQLClient;

{$APPTYPE CONSOLE}

uses
  {$I SynDprUses.inc} // use FastMM4 on older Delphi, or set FPC threads
  SysUtils,
  SynCrtSock,
  SynCommons;

function Client(const SQL: RawUTF8): RawUTF8;
var Http: THttpClientSocket;
    URI: AnsiString;
begin
  if ParamCount<>0 then
    URI := AnsiString(ParamStr(1)) else
    URI := 'localhost';
  Http := OpenHttp(URI,'888');
  if Http<>nil then
  try
    Http.Post('root',SQL,TEXT_CONTENT_TYPE);
    result := Http.Content;
  finally
    Http.Free;
  end else
    result := '';
end;

begin
  writeln(Client('select * from People where LastName=''Schubert'''));
  readln;
end.

 

自己新建的表结构

扫描二维码关注公众号,回复: 11250814 查看本文章

猜你喜欢

转载自www.cnblogs.com/redhat588/p/12941201.html
今日推荐