diff --git a/Src/Asp.NetCore2/XuGuTest/BaseDataLogic.cs b/Src/Asp.NetCore2/XuGuTest/BaseDataLogic.cs deleted file mode 100644 index fc83e4839..000000000 --- a/Src/Asp.NetCore2/XuGuTest/BaseDataLogic.cs +++ /dev/null @@ -1,216 +0,0 @@ -using SqlSugar; -using Mapster; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Linq.Expressions; -using System.Reflection; -using System.Threading.Tasks; -using System.Diagnostics; -using SqlSugar.Xugu; - -namespace Data_logic -{ - /// - /// 业务逻辑基类 - /// - /// 与数据库中表的结构一致的模型类型 - public class BaseDataLogic where T : class, new() - { - /// - /// 主键个数 - /// - private int primarykeyCount = -1; - /// - /// 主键属性信息 - /// - private PropertyInfo primarykeyInfo = null; - /// - /// 构造函数 - /// - public BaseDataLogic() - { - //获取主键个数 - if (primarykeyCount == -1) - { - var list = db.EntityMaintenance.GetEntityInfo().Columns.Where(d => d.IsPrimarykey == true); - primarykeyCount = list.Count(); - primarykeyInfo = primarykeyCount == 1 ? list.FirstOrDefault().PropertyInfo : null; - } - //* - db.Aop.OnLogExecuting = (sql, pars) => - { - Debugger.Log(1, "SQL", sql); - }; - //*/ - } - /// - /// SqlSugar 操作对象,单例用SqlSugarClient,否则用SqlSugarClient - /// - protected static SqlSugarScope db = new SqlSugarScope(new ConnectionConfig() - { - ConnectionString = "IP=10.1.1.1;DB=SYSTEM;User=SYSDBA;PWD=SYSDBA;Port=5138;AUTO_COMMIT=on;CHAR_SET=UTF8",//CHAR_SET=GBK - DbType = DbType.Xugu, - IsAutoCloseConnection = true, - //ConfigureExternalServices = new ConfigureExternalServices() { SqlFuncServices = SqlFuncCustom.Methods } - }); - /// - /// 从数据库中生成模型类文件 - /// - /// 文件放置的位置 - /// 默认命名空间 - /// 是否生成所有表。是则包含所有T开头的表,否则只生成当前对象名称相同的表 - public void CreateModel(string path, string nameSpace = "Data.Model", bool allTable = false) - { - db.DbFirst.Where(c => allTable ? c.StartsWith("T") : typeof(T).Name == c) - .IsCreateAttribute().IsCreateDefaultValue() - .CreateClassFile(path, nameSpace); - } - /// - /// 添加一条数据 - /// - /// 视图模型类型 - /// 视图模型,仅包含模型中有的且值不为Null的字段 - /// 是否添加成功,已存在视为false - public async Task Add(TModel model) where TModel : class - { - if (db.Queryable().In(primarykeyInfo.GetValue(model.Adapt())).Any()) return false; - return await db.Insertable(model) - .IgnoreColumns(ignoreNullColumn: true) - .ExecuteReturnBigIdentityAsync().ContinueWith(t => - t.Result>0 - ); - } - /// - /// 根据主键或根据条件更新数据 - /// - /// 视图模型类型 - /// 视图模型,仅包含模型中有的且值不为Null的字段 - /// 查询条件,当条件为Null时为主键更新(T必须配置主键),否则为条件批量更新 - /// 更新是否成功,不存在视为false - /// 类型T未配置主键或有多个主键 - public async Task Update(TModel model, Expression> where = null) where TModel : class - { - if (where != null && this.primarykeyCount != 1) throw new Exception($"类型'{typeof(T).Name}'未配置主键或有多个主键"); - var expression = db.Updateable(model); - if (where != null) expression = expression.Where(where); - return await expression - .IgnoreColumns(ignoreAllNullColumns: true) - .ExecuteCommandHasChangeAsync(); - } - /// - /// 根据主键插入或更新一条数据 - /// - /// 视图模型类型(必须配置主键) - /// 视图模型,仅包含模型中有的且值不为Null的字段 - /// 更新是否成功 - /// 类型T未配置主键或有多个主键 - public async Task AddOrUpdate(TModel model) where TModel : class, new() - { - if (this.primarykeyCount != 1) throw new Exception($"类型'{typeof(T).Name}'未配置主键或有多个主键"); - - if (!await db.Queryable().In(primarykeyInfo.GetValue(model.Adapt())).AnyAsync()) return await Add(model); - else return await Update(model); - /* - var o = db.Storageable(model.Adapt()).ToStorage(); - o.AsInsertable.ExecuteCommand();//不存在插入 - o.AsUpdateable.ExecuteCommand();//存在更新 - - return await db.Storageable(model.Adapt()) - .ExecuteCommandAsync() - .ContinueWith(t => t.Result > 0); - */ - } - /// - /// 根据主键删除数据 - /// - /// 主键的类型 - /// 主键值 - /// 删除是否成功 - /// 类型T未配置主键或有多个主键 - public async Task Delete(TKey id) - { - if (this.primarykeyCount != 1) throw new Exception($"类型'{typeof(T).Name}'未配置主键或有多个主键"); - //不存在时视为删除成功 - if (!await db.Queryable().In(id).AnyAsync()) return true; - return await db.Deleteable(id).ExecuteCommandHasChangeAsync(); - } - /// - /// 根据查询条件删除数据 - /// - /// 查询条件表达式 - /// 删除是否成功 - public async Task Delete(Expression> where) - { - //不存在时视为删除成功 - if (!await db.Queryable().Where(where).AnyAsync()) return true; - return await db.Deleteable(where).ExecuteCommandHasChangeAsync(); - } - /// - /// 根据单个主键获取一条数据 - /// - /// 要返回的模型类型 - /// 主键的类型 - /// 主键值 - /// 返回指定主键值的数据 - /// 类型T未配置主键或有多个主键 - public async Task Single(TKey id) - { - if (this.primarykeyCount != 1) throw new Exception($"类型'{typeof(T).Name}'未配置主键或有多个主键"); - return await db.Queryable().InSingleAsync(id).ContinueWith(t => t.Result.Adapt()); - } - /// - /// 根据条件获取唯一数据 - /// - /// 要返回的模型类型 - /// 主键的类型 - /// 查询条件 - /// 返回符合指定条件的唯一数据,不唯一时将抛出异常 - public async Task Single(Expression> where) - { - return await db.Queryable().Where(where).Select().SingleAsync(); - } - /// - /// 获取符合条件的分页数据 - /// - /// 列表模型的类型 - /// 条件表达式 - /// 排序表达式 - /// 排序方向,默认顺序 - /// 页码,从1开始 - /// 每页条数,默认20 - /// 符合条件的分页数据列表,以及总条数 - public async Task<(List, int)> List(Expression> where = null - , Expression> order = null, OrderByType orderType = OrderByType.Asc - , int pageNum = 1, int pageSize = 20, bool noPager = false) - { - var expression = db.Queryable(); - if (where != null && where.ToString() != "it => True") expression = expression.Where(where); - if (order != null) expression = expression.OrderBy(order, orderType); - - var expressionTModel = expression.Select(); - if (noPager) return await expressionTModel.ToListAsync().ContinueWith(d => (d.Result, d.Result.Count)); - else return await ToPagedList(expressionTModel, pageNum, pageSize); - } - /// - /// 通用分页 - /// - /// 列表模型的类型 - /// 查询表达式 - /// 页码,从1开始 - /// 每页条数,默认20 - /// 返回查询表达式分页后的数据列表,以及总条数 - protected static async Task<(List, int)> ToPagedList(ISugarQueryable expression, int pageNum = 1, int pageSize = 20) - { - RefAsync totalCount = 0; - return await expression - .ToPageListAsync/*ToOffsetPageAsync//2012以上才支持*/(pageNum, pageSize, totalCount) - .ContinueWith(d => (d.Result, totalCount)); - } - /// - /// 获取动态where条件对象 - /// - /// 动态where条件对象 - public Expressionable CreateWhere() => Expressionable.Create(); - } -} \ No newline at end of file diff --git a/Src/Asp.NetCore2/XuGuTest/Program.cs b/Src/Asp.NetCore2/XuGuTest/Program.cs index 310f2ca1e..1c9a47a44 100644 --- a/Src/Asp.NetCore2/XuGuTest/Program.cs +++ b/Src/Asp.NetCore2/XuGuTest/Program.cs @@ -1,5 +1,4 @@ using Data.Model; -using Data_logic; using Microsoft.IdentityModel.Tokens; using SqlSugar; using SqlSugar.DbConvert; @@ -22,38 +21,119 @@ namespace XuguTest { static void Main(string[] args) { - - Task.Run(async () => + + + SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { - var o = new UserService(); - //o.CreateModel(@"F:\!GZH\Models\南海气象数据支撑系统", allTable: true); - var task = o.GetList(); - var data = task.First(); - data.ID = task.Last().ID + 1; - //data.C_BINARY = File.ReadAllBytes(@"C:\Users\28679\Desktop\项目网址(新).docx"); - //data.C_DATE = DateTime.Now.AddDays(-10); - var r = await o.Add(data); - ////////////////////////var d1 = await o.Delete(o => o.C_BIGINT >=154 && o.C_BIGINT <=166); - ////////////////////////var d = await o.Delete(o=>o.ID>1); + ConnectionString = "IP=118.123.17.3;DB=HOUSE;User=SYSDBA;PWD=SYSDBA;Port=5138;AUTO_COMMIT=on;CHAR_SET=UTF8",//CHAR_SET=GBK + DbType = SqlSugar.DbType.Xugu, + IsAutoCloseConnection = true, + //ConfigureExternalServices = new ConfigureExternalServices() { SqlFuncServices = SqlFuncCustom.Methods } + }, + db => { + db.Aop.OnLogExecuting = (sql, pars) => + { + + Console.WriteLine(UtilMethods.GetNativeSql(sql,pars)); + }; + }); - var u = await o.Single(t=>t.C_DATETIME > DateTime.Today); - u.C_DATETIME = DateTime.Now.AddDays(2); - var b = await o.Update(u); - Console.WriteLine("Hello World!"); - }).Wait(); + db.CodeFirst.InitTables(); + + db.DbMaintenance.TruncateTable(); + + db.Insertable(new MY_USER() + { + C_BIGINT = 1, + C_BINARY = new byte[] { 1 }, + C_BLOB = new byte[] { 1}, + C_BOOLEAN = true, + C_CHAR = "A", + C_CLOB = "A", + C_DATE = DateTime.Now, + C_DATETIME = DateTime.Now, + C_DATETIME_WITH_TIME_ZONE = DateTimeOffset.Now, + C_DECIMAL = 1.1M, + C_DOUBLE = 1.1, + C_FLOAT = 1.1F, + C_GUID = Guid.NewGuid(), + C_INT = 1, + C_INTEGER = 1, + C_INTERVAL_DAY = "A", + C_INTERVAL_DAY_TO_HOUR = "A", + C_INTERVAL_DAY_TO_MINUTE = "A", + C_INTERVAL_DAY_TO_SECOND = "A", + C_INTERVAL_HOUR = "A", + C_INTERVAL_HOUR_TO_MINUTE = "A", + C_INTERVAL_HOUR_TO_SECOND = "A", + C_INTERVAL_MINUTE = "A", + C_INTERVAL_MINUTE_TO_SECOND = "A", + C_INTERVAL_MONTH = "A", + C_INTERVAL_SECOND = "A", + C_INTERVAL_YEAR = "2001", + C_INTERVAL_YEAR_TO_MONTH = "2", + C_NCHAR = "A", + C_NUMERIC = 1.1M, + C_NVARCHAR = "A", + C_ROWID = "A", + C_TIMESTAMP = DateTime.Now, + C_TIME = DateTimeOffset.Now.TimeOfDay, + C_TINYINT = 1, + C_VARCHAR = "A", + C_TIMESTAMP_AUTO_UPDATE = DateTime.Now, + C_TIME_WITH_TIME_ZONE = DateTime.Now.TimeOfDay + + }).ExecuteCommand(); + + // var list0=db.Ado.GetDataTable("select * from MY_USER"); + + var list = db.Queryable().ToList(); + list.ForEach(it => + { + Console.WriteLine(it.C_BIGINT); + Console.WriteLine(it.C_BINARY); + Console.WriteLine(it.C_BLOB); + Console.WriteLine(it.C_BOOLEAN); + Console.WriteLine(it.C_CHAR); + Console.WriteLine(it.C_CLOB); + Console.WriteLine(it.C_DATE); + Console.WriteLine(it.C_DATETIME); + Console.WriteLine(it.C_DATETIME_WITH_TIME_ZONE); + Console.WriteLine(it.C_DECIMAL); + Console.WriteLine(it.C_DOUBLE); + Console.WriteLine(it.C_FLOAT); + Console.WriteLine(it.C_GUID); + Console.WriteLine(it.C_INT); + Console.WriteLine(it.C_INTEGER); + Console.WriteLine(it.C_INTERVAL_DAY); + Console.WriteLine(it.C_INTERVAL_DAY_TO_HOUR); + Console.WriteLine(it.C_INTERVAL_DAY_TO_MINUTE); + Console.WriteLine(it.C_INTERVAL_DAY_TO_SECOND); + Console.WriteLine(it.C_INTERVAL_HOUR); + Console.WriteLine(it.C_INTERVAL_HOUR_TO_MINUTE); + Console.WriteLine(it.C_INTERVAL_HOUR_TO_SECOND); + Console.WriteLine(it.C_INTERVAL_MINUTE); + Console.WriteLine(it.C_INTERVAL_MINUTE_TO_SECOND); + Console.WriteLine(it.C_INTERVAL_MONTH); + Console.WriteLine(it.C_INTERVAL_SECOND); + Console.WriteLine(it.C_INTERVAL_YEAR); + Console.WriteLine(it.C_INTERVAL_YEAR_TO_MONTH); + Console.WriteLine(it.C_NCHAR); + Console.WriteLine(it.C_NUMERIC); + Console.WriteLine(it.C_NVARCHAR); + Console.WriteLine(it.C_ROWID); + Console.WriteLine(it.C_TIMESTAMP); + Console.WriteLine(it.C_TIME); + Console.WriteLine(it.C_TINYINT); + Console.WriteLine(it.C_VARCHAR); + Console.WriteLine(it.C_TIMESTAMP_AUTO_UPDATE); + Console.WriteLine(it.C_TIME_WITH_TIME_ZONE); + }); + db.Updateable(list).ExecuteCommand(); + db.Deleteable(list).ExecuteCommand(); + Console.ReadKey(); } } - public class UserService : BaseDataLogic - { - public UserService() { - - //db.CodeFirst.InitTables(typeof(T_USER)); - } - public List GetList() - { - return db.Queryable().Where(d=>d.C_DATETIME < DateTime.Now).ToPageList(1,1); - } - } } diff --git a/Src/Asp.NetCore2/XuGuTest/T_USER.cs b/Src/Asp.NetCore2/XuGuTest/T_USER.cs index 865f0f2ce..34e0d5de1 100644 --- a/Src/Asp.NetCore2/XuGuTest/T_USER.cs +++ b/Src/Asp.NetCore2/XuGuTest/T_USER.cs @@ -1,6 +1,7 @@ using System; using System.Data; using System.Linq; +using System.Security.Principal; using System.Text; using SqlSugar; using SqlSugar.Xugu; @@ -11,10 +12,10 @@ namespace Data.Model /// /// /// - [SugarTable("T_USER",TableDescription ="测试表")] - public partial class T_USER + [SugarTable("MY_USER1",TableDescription ="测试表")] + public partial class MY_USER { - public T_USER() + public MY_USER() { @@ -24,15 +25,10 @@ namespace Data.Model /// Default: /// Nullable:False /// - [SugarColumn(IsPrimaryKey = true,ColumnDescription ="主键")] + [SugarColumn(IsPrimaryKey = true, IsIdentity=true, ColumnDescription ="主键")] public int ID { get; set; } - /// - /// Desc:数字类型 - /// Default: - /// Nullable:False - /// - [SugarColumn(IsIdentity = true)] + public long C_BIGINT { get; set; } /// @@ -47,7 +43,7 @@ namespace Data.Model /// Default: /// Nullable:True /// - public object C_BLOB { get; set; } + public byte[] C_BLOB { get; set; } /// /// Desc: @@ -279,12 +275,7 @@ namespace Data.Model [SugarColumn(ColumnDataType = "ROWID")] public string C_ROWID { get; set; } - /// - /// Desc: - /// Default: - /// Nullable:True - /// - public Version C_ROWVERSION { get; set; } + /// /// Desc: