Menu

[r4212]: / branches / experimental / Src / Database / Engine / CS.Database.Main.pas  Maximize  Restore  History

Download this file

221 lines (195 with data), 5.4 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
unit CS.Database.Main;
interface
uses
Generics.Collections,
CS.Database.Snippets,
CS.Database.Types,
CS.Database.Core.Lookups,
CS.Database.Core.SnippetsTable,
CS.Database.IO.Native,
CS.SourceCode.Languages,
CS.Utils.Dates,
UBaseObjects,
UExceptions;
type
TDatabase = class(TNoConstructObject)
strict private
class var
fSnippetsTable: TDBSnippetsTable;
fTagLookup: TDBLookup<TDBTag>;
fLanguageLookup: TDBLookup<TSourceCodeLanguageID>;
fLastModified: TUTCDateTime;
fDirty: Boolean;
class procedure FlagUpdate;
class procedure Clear;
class function DatabasePath: string;
public
class constructor Create;
class destructor Destroy;
class procedure Load;
class procedure Save;
class function NewSnippet: ISnippet;
class procedure InsertSnippet(ASnippet: ISnippet);
class procedure UpdateSnippet(ASnippet: ISnippet);
class procedure DeleteSnippet(const ASnippetID: TDBSnippetID);
class function SnippetExists(const ASnippetID: TDBSnippetID): Boolean;
class function GetReadOnlySnippet(const ASnippetID: TDBSnippetID;
const RequiredProps: TDBSnippetProps = []): IReadOnlySnippet;
class function GetSnippet(const ASnippetID: TDBSnippetID): ISnippet;
class function GetSnippetIDs(Filter: IDBFilter): IDBSnippetIDList;
class function IsDirty: Boolean;
end;
implementation
uses
SysUtils,
CS.Database.Exceptions;
{ TDatabase }
class procedure TDatabase.Clear;
begin
fSnippetsTable.Clear;
end;
class constructor TDatabase.Create;
begin
fSnippetsTable := TDBSnippetsTable.Create;
fTagLookup := TDBLookup<TDBTag>.Create(
TDBTag.TEqualityComparer.Create
);
fLanguageLookup := TDBLookup<TSourceCodeLanguageID>.Create(
TSourceCodeLanguageID.TEqualityComparer.Create
);
fLastModified := TUTCDateTime.CreateNull;
fDirty := False;
end;
class function TDatabase.DatabasePath: string;
begin
// TODO -cPRERELEASE: Replace this database path one from TAppInfo
Result := ExtractFilePath(ParamStr(0));
end;
class procedure TDatabase.DeleteSnippet(const ASnippetID: TDBSnippetID);
var
OldSnippet: TDBSnippet;
Tag: TDBTag;
begin
OldSnippet := fSnippetsTable.Get(ASnippetID);
for Tag in OldSnippet.GetTags do
fTagLookup.Delete(Tag, ASnippetID);
fLanguageLookup.Delete(OldSnippet.GetLanguageID, ASnippetID);
fSnippetsTable.Delete(ASnippetID);
FlagUpdate;
end;
class destructor TDatabase.Destroy;
begin
fLanguageLookup.Free;
fTagLookup.Free;
fSnippetsTable.Free;
end;
class procedure TDatabase.FlagUpdate;
begin
fLastModified := TUTCDateTime.Now.RoundToNearestSecond;
fDirty := True;
end;
class function TDatabase.GetReadOnlySnippet(const ASnippetID: TDBSnippetID;
const RequiredProps: TDBSnippetProps): IReadOnlySnippet;
var
Row: TDBSnippet;
begin
Row := fSnippetsTable.Get(ASnippetID);
Result := Row.CopyPartial(RequiredProps);
end;
class function TDatabase.GetSnippet(const ASnippetID: TDBSnippetID): ISnippet;
var
Row: TDBSnippet;
begin
Row := fSnippetsTable.Get(ASnippetID);
Result := Row.Copy;
end;
class function TDatabase.GetSnippetIDs(Filter: IDBFilter): IDBSnippetIDList;
var
Snippet: TDBSnippet;
begin
Result := TDBSnippetIDList.Create;
for Snippet in fSnippetsTable do
begin
if Filter.Match(
TPartialSnippet.Create(Snippet, Filter.RequiredProperties)
) then
Result.Add(Snippet.GetID);
end;
end;
class procedure TDatabase.InsertSnippet(ASnippet: ISnippet);
var
DBSnippet: TDBSnippet;
Tag: TDBTag;
begin
DBSnippet := TDBSnippet.CreateFrom(ASnippet);
fSnippetsTable.Add(DBSnippet);
FlagUpdate;
DBSnippet.SetModified(fLastModified);
for Tag in ASnippet.Tags do
fTagLookup.Add(Tag, ASnippet.ID);
fLanguageLookup.Add(ASnippet.LanguageID, ASnippet.ID);
end;
class function TDatabase.IsDirty: Boolean;
begin
Result := fDirty;
end;
class procedure TDatabase.Load;
begin
Clear;
// TODO: implement load process
// update last modification date to that from file
// clear and load snippets table
// clear and rebuild lookups
fDirty := False;
end;
class function TDatabase.NewSnippet: ISnippet;
begin
Result := TSnippet.CreateNew;
end;
class procedure TDatabase.Save;
var
Writer: TDBNativeWriter;
begin
if not fDirty then
Exit;
// TODO: backup database here
try
Writer := TDBNativeWriter.Create(DatabasePath);
try
Writer.Save(fSnippetsTable, fLastModified);
finally
Writer.Free;
end;
except
// TODO: restore database on exception here
raise;
end;
fDirty := False;
end;
class function TDatabase.SnippetExists(const ASnippetID: TDBSnippetID): Boolean;
begin
Result := fSnippetsTable.Contains(ASnippetID);
end;
class procedure TDatabase.UpdateSnippet(ASnippet: ISnippet);
var
OldSnippet: TDBSnippet;
UpdatedSnippet: TDBSnippet;
Tag: TDBTag;
begin
OldSnippet := fSnippetsTable.Get(ASnippet.ID);
UpdatedSnippet := TDBSnippet.CreateFrom(ASnippet);
// Update snippets table
fSnippetsTable.Update(UpdatedSnippet);
FlagUpdate;
UpdatedSnippet.SetModified(fLastModified);
// Update tags lookup
for Tag in OldSnippet.GetTags do
fTagLookup.Delete(Tag, ASnippet.ID);
for Tag in NewSnippet.GetTags do
fTagLookup.Add(Tag, ASnippet.ID);
// Update languages lookup
fLanguageLookup.Update(
OldSnippet.GetLanguageID, UpdatedSnippet.GetLanguageID, ASnippet.ID
);
end;
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.