summaryrefslogtreecommitdiff
path: root/fpcsrc/packages/fcl-db/src/datadict/fpdddiff.pp
blob: 86e74a8357a64df9758e940cd66c31e77c4221c5 (plain)
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
{
    This file is part of the Free Pascal run time library.
    Copyright (c) 2008 by the Free Pascal development team

    Data Dictionary diff mechanism, compare 2 data dictionaries.

    See the file COPYING.FPC, included in this distribution,
    for details about the copyright.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

  **********************************************************************}
unit fpdddiff;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, fpdatadict;

type

  TDiffKind = (DiffTables, DiffFields, DiffIndexes, DiffSequences, DiffDomains);
  TDiffKindSet = set of TDiffKind;

  TDifferenceType = (dtMissing, dtDifferent, dtSurplus);
  
const
  diffAll = [DiffTables, DiffFields, DiffIndexes, DiffSequences, DiffDomains];
  
type
  
  { TCustomDDDiffer }

  TCustomDDDiffer = class
  private
    FSourceDD: TFPdatadictionary;
    FTargetDD: TFPdatadictionary;
  protected
    procedure DomainDifference (DiffType: TDifferenceType; SourceDomain, TargetDomain: TDDDomainDef); virtual;
    procedure SequenceDifference (DiffType: TDifferenceType; SourceSequence, TargetSequence: TDDSequenceDef); virtual;
    procedure TableDifference (DiffType: TDifferenceType; SourceTable, TargetTable: TDDTableDef); virtual;
    procedure IndexDifference (DiffType: TDifferenceType; SourceIndex, TargetIndex: TDDIndexDef); virtual;
    procedure FieldDifference (DiffType: TDifferenceType; SourceField, TargetField: TDDFieldDef); virtual;
    procedure CompareTables (Kind: TDiffKindSet);
    procedure CompareTable (TableName: string; Kind: TDiffKindSet);
    procedure CompareFields (Source, Target: TDDFieldDefs; Kind: TDiffKindSet);
    procedure CompareField (Source, Target: TDDFieldDefs; Fieldname: string; Kind: TDiffKindSet);
    procedure CompareIndexes (Source, Target: TDDIndexDefs; Kind: TDiffKindSet);
    procedure CompareIndex (Source, Target: TDDIndexDefs; Indexname: string; Kind: TDiffKindSet);
    procedure CompareDomains (Kind: TDiffKindSet);
    procedure CompareDomain (Source, Target: TDDDomainDefs; DomainName: string; Kind: TDiffKindSet);
    procedure CompareSequences (Kind: TDiffKindSet);
    procedure CompareSequence (Source, Target: TDDSequenceDefs; SequenceName: string; Kind: TDiffKindSet);
  public
    procedure Compare (Kind: TDiffKindSet);
    property SourceDD : TFPdatadictionary read FSourceDD write FSourceDD;
    property TargetDD : TFPdatadictionary read FTargetDD write FTargetDD;
  end;
  
  EDataDictDiff = Class(EDataDict);
  
implementation

uses db;

resourcestring
  SErrMissingDatadict = 'Source and/or target datadictionary not assigned.';

{ TCustomDDDiffer }

procedure TCustomDDDiffer.DomainDifference(DiffType: TDifferenceType;
  SourceDomain, TargetDomain: TDDDomainDef);
begin

end;

procedure TCustomDDDiffer.SequenceDifference(DiffType: TDifferenceType;
  SourceSequence, TargetSequence: TDDSequenceDef);
begin

end;

procedure TCustomDDDiffer.TableDifference(DiffType: TDifferenceType;
  SourceTable, TargetTable: TDDTableDef);
begin
end;

procedure TCustomDDDiffer.IndexDifference(DiffType: TDifferenceType;
  SourceIndex, TargetIndex: TDDIndexDef);
begin
end;

procedure TCustomDDDiffer.FieldDifference(DiffType: TDifferenceType;
  SourceField, TargetField: TDDFieldDef);
begin
end;

