public void TestSaveAndRemoveItem() { var repository = new SqlRewriteDbRepository(new SqlConnection(Settings.Default.ConnectionString)); var item = new SqlRewriteRule { Id = 0, // This will cause insertion DatabaseRegEx = ".*", QueryMatchRegEx = "SELECT @@VERSION", QueryReplacementString = "SELECT DBDATE()", RegExOptions = 0 }; repository.SaveSqlRewriteRule(item); try { Assert.AreNotEqual(0, item.Id); var items = repository.LoadSqlRewriteRules(); ItemExists(items, item); repository.RemoveSqlRewriteRule(item.Id); items = repository.LoadSqlRewriteRules(); Assert.ThrowsException <Exception>(() => { ItemExists(items, item); }); } finally { try { repository.RemoveSqlRewriteRule(item.Id); } catch (Exception) { // Ignore exceptions trying to remove to cleanup } } }
public void TestBasicSqlRewriteRuleService() { using var conn = new SqlConnection(Settings.Default.ConnectionString); var repo = new SqlRewriteDbRepository(conn); var rule = new SqlRewriteRule { DatabaseRegEx = ".*", QueryMatchRegEx = Stm, QueryReplacementString = "SELECT GETDATE()" }; repo.SaveSqlRewriteRule(rule); using var service = new SqlRewriteRuleService(repo, true); using var con = new SqlConnection(Settings.Default.ConnectionString); con.Open(); using var cmd = new SqlCommand(Stm, con); var version = cmd.ExecuteScalar().ToString(); Assert.IsFalse(version.Contains("Microsoft")); service.RemoveRule(rule.Id); service.RefreshRulesFromRepository(); using var con2 = new SqlConnection(Settings.Default.ConnectionString); con2.Open(); using var cmd2 = new SqlCommand(Stm, con2); version = cmd2.ExecuteScalar().ToString(); Assert.IsTrue(version.Contains("Microsoft")); }
private static void Main() { Console.TreatControlCAsInput = true; using var repo = new SqlRewriteDbRepository(Settings.Default.ConnectionString); repo.RemoveAllSqlRewriteRules(); var lastRefreshFromRepo = DateTime.Now; using var svc = new SqlRewriteRuleService(repo, true) { AutoRefreshRulesAndSettingsEnabled = true }; svc.AutoRefreshEvent += () => lastRefreshFromRepo = DateTime.Now; using var conn = new SqlConnection(Settings.Default.ConnectionString); conn.Open(); SqlRewriteRule rule = null; var input = ""; do { Console.Clear(); if (input != "") { rule ??= new SqlRewriteRule() { DatabaseRegEx = ".*", QueryMatchRegEx = "SELECT @@VERSION" }; rule.QueryReplacementString = $"{input} /*x*/"; repo.SaveSqlRewriteRule(rule); } PrintCurrentRuleSet(repo); using var cmd = CreateSqlCommand(conn); Console.WriteLine($"Current datetime: {DateTime.Now}"); Console.WriteLine($"Last refresh from repository: {lastRefreshFromRepo}"); Console.WriteLine($"Auto-refresh interval (milliseconds): {svc.AutoRefreshTimerInterval}"); Console.WriteLine($"Query:\r\n{cmd.CommandText}\r\n"); Console.WriteLine($"Result:\r\n{cmd.ExecuteScalar()}\r\n"); Console.WriteLine("Press Enter to continue, enter a replacement SQL and press Enter or press Ctrl-C to finish execution"); input = ""; do { var keyPressed = Console.ReadKey(); if (keyPressed.Key == ConsoleKey.C && keyPressed.Modifiers == ConsoleModifiers.Control) { return; } if (keyPressed.Key == ConsoleKey.Enter) { break; } input += keyPressed.KeyChar; } while (true); Console.WriteLine(); } while (true); }