mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-04-05 17:37:58 +08:00
Update 谷虚数据库
This commit is contained in:
parent
39cd0a0eea
commit
aec81679dc
Src/Asp.NetCore2/XuGuTest
@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// 业务逻辑基类
|
||||
/// </summary>
|
||||
/// <typeparam name="T">与数据库中表的结构一致的模型类型</typeparam>
|
||||
public class BaseDataLogic<T> where T : class, new()
|
||||
{
|
||||
/// <summary>
|
||||
/// 主键个数
|
||||
/// </summary>
|
||||
private int primarykeyCount = -1;
|
||||
/// <summary>
|
||||
/// 主键属性信息
|
||||
/// </summary>
|
||||
private PropertyInfo primarykeyInfo = null;
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
public BaseDataLogic()
|
||||
{
|
||||
//获取主键个数
|
||||
if (primarykeyCount == -1)
|
||||
{
|
||||
var list = db.EntityMaintenance.GetEntityInfo<T>().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);
|
||||
};
|
||||
//*/
|
||||
}
|
||||
/// <summary>
|
||||
/// SqlSugar 操作对象,单例用SqlSugarClient,否则用SqlSugarClient
|
||||
/// </summary>
|
||||
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 }
|
||||
});
|
||||
/// <summary>
|
||||
/// 从数据库中生成模型类文件
|
||||
/// </summary>
|
||||
/// <param name="path">文件放置的位置</param>
|
||||
/// <param name="nameSpace">默认命名空间</param>
|
||||
/// <param name="allTable">是否生成所有表。是则包含所有T开头的表,否则只生成当前对象名称相同的表</param>
|
||||
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);
|
||||
}
|
||||
/// <summary>
|
||||
/// 添加一条数据
|
||||
/// </summary>
|
||||
/// <typeparam name="TModel">视图模型类型</typeparam>
|
||||
/// <param name="model">视图模型,仅包含模型中有的且值不为Null的字段</param>
|
||||
/// <returns>是否添加成功,已存在视为false</returns>
|
||||
public async Task<bool> Add<TModel>(TModel model) where TModel : class
|
||||
{
|
||||
if (db.Queryable<T>().In(primarykeyInfo.GetValue(model.Adapt<T>())).Any()) return false;
|
||||
return await db.Insertable<T>(model)
|
||||
.IgnoreColumns(ignoreNullColumn: true)
|
||||
.ExecuteReturnBigIdentityAsync().ContinueWith(t =>
|
||||
t.Result>0
|
||||
);
|
||||
}
|
||||
/// <summary>
|
||||
/// 根据主键或根据条件更新数据
|
||||
/// </summary>
|
||||
/// <typeparam name="TModel">视图模型类型</typeparam>
|
||||
/// <param name="model">视图模型,仅包含模型中有的且值不为Null的字段</param>
|
||||
/// <param name="where">查询条件,当条件为Null时为主键更新(T必须配置主键),否则为条件批量更新</param>
|
||||
/// <returns>更新是否成功,不存在视为false</returns>
|
||||
/// <exception cref="Exception">类型T未配置主键或有多个主键</exception>
|
||||
public async Task<bool> Update<TModel>(TModel model, Expression<Func<T, bool>> where = null) where TModel : class
|
||||
{
|
||||
if (where != null && this.primarykeyCount != 1) throw new Exception($"类型'{typeof(T).Name}'未配置主键或有多个主键");
|
||||
var expression = db.Updateable<T>(model);
|
||||
if (where != null) expression = expression.Where(where);
|
||||
return await expression
|
||||
.IgnoreColumns(ignoreAllNullColumns: true)
|
||||
.ExecuteCommandHasChangeAsync();
|
||||
}
|
||||
/// <summary>
|
||||
/// 根据主键插入或更新一条数据
|
||||
/// </summary>
|
||||
/// <typeparam name="TModel">视图模型类型(必须配置主键)</typeparam>
|
||||
/// <param name="model">视图模型,仅包含模型中有的且值不为Null的字段</param>
|
||||
/// <returns>更新是否成功</returns>
|
||||
/// <exception cref="Exception">类型T未配置主键或有多个主键</exception>
|
||||
public async Task<bool> AddOrUpdate<TModel>(TModel model) where TModel : class, new()
|
||||
{
|
||||
if (this.primarykeyCount != 1) throw new Exception($"类型'{typeof(T).Name}'未配置主键或有多个主键");
|
||||
|
||||
if (!await db.Queryable<T>().In(primarykeyInfo.GetValue(model.Adapt<T>())).AnyAsync()) return await Add(model);
|
||||
else return await Update(model);
|
||||
/*
|
||||
var o = db.Storageable(model.Adapt<T>()).ToStorage();
|
||||
o.AsInsertable.ExecuteCommand();//不存在插入
|
||||
o.AsUpdateable.ExecuteCommand();//存在更新
|
||||
|
||||
return await db.Storageable(model.Adapt<T>())
|
||||
.ExecuteCommandAsync()
|
||||
.ContinueWith(t => t.Result > 0);
|
||||
*/
|
||||
}
|
||||
/// <summary>
|
||||
/// 根据主键删除数据
|
||||
/// </summary>
|
||||
/// <typeparam name="TKey">主键的类型</typeparam>
|
||||
/// <param name="id">主键值</param>
|
||||
/// <returns>删除是否成功</returns>
|
||||
/// <exception cref="Exception">类型T未配置主键或有多个主键</exception>
|
||||
public async Task<bool> Delete<TKey>(TKey id)
|
||||
{
|
||||
if (this.primarykeyCount != 1) throw new Exception($"类型'{typeof(T).Name}'未配置主键或有多个主键");
|
||||
//不存在时视为删除成功
|
||||
if (!await db.Queryable<T>().In(id).AnyAsync()) return true;
|
||||
return await db.Deleteable<T>(id).ExecuteCommandHasChangeAsync();
|
||||
}
|
||||
/// <summary>
|
||||
/// 根据查询条件删除数据
|
||||
/// </summary>
|
||||
/// <param name="where">查询条件表达式</param>
|
||||
/// <returns>删除是否成功</returns>
|
||||
public async Task<bool> Delete(Expression<Func<T, bool>> where)
|
||||
{
|
||||
//不存在时视为删除成功
|
||||
if (!await db.Queryable<T>().Where(where).AnyAsync()) return true;
|
||||
return await db.Deleteable<T>(where).ExecuteCommandHasChangeAsync();
|
||||
}
|
||||
/// <summary>
|
||||
/// 根据单个主键获取一条数据
|
||||
/// </summary>
|
||||
/// <typeparam name="TModel">要返回的模型类型</typeparam>
|
||||
/// <typeparam name="TKey">主键的类型</typeparam>
|
||||
/// <param name="id">主键值</param>
|
||||
/// <returns>返回指定主键值的数据</returns>
|
||||
/// <exception cref="Exception">类型T未配置主键或有多个主键</exception>
|
||||
public async Task<TModel> Single<TModel, TKey>(TKey id)
|
||||
{
|
||||
if (this.primarykeyCount != 1) throw new Exception($"类型'{typeof(T).Name}'未配置主键或有多个主键");
|
||||
return await db.Queryable<T>().InSingleAsync(id).ContinueWith(t => t.Result.Adapt<TModel>());
|
||||
}
|
||||
/// <summary>
|
||||
/// 根据条件获取唯一数据
|
||||
/// </summary>
|
||||
/// <typeparam name="TModel">要返回的模型类型</typeparam>
|
||||
/// <typeparam name="TKey">主键的类型</typeparam>
|
||||
/// <param name="where">查询条件</param>
|
||||
/// <returns>返回符合指定条件的唯一数据,不唯一时将抛出异常</returns>
|
||||
public async Task<TModel> Single<TModel, TKey>(Expression<Func<T, bool>> where)
|
||||
{
|
||||
return await db.Queryable<T>().Where(where).Select<TModel>().SingleAsync();
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取符合条件的分页数据
|
||||
/// </summary>
|
||||
/// <typeparam name="TModel">列表模型的类型</typeparam>
|
||||
/// <param name="where">条件表达式</param>
|
||||
/// <param name="order">排序表达式</param>
|
||||
/// <param name="orderType">排序方向,默认顺序</param>
|
||||
/// <param name="pageNum">页码,从1开始</param>
|
||||
/// <param name="pageSize">每页条数,默认20</param>
|
||||
/// <returns>符合条件的分页数据列表,以及总条数</returns>
|
||||
public async Task<(List<TModel>, int)> List<TModel>(Expression<Func<T, bool>> where = null
|
||||
, Expression<Func<T, object>> order = null, OrderByType orderType = OrderByType.Asc
|
||||
, int pageNum = 1, int pageSize = 20, bool noPager = false)
|
||||
{
|
||||
var expression = db.Queryable<T>();
|
||||
if (where != null && where.ToString() != "it => True") expression = expression.Where(where);
|
||||
if (order != null) expression = expression.OrderBy(order, orderType);
|
||||
|
||||
var expressionTModel = expression.Select<TModel>();
|
||||
if (noPager) return await expressionTModel.ToListAsync().ContinueWith(d => (d.Result, d.Result.Count));
|
||||
else return await ToPagedList<TModel>(expressionTModel, pageNum, pageSize);
|
||||
}
|
||||
/// <summary>
|
||||
/// 通用分页
|
||||
/// </summary>
|
||||
/// <typeparam name="TModel">列表模型的类型</typeparam>
|
||||
/// <param name="expression">查询表达式</param>
|
||||
/// <param name="pageNum">页码,从1开始</param>
|
||||
/// <param name="pageSize">每页条数,默认20</param>
|
||||
/// <returns>返回查询表达式分页后的数据列表,以及总条数</returns>
|
||||
protected static async Task<(List<TModel>, int)> ToPagedList<TModel>(ISugarQueryable<TModel> expression, int pageNum = 1, int pageSize = 20)
|
||||
{
|
||||
RefAsync<int> totalCount = 0;
|
||||
return await expression
|
||||
.ToPageListAsync/*ToOffsetPageAsync//2012以上才支持*/(pageNum, pageSize, totalCount)
|
||||
.ContinueWith(d => (d.Result, totalCount));
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取动态where条件对象
|
||||
/// </summary>
|
||||
/// <returns>动态where条件对象</returns>
|
||||
public Expressionable<T> CreateWhere() => Expressionable.Create<T>();
|
||||
}
|
||||
}
|
@ -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_USER,int>(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<MY_USER>();
|
||||
|
||||
db.DbMaintenance.TruncateTable<MY_USER>();
|
||||
|
||||
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<MY_USER>().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<T_USER>
|
||||
{
|
||||
public UserService() {
|
||||
|
||||
//db.CodeFirst.InitTables(typeof(T_USER));
|
||||
}
|
||||
public List<T_USER> GetList()
|
||||
{
|
||||
return db.Queryable<T_USER>().Where(d=>d.C_DATETIME < DateTime.Now).ToPageList(1,1);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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
|
||||
///<summary>
|
||||
///
|
||||
///</summary>
|
||||
[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
|
||||
/// </summary>
|
||||
[SugarColumn(IsPrimaryKey = true,ColumnDescription ="主键")]
|
||||
[SugarColumn(IsPrimaryKey = true, IsIdentity=true, ColumnDescription ="主键")]
|
||||
public int ID { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Desc:数字类型
|
||||
/// Default:
|
||||
/// Nullable:False
|
||||
/// </summary>
|
||||
[SugarColumn(IsIdentity = true)]
|
||||
|
||||
public long C_BIGINT { get; set; }
|
||||
|
||||
/// <summary>
|
||||
@ -47,7 +43,7 @@ namespace Data.Model
|
||||
/// Default:
|
||||
/// Nullable:True
|
||||
/// </summary>
|
||||
public object C_BLOB { get; set; }
|
||||
public byte[] C_BLOB { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Desc:
|
||||
@ -279,12 +275,7 @@ namespace Data.Model
|
||||
[SugarColumn(ColumnDataType = "ROWID")]
|
||||
public string C_ROWID { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Desc:
|
||||
/// Default:
|
||||
/// Nullable:True
|
||||
/// </summary>
|
||||
public Version C_ROWVERSION { get; set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Desc:
|
||||
|
Loading…
Reference in New Issue
Block a user