/BusinessManager/DatabaseManager.cs

# · C# · 233 lines · 205 code · 28 blank · 0 comment · 16 complexity · b4addb53b887029cab288d6d304f605f MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using DbDoc;
  6. using DbDoc.BusinessObjects;
  7. using DbDoc.Provider.Persistence;
  8. using DbDoc.Provider.Source;
  9. namespace DbDoc.BusinessManager
  10. {
  11. public static class DatabaseManager
  12. {
  13. private static List<string> includeDatabases = new List<string>();
  14. private static List<string> excludeDatabases = new List<string>();
  15. #region Properties
  16. public static string SourceConnectionInfo
  17. {
  18. get { return SourceProviderManager.Provider.ConnectionInfo; }
  19. }
  20. public static string PersistenceConnectionInfo
  21. {
  22. get { return PersistenceProviderManager.Provider.ConnectionInfo; }
  23. }
  24. public static string SourceStorageName
  25. {
  26. get { return SourceProviderManager.Provider.StorageName; }
  27. }
  28. public static string PersistenceStorageName
  29. {
  30. get { return PersistenceProviderManager.Provider.StorageName; }
  31. }
  32. public static string SourceExcludedDatabases
  33. {
  34. get { return string.Join(",",SourceProviderManager.Provider.ExcludedDatabasesList); }
  35. }
  36. #endregion
  37. public static IList<Database> GetDatabases(bool includeChildren)
  38. {
  39. IList<Database> databases = PersistenceProviderManager.Provider.GetDatabases();
  40. if (includeChildren)
  41. {
  42. foreach (Database db in databases)
  43. {
  44. db.Tables = GetTables(db);
  45. foreach (Table tbl in db.Tables)
  46. tbl.Columns = GetColumns(db, tbl);
  47. db.Procs = GetStoredProcs(db);
  48. }
  49. }
  50. return databases;
  51. }
  52. public static IList<Database> GetDatabases()
  53. {
  54. return GetDatabases(false);
  55. }
  56. public static IList<Table> GetTables(DbDoc.BusinessObjects.Database db)
  57. {
  58. return PersistenceProviderManager.Provider.GetTables(db);
  59. }
  60. public static IList<StoredProc > GetStoredProcs(DbDoc.BusinessObjects.Database db)
  61. {
  62. return PersistenceProviderManager.Provider.GetStoredProcs(db);
  63. }
  64. public static IList<Column > GetColumns(DbDoc.BusinessObjects.Database db, Table tbl)
  65. {
  66. return PersistenceProviderManager.Provider.GetColumns(db, tbl);
  67. }
  68. public static void SaveDbObject(DbObject dbobj)
  69. {
  70. PersistenceProviderManager.Provider.SaveDbObject(dbobj);
  71. }
  72. public static void UpdateDbObject(DbObject dbobj)
  73. {
  74. PersistenceProviderManager.Provider.UpdateDbObject(dbobj);
  75. }
  76. public static IEnumerable<Comment> GetCommentsHistory(DbObject obj)
  77. {
  78. return PersistenceProviderManager.Provider.GetCommentsHistory(obj);
  79. }
  80. #region Synchronization
  81. public static void Synchronize()
  82. {
  83. SynchronizeDatabases();
  84. }
  85. private static void SynchronizeDatabases()
  86. {
  87. bool includeSystemObjects = Boolean.Parse(PersistenceProviderManager.Provider.GetByKey(Constants.Settings.INCLUDE_SYSTEM_OBJECTS).Value.ToString());
  88. IList<string> serverDatabases = SourceProviderManager.Provider.GetServerDatabases();
  89. IList<Database> databases = PersistenceProviderManager.Provider.GetDatabases();
  90. IList<Database> deletedDatabases = new List<Database>();
  91. foreach (string sDb in serverDatabases)
  92. {
  93. bool found = false;
  94. foreach (Database db in databases)
  95. {
  96. if (string.Compare(db.Name, sDb) == 0)
  97. {
  98. found = true;
  99. break;
  100. }
  101. if (!serverDatabases.Contains(db.Name) && !deletedDatabases.Contains(db))
  102. deletedDatabases.Add(db);
  103. }
  104. if (!found && !excludeDatabases.Contains(sDb.Trim()))
  105. {
  106. Database newDb = new Database();
  107. newDb.Name = sDb;
  108. SaveDbObject(newDb);
  109. databases.Add(newDb);
  110. }
  111. }
  112. foreach (Database db in deletedDatabases)
  113. {
  114. db.Deleted = true;
  115. UpdateDbObject(db);
  116. }
  117. databases = PersistenceProviderManager.Provider.GetDatabases();
  118. foreach (Database db in databases)
  119. {
  120. SynchronizeTables(db);
  121. SynchronizeStoredProcs(db, includeSystemObjects);
  122. }
  123. }
  124. public static void SynchronizeTables(Database database)
  125. {
  126. IList<string> serverTables = SourceProviderManager.Provider.GetServerTables(database.Name);
  127. IList<Table> tables = GetTables(database);
  128. foreach (string stbl in serverTables)
  129. {
  130. bool found = false;
  131. foreach (Table tbl in tables)
  132. {
  133. if (string.Compare(tbl.Name, stbl) == 0)
  134. {
  135. found = true;
  136. break;
  137. }
  138. }
  139. if (!found)
  140. {
  141. Database newDb = new DbDoc.BusinessObjects.Database();
  142. Table newTable = new DbDoc.BusinessObjects.Table();
  143. newTable.Name = stbl;
  144. newTable.Parent = database;
  145. SaveDbObject(newTable);
  146. }
  147. }
  148. tables = GetTables(database);
  149. foreach (Table tbl in tables)
  150. SynchronizeColumns(database, tbl);
  151. }
  152. public static void SynchronizeColumns(Database database, Table tbl)
  153. {
  154. IList<string> serverColumns = SourceProviderManager.Provider.GetServerColumns(database.Name, tbl.Name);
  155. IList<Column> columns = GetColumns(database, tbl);
  156. foreach (string scol in serverColumns)
  157. {
  158. bool found = false;
  159. foreach (Column col in columns)
  160. {
  161. if (string.Compare(col.Name, scol) == 0)
  162. {
  163. found = true;
  164. break;
  165. }
  166. }
  167. if (!found)
  168. {
  169. Column newColumn = new DbDoc.BusinessObjects.Column();
  170. newColumn.Name = scol;
  171. newColumn.Parent = tbl;
  172. SaveDbObject(newColumn);
  173. }
  174. }
  175. }
  176. public static void SynchronizeViews(Database database) { }
  177. public static void SynchronizeStoredProcs(Database database, bool includeSystemObjects)
  178. {
  179. IList<string> serverProcs = SourceProviderManager.Provider.GetServerStoredProcs (database.Name, includeSystemObjects);
  180. IList<StoredProc> procs = GetStoredProcs (database);
  181. foreach (string sprocs in serverProcs)
  182. {
  183. bool found = false;
  184. foreach (StoredProc proc in procs)
  185. {
  186. if (string.Compare(proc.Name, sprocs) == 0)
  187. {
  188. found = true;
  189. break;
  190. }
  191. }
  192. if (!found)
  193. {
  194. Database newDb = new DbDoc.BusinessObjects.Database();
  195. StoredProc newProc = new DbDoc.BusinessObjects.StoredProc();
  196. newProc.Name = sprocs;
  197. newProc.Parent = database;
  198. SaveDbObject(newProc);
  199. }
  200. }
  201. }
  202. public static void SynchronizeFunctions(Database database) { }
  203. #endregion
  204. }
  205. }