Browse Source

增加数据库类型和读取SQL文件编码处理

yuzhengyang 1 year ago
parent
commit
f99f457540

+ 7 - 0
Azylee.Utils/Azylee.Core/AppUtils/AppConfigUtils/AppConfigModels/AppConfigDbItem.cs

@@ -126,6 +126,8 @@ namespace Azylee.Core.AppUtils.AppConfigUtils.AppConfigModels
 
                 case "ddm": return DatabaseType.DDM;
 
+                case "starrocks": return DatabaseType.StarRocks;
+
                 case "mysql":
                 default:
                     return DatabaseType.Mysql;
@@ -151,6 +153,11 @@ namespace Azylee.Core.AppUtils.AppConfigUtils.AppConfigModels
                         return $"server = {Server}; port = {(Str.Ok(Port) ? Port : "3306")}; userid = {UserId}; password = {GetPasswordEnc()}; database = {database}; persistsecurityinfo = True; {JoinConnectString} {ExtConnectString}";
                     }
 
+                case DatabaseType.StarRocks:
+                    {
+                        return $"server = {Server}; port = {(Str.Ok(Port) ? Port : "9030")}; userid = {UserId}; password = {GetPasswordEnc()}; database = {database}; persistsecurityinfo = True; {JoinConnectString} {ExtConnectString}";
+                    }
+
                 case DatabaseType.Mysql:
                 default:
                     {

+ 2 - 0
Azylee.Utils/Azylee.Core/Azylee.Core.csproj

@@ -106,6 +106,8 @@
     <Compile Include="DllUtils\DllInvokeTool.cs" />
     <Compile Include="DrawingUtils\ColorUtils\ColorStyle.cs" />
     <Compile Include="DrawingUtils\ImageUtils\IMG.cs" />
+    <Compile Include="ExtensionUtils\StringBuilderExtension.cs" />
+    <Compile Include="ExtensionUtils\DictionaryExtension.cs" />
     <Compile Include="FormUtils\FormManTool.cs" />
     <Compile Include="FormUtils\FormModuleUtils\FormModuleManTool.cs" />
     <Compile Include="FormUtils\FormModuleUtils\IModuleForm.cs" />

+ 4 - 2
Azylee.Utils/Azylee.Core/DbUtils/DbInterface/IDatabaseHelper.cs

@@ -50,15 +50,17 @@ namespace Azylee.Core.DbUtils.DbInterface
         /// 查询表字段信息列表
         /// </summary>
         /// <returns></returns>
-        DataTable ColumnList(string database, string schema,string table);
+        DataTable ColumnList(string database, string schema, string table);
 
         /// <summary>
         /// 执行文件
         /// </summary>
         /// <param name="SqlFile">执行文件路径</param>
+        /// <param name="runParams">执行文件参数控制</param>
+        /// <param name="repParams">替换参数</param>
         /// <param name="action">执行后动作(执行语句,是否成功,影响行数,异常提示信息)</param>
         /// <returns></returns>
-        Tuple<bool, int, string> ExecuteFile(string SqlFile, Action<string, bool, int, string> action);
+        Tuple<bool, int, string> ExecuteFile(string SqlFile, Dictionary<string, string> runParams, Dictionary<string, string> repParams, Action<string, bool, int, string> action);
         /// <summary>
         /// 执行文件SQL(一段SQL脚本)
         /// </summary>

+ 2 - 1
Azylee.Utils/Azylee.Core/DbUtils/DbModels/DatabaseType.cs

@@ -9,6 +9,7 @@ namespace Azylee.Core.DbUtils.DbModels
     {
         Mysql,
         PostgreSQL,
-        DDM
+        DDM,
+        StarRocks
     }
 }

+ 21 - 0
Azylee.Utils/Azylee.Core/ExtensionUtils/DictionaryExtension.cs

@@ -0,0 +1,21 @@
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Azylee.Core.ExtensionUtils
+{
+    /// <summary>
+    /// Dictionary 类型的扩展方法
+    /// </summary>
+    public static class DictionaryExtension
+    {
+        public static string GetString(this IDictionary dic, string key, string defaultVal = "")
+        {
+            if (dic == null || key == null) return defaultVal;
+            if (dic.Contains(key) && dic[key] != null) return dic[key].ToString();
+            return defaultVal;
+        }
+    }
+}

+ 26 - 0
Azylee.Utils/Azylee.Core/ExtensionUtils/StringBuilderExtension.cs

