Skip to content

Commit 3fab0d3

Browse files
committed
Replace TUserDataProvider w/ TCollectionDataProvider
TUserDataProvider provided information about snippets & ctaegories from the user database / collection. TCollectionDataProvider generalises TUserDataProvider to provide information about snippets & categories from any given collection. TDatabase.Save was modified to use different TCollectionDataProvider instances for saving both the "main" and "user" collections. The previous reliance on the same TUserDataProvider instance for both user and main collections was a bug which had only not materialised because, as yet, the "main" collection does not have an associated saver object and so nothing is saved.
1 parent 86f63ff commit 3fab0d3

File tree

1 file changed

+96
-95
lines changed

1 file changed

+96
-95
lines changed

Diff for: Src/DB.UMain.pas

+96-95
Original file line numberDiff line numberDiff line change
@@ -54,33 +54,39 @@ interface
5454
evCategoryChanged // a category's properties have changed
5555
);
5656

57-
{
58-
IDBDataProvider:
59-
Interface supported by objects that provides data about the categories and
60-
snippets in the database.
61-
}
57+
/// <summary>Interface supported by objects that provide information about
58+
/// categories and snippets.</summary>
6259
IDBDataProvider = interface(IInterface)
6360
['{D2D57A0D-DB29-4012-891E-E817E0EED8C8}']
61+
62+
/// <summary>Retrieves all the properties of a category.</summary>
63+
/// <param name="Cat"><c>Category</c> [in] Category for which properties
64+
/// are requested.</param>
65+
/// <returns><c>TCategoryData</c>. Record containing the property data.
66+
/// </returns>
6467
function GetCategoryProps(const Cat: TCategory): TCategoryData;
65-
{Retrieves all the properties of a category.
66-
@param Cat [in] Category for which data is requested.
67-
@return Record containing property data.
68-
}
68+
69+
/// <summary>Retrieves names of all snippets that belong to a category.
70+
/// </summary>
71+
/// <param name="Cat"><c>Category</c> [in] Category for which snippet names
72+
/// are requested.</param>
73+
/// <returns><c>IStringList</c>. List of snippet names.</returns>
6974
function GetCategorySnippets(const Cat: TCategory): IStringList;
70-
{Retrieves names of all snippets that belong to a category.
71-
@param Cat [in] Category for which snippet names are requested.
72-
@return Required list of snippet names.
73-
}
75+
76+
/// <summary>Retrieves all the properties of a snippet.</summary>
77+
/// <param name="Snippet"><c>TSnippet</c> [in] Snippet for which properties
78+
/// are requested.</param>
79+
/// <returns><c>TSnippetData</c>. Record containing the property data.
80+
/// </returns>
7481
function GetSnippetProps(const Snippet: TSnippet): TSnippetData;
75-
{Retrieves all the properties of a snippet.
76-
@param Snippet [in] Snippet for which data is requested.
77-
@return Record containing property data.
78-
}
82+
83+
/// <summary>Retrieves information about all the references of a snippet.
84+
/// </summary>
85+
/// <param name="Snippet"><c>TSnippet</c> [in] Snippet for which references
86+
/// are requested.</param>
87+
/// <returns><c>TSnippetReferences</c>. Record containing references.
88+
/// </returns>
7989
function GetSnippetRefs(const Snippet: TSnippet): TSnippetReferences;
80-
{Retrieves information about all the references of a snippet.
81-
@param Snippet [in] Snippet for which information is requested.
82-
@return Record containing references.
83-
}
8490
end;
8591

8692
{
@@ -106,11 +112,8 @@ interface
106112
depends on Kind. May be nil}
107113
end;
108114

109-
{
110-
IDataItemFactory:
111-
Interface to factory object that creates snippet and category objects. For
112-
use by database loader objects.
113-
}
115+
/// <summary>Interface to factory object that creates snippet and category
116+
/// objects use by collection loader objects.</summary>
114117
IDBDataItemFactory = interface(IInterface)
115118
['{C6DD85BD-E649-4A90-961C-4011D2714B3E}']
116119

@@ -301,12 +304,10 @@ implementation
301304

302305
type
303306

304-
{
305-
TDBDataItemFactory:
306-
Class that can create category and snippet objects.
307-
}
307+
/// <summary>Class that can create category and snippet objects.</summary>.
308308
TDBDataItemFactory = class(TInterfacedObject, IDBDataItemFactory)
309309
public
310+
310311
/// <summary>Creates a new category object.</summary>
311312
/// <param name="CatID"><c>string</c> [in] ID of new category. Must be
312313
/// unique.</param>
@@ -540,44 +541,59 @@ TEventInfo = class(TInterfacedObject, IDatabaseChangeEventInfo)
540541
}
541542
end;
542543

