Browse Source

DB简单支持

yuzhengyang 2 years ago
parent
commit
ef999b47ef

+ 14 - 1
Azylee.Utils/Azylee.Core/AppUtils/AppConfigUtils/AppConfigModels/AppConfigDbItem.cs

@@ -124,6 +124,8 @@ namespace Azylee.Core.AppUtils.AppConfigUtils.AppConfigModels
                 case "pg":
                 case "postgresql": return DatabaseType.PostgreSQL;
 
+                case "ddm": return DatabaseType.DDM;
+
                 case "mysql":
                 default:
                     return DatabaseType.Mysql;
@@ -144,6 +146,11 @@ namespace Azylee.Core.AppUtils.AppConfigUtils.AppConfigModels
                         return $"Server = {Server}; Port = {(Str.Ok(Port) ? Port : "3306")}; User Id = {UserId}; Password = {GetPasswordEnc()}; Database = {database}; {JoinConnectString} {ExtConnectString}";
                     }
 
+                case DatabaseType.DDM:
+                    {
+                        return $"server = {Server}; port = {(Str.Ok(Port) ? Port : "3306")}; userid = {UserId}; password = {GetPasswordEnc()}; database = {database}; persistsecurityinfo = True; {JoinConnectString} {ExtConnectString}";
+                    }
+
                 case DatabaseType.Mysql:
                 default:
                     {
@@ -164,7 +171,10 @@ namespace Azylee.Core.AppUtils.AppConfigUtils.AppConfigModels
                     {
                         return "";
                     }
-
+                case DatabaseType.DDM:
+                    {
+                        return "";
+                    }
                 case DatabaseType.Mysql:
                 default:
                     {
@@ -181,6 +191,7 @@ namespace Azylee.Core.AppUtils.AppConfigUtils.AppConfigModels
             switch (DbType())
             {
                 case DatabaseType.PostgreSQL:
+                case DatabaseType.DDM:
                 case DatabaseType.Mysql:
                 default:
                     {
@@ -200,6 +211,8 @@ namespace Azylee.Core.AppUtils.AppConfigUtils.AppConfigModels
                 case DatabaseType.PostgreSQL:
                     return "SELECT datname AS schema_name FROM pg_database";
 
+                case DatabaseType.DDM: return "SHOW DATABASES";
+
                 case DatabaseType.Mysql:
                 default:
                     return "SELECT schema_name FROM `SCHEMATA`";

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

@@ -87,6 +87,7 @@
     <Compile Include="DataUtils\GuidUtils\GuidTool.cs" />
     <Compile Include="DataUtils\SerializeUtils\SerializeTool.cs" />
     <Compile Include="DataUtils\StringUtils\NameFormat.cs" />
+    <Compile Include="DataUtils\StringUtils\NameType.cs" />
     <Compile Include="DataUtils\StringUtils\Str.cs" />
     <Compile Include="DataUtils\StringUtils\StringArrayTool.cs" />
     <Compile Include="DataUtils\StringUtils\StringExtension.cs" />
@@ -96,6 +97,8 @@
     <Compile Include="DataUtils\UnitConvertUtils\ByteConvertTool.cs" />
     <Compile Include="DbUtils\DbModels\DatabaseType.cs" />
     <Compile Include="DbUtils\DbInterface\IDatabaseHelper.cs" />
+    <Compile Include="DbUtils\DbModels\DbmColumn.cs" />
+    <Compile Include="DbUtils\DbModels\DbmTable.cs" />
     <Compile Include="DbUtils\DbSqls\DbSqlTool.cs" />
     <Compile Include="DbUtils\DbSqls\SqlGenerate.cs" />
     <Compile Include="DelegateUtils\ProcessDelegateUtils\ProgressDelegate.cs" />

+ 24 - 1
Azylee.Utils/Azylee.Core/DataUtils/DataTableUtils/DataTableTool.cs

@@ -1,6 +1,8 @@
-using System;
+using Azylee.Core.DataUtils.StringUtils;
+using System;
 using System.Collections.Generic;
 using System.Data;
+using System.Data.SqlClient;
 using System.Linq;
 using System.Text;
 
@@ -16,5 +18,26 @@ namespace Azylee.Core.DataUtils.DataTableUtils
             }
             return false;
         }
+
+        public static List<Dictionary<string, object>> ToDictionary(DataTable data, NameType nameType = NameType.NONE)
+        {
+            List<Dictionary<string, object>> result = new List<Dictionary<string, object>>();
+            if (Ok(data))
+            {
+                List<string> colNames = new List<string>();
+                foreach (DataColumn col in data.Columns) colNames.Add(col.ColumnName);
+
+                foreach (DataRow row in data.Rows)
+                {
+                    Dictionary<string, object> keyValuePairs = new Dictionary<string, object>();
+                    foreach (string name in colNames)
+                    {
+                        keyValuePairs.Add(NameFormat.Format(name, nameType), row[name]);
+                    }
+                    result.Add(keyValuePairs);
+                }
+            }
+            return result;
+        }
     }
 }

+ 19 - 0
Azylee.Utils/Azylee.Core/DataUtils/StringUtils/NameFormat.cs

@@ -105,5 +105,24 @@ namespace Azylee.Core.DataUtils.StringUtils
             }
             return result;
         }
+
+        public static string Format(string s, NameType nameType)
+        {
+            switch (nameType)
+            {
+                case NameType.CAMEL:
+                    return ToCamelCase(s);
+
+                case NameType.UPPER_CAMEL:
+                    return ToUpCamelCase(s);
+
+                case NameType.UNDER_LINE:
+                    return ToUnderline(s);
+
+                case NameType.NONE:
+                default:
+                    return s;
+            }
+        }
     }
 }