@@ -0,0 +1,26 @@
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Azylee.Core.ExtensionUtils
+{
+    /// <summary>
+    /// Dictionary 类型的扩展方法
+    /// </summary>
+    public static class StringBuilderExtension
+    {
+        public static StringBuilder replace(this StringBuilder stringBuilder, Dictionary<string, string> pms)
+        {
+            if (pms != null)
+            {
+                foreach (var key in pms.Keys)
+                {
+                    stringBuilder = stringBuilder.Replace(key, pms[key]);
+                }
+            }
+            return stringBuilder;
+        }
+    }
+}

+ 101 - 0
Azylee.Utils/Azylee.Core/IOUtils/TxtUtils/TxtTool.cs

@@ -5,6 +5,7 @@
 //      Copyright (c) yuzhengyang. All rights reserved.
 //************************************************************************
 using Azylee.Core.DataUtils.CollectionUtils;
+using Azylee.Core.DataUtils.StringUtils;
 using Azylee.Core.IOUtils.DirUtils;
 using System;
 using System.Collections.Generic;
@@ -77,6 +78,24 @@ namespace Azylee.Core.IOUtils.TxtUtils
             catch { }
             return null;
         }
+        public static string Read(string file, Encoding encoding)
+        {
+            try
+            {
+                if (File.Exists(file))
+                {
+                    using (StreamReader sr = new StreamReader(file, encoding))
+                    {
+                        string result = "", line;
+                        while ((line = sr.ReadLine()) != null)
+                            result += line.ToString();
+                        return result;
+                    }
+                }
+            }
+            catch { }
+            return null;
+        }
         public static List<string> ReadLine(string file)
         {
             try
@@ -93,6 +112,22 @@ namespace Azylee.Core.IOUtils.TxtUtils
             catch (Exception e) { }
             return null;
         }
+        public static List<string> ReadLine(string file, Encoding encoding)
+        {
+            try
+            {
+                using (StreamReader sr = new StreamReader(file, encoding))
+                {
+                    List<string> result = new List<string>();
+                    string line;
+                    while ((line = sr.ReadLine()) != null)
+                        result.Add(line.ToString());
+                    return result;
+                }
+            }
+            catch (Exception e) { }
+            return null;
+        }
         public static void ReadLine(string file, Action<int, string> action)
         {
             try
@@ -146,5 +181,71 @@ namespace Azylee.Core.IOUtils.TxtUtils
             catch (Exception e) { }
             return count;
         }
+
+        /// <summary>
+        /// 替换执行文件文本块
+        /// </summary>
+        /// <param name="file"></param>
+        /// <param name="begString"></param>
+        /// <param name="endString"></param>
+        /// <param name="content"></param>
+        /// <returns></returns>
+        public static bool ReplaceBlock(string file, string begString, string endString, List<string> content)
+        {
+            if (Str.Ok(file) && File.Exists(file))
+            {
+                List<string> result = new List<string>();
+                int begIndex = 0, endIndex = 0;
+
+                // 找到标记位置
+                List<string> txt = TxtTool.ReadLine(file);
+                if (Ls.Ok(txt))
+                {
+                    for (int i = 0; i < txt.Count; i++)
+                    {
+                        // 找到要替换内容的开始行和结束行
+                        if (txt[i].StartsWith(begString)) begIndex = i;
+                        if (txt[i].StartsWith(endString)) endIndex = i;
+                    }
+                }
+                else
+                {
+                    txt = new List<string>();
+                }
+                // 整理输出内容
+                if (begIndex < endIndex)
+                {
+                    List<string> upPart = txt.GetRange(0, begIndex + 1);
+                    List<string> downPart = txt.GetRange(endIndex, txt.Count - endIndex);
+
+                    result.AddRange(upPart);
+                    result.AddRange(content);
+                    result.AddRange(downPart);
+                }
+                else
+                {
+                    result.AddRange(txt);
+                    result.Add("");
+                    result.Add(begString);
+                    result.AddRange(content);
+                    result.Add(endString);
+                }
+                // 写出文件
+                try
+                {
+                    var utf8WithoutBom = new UTF8Encoding(false);
+                    using (StreamWriter sw = new StreamWriter(file, false, utf8WithoutBom))
+                    {
+                        foreach (var line in result)
+                        {
+                            sw.WriteLine(line);
+                        }
+                    }
+                    return true;
+                }
+                catch (Exception ex) { }
+            }
+            return false;
+        }
     }
 }

+ 3 - 1
Azylee.Utils/Azylee.Core/WindowsUtils/BrowserUtils/BrowserSelector.cs

