3 * Copyright (C)2007-2008 Versabanq Innovations Inc. and contributors.
4 * See the included file named LICENSE for license information.
9 using System
.Collections
.Generic
;
11 internal enum VxDiffType
19 // Figures out what changes are needed to convert srcsums to goalsums.
21 // FIXME: It might be nicer in the long term to just implement
22 // IEnumerable<...> or IDictionary<...> ourselves, and defer to
23 // an internal member. But it's a lot of boilerplate code.
24 internal class VxSchemaDiff
: SortedList
<string, VxDiffType
>
26 public VxSchemaDiff(VxSchemaChecksums srcsums
,
27 VxSchemaChecksums goalsums
):
28 base(new SchemaTypeComparer())
30 List
<string> keys
= srcsums
.Keys
.Union(goalsums
.Keys
).ToList();
31 keys
.Sort(new SchemaTypeComparer());
32 foreach (string key
in keys
)
34 if (!srcsums
.ContainsKey(key
))
35 this.Add(key
, VxDiffType
.Add
);
36 else if (!goalsums
.ContainsKey(key
))
37 this.Add(key
, VxDiffType
.Remove
);
38 else if (!srcsums
[key
].Equals(goalsums
[key
]))
40 if (!this.ContainsKey(key
))
41 this.Add(key
, VxDiffType
.Change
);
45 //this.Add(key, VxDiffType.Unchanged);
50 // Convert a set of diffs to a string of the form:
55 // The leading characters are taken directly from the enum definition.
56 public override string ToString()
58 StringBuilder sb
= new StringBuilder();
59 // Estimate around 32 characters per entry. May be slightly off, but
60 // it'll be way better than the default of 16 for the whole thing.
61 sb
.Capacity
= 32 * this.Count
;
62 foreach (KeyValuePair
<string,VxDiffType
> p
in this)
64 sb
.AppendLine(((char)p
.Value
) + " " + p
.Key
);
70 internal class SchemaTypeComparer
: IComparer
<string>
78 scalarfunction
= 1100,
84 private int sort_order(string s
)
87 VxSchema
.ParseKey(s
, out type
, out name
);
90 bool ignore_case
= true;
93 retval
= Convert
.ToInt32(Enum
.Parse(typeof(SchemaTypes
),
103 public int Compare(string x
, string y
)
105 int sort_x
= sort_order(x
);
106 int sort_y
= sort_order(y
);
108 if (sort_x
!= sort_y
)
109 return sort_x
- sort_y
;
111 return String
.Compare(x
, y
);