zoukankan      html  css  js  c++  java
  • mORMOT---Samples13

    前言:从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.

     

    自己新建的表结构

  • 相关阅读:
    编写一个函数func(),将此函数的输入参数(int型)逆序输出显示,如54321 –> 12345,要求使用递归,并且函数体代码不超过8行
    java中两种单例模式
    springMVC配置freemarker 二(问题讨论篇)
    springMVC配置freemarker
    java中@value的环境配置
    java环境log4j日志环境的配置。
    websocket协议
    http报文和浏览器缓存机制
    详解网络连接
    编码总结
  • 原文地址:https://www.cnblogs.com/redhat588/p/12941201.html
Copyright © 2011-2022 走看看