@@ -21,13 +21,15 @@ namespace Azylee.Core.WindowsUtils.BrowserUtils
         {
             browser = "";
 
+            string edge_86 = @"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe";
             string chrome = @"C:\Program Files\Google\Chrome\Application\chrome.exe";
             string chrome_86 = @"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe";
             string chrome_app = @"C:\Users\Administrator\AppData\Local\Google\Chrome\Application\chrome.exe";
             string firefox = @"C:\Program Files\Mozilla Firefox\firefox.exe";
             string firefox_86 = @"C:\Program Files (x86)\Mozilla Firefox\firefox.exe";
 
-            if (File.Exists(chrome)) browser = chrome;
+            if (File.Exists(edge_86)) browser = edge_86;
+            else if (File.Exists(chrome)) browser = chrome;
             else if (File.Exists(chrome_86)) browser = chrome_86;
             else if (File.Exists(chrome_app)) browser = chrome_app;
             else if (File.Exists(firefox)) browser = firefox;

+ 16 - 10
Azylee.Utils/Tests/Test.Runrun/Form1.Designer.cs

@@ -36,9 +36,10 @@
             // 
             // BTRun
             // 
-            this.BTRun.Location = new System.Drawing.Point(197, 11);
+            this.BTRun.Location = new System.Drawing.Point(263, 14);
+            this.BTRun.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
             this.BTRun.Name = "BTRun";
-            this.BTRun.Size = new System.Drawing.Size(75, 23);
+            this.BTRun.Size = new System.Drawing.Size(100, 29);
             this.BTRun.TabIndex = 0;
             this.BTRun.Text = "运行";
             this.BTRun.UseVisualStyleBackColor = true;
@@ -46,37 +47,42 @@
             // 
             // TBUsername
             // 
-            this.TBUsername.Location = new System.Drawing.Point(13, 13);
+            this.TBUsername.Location = new System.Drawing.Point(17, 16);
+            this.TBUsername.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
             this.TBUsername.Name = "TBUsername";
-            this.TBUsername.Size = new System.Drawing.Size(100, 21);
+            this.TBUsername.Size = new System.Drawing.Size(132, 25);
             this.TBUsername.TabIndex = 1;
             // 
             // TBPassword
             // 
-            this.TBPassword.Location = new System.Drawing.Point(13, 40);
+            this.TBPassword.Location = new System.Drawing.Point(17, 50);
+            this.TBPassword.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
             this.TBPassword.Name = "TBPassword";
-            this.TBPassword.Size = new System.Drawing.Size(100, 21);
+            this.TBPassword.Size = new System.Drawing.Size(132, 25);
             this.TBPassword.TabIndex = 2;
             // 
             // TBApp
             // 
-            this.TBApp.Location = new System.Drawing.Point(12, 136);
+            this.TBApp.Location = new System.Drawing.Point(16, 170);
+            this.TBApp.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
             this.TBApp.Multiline = true;
             this.TBApp.Name = "TBApp";
-            this.TBApp.Size = new System.Drawing.Size(260, 100);
+            this.TBApp.Size = new System.Drawing.Size(345, 124);
             this.TBApp.TabIndex = 3;
             // 
             // Form1
             // 
-            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
+            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
-            this.ClientSize = new System.Drawing.Size(284, 261);
+            this.ClientSize = new System.Drawing.Size(379, 326);
             this.Controls.Add(this.TBApp);
             this.Controls.Add(this.TBPassword);
             this.Controls.Add(this.TBUsername);
             this.Controls.Add(this.BTRun);
+            this.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
             this.Name = "Form1";
             this.Text = "Form1";
+            this.Load += new System.EventHandler(this.Form1_Load);
             this.ResumeLayout(false);
             this.PerformLayout();
 

+ 5 - 0
Azylee.Utils/Tests/Test.Runrun/Form1.cs

@@ -29,5 +29,10 @@ namespace Test.Runrun
             MessageBox.Show($"确认管理员密码为:{ AdminTool.CheckPasswords(pwdlist)}");
 
         }
+
+        private void Form1_Load(object sender, EventArgs e)
+        {
+
+        }
     }
 }

+ 68 - 1
Azylee.Utils/Tests/Test.Runrun/Program.cs

@@ -1,6 +1,11 @@
-using System;
+using Azylee.Core.IOUtils.TxtUtils;
+using Azylee.Core.LogUtils.SimpleLogUtils;
+using Microsoft.SqlServer.Server;
+using System;
 using System.Collections.Generic;
+using System.IO;
 using System.Linq;
+using System.Text;
 using System.Windows.Forms;
 
 namespace Test.Runrun