+ 15 - 0
Azylee.Utils/Azylee.Core/DataUtils/StringUtils/NameType.cs

@@ -0,0 +1,15 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Azylee.Core.DataUtils.StringUtils
+{
+    public enum NameType
+    {
+        NONE,
+        CAMEL,
+        UPPER_CAMEL,
+        UNDER_LINE
+    }
+}

+ 26 - 0
Azylee.Utils/Azylee.Core/DbUtils/DbInterface/IDatabaseHelper.cs

@@ -59,5 +59,31 @@ namespace Azylee.Core.DbUtils.DbInterface
         /// <param name="action">执行后动作(执行语句,是否成功,影响行数,异常提示信息)</param>
         /// <returns></returns>
         Tuple<bool, int, string> ExecuteFile(string SqlFile, Action<string, bool, int, string> action);
+        /// <summary>
+        /// 执行文件SQL(一段SQL脚本)
+        /// </summary>
+        /// <param name="sql"></param>
+        /// <returns></returns>
+        Tuple<bool, int, string> ExecuteFileSql(string sql);
+        /// <summary>
+        /// 执行SQL(返回影响行数)
+        /// </summary>
+        /// <param name="sql"></param>
+        /// <returns></returns>
+        int ExecuteNonQuery(string sql);
+        /// <summary>
+        /// 查询表清单
+        /// </summary>
+        /// <param name="key"></param>
+        /// <returns></returns>
+        List<DbmTable> QueryTables(string key = "");
+        /// <summary>
+        /// 查询表字段列表
+        /// </summary>
+        /// <param name="database"></param>
+        /// <param name="schema"></param>
+        /// <param name="table"></param>
+        /// <returns></returns>
+        List<DbmColumn> QueryColumns(string database, string schema, string table);
     }
 }

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

@@ -8,6 +8,7 @@ namespace Azylee.Core.DbUtils.DbModels
     public enum DatabaseType
     {
         Mysql,
-        PostgreSQL
+        PostgreSQL,
+        DDM
     }
 }

+ 17 - 0
Azylee.Utils/Azylee.Core/DbUtils/DbModels/DbmColumn.cs

@@ -0,0 +1,17 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Azylee.Core.DbUtils.DbModels
+{
+    public class DbmColumn
+    {
+        public string Field { get; set; }
+        public string Type { get; set; }
+        public int Length { get; set; }
+        public string Null { get; set; }
+        public string Key { get; set; }
+        public string Default { get; set; }
+    }
+}

+ 16 - 0
Azylee.Utils/Azylee.Core/DbUtils/DbModels/DbmTable.cs

@@ -0,0 +1,16 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Azylee.Core.DbUtils.DbModels
+{
+    public class DbmTable
+    {
+        public string Name { get; set; }
+        public long Rows { get; set; }
+        public DateTime CreateTime { get; set; }
+        public DateTime UpdateTime { get; set; }
+        public string Comment { get; set; }
+    }
+}

+ 6 - 6
Azylee.Utils/Azylee.Core/IOUtils/FileUtils/FileCodeTool.cs

@@ -11,14 +11,14 @@ namespace Azylee.Core.IOUtils.FileUtils
     /// <summary>
     /// 获取文件特征码(MD5,SHA1)
     /// </summary>
-    public class FileCodeTool
+    public static class FileCodeTool
     {
         /// <summary>
         /// 计算文件的 MD5 值
         /// </summary>
         /// <param name="fileName">要计算 MD5 值的文件名和路径</param>
         /// <returns>MD5 值16进制字符串</returns>
-        public string GetMD5(string fileName)
+        public static string GetMD5(string fileName)
         {
             return HashFile(fileName, "md5");
         }
@@ -28,7 +28,7 @@ namespace Azylee.Core.IOUtils.FileUtils
         /// </summary>
         /// <param name="fileName">要计算 sha1 值的文件名和路径</param>
         /// <returns>sha1 值16进制字符串</returns>
-        public string GetSHA1(string fileName)
+        public static string GetSHA1(string fileName)
         {
             return HashFile(fileName, "sha1");
         }
@@ -39,7 +39,7 @@ namespace Azylee.Core.IOUtils.FileUtils
         /// <param name="fileName">要计算哈希值的文件名和路径</param>
         /// <param name="algName">算法:sha1,md5</param>
         /// <returns>哈希值16进制字符串</returns>
-        private string HashFile(string fileName, string algName)
+        private static string HashFile(string fileName, string algName)
         {
             if (!System.IO.File.Exists(fileName))
                 return string.Empty;
@@ -56,7 +56,7 @@ namespace Azylee.Core.IOUtils.FileUtils
         /// <param name="stream">要计算哈希值的 Stream</param>
         /// <param name="algName">算法:sha1,md5</param>
         /// <returns>哈希值字节数组</returns>
-        private byte[] HashData(System.IO.Stream stream, string algName)
+        private static byte[] HashData(System.IO.Stream stream, string algName)
         {
             System.Security.Cryptography.HashAlgorithm algorithm;
             if (algName == null)
@@ -81,7 +81,7 @@ namespace Azylee.Core.IOUtils.FileUtils
         /// <summary>
         /// 字节数组转换为16进制表示的字符串
         /// </summary>
-        private string ByteArrayToHexString(byte[] buf)
+        private static string ByteArrayToHexString(byte[] buf)
         {
             return BitConverter.ToString(buf).Replace("-", "");
         }