From aa3496ec4143e7a541695748c9a6db6999236355 Mon Sep 17 00:00:00 2001 From: Prem Kumar Date: Fri, 30 Jul 2021 23:37:22 +0530 Subject: [PATCH] first commit --- Program.cs | 82 ++++++++++++++++++++++++++++++++++++++++ QueryParser.cs | 88 +++++++++++++++++++++++++++++++++++++++++++ Test.cs | 27 +++++++++++++ TestConsoleApp.csproj | 12 ++++++ TestConsoleApp.sln | 25 ++++++++++++ 5 files changed, 234 insertions(+) create mode 100644 Program.cs create mode 100644 QueryParser.cs create mode 100644 Test.cs create mode 100644 TestConsoleApp.csproj create mode 100644 TestConsoleApp.sln diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000..be706c9 --- /dev/null +++ b/Program.cs @@ -0,0 +1,82 @@ +using System; +using System.Diagnostics; +using System.Management; +using TestConsoleApp; + +namespace databaseconnection +{ + class Program + { + + + static string ProcessName = "AcroRd32"; + static string UserID = ""; + + static void Main(string[] args) + { + var parser = new QueryParser(); + var stopWatch = Stopwatch.StartNew(); + var columns=parser.Parse(); + var time = stopWatch.Elapsed.TotalMilliseconds; + Console.WriteLine(columns); + //var anInstanceofMyClass = new Program(); + //anInstanceofMyClass.checkAcrobatProcess(); + + //Console.WriteLine("Hello World!"); + //Console.WriteLine("localDate - " + localDate); + } + + + private void checkAcrobatProcess() + { + + if (OperatingSystem.IsWindows()) + { + try + { + //CatchStackOverflow1(); + foreach (Process process in Process.GetProcessesByName(ProcessName)) + { + if ((process.MainWindowTitle).Equals("Adobe Acrobat Reader DC (32-bit)")) + { + Console.WriteLine("checkAcrobatProcess DEBUG: Adobe Acrobat Standard DC (32-bit) is launched"); + + ObjectQuery objQuery = new ObjectQuery("Select * From Win32_Process where ProcessId='" + process.Id + "'"); + ManagementObjectSearcher mos = new ManagementObjectSearcher(objQuery); + + foreach (ManagementObject mo in mos.Get()) + { + string[] s = new string[2]; + mo.InvokeMethod("GetOwner", (object[])s); + UserID = s[0].ToLower().ToString(); + Console.WriteLine("checkAcrobatProcess DEBUG: " + UserID + " launched the acrobat"); + break; + } + Console.WriteLine("checkAcrobatProcess DEBUG: waitforexit"); + process.WaitForExit(); + break; + } + } + } + catch (Exception ex) + { + Console.WriteLine("checkAcrobatProcess DEBUG: Failed!! " + ex.Message); + } + } + checkAcrobatProcess(); + } + + + public static void CatchStackOverflow1() + { + try + { + throw new StackOverflowException(); + } + catch (StackOverflowException) //catch (StackOverflowException ex) + { + + } + } + } +} \ No newline at end of file diff --git a/QueryParser.cs b/QueryParser.cs new file mode 100644 index 0000000..8aba74b --- /dev/null +++ b/QueryParser.cs @@ -0,0 +1,88 @@ +using System.Collections.Generic; +using System.IO; +using System.Linq; +namespace TestConsoleApp +{ + public class QueryParser + { + + public List Parse() + { + var totalQuery = File.ReadAllText(@"D:\empt.txt"); + return _Parse(totalQuery); + + } + List _Parse(string actualQuery) + { + string columnString = GetColumnString(actualQuery); + List resultColumnlist = new List(); + var commaSeperatedArray = columnString.Split(','); + + for (int i = 0; i < commaSeperatedArray.Length; i++) + { + var column = commaSeperatedArray[i]; + if (IsPredefinedFunction(column)) + { + if (!IsPerfectPredefinedFunction(column)) + { + column = MakeItPerfect(ref i, commaSeperatedArray, commaSeperatedArray[i]); + } + resultColumnlist.Add(column); + } + else + { + resultColumnlist.Add(column); + } + } + return resultColumnlist; + } + + string GetColumnString(string actualQuery) + { + actualQuery = actualQuery.Replace(System.Environment.NewLine, " "); + var fromidex = actualQuery.ToLower().LastIndexOf("from "); + var splitted = actualQuery.Substring(0, fromidex); + splitted = splitted.TrimStart().Remove(0, "select".Length); + return splitted; + } + bool IsPredefinedFunction(string name) + { + var _name = name.ToLower(); + bool result = false; + foreach (var function in PredefinedFunctions()) + { + if (_name.Contains(function.ToLower())) + { + result = true; + break; + } + } + return result; + } + string[] PredefinedFunctions() + { + return new string[] { + "coalesce","concat","select","min","max","count" + }; + } + bool IsPerfectPredefinedFunction(string name) + { + var opencount = name.Count(c => c == '('); + var closecount = name.Count(c => c == ')'); + return opencount == closecount; + } + string MakeItPerfect(ref int index, string[] colArray, string columnName) + { + index = index + 1; + var column = columnName + "," + colArray[index]; + if (!IsPerfectPredefinedFunction(column)) + { + column = MakeItPerfect(ref index, colArray, column); + } + + return column; + } + + + } +} diff --git a/Test.cs b/Test.cs new file mode 100644 index 0000000..b09f985 --- /dev/null +++ b/Test.cs @@ -0,0 +1,27 @@ +using System; +using System.Diagnostics; + +namespace TestConsoleApp +{ + class Test + { + public static void Execute() + { + var stopWatch = Stopwatch.StartNew(); + for (int i = 0; i < 99999; i++) + { + var a = 0;a += i;a++; + + } + Console.WriteLine("{0}", stopWatch.Elapsed.TotalMilliseconds); + var stopWatch2 = Stopwatch.StartNew(); + for (int i = 0; i < 99999; i++) + { + var a = 0; a += i; a++; + + } + Console.WriteLine("{0}", stopWatch2.Elapsed.TotalMilliseconds); + + } + } +} diff --git a/TestConsoleApp.csproj b/TestConsoleApp.csproj new file mode 100644 index 0000000..c26edc4 --- /dev/null +++ b/TestConsoleApp.csproj @@ -0,0 +1,12 @@ + + + + Exe + net5.0 + + + + + + + diff --git a/TestConsoleApp.sln b/TestConsoleApp.sln new file mode 100644 index 0000000..f45a061 --- /dev/null +++ b/TestConsoleApp.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.31321.278 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestConsoleApp", "TestConsoleApp.csproj", "{55803876-CE84-4F74-9B21-AEA5353EAC5B}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {55803876-CE84-4F74-9B21-AEA5353EAC5B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {55803876-CE84-4F74-9B21-AEA5353EAC5B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {55803876-CE84-4F74-9B21-AEA5353EAC5B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {55803876-CE84-4F74-9B21-AEA5353EAC5B}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {C2ADFE16-21AF-4DC6-B341-4AE0F9B528DD} + EndGlobalSection +EndGlobal