From ba715af0ce9a2f1eb51e9e610936bd6dfb6ebd2c Mon Sep 17 00:00:00 2001
From: wjf <1448376744@qq.com>
Date: Sat, 9 May 2020 17:26:16 +0800
Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E5=85=A5=E5=BF=BD=E7=95=A5=E9=A1=B9?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/AutoEntitys.tt | 116 ++++
.../Attributes/ColumnAttribute.cs | 21 +
.../Attributes/ComplexTypeAttribute.cs | 14 +
.../Attributes/ConcurrencyCheckAttribute.cs | 14 +
.../Attributes/DefaultAttribute.cs | 13 +
.../Attributes/FunctionAttribute.cs | 13 +
.../Attributes/IdentityAttribute.cs | 13 +
.../Attributes/NotMappedAttribute.cs | 13 +
.../Attributes/PrimaryKeyAttribute.cs | 13 +
.../Attributes/TableAttribute.cs | 17 +
src/Dapper.Common/Dapper.Common.csproj | 32 +
src/Dapper.Common/DbContexts/DbContext.cs | 493 +++++++++++++++
.../DbContexts/DbContextBuilder.cs | 12 +
.../DbContexts/DbContextState.cs | 14 +
src/Dapper.Common/DbContexts/DbContextType.cs | 15 +
src/Dapper.Common/DbContexts/EmitConvert.cs | 292 +++++++++
src/Dapper.Common/DbContexts/EntityMapper.cs | 585 ++++++++++++++++++
src/Dapper.Common/DbContexts/MultiResult.cs | 147 +++++
.../DbUpdateConcurrencyException.cs | 18 +
.../Expressions/BooleanExpressionResovle.cs | 228 +++++++
.../Expressions/ExpressionActivator.cs | 237 +++++++
.../Expressions/ExpressionResovle.cs | 82 +++
.../Expressions/FunctionExpressionResovle.cs | 77 +++
.../Expressions/GroupExpressionResovle.cs | 40 ++
.../Expressions/OrderExpressionResovle.cs | 45 ++
.../Expressions/SelectExpressionResovle.cs | 83 +++
.../Queryables/DbMetaInfoCache.cs | 130 ++++
src/Dapper.Common/Queryables/DbQuery.cs | 433 +++++++++++++
src/Dapper.Common/Queryables/DbQueryAsync.cs | 166 +++++
src/Dapper.Common/Queryables/DbQuerySync.cs | 275 ++++++++
src/Dapper.Common/Queryables/IDbQuery.cs | 354 +++++++++++
src/Dapper.Common/Queryables/Operator.cs | 201 ++++++
.../XmlResovles/Nodes/CommandNode.cs | 100 +++
src/Dapper.Common/XmlResovles/Nodes/INode.cs | 11 +
src/Dapper.Common/XmlResovles/Nodes/IfNode.cs | 13 +
.../XmlResovles/Nodes/TextNode.cs | 13 +
.../XmlResovles/Nodes/WhereNode.cs | 11 +
src/Dapper.Common/XmlResovles/XmlQuery.cs | 168 +++++
src/Dapper.Common/XmlResovles/XmlResovle.cs | 240 +++++++
src/Dapper.Linq.NUnitTest/AutoEntitys.cs | 42 ++
src/Dapper.Linq.NUnitTest/AutoEntitys.tt | 116 ++++
.../Dapper.Linq.NUnitTest.csproj | 39 ++
src/Dapper.Linq.NUnitTest/UnitTest1.cs | 55 ++
src/Dapper.Linq.NUnitTest/student.xml | 35 ++
src/Dapper.Linq.sln | 37 ++
src/Dapper.Linq/Attributes/ColumnAttribute.cs | 21 +
.../Attributes/ComplexTypeAttribute.cs | 14 +
.../Attributes/ConcurrencyCheckAttribute.cs | 14 +
.../Attributes/DefaultAttribute.cs | 13 +
.../Attributes/FunctionAttribute.cs | 13 +
.../Attributes/IdentityAttribute.cs | 13 +
.../Attributes/NotMappedAttribute.cs | 13 +
.../Attributes/PrimaryKeyAttribute.cs | 13 +
src/Dapper.Linq/Attributes/TableAttribute.cs | 17 +
src/Dapper.Linq/Dapper.Linq.csproj | 33 +
src/Dapper.Linq/DbContexts/DbContext.cs | 493 +++++++++++++++
.../DbContexts/DbContextBuilder.cs | 12 +
src/Dapper.Linq/DbContexts/DbContextState.cs | 14 +
src/Dapper.Linq/DbContexts/DbContextType.cs | 15 +
src/Dapper.Linq/DbContexts/EmitConvert.cs | 292 +++++++++
src/Dapper.Linq/DbContexts/EntityMapper.cs | 585 ++++++++++++++++++
src/Dapper.Linq/DbContexts/MultiResult.cs | 147 +++++
.../DbUpdateConcurrencyException.cs | 18 +
.../Expressions/BooleanExpressionResovle.cs | 228 +++++++
.../Expressions/ExpressionActivator.cs | 237 +++++++
.../Expressions/ExpressionResovle.cs | 82 +++
.../Expressions/FunctionExpressionResovle.cs | 77 +++
.../Expressions/GroupExpressionResovle.cs | 40 ++
.../Expressions/OrderExpressionResovle.cs | 45 ++
.../Expressions/SelectExpressionResovle.cs | 83 +++
src/Dapper.Linq/Queryables/DbMetaInfoCache.cs | 130 ++++
src/Dapper.Linq/Queryables/DbQuery.cs | 433 +++++++++++++
src/Dapper.Linq/Queryables/DbQueryAsync.cs | 166 +++++
src/Dapper.Linq/Queryables/DbQuerySync.cs | 275 ++++++++
src/Dapper.Linq/Queryables/IDbQuery.cs | 354 +++++++++++
src/Dapper.Linq/Queryables/Operator.cs | 201 ++++++
.../XmlResovles/Nodes/CommandNode.cs | 100 +++
src/Dapper.Linq/XmlResovles/Nodes/INode.cs | 11 +
src/Dapper.Linq/XmlResovles/Nodes/IfNode.cs | 13 +
src/Dapper.Linq/XmlResovles/Nodes/TextNode.cs | 13 +
.../XmlResovles/Nodes/WhereNode.cs | 11 +
src/Dapper.Linq/XmlResovles/XmlQuery.cs | 168 +++++
src/Dapper.Linq/XmlResovles/XmlResovle.cs | 240 +++++++
src/References/Dapper.dll | Bin 0 -> 120320 bytes
src/References/MySql.Data.dll | Bin 0 -> 431616 bytes
85 files changed, 9733 insertions(+)
create mode 100644 src/AutoEntitys.tt
create mode 100644 src/Dapper.Common/Attributes/ColumnAttribute.cs
create mode 100644 src/Dapper.Common/Attributes/ComplexTypeAttribute.cs
create mode 100644 src/Dapper.Common/Attributes/ConcurrencyCheckAttribute.cs
create mode 100644 src/Dapper.Common/Attributes/DefaultAttribute.cs
create mode 100644 src/Dapper.Common/Attributes/FunctionAttribute.cs
create mode 100644 src/Dapper.Common/Attributes/IdentityAttribute.cs
create mode 100644 src/Dapper.Common/Attributes/NotMappedAttribute.cs
create mode 100644 src/Dapper.Common/Attributes/PrimaryKeyAttribute.cs
create mode 100644 src/Dapper.Common/Attributes/TableAttribute.cs
create mode 100644 src/Dapper.Common/Dapper.Common.csproj
create mode 100644 src/Dapper.Common/DbContexts/DbContext.cs
create mode 100644 src/Dapper.Common/DbContexts/DbContextBuilder.cs
create mode 100644 src/Dapper.Common/DbContexts/DbContextState.cs
create mode 100644 src/Dapper.Common/DbContexts/DbContextType.cs
create mode 100644 src/Dapper.Common/DbContexts/EmitConvert.cs
create mode 100644 src/Dapper.Common/DbContexts/EntityMapper.cs
create mode 100644 src/Dapper.Common/DbContexts/MultiResult.cs
create mode 100644 src/Dapper.Common/Exceptions/DbUpdateConcurrencyException.cs
create mode 100644 src/Dapper.Common/Expressions/BooleanExpressionResovle.cs
create mode 100644 src/Dapper.Common/Expressions/ExpressionActivator.cs
create mode 100644 src/Dapper.Common/Expressions/ExpressionResovle.cs
create mode 100644 src/Dapper.Common/Expressions/FunctionExpressionResovle.cs
create mode 100644 src/Dapper.Common/Expressions/GroupExpressionResovle.cs
create mode 100644 src/Dapper.Common/Expressions/OrderExpressionResovle.cs
create mode 100644 src/Dapper.Common/Expressions/SelectExpressionResovle.cs
create mode 100644 src/Dapper.Common/Queryables/DbMetaInfoCache.cs
create mode 100644 src/Dapper.Common/Queryables/DbQuery.cs
create mode 100644 src/Dapper.Common/Queryables/DbQueryAsync.cs
create mode 100644 src/Dapper.Common/Queryables/DbQuerySync.cs
create mode 100644 src/Dapper.Common/Queryables/IDbQuery.cs
create mode 100644 src/Dapper.Common/Queryables/Operator.cs
create mode 100644 src/Dapper.Common/XmlResovles/Nodes/CommandNode.cs
create mode 100644 src/Dapper.Common/XmlResovles/Nodes/INode.cs
create mode 100644 src/Dapper.Common/XmlResovles/Nodes/IfNode.cs
create mode 100644 src/Dapper.Common/XmlResovles/Nodes/TextNode.cs
create mode 100644 src/Dapper.Common/XmlResovles/Nodes/WhereNode.cs
create mode 100644 src/Dapper.Common/XmlResovles/XmlQuery.cs
create mode 100644 src/Dapper.Common/XmlResovles/XmlResovle.cs
create mode 100644 src/Dapper.Linq.NUnitTest/AutoEntitys.cs
create mode 100644 src/Dapper.Linq.NUnitTest/AutoEntitys.tt
create mode 100644 src/Dapper.Linq.NUnitTest/Dapper.Linq.NUnitTest.csproj
create mode 100644 src/Dapper.Linq.NUnitTest/UnitTest1.cs
create mode 100644 src/Dapper.Linq.NUnitTest/student.xml
create mode 100644 src/Dapper.Linq.sln
create mode 100644 src/Dapper.Linq/Attributes/ColumnAttribute.cs
create mode 100644 src/Dapper.Linq/Attributes/ComplexTypeAttribute.cs
create mode 100644 src/Dapper.Linq/Attributes/ConcurrencyCheckAttribute.cs
create mode 100644 src/Dapper.Linq/Attributes/DefaultAttribute.cs
create mode 100644 src/Dapper.Linq/Attributes/FunctionAttribute.cs
create mode 100644 src/Dapper.Linq/Attributes/IdentityAttribute.cs
create mode 100644 src/Dapper.Linq/Attributes/NotMappedAttribute.cs
create mode 100644 src/Dapper.Linq/Attributes/PrimaryKeyAttribute.cs
create mode 100644 src/Dapper.Linq/Attributes/TableAttribute.cs
create mode 100644 src/Dapper.Linq/Dapper.Linq.csproj
create mode 100644 src/Dapper.Linq/DbContexts/DbContext.cs
create mode 100644 src/Dapper.Linq/DbContexts/DbContextBuilder.cs
create mode 100644 src/Dapper.Linq/DbContexts/DbContextState.cs
create mode 100644 src/Dapper.Linq/DbContexts/DbContextType.cs
create mode 100644 src/Dapper.Linq/DbContexts/EmitConvert.cs
create mode 100644 src/Dapper.Linq/DbContexts/EntityMapper.cs
create mode 100644 src/Dapper.Linq/DbContexts/MultiResult.cs
create mode 100644 src/Dapper.Linq/Exceptions/DbUpdateConcurrencyException.cs
create mode 100644 src/Dapper.Linq/Expressions/BooleanExpressionResovle.cs
create mode 100644 src/Dapper.Linq/Expressions/ExpressionActivator.cs
create mode 100644 src/Dapper.Linq/Expressions/ExpressionResovle.cs
create mode 100644 src/Dapper.Linq/Expressions/FunctionExpressionResovle.cs
create mode 100644 src/Dapper.Linq/Expressions/GroupExpressionResovle.cs
create mode 100644 src/Dapper.Linq/Expressions/OrderExpressionResovle.cs
create mode 100644 src/Dapper.Linq/Expressions/SelectExpressionResovle.cs
create mode 100644 src/Dapper.Linq/Queryables/DbMetaInfoCache.cs
create mode 100644 src/Dapper.Linq/Queryables/DbQuery.cs
create mode 100644 src/Dapper.Linq/Queryables/DbQueryAsync.cs
create mode 100644 src/Dapper.Linq/Queryables/DbQuerySync.cs
create mode 100644 src/Dapper.Linq/Queryables/IDbQuery.cs
create mode 100644 src/Dapper.Linq/Queryables/Operator.cs
create mode 100644 src/Dapper.Linq/XmlResovles/Nodes/CommandNode.cs
create mode 100644 src/Dapper.Linq/XmlResovles/Nodes/INode.cs
create mode 100644 src/Dapper.Linq/XmlResovles/Nodes/IfNode.cs
create mode 100644 src/Dapper.Linq/XmlResovles/Nodes/TextNode.cs
create mode 100644 src/Dapper.Linq/XmlResovles/Nodes/WhereNode.cs
create mode 100644 src/Dapper.Linq/XmlResovles/XmlQuery.cs
create mode 100644 src/Dapper.Linq/XmlResovles/XmlResovle.cs
create mode 100644 src/References/Dapper.dll
create mode 100644 src/References/MySql.Data.dll
diff --git a/src/AutoEntitys.tt b/src/AutoEntitys.tt
new file mode 100644
index 0000000..3459411
--- /dev/null
+++ b/src/AutoEntitys.tt
@@ -0,0 +1,116 @@
+<#@ template debug="false" hostspecific="false" language="C#" #>
+<#@ assembly name="System.Core" #>
+<#@ assembly name="System.Data" #>
+<#@ assembly name="$(SolutionDir)\YC51.References\Dapper.dll" #>
+<#@ assembly name="$(SolutionDir)\YC51.References\MySql.Data.dll" #>
+<#@ import namespace="System.Linq" #>
+<#@ import namespace="System.Text" #>
+<#@ import namespace="System.Collections.Generic" #>
+<#@ import namespace="System.Data" #>
+<#@ import namespace="MySql.Data.MySqlClient" #>
+<#@ import namespace="Dapper" #>
+<#@ output extension=".cs" #>
+<#
+ //数据库连接字符串
+ var connectionString = "server=127.0.0.1;user id=root;password=1024;database=test;";
+#>
+<#
+ var connection = new MySqlConnection(connectionString);
+ var tablesql = string.Format("SELECT TABLE_NAME as TableName,TABLE_TYPE as TableType,TABLE_COMMENT as TableComment from INFORmation_schema.TABLES WHERE TABLE_SCHEMA='{0}'", connection.Database);
+ var columnsql = string.Format("SELECT TABLE_NAME as TableName,COLUMN_NAME as ColumnName,COLUMN_COMMENT as ColumnComment,COLUMN_DEFAULT as ColumnDefault,IS_NULLABLE as IsNullable,DATA_TYPE as DataType,COLUMN_TYPE as ColumnType,COLUMN_KEY as ColumnKey,EXTRA as Extra FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA='{0}' ORDER BY ORDINAL_POSITION", connection.Database);
+ var tables = connection.Query
(tablesql).ToList();
+ var columns = connection.Query(columnsql).ToList();
+ connection.Close();
+#>
+using System;
+using YC51.SqlBatis.Attributes;
+
+namespace YC51.MiniProgram.Service
+{
+<#foreach(var table in tables){#>
+
+ ///
+ /// <#=table.TableComment#>
+ /// 更新时间:<#=DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")#>
+ ///
+ [Table("<#=table.TableName#>")]
+ public partial class <#=Utils.Pascal(table.TableName)#>
+ {
+ <#foreach(var column in columns.FindAll(f => f.TableName == table.TableName)){#>
+ ///
+ /// <#=column.ColumnComment#>
+ ///
+ [Column("<#= column.ColumnName #>")]
+ <#=column.ColumnKey == "PRI"?"[PrimaryKey]\r\n":""#><#=column.Extra == "auto_increment"?"[Identity]\r\n":"" #><#=column.ColumnDefault != null?"[Default]\r\n":"" #>
+ public <#= Utils.GetCSharpType(column.DataType) #> <#= Utils.Pascal(column.ColumnName) #> { get; set; }
+ <#}#>}
+<#}#>
+}
+
+
+
+<#+
+ public class Table
+ {
+ public string TableName { get; set; }
+ public string TableType { get; set; }
+ public string TableComment { get; set; }
+ }
+ public class Column
+ {
+ public string TableName { get; set; }
+ public string ColumnName { get; set; }
+ public string ColumnComment { get; set; }
+ public string ColumnDefault { get; set; }
+ public string IsNullable { get; set; }
+ public string ColumnType { get; set; }
+ public string DataType { get; set; }
+ public string ColumnKey { get; set; }
+ public string Extra { get; set; }
+ }
+ public static class Utils
+ {
+ //字段类型映射
+ public static string GetCSharpType(string columnType)
+ {
+ var type = "object";
+ switch (columnType)
+ {
+ case "varchar": type = "string"; break;
+ case "text": type = "string"; break;
+ case "char": type = "string"; break;
+ case "bit": type = "bool?"; break;
+ case "tinyint": type = "int?"; break;
+ case "smallint": type = "int?"; break;
+ case "int": type = "int?"; break;
+ case "integer": type = "int?"; break;
+ case "bigint": type = "int?"; break;
+ case "mediumint": type = "int?"; break;
+ case "real": type = "float?"; break;
+ case "float": type = "float?"; break;
+ case "double": type = "double?"; break;
+ case "decimal": type = "decimal?"; break;
+ case "date": type = "DateTime?"; break;
+ case "datetime": type = "DateTime?"; break;
+ case "json": type = "System.Text.Json.JsonElement?"; break;
+ }
+ return type;
+ }
+ //Pacsl命名转换
+ public static string Pascal(string name)
+ {
+ var list = new List();
+ foreach (var item in name.Split('_'))
+ {
+ list.Add(System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(item.ToLower()));
+ }
+ return string.Join("",list);
+ }
+ //Camel命名转换
+ public static string Camel(string name)
+ {
+ name = Pascal(name);
+ return char.ToLower(name[0]) + name.Substring(1);
+ }
+ }
+#>
\ No newline at end of file
diff --git a/src/Dapper.Common/Attributes/ColumnAttribute.cs b/src/Dapper.Common/Attributes/ColumnAttribute.cs
new file mode 100644
index 0000000..4c631b2
--- /dev/null
+++ b/src/Dapper.Common/Attributes/ColumnAttribute.cs
@@ -0,0 +1,21 @@
+using System;
+
+namespace Dapper.Attributes
+{
+ ///
+ /// 字段映射
+ ///
+ [AttributeUsage(AttributeTargets.Property)]
+ public class ColumnAttribute : Attribute
+ {
+ internal string Name { get; set; }
+ ///
+ /// 属性字段映射
+ ///
+ /// 数据库字段名
+ public ColumnAttribute(string name = null)
+ {
+ Name = name;
+ }
+ }
+}
diff --git a/src/Dapper.Common/Attributes/ComplexTypeAttribute.cs b/src/Dapper.Common/Attributes/ComplexTypeAttribute.cs
new file mode 100644
index 0000000..557443c
--- /dev/null
+++ b/src/Dapper.Common/Attributes/ComplexTypeAttribute.cs
@@ -0,0 +1,14 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace Dapper.Attributes
+{
+ ///
+ /// 计算列,如果字段一个是计算列则新增和修改的时候不会处理
+ ///
+ public class ComplexTypeAttribute : Attribute
+ {
+
+ }
+}
diff --git a/src/Dapper.Common/Attributes/ConcurrencyCheckAttribute.cs b/src/Dapper.Common/Attributes/ConcurrencyCheckAttribute.cs
new file mode 100644
index 0000000..7eab1a9
--- /dev/null
+++ b/src/Dapper.Common/Attributes/ConcurrencyCheckAttribute.cs
@@ -0,0 +1,14 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace Dapper.Attributes
+{
+ ///
+ /// 并发检查,如果字段属性是number类型则用时间戳,否则使用GUID
+ ///
+ public class ConcurrencyCheckAttribute : Attribute
+ {
+
+ }
+}
diff --git a/src/Dapper.Common/Attributes/DefaultAttribute.cs b/src/Dapper.Common/Attributes/DefaultAttribute.cs
new file mode 100644
index 0000000..d0f7e7c
--- /dev/null
+++ b/src/Dapper.Common/Attributes/DefaultAttribute.cs
@@ -0,0 +1,13 @@
+using System;
+
+namespace Dapper.Attributes
+{
+ ///
+ /// 默认值约束
+ ///
+ [AttributeUsage(AttributeTargets.Property)]
+ public class DefaultAttribute : Attribute
+ {
+
+ }
+}
diff --git a/src/Dapper.Common/Attributes/FunctionAttribute.cs b/src/Dapper.Common/Attributes/FunctionAttribute.cs
new file mode 100644
index 0000000..ef6ffb5
--- /dev/null
+++ b/src/Dapper.Common/Attributes/FunctionAttribute.cs
@@ -0,0 +1,13 @@
+using System;
+
+namespace Dapper.Attributes
+{
+ ///
+ /// 数据库函数标识
+ ///
+ [AttributeUsage(AttributeTargets.Class)]
+ public class FunctionAttribute : Attribute
+ {
+
+ }
+}
diff --git a/src/Dapper.Common/Attributes/IdentityAttribute.cs b/src/Dapper.Common/Attributes/IdentityAttribute.cs
new file mode 100644
index 0000000..2605c87
--- /dev/null
+++ b/src/Dapper.Common/Attributes/IdentityAttribute.cs
@@ -0,0 +1,13 @@
+using System;
+
+namespace Dapper.Attributes
+{
+ ///
+ /// 自增列标识
+ ///
+ [AttributeUsage(AttributeTargets.Property)]
+ public class IdentityAttribute : Attribute
+ {
+
+ }
+}
diff --git a/src/Dapper.Common/Attributes/NotMappedAttribute.cs b/src/Dapper.Common/Attributes/NotMappedAttribute.cs
new file mode 100644
index 0000000..c0a85b4
--- /dev/null
+++ b/src/Dapper.Common/Attributes/NotMappedAttribute.cs
@@ -0,0 +1,13 @@
+using System;
+
+namespace Dapper.Attributes
+{
+ ///
+ /// 忽略映射
+ ///
+ [AttributeUsage(AttributeTargets.Property)]
+ public class NotMappedAttribute: Attribute
+ {
+
+ }
+}
diff --git a/src/Dapper.Common/Attributes/PrimaryKeyAttribute.cs b/src/Dapper.Common/Attributes/PrimaryKeyAttribute.cs
new file mode 100644
index 0000000..f9ed31c
--- /dev/null
+++ b/src/Dapper.Common/Attributes/PrimaryKeyAttribute.cs
@@ -0,0 +1,13 @@
+using System;
+
+namespace Dapper.Attributes
+{
+ ///
+ /// 主键约束
+ ///
+ [AttributeUsage(AttributeTargets.Property)]
+ public class PrimaryKeyAttribute : Attribute
+ {
+
+ }
+}
diff --git a/src/Dapper.Common/Attributes/TableAttribute.cs b/src/Dapper.Common/Attributes/TableAttribute.cs
new file mode 100644
index 0000000..8631d9b
--- /dev/null
+++ b/src/Dapper.Common/Attributes/TableAttribute.cs
@@ -0,0 +1,17 @@
+using System;
+
+namespace Dapper.Attributes
+{
+ ///
+ /// 表名映射
+ ///
+ [AttributeUsage(AttributeTargets.Class)]
+ public class TableAttribute : Attribute
+ {
+ internal string Name { get; set; }
+ public TableAttribute(string name = null)
+ {
+ Name = name;
+ }
+ }
+}
diff --git a/src/Dapper.Common/Dapper.Common.csproj b/src/Dapper.Common/Dapper.Common.csproj
new file mode 100644
index 0000000..f705e93
--- /dev/null
+++ b/src/Dapper.Common/Dapper.Common.csproj
@@ -0,0 +1,32 @@
+
+
+
+ net45;netstandard2.0
+ $(NoWarn);1591
+ chaeyeon
+ Apache-2.0
+ A high performance Micro-ORM supporting SQL Server, MySQL, Sqlite, etc..
+ https://github.com/1448376744/Dapper.Linq
+ https://github.com/1448376744/Dapper.Linq
+ GIT
+ true
+ true
+ chaeyeon
+ chaeyeon
+ Dapper.Common
+ 3.0.0
+ 3.0.0
+ 3.0.0
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/src/Dapper.Common/DbContexts/DbContext.cs b/src/Dapper.Common/DbContexts/DbContext.cs
new file mode 100644
index 0000000..f043db3
--- /dev/null
+++ b/src/Dapper.Common/DbContexts/DbContext.cs
@@ -0,0 +1,493 @@
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using System.Data;
+using System.Data.Common;
+using System.Linq;
+using System.Text.RegularExpressions;
+using System.Threading.Tasks;
+
+namespace Dapper
+{
+ ///
+ /// 数据库上下文
+ ///
+ public interface IDbContext : IDisposable
+ {
+ ///
+ /// 数据库连接
+ ///
+ IDbConnection Connection { get; }
+ ///
+ /// 数据库上下文类型
+ ///
+ DbContextType DbContextType { get; }
+ ///
+ /// 获取一个xml执行器
+ ///
+ /// 参数类型
+ /// 命令id
+ /// 参数
+ ///
+ IXmlQuery From(string id, T parameter) where T : class;
+ ///
+ /// 获取一个xml执行器
+ ///
+ /// 命令id
+ ///
+ IXmlQuery From(string id);
+ ///
+ /// 获取一个linq执行器
+ ///
+ ///
+ ///
+ IDbQuery From();
+ ///
+ /// 开启事务会话
+ ///
+ void BeginTransaction();
+ ///
+ /// 开启事务会话
+ ///
+ /// 事务隔离级别
+ void BeginTransaction(IsolationLevel level);
+ ///
+ /// 关闭连接和事务
+ ///
+ void Close();
+ ///
+ /// 提交当前事务会话
+ ///
+ void CommitTransaction();
+ ///
+ /// 执行多结果集查询,返回IMultiResult
+ ///
+ /// sql命令
+ /// 参数
+ /// 超时时间
+ /// 命令类型
+ ///
+ IMultiResult ExecuteMultiQuery(string sql, object parameter = null, int? commandTimeout = null, CommandType? commandType = null);
+ ///
+ /// 执行单结果集查询,并返回dynamic类型的结果集
+ ///
+ /// sql命令
+ /// 参数
+ /// 超时时间
+ /// 命令类型
+ ///
+ IEnumerable ExecuteQuery(string sql, object parameter = null, int? commandTimeout = null, CommandType? commandType = null);
+ ///
+ /// 异步执行单结果集查询,并返回dynamic类型的结果集
+ ///
+ /// sql命令
+ /// 参数
+ /// 超时时间
+ /// 命令类型
+ ///
+ Task> ExecuteQueryAsync(string sql, object parameter = null, int? commandTimeout = null, CommandType? commandType = null);
+ ///
+ /// 执行单结果集查询,并返回T类型的结果集
+ ///
+ /// 返回类型
+ /// sql命令
+ /// 参数
+ /// 超时时间
+ /// 命令类型
+ ///
+ IEnumerable ExecuteQuery(string sql, object parameter = null, int? commandTimeout = null, CommandType? commandType = null);
+ ///
+ /// 异步执行单结果集查询,并返回T类型的结果集
+ ///
+ /// 返回类型
+ /// sql命令
+ /// 参数
+ /// 超时时间
+ /// 命令类型
+ ///
+ Task> ExecuteQueryAsync(string sql, object parameter = null, int? commandTimeout = null, CommandType? commandType = null);
+ ///
+ /// 执行无结果集查询,并返回受影响的行数
+ ///
+ /// sql命令
+ /// 参数
+ /// 超时时间
+ /// 命令类型
+ ///
+ int ExecuteNonQuery(string sql, object parameter = null, int? commandTimeout = null, CommandType? commandType = null);
+ ///
+ /// 异步执行无结果集查询,并返回受影响的行数
+ ///
+ /// sql命令
+ /// 参数
+ /// 超时时间
+ /// 命令类型
+ ///
+ Task ExecuteNonQueryAsync(string sql, object parameter = null, int? commandTimeout = null, CommandType? commandType = null);
+ ///
+ /// 执行无结果集查询,并返回指定类型的数据
+ ///
+ /// 返回类型
+ /// sql命令
+ /// 参数
+ /// 超时时间
+ /// 命令类型
+ ///
+ T ExecuteScalar(string sql, object parameter = null, int? commandTimeout = null, CommandType? commandType = null);
+ ///
+ /// 异步执行无结果集查询,并返回指定类型的数据
+ ///
+ /// 返回类型
+ /// sql命令
+ /// 参数
+ /// 超时时间
+ /// 命令类型
+ ///
+ Task ExecuteScalarAsync(string sql, object parameter = null, int? commandTimeout = null, CommandType? commandType = null);
+ ///
+ /// 打开数据库连接
+ ///
+ void Open();
+ ///
+ /// 异步打开数据库连接
+ ///
+ ///
+ Task OpenAsync();
+ ///
+ /// 回滚当前事务会话
+ ///
+ void RollbackTransaction();
+ }
+
+ public class DbContext : IDbContext
+ {
+ public DbContextState DbContextState = DbContextState.Closed;
+
+ private readonly IXmlResovle _xmlResovle = null;
+
+ private IDbTransaction _transaction = null;
+
+ private readonly IEntityMapper _typeMapper = null;
+
+ public IDbConnection Connection { get; } = null;
+
+ public DbContextType DbContextType { get; } = DbContextType.Mysql;
+
+ protected virtual void OnLogging(string message, IDataParameterCollection parameter = null, int? commandTimeout = null, CommandType? commandType = null)
+ {
+
+ }
+
+ protected virtual DbContextBuilder OnConfiguring(DbContextBuilder builder)
+ {
+ return builder;
+ }
+
+ protected DbContext()
+ {
+ var builder = OnConfiguring(new DbContextBuilder());
+ Connection = builder.Connection;
+ _xmlResovle = builder.XmlResovle;
+ _typeMapper = builder.TypeMapper ?? new EntityMapper();
+ DbContextType = builder.DbContextType;
+ }
+
+ public DbContext(DbContextBuilder builder)
+ {
+ Connection = builder.Connection;
+ _xmlResovle = builder.XmlResovle;
+ _typeMapper = builder.TypeMapper ?? new EntityMapper();
+ DbContextType = builder.DbContextType;
+ }
+ public IXmlQuery From(string id, T parameter) where T : class
+ {
+ var sql = _xmlResovle.Resolve(id, parameter);
+ var deserializer = EmitConvert.GetDeserializer(typeof(T));
+ var values = deserializer(parameter);
+ return new XmlQuery(this, sql, values);
+ }
+ public IXmlQuery From(string id)
+ {
+ var sql = _xmlResovle.Resolve(id);
+ return new XmlQuery(this, sql);
+ }
+
+ public IDbQuery From()
+ {
+ return new DbQuery(this);
+ }
+
+ public IEnumerable ExecuteQuery(string sql, object parameter = null, int? commandTimeout = null, CommandType? commandType = null)
+ {
+ using (var cmd = Connection.CreateCommand())
+ {
+ Initialize(cmd, sql, parameter, commandTimeout, commandType);
+ var list = new List();
+ using (var reader = cmd.ExecuteReader())
+ {
+ var handler = EmitConvert.GetSerializer();
+ while (reader.Read())
+ {
+ list.Add(handler(reader));
+ }
+ return list;
+ }
+ }
+ }
+
+ public async Task> ExecuteQueryAsync(string sql, object parameter = null, int? commandTimeout = null, CommandType? commandType = null)
+ {
+ using (var cmd = (Connection as DbConnection).CreateCommand())
+ {
+ Initialize(cmd, sql, parameter, commandTimeout, commandType);
+ using (var reader = await cmd.ExecuteReaderAsync())
+ {
+ var list = new List();
+ var handler = EmitConvert.GetSerializer();
+ while (reader.Read())
+ {
+ list.Add(handler(reader));
+ }
+ return list;
+ }
+ }
+ }
+
+ public IMultiResult ExecuteMultiQuery(string sql, object parameter = null, int? commandTimeout = null, CommandType? commandType = null)
+ {
+ var cmd = Connection.CreateCommand();
+ Initialize(cmd, sql, parameter, commandTimeout, commandType);
+ return new MultiResult(cmd, _typeMapper);
+ }
+
+ public IEnumerable ExecuteQuery(string sql, object parameter = null, int? commandTimeout = null, CommandType? commandType = null)
+ {
+ using (var cmd = Connection.CreateCommand())
+ {
+ var list = new List();
+ Initialize(cmd, sql, parameter, commandTimeout, commandType);
+ using (var reader = cmd.ExecuteReader())
+ {
+ var handler = EmitConvert.GetSerializer(_typeMapper, reader);
+ while (reader.Read())
+ {
+ list.Add(handler(reader));
+ }
+ return list;
+ }
+ }
+ }
+
+ public async Task> ExecuteQueryAsync(string sql, object parameter = null, int? commandTimeout = null, CommandType? commandType = null)
+ {
+ using (var cmd = (Connection as DbConnection).CreateCommand())
+ {
+ Initialize(cmd, sql, parameter, commandTimeout, commandType);
+ using (var reader = await cmd.ExecuteReaderAsync())
+ {
+ var list = new List();
+ var handler = EmitConvert.GetSerializer(_typeMapper, reader);
+ while (await reader.ReadAsync())
+ {
+ list.Add(handler(reader));
+ }
+ return list;
+ }
+ }
+ }
+
+ public int ExecuteNonQuery(string sql, object parameter = null, int? commandTimeout = null, CommandType? commandType = null)
+ {
+ using (var cmd = Connection.CreateCommand())
+ {
+ Initialize(cmd, sql, parameter, commandTimeout, commandType);
+ return cmd.ExecuteNonQuery();
+ }
+ }
+
+ public async Task ExecuteNonQueryAsync(string sql, object parameter = null, int? commandTimeout = null, CommandType? commandType = null)
+ {
+ using (var cmd = (Connection as DbConnection).CreateCommand())
+ {
+ Initialize(cmd, sql, parameter, commandTimeout, commandType);
+ return await cmd.ExecuteNonQueryAsync();
+ }
+ }
+
+ public T ExecuteScalar(string sql, object parameter = null, int? commandTimeout = null, CommandType? commandType = null)
+ {
+ using (var cmd = Connection.CreateCommand())
+ {
+ Initialize(cmd, sql, parameter, commandTimeout, commandType);
+ var result = cmd.ExecuteScalar();
+ if (result is DBNull || result == null)
+ {
+ return default;
+ }
+ return (T)Convert.ChangeType(result, typeof(T));
+ }
+ }
+
+ public async Task ExecuteScalarAsync(string sql, object parameter = null, int? commandTimeout = null, CommandType? commandType = null)
+ {
+ using (var cmd = (Connection as DbConnection).CreateCommand())
+ {
+ Initialize(cmd, sql, parameter, commandTimeout, commandType);
+ var result = await cmd.ExecuteScalarAsync();
+ if (result is DBNull || result == null)
+ {
+ return default;
+ }
+ return (T)Convert.ChangeType(result, typeof(T));
+ }
+ }
+
+ public void BeginTransaction()
+ {
+ _transaction = Connection.BeginTransaction();
+ OnLogging("Begin transaction");
+ }
+
+ public void BeginTransaction(IsolationLevel level)
+ {
+ _transaction = Connection.BeginTransaction(level);
+ OnLogging("Begin transaction isolationLevel = " + level);
+ }
+
+ public void Close()
+ {
+ _transaction?.Dispose();
+ Connection?.Dispose();
+ DbContextState = DbContextState.Closed;
+ OnLogging("Colsed connection");
+ }
+
+ public void CommitTransaction()
+ {
+ _transaction?.Commit();
+ DbContextState = DbContextState.Commit;
+ OnLogging("Commit transaction");
+ }
+
+ public void Open()
+ {
+ Connection?.Open();
+ DbContextState = DbContextState.Open;
+ OnLogging("Open connection");
+ }
+
+ public async Task OpenAsync()
+ {
+ await (Connection as DbConnection).OpenAsync();
+ DbContextState = DbContextState.Open;
+ OnLogging("Open connection");
+ }
+
+ public void RollbackTransaction()
+ {
+ _transaction?.Rollback();
+ DbContextState = DbContextState.Rollback;
+ OnLogging("rollback");
+
+ }
+
+ private void Initialize(IDbCommand cmd, string sql, object parameter, int? commandTimeout = null, CommandType? commandType = null)
+ {
+ var dbParameters = new List();
+ cmd.Transaction = _transaction;
+ cmd.CommandText = sql;
+ if (commandTimeout.HasValue)
+ {
+ cmd.CommandTimeout = commandTimeout.Value;
+ }
+ if (commandType.HasValue)
+ {
+ cmd.CommandType = commandType.Value;
+ }
+ if (parameter is IDbDataParameter)
+ {
+ dbParameters.Add(parameter as IDbDataParameter);
+ }
+ else if (parameter is IEnumerable parameters)
+ {
+ dbParameters.AddRange(parameters);
+ }
+ else if (parameter is Dictionary keyValues)
+ {
+ foreach (var item in keyValues)
+ {
+ var param = CreateParameter(cmd, item.Key, item.Value);
+ dbParameters.Add(param);
+ }
+ }
+ else if (parameter != null)
+ {
+ var handler = EmitConvert.GetDeserializer(parameter.GetType());
+ var values = handler(parameter);
+ foreach (var item in values)
+ {
+ var param = CreateParameter(cmd, item.Key, item.Value);
+ dbParameters.Add(param);
+ }
+ }
+ if (dbParameters.Count > 0)
+ {
+ foreach (IDataParameter item in dbParameters)
+ {
+ if (item.Value == null)
+ {
+ item.Value = DBNull.Value;
+ }
+ var pattern = $@"in\s+([\@,\:,\?]?{item.ParameterName})";
+ var options = RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Multiline;
+ if (cmd.CommandText.IndexOf("in", StringComparison.OrdinalIgnoreCase) != -1 && Regex.IsMatch(cmd.CommandText, pattern, options))
+ {
+ var name = Regex.Match(cmd.CommandText, pattern, options).Groups[1].Value;
+ var list = new List