Allow schema files that are missing checksums on the !!SCHEMAMATIC line.
[versaplex.git] / versaplexd / vxschemadiff.cs
blob22a81e885da11f4f7412e0ba65f7dd9d8b8129dc
1 /*
2 * Versaplex:
3 * Copyright (C)2007-2008 Versabanq Innovations Inc. and contributors.
4 * See the included file named LICENSE for license information.
5 */
6 using System;
7 using System.Linq;
8 using System.Text;
9 using System.Collections.Generic;
11 internal enum VxDiffType
13 Unchanged = '.',
14 Add = '+',
15 Remove = '-',
16 Change = '*'
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);
43 else
45 //this.Add(key, VxDiffType.Unchanged);
50 // Convert a set of diffs to a string of the form:
51 // + AddedEntry
52 // - RemovedEntry
53 // * ChangedEntry
54 // . UnchangedEntry
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);
66 return sb.ToString();
70 internal class SchemaTypeComparer: IComparer<string>
72 enum SchemaTypes
74 xmlschema = 100,
75 table = 200,
76 view = 300,
77 index = 400,
78 scalarfunction = 1100,
79 tablefunction = 1200,
80 procedure = 1300,
81 trigger = 1400
84 private int sort_order(string s)
86 string type, name;
87 VxSchema.ParseKey(s, out type, out name);
89 int retval;
90 bool ignore_case = true;
91 try
93 retval = Convert.ToInt32(Enum.Parse(typeof(SchemaTypes),
94 type, ignore_case));
96 catch (Exception)
98 retval = 9999;
100 return retval;
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;
110 else
111 return String.Compare(x, y);