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
| ///////////////////////////////////////////////////////////////////////////
// //
// Copyright (c) 2015 by Charta Software B.V. //
// All rights reserved //
// //
// Version: 1.7.0.83525 //
// Web site: https://pascal.chartasoftware.com/ //
// //
// This code and information are provided "as is" without warranty of //
// any kind. Dissemination of this information or reproduction of //
// this material is strictly forbidden unless prior written permission //
// is obtained from Charta Software B.V.. //
// //
///////////////////////////////////////////////////////////////////////////
unit Sql.Database.Logical.Branching.BranchSet.Manager;
interface
uses
Boolean,
Collection,
Collection.List,
Exception,
Integer,
Sql.Builder,
Sql.Database,
Sql.Database.Logical.Branching.BranchSet,
Sql.Database.Logical.Branching.Table,
Sql.View.Subquery,
Text;
type
{
Currently sometimes this code returns "Empty" BranchSets (e.g. they will have an ID but no actual branches. Check
code when relying on branches.
}
TBranchSetManager = class(TObject)
private
BranchSetTable: TBranchSetTable;
Database: TSqlDatabase;
procedure DeleteDatabaseBranchSetBranchID(const BranchID: TInteger; BranchSet: TBranchSet);
procedure DeleteDatabaseBranchSet(BranchSet: TBranchSet);
protected
function GetBranchSetsContainingBranches(BranchIds: TCollection<TInteger>): TList<TBranchSet>; virtual;
function GetBranchSetWithExactBranches(const BranchIDs: array of TInteger): TBranchSet;
procedure StoreBranchSet(BranchSet: TBranchSet);
public
constructor Create(Database: TSqlDatabase; BranchSetTable: TBranchSetTable);
function GetBranchSetsContainingBranchSet(BranchSet: TBranchSet): TList<TBranchSet>; virtual;
function GetBranchSetWithId(const BranchSetID: TInteger): TBranchSet;
function GetMergedBranchSet(BranchSet1: TBranchSet; BranchSet2: TBranchSet): TBranchSet;
function GetSubsets(BranchSet: TBranchSet): TList<TBranchSet>;
function SubtractBranchSet(BranchSet1: TBranchSet; BranchSet2: TBranchSet): TBranchSet;
function GetOrCreateBranchSetWithBranches(const BranchIDs: array of TInteger): TBranchSet; virtual;
function DeleteBranchFromBranchSet(const BranchID: TInteger; BranchSet: TBranchSet): TInteger; virtual;
function GetBranchSetsContainingAtMostAllButOneBranches(const BranchIDs: array of TInteger): TList<TBranchSet>;
procedure AddBranchToAllBranchSetsContainingBranch(const BranchToAdd: TInteger; const ContainingBranch: TInteger);
end;
TBranchSetMatchingCountView = class(TSqlSubqueryView)
public
Id: TSqlTableColumnBuilder;
Count: TSqlTableColumnBuilder;
constructor Create(BranchSetTable: TBranchSetTable; const BranchIds: array of TInteger; ViewName: TText; InSet: TBoolean); reintroduce; virtual;
end;
EInvalidBranchIdException = class(EException);
implementation
uses
Collection.List.Array_,
Integer._32,
Object_.List,
Sql,
Sql.Result;
{ TBranchSetManager }
procedure TBranchSetManager.AddBranchToAllBranchSetsContainingBranch(const BranchToAdd: TInteger; const ContainingBranch: TInteger);
var
Result: TSqlResult;
begin
Result := Database.Query(
InsertSelect(
BranchSetTable,
[
BranchSetTable.Id,
BranchSetTable.BranchID
],
Select(
[
BranchSetTable.Id,
BranchToAdd
],
[ BranchSetTable ],
[ BranchSetTable.BranchID = ContainingBranch ]
)
)
);
Result.Free();
end;
constructor TBranchSetManager.Create(Database: TSqlDatabase; BranchSetTable: TBranchSetTable);
begin
inherited Create();
Self.BranchSetTable := BranchSetTable;
Self.Database := Database;
end;
function TBranchSetManager.DeleteBranchFromBranchSet(const BranchID: TInteger; BranchSet: TBranchSet): TInteger;
var
OtherBranchIDs: TArray<TInteger>;
i: TInteger32;
Index: TInteger32;
NewBranchSet: TBranchSet;
begin
if BranchSet.Branches.Count = 1 then
begin
if BranchSet.Branches[0].Id <> BranchID then
raise EInvalidBranchIdException.Create('DeleteBranchFromBranchSet called with BranchID not in BranchSet');
DeleteDatabaseBranchSet(BranchSet);
Result := -1;
end
else
begin
SetLength(OtherBranchIDs, BranchSet.Branches.Count - 1);
Index := 0;
for i := 0 to BranchSet.Branches.Count - 1 do
begin
if BranchSet.Branches[i].Id <> BranchID then
begin
OtherBranchIDs[Index] := BranchSet.Branches[i].Id;
Inc(Index);
end;
end;
if (Index <> (BranchSet.Branches.Count - 1)) then
raise EInvalidBranchIdException.Create(
'Branch with ID '
+ BranchID.ToText()
+ ' not found in branch set with id '
+ BranchSet.Id.ToText()
);
NewBranchSet := GetBranchSetWithExactBranches(OtherBranchIDs);
if NewBranchSet <> nil then
begin
Result := NewBranchSet.Id;
DeleteDatabaseBranchSet(BranchSet);
NewBranchSet.Free();
end
else
begin
DeleteDatabaseBranchSetBranchID(BranchID, BranchSet);
Result := BranchSet.Id;
end;
end;
end;
procedure TBranchSetManager.DeleteDatabaseBranchSet(BranchSet: TBranchSet);
begin
Database.Query(Delete([BranchSetTable], [BranchSetTable.Id = BranchSet.Id]));
end;
procedure TBranchSetManager.DeleteDatabaseBranchSetBranchID(const BranchID: TInteger; BranchSet: TBranchSet);
begin
Database.Query(Delete([BranchSetTable], [BranchSetTable.Id = BranchSet.Id, BranchSetTable.BranchID = BranchID]));
end;
function TBranchSetManager.GetBranchSetsContainingAtMostAllButOneBranches(const BranchIDs: array of TInteger): TList<TBranchSet>;
var
SqlResult: TSqlResult;
MatchingCount: TBranchSetMatchingCountView;
begin
MatchingCount := TBranchSetMatchingCountView.Create(BranchSetTable, BranchIDs, 'MatchingCount', True);
SqlResult := Database.Query(
Select(
[ MatchingCount.Id ],
[ MatchingCount ],
[
MatchingCount.Count > 0,
MatchingCount.Count < System.Length(BranchIDs)
]
)
);
Result := TObjectList<TBranchSet>.Create();
while SqlResult.FetchRow() do
Result.Add(GetBranchSetWithId(SqlResult.ReadInteger()));
SqlResult.Free();
MatchingCount.Free();
end;
function TBranchSetManager.GetBranchSetsContainingBranches(BranchIds: TCollection<TInteger>): TList<TBranchSet>;
var
SqlResult: TSqlResult;
Subquery: TBranchSetMatchingCountView;
begin
Subquery := TBranchSetMatchingCountView.Create(BranchSetTable, BranchIDs.ToArray(), 'BranchMatchCounts', True);
SqlResult := Database.Query(
Select(
[ Subquery.Id ],
[ Subquery ],
[ Subquery.Count >= BranchIds.Count ]
)
);
Result := TObjectList<TBranchSet>.Create();
while SqlResult.FetchRow() do
Result.Add(GetBranchSetWithId(SqlResult.ReadInteger()));
Subquery.Free();
SqlResult.Free();
end;
function TBranchSetManager.GetBranchSetsContainingBranchSet(BranchSet: TBranchSet): TList<TBranchSet>;
var
BranchIds: TList<TInteger>;
Branch: TBranch;
begin
BranchIds := TArrayList<TInteger>.Create();
for Branch in BranchSet.Branches do
BranchIds.Add(Branch.Id);
Result := GetBranchSetsContainingBranches(BranchIDs);
BranchIds.Free();
end;
function TBranchSetManager.GetBranchSetWithId(const BranchSetID: TInteger): TBranchSet;
var
SqlResult: TSqlResult;
begin
SqlResult := Database.Query(
Select(
[ BranchSetTable.BranchID ],
[ BranchSetTable ],
[ BranchSetTable.Id = BranchSetID ]
)
);
Result := TBranchSet.Create(BranchSetID);
while SqlResult.FetchRow() do
Result.Branches.Add(TBranch.Create(SqlResult.ReadInteger()));
SqlResult.Free();
end;
function TBranchSetManager.GetMergedBranchSet(BranchSet1, BranchSet2: TBranchSet): TBranchSet;
var
MergedBranchIDs: TArray<TInteger>;
FinalMergedBranchIDs: TArray<TInteger>;
SetSize: TInteger32;
i: TInteger32;
j: TInteger32;
begin
SetLength(MergedBranchIDs, BranchSet1.Branches.Count + BranchSet2.Branches.Count);
SetSize := 0;
// Merge the two sets without duplicates (assumption: there are no dups in the individual sets
for i := 0 to BranchSet1.Branches.Count - 1 do
begin
MergedBranchIDs[SetSize] := BranchSet1.Branches[i].Id;
Inc(SetSize);
end;
for i := 0 to BranchSet2.Branches.Count - 1 do
begin
j := 0;
while (MergedBranchIDs[j] <> BranchSet2.Branches[i].Id) and (j < BranchSet1.Branches.Count) do
Inc(j);
if j = BranchSet1.Branches.Count then
begin
MergedBranchIDs[SetSize] := BranchSet2.Branches[i].Id;
Inc(SetSize);
end;
end;
SetLength(FinalMergedBranchIDs, SetSize);
for i := 0 to SetSize - 1 do
FinalMergedBranchIDs[i] := MergedBranchIDs[i];
Result := GetOrCreateBranchSetWithBranches(FinalMergedBranchIDs);
end;
function TBranchSetManager.GetSubsets(BranchSet: TBranchSet): TList<TBranchSet>;
var
NonMatchingCount: TBranchSetMatchingCountView;
BranchSetsTable: TBranchSetTable;
BranchIDs: TArray<TInteger>;
SqlResult: TSqlResult;
i: TInteger32;
begin
Result := TObjectList<TBranchSet>.Create();
BranchSetsTable := BranchSetTable.AliasTable('AllBranchSets');
SetLength(BranchIDs, BranchSet.Branches.Count);
for i := 0 to BranchSet.Branches.Count - 1 do
BranchIDs[i] := BranchSet.Branches[i].Id;
NonMatchingCount := TBranchSetMatchingCountView.Create(BranchSetTable, BranchIDs, 'NonMatching', False);
SqlResult := Database.Query(
Select(
[ BranchSetsTable.Id ],
[ LeftJoin(BranchSetsTable, NonMatchingCount, [ BranchSetsTable.Id = NonMatchingCount.Id ]) ],
[ OrAll( [ NonMatchingCount.Count = 0, NonMatchingCount.Count.IsNull ]) ]
)
);
while SqlResult.FetchRow() do
Result.Add(TBranchSet.Create(SqlResult.ReadInteger()));
SqlResult.Free();
NonMatchingCount.Free();
BranchSetsTable.Free();
end;
{
Inserts the specified BranchSet into the database and assigns a new branchset id to it.
}
procedure TBranchSetManager.StoreBranchSet(BranchSet: TBranchSet);
var
SqlResult: TSqlResult;
i: TInteger32;
begin
SqlResult := Database.QuerySingleRow(Select( [ Sql.Maximum(BranchSetTable.Id) + 1 ], [BranchSetTable], []));
if SqlResult.ReadNull() then
BranchSet.Id := 1
else
BranchSet.Id := SqlResult.ReadInteger();
SqlResult.Free();
for i := 0 to BranchSet.Branches.Count - 1 do
Database.Query(InsertValues(BranchSetTable, [ BranchSetTable.Id, BranchSetTable.BranchID ], [BranchSet.Id, BranchSet.Branches[i].Id ]));
end;
function TBranchSetManager.SubtractBranchSet(BranchSet1: TBranchSet; BranchSet2: TBranchSet): TBranchSet;
var
BranchIDs: TArray<TInteger>;
FinalBranchIDs: TArray<TInteger>;
BranchCount: TInteger32;
i: TInteger32;
j: TInteger32;
begin
SetLength(BranchIDs, BranchSet1.Branches.Count);
BranchCount := 0;
for i := 0 to BranchSet1.Branches.Count - 1 do
begin
j := 0;
while (j < BranchSet2.Branches.Count) and (BranchSet2.Branches[j].Id <> BranchSet1.Branches[i].Id) do
Inc(j);
if j = BranchSet2.Branches.Count then
begin
BranchIDs[BranchCount] := BranchSet1.Branches[i].Id;
Inc(BranchCount);
end;
end;
SetLength(FinalBranchIDs, BranchCount);
for i := 0 to BranchCount - 1 do
FinalBranchIDs[i] := BranchIDs[i];
Result := GetOrCreateBranchSetWithBranches(FinalBranchIDs);
end;
function TBranchSetManager.GetOrCreateBranchSetWithBranches(const BranchIDs: array of TInteger): TBranchSet;
var
i: TInteger32;
begin
Result := GetBranchSetWithExactBranches(BranchIDs);
if Result = nil then
begin
Result := TBranchSet.Create(0);
for i := 0 to System.Length(BranchIDs) - 1 do
Result.Branches.Add(TBranch.Create(BranchIDs[i]));
StoreBranchSet(Result);
end;
end;
function TBranchSetManager.GetBranchSetWithExactBranches(const BranchIDs: array of TInteger): TBranchSet;
var
SqlResult: TSqlResult;
MatchingCount: TBranchSetMatchingCountView;
NonMatchingCount: TBranchSetMatchingCountView;
i: TInteger32;
begin
MatchingCount := TBranchSetMatchingCountView.Create(BranchSetTable, BranchIDs, 'MatchingCount', True);
NonMatchingCount := TBranchSetMatchingCountView.Create(BranchSetTable, BranchIDs, 'NonMatchingCount', False);
SqlResult := Database.Query(
Select(
[ MatchingCount.Id ],
[
LeftJoin(MatchingCount, NonMatchingCount, [ MatchingCount.Id = NonMatchingCount.Id ])
],
[
MatchingCount.Count = System.Length(BranchIDs),
OrAll([ NonMatchingCount.Count = 0, NonMatchingCount.Count.IsNull ])
]
)
);
if SqlResult.GetRowCount() = 0 then
Result := nil
else if SqlResult.GetRowCount() > 1 then
raise EException.Create('Multiple branchsets marching.')
else
begin
SqlResult.FetchRow();
Result := TBranchSet.Create(SqlResult.ReadInteger());
for i := 0 to System.Length(BranchIDs) - 1 do
Result.Branches.Add(TBranch.Create(BranchIDs[i]));
end;
MatchingCount.Free();
NonMatchingCount.Free();
SqlResult.Free();
end;
{ TBranchSetNestedView }
constructor TBranchSetMatchingCountView.Create(BranchSetTable: TBranchSetTable; const BranchIds: array of TInteger; ViewName: TText; InSet: TBoolean);
var
InSetExpression: TSqlExpressionBuilder;
begin
InSetExpression := Sql.InSet(BranchSetTable.BranchID, IntegerSet(BranchIds));
if not InSet then
InSetExpression := not InSetExpression;
inherited Create(
ViewName,
SelectGrouped(
[ BranchSetTable.Id, Alias('Count', nil, Sql.Count) ],
[ BranchSetTable ],
[ InSetExpression ],
[ BranchSetTable.Id ]
)
);
Id := Columns[0];
Count := Columns[1];
end;
end.
|