PageRenderTime 58ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/DBSourceTools_root/DBSourceTools/DBSourceToolsLib/SysObjects/SysObjects.cs

#
C# | 1260 lines | 870 code | 195 blank | 195 comment | 113 complexity | c9c4e4c9da6df035e3a92b163a6f8839 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections;
  4. //using System.Linq;
  5. using System.Text;
  6. using System.Data.SqlClient;
  7. using DBConnectionLib;
  8. //using DBConnectionLib.Types;
  9. using Microsoft.SqlServer.Management.Smo; // 2005
  10. using Microsoft.SqlServer.Management.Sdk.Sfc; // 2008
  11. namespace DBSourceToolsLib.SysObjects
  12. {
  13. public class SysObjects //: Interfaces.IProgressEvent
  14. {
  15. public event EventHandler<ProgressEventArgs> ProgressEventPublisher;
  16. private Commands.DatabaseCommandTypes m_DBCommandType;
  17. private bool m_HasErrors = false;
  18. private string m_ErrorMessage = null;
  19. // public DatabaseScriptOptions ScriptOptions = null;
  20. private string m_SourceBaseDirectory = "";
  21. private SourceDatabase m_Database;
  22. public DatabaseEx DatabaseEx
  23. {
  24. get { return m_Database; }
  25. }
  26. private DBSourceToolsLib.DataSets.ScriptOptionsDS m_TableScriptOptionsDS
  27. = new DBSourceToolsLib.DataSets.ScriptOptionsDS();
  28. private DBSourceToolsLib.DataSets.ScriptOptionsDS m_ProcScriptOptionsDS
  29. = new DBSourceToolsLib.DataSets.ScriptOptionsDS();
  30. private SortedList<string, SysObject> m_SysObjectsList = new SortedList<string, SysObject>();
  31. private SODependencyTree m_SODependencyTree;// = new SODependencyTree();
  32. #region getters and setters
  33. //public DatabaseListEx DeploymentTargets
  34. //{
  35. // get { return m_DeploymentTargets; }
  36. // set { m_DeploymentTargets = value; }
  37. //}
  38. public string SourceBaseDirectory
  39. {
  40. get { return m_SourceBaseDirectory; }
  41. }
  42. public SODependencyTree SODependencyTree
  43. {
  44. get { return m_SODependencyTree; }
  45. set { m_SODependencyTree = value; }
  46. }
  47. public SortedList<string, SysObject> SysObjectsList
  48. {
  49. get { return m_SysObjectsList; }
  50. set { m_SysObjectsList = value; }
  51. }
  52. public DBSourceToolsLib.DataSets.ScriptOptionsDS TableScriptOptionsDS
  53. {
  54. get { return m_TableScriptOptionsDS; }
  55. set { m_TableScriptOptionsDS = value; }
  56. }
  57. public DBSourceToolsLib.DataSets.ScriptOptionsDS ProcScriptOptionsDS
  58. {
  59. get { return m_ProcScriptOptionsDS; }
  60. set { m_ProcScriptOptionsDS = value; }
  61. }
  62. public string DatabaseNickName
  63. {
  64. get
  65. {
  66. return m_Database.NickName;
  67. }
  68. }
  69. public string DatabaseName
  70. {
  71. get { return m_Database.DatabaseName; }
  72. }
  73. public string DatabaseUserName
  74. {
  75. get { return m_Database.UserName; }
  76. }
  77. public string DatabasePassword
  78. {
  79. get { return m_Database.Password; }
  80. }
  81. public string DataSourceName
  82. {
  83. get { return m_Database.DataSourceName; }
  84. }
  85. #endregion
  86. public int Count(SysObjectType oType)
  87. {
  88. // only here cause .NET 2.0 doesnt implement the Count property on IEnumerable
  89. int i = 0;
  90. switch (oType)
  91. {
  92. case SysObjectType.DataType:
  93. foreach (SysObject oDataType in this.DataTypes)
  94. i++;
  95. break;
  96. case SysObjectType.Function:
  97. foreach (SysObject oDataType in this.Functions)
  98. i++;
  99. break;
  100. case SysObjectType.Proc:
  101. foreach (SysObject oDataType in this.Procs)
  102. i++;
  103. break;
  104. case SysObjectType.Querie:
  105. foreach (SysObject oDataType in this.Queries)
  106. i++;
  107. break;
  108. case SysObjectType.Schema:
  109. foreach (SysObject oDataType in this.Schemas)
  110. i++;
  111. break;
  112. case SysObjectType.Table:
  113. foreach (SysObject oDataType in this.Tables)
  114. i++;
  115. break;
  116. case SysObjectType.View:
  117. foreach (SysObject oDataType in this.Views)
  118. i++;
  119. break;
  120. case SysObjectType.XmlSchema:
  121. foreach (SysObject oDataType in this.XmlSchemas)
  122. i++;
  123. break;
  124. case SysObjectType.User:
  125. foreach (SysObject oUser in this.Users)
  126. i++;
  127. break;
  128. case SysObjectType.Role:
  129. foreach (SysObject oRole in this.Roles)
  130. i++;
  131. break;
  132. case SysObjectType.TableType:
  133. foreach (SysObject oTableType in this.TableTypes)
  134. i++;
  135. break;
  136. case SysObjectType.Trigger:
  137. foreach (SysObject oTrigger in this.Triggers)
  138. i++;
  139. break;
  140. case SysObjectType.Synonym:
  141. foreach (SysObject oTrigger in this.Synonyms)
  142. i++;
  143. break;
  144. }
  145. return i;
  146. }
  147. public SysObjects(SourceDatabase oDatabase, string strBaseDirectory)
  148. {
  149. // this.m_Database = oDatabase;
  150. this.m_SourceBaseDirectory = strBaseDirectory;
  151. this.m_Database = oDatabase;
  152. this.m_SODependencyTree = new SODependencyTree(this.m_Database, this);
  153. // this.m_DeploymentTargets = new DatabaseListEx(this.m_SourceBaseDirectory + "\\DeploymentTargets");
  154. // this.ScriptOptions = new DatabaseScriptOptions(this.m_Database);
  155. }
  156. #region Enumerators
  157. public IEnumerable<SOTable> Tables
  158. {
  159. get
  160. {
  161. foreach (SysObject oSysObject in this.SysObjectsList.Values)
  162. {
  163. if (oSysObject.Type == SysObjectType.Table)
  164. yield return (SOTable)oSysObject;
  165. }
  166. }
  167. }
  168. public IEnumerable<SOTableType> TableTypes
  169. {
  170. get
  171. {
  172. foreach (SysObject oSysObject in this.SysObjectsList.Values)
  173. {
  174. if (oSysObject.Type == SysObjectType.TableType)
  175. yield return (SOTableType)oSysObject;
  176. }
  177. }
  178. }
  179. public IEnumerable<SODataType> DataTypes
  180. {
  181. get
  182. {
  183. foreach (SysObject oSysObject in this.SysObjectsList.Values)
  184. {
  185. if (oSysObject.Type == SysObjectType.DataType)
  186. yield return (SODataType)oSysObject;
  187. }
  188. }
  189. }
  190. public IEnumerable<SOSchema> Schemas
  191. {
  192. get
  193. {
  194. foreach (SysObject oSysObject in this.SysObjectsList.Values)
  195. {
  196. if (oSysObject.Type == SysObjectType.Schema)
  197. yield return (SOSchema)oSysObject;
  198. }
  199. }
  200. }
  201. public IEnumerable<SOXmlSchema> XmlSchemas
  202. {
  203. get
  204. {
  205. foreach (SysObject oSysObject in this.SysObjectsList.Values)
  206. {
  207. if (oSysObject.Type == SysObjectType.XmlSchema)
  208. yield return (SOXmlSchema)oSysObject;
  209. }
  210. }
  211. }
  212. public IEnumerable<SOView> Views
  213. {
  214. get
  215. {
  216. foreach (SysObject oSysObject in this.SysObjectsList.Values)
  217. {
  218. if (oSysObject.Type == SysObjectType.View)
  219. yield return (SOView)oSysObject;
  220. }
  221. }
  222. }
  223. public IEnumerable<SOFunction> Functions
  224. {
  225. get
  226. {
  227. foreach (SysObject oSysObject in this.SysObjectsList.Values)
  228. {
  229. if (oSysObject.Type == SysObjectType.Function)
  230. yield return (SOFunction)oSysObject;
  231. }
  232. }
  233. }
  234. public IEnumerable<SOProc> Procs
  235. {
  236. get
  237. {
  238. foreach (SysObject oSysObject in this.SysObjectsList.Values)
  239. {
  240. if (oSysObject.Type == SysObjectType.Proc)
  241. yield return (SOProc)oSysObject;
  242. }
  243. }
  244. }
  245. public IEnumerable<SORole> Roles
  246. {
  247. get
  248. {
  249. foreach (SysObject oSysObject in this.SysObjectsList.Values)
  250. {
  251. if (oSysObject.Type == SysObjectType.Role)
  252. yield return (SORole)oSysObject;
  253. }
  254. }
  255. }
  256. public IEnumerable<SOQuery> Queries
  257. {
  258. get
  259. {
  260. foreach (SysObject oSysObject in this.SysObjectsList.Values)
  261. {
  262. if (oSysObject.Type == SysObjectType.Querie)
  263. yield return (SOQuery)oSysObject;
  264. }
  265. }
  266. }
  267. public IEnumerable<SOUser> Users
  268. {
  269. get
  270. {
  271. foreach (SysObject oSysObject in this.SysObjectsList.Values)
  272. {
  273. if (oSysObject.Type == SysObjectType.User)
  274. yield return (SOUser)oSysObject;
  275. }
  276. }
  277. }
  278. public IEnumerable<SOTrigger> Triggers
  279. {
  280. get
  281. {
  282. foreach (SysObject oSysObject in this.SysObjectsList.Values)
  283. {
  284. if (oSysObject.Type == SysObjectType.Trigger)
  285. yield return (SOTrigger)oSysObject;
  286. }
  287. }
  288. }
  289. public IEnumerable<SOSynonym> Synonyms
  290. {
  291. get
  292. {
  293. foreach (SysObject oSysObject in this.SysObjectsList.Values)
  294. {
  295. if (oSysObject.Type == SysObjectType.Synonym)
  296. yield return (SOSynonym)oSysObject;
  297. }
  298. }
  299. }
  300. #endregion
  301. public void LoadFromDb(EventHandler<ProgressEventArgs> localHandler)
  302. {
  303. Clear();
  304. // EventHandler<ProgressEventArgs> localHandler = ProgressEventPublisher;
  305. try
  306. {
  307. #region sql_query way
  308. //using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(this.m_Database.ConnectionString))
  309. //{
  310. // conn.Open();
  311. // DataSets.SysObjectsDS oSysObjectsDs = new DBSourceToolsLib.DataSets.SysObjectsDS();
  312. // DataSets.SysObjectsDSTableAdapters.SysObjectsTableAdapter soTableAdapter = new DBSourceToolsLib.DataSets.SysObjectsDSTableAdapters.SysObjectsTableAdapter();
  313. // soTableAdapter.Connection = conn;
  314. // soTableAdapter.Fill(oSysObjectsDs.SysObjects);
  315. // SysObjectsFactory oSysObjectsFactory = new SysObjectsFactory();
  316. // int iTotalCount = oSysObjectsDs.SysObjects.Rows.Count;
  317. // int iProcessed = 0;
  318. // foreach (DataSets.SysObjectsDS.SysObjectsRow row in oSysObjectsDs.SysObjects)
  319. // {
  320. // if (localHandler != null)
  321. // localHandler(this, new ProgressEventArgs(iProcessed++, iTotalCount,
  322. // "Processing " + row.name));
  323. // SysObject newSysObject = oSysObjectsFactory.getSysObject(row.name, row.type
  324. // , this.m_SourceBaseDirectory, this.m_Database);
  325. // System.Console.WriteLine("Adding " + newSysObject.QualifiedName);
  326. // this.m_SysObjectsList.Add(newSysObject.QualifiedName, newSysObject);
  327. // }
  328. // if (localHandler != null)
  329. // localHandler(this, new ProgressEventArgs(iTotalCount, iTotalCount,
  330. // "Processing Complete"));
  331. // conn.Close();
  332. //}
  333. #endregion
  334. {
  335. //srv.SetDefaultInitFields(typeof(StoredProcedure), "IsSystemObject");
  336. Microsoft.SqlServer.Management.Smo.Server oServer = this.m_Database.GetSmoServer();
  337. Microsoft.SqlServer.Management.Smo.Database oMDatabase = this.m_Database.GetSmoDatabase(oServer);
  338. oServer.SetDefaultInitFields(typeof(StoredProcedure), "IsSystemObject");
  339. //SysObjectsFactory oSysObjectsFactory = new SysObjectsFactory();
  340. int iTotalCount = oMDatabase.UserDefinedDataTypes.Count;
  341. int iProcessed = 0;
  342. foreach (Microsoft.SqlServer.Management.Smo.UserDefinedDataType oSmo in oMDatabase.UserDefinedDataTypes)
  343. {
  344. if (localHandler != null)
  345. localHandler(this, new ProgressEventArgs(iProcessed++, iTotalCount,
  346. "Processing DataType " + oSmo.Name));
  347. SODataType newSODataType = new SODataType(oSmo.Name, this.m_SourceBaseDirectory, this.m_Database
  348. ,oSmo.Urn.ToString());
  349. newSODataType.LoadScript(this.m_Database, newSODataType.FileName, oSmo);
  350. this.m_SysObjectsList.Add(newSODataType.QualifiedName, newSODataType);
  351. }
  352. iTotalCount = oMDatabase.Tables.Count;
  353. iProcessed = 0;
  354. foreach (Microsoft.SqlServer.Management.Smo.Table oSmo in oMDatabase.Tables)
  355. {
  356. string strFullName = "";
  357. if (oSmo.Schema.CompareTo("dbo") != 0)
  358. {
  359. strFullName = oSmo.Schema + "." + oSmo.Name;
  360. }
  361. else
  362. {
  363. strFullName = oSmo.Name;
  364. }
  365. if (localHandler != null)
  366. localHandler(this, new ProgressEventArgs(iProcessed++, iTotalCount,
  367. "Processing Table " + strFullName));
  368. if (!oSmo.IsSystemObject)
  369. {
  370. SOTable newSODataType = new SOTable(strFullName, this.m_SourceBaseDirectory, this.m_Database,
  371. oSmo.Urn.ToString());
  372. newSODataType.LoadScript(this.m_Database, newSODataType.FileName, oSmo);
  373. this.m_SysObjectsList.Add(newSODataType.QualifiedName, newSODataType);
  374. }
  375. }
  376. iTotalCount = oMDatabase.StoredProcedures.Count;
  377. iProcessed = 0;
  378. foreach (Microsoft.SqlServer.Management.Smo.StoredProcedure oSmo in oMDatabase.StoredProcedures)
  379. {
  380. string strFullName = "";
  381. if (oSmo.Schema.CompareTo("dbo") != 0)
  382. {
  383. strFullName = oSmo.Schema + "." + oSmo.Name;
  384. }
  385. else
  386. {
  387. strFullName = oSmo.Name;
  388. }
  389. if (localHandler != null)
  390. localHandler(this, new ProgressEventArgs(iProcessed++, iTotalCount,
  391. "Processing Stored Procedure " + strFullName));
  392. if (!oSmo.IsSystemObject)
  393. {
  394. SOProc newSODataType = new SOProc(strFullName, this.m_SourceBaseDirectory, this.m_Database,
  395. oSmo.Urn.ToString());
  396. newSODataType.LoadScript(this.m_Database, newSODataType.FileName, oSmo);
  397. this.m_SysObjectsList.Add(newSODataType.QualifiedName, newSODataType);
  398. }
  399. }
  400. iTotalCount = oMDatabase.Views.Count;
  401. iProcessed = 0;
  402. foreach (Microsoft.SqlServer.Management.Smo.View oSmo in oMDatabase.Views)
  403. {
  404. string strFullName = "";
  405. if (oSmo.Schema.CompareTo("dbo") != 0)
  406. {
  407. strFullName = oSmo.Schema + "." + oSmo.Name;
  408. }
  409. else
  410. {
  411. strFullName = oSmo.Name;
  412. }
  413. if (localHandler != null)
  414. localHandler(this, new ProgressEventArgs(iProcessed++, iTotalCount,
  415. "Processing View " + strFullName));
  416. if (!oSmo.IsSystemObject)
  417. {
  418. SOView newSODataType = new SOView(strFullName, this.m_SourceBaseDirectory, this.m_Database
  419. ,oSmo.Urn.ToString());
  420. newSODataType.LoadScript(this.m_Database, newSODataType.FileName, oSmo);
  421. this.m_SysObjectsList.Add(newSODataType.QualifiedName, newSODataType);
  422. }
  423. }
  424. foreach (Microsoft.SqlServer.Management.Smo.Synonym oSmo in oMDatabase.Synonyms)
  425. {
  426. string strFullName = "";
  427. if (oSmo.Schema.CompareTo("dbo") != 0)
  428. strFullName = oSmo.Schema + "." + oSmo.Name;
  429. else
  430. strFullName = oSmo.Name;
  431. if (localHandler != null)
  432. localHandler(this, new ProgressEventArgs(iProcessed++, iTotalCount,
  433. "Processing Synonym " + strFullName));
  434. //SOSynonym
  435. SOSynonym newSODataType = new SOSynonym(strFullName, this.m_SourceBaseDirectory, this.m_Database
  436. , oSmo.Urn.ToString());
  437. newSODataType.LoadScript(this.m_Database, newSODataType.FileName, oSmo);
  438. this.m_SysObjectsList.Add(newSODataType.QualifiedName, newSODataType);
  439. }
  440. iTotalCount = oMDatabase.UserDefinedFunctions.Count;
  441. iProcessed = 0;
  442. foreach (Microsoft.SqlServer.Management.Smo.UserDefinedFunction oSmo in oMDatabase.UserDefinedFunctions)
  443. {
  444. if (localHandler != null)
  445. localHandler(this, new ProgressEventArgs(iProcessed++, iTotalCount,
  446. "Processing Function " + oSmo.Name));
  447. if (!oSmo.IsSystemObject)
  448. {
  449. SOFunction newSODataType = new SOFunction(oSmo.Schema + "." + oSmo.Name, this.m_SourceBaseDirectory, this.m_Database,
  450. oSmo.Urn.ToString());
  451. newSODataType.LoadScript(this.m_Database, newSODataType.FileName, oSmo);
  452. this.m_SysObjectsList.Add(newSODataType.QualifiedName, newSODataType);
  453. }
  454. }
  455. try
  456. {
  457. iTotalCount = oMDatabase.Schemas.Count;
  458. iProcessed = 0;
  459. foreach (Microsoft.SqlServer.Management.Smo.Schema oSmo in oMDatabase.Schemas)
  460. {
  461. if (localHandler != null)
  462. localHandler(this, new ProgressEventArgs(iProcessed++, iTotalCount,
  463. "Processing Schema " + oSmo.Name));
  464. if ((oSmo.Owner.CompareTo("dbo") == 0)
  465. && oSmo.Name.CompareTo("dbo") != 0)
  466. {
  467. SOSchema newSODataType = new SOSchema(oSmo.Name, this.m_SourceBaseDirectory, this.m_Database
  468. ,oSmo.Urn.ToString());
  469. newSODataType.LoadScript(this.m_Database, newSODataType.FileName, oSmo);
  470. this.m_SysObjectsList.Add(newSODataType.QualifiedName, newSODataType);
  471. }
  472. }
  473. iTotalCount = oMDatabase.XmlSchemaCollections.Count;
  474. iProcessed = 0;
  475. foreach (Microsoft.SqlServer.Management.Smo.XmlSchemaCollection oSmo in oMDatabase.XmlSchemaCollections)
  476. {
  477. if (localHandler != null)
  478. localHandler(this, new ProgressEventArgs(iProcessed++, iTotalCount,
  479. "Processing Schema " + oSmo.Name));
  480. //if ((oSmo.Owner.CompareTo("dbo") == 0)
  481. // && oSmo.Name.CompareTo("dbo") != 0)
  482. //{
  483. SOXmlSchema newSODataType = new SOXmlSchema(oSmo.Schema + "." + oSmo.Name, this.m_SourceBaseDirectory, this.m_Database
  484. ,oSmo.Urn.ToString());
  485. newSODataType.LoadScript(this.m_Database, newSODataType.FileName, oSmo);
  486. this.m_SysObjectsList.Add(newSODataType.QualifiedName, newSODataType);
  487. // }
  488. }
  489. iTotalCount = oMDatabase.Roles.Count;
  490. iProcessed = 0;
  491. foreach (Microsoft.SqlServer.Management.Smo.DatabaseRole oRole in oMDatabase.Roles)
  492. {
  493. if (localHandler != null)
  494. localHandler(this, new ProgressEventArgs(iProcessed++, iTotalCount,
  495. "Processing Role " + oRole.Name));
  496. if (!oRole.IsFixedRole)
  497. {
  498. if (oRole.Name.CompareTo("public") != 0) // there is a bug with this role
  499. {
  500. SORole newUser = new SORole(oRole.Name, this.m_SourceBaseDirectory, this.m_Database
  501. , oRole.Urn.ToString());
  502. newUser.LoadScript(this.m_Database, newUser.FileName, oRole);
  503. this.m_SysObjectsList.Add(newUser.QualifiedName, newUser);
  504. }
  505. }
  506. }
  507. iTotalCount = oMDatabase.Users.Count;
  508. iProcessed = 0;
  509. foreach (Microsoft.SqlServer.Management.Smo.User oUser in oMDatabase.Users)
  510. {
  511. if (localHandler != null)
  512. localHandler(this, new ProgressEventArgs(iProcessed++, iTotalCount,
  513. "Processing User " + oUser.Name));
  514. if (!oUser.IsSystemObject)
  515. {
  516. SOUser newUser = new SOUser(oUser.Name, this.m_SourceBaseDirectory, this.m_Database
  517. , oUser.Urn.ToString());
  518. newUser.LoadScript(this.m_Database, newUser.FileName, oUser);
  519. this.m_SysObjectsList.Add(newUser.QualifiedName, newUser);
  520. }
  521. }
  522. //oMDatabase.UserDefinedDataTypes.
  523. int iVer = oServer.VersionMajor;
  524. // sql 2008 only
  525. if (oServer.VersionMajor > 9)
  526. {
  527. iTotalCount = oMDatabase.UserDefinedTableTypes.Count;
  528. iProcessed = 0;
  529. foreach (Microsoft.SqlServer.Management.Smo.UserDefinedTableType oTableType in oMDatabase.UserDefinedTableTypes)
  530. {
  531. if (localHandler != null)
  532. localHandler(this, new ProgressEventArgs(iProcessed++, iTotalCount,
  533. "Processing TableType " + oTableType.Name));
  534. SOTableType newTableType = new SOTableType(oTableType.Name, this.m_SourceBaseDirectory, this.m_Database
  535. , oTableType.Urn.ToString());
  536. newTableType.LoadScript(this.m_Database, newTableType.FileName, oTableType);
  537. this.m_SysObjectsList.Add(newTableType.QualifiedName, newTableType);
  538. }
  539. }
  540. // triggers
  541. iTotalCount = oMDatabase.Triggers.Count;
  542. iProcessed = 0;
  543. foreach (Microsoft.SqlServer.Management.Smo.DatabaseDdlTrigger oTrigger in oMDatabase.Triggers)
  544. {
  545. if (localHandler != null)
  546. localHandler(this, new ProgressEventArgs(iProcessed++, iTotalCount,
  547. "Processing Trigger " + oTrigger.Name));
  548. if (!oTrigger.IsSystemObject)
  549. {
  550. SOTrigger newTrigger = new SOTrigger(oTrigger.Name, this.m_SourceBaseDirectory, this.m_Database
  551. , oTrigger.Urn.ToString());
  552. newTrigger.LoadScript(this.m_Database, newTrigger.FileName, oTrigger);
  553. this.m_SysObjectsList.Add(newTrigger.QualifiedName, newTrigger);
  554. }
  555. }
  556. }
  557. catch (InvalidVersionEnumeratorException ex)
  558. {
  559. System.Console.WriteLine(ex.ToString());
  560. }
  561. }
  562. this.LoadQueriesFromDisk();
  563. #region smo way
  564. // generates 1272 stored procedures, seems to be stuck in a loop.
  565. //iTotalCount = oMDatabase.Views.Count;
  566. //iProcessed = 0;
  567. //srv.SetDefaultInitFields(typeof(StoredProcedure), "IsSystemObject");
  568. //foreach (Microsoft.SqlServer.Management.Smo.View oView in oMDatabase.Views)
  569. //{
  570. // if (!oView.IsSystemObject)
  571. // {
  572. // if (localHandler != null)
  573. // localHandler(this, new ProgressEventArgs(iProcessed, iTotalCount,
  574. // "Processing View " + oView.Name));
  575. // SOView newSoView = new SOView(oView.Name, this.m_SourceBaseDirectory, oDatabase);
  576. // m_SysObjectsList.Add(newSoView.QualifiedName, newSoView);
  577. // }
  578. // iProcessed++;
  579. //}
  580. //iTotalCount = oMDatabase.UserDefinedFunctions.Count;
  581. //iProcessed = 0;
  582. //foreach (Microsoft.SqlServer.Management.Smo.UserDefinedFunction oFunction in oMDatabase.UserDefinedFunctions)
  583. //{
  584. // if (!oFunction.IsSystemObject)
  585. // {
  586. // if (localHandler != null)
  587. // localHandler(this, new ProgressEventArgs(iProcessed, iTotalCount,
  588. // "Processing Function " + oFunction.Name));
  589. // SOFunction newSoFunction = new SOFunction(oFunction.Name, this.m_SourceBaseDirectory, oDatabase);
  590. // m_SysObjectsList.Add(newSoFunction.QualifiedName, newSoFunction);
  591. // }
  592. // iProcessed++;
  593. //}
  594. #endregion
  595. }
  596. catch (Microsoft.SqlServer.Management.Smo.FailedOperationException ex)
  597. {
  598. // do not re-throw a thread abort exception
  599. if (localHandler != null)
  600. localHandler(this, new ProgressEventArgs(-1, -1,
  601. ex.ToString()));
  602. System.Console.WriteLine(ex.ToString());
  603. //throw new Exception(ex.ToString());
  604. }
  605. catch (Exception ex)
  606. {
  607. System.Console.WriteLine(ex.ToString());
  608. if (localHandler != null)
  609. localHandler(this, new ProgressEventArgs(-1, -1,
  610. ex.ToString()));
  611. //throw new Exception(ex.ToString());
  612. }
  613. }
  614. private void Clear()
  615. {
  616. this.m_SysObjectsList.Clear();
  617. }
  618. //public void aWriteCreateScript(DatabaseEx parentDatabase)
  619. //{
  620. // if (this.m_Database.IsDeploymentTarget)
  621. // this.SODependencyTree.aWriteCreateScripts(parentDatabase
  622. // ,""//,parentDatabase.SysObjects.DeploymentTargets.DatabaseListXMLFile
  623. // ,""//this.ScriptOptions.BinDir
  624. // );
  625. //}
  626. public void WriteFiles(bool bWriteData, bool isDeploymentTarget)
  627. {
  628. EventHandler<ProgressEventArgs> localHandler = ProgressEventPublisher;
  629. int count = 0;
  630. int TotalCount = this.SysObjectsList.Values.Count;
  631. if (isDeploymentTarget)
  632. {
  633. this.m_Database.DropScript.WriteScript();
  634. this.m_Database.CreateScript.WriteScript();
  635. }
  636. foreach (SysObject sysObject in this.SysObjectsList.Values)
  637. {
  638. if (localHandler != null)
  639. localHandler(this, new ProgressEventArgs(count, TotalCount,
  640. "Processing " + sysObject.Name));
  641. if (this.m_Database.IsDeploymentTarget)
  642. {
  643. switch (sysObject.Type)
  644. {
  645. case SysObjectType.Table:
  646. {
  647. //if (this.ScriptOptions.MustScriptTable(sysObject.Name))
  648. // sysObject.WriteFile(this.ScriptOptions.MustScriptData(sysObject.Name), isDeploymentTarget,
  649. // this.ScriptOptions.OrderByField(sysObject.Name));
  650. }
  651. break;
  652. case SysObjectType.View:
  653. {
  654. //if (this.ScriptOptions.MustScriptView(sysObject.Name))
  655. // sysObject.WriteFile(false, isDeploymentTarget, null);
  656. }
  657. break;
  658. default:
  659. sysObject.WriteFile(false, isDeploymentTarget, null, false);
  660. break;
  661. }
  662. }
  663. else
  664. {
  665. if (sysObject.Type == SysObjectType.Table)
  666. {
  667. //sysObject.WriteFile(this.ScriptOptions.ScriptData && this.ScriptOptions.MustScriptData(sysObject.Name), isDeploymentTarget,
  668. // this.ScriptOptions.OrderByField(sysObject.Name));
  669. }
  670. else
  671. {
  672. sysObject.WriteFile(false, isDeploymentTarget, null, false);
  673. }
  674. // sysObject.WriteFile(false, isDeploymentTarget);
  675. }
  676. count++;
  677. }
  678. }
  679. internal void WriteFiles(DatabaseScriptOptions databaseScriptOptions
  680. , EventHandler<ProgressEventArgs> localHandler)
  681. {
  682. int count = 0;
  683. int TotalCount = this.SysObjectsList.Values.Count;
  684. foreach (SysObject sysObject in this.SysObjectsList.Values)
  685. {
  686. if (localHandler != null)
  687. localHandler(this, new ProgressEventArgs(count, TotalCount,
  688. "Processing " + sysObject.Name));
  689. if (sysObject.Type == SysObjectType.Table)
  690. {
  691. sysObject.WriteFile(databaseScriptOptions.ScriptData && databaseScriptOptions.MustScriptData(sysObject.Name), false,
  692. databaseScriptOptions.OrderByField(sysObject.Name), databaseScriptOptions.RefreshData);
  693. }
  694. else
  695. {
  696. sysObject.WriteFile(false, false, null, false);
  697. }
  698. // sysObject.WriteFile(false, isDeploymentTarget);
  699. count++;
  700. }
  701. }
  702. public void LoadFromDisk()
  703. {
  704. this.Clear();
  705. //throw new NotImplementedException();
  706. SysObjectsFactory oSysObjectsFactory = new SysObjectsFactory();
  707. string strFileDirectory = this.m_SourceBaseDirectory + "\\Tables";
  708. this.LoadQueriesFromDisk();
  709. if (System.IO.Directory.Exists(strFileDirectory))
  710. {
  711. foreach (string strFileName in System.IO.Directory.GetFiles(strFileDirectory, "*.sql"))
  712. {
  713. SysObject newSysObject = oSysObjectsFactory.getSysObject
  714. (strFileName, SysObjectType.Table, m_Database);
  715. this.m_SysObjectsList.Add(newSysObject.QualifiedName, newSysObject);
  716. }
  717. }
  718. strFileDirectory = this.m_SourceBaseDirectory + "\\Procs";
  719. if (System.IO.Directory.Exists(strFileDirectory))
  720. {
  721. foreach (string strFileName in System.IO.Directory.GetFiles(strFileDirectory, "*.sql"))
  722. {
  723. SysObject newSysObject = oSysObjectsFactory.getSysObject
  724. (strFileName, SysObjectType.Proc, m_Database);
  725. this.m_SysObjectsList.Add(newSysObject.QualifiedName, newSysObject);
  726. }
  727. }
  728. strFileDirectory = this.m_SourceBaseDirectory + "\\Functions";
  729. if (System.IO.Directory.Exists(strFileDirectory))
  730. {
  731. foreach (string strFileName in System.IO.Directory.GetFiles(strFileDirectory, "*.sql"))
  732. {
  733. SysObject newSysObject = oSysObjectsFactory.getSysObject
  734. (strFileName, SysObjectType.Function, m_Database);
  735. this.m_SysObjectsList.Add(newSysObject.QualifiedName, newSysObject);
  736. }
  737. }
  738. strFileDirectory = this.m_SourceBaseDirectory + "\\Views";
  739. if (System.IO.Directory.Exists(strFileDirectory))
  740. {
  741. foreach (string strFileName in System.IO.Directory.GetFiles(strFileDirectory, "*.sql"))
  742. {
  743. SysObject newSysObject = oSysObjectsFactory.getSysObject
  744. (strFileName, SysObjectType.View, m_Database);
  745. this.m_SysObjectsList.Add(newSysObject.QualifiedName, newSysObject);
  746. }
  747. }
  748. strFileDirectory = this.m_SourceBaseDirectory + "\\DataTypes";
  749. if (System.IO.Directory.Exists(strFileDirectory))
  750. {
  751. foreach (string strFileName in System.IO.Directory.GetFiles(strFileDirectory, "*.sql"))
  752. {
  753. SysObject newSysObject = oSysObjectsFactory.getSysObject
  754. (strFileName, SysObjectType.DataType, m_Database);
  755. this.m_SysObjectsList.Add(newSysObject.QualifiedName, newSysObject);
  756. }
  757. }
  758. strFileDirectory = this.m_SourceBaseDirectory + "\\Schemas";
  759. if (System.IO.Directory.Exists(strFileDirectory))
  760. {
  761. foreach (string strFileName in System.IO.Directory.GetFiles(strFileDirectory, "*.sql"))
  762. {
  763. SysObject newSysObject = oSysObjectsFactory.getSysObject
  764. (strFileName, SysObjectType.Schema, m_Database);
  765. this.m_SysObjectsList.Add(newSysObject.QualifiedName, newSysObject);
  766. }
  767. }
  768. strFileDirectory = this.m_SourceBaseDirectory + "\\XmlSchemas";
  769. if (System.IO.Directory.Exists(strFileDirectory))
  770. {
  771. foreach (string strFileName in System.IO.Directory.GetFiles(strFileDirectory, "*.sql"))
  772. {
  773. SysObject newSysObject = oSysObjectsFactory.getSysObject
  774. (strFileName, SysObjectType.XmlSchema, m_Database);
  775. this.m_SysObjectsList.Add(newSysObject.QualifiedName, newSysObject);
  776. }
  777. }
  778. strFileDirectory = this.m_SourceBaseDirectory + "\\Users";
  779. if (System.IO.Directory.Exists(strFileDirectory))
  780. {
  781. foreach (string strFileName in System.IO.Directory.GetFiles(strFileDirectory, "*.sql"))
  782. {
  783. SysObject newSysObject = oSysObjectsFactory.getSysObject
  784. (strFileName, SysObjectType.User, m_Database);
  785. this.m_SysObjectsList.Add(newSysObject.QualifiedName, newSysObject);
  786. }
  787. }
  788. strFileDirectory = this.m_SourceBaseDirectory + "\\Roles";
  789. if (System.IO.Directory.Exists(strFileDirectory))
  790. {
  791. foreach (string strFileName in System.IO.Directory.GetFiles(strFileDirectory, "*.sql"))
  792. {
  793. SysObject newSysObject = oSysObjectsFactory.getSysObject
  794. (strFileName, SysObjectType.Role, m_Database);
  795. this.m_SysObjectsList.Add(newSysObject.QualifiedName, newSysObject);
  796. }
  797. }
  798. strFileDirectory = this.m_SourceBaseDirectory + "\\TableTypes";
  799. if (System.IO.Directory.Exists(strFileDirectory))
  800. {
  801. foreach (string strFileName in System.IO.Directory.GetFiles(strFileDirectory, "*.sql"))
  802. {
  803. SysObject newSysObject = oSysObjectsFactory.getSysObject
  804. (strFileName, SysObjectType.TableType, m_Database);
  805. this.m_SysObjectsList.Add(newSysObject.QualifiedName, newSysObject);
  806. }
  807. }
  808. strFileDirectory = this.m_SourceBaseDirectory + "\\Triggers";
  809. if (System.IO.Directory.Exists(strFileDirectory))
  810. {
  811. foreach (string strFileName in System.IO.Directory.GetFiles(strFileDirectory, "*.sql"))
  812. {
  813. SysObject newSysObject = oSysObjectsFactory.getSysObject
  814. (strFileName, SysObjectType.Trigger, m_Database);
  815. this.m_SysObjectsList.Add(newSysObject.QualifiedName, newSysObject);
  816. }
  817. }
  818. strFileDirectory = this.m_SourceBaseDirectory + "\\Synonyms";
  819. if (System.IO.Directory.Exists(strFileDirectory))
  820. {
  821. foreach (string strFileName in System.IO.Directory.GetFiles(strFileDirectory, "*.sql"))
  822. {
  823. SysObject newSysObject = oSysObjectsFactory.getSysObject
  824. (strFileName, SysObjectType.Synonym, m_Database);
  825. this.m_SysObjectsList.Add(newSysObject.QualifiedName, newSysObject);
  826. }
  827. }
  828. }
  829. private void LoadQueriesFromDisk()
  830. {
  831. SysObjectsFactory oSysObjectsFactory = new SysObjectsFactory();
  832. string strFileDirectory = this.m_SourceBaseDirectory + "\\Queries";
  833. if (System.IO.Directory.Exists(strFileDirectory))
  834. {
  835. foreach (string strFileName in System.IO.Directory.GetFiles(strFileDirectory, "*.sql"))
  836. {
  837. SysObject newSysObject = oSysObjectsFactory.getSysObject
  838. (strFileName, SysObjectType.Querie, m_Database);
  839. this.m_SysObjectsList.Add(newSysObject.QualifiedName, newSysObject);
  840. }
  841. }
  842. }
  843. public SysObject getSysObject(string strTypeName, string strObjectName)
  844. {
  845. // remove the 's'
  846. string key = strTypeName.Substring(0, strTypeName.Length - 1) + "." + strObjectName;
  847. if (this.m_SysObjectsList.ContainsKey(key))
  848. {
  849. return this.m_SysObjectsList[key];
  850. }
  851. return null;
  852. }
  853. #region IProgressEvent Members
  854. public void Execute()
  855. {
  856. // replaced by SourceDatabaseLoader
  857. EventHandler<ProgressEventArgs> localHandler = ProgressEventPublisher;
  858. switch (this.m_DBCommandType)
  859. {
  860. //case DBSourceToolsLib.Commands.DatabaseCommandTypes.LoadTableList:
  861. // this.m_TableScriptOptionsDS = this.ScriptOptions.LoadTablesFromDB(this, localHandler);
  862. // break;
  863. //case DBSourceToolsLib.Commands.DatabaseCommandTypes.LoadStoredProcList:
  864. // this.m_ProcScriptOptionsDS = this.ScriptOptions.LoadStoredProcsFromDB(this, localHandler);
  865. // break;
  866. //case DBSourceToolsLib.Commands.DatabaseCommandTypes.LoadViewList:
  867. // this.m_ProcScriptOptionsDS = this.ScriptOptions.LoadViewsFromDB(this, localHandler);
  868. // break;
  869. //case DBSourceToolsLib.Commands.DatabaseCommandTypes.LoadUserList:
  870. // this.m_ProcScriptOptionsDS = this.ScriptOptions.LoadUsersFromDB(this, localHandler);
  871. // break;
  872. }
  873. if (localHandler != null)
  874. localHandler(this, new ProgressEventArgs(100, 100,
  875. "Processing Complete"));
  876. }
  877. #endregion
  878. //public void SetDatabase(DatabaseEx database)
  879. //{
  880. // this.m_Database = database;
  881. // this.m_SODependencyTree = new SODependencyTree(this.m_Database, this);
  882. // this.m_DeploymentTargets = new DatabaseListEx(this.m_SourceBaseDirectory + "\\DeploymentTargets");
  883. //}
  884. public void SetDBCommandType(Commands.DatabaseCommandTypes dbCommandType)
  885. {
  886. this.m_DBCommandType = dbCommandType;
  887. }
  888. //public string NewQueryName()
  889. //{
  890. // int i = 0;
  891. // foreach (SOQuery oQuery in this.Queries)
  892. // i++;
  893. // string strKeyName = "Query" + (i + 1).ToString();
  894. // if (m_SysObjectsList.ContainsKey("Querie." + strKeyName))
  895. // {
  896. // // a query with this name already exists.
  897. // strKeyName += "_1";
  898. // }
  899. // return strKeyName;
  900. //}
  901. //internal void NewQuery()
  902. //{
  903. // string strNewQueryName = NewQueryName() + ".sql";
  904. // SysObjectsFactory oSysObjectsFactory = new SysObjectsFactory();
  905. // SysObject newSysObject = oSysObjectsFactory.getSysObject
  906. // (this.m_SourceBaseDirectory + "\\Queries\\" + strNewQueryName, SysObjectType.Querie, m_Database);
  907. // this.m_SysObjectsList.Add(newSysObject.QualifiedName, newSysObject);
  908. //}
  909. public void RenameSysObject(string strType, string strOldName, string strNewName)
  910. {
  911. SysObject oldSysObject = getSysObject(strType, strOldName);
  912. if (oldSysObject == null)
  913. throw new Exception("SysObjects does not contain object " + strType + "." + strOldName);
  914. SysObjectsFactory oFactory = new SysObjectsFactory();
  915. SysObject newSysObject = oFactory.NewSysObject(strNewName, oldSysObject, this.m_Database);
  916. newSysObject.Name = strNewName;
  917. newSysObject.Text = oldSysObject.Text;
  918. this.m_SysObjectsList.Add(newSysObject.QualifiedName, newSysObject);
  919. this.m_SysObjectsList.Remove(oldSysObject.QualifiedName);
  920. }
  921. public void LoadDependencyTreeFromDb(EventHandler<ProgressEventArgs> localHandler)
  922. {
  923. this.SODependencyTree.LoadFromDatabase(localHandler);
  924. }
  925. void oWalker_DiscoveryProgress(object sender, ProgressReportEventArgs e)
  926. {
  927. System.Console.WriteLine(e.Current);
  928. }
  929. public void LoadDependencyTreeFromDisk()
  930. {
  931. this.SODependencyTree.LoadFromDisk();
  932. }
  933. public void WriteDependencyTree()
  934. {
  935. this.SODependencyTree.Write();
  936. }
  937. //public void LoadDeploymentTargets(string strBaseDirectory)
  938. //{
  939. // //throw new Exception("change LoadDatabases()");
  940. // this.m_DeploymentTargets.LoadDeploymentTargets(strBaseDirectory + @"\DeploymentTargets", "", false, true,
  941. // this.m_Database);
  942. //}
  943. // public void WriteDeploymentTargets()
  944. // {
  945. // foreach (DatabaseEx oDatabase in this.m_DeploymentTargets.Databases)
  946. // {
  947. // //DirHelper oDirHelper = new DirHelper();
  948. // //oDirHelper.DeleteFilesInDir(oDatabase.BaseDirectory);
  949. // oDatabase.IsDeploymentTarget = true;
  950. // //oDatabase.SysObjects = new SysObjects(oDatabase, oDatabase.BaseDirectory);
  951. // oDatabase.SysObjects.Clear();
  952. // oDatabase.SysObjects.Clone(this, oDatabase, this.m_SODependencyTree);
  953. // //oDatabase.SysObjects.ScriptOptions = this.ScriptOptions;
  954. // oDatabase.SysObjects.WriteCreateScript(this.m_Database);
  955. // }
  956. //// throw new NotImplementedException();
  957. // }
  958. //private void Clone(SysObjects sourceSysObjects, DatabaseEx targetDatabase, SODependencyTree sourceSODependencyTree)
  959. //{
  960. // //clone actual sys objects
  961. // SysObjectsFactory oSysObjectsFactory = new SysObjectsFactory();
  962. // foreach (SysObject oSysObject in sourceSysObjects.SysObjectsList.Values)
  963. // {
  964. // SysObject newSysObject = oSysObjectsFactory.CloneSysObject(oSysObject, targetDatabase);
  965. // this.m_SysObjectsList.Add(newSysObject.QualifiedName, newSysObject);
  966. // }
  967. // this.m_SODependencyTree.Clone(sourceSODependencyTree);
  968. //}
  969. //public void AddDeploymentTarget(DBConnectionLib.Database database)
  970. //{
  971. // database.BaseDirectory = this.m_Database.BaseDirectory + "\\DeploymentTargets\\" + database.NickName;
  972. // DatabaseEx oNewTarget = new DatabaseEx(database, true);
  973. // this.m_DeploymentTargets.AddDatabase(oNewTarget);
  974. // oNewTarget.AddDropAndCreateScript(this.m_Database);
  975. //}
  976. //public void ClearDeploymentTargets()
  977. //{
  978. // this.m_DeploymentTargets.ClearDatabases();
  979. //}
  980. //internal void DeployTarget(DBConnectionLib.Database oDeploymentTargetDatabase, System.Windows.Forms.Form form)
  981. //{
  982. // throw new NotImplementedException();
  983. //}
  984. internal string NewQuery(DBSourceToolsLib.Commands.DatabaseCommandTypes m_CommandType, string m_Params)
  985. {
  986. // TODO : COMPLETE 1.2.0.8 Write new query do disk, then reload.
  987. string strCurrentQueryName = this.m_SourceBaseDirectory + "\\Queries\\Query.sql";
  988. FileUtils oFileUtils = new FileUtils();
  989. string strNewQueryFileName = oFileUtils.NewFile(strCurrentQueryName);
  990. SysObjectsFactory oSysObj

Large files files are truncated, but you can click here to view the full file