浏览代码

first commit

Prem Kumar 3 年前
当前提交
aa3496ec41
共有 5 个文件被更改,包括 234 次插入0 次删除
  1. 82
    0
      Program.cs
  2. 88
    0
      QueryParser.cs
  3. 27
    0
      Test.cs
  4. 12
    0
      TestConsoleApp.csproj
  5. 25
    0
      TestConsoleApp.sln

+ 82
- 0
Program.cs 查看文件

@@ -0,0 +1,82 @@
1
+using System;
2
+using System.Diagnostics;
3
+using System.Management;
4
+using TestConsoleApp;
5
+
6
+namespace databaseconnection
7
+{
8
+    class Program
9
+    {
10
+
11
+
12
+        static string ProcessName = "AcroRd32";
13
+        static string UserID = "";
14
+
15
+        static void Main(string[] args)
16
+        {
17
+            var parser = new QueryParser();
18
+            var stopWatch = Stopwatch.StartNew();
19
+            var columns=parser.Parse();
20
+            var time = stopWatch.Elapsed.TotalMilliseconds;
21
+            Console.WriteLine(columns);
22
+            //var anInstanceofMyClass = new Program();
23
+            //anInstanceofMyClass.checkAcrobatProcess();
24
+
25
+            //Console.WriteLine("Hello World!");
26
+            //Console.WriteLine("localDate - " + localDate);
27
+        }
28
+
29
+
30
+        private void checkAcrobatProcess()
31
+        {
32
+
33
+            if (OperatingSystem.IsWindows())
34
+            {
35
+                try
36
+                {
37
+                    //CatchStackOverflow1();
38
+                    foreach (Process process in Process.GetProcessesByName(ProcessName))
39
+                    {
40
+                        if ((process.MainWindowTitle).Equals("Adobe Acrobat Reader DC (32-bit)"))
41
+                        {
42
+                            Console.WriteLine("checkAcrobatProcess DEBUG: Adobe Acrobat Standard DC (32-bit) is launched");
43
+
44
+                            ObjectQuery objQuery = new ObjectQuery("Select * From Win32_Process where ProcessId='" + process.Id + "'");
45
+                            ManagementObjectSearcher mos = new ManagementObjectSearcher(objQuery);
46
+
47
+                            foreach (ManagementObject mo in mos.Get())
48
+                            {
49
+                                string[] s = new string[2];
50
+                                mo.InvokeMethod("GetOwner", (object[])s);
51
+                                UserID = s[0].ToLower().ToString();
52
+                                Console.WriteLine("checkAcrobatProcess DEBUG: " + UserID + " launched the acrobat");
53
+                                break;
54
+                            }
55
+                            Console.WriteLine("checkAcrobatProcess DEBUG: waitforexit");
56
+                            process.WaitForExit();
57
+                            break;
58
+                        }
59
+                    }
60
+                }
61
+                catch (Exception ex)
62
+                {
63
+                    Console.WriteLine("checkAcrobatProcess DEBUG: Failed!! " + ex.Message);
64
+                }
65
+            }
66
+            checkAcrobatProcess();
67
+        }
68
+
69
+
70
+        public static void CatchStackOverflow1()
71
+        {
72
+            try
73
+            {
74
+                throw new StackOverflowException();
75
+            }
76
+            catch (StackOverflowException) //catch (StackOverflowException ex)
77
+            {
78
+
79
+            }
80
+        }
81
+    }
82
+}

+ 88
- 0
QueryParser.cs 查看文件