procedure TCustomDDDiffer.CompareTables(Kind: TDiffKindSet);

var
  List : TStringlist;
  r : integer;

begin
  List := TStringlist.Create;
  try
    List.Duplicates:=dupIgnore;
    List.sorted := true;
    for r := 0 to SourceDD.Tables.Count-1 do
      List.Add (SourceDD.Tables[r].TableName);
    for r := 0 to TargetDD.Tables.Count-1 do
      List.Add (TargetDD.Tables[r].TableName);
    for r := 0 to List.count-1 do
      CompareTable (List[r], Kind);
  finally
    List.Free;
  end;
end;

procedure TCustomDDDiffer.CompareTable(TableName: string; Kind: TDiffKindSet);

var
  Src, Targ : TDDTableDef;
begin
  Src := FSourceDD.Tables.FindTable(TableName);
  Targ := FTargetDD.Tables.FindTable(TableName);
  if Not assigned (Targ) then
    begin
    if DiffTables in Kind then
      TableDifference (dtMissing, Src, nil);
    end
  else if not assigned (Src) then
    begin
    if DiffTables in Kind then
      TableDifference (dtSurplus, nil, Targ);
    end
  else
    begin  // table exists in source and target, compare fields and Indexes
    if DiffFields in Kind then
      CompareFields (Src.Fields, Targ.Fields, Kind);
    if DiffIndexes in Kind then
      CompareIndexes(Src.Indexes, Targ.Indexes, Kind);
    end;
end;

procedure TCustomDDDiffer.CompareFields(Source, Target: TDDFieldDefs;
  Kind: TDiffKindSet);
var
  FieldList : TStringlist;
  r : integer;
begin
  FieldList := TStringlist.Create;
  try
    FieldList.Duplicates := dupIgnore;
    FieldList.Sorted := true;
    for r := 0 to Source.Count-1 do
      FieldList.Add (Source[r].FieldName);
    for r := 0 to Target.Count-1 do
      FieldList.Add (Target[r].FieldName);
    for r := 0 to FieldList.count-1 do
      CompareField(Source, Target, FieldList[r], Kind);
  finally
    FieldList.Free;
  end;
end;

procedure TCustomDDDiffer.CompareField(Source, Target: TDDFieldDefs;
  Fieldname: string; Kind: TDiffKindSet);

  Function FieldTypesEqual(F1,F2 : TDDFieldDef) : boolean;

  begin
    Result:=(F1.FieldType=F2.FieldType);
  end;

var
  Src, Targ : TDDFieldDef;
begin
  Src := Source.FindField(FieldName);
  Targ := Target.FindField(FieldName);
  if not assigned (Targ) then
    FieldDifference(dtMissing, Src, nil)
  else if not assigned (Src) then
    FieldDifference(dtSurplus, nil, Targ)
  else if (Not FieldTypesEqual(Src,Targ))
          or (Src.required <> Targ.required)
          or (comparetext(Src.DomainName, Targ.DomainName) <> 0)
          or (comparetext(Src.DefaultExpression, Targ.DefaultExpression) <> 0)
          or ((Src.Size <> Targ.Size) and not (Src.Fieldtype in [ftBlob]))
          or (Src.Precision <> Targ.Precision) then
    FieldDifference(dtDifferent, Src, Targ)
end;

procedure TCustomDDDiffer.CompareIndexes(Source, Target: TDDIndexDefs;
  Kind: TDiffKindSet);
var
  IndexList : TStringlist;
  r : integer;
begin
  IndexList := TStringlist.Create;
  try
    IndexList.Duplicates := dupIgnore;
    IndexList.Sorted := true;
    for r := 0 to Source.Count-1 do
      IndexList.Add (Source[r].IndexName);
    for r := 0 to Target.Count-1 do
      IndexList.Add (Target[r].IndexName);
    for r := 0 to IndexList.count-1 do
      CompareIndex(Source, Target, IndexList[r], Kind);
  finally
    IndexList.Free;
  end;
end;

