Advertisement
Guest User

Untitled

a guest
Apr 7th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.61 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.  
  15. public Scripter(Repo repo) {
  16. PATH = repo.path;
  17. BRANCH = repo.branch;
  18. SERVER_NAME = repo.server;
  19. USER = repo.user;
  20. PWD = repo.pwd;
  21. DB = repo.db;
  22. TBL_PREFIX = repo.tbl;
  23. }
  24.  
  25. public void Main() {
  26. string path,
  27. txt;
  28.  
  29. /* time how long it takes */
  30. stop_watch.Start();
  31.  
  32.  
  33. // Make sure save directories are there
  34. CheckDirectories();
  35.  
  36. Console.WriteLine("bee bop, Tables");
  37. Console.WriteLine("This takes like 15sec, hold on sweetie");
  38.  
  39. // emits a table object on change
  40. GetTables()
  41. .ForEachAsync( tbl => {
  42. path = CreateFilePath(TBL_PATH + tbl.Name);
  43. txt = TransformSQL(tbl.Script());
  44. Task.Run(() => SaveStatement(path, txt));
  45. });
  46.  
  47. Console.WriteLine(stop_watch.Elapsed);
  48. Console.WriteLine("bee bop, Stored Procs and Views");
  49. Console.WriteLine("This takes like 15sec, chill, we're almost done");
  50.  
  51. // emits a view object on change
  52. GetViews()
  53. .ForEachAsync(vw => {
  54. path = CreateFilePath(VW_PATH + vw.Name);
  55. txt = TransformSQL(vw.Script());
  56. Task.Run(() => SaveStatement(path, txt));
  57. });
  58.  
  59. // emits a stored procedure object on change
  60. GetStoredProcedures()
  61. .ForEachAsync(usp => {
  62. path = CreateFilePath(SP_PATH + usp.Name);
  63. txt = TransformSQL(usp.Script());
  64. Task.Run(() => SaveStatement(path, txt));
  65. });
  66.  
  67. Console.WriteLine(stop_watch.Elapsed);
  68. }
  69.  
  70. public IObservable<Table> GetTables() {
  71. return db.Tables
  72. .OfType<Table>()
  73. .Where(tbl => tbl.Name.StartsWith(TBL_PREFIX))
  74. .ToObservable();
  75. }
  76.  
  77. public IObservable<View> GetViews() {
  78. return db.Views
  79. .OfType<View>()
  80. .Where(vw => !vw.IsSystemObject)
  81. .ToObservable();
  82. }
  83.  
  84. public IObservable<StoredProcedure> GetStoredProcedures() {
  85. return db.StoredProcedures
  86. .OfType<StoredProcedure>()
  87. .Where(sp => !sp.IsSystemObject)
  88. .ToObservable();
  89. }
  90.  
  91. /* makes file name */
  92. public string CreateFilePath(string path) {
  93. return string.Format("{0}{1}", path, FILE_TYPE);
  94. }
  95.  
  96. /* gets actual sql command out of object */
  97. public string TransformSQL(StringCollection collection) {
  98. return string.Join("", collection.Cast<string>().ToList());
  99. }
  100.  
  101. /* saves the sql string to a file */
  102. public async void SaveStatement(string path, string txt) {
  103. byte[] encodedText = Encoding.UTF8.GetBytes(txt);
  104. using (FileStream sourceStream = new FileStream(path,
  105. FileMode.Create, FileAccess.Write, FileShare.None,
  106. bufferSize: 4096, useAsync: true)) {
  107. await sourceStream.WriteAsync(encodedText, 0, encodedText.Length);
  108. };
  109. }
  110.  
  111. /* if a directory is not there, it makes it */
  112. public void CheckDirectories() {
  113. SP_PATH = string.Format("{0}\\{1}\\", PATH, USP);
  114. TBL_PATH = string.Format("{0}\\{1}\\", PATH, TBL);
  115. VW_PATH = string.Format("{0}\\{1}\\", PATH, VW);
  116. var dirs = new[] { SP_PATH, TBL_PATH, VW_PATH };
  117.  
  118. dirs.Where(dir => !Directory.Exists(dir))
  119. .ToList()
  120. .ForEach(dir=> Directory.CreateDirectory(dir));
  121. }
  122.  
  123. /* tries to connect to server/db */
  124. public bool CreateConnection() {
  125. try {
  126. conn = new ServerConnection(SERVER_NAME);
  127. conn.LoginSecure = false;
  128. conn.Login = this.USER;
  129. conn.Password = this.PWD;
  130. conn.MultipleActiveResultSets = true;
  131. svr = new Server(conn);
  132. svr.SetDefaultInitFields(typeof(StoredProcedure), this.SP_SYS_OBJ);
  133. svr.SetDefaultInitFields(typeof(Table), true);
  134. db = svr.Databases[this.DB];
  135. }
  136. catch (Exception e) {
  137. Debug.WriteLine(e.ToString());
  138. return false;
  139. }
  140.  
  141. return true;
  142. }
  143.  
  144. #region SetUpVars
  145. Stopwatch stop_watch = new Stopwatch();
  146. private ServerConnection conn;
  147. private Server svr;
  148. private Database db;
  149.  
  150. string DATE = DateTime.Today.ToString("MMddyyyy"),
  151. SP_SYS_OBJ = "IsSystemObject",
  152. TBL = "tbl",
  153. USP = "usp",
  154. VW = "vw",
  155. FILE_TYPE = ".sql",
  156. BRANCH,
  157. PATH,
  158. SP_PATH,
  159. TBL_PATH,
  160. VW_PATH,
  161. SERVER_NAME,
  162. TBL_PREFIX,
  163. USER,
  164. PWD,
  165. DB;
  166. #endregion
  167. }
  168.  
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement