01-Synopse mORMot框架样例学习-01 In Memory ORM(JSON数据库)

 


样例文本中已经做了说明:

这个例子旨在展示框架中基本的ORM操作方式:

- 在Unit1.pas中定义了一个TSampleRecord类

- 一个静态服务器(即:内存数据库)被初始化(TSQLRestServerStatic.Create方法);

它以JSON文件的形式在磁盘上存储数据,并且不需要SQLite3数据库引擎

- Unit1.pas单元中的窗口设计意在想数据库中添加一条记录,时间字段按当前的日期和时间进行填充

- '查找之前信息'按钮展示了如何执行一个基本的查询

- 在程序退出时,Database.Destroy过程将会更新JSON文件

- 由于框架采用UTF-8编码,我们在用户接口中使用了一些快速转换操作;在实际应用中,您最好使用我们的SQLite3国际化单元和对应的TLanguageFile.StringToUTF8()以及TLanguageFile.UTF8ToString()方法

- 请注意exe的最小体积(在我们不使用SQLite3的时候),在使用LVCL的情况下一般不会超过80K


我将SampleData.pas集成到Unit1.pas,并修改了Project01.dpr和数据类型

program Project01;
uses
  {$I SynDprUses.inc} // use FastMM4 on older Delphi, or set FPC threads
  Forms,   SysUtils,       SynCommons,     mORMot,
  Unit1 in 'Unit1.pas' {Form1}   ;
{$R *.res}
begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Form1.Caption := ' Sample 01 - In Memory ORM';
  Form1.aSQLRest := TSQLRestStorageInMemory.Create(TSQLSampleRecord,nil,
    ChangeFileExt(ExeVersion.ProgramFileName,'.db'));// 程序启动时,会加载同名.db数据库,退出时,会写入.db数据库
  Application.Run;
end.

unit Unit1;

interface

uses
  {$ifdef MSWINDOWS}
  Windows,
  Messages,
  Graphics,
  {$endif}
  Classes, SysUtils, Forms, Controls, Dialogs, StdCtrls,
  SynCommons,
  SynTable,
  mORMot;

type

  TSQLSampleRecord = class(TSQLRecord)
  private
   // fID: TID; //可以省略,系统自动生成RowID字段
    fName: RawUTF8;
    fInfo: RawUTF8;
  published
    // property aID: TID read fID write fID; // 可以省略,系统 自动生成 RowID字段
    property Name: RawUTF8 read fName write fName;    //Name字段
    property Info: RawUTF8 read fInfo write fInfo;    //Info字段
  end;

  { TForm1 }

  TForm1 = class(TForm)
    AddButton: TButton;
    Label1: TLabel;
    Label2: TLabel;
    QuitButton: TButton;
    FindButton: TButton;
    QuestionMemo: TMemo;
    NameEdit: TEdit;
    procedure AddButtonClick(Sender: TObject);
    procedure FindButtonClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure QuitButtonClick(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
    aSQLRest: TSQLRest;
    aSQLModel: TSQLModel;
  end;
var
  Form1: TForm1;
implementation
{$ifdef FPC} {$R *.lfm}  {$else}   {$R *.dfm}   {$endif}
procedure TForm1.FormCreate(Sender: TObject);
begin
aSQLModel :=TSQLModel.Create([TSQLSampleRecord]);
end;

procedure TForm1.AddButtonClick(Sender: TObject);
var Rec: TSQLSampleRecord;
begin
  Rec := TSQLSampleRecord.Create;
  try
    Rec.Name := StringToUTF8(NameEdit.Text);
    Rec.Info := StringToUTF8(QuestionMemo.Text);
    if aSQLRest.Add(Rec,true)=0 then  //添加
      ShowMessage('Error adding the data') else begin
      NameEdit.Text := '';
      QuestionMemo.Text := '';
      NameEdit.SetFocus;
    end;
  finally
    Rec.Free;
  end;
end;

procedure TForm1.FindButtonClick(Sender: TObject);
var Rec: TSQLSampleRecord;
begin
  Rec := TSQLSampleRecord.Create(aSQLRest,'Name=?',[StringToUTF8(NameEdit.Text)]);//查找
  try
    if Rec.ID=0 then
      QuestionMemo.Text := 'Not found' else
      QuestionMemo.Text := UTF8ToString(Rec.Info);
  finally
    Rec.Free;
  end;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  aSQLRest.Free;   aSQLModel.Free;
end;

procedure TForm1.QuitButtonClick(Sender: TObject);
begin
  Close;
end;

end.