procedure TCustomDDDiffer.CompareIndex(Source, Target: TDDIndexDefs;
  Indexname: string; Kind: TDiffKindSet);
var
  Src, Targ : TDDIndexDef;
begin
  Src := Source.FindIndex(IndexName);
  Targ := Target.FindIndex(IndexName);
  if not assigned (Targ) then
    IndexDifference(dtMissing, Src, nil)
  else if not assigned (Src) then
    IndexDifference(dtSurplus, nil, Targ)
  else if (CompareText(Src.Expression,Targ.Expression) <> 0) or
          (CompareText(Src.Fields,Targ.Fields) <> 0) or
          (Src.Options <> Targ.Options) or
          (CompareText(Src.DescFields,Targ.DescFields) <> 0) or
          (CompareText(Src.CaseInsFields,Targ.CaseInsFields) <> 0) then
    IndexDifference(dtDifferent, Src, Targ)
end;

procedure TCustomDDDiffer.CompareDomains(Kind: TDiffKindSet);

Var
  List : TStringList;
  R : Integer;

begin
  List := TStringlist.Create;
  try
    List.Duplicates:=dupIgnore;
    List.sorted := true;
    for r := 0 to SourceDD.Domains.Count-1 do
      List.Add (SourceDD.Domains[r].DomainName);
    for r := 0 to TargetDD.Domains.Count-1 do
      List.Add (TargetDD.Domains[r].DomainName);
    for r := 0 to List.count-1 do
      CompareDomain (SourceDD.Domains,TargetDD.Domains,List[r], Kind);
  finally
    List.Free;
  end;
end;

procedure TCustomDDDiffer.CompareDomain(Source, Target: TDDDomainDefs;
  DomainName: string; Kind: TDiffKindSet);

var
  Src,Targ : TDDDomainDef;

begin
  Src := Source.FindDomain(DomainName);
  Targ := Target.FindDomain(DomainName);
  if not assigned (Targ) then
    DomainDifference(dtMissing, Src, nil)
  else if not assigned (Src) then
    DomainDifference(dtSurplus, nil, Targ)
  else if (Src.FieldType<>Targ.FieldType) or
          (Src.Required<>Targ.Required) or
          (Src.Precision<>Targ.Precision) or
          (Src.Size<>Targ.Size) then
    DomainDifference(dtDifferent, Src, Targ)
end;

procedure TCustomDDDiffer.CompareSequences(Kind: TDiffKindSet);

Var
  List : TStringList;
  R : Integer;

begin
  List := TStringlist.Create;
  try
    List.Duplicates:=dupIgnore;
    List.sorted := true;
    for r := 0 to SourceDD.Sequences.Count-1 do
      List.Add (SourceDD.Sequences[r].SequenceName);
    for r := 0 to TargetDD.Sequences.Count-1 do
      List.Add (TargetDD.Sequences[r].SequenceName);
    for r := 0 to List.count-1 do
      CompareSequence (SourceDD.Sequences,TargetDD.Sequences,List[r], Kind);
  finally
    List.Free;
  end;
end;

procedure TCustomDDDiffer.CompareSequence(Source, Target: TDDSequenceDefs;
  SequenceName: string; Kind: TDiffKindSet);

var
  Src,Targ : TDDSequenceDef;

begin
  Src := Source.FindSequence(SequenceName);
  Targ := Target.FindSequence(SequenceName);
  if not assigned (Targ) then
    SequenceDifference(dtMissing, Src, nil)
  else if not assigned (Src) then
    SequenceDifference(dtSurplus, nil, Targ)
  else if (Src.StartValue<>Targ.StartValue) or
          (Src.Increment<>Targ.Increment) then
    SequenceDifference(dtDifferent, Src, Targ)
end;

procedure TCustomDDDiffer.Compare (Kind: TDiffKindSet);
begin
  if not assigned (FSourceDD) or not assigned (FTargetDD) then
    raise EDataDictDiff.Create(SErrMissingDatadict);
  CompareTables (Kind);
end;

end.