543-
{
544-
TUserDataProvider:
545-
Class that provides data about the categories and snippets in the user-
546-
defined database.
547-
}
548-
TUserDataProvider = class(TInterfacedObject, IDBDataProvider)
544+
/// <summary>Class that provides data about the categories and snippets in
545+
/// a given collection.</summary>
546+
TCollectionDataProvider = class(TInterfacedObject, IDBDataProvider)
549547
strict private
550-
fSnippets: TSnippetList; // All snippets in the whole database
551-
fCategories: TCategoryList; // All categories in the whole database
548+
var
549+
fCollectionID: TCollectionID; // Collection on which to operate
550+
fSnippets: TSnippetList; // All snippets in the whole database
551+
fCategories: TCategoryList; // All categories in the whole database
552552
public
553-
constructor Create(const SnipList: TSnippetList;
553+
/// <summary>Object constructor. Sets up data provider.</summary>
554+
/// <param name="ACollectionID"><c>TCollectionID</c> [in] Collection for
555+
/// which to provide data.</param>
556+
/// <param name="SnipList"><c>TSnippetList</c> [in] List of all snippets
557+
/// in the database.</param>
558+
/// <param name="Categories"><c>TCategoryList</c> [in] List of all
559+
/// categories in the database.</param>
560+
constructor Create(const ACollectionID: TCollectionID;
561+
const SnipList: TSnippetList;
554562
const Categories: TCategoryList);
555-
{Constructor. Records list of all snippets and categories in both
556-
databases.
557-
@param SnipList [in] List of all snippets in whole database.
558-
@param Categories [in] List of all categories in whole database.
559-
}
560-
{ IDBDataProvider methods }
563+
564+
/// <summary>Retrieves all the properties of a category.</summary>
565+
/// <param name="Cat"><c>Category</c> [in] Category for which properties
566+
/// are requested.</param>
567+
/// <returns><c>TCategoryData</c>. Record containing the property data.
568+
/// </returns>
569+
/// <remarks>Method of <c>IDBDataProvider</c></remarks>
561570
function GetCategoryProps(const Cat: TCategory): TCategoryData;
562-
{Retrieves all the properties of a category.
563-
@param Cat [in] Category for which data is requested.
564-
@return Record containing property data.
565-
}
571+
572+
/// <summary>Retrieves names of all snippets from the collection that
573+
/// belong to a category.</summary>
574+
/// <param name="Cat"><c>Category</c> [in] Category for which snippet names
575+
/// are requested.</param>
576+
/// <returns><c>IStringList</c>. List of snippet names.</returns>
577+
/// <remarks>Method of <c>IDBDataProvider</c></remarks>
566578
function GetCategorySnippets(const Cat: TCategory): IStringList;
567-
{Retrieves names of all user-defined snippets that belong to a category.
568-
@param Cat [in] Category for which snippet names are requested.
569-
@return Required list of snippet names.
570-
}
579+
580+
/// <summary>Retrieves all the properties of a snippet.</summary>
581+
/// <param name="Snippet"><c>TSnippet</c> [in] Snippet for which properties
582+
/// are requested.</param>
583+
/// <returns><c>TSnippetData</c>. Record containing the property data.
584+
/// </returns>
585+
/// <remarks>Method of <c>IDBDataProvider</c></remarks>
571586
function GetSnippetProps(const Snippet: TSnippet): TSnippetData;
572-
{Retrieves all the properties of a snippet.
573-
@param Snippet [in] Snippet for which data is requested.
574-
@return Record containing property data.
575-
}
587+
588+
/// <summary>Retrieves information about all the references of a snippet.
589+
/// </summary>
590+
/// <param name="Snippet"><c>TSnippet</c> [in] Snippet for which references
591+
/// are requested.</param>
592+
/// <returns><c>TSnippetReferences</c>. Record containing references.
593+
/// </returns>
594+
/// <remarks>Method of <c>IDBDataProvider</c></remarks>
576595
function GetSnippetRefs(const Snippet: TSnippet): TSnippetReferences;
577-
{Retrieves information about all the references of a snippet.
578-
@param Snippet [in] Snippet for which information is requested.
579-
@return Record containing references.
580-
}
596+
581597
end;
582598

583599
function Database: IDatabase;
@@ -1030,15 +1046,12 @@ procedure TDatabase.Save;
10301046
{Saves user defined snippets and all categories to user database.
10311047
}
10321048
var
1033-
Provider: IDBDataProvider; // object that supplies info to writer
1049+
MainProvider, UserProvider: IDBDataProvider; // object that supplies info to writer
10341050
MainCollectionIdx, UserCollectionIdx: Integer;
10351051
Saver: IDataFormatSaver;
10361052
Collections: TCollections;
10371053
Collection: TCollection;
10381054
begin
1039-
// Create object that can provide required information about user database
1040-
Provider := TUserDataProvider.Create(fSnippets, fCategories);
1041-
10421055
Collections := TCollections.Instance;
10431056

10441057
{TODO: -cVault: The following code is a kludge to maintain compatibility with
@@ -1055,9 +1068,12 @@ procedure TDatabase.Save;
10551068
if MainCollectionIdx >= 0 then
10561069
begin
10571070
Collection := Collections[MainCollectionIdx];
1071+
MainProvider := TCollectionDataProvider.Create(
1072+
TCollectionID.__TMP__MainDBCollectionID, fSnippets, fCategories
1073+
);
10581074
Saver := TDatabaseIOFactory.CreateDBSaver(Collection);
10591075
if Assigned(Saver) then
1060-
Saver.Save(fSnippets, fCategories, Provider);
1076+
Saver.Save(fSnippets, fCategories, MainProvider);
10611077
end;
10621078

10631079
UserCollectionIdx := TCollections.Instance.IndexOfID(
@@ -1066,9 +1082,12 @@ procedure TDatabase.Save;
10661082
if UserCollectionIdx >= 0 then
10671083
begin
10681084
Collection := Collections[UserCollectionIdx];
1085+
UserProvider := TCollectionDataProvider.Create(
1086+
TCollectionID.__TMP__UserDBCollectionID, fSnippets, fCategories
1087+
);
10691088
Saver := TDatabaseIOFactory.CreateDBSaver(Collection);
10701089
if Assigned(Saver) then
1071-
Saver.Save(fSnippets, fCategories, Provider);
1090+
Saver.Save(fSnippets, fCategories, UserProvider);
10721091
end;
10731092

10741093
fUpdated := False;
@@ -1243,65 +1262,47 @@ function TDBDataItemFactory.CreateSnippet(const Name: string;
12431262
Result := TSnippetEx.Create(Name, ACollectionID, Props);
12441263
end;
12451264

1246-
{ TUserDataProvider }
1265+
{ TCollectionDataProvider }
12471266

1248-
constructor TUserDataProvider.Create(const SnipList: TSnippetList;
1249-
const Categories: TCategoryList);
1250-
{Constructor. Records list of all snippets and categories in both databases.
1251-
@param SnipList [in] List of all snippets in whole database.
1252-
@param Categories [in] List of all categories in whole database.
1253-
}
1267+
constructor TCollectionDataProvider.Create(const ACollectionID: TCollectionID;
1268+
const SnipList: TSnippetList; const Categories: TCategoryList);
12541269
begin
12551270
inherited Create;
1271+
fCollectionID := ACollectionID;
12561272
fSnippets := SnipList;
12571273
fCategories := Categories;
12581274
end;
12591275

1260-
function TUserDataProvider.GetCategoryProps(
1276+
function TCollectionDataProvider.GetCategoryProps(
12611277
const Cat: TCategory): TCategoryData;
1262-
{Retrieves all the properties of a category.
1263-
@param Cat [in] Category for which data is requested.
1264-
@return Record containing property data.
1265-
}
12661278
begin
12671279
Result.Desc := Cat.Description;
12681280
end;
12691281

1270-
function TUserDataProvider.GetCategorySnippets(
1282+
function TCollectionDataProvider.GetCategorySnippets(
12711283
const Cat: TCategory): IStringList;
1272-
{Retrieves names of all user-defined snippets that belong to a category.
1273-
@param Cat [in] Category for which snippet names are requested.
1274-
@return Required list of snippet names.
1275-
}
12761284
var
12771285
Snippet: TSnippet; // references each snippet in category
12781286
begin
12791287
Result := TIStringList.Create;
12801288
for Snippet in Cat.Snippets do
1281-
if Snippet.CollectionID <> TCollectionID.__TMP__MainDBCollectionID then
1289+
if Snippet.CollectionID = fCollectionID then
12821290
Result.Add(Snippet.Name);
12831291
end;
12841292

1285-
function TUserDataProvider.GetSnippetProps(
1293+
function TCollectionDataProvider.GetSnippetProps(
12861294
const Snippet: TSnippet): TSnippetData;
1287-
{Retrieves all the properties of a snippet.
1288-
@param Snippet [in] Snippet for which data is requested.
1289-
@return Record containing property data.
1290-
}
12911295
begin
12921296
Result := (Snippet as TSnippetEx).GetProps;
12931297
end;
12941298

1295-
function TUserDataProvider.GetSnippetRefs(
1299+
function TCollectionDataProvider.GetSnippetRefs(
12961300
const Snippet: TSnippet): TSnippetReferences;
1297-
{Retrieves information about all the references of a snippet.
1298-
@param Snippet [in] Snippet for which information is requested.
1299-
@return Record containing references.
1300-
}
13011301
begin
13021302
Result := (Snippet as TSnippetEx).GetReferences;
13031303
end;
13041304

1305+
13051306
initialization
13061307

13071308

0 commit comments

Comments
 (0)