@@ -0,0 +1,88 @@
1
+using System.Collections.Generic;
2
+using System.IO;
3
+using System.Linq;
4
+namespace TestConsoleApp
5
+{
6
+    public class QueryParser
7
+    {
8
+
9
+        public List<string> Parse()
10
+        {
11
+            var totalQuery = File.ReadAllText(@"D:\empt.txt");
12
+            return _Parse(totalQuery);
13
+
14
+        }
15
+        List<string> _Parse(string actualQuery)
16
+        {
17
+            string columnString = GetColumnString(actualQuery);
18
+            List<string> resultColumnlist = new List<string>();
19
+            var commaSeperatedArray = columnString.Split(',');
20
+
21
+            for (int i = 0; i < commaSeperatedArray.Length; i++)
22
+            {
23
+                var column = commaSeperatedArray[i];
24
+                if (IsPredefinedFunction(column))
25
+                {
26
+                    if (!IsPerfectPredefinedFunction(column))
27
+                    {
28
+                        column = MakeItPerfect(ref i, commaSeperatedArray, commaSeperatedArray[i]);
29
+                    }
30
+                    resultColumnlist.Add(column);
31
+                }
32
+                else
33
+                {
34
+                    resultColumnlist.Add(column);
35
+                }
36
+            }
37
+            return resultColumnlist;
38
+        }
39
+
40
+        string GetColumnString(string actualQuery)
41
+        {
42
+            actualQuery = actualQuery.Replace(System.Environment.NewLine, " ");
43
+            var fromidex = actualQuery.ToLower().LastIndexOf("from ");
44
+            var splitted = actualQuery.Substring(0, fromidex);
45
+            splitted = splitted.TrimStart().Remove(0, "select".Length);
46
+            return splitted;
47
+        }
48
+        bool IsPredefinedFunction(string name)
49
+        {
50
+            var _name = name.ToLower();
51
+            bool result = false;
52
+            foreach (var function in PredefinedFunctions())
53
+            {
54
+                if (_name.Contains(function.ToLower()))
55
+                {
56
+                    result = true;
57
+                    break;
58
+                }
59
+            }
60
+            return result;
61
+        }
62
+        string[] PredefinedFunctions()
63
+        {
64
+            return new string[] {
65
+            "coalesce","concat","select","min","max","count"
66
+            };
67
+        }
68
+        bool IsPerfectPredefinedFunction(string name)
69
+        {
70
+            var opencount = name.Count(c => c == '(');
71
+            var closecount = name.Count(c => c == ')');
72
+            return opencount == closecount;
73
+        }
74
+        string MakeItPerfect(ref int index, string[] colArray, string columnName)
75
+        {
76
+            index = index + 1;
77
+            var column = columnName + "," + colArray[index];
78
+            if (!IsPerfectPredefinedFunction(column))
79
+            {
80
+                column = MakeItPerfect(ref index, colArray, column);
81
+            }
82
+
83
+            return column;
84
+        }
85
+
86
+       
87
+    }
88
+}

+ 27
- 0
Test.cs 查看文件

@@ -0,0 +1,27 @@
1
+using System;
2
+using System.Diagnostics;
3
+
4
+namespace TestConsoleApp
5
+{
6
+    class Test
7
+    {
8
+       public static void Execute()
9
+        {
10
+            var stopWatch = Stopwatch.StartNew();
11
+            for (int i = 0; i < 99999; i++)
12
+            {
13
+                var a = 0;a += i;a++;
14
+
15
+            }
16
+            Console.WriteLine("{0}", stopWatch.Elapsed.TotalMilliseconds);
17
+            var stopWatch2 = Stopwatch.StartNew();
18
+            for (int i = 0; i < 99999; i++)
19
+            {
20
+                var a = 0; a += i; a++;
21
+
22
+            }
23
+            Console.WriteLine("{0}", stopWatch2.Elapsed.TotalMilliseconds);
24
+
25
+        }
26
+    }
27
+}

+ 12
- 0
TestConsoleApp.csproj 查看文件

@@ -0,0 +1,12 @@
1
+<Project Sdk="Microsoft.NET.Sdk">
2
+
3
+  <PropertyGroup>
4
+    <OutputType>Exe</OutputType>
5
+    <TargetFramework>net5.0</TargetFramework>
6
+  </PropertyGroup>
7
+
8
+  <ItemGroup>
9
+    <PackageReference Include="System.Management" Version="5.0.0" />
10
+  </ItemGroup>
11
+
12
+</Project>

+ 25
- 0
TestConsoleApp.sln 查看文件

@@ -0,0 +1,25 @@
1
+
2
+Microsoft Visual Studio Solution File, Format Version 12.00
3
+# Visual Studio Version 16
4
+VisualStudioVersion = 16.0.31321.278
5
+MinimumVisualStudioVersion = 10.0.40219.1
6
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestConsoleApp", "TestConsoleApp.csproj", "{55803876-CE84-4F74-9B21-AEA5353EAC5B}"
7
+EndProject
8
+Global
9
+	GlobalSection(SolutionConfigurationPlatforms) = preSolution
10
+		Debug|Any CPU = Debug|Any CPU
11
+		Release|Any CPU = Release|Any CPU
12
+	EndGlobalSection
13
+	GlobalSection(ProjectConfigurationPlatforms) = postSolution
14
+		{55803876-CE84-4F74-9B21-AEA5353EAC5B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15
+		{55803876-CE84-4F74-9B21-AEA5353EAC5B}.Debug|Any CPU.Build.0 = Debug|Any CPU
16
+		{55803876-CE84-4F74-9B21-AEA5353EAC5B}.Release|Any CPU.ActiveCfg = Release|Any CPU
17
+		{55803876-CE84-4F74-9B21-AEA5353EAC5B}.Release|Any CPU.Build.0 = Release|Any CPU
18
+	EndGlobalSection
19
+	GlobalSection(SolutionProperties) = preSolution
20
+		HideSolutionNode = FALSE
21
+	EndGlobalSection
22
+	GlobalSection(ExtensibilityGlobals) = postSolution
23
+		SolutionGuid = {C2ADFE16-21AF-4DC6-B341-4AE0F9B528DD}
24
+	EndGlobalSection
25
+EndGlobal

正在加载...
取消
保存