-
-
Notifications
You must be signed in to change notification settings - Fork 136
/
Copy pathmormot.rest.memserver.pas
732 lines (654 loc) · 25.6 KB
/
mormot.rest.memserver.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
/// REpresentation State Tranfer (REST) In-Memory Server
// - this unit is a part of the Open Source Synopse mORMot framework 2,
// licensed under a MPL/GPL/LGPL three license - see LICENSE.md
unit mormot.rest.memserver;
{
*****************************************************************************
Standalone REST In-Memory Server Using JSON or Binary Persistence
- TRestOrmServerFullMemory Standalone REST ORM Engine
- TRestServerFullMemory Standalone REST Server
*****************************************************************************
}
interface
{$I ..\mormot.defines.inc}
uses
sysutils,
classes,
variants,
contnrs,
mormot.core.base,
mormot.core.os,
mormot.core.buffers,
mormot.core.unicode,
mormot.core.text,
mormot.core.datetime,
mormot.core.variants,
mormot.core.data,
mormot.core.rtti,
mormot.crypt.core,
mormot.core.json,
mormot.core.threads,
mormot.core.perf,
mormot.crypt.secure,
mormot.core.log,
mormot.core.interfaces,
mormot.orm.base,
mormot.orm.core,
mormot.orm.rest,
mormot.orm.server,
mormot.orm.storage,
mormot.soa.core,
mormot.soa.server,
mormot.rest.core,
mormot.rest.server;
{ ************ TRestOrmServerFullMemory Standalone REST ORM Engine }
type
/// implements TRestServerFullMemory.ORM process for REST server
// - this server will use TRestStorageInMemory instances to handle
// the data in memory, and optionally persist the data on disk as JSON or
// binary files
// - so it will not handle all SQL requests, just basic CRUD commands on
// separated tables
// - at least, it will compile as a TRestOrmServer without complaining for
// pure abstract methods; it can be used to host some services if database
// and ORM needs are basic (e.g. if only authentication and CRUD are needed),
// without the need to link the SQLite3 engine
TRestOrmServerFullMemory = class(TRestOrmServer)
protected
fFileName: TFileName;
fBinaryFile: boolean;
fStaticDataCount: integer;
public
function GetStorage(aTable: TOrmClass): TRestStorageInMemory;
/// overridden methods which will call fStaticData[TableModelIndex] directly
// without the TRestOrmServer overhead
function EngineAdd(TableModelIndex: integer; const SentData: RawUtf8): TID; override;
function EngineRetrieve(TableModelIndex: integer; ID: TID): RawUtf8; override;
function EngineUpdate(TableModelIndex: integer; ID: TID;
const SentData: RawUtf8): boolean; override;
function EngineDelete(TableModelIndex: integer; ID: TID): boolean; override;
function EngineDeleteWhere(TableModelIndex: integer; const SqlWhere: RawUtf8;
const IDs: TIDDynArray): boolean; override;
function EngineRetrieveBlob(TableModelIndex: integer; aID: TID;
BlobField: PRttiProp; out BlobData: RawBlob): boolean; override;
function EngineUpdateBlob(TableModelIndex: integer; aID: TID;
BlobField: PRttiProp; const BlobData: RawBlob): boolean; override;
function EngineUpdateField(TableModelIndex: integer;
const SetFieldName, SetValue, WhereFieldName, WhereValue: RawUtf8): boolean; override;
function EngineUpdateFieldIncrement(TableModelIndex: integer; ID: TID;
const FieldName: RawUtf8; Increment: Int64): boolean; override;
/// overridden methods which will return error (no main DB in our context)
function MainEngineAdd(TableModelIndex: integer; const SentData: RawUtf8): TID; override;
function MainEngineRetrieve(TableModelIndex: integer; ID: TID): RawUtf8; override;
function MainEngineList(const SQL: RawUtf8; ForceAjax: boolean;
ReturnedRowCount: PPtrInt): RawUtf8; override;
function MainEngineUpdate(TableModelIndex: integer; aID: TID;
const SentData: RawUtf8): boolean; override;
function MainEngineDelete(TableModelIndex: integer; ID: TID): boolean; override;
function MainEngineDeleteWhere(TableModelIndex: integer; const SqlWhere: RawUtf8;
const IDs: TIDDynArray): boolean; override;
function MainEngineRetrieveBlob(TableModelIndex: integer; aID: TID;
BlobField: PRttiProp; out BlobData: RawBlob): boolean; override;
function MainEngineUpdateBlob(TableModelIndex: integer; aID: TID;
BlobField: PRttiProp; const BlobData: RawBlob): boolean; override;
function MainEngineUpdateField(TableModelIndex: integer;
const SetFieldName, SetValue, WhereFieldName, WhereValue: RawUtf8): boolean; override;
function MainEngineUpdateFieldIncrement(TableModelIndex: integer; ID: TID;
const FieldName: RawUtf8; Increment: Int64): boolean; override;
// method not implemented: always return false
function EngineExecute(const aSql: RawUtf8): boolean; override;
public
/// initialize an in-memory REST server with no database file
constructor Create(aRest: TRest); overload; override;
/// initialize an in-memory REST server with a database file
// - all classes of the model will be created as TRestStorageInMemory
// - then data persistence will be initialized using aFileName, but no
// file will be written to disk, unless you call explicitly UpdateToFile
// - if aFileName is left void (''), data will not be persisted on disk
constructor Create(aRest: TRest; const aFileName: TFileName;
aBinaryFile: boolean = false); reintroduce; overload; virtual;
/// finalize the REST server
// - this overridden destructor will write any modification on file (if
// needed), and release all used memory
destructor Destroy; override;
/// Missing tables are created if they don't exist yet for every TOrm
// class of the Database Model
// - you must call explicitly this before having called OrmMapInMemory()
// - all table description (even Unique feature) is retrieved from the Model
// - this method also create additional fields, if the TOrm definition
// has been modified; only field adding is available, field renaming or
// field deleting are not allowed in the FrameWork (in such cases, you must
// create a new TOrm type)
procedure CreateMissingTables(user_version: cardinal = 0;
Options: TOrmInitializeTableOptions = []); override;
/// load the content from the specified file name
// - do nothing if file name was not assigned
procedure LoadFromFile; virtual;
/// load the content from the supplied resource
procedure LoadFromStream(aStream: TStream); virtual;
/// write any modification into file
// - do nothing if file name was not assigned
procedure UpdateToFile; virtual;
/// clear all internal storage content
procedure DropDatabase; virtual;
published
/// the file name used for data persistence
property FileName: TFileName
read fFileName write fFileName;
/// set if the file content is to be compressed binary, or standard JSON
// - it will use TRestStorageInMemory LoadFromJson/LoadFromBinary
// SaveToJson/SaveToBinary methods for optimized storage
property BinaryFile: boolean
read fBinaryFile write fBinaryFile;
end;
{ ************ TRestServerFullMemory Standalone REST Server }
type
/// a REST server using only in-memory tables
// - this server will use TRestStorageInMemory instances to handle
// the data in memory, and optionally persist the data on disk as JSON or
// binary files
// - so it will not handle all SQL requests, just basic CRUD commands and
// most simple SELECT with a single where clause, on a single table
// - at least, it will compile as a TRestServer without complaining for
// pure abstract methods; it can be used to host some services if database
// and ORM needs are basic (e.g. if only authentication and CRUD are needed),
// without the need to link the SQLite3 engine
TRestServerFullMemory = class(TRestServer)
protected
function GetBinaryFile: boolean;
function GetFileName: TFileName;
public
/// initialize an in-memory REST server with no database file
constructor Create(aModel: TOrmModel;
aHandleUserAuthentication: boolean = false); overload; override;
/// initialize an in-memory REST server with a database file
// - all classes of the model will be created as TRestStorageInMemory
// - then data persistence will be initialized using aFileName, but no
// file will be written to disk, unless you call explicitly UpdateToFile
// - if aFileName is left void (''), data will not be persistent
constructor Create(aModel: TOrmModel;
const aFileName: TFileName; aBinaryFile: boolean = false;
aHandleUserAuthentication: boolean = false); reintroduce; overload; virtual;
/// this method-based service will be accessible from PUT ModelRoot/Flush,
// and will write any modification into file
// - method parameters signature matches TOnRestServerCallBack type
// - do nothing if file name was not assigned, or the Model is void
// - can be used from a remote client to ensure that any Add/Update/Delete
// will be stored to disk, via
// ! aClient.CallBackPut('Flush','',dummy)
procedure Flush(Ctxt: TRestServerUriContext);
published
/// the file name used for data persistence
property FileName: TFileName
read GetFileName;
/// set if the file content is to be compressed binary, or standard JSON
// - it will use TRestStorageInMemory LoadFromJson/LoadFromBinary
// SaveToJson/SaveToBinary methods for optimized storage
property BinaryFile: boolean
read GetBinaryFile;
end;
/// create an external static in-memory database for a specific class
// - aServer is expected to be a TRestOrmServer, e.g. from TRestServer.OrmInstance
// - call it after TRestServer.Create, before IRestOrmServer.CreateMissingTables;
// warning: if you don't call this method before CreateMissingTable method
// is called, the table will be created as a regular table by the main
// database engine, and won't be static
// - can load the table content from a file if a file name is specified
// (could be either JSON or compressed Binary format on disk)
// - you can define a particular external engine by using a custom class -
// by default, it will create a TRestStorageInMemory instance
// - this data handles basic REST commands, since no complete SQL interpreter
// can be implemented by TRestStorage; to provide full SQL process,
// you should better use a Virtual Table class, inheriting e.g. from
// TOrmVirtualTableAutoID associated with TOrmVirtualTableJson/Binary
// via a Model.VirtualTableRegister() call before TRestServer.Create
// - you can use this method to change the filename of an existing storage
// - return nil on any error, or an EModelException if the class is not in
// the aServer database model
function OrmMapInMemory(aServer: TRestOrm; aClass: TOrmClass;
const aFileName: TFileName = ''; aBinaryFile: boolean = false;
aStorageClass: TRestStorageInMemoryClass = nil): TRestStorageInMemory;
/// create a new minimal TRestServer instance, to be used with
// external SQL or NoSQL storage
// - will try to instantiate an in-memory (':memory:') TRestServerDB, and if
// mormot.orm.sqlite3.pas is not linked, fallback to a TRestServerFullMemory
// - used e.g. by TRestMongoDBCreate() and TRestExternalDBCreate()
function CreateInMemoryServer(aModel: TOrmModel;
aHandleUserAuthentication: boolean): TRestServer;
// backward compatibility types redirections
{$ifndef PUREMORMOT2}
type
// should be a proper type for RegisterClassNameForDefinition
TSqlRestServerFullMemory = TRestServerFullMemory;
function StaticDataCreate(aServer: TRestOrm; aClass: TOrmClass;
const aFileName: TFileName = ''; aBinaryFile: boolean = false;
aStorageClass: TRestStorageInMemoryClass = nil): TRestStorageInMemory;
function CreateInMemoryServerForAllVirtualTables(aModel: TOrmModel;
aHandleUserAuthentication: boolean): TRestServer;
{$endif PUREMORMOT2}
implementation
{ ************ TRestOrmServerFullMemory Standalone REST ORM Engine }
{ TRestOrmServerFullMemory }
constructor TRestOrmServerFullMemory.Create(aRest: TRest);
var
t: PtrInt;
begin
inherited Create(aRest); // calls fRest.SetOrmInstance(self)
// pre-allocate all TRestStorageInMemory instances
fStaticDataCount := length(fModel.Tables);
for t := 0 to fStaticDataCount - 1 do
OrmMapInMemory(self, fModel.Tables[t]);
end;
constructor TRestOrmServerFullMemory.Create(aRest: TRest;
const aFileName: TFileName; aBinaryFile: boolean);
begin
fFileName := aFileName;
fBinaryFile := aBinaryFile;
Create(aRest);
LoadFromFile;
CreateMissingTables(0, []);
end;
procedure TRestOrmServerFullMemory.CreateMissingTables(user_version: cardinal;
Options: TOrmInitializeTableOptions);
var
t: PtrInt;
begin
inherited;
// create any missing static instances - e.g. for just added user/group
if fStaticDataCount <> length(fModel.Tables) then
begin
for t := fStaticDataCount to high(fModel.Tables) do
OrmMapInMemory(self, fModel.Tables[t]);
fStaticDataCount := length(fModel.Tables);
end;
// initialize new tables
for t := 0 to fStaticDataCount - 1 do
with TRestStorageInMemory(fStaticData[t]) do
if Count = 0 then
// emulates TRestServerDB.CreateMissingTables
StoredClass.InitializeTable(self, '', Options);
end;
destructor TRestOrmServerFullMemory.Destroy;
begin
UpdateToFile;
inherited;
end;
procedure TRestOrmServerFullMemory.DropDatabase;
var
t: PtrInt;
begin
for t := 0 to fStaticDataCount - 1 do
TRestStorageInMemory(fStaticData[t]).DropValues;
end;
procedure TRestOrmServerFullMemory.LoadFromStream(aStream: TStream);
var
magic, json: RawUtf8;
t: PtrInt;
P: PUtf8Char;
info: TGetJsonField;
begin
if aStream = nil then
exit;
if fBinaryFile then
begin
// optimized binary content with SynLZ + variable-length record storage
magic := ReadStringFromStream(aStream);
if PosEx('ServerFullMemory00', magic) > 0 then
// compatible with mORMot 1.18 'TSQLRestServerFullMemory00'
repeat
t := Model.GetTableIndex(ReadStringFromStream(aStream));
if t < 0 then
break;
until not TRestStorageInMemory(fStaticData[t]).LoadFromBinary(aStream);
end
else
begin
// [{"AuthUser":[{....},{...}]},{"AuthGroup":[{...},{...}]}]
json := StreamToRawByteString(aStream); // assume UTF-8 content
if json = '' then
exit;
P := pointer(json);
while P^ <> '[' do
if P^ = #0 then
exit
else
inc(P);
inc(P);
repeat
while (P^ <> ']') and
(P^ <> '{') do
if P^ = #0 then
exit
else
inc(P);
if P^ = ']' then
break;
info.Json := P + 1;
info.GetJsonField;
P := info.Json;
if not info.WasString or
(P = nil) then
exit;
t := Model.GetTableIndexPtr(info.Value);
if t < 0 then
exit;
if (P^ <> '[') and
(P^ <> '{') then
break;
info.Value := P;
P := GotoEndJsonItem(P);
if P = nil then
break;
TRestStorageInMemory(fStaticData[t]).LoadFromJson(
info.Value, P - info.Value);
until false;
end;
end;
procedure TRestOrmServerFullMemory.LoadFromFile;
var
S: THandleStream;
begin
if (fFileName = '') or
not FileExists(fFileName) then
exit;
DropDatabase;
S := FileStreamSequentialRead(FileName);
try
LoadFromStream(S);
finally
S.Free;
end;
end;
procedure TRestOrmServerFullMemory.UpdateToFile;
const
CHARS: array[0..6] of AnsiChar = '[{":,}]';
// for S.WriteBuffer(): as CHARS[ 0123456 ]
var
S: TStream;
t: PtrInt;
modified: boolean;
timer: TPrecisionTimer;
begin
if (self = nil) or
(FileName = '') then
exit;
modified := false;
for t := 0 to fStaticDataCount - 1 do
if TRestStorageInMemory(fStaticData[t]).Modified then
begin
modified := true;
break;
end;
if not modified then
exit;
timer.Start;
S := TFileStreamEx.Create(fFileName, fmCreate);
try
if fBinaryFile then
begin
// optimized binary content with SynLZ + variable-length record storage
WriteStringToStream(S, ToText(ClassType) + '00');
for t := 0 to fStaticDataCount - 1 do
with TRestStorageInMemory(fStaticData[t]) do
begin
// each TOrmClass is stored as SynLZ-compressed binary
WriteStringToStream(S, StoredClassRecordProps.SqlTableName);
SaveToBinary(S);
end;
end
else
begin
// [{"AuthUser":[{....},{...}]},{"AuthGroup":[{...},{...}]}]
S.WriteBuffer(CHARS[0], 1);
for t := 0 to fStaticDataCount - 1 do
with TRestStorageInMemory(fStaticData[t]) do
begin
S.WriteBuffer(CHARS[1], 2);
with StoredClassRecordProps do
S.WriteBuffer(pointer(SqlTableName)^, length(SqlTableName));
S.WriteBuffer(CHARS[2], 2);
SaveToJson(S, true);
S.WriteBuffer(CHARS[5], 1);
if t < PtrInt(fStaticDataCount - 1) then
S.WriteBuffer(CHARS[4], 1);
end;
S.WriteBuffer(CHARS[6], 1);
end;
finally
S.Free;
end;
InternalLog('UpdateToFile % done in %', [fFileName, timer.Stop], sllDB);
end;
function TRestOrmServerFullMemory.EngineExecute(const aSql: RawUtf8): boolean;
begin
result := false; // not implemented in this basic REST server class
end;
function TRestOrmServerFullMemory.GetStorage(
aTable: TOrmClass): TRestStorageInMemory;
var
i: cardinal;
begin
i := fModel.GetTableIndex(aTable);
if i >= cardinal(length(fStaticData)) then
result := nil
else
result := TRestStorageInMemory(fStaticData[i]);
end;
// Engine*() methods will have direct access to static fStorage[])
function TRestOrmServerFullMemory.EngineAdd(TableModelIndex: integer;
const SentData: RawUtf8): TID;
begin
result := fStaticData[TableModelIndex].EngineAdd(TableModelIndex, SentData);
inc(InternalState);
end;
function TRestOrmServerFullMemory.EngineRetrieve(TableModelIndex: integer;
ID: TID): RawUtf8;
begin
result := fStaticData[TableModelIndex].EngineRetrieve(TableModelIndex, ID);
end;
function TRestOrmServerFullMemory.EngineUpdate(TableModelIndex: integer; ID: TID;
const SentData: RawUtf8): boolean;
begin
result := fStaticData[TableModelIndex].EngineUpdate(TableModelIndex, ID, SentData);
end;
function TRestOrmServerFullMemory.EngineDelete(TableModelIndex: integer;
ID: TID): boolean;
begin
result := fStaticData[TableModelIndex].EngineDelete(TableModelIndex, ID);
end;
function TRestOrmServerFullMemory.EngineDeleteWhere(TableModelIndex: integer;
const SqlWhere: RawUtf8; const IDs: TIDDynArray): boolean;
begin
result := fStaticData[TableModelIndex].EngineDeleteWhere(TableModelIndex,
SqlWhere, IDs);
end;
function TRestOrmServerFullMemory.EngineRetrieveBlob(TableModelIndex: integer;
aID: TID; BlobField: PRttiProp; out BlobData: RawBlob): boolean;
begin
result := fStaticData[TableModelIndex].EngineRetrieveBlob(
TableModelIndex, aID, BlobField, BlobData);
end;
function TRestOrmServerFullMemory.EngineUpdateBlob(TableModelIndex: integer;
aID: TID; BlobField: PRttiProp; const BlobData: RawBlob): boolean;
begin
result := fStaticData[TableModelIndex].EngineUpdateBlob(
TableModelIndex, aID, BlobField, BlobData);
end;
function TRestOrmServerFullMemory.EngineUpdateField(TableModelIndex: integer;
const SetFieldName, SetValue, WhereFieldName, WhereValue: RawUtf8): boolean;
begin
result := fStaticData[TableModelIndex].EngineUpdateField(TableModelIndex,
SetFieldName, SetValue, WhereFieldName, WhereValue);
end;
function TRestOrmServerFullMemory.EngineUpdateFieldIncrement(
TableModelIndex: integer; ID: TID; const FieldName: RawUtf8;
Increment: Int64): boolean;
begin
result := fStaticData[TableModelIndex].EngineUpdateFieldIncrement(
TableModelIndex, ID, FieldName, Increment);
end;
// MainEngine*() methods should return error (only access via static fStaticData[])
function TRestOrmServerFullMemory.MainEngineAdd(TableModelIndex: integer;
const SentData: RawUtf8): TID;
begin
result := 0;
end;
function TRestOrmServerFullMemory.MainEngineRetrieve(TableModelIndex: integer;
ID: TID): RawUtf8;
begin
result := '';
end;
function TRestOrmServerFullMemory.MainEngineList(const SQL: RawUtf8;
ForceAjax: boolean; ReturnedRowCount: PPtrInt): RawUtf8;
begin
result := '';
end;
function TRestOrmServerFullMemory.MainEngineUpdate(
TableModelIndex: integer; aID: TID; const SentData: RawUtf8): boolean;
begin
result := false;
end;
function TRestOrmServerFullMemory.MainEngineDelete(
TableModelIndex: integer; ID: TID): boolean;
begin
result := false;
end;
function TRestOrmServerFullMemory.MainEngineDeleteWhere(
TableModelIndex: integer; const SqlWhere: RawUtf8;
const IDs: TIDDynArray): boolean;
begin
result := false;
end;
function TRestOrmServerFullMemory.MainEngineRetrieveBlob(
TableModelIndex: integer; aID: TID; BlobField: PRttiProp;
out BlobData: RawBlob): boolean;
begin
result := false;
end;
function TRestOrmServerFullMemory.MainEngineUpdateBlob(
TableModelIndex: integer; aID: TID; BlobField: PRttiProp;
const BlobData: RawBlob): boolean;
begin
result := false;
end;
function TRestOrmServerFullMemory.MainEngineUpdateField(
TableModelIndex: integer;
const SetFieldName, SetValue, WhereFieldName, WhereValue: RawUtf8): boolean;
begin
result := false;
end;
function TRestOrmServerFullMemory.MainEngineUpdateFieldIncrement(
TableModelIndex: integer; ID: TID; const FieldName: RawUtf8;
Increment: Int64): boolean;
begin
result := false;
end;
{ ************ TRestServerFullMemory Standalone REST Server }
function OrmMapInMemory(aServer: TRestOrm; aClass: TOrmClass;
const aFileName: TFileName; aBinaryFile: boolean;
aStorageClass: TRestStorageInMemoryClass): TRestStorageInMemory;
var
t: PtrInt;
s: TRestOrmServer;
begin
s := aServer as TRestOrmServer;
if aStorageClass = nil then
// default in-memory engine
aStorageClass := TRestStorageInMemory;
t := aServer.Model.GetTableIndexExisting(aClass);
result := TRestStorageInMemory(s.GetStaticTableIndex(t));
if result <> nil then
// class already registered -> check aStorageClass, and update file name
(result as aStorageClass).FileName := aFileName
else
begin
// class not already registered -> create and register now
result := aStorageClass.Create(aClass, s, aFileName, aBinaryFile);
result.StorageLockShouldIncreaseOwnerInternalState := true;
s.StaticTableSetup(t, result, sStaticDataTable);
end;
end;
function CreateInMemoryServer(aModel: TOrmModel;
aHandleUserAuthentication: boolean): TRestServer;
var
c: TRestClass;
fake: TSynConnectionDefinition;
begin
fake := TSynConnectionDefinition.Create;
try
// search SQLite3 by name, available if mormot.orm.sqlite3 is linked
fake.Kind := 'TRestServerDB';
c := TRest.ClassFrom(fake);
if (c = nil) or
not c.InheritsFrom(TRestServer) then
begin
// fallback if SQLite3 not linked
result := TRestServerFullMemory.Create(aModel, aHandleUserAuthentication);
exit;
end;
// we have the SQLite3 engine at hand
fake.ServerName := ':memory:';
result := TRestServerClass(c).RegisteredClassCreateFrom(
aModel, fake, aHandleUserAuthentication);
finally
fake.Free;
end;
end;
{ TRestServerFullMemory }
constructor TRestServerFullMemory.Create(aModel: TOrmModel;
aHandleUserAuthentication: boolean);
begin
inherited Create(aModel, aHandleUserAuthentication);
if not (rsoNoTableURI in fOptions) then
ServiceMethodRegister('Flush', Flush, false, [mPUT]);
TRestOrmServerFullMemory.Create(self); // assign the ORM in-memory engine
end;
constructor TRestServerFullMemory.Create(aModel: TOrmModel;
const aFileName: TFileName; aBinaryFile, aHandleUserAuthentication: boolean);
begin
inherited Create(aModel, aHandleUserAuthentication);
if not (rsoNoTableURI in fOptions) then
ServiceMethodRegister('Flush', Flush, false, [mPUT]);
TRestOrmServerFullMemory.Create(self, aFileName, aBinaryFile);
end;
function TRestServerFullMemory.GetBinaryFile: boolean;
begin
if self = nil then
result := false
else
result := (fOrmInstance as TRestOrmServerFullMemory).BinaryFile;
end;
function TRestServerFullMemory.GetFileName: TFileName;
begin
if self = nil then
result := ''
else
result := (fOrmInstance as TRestOrmServerFullMemory).FileName;
end;
procedure TRestServerFullMemory.Flush(Ctxt: TRestServerUriContext);
begin
if Ctxt.Method <> mPUT then
exit;
(fOrmInstance as TRestOrmServerFullMemory).UpdateToFile;
Ctxt.Success;
end;
// backward compatibility types redirections
{$ifndef PUREMORMOT2}
function StaticDataCreate(aServer: TRestOrm; aClass: TOrmClass;
const aFileName: TFileName; aBinaryFile: boolean;
aStorageClass: TRestStorageInMemoryClass): TRestStorageInMemory;
begin
result := OrmMapInMemory(aServer, aClass, aFileName, aBinaryFile, aStorageClass);
end;
function CreateInMemoryServerForAllVirtualTables(aModel: TOrmModel;
aHandleUserAuthentication: boolean): TRestServer;
begin
result := CreateInMemoryServer(aModel, aHandleUserAuthentication);
end;
{$endif PUREMORMOT2}
initialization
TRestServerFullMemory.RegisterClassNameForDefinition;
{$ifndef PUREMORMOT2}
TSqlRestServerFullMemory.RegisterClassNameForDefinition;
{$endif PUREMORMOT2}
end.