source upload
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
{
|
||||
Synopse mORMot framework
|
||||
|
||||
Sample 01 - In Memory ORM
|
||||
purpose of this sample is to show the basic ORM usage of the framework:
|
||||
|
||||
- a TRecord class is defined in Unit1.pas
|
||||
- a static server (i.e. in-memory database) is initialized (see
|
||||
TSQLRestStorage.Create below);
|
||||
it will store the data in a JSON file in the disk and won't require
|
||||
the SQLite3 database engine
|
||||
- the purpose of the form in Unit1.pas is to add a record to the
|
||||
database; the Time field is filled with the current date and time
|
||||
- the 'Find a previous message' button show how to perform a basic query
|
||||
- on application quit, the Database.Destroy will update the JSON file
|
||||
- since the framework use UTF-8 encoding, we use some basic functions for
|
||||
fast conversion to/from the User Interface; in real applications,
|
||||
you should better use our SQLite3i18n unit and the corresponding
|
||||
TLanguageFile.StringToUTF8() and TLanguageFile.UTF8ToString() methods
|
||||
- note the tiny size of the EXE (since we don't use SQLite3), less than
|
||||
80KB with LVCL :)
|
||||
|
||||
Version 1.0 - January 24, 2010
|
||||
- Initial Release
|
||||
|
||||
Version 1.1 - April 14, 2011
|
||||
- use TSQLRestStorageInMemory instead of abstract TSQLRestStorage
|
||||
|
||||
}
|
||||
|
||||
program Project01;
|
||||
|
||||
uses
|
||||
{$I SynDprUses.inc} // use FastMM4 on older Delphi, or set FPC threads
|
||||
Forms,
|
||||
SysUtils,
|
||||
SynCommons,
|
||||
mORMot,
|
||||
Unit1 in 'Unit1.pas' {Form1},
|
||||
SampleData in 'SampleData.pas';
|
||||
|
||||
{$R *.res}
|
||||
|
||||
begin
|
||||
Application.Initialize;
|
||||
Application.CreateForm(TForm1, Form1);
|
||||
Form1.Caption := ' Sample 01 - In Memory ORM';
|
||||
Form1.Database := TSQLRestStorageInMemory.Create(TSQLSampleRecord,nil,
|
||||
ChangeFileExt(ExeVersion.ProgramFileName,'.db'));
|
||||
Application.Run;
|
||||
end.
|
@@ -0,0 +1,88 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<CONFIG>
|
||||
<ProjectOptions>
|
||||
<Version Value="9"/>
|
||||
<PathDelim Value="\"/>
|
||||
<General>
|
||||
<SessionStorage Value="InProjectDir"/>
|
||||
<MainUnit Value="0"/>
|
||||
<Title Value="Project01"/>
|
||||
<ResourceType Value="res"/>
|
||||
<UseXPManifest Value="True"/>
|
||||
</General>
|
||||
<i18n>
|
||||
<EnableI18N LFM="False"/>
|
||||
</i18n>
|
||||
<VersionInfo>
|
||||
<StringTable ProductVersion=""/>
|
||||
</VersionInfo>
|
||||
<BuildModes Count="1">
|
||||
<Item1 Name="Default" Default="True"/>
|
||||
</BuildModes>
|
||||
<PublishOptions>
|
||||
<Version Value="2"/>
|
||||
</PublishOptions>
|
||||
<RunParams>
|
||||
<local>
|
||||
<FormatVersion Value="1"/>
|
||||
</local>
|
||||
</RunParams>
|
||||
<RequiredPackages Count="1">
|
||||
<Item1>
|
||||
<PackageName Value="LCL"/>
|
||||
</Item1>
|
||||
</RequiredPackages>
|
||||
<Units Count="2">
|
||||
<Unit0>
|
||||
<Filename Value="Project01.lpr"/>
|
||||
<IsPartOfProject Value="True"/>
|
||||
<UnitName Value="Project01"/>
|
||||
</Unit0>
|
||||
<Unit1>
|
||||
<Filename Value="Unit1.pas"/>
|
||||
<IsPartOfProject Value="True"/>
|
||||
<ComponentName Value="Form1"/>
|
||||
<HasResources Value="True"/>
|
||||
<ResourceBaseClass Value="Form"/>
|
||||
<UnitName Value="Unit1"/>
|
||||
</Unit1>
|
||||
</Units>
|
||||
</ProjectOptions>
|
||||
<CompilerOptions>
|
||||
<Version Value="11"/>
|
||||
<PathDelim Value="\"/>
|
||||
<Target>
|
||||
<Filename Value="Project01"/>
|
||||
</Target>
|
||||
<SearchPaths>
|
||||
<IncludeFiles Value="$(ProjOutDir);..\..\.."/>
|
||||
<OtherUnitFiles Value="..\..;..\..\.."/>
|
||||
<UnitOutputDirectory Value="lib\$(TargetCPU)-$(TargetOS)"/>
|
||||
</SearchPaths>
|
||||
<Conditionals Value="if TargetOS='darwin' then
|
||||
CustomOptions := ' -Cg-';"/>
|
||||
<Linking>
|
||||
<Options>
|
||||
<Win32>
|
||||
<GraphicApplication Value="True"/>
|
||||
</Win32>
|
||||
</Options>
|
||||
</Linking>
|
||||
<Other>
|
||||
<CompilerPath Value="$(CompPath)"/>
|
||||
</Other>
|
||||
</CompilerOptions>
|
||||
<Debugging>
|
||||
<Exceptions Count="3">
|
||||
<Item1>
|
||||
<Name Value="EAbort"/>
|
||||
</Item1>
|
||||
<Item2>
|
||||
<Name Value="ECodetoolError"/>
|
||||
</Item2>
|
||||
<Item3>
|
||||
<Name Value="EFOpenError"/>
|
||||
</Item3>
|
||||
</Exceptions>
|
||||
</Debugging>
|
||||
</CONFIG>
|
@@ -0,0 +1,53 @@
|
||||
{
|
||||
Synopse mORMot framework
|
||||
|
||||
Sample 01 - In Memory ORM
|
||||
purpose of this sample is to show the basic ORM usage of the framework:
|
||||
|
||||
- a TRecord class is defined in Unit1.pas
|
||||
- a static server (i.e. in-memory database) is initialized (see
|
||||
TSQLRestStorage.Create below);
|
||||
it will store the data in a JSON file in the disk and won't require
|
||||
the SQLite3 database engine
|
||||
- the purpose of the form in Unit1.pas is to add a record to the
|
||||
database; the Time field is filled with the current date and time
|
||||
- the 'Find a previous message' button show how to perform a basic query
|
||||
- on application quit, the Database.Destroy will update the JSON file
|
||||
- since the framework use UTF-8 encoding, we use some basic functions for
|
||||
fast conversion to/from the User Interface; in real applications,
|
||||
you should better use our SQLite3i18n unit and the corresponding
|
||||
TLanguageFile.StringToUTF8() and TLanguageFile.UTF8ToString() methods
|
||||
|
||||
Version 1.0 - January 24, 2010
|
||||
- Initial Release
|
||||
|
||||
Version 1.1 - April 14, 2011
|
||||
- use TSQLRestStorageInMemory instead of abstract TSQLRestStorage
|
||||
|
||||
}
|
||||
|
||||
program Project01;
|
||||
|
||||
uses
|
||||
{$IFDEF UNIX}{$IFDEF UseCThreads}
|
||||
cthreads,
|
||||
{$ENDIF}{$ENDIF}
|
||||
Interfaces, // this includes the LCL widgetset
|
||||
Forms,
|
||||
SysUtils,
|
||||
mORMot,
|
||||
Unit1 {Form1},
|
||||
SampleData in 'SampleData.pas';
|
||||
|
||||
{$R *.res}
|
||||
|
||||
begin
|
||||
RequireDerivedFormResource := True;
|
||||
Application.Initialize;
|
||||
Application.CreateForm(TForm1, Form1);
|
||||
Form1.Caption := ' Sample 01 - In Memory ORM';
|
||||
Form1.Database := TSQLRestStorageInMemory.Create(TSQLSampleRecord,nil,
|
||||
ChangeFileExt(paramstr(0),'.db'));
|
||||
Application.Run;
|
||||
end.
|
||||
|
BIN
contrib/mORMot/SQLite3/Samples/01 - In Memory ORM/Project01.res
Normal file
BIN
contrib/mORMot/SQLite3/Samples/01 - In Memory ORM/Project01.res
Normal file
Binary file not shown.
@@ -0,0 +1,39 @@
|
||||
/// it's a good practice to put all data definition into a stand-alone unit
|
||||
// - this unit will be shared between client and server
|
||||
unit SampleData;
|
||||
|
||||
interface
|
||||
|
||||
uses
|
||||
SynCommons,
|
||||
mORMot;
|
||||
|
||||
type
|
||||
/// here we declare the class containing the data
|
||||
// - it just has to inherits from TSQLRecord, and the published
|
||||
// properties will be used for the ORM (and all SQL creation)
|
||||
// - the beginning of the class name must be 'TSQL' for proper table naming
|
||||
// in client/server environnment
|
||||
TSQLSampleRecord = class(TSQLRecord)
|
||||
private
|
||||
fQuestion: RawUTF8;
|
||||
fName: RawUTF8;
|
||||
fTime: TModTime;
|
||||
published
|
||||
property Time: TModTime read fTime write fTime;
|
||||
property Name: RawUTF8 read fName write fName;
|
||||
property Question: RawUTF8 read fQuestion write fQuestion;
|
||||
end;
|
||||
|
||||
/// an easy way to create a database model for client and server
|
||||
function CreateSampleModel: TSQLModel;
|
||||
|
||||
|
||||
implementation
|
||||
|
||||
function CreateSampleModel: TSQLModel;
|
||||
begin
|
||||
result := TSQLModel.Create([TSQLSampleRecord]);
|
||||
end;
|
||||
|
||||
end.
|
73
contrib/mORMot/SQLite3/Samples/01 - In Memory ORM/Unit1.dfm
Normal file
73
contrib/mORMot/SQLite3/Samples/01 - In Memory ORM/Unit1.dfm
Normal file
@@ -0,0 +1,73 @@
|
||||
object Form1: TForm1
|
||||
Left = 604
|
||||
Top = 370
|
||||
BorderStyle = bsSingle
|
||||
ClientHeight = 286
|
||||
ClientWidth = 490
|
||||
Color = clBtnFace
|
||||
Font.Charset = DEFAULT_CHARSET
|
||||
Font.Color = clWindowText
|
||||
Font.Height = -13
|
||||
Font.Name = 'Tahoma'
|
||||
Font.Style = []
|
||||
OldCreateOrder = False
|
||||
OnCreate = FormCreate
|
||||
OnDestroy = FormDestroy
|
||||
PixelsPerInch = 96
|
||||
TextHeight = 16
|
||||
object Label1: TLabel
|
||||
Left = 40
|
||||
Top = 16
|
||||
Width = 67
|
||||
Height = 16
|
||||
Caption = 'Your name:'
|
||||
end
|
||||
object Label2: TLabel
|
||||
Left = 40
|
||||
Top = 72
|
||||
Width = 86
|
||||
Height = 16
|
||||
Caption = 'Your message:'
|
||||
end
|
||||
object QuestionMemo: TMemo
|
||||
Left = 32
|
||||
Top = 88
|
||||
Width = 409
|
||||
Height = 121
|
||||
TabOrder = 0
|
||||
end
|
||||
object NameEdit: TEdit
|
||||
Left = 32
|
||||
Top = 32
|
||||
Width = 217
|
||||
Height = 24
|
||||
TabOrder = 1
|
||||
end
|
||||
object AddButton: TButton
|
||||
Left = 48
|
||||
Top = 232
|
||||
Width = 145
|
||||
Height = 25
|
||||
Caption = 'Add the message'
|
||||
TabOrder = 2
|
||||
OnClick = AddButtonClick
|
||||
end
|
||||
object QuitButton: TButton
|
||||
Left = 296
|
||||
Top = 232
|
||||
Width = 75
|
||||
Height = 25
|
||||
Caption = 'Quit'
|
||||
TabOrder = 3
|
||||
OnClick = QuitButtonClick
|
||||
end
|
||||
object FindButton: TButton
|
||||
Left = 256
|
||||
Top = 32
|
||||
Width = 185
|
||||
Height = 25
|
||||
Caption = 'Find a previous message'
|
||||
TabOrder = 4
|
||||
OnClick = FindButtonClick
|
||||
end
|
||||
end
|
74
contrib/mORMot/SQLite3/Samples/01 - In Memory ORM/Unit1.lfm
Normal file
74
contrib/mORMot/SQLite3/Samples/01 - In Memory ORM/Unit1.lfm
Normal file
@@ -0,0 +1,74 @@
|
||||
object Form1: TForm1
|
||||
Left = 378
|
||||
Height = 286
|
||||
Top = 390
|
||||
Width = 490
|
||||
BorderStyle = bsSingle
|
||||
Caption = 'Form1'
|
||||
ClientHeight = 286
|
||||
ClientWidth = 490
|
||||
Color = clBtnFace
|
||||
Font.Color = clWindowText
|
||||
Font.Height = -13
|
||||
Font.Name = 'Tahoma'
|
||||
OnCreate = FormCreate
|
||||
OnDestroy = FormDestroy
|
||||
LCLVersion = '1.2.6.0'
|
||||
object Label1: TLabel
|
||||
Left = 40
|
||||
Height = 15
|
||||
Top = 16
|
||||
Width = 73
|
||||
Caption = 'Your name:'
|
||||
ParentColor = False
|
||||
end
|
||||
object Label2: TLabel
|
||||
Left = 40
|
||||
Height = 15
|
||||
Top = 72
|
||||
Width = 95
|
||||
Caption = 'Your message:'
|
||||
ParentColor = False
|
||||
end
|
||||
object QuestionMemo: TMemo
|
||||
Left = 32
|
||||
Height = 121
|
||||
Top = 88
|
||||
Width = 409
|
||||
TabOrder = 0
|
||||
end
|
||||
object NameEdit: TEdit
|
||||
Left = 32
|
||||
Height = 23
|
||||
Top = 32
|
||||
Width = 217
|
||||
TabOrder = 1
|
||||
end
|
||||
object AddButton: TButton
|
||||
Left = 48
|
||||
Height = 25
|
||||
Top = 232
|
||||
Width = 145
|
||||
Caption = 'Add the message'
|
||||
OnClick = AddButtonClick
|
||||
TabOrder = 2
|
||||
end
|
||||
object QuitButton: TButton
|
||||
Left = 296
|
||||
Height = 25
|
||||
Top = 232
|
||||
Width = 75
|
||||
Caption = 'Quit'
|
||||
OnClick = QuitButtonClick
|
||||
TabOrder = 3
|
||||
end
|
||||
object FindButton: TButton
|
||||
Left = 256
|
||||
Height = 25
|
||||
Top = 32
|
||||
Width = 185
|
||||
Caption = 'Find a previous message'
|
||||
OnClick = FindButtonClick
|
||||
TabOrder = 4
|
||||
end
|
||||
end
|
105
contrib/mORMot/SQLite3/Samples/01 - In Memory ORM/Unit1.pas
Normal file
105
contrib/mORMot/SQLite3/Samples/01 - In Memory ORM/Unit1.pas
Normal file
@@ -0,0 +1,105 @@
|
||||
unit Unit1;
|
||||
|
||||
interface
|
||||
|
||||
uses
|
||||
{$ifdef MSWINDOWS}
|
||||
Windows,
|
||||
Messages,
|
||||
Graphics,
|
||||
{$endif}
|
||||
Classes, SysUtils, Forms, Controls, Dialogs, StdCtrls,
|
||||
SynCommons,
|
||||
SynTable,
|
||||
mORMot,
|
||||
SampleData;
|
||||
|
||||
type
|
||||
|
||||
{ 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 }
|
||||
Database: TSQLRest;
|
||||
Model: TSQLModel;
|
||||
end;
|
||||
|
||||
var
|
||||
Form1: TForm1;
|
||||
|
||||
implementation
|
||||
|
||||
{$ifdef FPC}
|
||||
{$R *.lfm}
|
||||
{$else}
|
||||
{$R *.dfm}
|
||||
{$endif}
|
||||
|
||||
{ TForm1 }
|
||||
|
||||
procedure TForm1.FormCreate(Sender: TObject);
|
||||
begin
|
||||
Model := CreateSampleModel; // from SampleData unit
|
||||
end;
|
||||
|
||||
procedure TForm1.AddButtonClick(Sender: TObject);
|
||||
var Rec: TSQLSampleRecord;
|
||||
begin
|
||||
Rec := TSQLSampleRecord.Create;
|
||||
try
|
||||
// we use explicit StringToUTF8() for conversion below
|
||||
// a real application should use TLanguageFile.StringToUTF8() in mORMoti18n
|
||||
Rec.Name := StringToUTF8(NameEdit.Text);
|
||||
Rec.Question := StringToUTF8(QuestionMemo.Text);
|
||||
if Database.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(Database,'Name=?',[StringToUTF8(NameEdit.Text)]);
|
||||
try
|
||||
if Rec.ID=0 then
|
||||
QuestionMemo.Text := 'Not found' else
|
||||
QuestionMemo.Text := UTF8ToString(Rec.Question);
|
||||
finally
|
||||
Rec.Free;
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure TForm1.FormDestroy(Sender: TObject);
|
||||
begin
|
||||
Database.Free;
|
||||
Model.Free;
|
||||
end;
|
||||
|
||||
procedure TForm1.QuitButtonClick(Sender: TObject);
|
||||
begin
|
||||
Close;
|
||||
end;
|
||||
|
||||
end.
|
||||
|
Reference in New Issue
Block a user