浏览代码

程序配置定义增强,数据库工具接口定义

于正洋 4 年之前
父节点
当前提交
2b7f65e93b

+ 6 - 1
Azylee.Utils/Azylee.Core/AppUtils/AppConfigUtils/AppConfig.cs

@@ -1,4 +1,5 @@
-using Azylee.Core.AppUtils.AppConfigUtils.AppConfigModels;
+using Azylee.Core.AppUtils.AppConfigUtils.AppConfigInterfaces;
+using Azylee.Core.AppUtils.AppConfigUtils.AppConfigModels;
 using System;
 using System.Collections.Generic;
 using System.IO;
@@ -9,6 +10,10 @@ namespace Azylee.Core.AppUtils.AppConfigUtils
 {
     /// <summary>
     /// AppConfig 配置管理器
+    /// 
+    /// 如何使用:
+    /// 1. JSON工具中,已实现了基于JSO格式的配置管理器(即转换为JSON保存为文件)
+    /// 2. 如果需要实现其他方式的配置管理,如:二进制文件、ini文件、数据库,可继承并实现对应方法
     /// </summary>
     /// <typeparam name="T"></typeparam>
     public abstract class AppConfig<T> where T : IAppConfigModel, new()

+ 25 - 0
Azylee.Utils/Azylee.Core/AppUtils/AppConfigUtils/AppConfigInterfaces/IAppConfigItemModel.cs

@@ -0,0 +1,25 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Azylee.Core.AppUtils.AppConfigUtils.AppConfigInterfaces
+{
+    /// <summary>
+    /// AppConfig 配置管理器 详细配置项接口
+    /// </summary>
+    public interface IAppConfigItemModel
+    {
+        /// <summary>
+        /// 配置项序号
+        /// </summary>
+        /// <returns></returns>
+        int GetOrderNumber();
+
+        /// <summary>
+        /// 配置项唯一名称
+        /// </summary>
+        /// <returns></returns>
+        string GetUniqueName();
+    }
+}

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

@@ -3,7 +3,7 @@ using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 
-namespace Azylee.Core.AppUtils.AppConfigUtils.AppConfigModels
+namespace Azylee.Core.AppUtils.AppConfigUtils.AppConfigInterfaces
 {
     /// <summary>
     /// AppConfig 配置管理器 指定模型接口

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

@@ -0,0 +1,189 @@
+using Azylee.Core.AppUtils.AppConfigUtils.AppConfigInterfaces;
+using Azylee.Core.DataUtils.StringUtils;
+using Azylee.Core.DbUtils;
+using Azylee.Core.DbUtils.DbModels;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Azylee.Core.AppUtils.AppConfigUtils.AppConfigModels
+{
+    /// <summary>
+    /// 数据库配置信息
+    /// </summary>
+    public class AppConfigDbItem : IAppConfigItemModel
+    {
+        /// <summary>
+        /// 序号
+        /// </summary>
+        public int Number { get; set; }
+        /// <summary>
+        /// 唯一名称
+        /// </summary>
+        public string Name { get; set; }
+        /// <summary>
+        /// 服务器IP地址
+        /// </summary>
+        public string Server { get; set; }
+        /// <summary>
+        /// 服务器端口号
+        /// </summary>
+        public string Port { get; set; }
+        /// <summary>
+        /// 登录用户ID
+        /// </summary>
+        public string UserId { get; set; }
+        /// <summary>
+        /// 登录用户密码
+        /// </summary>
+        public string Password { get; set; }
+        /// <summary>
+        /// 数据库类型
+        /// </summary>
+        public string Type { get; set; }
+        /// <summary>
+        /// 默认数据库
+        /// </summary>
+        public string Database { get; set; }
+        /// <summary>
+        /// 扩展连接字符串
+        /// </summary>
+        public string ExtConnectString { get; set; }
+        /// <summary>
+        /// 描述信息
+        /// </summary>
+        public string Desc { get; set; }
+
+        /// <summary>
+        /// 全参数构造函数
+        /// </summary>
+        /// <param name="number"></param>
+        /// <param name="type"></param>
+        /// <param name="name"></param>
+        /// <param name="server"></param>
+        /// <param name="port"></param>
+        /// <param name="userid"></param>
+        /// <param name="password"></param>
+        /// <param name="database"></param>
+        /// <param name="desc"></param>
+        public AppConfigDbItem(int number, string type, string name, string server, string port, string userid, string password, string database, string desc)
+        {
+            Number = number;
+            Type = type;
+            Name = name;
+            Server = server;
+            Port = port;
+            UserId = userid;
+            Password = password;
+            Database = database;
+            Desc = desc;
+        }
+
+        public DatabaseType DbType()
+        {
+            switch (Type.ToLower())
+            {
+                case "pg":
+                case "postgresql": return DatabaseType.PostgreSQL;
+
+                case "mysql":
+                default:
+                    return DatabaseType.Mysql;
+            }
+        }
+        /// <summary>
+        /// 连接字符串
+        /// </summary>
+        /// <param name="database">执行连接库</param>
+        /// <returns></returns>
+        public string ConnectionString(string database = null)
+        {
+            if (!Str.Ok(database)) database = Database;
+            switch (DbType())
+            {
+                case DatabaseType.PostgreSQL:
+                    {
+                        return $"Server = {Server}; Port = {(Str.Ok(Port) ? Port : "3306")}; User Id = {UserId}; Password = {Password}; Database = {database}; {ExtConnectString}";
+                    }
+
+                case DatabaseType.Mysql:
+                default:
+                    {
+                        return $"server = {Server}; port = {(Str.Ok(Port) ? Port : "3306")}; userid = {UserId}; password = {Password}; database = {database}; persistsecurityinfo = True; {ExtConnectString}";
+                    }
+            }
+        }
+
+        /// <summary>
+        /// 表空间查询连接字符串
+        /// </summary>
+        /// <returns></returns>
+        public string SchemaConnectionString()
+        {
+            switch (DbType())
+            {
+                case DatabaseType.PostgreSQL:
+                    {
+                        return ConnectionString("");
+                    }
+
+                case DatabaseType.Mysql:
+                default:
+                    {
+                        return ConnectionString("information_schema"); ;
+                    }
+            }
+        }
+        /// <summary>
+        /// 测试连接查询语句
+        /// </summary>
+        /// <returns></returns>
+        public string ValidationQuery()
+        {
+            switch (DbType())
+            {
+                case DatabaseType.PostgreSQL:
+                case DatabaseType.Mysql:
+                default:
+                    {
+
+                        return "select now()";
+                    }
+            }
+        }
+        /// <summary>
+        /// 表空间查询语句
+        /// </summary>
+        /// <returns></returns>
+        public string SchemaQuery()
+        {
+            switch (DbType())
+            {
+                case DatabaseType.PostgreSQL:
+                    return "SELECT datname AS schema_name FROM pg_database";
+
+                case DatabaseType.Mysql:
+                default:
+                    return "SELECT schema_name FROM `SCHEMATA`";
+            }
+        }
+
+        /// <summary>
+        /// 排序序号
+        /// </summary>
+        /// <returns></returns>
+        public int GetOrderNumber()
+        {
+            return this.Number;
+        }
+        /// <summary>
+        /// 唯一名称
+        /// </summary>
+        /// <returns></returns>
+        public string GetUniqueName()
+        {
+            return this.Name;
+        }
+    }
+}

+ 49 - 0
Azylee.Utils/Azylee.Core/AppUtils/AppConfigUtils/AppConfigModels/AppConfigFileItem.cs

@@ -0,0 +1,49 @@
+using Azylee.Core.AppUtils.AppConfigUtils.AppConfigInterfaces;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Azylee.Core.AppUtils.AppConfigUtils.AppConfigModels
+{
+    public class AppConfigFileItem : IAppConfigItemModel
+    {
+        public int Number { get; set; }
+        public string Name { get; set; }
+        public Dictionary<string, string> Datas { get; set; }
+
+        public AppConfigFileItem()
+        {
+            this.Datas = new Dictionary<string, string>();
+        }
+        public string GetValue(string key, string defaultValue = "")
+        {
+            if (Datas.TryGetValue(key, out string value))
+            {
+                return value;
+            }
+            return defaultValue;
+        }
+        public void SetValue(string key, string value)
+        {
+            if (Datas.ContainsKey(key))
+            {
+                Datas[key] = value;
+            }
+            else
+            {
+                Datas.Add(key, value);
+            }
+        }
+
+        public int GetOrderNumber()
+        {
+            return this.Number;
+        }
+
+        public string GetUniqueName()
+        {
+            return this.Name;
+        }
+    }
+}

+ 49 - 0
Azylee.Utils/Azylee.Core/AppUtils/AppConfigUtils/AppConfigModels/AppConfigItem.cs

@@ -0,0 +1,49 @@
+using Azylee.Core.AppUtils.AppConfigUtils.AppConfigInterfaces;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Azylee.Core.AppUtils.AppConfigUtils.AppConfigModels
+{
+    public class AppConfigItem : IAppConfigItemModel
+    {
+        public int Number { get; set; }
+        public string Name { get; set; }
+        public Dictionary<string, string> Datas { get; set; }
+
+        public AppConfigItem()
+        {
+            this.Datas = new Dictionary<string, string>();
+        }
+        public string GetValue(string key, string defaultValue = "")
+        {
+            if (Datas.TryGetValue(key, out string value))
+            {
+                return value;
+            }
+            return defaultValue;
+        }
+        public void SetValue(string key, string value)
+        {
+            if (Datas.ContainsKey(key))
+            {
+                Datas[key] = value;
+            }
+            else
+            {
+                Datas.Add(key, value);
+            }
+        }
+
+        public int GetOrderNumber()
+        {
+            return this.Number;
+        }
+
+        public string GetUniqueName()
+        {
+            return this.Name;
+        }
+    }
+}

+ 6 - 1
Azylee.Utils/Azylee.Core/AppUtils/AppConfigUtils/AppConfigModels/AppConfigRegionModel.cs

@@ -1,4 +1,5 @@
-using Azylee.Core.DataUtils.CollectionUtils;
+using Azylee.Core.AppUtils.AppConfigUtils.AppConfigInterfaces;
+using Azylee.Core.DataUtils.CollectionUtils;
 using System;
 using System.Collections.Generic;
 using System.Linq;
@@ -6,6 +7,10 @@ using System.Text;
 
 namespace Azylee.Core.AppUtils.AppConfigUtils.AppConfigModels
 {
+    /// <summary>
+    /// 配置块模型
+    /// </summary>
+    /// <typeparam name="T"></typeparam>
     public class AppConfigRegionModel<T> where T : IAppConfigItemModel
     {
         public List<T> Items { get; set; }

+ 0 - 13
Azylee.Utils/Azylee.Core/AppUtils/AppConfigUtils/AppConfigModels/IAppConfigItemModel.cs

@@ -1,13 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-
-namespace Azylee.Core.AppUtils.AppConfigUtils.AppConfigModels
-{
-    public interface IAppConfigItemModel
-    {
-        int GetOrderNumber();
-        string GetUniqueName();
-    }
-}

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

@@ -47,9 +47,12 @@
   </ItemGroup>
   <ItemGroup>
     <Compile Include="AppUtils\AppConfigUtils\AppConfig.cs" />
-    <Compile Include="AppUtils\AppConfigUtils\AppConfigModels\IAppConfigItemModel.cs" />
+    <Compile Include="AppUtils\AppConfigUtils\AppConfigInterfaces\IAppConfigItemModel.cs" />
+    <Compile Include="AppUtils\AppConfigUtils\AppConfigModels\AppConfigDbItem.cs" />
+    <Compile Include="AppUtils\AppConfigUtils\AppConfigModels\AppConfigFileItem.cs" />
+    <Compile Include="AppUtils\AppConfigUtils\AppConfigModels\AppConfigItem.cs" />
     <Compile Include="AppUtils\AppConfigUtils\AppConfigModels\AppConfigRegionModel.cs" />
-    <Compile Include="AppUtils\AppConfigUtils\AppConfigModels\IAppConfigModel.cs" />
+    <Compile Include="AppUtils\AppConfigUtils\AppConfigInterfaces\IAppConfigModel.cs" />
     <Compile Include="AppUtils\AppInfoTool.cs" />
     <Compile Include="AppUtils\AppLaunchTool.cs" />
     <Compile Include="AppUtils\AppSettleTool.cs" />
@@ -84,6 +87,9 @@
     <Compile Include="DataUtils\StringUtils\StringExtension.cs" />
     <Compile Include="DataUtils\StringUtils\StringTool.cs" />
     <Compile Include="DataUtils\UnitConvertUtils\ByteConvertTool.cs" />
+    <Compile Include="DbUtils\DbModels\DatabaseType.cs" />
+    <Compile Include="DbUtils\DbInterface\IDatabaseHelper.cs" />
+    <Compile Include="DbUtils\DbSqls\DbSqlTool.cs" />
     <Compile Include="DelegateUtils\ProcessDelegateUtils\ProgressDelegate.cs" />
     <Compile Include="DelegateUtils\ProcessDelegateUtils\ProgressEventArgs.cs" />
     <Compile Include="DllUtils\DllInvokeTool.cs" />

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

@@ -0,0 +1,49 @@
+using Azylee.Core.AppUtils.AppConfigUtils.AppConfigModels;
+using Azylee.Core.DbUtils.DbModels;
+using System;
+using System.Collections.Generic;
+using System.Data;
+using System.Linq;
+using System.Text;
+
+namespace Azylee.Core.DbUtils.DbInterface
+{
+    /// <summary>
+    /// 数据库工具类接口定义
+    /// </summary>
+    public interface IDatabaseHelper : IDisposable
+    {
+        /// <summary>
+        /// 创建并打开连接
+        /// 注意:通常在构造函数中直接调用
+        /// </summary>
+        /// <returns></returns>
+        bool OpenConnect();
+
+        /// <summary>
+        /// 测试连接
+        /// </summary>
+        /// <returns></returns>
+        bool TestConnect();
+
+        /// <summary>
+        /// 普通查询
+        /// </summary>
+        /// <param name="sql"></param>
+        /// <returns></returns>
+        DataTable Select(string sql);
+        /// <summary>
+        /// 查询所有数据库名称
+        /// </summary>
+        /// <returns></returns>
+        DataTable SchemaList();
+
+        /// <summary>
+        /// 执行文件
+        /// </summary>
+        /// <param name="SqlFile"></param>
+        /// <param name="action"></param>
+        /// <returns></returns>
+        Tuple<bool, int, string> ExecuteFile(string SqlFile, Action<string, bool, int, string> action);
+    }
+}

+ 13 - 0
Azylee.Utils/Azylee.Core/DbUtils/DbModels/DatabaseType.cs

@@ -0,0 +1,13 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Azylee.Core.DbUtils.DbModels
+{
+    public enum DatabaseType
+    {
+        Mysql,
+        PostgreSQL
+    }
+}

+ 12 - 0
Azylee.Utils/Azylee.Core/DbUtils/DbSqls/DbSqlTool.cs

@@ -0,0 +1,12 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Azylee.Core.DbUtils.DbSqls
+{
+    public class DbSqlTool
+    {
+        // 目前预制的SQL语句都在 AppConfigDBItem 类中
+    }
+}

+ 1 - 1
Azylee.Utils/Azylee.Core/Properties/AssemblyInfo.cs

@@ -32,7 +32,7 @@ using System.Runtime.InteropServices;
 // 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号
 //通过使用 "*",如下所示:
 // [assembly: AssemblyVersion("1.0.*")]
-[assembly: AssemblyVersion("1.0.1.0")]
+[assembly: AssemblyVersion("1.0.1.1")]
 //v*.*.*.*(年月日)//+添加 -删减 @优化 #修复
 
 // v 1.0.1.0(2019年7月4日)

+ 7 - 0
Azylee.Utils/Azylee.Jsons/JsonAppConfigUtils/JsonAppConfig.cs

@@ -1,4 +1,5 @@
 using Azylee.Core.AppUtils.AppConfigUtils;
+using Azylee.Core.AppUtils.AppConfigUtils.AppConfigInterfaces;
 using Azylee.Core.AppUtils.AppConfigUtils.AppConfigModels;
 using Azylee.Core.IOUtils.FileUtils;
 using Azylee.Core.IOUtils.TxtUtils;
@@ -12,6 +13,12 @@ namespace Azylee.Jsons.JsonAppConfigUtils
 {
     /// <summary>
     /// Json 配置管理器
+    /// 
+    /// 如何使用:
+    /// 1. 根据自己需求,创建配置类,实现IAppConfigModel接口
+    /// 2. 使用配置管理器,new出创建的配置类
+    /// 3. 通过配置管理器,管理配置信息即可
+    /// PS. 建议直接使用静态变量创建
     /// </summary>
     /// <typeparam name="T"></typeparam>
     public class JsonAppConfig<T> : AppConfig<T> where T : IAppConfigModel, new()

+ 2 - 1
Azylee.Utils/Tests/Test.Ges/User.cs

@@ -1,4 +1,5 @@
-using Azylee.Core.AppUtils.AppConfigUtils.AppConfigModels;
+using Azylee.Core.AppUtils.AppConfigUtils.AppConfigInterfaces;
+using Azylee.Core.AppUtils.AppConfigUtils.AppConfigModels;
 using Azylee.Core.DataUtils.CollectionUtils;
 using System;
 using System.Collections.Generic;

+ 2 - 1
Azylee.Utils/Tests/Test.Ges/UserBook.cs

@@ -1,4 +1,5 @@
-using Azylee.Core.AppUtils.AppConfigUtils.AppConfigModels;
+using Azylee.Core.AppUtils.AppConfigUtils.AppConfigInterfaces;
+using Azylee.Core.AppUtils.AppConfigUtils.AppConfigModels;
 using System;
 using System.Collections.Generic;
 using System.Linq;