Advertisement
Guest User

Untitled

a guest
Mar 17th, 2017
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.38 KB | None | 0 0
  1. using System;
  2. using System.Collections.Specialized;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Diagnostics;
  7. using Microsoft.SqlServer.Management.Smo;
  8. using Microsoft.SqlServer.Management.Common;
  9. using System.Reactive.Linq;
  10. using System.Threading.Tasks;
  11.  
  12. namespace SQLScripter {
  13. class Scripter {
  14. public Scripter(string branch) {
  15. BRANCH = branch;
  16. stop_watch.Start();
  17. if (CreateConnection()) {
  18. Main();
  19. }
  20. Debug.WriteLine(stop_watch.Elapsed);
  21. }
  22.  
  23. public void Main() {
  24. string path, txt;
  25. CheckDirectories();
  26.  
  27. GetTables()
  28. .ForEachAsync( tbl => {
  29. path = CreateFileName(TBL_PATH + tbl.Name);
  30. txt = TransformSQL(tbl.Script());
  31. Task.Run(() => SaveStatement(path, txt));
  32. });
  33.  
  34. GetStoredProcedures()
  35. .ForEachAsync(usp => {
  36. path = CreateFileName(SP_PATH + usp.Name);
  37. txt = TransformSQL(usp.Script());
  38. Task.Run(() => SaveStatement(path, txt));
  39. });
  40. }
  41.  
  42. public IObservable<Table> GetTables() {
  43. return db.Tables
  44. .OfType<Table>()
  45. .Where(tbl => tbl.Name.StartsWith("G3"))
  46. .ToObservable();
  47. }
  48.  
  49. public IObservable<StoredProcedure> GetStoredProcedures() {
  50. return db.StoredProcedures
  51. .OfType<StoredProcedure>()
  52. .Where(sp => !sp.IsSystemObject)
  53. .ToObservable();
  54. }
  55.  
  56. public string CreateFileName(string path) {
  57. return string.Format("{0}{1}", path, FILE_TYPE);
  58. }
  59.  
  60. public string TransformSQL(StringCollection collection) {
  61. var strings = collection.Cast<string>().ToList();
  62. return string.Join("", strings);
  63. }
  64.  
  65. public async void SaveStatement(string path, string txt) {
  66. byte[] encodedText = Encoding.Unicode.GetBytes(txt);
  67. using (FileStream sourceStream = new FileStream(path,
  68. FileMode.Create, FileAccess.Write, FileShare.None,
  69. bufferSize: 4096, useAsync: true)) {
  70. await sourceStream.WriteAsync(encodedText, 0, encodedText.Length);
  71. };
  72. }
  73.  
  74. public void CheckDirectories() {
  75. SP_PATH = string.Format("{0}\\{1}\\{2}\\", ROOT, BRANCH, USP);
  76. TBL_PATH = string.Format("{0}\\{1}\\{2}\\", ROOT, BRANCH, TBL);
  77. var dirs = new[] { SP_PATH, TBL_PATH };
  78.  
  79. dirs.Where(dir => !Directory.Exists(dir))
  80. .ToList()
  81. .ForEach(dir=> Directory.CreateDirectory(dir));
  82. }
  83.  
  84. public bool CreateConnection() {
  85. try {
  86. conn = new ServerConnection(SERVER_NAME);
  87. conn.LoginSecure = false;
  88. conn.Login = this.USER;
  89. conn.Password = this.PWD;
  90. conn.MultipleActiveResultSets = true;
  91. svr = new Server(conn);
  92. svr.SetDefaultInitFields(typeof(StoredProcedure), this.SP_SYS_OBJ);
  93. svr.SetDefaultInitFields(typeof(Table), true);
  94. db = svr.Databases[this.DB];
  95. return true;
  96. }
  97. catch (Exception e) {
  98. Debug.WriteLine(e.ToString());
  99. return false;
  100. }
  101.  
  102. }
  103.  
  104. #region SetUpVars
  105. Stopwatch stop_watch = new Stopwatch();
  106. private ServerConnection conn;
  107. private Server svr;
  108. private Database db;
  109. const string ROOT = @"\\g3mesacc\d$\utils\Scripts\";
  110.  
  111. //going to have to figure out a good way to
  112. //to handle db changes
  113. //▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼
  114. string SERVER_NAME = "server_name",
  115. USER = "user",
  116. PWD = "pwd",
  117. DB = "db_bane",
  118. SP_SYS_OBJ = "IsSystemObject",
  119. DATE = DateTime.Today.ToString("MMddyyyy"),
  120. TBL = "tbl",
  121. USP = "usp",
  122. FILE_TYPE = ".sql",
  123. BRANCH,
  124. SP_PATH,
  125. TBL_PATH;
  126. #endregion
  127. }
  128.  
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement