PageRenderTime 40ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/source/library/DatabaseCop/Program.cs

https://bitbucket.org/VahidN/interlace
C# | 284 lines | 200 code | 59 blank | 25 comment | 14 complexity | e18bbe9bfc47dfef9844e88537241f94 MD5 | raw file
  1. #region Using Directives and Copyright Notice
  2. // Copyright (c) 2007-2010, Computer Consultancy Pty Ltd
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are met:
  7. // * Redistributions of source code must retain the above copyright
  8. // notice, this list of conditions and the following disclaimer.
  9. // * Redistributions in binary form must reproduce the above copyright
  10. // notice, this list of conditions and the following disclaimer in the
  11. // documentation and/or other materials provided with the distribution.
  12. // * Neither the name of the Computer Consultancy Pty Ltd nor the
  13. // names of its contributors may be used to endorse or promote products
  14. // derived from this software without specific prior written permission.
  15. //
  16. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. // ARE DISCLAIMED. IN NO EVENT SHALL COMPUTER CONSULTANCY PTY LTD BE LIABLE
  20. // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  26. // DAMAGE.
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Data;
  30. using System.Data.SqlClient;
  31. using System.IO;
  32. using System.Reflection;
  33. using System.Text;
  34. using System.Text.RegularExpressions;
  35. using Interlace.Collections;
  36. using Interlace.DatabaseManagement;
  37. #endregion
  38. namespace DatabaseCop
  39. {
  40. class Program
  41. {
  42. static Regex _connectionStringExpression = new Regex("(([^:@]+):([^:@]+)@)?([^:@]+):([^:@]+)");
  43. static Regex _fields = new Regex("([^.(]|\\w+\\()(\\w+)\\.Fields\\(\\\"([a-zA-Z0-9_]+)\\\"\\)\\.Value");
  44. static void Main(string[] args)
  45. {
  46. DatabaseConnectionString connectionString = new DatabaseConnectionString();
  47. connectionString.ServerName = ".\\SQLEXPRESS";
  48. connectionString.DatabaseName = "Avance";
  49. connectionString.UseIntegratedAuthentication = false;
  50. connectionString.Username = "tmsplus";
  51. connectionString.Password = "cool";
  52. Database database;
  53. using (SqlConnection connection = new SqlConnection(connectionString.ToString()))
  54. {
  55. connection.Open();
  56. database = Database.FromConnection(connection);
  57. }
  58. Plan plan = new Plan(database);
  59. plan.AddPrecondition("InvoiceLineSource", "CourierJobId");
  60. plan.AddPrecondition("JobDriverEffort", "JobId");
  61. plan.AddPrecondition("Transfer", "JobNoteId");
  62. plan.AddPrecondition("Transfer", "WaypointId");
  63. Dependencies.CreateDependenciesGraph(database);
  64. }
  65. static void CodePatchMain(string[] args)
  66. {
  67. DatabaseConnectionString connectionString = new DatabaseConnectionString();
  68. connectionString.ServerName = "SERVER14";
  69. connectionString.DatabaseName = "RMTSv2";
  70. connectionString.UseIntegratedAuthentication = false;
  71. connectionString.Username = "tmsplus";
  72. connectionString.Password = "cool";
  73. Database database;
  74. using (SqlConnection connection = new SqlConnection(connectionString.ToString()))
  75. {
  76. connection.Open();
  77. database = Database.FromConnection(connection);
  78. }
  79. Dictionary<string, string> fieldMappings = GetColumnTypeMappings(database);
  80. Dictionary<string, string> functionMap = new Dictionary<string,string>();
  81. functionMap["char"] = "FieldCasts.FromChar";
  82. functionMap["nvarchar"] = "FieldCasts.FromNVarChar";
  83. functionMap["nchar"] = "FieldCasts.FromNChar";
  84. functionMap["float"] = "FieldCasts.FromFloat";
  85. functionMap["real"] = "FieldCasts.FromReal";
  86. functionMap["datetime"] = "FieldCasts.FromDateTime";
  87. functionMap["bit"] = "FieldCasts.FromBit";
  88. functionMap["varchar"] = "FieldCasts.FromVarChar";
  89. functionMap["text"] = "FieldCasts.FromText";
  90. functionMap["ntext"] = "FieldCasts.FromNText";
  91. functionMap["numeric"] = "FieldCasts.FromNumeric";
  92. functionMap["int"] = "FieldCasts.FromInt";
  93. functionMap["image"] = "FieldCasts.FromImage";
  94. functionMap["varbinary"] = "FieldCasts.FromVarBinary";
  95. functionMap["smallint"] = "FieldCasts.FromSmallInt";
  96. functionMap["tinyint"] = "FieldCasts.FromTinyInt";
  97. DirectoryInfo info = new DirectoryInfo(@"C:\Versioning\Haulage");
  98. FileInfo[] files = info.GetFiles("*.vb", SearchOption.AllDirectories);
  99. Set<string> unknownFields = new Set<string>();
  100. foreach (FileInfo file in files)
  101. {
  102. using (StreamReader reader = new StreamReader(file.FullName))
  103. {
  104. string entireFile = reader.ReadToEnd();
  105. _fields.Replace(entireFile, (MatchEvaluator)delegate(Match match)
  106. {
  107. Console.WriteLine(match.Value);
  108. string characterBeforeIdentifier = match.Groups[1].Value;
  109. string recordSetIdentifier = match.Groups[2].Value;
  110. string fieldName = match.Groups[3].Value;
  111. string converterName = "FieldCasts.Identity";
  112. if (fieldMappings.ContainsKey(fieldName.ToLower()))
  113. {
  114. string fieldType = fieldMappings[fieldName.ToLower()];
  115. converterName = functionMap[fieldType];
  116. }
  117. else
  118. {
  119. unknownFields.UnionUpdate(fieldName.ToLower());
  120. }
  121. string replacementText = string.Format("{0}{1}({2}.Fields(\"{3}\").Value)",
  122. characterBeforeIdentifier, converterName, recordSetIdentifier, fieldName);
  123. Console.WriteLine(" " + replacementText);
  124. return replacementText;
  125. });
  126. }
  127. }
  128. }
  129. static Dictionary<string, string> GetColumnTypeMappings(Database database)
  130. {
  131. Dictionary<string, string> fieldMappings = new Dictionary<string, string>();
  132. Set<string> ambiguousMappings = new Set<string>();
  133. foreach (Table table in database.Tables)
  134. {
  135. foreach (KeyValuePair<string, Column> pair in table.Columns)
  136. {
  137. string fieldNameLower = pair.Key.ToLower();
  138. if (fieldMappings.ContainsKey(fieldNameLower))
  139. {
  140. if (fieldMappings[fieldNameLower] != pair.Value.DataType)
  141. {
  142. ambiguousMappings.UnionUpdate(fieldNameLower);
  143. }
  144. }
  145. else
  146. {
  147. fieldMappings[fieldNameLower] = pair.Value.DataType;
  148. }
  149. }
  150. }
  151. foreach (string ambiguousMapping in ambiguousMappings)
  152. {
  153. fieldMappings.Remove(ambiguousMapping);
  154. }
  155. return fieldMappings;
  156. }
  157. static void OldMain(string[] args)
  158. {
  159. if (args.Length < 1)
  160. {
  161. Console.WriteLine(":(");
  162. return;
  163. }
  164. if (args[0] == "/dump")
  165. {
  166. if (args.Length < 2)
  167. {
  168. Console.WriteLine(":( :(");
  169. return;
  170. }
  171. DatabaseConnectionString connectionString = ParseConnectionString(args[1]);
  172. Database database;
  173. using (SqlConnection connection = new SqlConnection(connectionString.ToString()))
  174. {
  175. connection.Open();
  176. database = Database.FromConnection(connection);
  177. Dependencies.DumpSerialized(database, connection);
  178. }
  179. }
  180. if (args[0] == "/compare")
  181. {
  182. Dependencies.CompareInCurrentDirectory();
  183. }
  184. }
  185. private static DatabaseConnectionString ParseConnectionString(string argument)
  186. {
  187. Match match = _connectionStringExpression.Match(argument);
  188. DatabaseConnectionString connectionString = new DatabaseConnectionString();
  189. if (match.Groups[1].Success)
  190. {
  191. connectionString.Username = match.Groups[2].Value;
  192. connectionString.Password = match.Groups[3].Value;
  193. connectionString.UseIntegratedAuthentication = false;
  194. }
  195. else
  196. {
  197. connectionString.UseIntegratedAuthentication = true;
  198. }
  199. connectionString.ServerName = match.Groups[4].Value;
  200. connectionString.DatabaseName = match.Groups[5].Value;
  201. connectionString.ConnectionTimeout = 10;
  202. return connectionString;
  203. }
  204. static void CheckRules(Database database)
  205. {
  206. RuleVisitor visitor = new RuleVisitor();
  207. Assembly thisAssembly = Assembly.GetExecutingAssembly();
  208. PopulateRuleVisitorFromAssembly(visitor, thisAssembly);
  209. database.Visit(visitor);
  210. }
  211. private static void PopulateRuleVisitorFromAssembly(RuleVisitor visitor, Assembly assembly)
  212. {
  213. Type[] types = assembly.GetExportedTypes();
  214. foreach (Type type in types)
  215. {
  216. if (type.IsSubclassOf(typeof(Rule)) && type != typeof(Rule))
  217. {
  218. ConstructorInfo constructor = type.GetConstructor(new Type[0]);
  219. Rule rule = constructor.Invoke(new object[0]) as Rule;
  220. visitor.AddRule(rule);
  221. }
  222. }
  223. }
  224. }
  225. }