1 #region MIT license
2 //
3 // MIT license
4 //
5 // Copyright (c) 2007-2008 Jiri Moudry, Pascal Craponne
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining a copy
8 // of this software and associated documentation files (the "Software"), to deal
9 // in the Software without restriction, including without limitation the rights
10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 // copies of the Software, and to permit persons to whom the Software is
12 // furnished to do so, subject to the following conditions:
13 //
14 // The above copyright notice and this permission notice shall be included in
15 // all copies or substantial portions of the Software.
16 //
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 // THE SOFTWARE.
24 //
25 #endregion
26 
27 using System;
28 using System.Collections.Generic;
29 using System.IO;
30 using System.Linq;
31 using System.Xml.Serialization;
32 using DbLinq.Vendor;
33 using DbMetal;
34 
35 namespace DbMetal.Schema
36 {
37 #if !MONO_STRICT
38     public
39 #endif
40     class TableAlias
41     {
42         public class Renamings : INameAliases
43         {
44             [XmlAttribute("Name")]
45             public string Name { get; set; }
46 
47             [XmlAttribute("Class")]
48             public string Class { get; set; }
49 
50             [XmlElement("Renaming")]
51             public readonly List<Renaming> Arr = new List<Renaming>();
52 
GetAlias(string name)53             protected string GetAlias(string name)
54             {
55                 return (from r in Arr where r.old == name select r.@new).SingleOrDefault();
56             }
57 
GetTableTypeAlias(string table, string schema)58             public string GetTableTypeAlias(string table, string schema)
59             {
60                 return GetAlias(table);
61             }
62 
GetTableMemberAlias(string table, string schema)63             public string GetTableMemberAlias(string table, string schema)
64             {
65                 return null;
66             }
67 
GetColumnMemberAlias(string column, string table, string schema)68             public string GetColumnMemberAlias(string column, string table, string schema)
69             {
70                 return GetAlias(column);
71             }
72 
GetColumnForcedType(string column, string table, string schema)73             public string GetColumnForcedType(string column, string table, string schema)
74             {
75                 return null;
76             }
77 
GetColumnGenerated(string column, string table, string schema)78             public bool? GetColumnGenerated(string column, string table, string schema)
79             {
80                 return null;
81             }
82 
GetColumnAutoSync(string column, string table, string schema)83             public DbLinq.Schema.Dbml.AutoSync? GetColumnAutoSync(string column, string table, string schema)
84             {
85                 return null;
86             }
87 
GetDatabaseNameAlias(string databaseName)88             public string GetDatabaseNameAlias(string databaseName)
89             {
90                 return Name;
91             }
92 
GetClassNameAlias(string className)93             public string GetClassNameAlias(string className)
94             {
95                 return Class;
96             }
97         }
98 
99         public class Renaming
100         {
101             [XmlAttribute]
102             public string old;
103             [XmlAttribute]
104             public string @new;
105         }
106 
Load(string fileName)107         public static Renamings Load(string fileName)
108         {
109             using (var stream = File.OpenRead(fileName))
110             {
111                 var renamingsXmlSerializer = new XmlSerializer(typeof(Renamings));
112                 var renamings = (Renamings)renamingsXmlSerializer.Deserialize(stream);
113                 return renamings;
114             }
115         }
116 
Load(string fileName, Parameters parameters)117         public static IDictionary<string, string> Load(string fileName, Parameters parameters)
118         {
119             if (!System.IO.File.Exists(fileName))
120                 throw new ArgumentException("Renames file missing:" + parameters.Aliases);
121 
122             Console.WriteLine("Loading renames file: " + fileName);
123 
124             Renamings renamings = Load(parameters.Aliases);
125 
126             Dictionary<string, string> aliases = new Dictionary<string, string>();
127             foreach (Renaming renaming in renamings.Arr)
128             {
129                 aliases[renaming.old] = renaming.@new;
130             }
131             return aliases;
132         }
133 
Load(Parameters parameters)134         public static IDictionary<string, string> Load(Parameters parameters)
135         {
136             if (parameters.Aliases == null)
137                 return new Dictionary<string, string>();
138             return Load(parameters.Aliases, parameters);
139         }
140 
141     }
142 }