sqlsugar入门(1)-初识sugar正确打开sugar的方式
1、实例化DB
public static SqlSugarClient GetDB(string s)
{
var ssc = new SqlSugarClient(new ConnectionConfig()
{
ConnectionString = s, //必填
DbType = SqlSugar.DbType.SqlServer, //必填
IsAutoCloseConnection = true
});
ssc.SetExpMethods();
ssc.SetEvents();
return ssc;
}
2、自定义扩展方法
public static void SetExpMethods(this SqlSugarClient ssc)
{
var expMethods = new List<SqlFuncExternal>();
expMethods.Add(new SqlFuncExternal()
{
UniqueMethodName = "CastToFloat",
MethodValue = (expInfo, dbType, expContext) =>
{
if (dbType == DbType.SqlServer)
return string.Format("CAST({0} AS float)", expInfo.Args[0].MemberName);
else
throw new Exception("未实现");
}
});
ssc.CurrentConnectionConfig.ConfigureExternalServices = new ConfigureExternalServices()
{
SqlFuncServices = expMethods //set ext method
};
}
3、AOP输出错误的sql
public static void SetEvents(this SqlSugarClient ssc)
{
ssc.Aop.OnError = error =>
{
Regex reg = new Regex(@"[^0-9]+");
var sql = error.Sql;
var pars = error.Parametres as SugarParameter[];
sql = pars.OrderByDescending(o => reg.Replace(o.ParameterName, "").ObjToInt()).ThenByDescending(o => o.ParameterName.Length).Aggregate(sql, (current, p) => current.Replace(p.ParameterName, (p.DbType == System.Data.DbType.Decimal ||
p.DbType == System.Data.DbType.Double ||
p.DbType == System.Data.DbType.Int16 ||
p.DbType == System.Data.DbType.Int32 ||
p.DbType == System.Data.DbType.Int64 ||
p.DbType == System.Data.DbType.Single ||
p.DbType == System.Data.DbType.VarNumeric ||
p.DbType == System.Data.DbType.UInt16 ||
p.DbType == System.Data.DbType.UInt32 ||
p.DbType == System.Data.DbType.UInt64
) ? (p.Value == null || p.Value is System.DBNull) ? "null" : string.Format("{0}", p.Value) : (p.Value == null || p.Value is System.DBNull) ? "null" : string.Format("'{0}'", p.Value))); }; }
4、输出sql
public static string ToSqlString<T>(this ISugarQueryable<T> ISugarQueryable)
{
var tosql = ISugarQueryable.Clone().ToSql();
Regex reg = new Regex(@"[^0-9]+");
var sql = tosql.Key;
var pars = tosql.Value;
if (pars == null)
{
return sql;
}
sql = pars.OrderByDescending(o => reg.Replace(o.ParameterName, "").ObjToInt()).ThenByDescending(o => o.ParameterName.Length).Aggregate(sql, (current, p) => current.Replace(p.ParameterName, (p.DbType == System.Data.DbType.Decimal ||
p.DbType == System.Data.DbType.Double ||
p.DbType == System.Data.DbType.Int16 ||
p.DbType == System.Data.DbType.Int32 ||
p.DbType == System.Data.DbType.Int64 ||
p.DbType == System.Data.DbType.Single ||
p.DbType == System.Data.DbType.VarNumeric ||
p.DbType == System.Data.DbType.UInt16 ||
p.DbType == System.Data.DbType.UInt32 ||
p.DbType == System.Data.DbType.UInt64
) ? (p.Value == null || p.Value is System.DBNull) ? "null" : string.Format("{0}", p.Value) : (p.Value == null || p.Value is System.DBNull) ? "null" : string.Format("'{0}'", p.Value)));
return sql;
} public static string ToSqlString<T>(this IUpdateable<T> IUpdateable) where T : class, new()
{
var tosql = IUpdateable.ToSql();
Regex reg = new Regex(@"[^0-9]+");
var sql = tosql.Key;
var pars = tosql.Value;
if (pars == null)
{
return sql;
}
sql = pars.OrderByDescending(o => reg.Replace(o.ParameterName, "").ObjToInt()).ThenByDescending(o => o.ParameterName.Length).Aggregate(sql, (current, p) => current.Replace(p.ParameterName, (p.DbType == System.Data.DbType.Decimal ||
p.DbType == System.Data.DbType.Double ||
p.DbType == System.Data.DbType.Int16 ||
p.DbType == System.Data.DbType.Int32 ||
p.DbType == System.Data.DbType.Int64 ||
p.DbType == System.Data.DbType.Single ||
p.DbType == System.Data.DbType.VarNumeric ||
p.DbType == System.Data.DbType.UInt16 ||
p.DbType == System.Data.DbType.UInt32 ||
p.DbType == System.Data.DbType.UInt64
) ? (p.Value == null || p.Value is System.DBNull) ? "null" : string.Format("{0}", p.Value) : (p.Value == null || p.Value is System.DBNull) ? "null" : string.Format("'{0}'", p.Value)));
return sql;
} public static string ToSqlString<T>(this IDeleteable<T> IDeleteable) where T : class, new()
{
var tosql = IDeleteable.ToSql();
Regex reg = new Regex(@"[^0-9]+");
var sql = tosql.Key;
var pars = tosql.Value;
if (pars == null)
{
return sql;
}
sql = pars.OrderByDescending(o => reg.Replace(o.ParameterName, "").ObjToInt()).ThenByDescending(o => o.ParameterName.Length).Aggregate(sql, (current, p) => current.Replace(p.ParameterName, (p.DbType == System.Data.DbType.Decimal ||
p.DbType == System.Data.DbType.Double ||
p.DbType == System.Data.DbType.Int16 ||
p.DbType == System.Data.DbType.Int32 ||
p.DbType == System.Data.DbType.Int64 ||
p.DbType == System.Data.DbType.Single ||
p.DbType == System.Data.DbType.VarNumeric ||
p.DbType == System.Data.DbType.UInt16 ||
p.DbType == System.Data.DbType.UInt32 ||
p.DbType == System.Data.DbType.UInt64
) ? (p.Value == null || p.Value is System.DBNull) ? "null" : string.Format("{0}", p.Value) : (p.Value == null || p.Value is System.DBNull) ? "null" : string.Format("'{0}'", p.Value)));
return sql;
} public static string ToSqlString<T>(this IInsertable<T> IInsertable) where T : class, new()
{
var tosql = IInsertable.ToSql();
Regex reg = new Regex(@"[^0-9]+");
var sql = tosql.Key;
var pars = tosql.Value;
if (pars == null)
{
return sql;
}
sql = pars.OrderByDescending(o => reg.Replace(o.ParameterName, "").ObjToInt()).ThenByDescending(o => o.ParameterName.Length).Aggregate(sql, (current, p) => current.Replace(p.ParameterName, (p.DbType == System.Data.DbType.Decimal ||
p.DbType == System.Data.DbType.Double ||
p.DbType == System.Data.DbType.Int16 ||
p.DbType == System.Data.DbType.Int32 ||
p.DbType == System.Data.DbType.Int64 ||
p.DbType == System.Data.DbType.Single ||
p.DbType == System.Data.DbType.VarNumeric ||
p.DbType == System.Data.DbType.UInt16 ||
p.DbType == System.Data.DbType.UInt32 ||
p.DbType == System.Data.DbType.UInt64
) ? (p.Value == null || p.Value is System.DBNull) ? "null" : string.Format("{0}", p.Value) : (p.Value == null || p.Value is System.DBNull) ? "null" : string.Format("'{0}'", p.Value)));
return sql;
}
5、Demo地址:https://gitee.com/xuanyun2018/sqlsugardemo.git
sqlsugar入门(1)-初识sugar正确打开sugar的方式的更多相关文章
- 跨平台C++:(前言)正确打开C++的方式
接触C++已经十五年了...但是对于C++而言,我至今是个门外汉,不是谦虚,而是确实不得其门而入. 历程是这样的—— 大学考研要考C++,就自学了.研没考上,C++算是学了,准确的说是C++的语法,以 ...
- iOS开发小技巧--相机相册的正确打开方式
iOS相机相册的正确打开方式- UIImagePickerController 通过指定sourceType来实现打开相册还是相机 UIImagePickerControllerSourceTypeP ...
- Xcode 的正确打开方式——Debugging(转载)
Xcode 的正确打开方式——Debugging 程序员日常开发中有大量时间都会花费在 debug 上,从事 iOS 开发不可避免地需要使用 Xcode.这篇博客就主要介绍了 Xcode 中几种能 ...
- C#语法——泛型的多种应用 C#语法——await与async的正确打开方式 C#线程安全使用(五) C#语法——元组类型 好好耕耘 redis和memcached的区别
C#语法——泛型的多种应用 本篇文章主要介绍泛型的应用. 泛型是.NET Framework 2.0 版类库就已经提供的语法,主要用于提高代码的可重用性.类型安全性和效率. 泛型的定义 下面定义了 ...
- InnoDB缓冲池预加载在MySQL 5.7中的正确打开方式
InnoDB缓冲池预加载在MySQL 5.7中的正确打开方式 https://mp.weixin.qq.com/s/HGa_90XvC22anabiBF8AbQ 在这篇文章里,我将讨论在MySQL 5 ...
- Console控制台的正确打开方式
Console控制台的正确打开方式 console对象提供了访问浏览器调试模式的信息到控制台 -- Console对象 |-- assert() 如果第一个参数断言为false,则在控制台输出错误信息 ...
- 任务队列和异步接口的正确打开方式(.NET Core版本)
任务队列和异步接口的正确打开方式 什么是异步接口? Asynchronous Operations Certain types of operations might require processi ...
- mysql core文件的正确打开姿势
最近两天自己负责的一个实例频繁出现crash的情况,分析了日志,大致明白了crash的原因,但是没有定位到具体的SQL,也没有找到很好的规避的办法,因此想在mysql出现crash的时候自动 ...
- 【Android开发笔记】返回上层Activity的正确打开方式
技术支持 http://stackoverflow.com/questions/12276027/how-can-i-return-to-a-parent-activity-correctly 首先, ...
随机推荐
- spring aop 源码分析(二) 代理方法的执行过程分析
在上一篇aop源码分析时,我们已经分析了一个bean被代理的详细过程,参考:https://www.cnblogs.com/yangxiaohui227/p/13266014.html 本次主要是分析 ...
- MySQL中事务和事务的隔离级别
本文主要是帮助理解相关知识,没有具体的操作和代码. 事务 事务就是一组操作,这组操作要么全部成功,要么全部失败. 最经典的例子就是银行转账: 张三给李四转账100,对用户来说,就是一个操作.但对应到数 ...
- 返回头添加cookie信息
返回类型 HttpResponseMessage //构建返回对象 var res= Request.CreateResponse(HttpStstusCode.Ok,返回体) //创建cookie对 ...
- Fabric1.4.4 多机solo共识搭建
简单记录一下fabric版本1.4.4的搭建部署,运行环境为CentOs7.8,如有错误欢迎批评指正.然后就是本编文章不对环境搭建做说明,基础环境搭建看这里. 1.总体设计 本案例部署一个排序(ord ...
- 2017-01-26--编译busybox总结
错误一: ox@ubuntu:busybox-1.16.0$ make menuconfig Makefile:431: *** mixed implicit and normal rules: de ...
- linux 虚拟机下 安装redis
虚拟机安装linux,打开,挂起就好: 使用ssh连接,这里使用的是Moba Xterm 可以ssh 可以ftp 满足你的日常开发所需,开发必备.每个人都有自己顺手的工具,你喜欢就好 虚拟机挂一边就 ...
- 【全网免费VIP观看】哔哩哔哩番剧解锁大会员-集合了优酷-爱奇艺-腾讯-芒果-乐视-ab站等全网vip视频免费破解去广告-高清普清电视观看-持续更新
哔哩哔哩番剧解锁大会员-集合了优酷-爱奇艺-腾讯-芒果-乐视-ab站等全网vip视频免费破解去广告-高清普清电视观看-持续更新 前言 突然想看电视,结果 没有VIP 又不想花钱,这免费的不久来啦. 示 ...
- ngx_align 值对齐宏
ngx_align 值对齐宏 ngx_align 为nginx中的一个值对齐宏.主要在需要内存申请的地方使用,为了减少在不同的 cache line 中内存而生. // d 为需要对齐的 // a 为 ...
- php 注册器模式 工厂模式
<?php /** * 注册器模式 * 全局共享和交换对象 */ class Register { public static $objects; // 定义全局数组 // 保存对象到全局数组 ...
- 第十章 nginx常用配置介绍
一.虚拟主机 1.配置方式 #虚拟主机配置方式:1.基于多IP的方式2.基于多端口的方式3.基于多域名的方式 2.方式一:基于多IP的方式 1.第一个配置文件[root@web02 /etc/ngin ...