@@ -13,9 +18,71 @@ namespace Test.Runrun
         [STAThread]
         static void Main()
         {
+            string errorCode = "";
+            string sqlFile = @"D:\code\workspace\histone\cmp7\svn\CMP7_02_工程域\03_SRC\Scripts\04_提交测试脚本\CMP7.3.1\7.3.1.33_0105\hicmp_report(pg+adbpg)\2023-12-25\福建高速专用\fjgsrpt_0302_pur_dt_进货明细报表.sql";
+            Encoding encoding1 = GetFileEncoding(sqlFile);
+            List<string> lines = TxtTool.ReadLine(sqlFile, Encoding.UTF8); // Encoding.GetEncoding("gb2312")
+            foreach (string l in lines)
+            {
+                if (l.Contains("�"))
+                {
+                    errorCode = "乱码";
+                    break;
+                }
+            }
+
+            byte[] bytes = Encoding.UTF8.GetBytes("�");
+
+
+
+            sqlFile = @"D:\code\workspace\histone\cmp7\svn\CMP7_02_工程域\03_SRC\Scripts\04_提交测试脚本\CMP7.3.1\7.3.1.33_0105\hicmp_report(pg+adbpg)\2023-12-25\福建高速专用\fjgsrpt_0501_stk_sum_库存汇总报表.sql";
+            Encoding encoding2 = GetFileEncoding(sqlFile);
+            List<string> lines2 = TxtTool.ReadLine(sqlFile, Encoding.Default);
+
+
+
             Application.EnableVisualStyles();
             Application.SetCompatibleTextRenderingDefault(false);
             Application.Run(new Form1());
         }
+
+        /// <summary>
+        /// 获取指定文件的编码
+        /// 以防止在不知道文件编码格式的情况下处理文件而造成的乱码问题
+        /// </summary>
+        /// <param name="filename">文件路径</param>
+        /// <returns></returns>
+        public static Encoding GetFileEncoding(string filename)
+        {
+            Encoding ReturnReturn = Encoding.Default;
+            if (!File.Exists(filename)) return ReturnReturn;
+
+            try
+            {
+                using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read))
+                {
+                    using (BinaryReader br = new BinaryReader(fs))
+                    {
+                        byte[] buffer = br.ReadBytes(4);
+                        if (buffer.Length > 0 && buffer[0] >= 0xEF)
+                        {
+                            if (buffer[0] == 0xEF && buffer[1] == 0xBB) ReturnReturn = Encoding.UTF8;
+                            else if (buffer[0] == 0xFE && buffer[1] == 0xFF) ReturnReturn = Encoding.BigEndianUnicode;
+                            else if (buffer[0] == 0xFF && buffer[1] == 0xFE) ReturnReturn = Encoding.Unicode;
+                            else ReturnReturn = Encoding.Default;
+                        }
+                        else if (buffer.Length > 0 && buffer[0] == 0xe4 && buffer[1] == 0xbd)
+                            ReturnReturn = Encoding.UTF8;
+                        else
+                            ReturnReturn = Encoding.Default;
+                    }
+                }
+            }
+            catch (Exception ex)
+            {
+                ReturnReturn = Encoding.Default;
+            }
+            return ReturnReturn;
+        }
     }
 }

+ 6 - 4
Azylee.Utils/Tests/Test.Runrun/Test.Runrun.csproj

@@ -31,9 +31,6 @@
     <WarningLevel>4</WarningLevel>
   </PropertyGroup>
   <ItemGroup>
-    <Reference Include="Azylee.Core, Version=1.0.0.8, Culture=neutral, processorArchitecture=MSIL">
-      <HintPath>..\..\packages\Azylee.Core.1.0.0.8\lib\Azylee.Core.dll</HintPath>
-    </Reference>
     <Reference Include="System" />
     <Reference Include="System.Core" />
     <Reference Include="System.Xml.Linq" />
@@ -66,7 +63,6 @@
       <AutoGen>True</AutoGen>
       <DependentUpon>Resources.resx</DependentUpon>
     </Compile>
-    <None Include="packages.config" />
     <None Include="Properties\Settings.settings">
       <Generator>SettingsSingleFileGenerator</Generator>
       <LastGenOutput>Settings.Designer.cs</LastGenOutput>
@@ -77,5 +73,11 @@
       <DesignTimeSharedInput>True</DesignTimeSharedInput>
     </Compile>
   </ItemGroup>
+  <ItemGroup>
+    <ProjectReference Include="..\..\Azylee.Core\Azylee.Core.csproj">
+      <Project>{88dc61fa-95f0-41b7-9d7d-ab0f3cbd169c}</Project>
+      <Name>Azylee.Core</Name>
+    </ProjectReference>
+  </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
 </Project>

+ 0 - 4
Azylee.Utils/Tests/Test.Runrun/packages.config

@@ -1,4 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<packages>
-  <package id="Azylee.Core" version="1.0.0.8" targetFramework="net40" />
-</packages>