Menu

[r4300]: / branches / parsnip / Src / DB.UMain.pas  Maximize  Restore  History

Download this file

842 lines (761 with data), 27.2 kB

  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
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
{
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/
*
* Copyright (C) 2005-2013, Peter Johnson (www.delphidabbler.com).
*
* $Rev$
* $Date$
*
* Defines a singleton object and subsidiary classes that encapsulate the
* snippets and tags in the snippets database.
}
unit DB.UMain;
interface
uses
// Project
CS.Database.SnippetsTable,
CS.Database.Types,
CS.Utils.Dates,
DB.USnippet,
UMultiCastEvents,
USingleton;
type
TDBFilterFn = reference to function (ASnippet: ISnippet): Boolean;
TDatabase = class(TSingleton)
strict private
class var
fInstance: TDatabase;
class function GetInstance: TDatabase; static;
strict private
var
fSnippetsTable: TDBSnippetsTable;
fAllTags: ITagSet;
fLastModified: TUTCDateTime;
fDirty: Boolean;
procedure FlagUpdate;
strict protected
procedure Initialize; override;
procedure Finalize; override;
protected
{ TODO: Remove these properties when _TDatabase no longer needs access to
them }
// NOTE: These properties have been made protected so that _TDatabase can
// directly access them using unit scope.
// DON'T make this section strict.
property __SnippetsTable: TDBSnippetsTable read fSnippetsTable;
property __AllTags: ITagSet read fAllTags;
procedure __SetDirty(Dirty: Boolean);
property __Updated: Boolean read fDirty write __SetDirty;
public
class property Instance: TDatabase read GetInstance;
public
procedure Load;
procedure Save;
function IsDirty: Boolean;
function LookupEditableSnippet(const ASnippetID: TSnippetID):
IEditableSnippet;
function TryLookupEditableSnippet(const ASnippetID: TSnippetID;
out ASnippet: IEditableSnippet): Boolean;
function LookupSnippet(const ASnippetID: TSnippetID): ISnippet;
function TryLookupSnippet(const ASnippetID: TSnippetID;
out ASnippet: ISnippet): Boolean;
function SelectSnippets(FilterFn: TDBFilterFn): ISnippetIDList;
function GetAllSnippets: ISnippetIDList;
function GetAllTags: ITagSet;
function SnippetExists(const ASnippetID: TSnippetID): Boolean;
function SnippetCount: Integer;
function IsEmpty: Boolean;
// Returns a list of IDs of all snippets that depend on the snippet with
// the given ID.
function GetDependentsOf(const ASnippetID: TSnippetID): ISnippetIDList;
// Returns a list of IDs of all snippet that refer to (i.e. cross-reference)
// the snippet with the given ID.
function GetReferrersTo(const ASnippetID: TSnippetID): ISnippetIDList;
end;
{
TDatabaseChangeEventKind:
Enumeration that specifies the different kind of change events triggered by
the database.
}
TDatabaseChangeEventKind = (
evChangeBegin, // a change to the database is about to take place
evChangeEnd, // a change to the database has completed
evSnippetAdded, // a snippet has been added
evBeforeSnippetDelete, // a snippet is about to be deleted
evSnippetDeleted, // a snippet has been deleted
evBeforeSnippetChange, // a snippet is about to be changed
evSnippetChanged // a snippet's properties / references have changed
// TODO: add suitable event kinds for changes to tags as required
);
{
IDatabaseChangeEventInfo:
Interface supported by objects passed to Database object's change event
handler that provides information about a change event. Some properites
are not defined for certain event types. Kind property always defined.
}
IDatabaseChangeEventInfo = interface(IInterface)
['{80DEE62F-DC23-4EE7-A0B1-5DE46F483CE1}']
function GetKind: TDatabaseChangeEventKind;
{Gets kind (type) of event.
@return Event kind.
}
function GetInfo: TObject;
{Gets additional information about event.
@return Object that provides required information.
}
property Kind: TDatabaseChangeEventKind read GetKind;
{Identifies kind (type) of an event. Always defined}
property Info: TObject read GetInfo;
{Provides additional information about the event. Actual type of object
depends on Kind. May be nil}
end;
_IDatabase = interface(IInterface)
['{A280DEEF-0336-4264-8BD0-7CDFBB207D2E}']
function Lookup(const SnippetID: TSnippetID): TSnippet;
function TryLookup(const SnippetID: TSnippetID; out Snippet: TSnippet):
Boolean;
procedure AddChangeEventHandler(const Handler: TNotifyEventInfo);
{Adds a change event handler to list of listeners.
@param Handler [in] Event handler to be added.
}
procedure RemoveChangeEventHandler(const Handler: TNotifyEventInfo);
{Removes a change event handler from list of listeners.
@param Handler [in] Handler to remove from list.
}
end;
{
IDatabaseEdit:
Interface to object that can be used to edit the database.
}
IDatabaseEdit = interface(IInterface)
['{CBF6FBB0-4C18-481F-A378-84BB09E5ECF4}']
function GetEditableSnippetInfo(const Snippet: TSnippet = nil):
TSnippetEditData;
{Provides details of all a snippet's data (properties and references) that
may be edited.
@param Snippet [in] Snippet for which data is required. May be nil in
which case a blank record is returned.
@return Required data.
}
procedure UpdateSnippet(const Snippet: TSnippet; Data: TSnippetEditData);
{Updates a snippet's properties and references using provided data.
@param Snippet [in] Snippet to be updated.
@param Data [in] Record containing revised data.
}
function AddSnippet(Data: TSnippetEditData): TSnippet;
{Adds a new snippet to the database.
@param Data [in] Record storing new snippet's properties and references.
@return Reference to new snippet.
}
/// <summary>Creates new snippet that duplicates the given snippet, except
/// that its is a given a new unique name and the given title.</summary>
/// <param name="Snippet">TSnippet [in] Snippet to be duplicated.</param>
/// <param name="Title">string [in] Title of duplicated snippet.</param>
/// <return>TSnippet. Duplicated snippet.</return>
function DuplicateSnippet(const Snippet: TSnippet; const Title: string):
TSnippet;
function CreateTempSnippet(const Data: TSnippetEditData;
const Name: string = ''): TSnippet; overload;
{Creates a new temporary snippet without adding it to the Snippets
object's snippets list. The new instance may not be added to the
Snippets object.
@param Data [in] Record storing new snippet's properties and references.
@param Name [in] Required snippet name. If ommitted then the snippet is
created with a new, unique, name.
@return Reference to new snippet.
}
function CreateTempSnippet(const Snippet: TSnippet): TSnippet; overload;
{Creates a new temporary copy of a snippet without adding it to the
Snippets object's snippets list. The new instance may not be added to the
Snippets object.
@param Snippet [in] Snippet to be copied.
@return Reference to new copied snippet.
}
procedure DeleteSnippet(const Snippet: TSnippet);
{Deletes a snippet from the database.
@param Snippet [in] Snippet to be deleted.
}
end;
function _Database: _IDatabase;
{Returns singleton instance of object that encapsulates the snippets database.
@return Singleton object.
}
function Database: TDatabase;
implementation
uses
// Project
CS.Database.IO.Factory,
CS.Database.IO.Types,
CS.Database.Snippets,
CS.Database.Tags,
UExceptions,
UQuery,
UStrUtils,
UUniqueID;
var
// Private global snippets singleton object
_PvtDatabase: _IDatabase = nil;
type
_TDatabase = class(TInterfacedObject,
_IDatabase,
IDatabaseEdit
)
strict private
fChangeEvents: TMulticastEvents; // List of change event handlers
type
{
TEventInfo:
Class that provides information about a change event.
}
TEventInfo = class(TInterfacedObject, IDatabaseChangeEventInfo)
strict private
fKind: TDatabaseChangeEventKind; // Kind of event
fInfo: TObject; // Extra info about event
public
constructor Create(const Kind: TDatabaseChangeEventKind;
const Info: TObject = nil);
{Constructor. Creates an event information object.
@param Kind [in] Kind of event.
@param Info [in] Reference to further information about the event.
May be nil if event doesn't have additional information.
}
{ IDatabaseChangeEventInfo methods }
function GetKind: TDatabaseChangeEventKind;
{Gets kind (type) of event.
@return Event kind.
}
function GetInfo: TObject;
{Gets additional information about event.
@return Object that provides required information.
}
end;
procedure TriggerEvent(const Kind: TDatabaseChangeEventKind;
const Info: TObject = nil);
{Triggers a change event. Notifies all registered listeners.
@param Kind [in] Kind of event.
@param Info [in] Reference to any further information for event. May be
nil.
}
function InternalAddSnippet(const SnippetID: TSnippetID;
const Data: TSnippetEditData): TSnippet;
{Adds a new snippet to the database. Assumes snippet not already in
database.
@param SnippetID [in] ID of new snippet.
@param Data [in] Properties and references of new snippet.
@return Reference to new snippet object.
}
procedure InternalDeleteSnippet(const Snippet: TSnippet);
{Deletes a snippet from database.
@param Snippet [in] Snippet to delete from database.
}
function UniqueSnippetName: string;
{Generates a snippet "name" that is unique in the database.
@return Required unique snippet "name".
}
procedure CleanUpRefs(var Refs: TSnippetReferences);
{Cleans any references non-existant snippets from snippet references.
@param [in/out] Snippet references to be cleaned: .RequiredSnippets and
.XRefs fields are modified.
}
function MakeValidRefs(const Refs: TSnippetReferences): TSnippetReferences;
public
constructor Create;
{Constructor. Sets up new empty object.
}
destructor Destroy; override;
{Destructor. Tidies up and tears down object.
}
{ IDatabase methods }
procedure AddChangeEventHandler(const Handler: TNotifyEventInfo);
{Adds a change event handler to list of listeners.
@param Handler [in] Event handler to be added.
}
procedure RemoveChangeEventHandler(const Handler: TNotifyEventInfo);
{Removes a change event handler from list of listeners.
@param Handler [in] Handler to remove from list.
}
function Lookup(const SnippetID: TSnippetID): TSnippet;
function TryLookup(const SnippetID: TSnippetID; out Snippet: TSnippet):
Boolean;
{ IDatabaseEdit methods }
function GetEditableSnippetInfo(const Snippet: TSnippet = nil):
TSnippetEditData;
{Provides details of all a snippet's data (properties and references) that
may be edited.
@param Snippet [in] Snippet for which data is required. May be nil in
which case a blank record is returned.
@return Required data.
}
procedure UpdateSnippet(const Snippet: TSnippet; Data: TSnippetEditData);
{Updates a snippet's properties and references using provided data.
@param Snippet [in] Snippet to be updated.
@param Data [in] Record containing revised data.
}
function AddSnippet(Data: TSnippetEditData): TSnippet;
{Adds a new snippet to the database.
@param Data [in] Record storing new snippet's properties and references.
@return Reference to new snippet.
}
/// <summary>Creates new snippet that duplicates the given snippet, except
/// that its is a given a new unique name and the given title.</summary>
/// <param name="Snippet">TSnippet [in] Snippet to be duplicated.</param>
/// <param name="Title">string [in] Title of duplicated snippet.</param>
/// <return>TSnippet. Duplicated snippet.</return>
/// <remarks>Method of IDatabaseEdit.</remarks>
function DuplicateSnippet(const Snippet: TSnippet; const Title: string):
TSnippet;
function CreateTempSnippet(const Data: TSnippetEditData;
const Name: string = ''): TSnippet; overload;
{Creates a new temporary snippet without adding it to the Snippets
object's snippets list. The new instance may not be added to the
Snippets object.
@param Data [in] Record storing new snippet's properties and references.
@param Name [in] Required snippet name. If ommitted then the snippet is
created with a new, unique, name.
@return Reference to new snippet.
}
function CreateTempSnippet(const Snippet: TSnippet): TSnippet; overload;
{Creates a new temporary copy of a snippet without adding it to the
Snippets object's snippets list. The new instance may not be added to the
Snippets object.
@param Snippet [in] Snippet to be copied.
@return Reference to new snippet.
}
procedure DeleteSnippet(const Snippet: TSnippet);
{Deletes a snippet from the database.
@param Snippet [in] Snippet to be deleted.
}
end;
function Database: TDatabase;
begin
Result := TDatabase.Instance;
end;
function _Database: _IDatabase;
{Returns singleton instance of object that encapsulates the snippets database.
@return Singleton object.
}
begin
if not Assigned(_PvtDatabase) then
_PvtDatabase := _TDatabase.Create;
Result := _PvtDatabase;
end;
{ _TDatabase }
procedure _TDatabase.AddChangeEventHandler(const Handler: TNotifyEventInfo);
{Adds a change event handler to list of listeners.
@param Handler [in] Event handler to be added.
}
begin
fChangeEvents.AddHandler(Handler);
end;
function _TDatabase.AddSnippet(Data: TSnippetEditData): TSnippet;
{Adds a new snippet to the database.
@param Data [in] Record storing new snippet's properties and references.
@return Reference to new snippet.
}
resourcestring
// Error message
sNameExists = 'Snippet "%s" already exists in database';
var
NewSnippetID: TSnippetID;
begin
Result := nil; // keeps compiler happy
TriggerEvent(evChangeBegin);
try
NewSnippetID := TSnippetID.Create(UniqueSnippetName);
// Check if snippet with same name exists in database: error if so
if Database.__SnippetsTable.Contains(NewSnippetID) then
raise ECodeSnip.CreateFmt(sNameExists, [NewSnippetID.ToString]);
CleanUpRefs(Data.Refs);
Result := InternalAddSnippet(NewSnippetID, Data);
Query.Update;
TriggerEvent(evSnippetAdded, Result);
finally
Database.__Updated := True;
TriggerEvent(evChangeEnd);
end;
end;
procedure _TDatabase.CleanUpRefs(var Refs: TSnippetReferences);
function CleanSnippetList(const SnipList: ISnippetIDList): ISnippetIDList;
var
SnippetID: TSnippetID;
begin
Result := TSnippetIDList.Create;
for SnippetID in SnipList do
if Database.__SnippetsTable.Contains(SnippetID) then
Result.Add(SnippetID);
end;
begin
Refs.RequiredSnippets := CleanSnippetList(Refs.RequiredSnippets);
Refs.XRefs := CleanSnippetList(Refs.XRefs);
end;
constructor _TDatabase.Create;
{Constructor. Sets up new empty object.
}
begin
inherited Create;
fChangeEvents := TMultiCastEvents.Create(Self);
end;
function _TDatabase.CreateTempSnippet(const Snippet: TSnippet): TSnippet;
{Creates a new temporary copy of a snippet without adding it to the
Snippets object's snippets list. The new instance may not be added to the
Snippets object.
@param Snippet [in] Snippet to be copied.
@return Reference to new snippet.
}
begin
Assert(Assigned(Snippet), ClassName + '.CreateTempSnippet: Snippet is nil');
Result := TSnippet.Create(Snippet.ID);
Result.SetProps(Snippet.GetProps);
Result.SetCreated(TUTCDateTime.Now);
Result.SetModified(Result.GetCreated);
Result.UpdateRefs(MakeValidRefs(Snippet.GetReferences));
end;
function _TDatabase.CreateTempSnippet(const Data: TSnippetEditData;
const Name: string = ''): TSnippet;
{Creates a new temporary snippet without adding it to the Snippets object's
snippets list. The new instance may not be added to the Snippets object.
@param Data [in] Record storing new snippet's properties and references.
@param Name [in] Required snippet name. If ommitted then the snippet is
created with a new, unique, name.
@return Reference to new snippet.
}
begin
Result := TSnippet.Create(
TSnippetID.Create(StrIf(Name <> '', Name, UniqueSnippetName))
);
Result.SetProps(Data.Props);
Result.SetCreated(TUTCDateTime.Now);
Result.SetModified(Result.GetCreated);
Result.UpdateRefs(MakeValidRefs(Data.Refs));
end;
procedure _TDatabase.DeleteSnippet(const Snippet: TSnippet);
{Deletes a snippet from the database.
@param Snippet [in] Snippet to be deleted.
}
var
Dependent: TSnippetID; // each snippet that depends on Snippet
Dependents: ISnippetIDList; // list of dependent snippets
Referrer: TSnippetID; // each snippet that cross references Snippet
Referrers: ISnippetIDList; // list of referencing snippets
begin
Assert(Database.__SnippetsTable.Contains(Snippet.ID),
ClassName + '.DeleteSnippet: Snippet is not in the database');
TriggerEvent(evChangeBegin);
TriggerEvent(evBeforeSnippetDelete, Snippet);
// Get list of referencing and dependent snippets
Dependents := nil;
Referrers := nil;
try
Dependents := Database.GetDependentsOf(Snippet.ID);
Referrers := Database.GetReferrersTo(Snippet.ID);
// TODO: scan all snippets and remove references that match snippet ID
// Delete snippet for XRef or Depends list of referencing snippets
for Referrer in Referrers do
Database.__SnippetsTable.Get(Referrer).XRefs.Remove(Snippet.ID);
for Dependent in Dependents do
Database.__SnippetsTable.Get(Dependent).RequiredSnippets.Remove(
Snippet.ID
);
// Delete snippet itself
InternalDeleteSnippet(Snippet);
Query.Update;
finally
Database.__Updated := True;
TriggerEvent(evSnippetDeleted);
TriggerEvent(evChangeEnd);
end;
end;
destructor _TDatabase.Destroy;
{Destructor. Tidies up and tears down object.
}
begin
fChangeEvents.Free;
inherited;
end;
function _TDatabase.DuplicateSnippet(const Snippet: TSnippet;
const Title: string): TSnippet;
var
Data: TSnippetEditData;
begin
Data := Snippet.GetEditData;
Data.Props.Title := Title;
Result := AddSnippet(Data);
end;
function _TDatabase.GetEditableSnippetInfo(
const Snippet: TSnippet): TSnippetEditData;
{Provides details of all a snippet's data (properties and references) that may
be edited.
@param Snippet [in] Snippet for which data is required. May be nil in which
case a blank record is returned.
@return Required data.
}
begin
if Assigned(Snippet) then
Result := Snippet.GetEditData
else
Result.Init;
end;
function _TDatabase.InternalAddSnippet(const SnippetID: TSnippetID;
const Data: TSnippetEditData): TSnippet;
{Adds a new snippet to the database. Assumes snippet not already in the
database.
@param SnippetID [in] ID of new snippet.
@param Data [in] Properties and references of new snippet.
@return Reference to new snippet object.
}
begin
Result := TSnippet.Create(SnippetID);
Result.SetProps(Data.Props);
Result.SetCreated(TUTCDateTime.Now);
Result.SetModified(Result.GetCreated);
Result.UpdateRefs(MakeValidRefs(Data.Refs));
// ensure any unknown tags are added to set of all known tags
Database.__AllTags.Include(Result.Tags);
Database.__SnippetsTable.Add(Result);
end;
procedure _TDatabase.InternalDeleteSnippet(const Snippet: TSnippet);
{Deletes a snippet from the database.
@param Snippet [in] Snippet to delete from database.
}
begin
// Don't remove snippets tags from tag list - leave them there
// Delete from "master" list: this frees Snippet
Database.__SnippetsTable.Delete(Snippet.ID);
end;
function _TDatabase.Lookup(const SnippetID: TSnippetID): TSnippet;
begin
if not TryLookup(SnippetID, Result) then
raise EBug.Create(ClassName + '.Lookup: SnippetID not found in database');
end;
function _TDatabase.MakeValidRefs(const Refs: TSnippetReferences):
TSnippetReferences;
begin
Result.Assign(Refs);
CleanUpRefs(Result);
end;
procedure _TDatabase.RemoveChangeEventHandler(const Handler: TNotifyEventInfo);
{Removes a change event handler from list of listeners.
@param Handler [in] Handler to remove from list.
}
begin
fChangeEvents.RemoveHandler(Handler);
end;
procedure _TDatabase.TriggerEvent(const Kind: TDatabaseChangeEventKind;
const Info: TObject);
{Triggers a change event. Notifies all registered listeners.
@param Kind [in] Kind of event.
@param Info [in] Reference to any further information for event. May be nil.
}
var
EvtInfo: IDatabaseChangeEventInfo; // event information object
begin
EvtInfo := TEventInfo.Create(Kind, Info);
fChangeEvents.TriggerEvents(EvtInfo);
end;
function _TDatabase.TryLookup(const SnippetID: TSnippetID;
out Snippet: TSnippet): Boolean;
begin
Result := Database.__SnippetsTable.Contains(SnippetID);
if Result then
Snippet := Database.__SnippetsTable.Get(SnippetID);
end;
function _TDatabase.UniqueSnippetName: string;
begin
repeat
Result := 'Snippet' + TUniqueID.Generate;
until not Database.__SnippetsTable.Contains(TSnippetID.Create(Result));
end;
procedure _TDatabase.UpdateSnippet(const Snippet: TSnippet;
Data: TSnippetEditData);
{Updates a defined snippet's properties and references using provided data.
@param Snippet [in] Snippet to be updated.
@param Data [in] Record containing revised data.
}
begin
TriggerEvent(evChangeBegin);
TriggerEvent(evBeforeSnippetChange, Snippet);
try
CleanUpRefs(Data.Refs);
Snippet.Update(Data);
Snippet.SetModified(TUTCDateTime.Now);
// ensure any new, unknown, tags are added to set of all tags
Database.__AllTags.Include(Snippet.Tags);
Query.Update;
TriggerEvent(evSnippetChanged, Snippet);
finally
Database.__Updated := True;
TriggerEvent(evChangeEnd);
end;
end;
{ TSnippets.TEventInfo }
constructor _TDatabase.TEventInfo.Create(const Kind: TDatabaseChangeEventKind;
const Info: TObject);
{Constructor. Creates an event information object.
@param Kind [in] Kind of event.
@param Info [in] Reference to further information about the event. May be
nil if event doesn't have additional information.
}
begin
inherited Create;
fKind := Kind;
fInfo := Info;
end;
function _TDatabase.TEventInfo.GetInfo: TObject;
{Gets additional information about event.
@return Object that provides required information.
}
begin
Result := fInfo;
end;
function _TDatabase.TEventInfo.GetKind: TDatabaseChangeEventKind;
{Gets kind (type) of event.
@return Event kind.
}
begin
Result := fKind;
end;
{ TDatabase }
procedure TDatabase.Finalize;
begin
fSnippetsTable.Free;
inherited;
end;
procedure TDatabase.FlagUpdate;
begin
fDirty := True;
fLastModified := TUTCDateTime.Now;
end;
function TDatabase.GetAllSnippets: ISnippetIDList;
var
Snippet: TDBSnippet;
begin
Result := TSnippetIDList.Create(fSnippetsTable.Size);
for Snippet in fSnippetsTable do
Result.Add(Snippet.ID);
end;
function TDatabase.GetAllTags: ITagSet;
begin
Result := TTagSet.Create(fAllTags);
end;
function TDatabase.GetDependentsOf(const ASnippetID: TSnippetID):
ISnippetIDList;
var
Snippet: TDBSnippet;
begin
Result := TSnippetIDList.Create;
for Snippet in fSnippetsTable do
if (Snippet.GetID <> ASnippetID)
and Snippet.GetRequiredSnippets.Contains(ASnippetID) then
Result.Add(Snippet.GetID);
end;
class function TDatabase.GetInstance: TDatabase;
begin
if not Assigned(fInstance) then
fInstance := TDatabase.Create;
Result := fInstance;
end;
function TDatabase.GetReferrersTo(const ASnippetID: TSnippetID): ISnippetIDList;
var
Snippet: TDBSnippet; // references each snippet in database
begin
Result := TSnippetIDList.Create;
for Snippet in fSnippetsTable do
if (Snippet.GetID <> ASnippetID)
and Snippet.GetXRefs.Contains(ASnippetID) then
Result.Add(Snippet.GetID);
end;
procedure TDatabase.Initialize;
begin
inherited;
fSnippetsTable := TDBSnippetsTable.Create;
fAllTags := TTagSet.Create;
end;
function TDatabase.IsDirty: Boolean;
begin
Result := fDirty;
end;
function TDatabase.IsEmpty: Boolean;
begin
Result := fSnippetsTable.Size = 0;
end;
procedure TDatabase.Load;
var
Loader: IDatabaseLoader;
begin
fSnippetsTable.Clear; // TODO: decide if loaders should clear this table
Loader := TDatabaseIOFactory.CreateLoader;
Loader.Load(fSnippetsTable, fAllTags, fLastModified);
fDirty := False;
end;
function TDatabase.LookupEditableSnippet(const ASnippetID: TSnippetID):
IEditableSnippet;
var
Row: TDBSnippet;
begin
Row := fSnippetsTable.Get(ASnippetID);
Result := Row.CloneAsEditable;
end;
function TDatabase.LookupSnippet(const ASnippetID: TSnippetID): ISnippet;
var
Row: TDBSnippet;
begin
Row := fSnippetsTable.Get(ASnippetID);
Result := Row.CloneAsReadOnly;
end;
procedure TDatabase.Save;
var
Writer: IDatabaseWriter;
begin
Writer := TDatabaseIOFactory.CreateWriter;
Writer.Save(fSnippetsTable, fAllTags, fLastModified);
fDirty := False;
end;
function TDatabase.SelectSnippets(FilterFn: TDBFilterFn): ISnippetIDList;
var
Snippet: TDBSnippet;
begin
Result := TSnippetIDList.Create;
for Snippet in fSnippetsTable do
if FilterFn(Snippet.CloneAsReadOnly) then
Result.Add(Snippet.ID)
end;
function TDatabase.SnippetCount: Integer;
begin
Result := fSnippetsTable.Size;
end;
function TDatabase.SnippetExists(const ASnippetID: TSnippetID): Boolean;
begin
Result := fSnippetsTable.Contains(ASnippetID);
end;
function TDatabase.TryLookupEditableSnippet(const ASnippetID: TSnippetID;
out ASnippet: IEditableSnippet): Boolean;
begin
Result := fSnippetsTable.Contains(ASnippetID);
if Result then
ASnippet := fSnippetsTable.Get(ASnippetID).CloneAsEditable
else
ASnippet := nil;
end;
function TDatabase.TryLookupSnippet(const ASnippetID: TSnippetID;
out ASnippet: ISnippet): Boolean;
begin
Result := fSnippetsTable.Contains(ASnippetID);
if Result then
ASnippet := fSnippetsTable.Get(ASnippetID).CloneAsReadOnly
else
ASnippet := nil;
end;
procedure TDatabase.__SetDirty(Dirty: Boolean);
begin
if Dirty then
FlagUpdate
else
fDirty := False;
end;
initialization
finalization
// Free the singleton
_PvtDatabase := nil;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.