mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-06-28 04:35:29 +08:00
Ok
This commit is contained in:
commit
3363b2aa91
BIN
.vs/SqlSugar/v14/.suo
Normal file
BIN
.vs/SqlSugar/v14/.suo
Normal file
Binary file not shown.
6
OrmTest/App.config
Normal file
6
OrmTest/App.config
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<configuration>
|
||||
<startup>
|
||||
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
|
||||
</startup>
|
||||
</configuration>
|
18
OrmTest/Models/School.cs
Normal file
18
OrmTest/Models/School.cs
Normal file
@ -0,0 +1,18 @@
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OrmTest.Models
|
||||
{
|
||||
public class School
|
||||
{
|
||||
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
|
||||
public int Id { get; set; }
|
||||
public string Name { get; set; }
|
||||
public virtual List<Student> Students { get; set; }
|
||||
}
|
||||
}
|
24
OrmTest/Models/Student.cs
Normal file
24
OrmTest/Models/Student.cs
Normal file
@ -0,0 +1,24 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using SqlSugar;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace OrmTest.Models
|
||||
{
|
||||
[SugarTable("STudent")]
|
||||
public class Student
|
||||
{
|
||||
[SugarColumn(IsPrimaryKey = true, IsIdentity = true, ColumnName = "ID")]
|
||||
public int Id { get; set; }
|
||||
public string Name { get; set; }
|
||||
[SugarColumn(IsIgnore = true)]
|
||||
public int SchoolId { get; set; }
|
||||
public DateTime CreateTime { get; set; }
|
||||
|
||||
[SugarColumn(MappingKeys = "id,SchoolId")]
|
||||
public virtual School School { get; set; }
|
||||
}
|
||||
}
|
68
OrmTest/OrmTest.csproj
Normal file
68
OrmTest/OrmTest.csproj
Normal file
@ -0,0 +1,68 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProjectGuid>{CB591654-0476-40AA-8508-784CF32BE239}</ProjectGuid>
|
||||
<OutputType>Exe</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>OrmTest</RootNamespace>
|
||||
<AssemblyName>OrmTest</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
<Reference Include="System.Data.DataSetExtensions" />
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Net.Http" />
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Models\School.cs" />
|
||||
<Compile Include="Models\Student.cs" />
|
||||
<Compile Include="Program.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="App.config" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\SqlSugar\SqlSugar.csproj">
|
||||
<Project>{489bb790-226c-4fad-8d1e-51d72a7ff8e5}</Project>
|
||||
<Name>SqlSugar</Name>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
Other similar extension points exist, see Microsoft.Common.targets.
|
||||
<Target Name="BeforeBuild">
|
||||
</Target>
|
||||
<Target Name="AfterBuild">
|
||||
</Target>
|
||||
-->
|
||||
</Project>
|
6
OrmTest/OrmTest.csproj.user
Normal file
6
OrmTest/OrmTest.csproj.user
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<ProjectView>ShowAllFiles</ProjectView>
|
||||
</PropertyGroup>
|
||||
</Project>
|
59
OrmTest/Program.cs
Normal file
59
OrmTest/Program.cs
Normal file
@ -0,0 +1,59 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Linq.Expressions;
|
||||
using SqlSugar;
|
||||
using OrmTest.Models;
|
||||
|
||||
namespace OrmTest
|
||||
{
|
||||
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Expression<Func<Student, bool>> exp = it => it.Id > 0;
|
||||
ExpressionContext expContext = new ExpressionContext(exp, ResolveExpressType.Single);
|
||||
// var x = expContext.GetFiledName();
|
||||
var xx = expContext.GetWhere();
|
||||
|
||||
|
||||
var b = DateTime.Now;
|
||||
var config = new AttrbuitesCofnig()
|
||||
{
|
||||
DbType = DbType.SqlServer,
|
||||
ConnectionString = "server=.;uid=sa;pwd=sasa;database=SqlSugar4XTest",
|
||||
EntityNamespace = "SqlSugar4XTest.Models",
|
||||
IsAutoCloseConnection = false
|
||||
};
|
||||
SqlSugarClient db = new SqlSugarClient(config);
|
||||
for (int i = 0; i < 1; i++)
|
||||
{
|
||||
db.Database.IsEnableLogEvent = true;
|
||||
db.Database.LogEventStarting = (sql, par) =>
|
||||
{
|
||||
Console.WriteLine(sql + " " + par);
|
||||
};
|
||||
|
||||
|
||||
var tb = db.Database.DbMaintenance.GetTableInfoList();
|
||||
var tc = db.Database.DbMaintenance.GetColumnInfosByTableName("student");
|
||||
|
||||
|
||||
//var list = db.Queryable<Student>()
|
||||
// .JoinTable<School>((st,sc)=>st.SchoolId==sc.Id)
|
||||
// .Where("id=2").Where(it => it.Name == "id").Where(it => it.Id > 0).ToList();
|
||||
|
||||
//var list2 = db.Queryable<Student>().Where(it => it.Name == "id").Where(it => it.Id > 0).ToList();
|
||||
//var xx = db.SqlQuery<Student>("select * from Student");
|
||||
//var cl = db.Database.DbMaintenance.GetColumnInfosByTableName("student");
|
||||
|
||||
}
|
||||
var e = DateTime.Now;
|
||||
var t = (e - b).TotalMilliseconds;
|
||||
}
|
||||
}
|
||||
}
|
36
OrmTest/Properties/AssemblyInfo.cs
Normal file
36
OrmTest/Properties/AssemblyInfo.cs
Normal file
@ -0,0 +1,36 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// General Information about an assembly is controlled through the following
|
||||
// set of attributes. Change these attribute values to modify the information
|
||||
// associated with an assembly.
|
||||
[assembly: AssemblyTitle("OrmTest")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("OrmTest")]
|
||||
[assembly: AssemblyCopyright("Copyright © 2017")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
// Setting ComVisible to false makes the types in this assembly not visible
|
||||
// to COM components. If you need to access a type in this assembly from
|
||||
// COM, set the ComVisible attribute to true on that type.
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
// The following GUID is for the ID of the typelib if this project is exposed to COM
|
||||
[assembly: Guid("cb591654-0476-40aa-8508-784cf32be239")]
|
||||
|
||||
// Version information for an assembly consists of the following four values:
|
||||
//
|
||||
// Major Version
|
||||
// Minor Version
|
||||
// Build Number
|
||||
// Revision
|
||||
//
|
||||
// You can specify all the values or you can default the Build and Revision Numbers
|
||||
// by using the '*' as shown below:
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
BIN
OrmTest/bin/Debug/Newtonsoft.Json.dll
Normal file
BIN
OrmTest/bin/Debug/Newtonsoft.Json.dll
Normal file
Binary file not shown.
BIN
OrmTest/bin/Debug/OrmTest.exe
Normal file
BIN
OrmTest/bin/Debug/OrmTest.exe
Normal file
Binary file not shown.
6
OrmTest/bin/Debug/OrmTest.exe.config
Normal file
6
OrmTest/bin/Debug/OrmTest.exe.config
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<configuration>
|
||||
<startup>
|
||||
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
|
||||
</startup>
|
||||
</configuration>
|
BIN
OrmTest/bin/Debug/OrmTest.pdb
Normal file
BIN
OrmTest/bin/Debug/OrmTest.pdb
Normal file
Binary file not shown.
BIN
OrmTest/bin/Debug/OrmTest.vshost.exe
Normal file
BIN
OrmTest/bin/Debug/OrmTest.vshost.exe
Normal file
Binary file not shown.
6
OrmTest/bin/Debug/OrmTest.vshost.exe.config
Normal file
6
OrmTest/bin/Debug/OrmTest.vshost.exe.config
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<configuration>
|
||||
<startup>
|
||||
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
|
||||
</startup>
|
||||
</configuration>
|
BIN
OrmTest/bin/Debug/SqlSugar.dll
Normal file
BIN
OrmTest/bin/Debug/SqlSugar.dll
Normal file
Binary file not shown.
BIN
OrmTest/bin/Debug/SqlSugar.pdb
Normal file
BIN
OrmTest/bin/Debug/SqlSugar.pdb
Normal file
Binary file not shown.
BIN
OrmTest/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache
Normal file
BIN
OrmTest/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache
Normal file
Binary file not shown.
9
OrmTest/obj/Debug/OrmTest.csproj.FileListAbsolute.txt
Normal file
9
OrmTest/obj/Debug/OrmTest.csproj.FileListAbsolute.txt
Normal file
@ -0,0 +1,9 @@
|
||||
F:\MyOpenSource\SqlSugar4.XNew\SqlSugar\OrmTest\bin\Debug\OrmTest.exe.config
|
||||
F:\MyOpenSource\SqlSugar4.XNew\SqlSugar\OrmTest\bin\Debug\OrmTest.exe
|
||||
F:\MyOpenSource\SqlSugar4.XNew\SqlSugar\OrmTest\bin\Debug\OrmTest.pdb
|
||||
F:\MyOpenSource\SqlSugar4.XNew\SqlSugar\OrmTest\bin\Debug\SqlSugar.dll
|
||||
F:\MyOpenSource\SqlSugar4.XNew\SqlSugar\OrmTest\bin\Debug\Newtonsoft.Json.dll
|
||||
F:\MyOpenSource\SqlSugar4.XNew\SqlSugar\OrmTest\bin\Debug\SqlSugar.pdb
|
||||
F:\MyOpenSource\SqlSugar4.XNew\SqlSugar\OrmTest\obj\Debug\OrmTest.csprojResolveAssemblyReference.cache
|
||||
F:\MyOpenSource\SqlSugar4.XNew\SqlSugar\OrmTest\obj\Debug\OrmTest.exe
|
||||
F:\MyOpenSource\SqlSugar4.XNew\SqlSugar\OrmTest\obj\Debug\OrmTest.pdb
|
BIN
OrmTest/obj/Debug/OrmTest.csprojResolveAssemblyReference.cache
Normal file
BIN
OrmTest/obj/Debug/OrmTest.csprojResolveAssemblyReference.cache
Normal file
Binary file not shown.
BIN
OrmTest/obj/Debug/OrmTest.exe
Normal file
BIN
OrmTest/obj/Debug/OrmTest.exe
Normal file
Binary file not shown.
BIN
OrmTest/obj/Debug/OrmTest.pdb
Normal file
BIN
OrmTest/obj/Debug/OrmTest.pdb
Normal file
Binary file not shown.
28
SqlSugar.sln
Normal file
28
SqlSugar.sln
Normal file
@ -0,0 +1,28 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio 14
|
||||
VisualStudioVersion = 14.0.25420.1
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SqlSugar", "SqlSugar\SqlSugar.csproj", "{489BB790-226C-4FAD-8D1E-51D72A7FF8E5}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OrmTest", "OrmTest\OrmTest.csproj", "{CB591654-0476-40AA-8508-784CF32BE239}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{489BB790-226C-4FAD-8D1E-51D72A7FF8E5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{489BB790-226C-4FAD-8D1E-51D72A7FF8E5}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{489BB790-226C-4FAD-8D1E-51D72A7FF8E5}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{489BB790-226C-4FAD-8D1E-51D72A7FF8E5}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{CB591654-0476-40AA-8508-784CF32BE239}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{CB591654-0476-40AA-8508-784CF32BE239}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{CB591654-0476-40AA-8508-784CF32BE239}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{CB591654-0476-40AA-8508-784CF32BE239}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
EndGlobal
|
@ -0,0 +1,11 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
namespace SqlSugar
|
||||
{
|
||||
public partial class CodeFirstProvider : ICodeFirst
|
||||
{
|
||||
public virtual SqlSugarClient Context { get; set; }
|
||||
}
|
||||
}
|
120
SqlSugar/Abstract/DbProvider/DbAccessory.cs
Normal file
120
SqlSugar/Abstract/DbProvider/DbAccessory.cs
Normal file
@ -0,0 +1,120 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
namespace SqlSugar
|
||||
{
|
||||
public partial class DbAccessory
|
||||
{
|
||||
protected IDbBind _DbBind;
|
||||
protected IDbFirst _DbFirst;
|
||||
protected ICodeFirst _CodeFirst;
|
||||
protected IDbMaintenance _DbMaintenance;
|
||||
protected IDbConnection _DbConnection;
|
||||
public virtual void SetParSize(SqlParameter[] pars)
|
||||
{
|
||||
if (pars != null)
|
||||
{
|
||||
foreach (var par in pars)
|
||||
{
|
||||
this.SetParSize(par);
|
||||
}
|
||||
}
|
||||
}
|
||||
public virtual void SetParSize(SqlParameter par)
|
||||
{
|
||||
int size = par.Size;
|
||||
if (size < 4000)
|
||||
{
|
||||
par.Size = 4000;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void SetSqlDbType(PropertyInfo prop, SqlParameter par)
|
||||
{
|
||||
var isNullable = false;
|
||||
var isByteArray = PubMethod.GetUnderType(prop, ref isNullable) == typeof(byte[]);
|
||||
if (isByteArray)
|
||||
{
|
||||
par.SqlDbType = SqlDbType.VarBinary;
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual SqlParameter[] GetParameters(object obj, PropertyInfo[] propertyInfo,string sqlParameterKeyWord)
|
||||
{
|
||||
List<SqlParameter> listParams = new List<SqlParameter>();
|
||||
if (obj != null)
|
||||
{
|
||||
var type = obj.GetType();
|
||||
var isDic = type.IsIn(PubConst.DicArraySO, PubConst.DicArraySS);
|
||||
if (isDic)
|
||||
{
|
||||
if (type == PubConst.DicArraySO)
|
||||
{
|
||||
var newObj = (Dictionary<string, object>)obj;
|
||||
var pars = newObj.Select(it => new SqlParameter(sqlParameterKeyWord + it.Key, it.Value));
|
||||
foreach (var par in pars)
|
||||
{
|
||||
SetParSize(par);
|
||||
}
|
||||
listParams.AddRange(pars);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
var newObj = (Dictionary<string, string>)obj;
|
||||
var pars = newObj.Select(it => new SqlParameter(sqlParameterKeyWord + it.Key, it.Value));
|
||||
foreach (var par in pars)
|
||||
{
|
||||
SetParSize(par);
|
||||
}
|
||||
listParams.AddRange(pars); ;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
PropertyInfo[] propertiesObj = null;
|
||||
if (propertyInfo != null)
|
||||
{
|
||||
propertiesObj = propertyInfo;
|
||||
}
|
||||
else
|
||||
{
|
||||
propertiesObj = type.GetProperties();
|
||||
}
|
||||
string replaceGuid = Guid.NewGuid().ToString();
|
||||
foreach (PropertyInfo r in propertiesObj)
|
||||
{
|
||||
var value = r.GetValue(obj, null);
|
||||
if (r.PropertyType.IsEnum)
|
||||
{
|
||||
value = Convert.ToInt64(value);
|
||||
}
|
||||
if (value == null || value.Equals(DateTime.MinValue)) value = DBNull.Value;
|
||||
if (r.Name.ToLower().Contains("hierarchyid"))
|
||||
{
|
||||
var par = new SqlParameter(sqlParameterKeyWord + r.Name, SqlDbType.Udt);
|
||||
par.UdtTypeName = "HIERARCHYID";
|
||||
par.Value = value;
|
||||
listParams.Add(par);
|
||||
}
|
||||
else
|
||||
{
|
||||
var par = new SqlParameter(sqlParameterKeyWord + r.Name, value);
|
||||
SetParSize(par);
|
||||
if (value == DBNull.Value)
|
||||
{//防止文件类型报错
|
||||
SetSqlDbType(r, par);
|
||||
}
|
||||
listParams.Add(par);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return listParams.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
144
SqlSugar/Abstract/DbProvider/DbBindProvider/DbBindAccessory.cs
Normal file
144
SqlSugar/Abstract/DbProvider/DbBindProvider/DbBindAccessory.cs
Normal file
@ -0,0 +1,144 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
namespace SqlSugar
|
||||
{
|
||||
public partial class DbBindAccessory
|
||||
{
|
||||
protected List<T> GetEntityList<T>(Type type,SqlSugarClient context, IDataReader dataReader,string fields)
|
||||
{
|
||||
var cacheManager = CacheManager<IDataReaderEntityBuilder<T>>.GetInstance();
|
||||
string key = "DataReaderToList." + fields +context.CurrentConnectionConfig.DbType+ type.FullName;
|
||||
IDataReaderEntityBuilder<T> eblist = null;
|
||||
if (cacheManager.ContainsKey(key))
|
||||
{
|
||||
eblist = cacheManager[key];
|
||||
}
|
||||
else
|
||||
{
|
||||
eblist = IDataReaderEntityBuilder<T>.CreateBuilder(type, dataReader, context);
|
||||
cacheManager.Add(key, eblist);
|
||||
}
|
||||
List<T> list = new List<T>();
|
||||
try
|
||||
{
|
||||
if (dataReader == null) return list;
|
||||
while (dataReader.Read())
|
||||
{
|
||||
list.Add(eblist.Build(dataReader));
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Check.Exception(true,ErrorMessage.EntityMappingError, ex.Message);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
protected List<T> GetKeyValueList<T>(Type type, IDataReader dataReader)
|
||||
{
|
||||
List<T> reval = new List<T>();
|
||||
using (IDataReader re = dataReader)
|
||||
{
|
||||
while (re.Read())
|
||||
{
|
||||
if (PubConst.DicOO == type)
|
||||
{
|
||||
var kv = new KeyValuePair<object, object>(dataReader.GetValue(0), re.GetValue(1));
|
||||
reval.Add((T)Convert.ChangeType(kv, typeof(KeyValuePair<object, object>)));
|
||||
}
|
||||
else if (PubConst.DicIS == type)
|
||||
{
|
||||
var kv = new KeyValuePair<int, string>(dataReader.GetValue(0).ObjToInt(), re.GetValue(1).ObjToString());
|
||||
reval.Add((T)Convert.ChangeType(kv, typeof(KeyValuePair<int, string>)));
|
||||
}
|
||||
else if (PubConst.Dicii == type)
|
||||
{
|
||||
var kv = new KeyValuePair<int, int>(dataReader.GetValue(0).ObjToInt(), re.GetValue(1).ObjToInt());
|
||||
reval.Add((T)Convert.ChangeType(kv, typeof(KeyValuePair<int, int>)));
|
||||
}
|
||||
else if (PubConst.DicSi == type)
|
||||
{
|
||||
var kv = new KeyValuePair<string, int>(dataReader.GetValue(0).ObjToString(), re.GetValue(1).ObjToInt());
|
||||
reval.Add((T)Convert.ChangeType(kv, typeof(KeyValuePair<string, int>)));
|
||||
}
|
||||
else if (PubConst.DicSo == type)
|
||||
{
|
||||
var kv = new KeyValuePair<string, object>(dataReader.GetValue(0).ObjToString(), re.GetValue(1));
|
||||
reval.Add((T)Convert.ChangeType(kv, typeof(KeyValuePair<string, object>)));
|
||||
}
|
||||
else if (PubConst.DicSS == type)
|
||||
{
|
||||
var kv = new KeyValuePair<string, string>(dataReader.GetValue(0).ObjToString(), dataReader.GetValue(1).ObjToString());
|
||||
reval.Add((T)Convert.ChangeType(kv, typeof(KeyValuePair<string, string>)));
|
||||
}
|
||||
else
|
||||
{
|
||||
Check.Exception(true,ErrorMessage.NotSupportedDictionary);
|
||||
}
|
||||
}
|
||||
}
|
||||
return reval;
|
||||
}
|
||||
|
||||
protected List<T> GetArrayList<T>(Type type, IDataReader dataReader)
|
||||
{
|
||||
List<T> reval = new List<T>();
|
||||
using (IDataReader re = dataReader)
|
||||
{
|
||||
int count = dataReader.FieldCount;
|
||||
var childType = type.GetElementType();
|
||||
while (re.Read())
|
||||
{
|
||||
object[] array = new object[count];
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
array[i] = Convert.ChangeType(re.GetValue(i), childType);
|
||||
}
|
||||
if (childType == PubConst.StringType)
|
||||
reval.Add((T)Convert.ChangeType(array.Select(it => it.ObjToString()).ToArray(), type));
|
||||
else if (childType == PubConst.ObjType)
|
||||
reval.Add((T)Convert.ChangeType(array.Select(it => it == DBNull.Value ? null : (object)it).ToArray(), type));
|
||||
else if (childType == PubConst.BoolType)
|
||||
reval.Add((T)Convert.ChangeType(array.Select(it => it.ObjToBool()).ToArray(), type));
|
||||
else if (childType == PubConst.ByteType)
|
||||
reval.Add((T)Convert.ChangeType(array.Select(it => it == DBNull.Value ? 0 : (byte)it).ToArray(), type));
|
||||
else if (childType == PubConst.DecType)
|
||||
reval.Add((T)Convert.ChangeType(array.Select(it => it.ObjToDecimal()).ToArray(), type));
|
||||
else if (childType == PubConst.GuidType)
|
||||
reval.Add((T)Convert.ChangeType(array.Select(it => it == DBNull.Value ? Guid.Empty : (Guid)it).ToArray(), type));
|
||||
else if (childType == PubConst.DateType)
|
||||
reval.Add((T)Convert.ChangeType(array.Select(it => it == DBNull.Value ? DateTime.MinValue : (DateTime)it).ToArray(), type));
|
||||
else if (childType == PubConst.IntType)
|
||||
reval.Add((T)Convert.ChangeType(array.Select(it => it.ObjToInt()).ToArray(), type));
|
||||
else
|
||||
Check.Exception(true,ErrorMessage.NotSupportedArray);
|
||||
}
|
||||
}
|
||||
return reval;
|
||||
}
|
||||
|
||||
protected List<T> GetValueTypeList<T>(Type type, IDataReader dataReader)
|
||||
{
|
||||
List<T> reval = new List<T>();
|
||||
using (IDataReader re = dataReader)
|
||||
{
|
||||
while (re.Read())
|
||||
{
|
||||
var value = re.GetValue(0);
|
||||
if (value == DBNull.Value)
|
||||
{
|
||||
reval.Add(default(T));
|
||||
}
|
||||
else
|
||||
{
|
||||
reval.Add((T)Convert.ChangeType(re.GetValue(0), type));
|
||||
}
|
||||
}
|
||||
}
|
||||
return reval;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,83 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
namespace SqlSugar
|
||||
{
|
||||
public abstract partial class DbBindProvider : DbBindAccessory, IDbBind
|
||||
{
|
||||
public virtual SqlSugarClient Context { get; set; }
|
||||
public virtual List<string> GuidThrow
|
||||
{
|
||||
get
|
||||
{
|
||||
return new List<string>() { "int32", "datetime", "decimal", "double", "byte", "string" };
|
||||
}
|
||||
}
|
||||
public virtual List<string> IntThrow
|
||||
{
|
||||
get
|
||||
{
|
||||
return new List<string>() { "datetime", "byte" };
|
||||
}
|
||||
}
|
||||
public virtual List<string> StringThrow
|
||||
{
|
||||
get
|
||||
{
|
||||
return new List<string>() { "int32", "datetime", "decimal", "double", "byte", "guid" };
|
||||
}
|
||||
}
|
||||
public virtual List<string> DecimalThrow
|
||||
{
|
||||
get
|
||||
{
|
||||
return new List<string>() { "datetime", "byte", "guid" };
|
||||
}
|
||||
}
|
||||
public virtual List<string> DoubleThrow
|
||||
{
|
||||
get
|
||||
{
|
||||
return new List<string>() { "datetime", "byte", "guid" };
|
||||
}
|
||||
}
|
||||
public virtual List<string> DateThrow
|
||||
{
|
||||
get
|
||||
{
|
||||
return new List<string>() { "int32", "decimal", "double", "byte", "guid" };
|
||||
}
|
||||
}
|
||||
public virtual List<string> ShortThrow
|
||||
{
|
||||
get
|
||||
{
|
||||
return new List<string>() { "datetime", "guid" };
|
||||
}
|
||||
}
|
||||
public virtual List<T> DataReaderToList<T>(Type type, IDataReader dataReader,string fields)
|
||||
{
|
||||
if (type.Name.Contains("KeyValuePair"))
|
||||
{
|
||||
return GetKeyValueList<T>(type, dataReader);
|
||||
}
|
||||
else if (type.IsValueType || type == PubConst.StringType)
|
||||
{
|
||||
return GetKeyValueList<T>(type, dataReader);
|
||||
}
|
||||
else if (type.IsArray)
|
||||
{
|
||||
return GetArrayList<T>(type, dataReader);
|
||||
}
|
||||
else
|
||||
{
|
||||
return GetEntityList<T>(type, Context, dataReader,fields);
|
||||
}
|
||||
}
|
||||
public abstract string ChangeDBTypeToCSharpType(string typeName);
|
||||
}
|
||||
}
|
@ -0,0 +1,258 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Data;
|
||||
using System.Reflection;
|
||||
using System.Reflection.Emit;
|
||||
namespace SqlSugar
|
||||
{
|
||||
public partial class IDataReaderEntityBuilder<T>
|
||||
{
|
||||
#region fields
|
||||
private static readonly MethodInfo isDBNullMethod = typeof(IDataRecord).GetMethod("IsDBNull", new Type[] { typeof(int) });
|
||||
private static readonly MethodInfo getValueMethod = typeof(IDataRecord).GetMethod("get_Item", new Type[] { typeof(int) });
|
||||
private static readonly MethodInfo getBoolean = typeof(IDataRecord).GetMethod("GetBoolean", new Type[] { typeof(int) });
|
||||
private static readonly MethodInfo getByte = typeof(IDataRecord).GetMethod("GetByte", new Type[] { typeof(int) });
|
||||
private static readonly MethodInfo getDateTime = typeof(IDataRecord).GetMethod("GetDateTime", new Type[] { typeof(int) });
|
||||
private static readonly MethodInfo getDecimal = typeof(IDataRecord).GetMethod("GetDecimal", new Type[] { typeof(int) });
|
||||
private static readonly MethodInfo getDouble = typeof(IDataRecord).GetMethod("GetDouble", new Type[] { typeof(int) });
|
||||
private static readonly MethodInfo getFloat = typeof(IDataRecord).GetMethod("GetFloat", new Type[] { typeof(int) });
|
||||
private static readonly MethodInfo getGuid = typeof(IDataRecord).GetMethod("GetGuid", new Type[] { typeof(int) });
|
||||
private static readonly MethodInfo getInt16 = typeof(IDataRecord).GetMethod("GetInt16", new Type[] { typeof(int) });
|
||||
private static readonly MethodInfo getInt32 = typeof(IDataRecord).GetMethod("GetInt32", new Type[] { typeof(int) });
|
||||
private static readonly MethodInfo getInt64 = typeof(IDataRecord).GetMethod("GetInt64", new Type[] { typeof(int) });
|
||||
private static readonly MethodInfo getString = typeof(IDataRecord).GetMethod("GetString", new Type[] { typeof(int) });
|
||||
private static readonly MethodInfo getEnum = typeof(IDataRecordExtensions).GetMethod("getEnum");
|
||||
private static readonly MethodInfo getConvertFloat = typeof(IDataRecordExtensions).GetMethod("GetConvertFloat");
|
||||
private static readonly MethodInfo getConvertBoolean = typeof(IDataRecordExtensions).GetMethod("GetConvertBoolean");
|
||||
private static readonly MethodInfo getConvertByte = typeof(IDataRecordExtensions).GetMethod("GetConvertByte");
|
||||
private static readonly MethodInfo getConvertChar = typeof(IDataRecordExtensions).GetMethod("GetConvertChar");
|
||||
private static readonly MethodInfo getConvertDateTime = typeof(IDataRecordExtensions).GetMethod("GetConvertDateTime");
|
||||
private static readonly MethodInfo getConvertDecimal = typeof(IDataRecordExtensions).GetMethod("GetConvertDecimal");
|
||||
private static readonly MethodInfo getConvertDouble = typeof(IDataRecordExtensions).GetMethod("GetConvertDouble");
|
||||
private static readonly MethodInfo getConvertGuid = typeof(IDataRecordExtensions).GetMethod("GetConvertGuid");
|
||||
private static readonly MethodInfo getConvertInt16 = typeof(IDataRecordExtensions).GetMethod("GetConvertInt16");
|
||||
private static readonly MethodInfo getConvertInt32 = typeof(IDataRecordExtensions).GetMethod("GetConvertInt32");
|
||||
private static readonly MethodInfo getConvetInt64 = typeof(IDataRecordExtensions).GetMethod("GetConvetInt64");
|
||||
private static readonly MethodInfo getConvertEnum_Null = typeof(IDataRecordExtensions).GetMethod("GetConvertEnum_Null");
|
||||
private static readonly MethodInfo getOtherNull = typeof(IDataRecordExtensions).GetMethod("GetOtherNull");
|
||||
private static readonly MethodInfo getOther = typeof(IDataRecordExtensions).GetMethod("GetOther");
|
||||
private delegate T Load(IDataRecord dataRecord);
|
||||
private Load handler;
|
||||
#endregion
|
||||
|
||||
#region functions
|
||||
public T Build(IDataRecord dataRecord)
|
||||
{
|
||||
return handler(dataRecord);
|
||||
}
|
||||
|
||||
public static IDataReaderEntityBuilder<T> CreateBuilder(Type type, IDataRecord dataRecord, SqlSugarClient context)
|
||||
{
|
||||
IDataReaderEntityBuilder<T> dynamicBuilder = new IDataReaderEntityBuilder<T>();
|
||||
DynamicMethod method = new DynamicMethod("DynamicCreateEntity", type,
|
||||
new Type[] { typeof(IDataRecord) }, type, true);
|
||||
ILGenerator generator = method.GetILGenerator();
|
||||
LocalBuilder result = generator.DeclareLocal(type);
|
||||
generator.Emit(OpCodes.Newobj, type.GetConstructor(Type.EmptyTypes));
|
||||
generator.Emit(OpCodes.Stloc, result);
|
||||
var classFieldNames = typeof(T).GetProperties().Select(it => it.Name).ToList();
|
||||
for (int i = 0; i < dataRecord.FieldCount; i++)
|
||||
{
|
||||
string dbFieldName = dataRecord.GetName(i);
|
||||
if (!classFieldNames.Any(it => it == dbFieldName) && classFieldNames.Any(it => it.Equals(dbFieldName, StringComparison.CurrentCultureIgnoreCase)))
|
||||
{
|
||||
dbFieldName = classFieldNames.Single(it => it.ToLower() == dbFieldName.ToLower());
|
||||
}
|
||||
PropertyInfo propertyInfo = type.GetProperty(dbFieldName);
|
||||
Label endIfLabel = generator.DefineLabel();
|
||||
if (propertyInfo != null && propertyInfo.GetSetMethod() != null)
|
||||
{
|
||||
bool isNullable = false;
|
||||
var underType = PubMethod.GetUnderType(propertyInfo, ref isNullable);
|
||||
|
||||
generator.Emit(OpCodes.Ldarg_0);
|
||||
generator.Emit(OpCodes.Ldc_I4, i);
|
||||
generator.Emit(OpCodes.Callvirt, isDBNullMethod);
|
||||
generator.Emit(OpCodes.Brtrue, endIfLabel);
|
||||
generator.Emit(OpCodes.Ldloc, result);
|
||||
generator.Emit(OpCodes.Ldarg_0);
|
||||
generator.Emit(OpCodes.Ldc_I4, i);
|
||||
GeneratorCallMethod(generator, underType, isNullable, propertyInfo, dataRecord.GetDataTypeName(i), dbFieldName, context);
|
||||
generator.Emit(OpCodes.Callvirt, propertyInfo.GetSetMethod());
|
||||
generator.MarkLabel(endIfLabel);
|
||||
}
|
||||
}
|
||||
generator.Emit(OpCodes.Ldloc, result);
|
||||
generator.Emit(OpCodes.Ret);
|
||||
dynamicBuilder.handler = (Load)method.CreateDelegate(typeof(Load));
|
||||
return dynamicBuilder;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region helpers
|
||||
private static void CheckType(List<string> errorTypes, string objType, string dbType, string field)
|
||||
{
|
||||
var isAny = errorTypes.Contains(objType);
|
||||
if (isAny)
|
||||
{
|
||||
throw new SqlSugarException(string.Format("{0} can't convert {1} to {2}", field, dbType, objType));
|
||||
}
|
||||
}
|
||||
|
||||
private static void GeneratorCallMethod(ILGenerator generator, Type type, bool isNullable, PropertyInfo pro, string dbTypeName, string fieldName, SqlSugarClient context)
|
||||
{
|
||||
var bind = context.Database.DbBind;
|
||||
List<string> guidThrow = bind.GuidThrow;
|
||||
List<string> intThrow = bind.IntThrow;
|
||||
List<string> stringThrow = bind.StringThrow;
|
||||
List<string> decimalThrow = bind.DecimalThrow;
|
||||
List<string> doubleThrow = bind.DoubleThrow;
|
||||
List<string> dateThrow = bind.DateThrow;
|
||||
List<string> shortThrow = bind.ShortThrow;
|
||||
MethodInfo method = null;
|
||||
var typeName = bind.ChangeDBTypeToCSharpType(dbTypeName);
|
||||
var objTypeName = type.Name.ToLower();
|
||||
var isEnum = type.IsEnum;
|
||||
if (isEnum)
|
||||
{
|
||||
typeName = "enum";
|
||||
}
|
||||
else if (typeName.IsIn("byte[]", "other", "object") || dbTypeName.Contains("hierarchyid"))
|
||||
{
|
||||
generator.Emit(OpCodes.Call, getValueMethod);
|
||||
generator.Emit(OpCodes.Unbox_Any, pro.PropertyType);
|
||||
return;
|
||||
}
|
||||
if (isNullable)
|
||||
{
|
||||
switch (typeName)
|
||||
{
|
||||
case "int":
|
||||
CheckType(intThrow, objTypeName, typeName, fieldName);
|
||||
var isNotInt = objTypeName != "int32";
|
||||
if (isNotInt)
|
||||
method = getOtherNull.MakeGenericMethod(type);
|
||||
else
|
||||
method = getConvertInt32; break;
|
||||
case "bool":
|
||||
if (objTypeName != "bool" && objTypeName != "boolean")
|
||||
method = getOtherNull.MakeGenericMethod(type);
|
||||
else
|
||||
method = getConvertBoolean; break;
|
||||
case "string":
|
||||
CheckType(stringThrow, objTypeName, typeName, fieldName);
|
||||
method = getString; break;
|
||||
case "dateTime":
|
||||
CheckType(dateThrow, objTypeName, typeName, fieldName);
|
||||
if (objTypeName != "datetime")
|
||||
method = getOtherNull.MakeGenericMethod(type);
|
||||
else
|
||||
method = getConvertDateTime; break;
|
||||
case "decimal":
|
||||
CheckType(decimalThrow, objTypeName, typeName, fieldName);
|
||||
var isNotDecimal = objTypeName != "decimal";
|
||||
if (isNotDecimal)
|
||||
method = getOtherNull.MakeGenericMethod(type);
|
||||
else
|
||||
method = getConvertDecimal; break;
|
||||
case "double":
|
||||
CheckType(doubleThrow, objTypeName, typeName, fieldName);
|
||||
var isNotDouble = objTypeName != "double";
|
||||
if (isNotDouble)
|
||||
method = getOtherNull.MakeGenericMethod(type);
|
||||
else
|
||||
method = getConvertDouble; break;
|
||||
case "guid":
|
||||
CheckType(guidThrow, objTypeName, typeName, fieldName);
|
||||
if (objTypeName != "guid")
|
||||
method = getOtherNull.MakeGenericMethod(type);
|
||||
else
|
||||
method = getConvertGuid; break;
|
||||
case "byte":
|
||||
method = getConvertByte; break;
|
||||
case "enum":
|
||||
method = getConvertEnum_Null.MakeGenericMethod(type); break;
|
||||
case "short":
|
||||
CheckType(shortThrow, objTypeName, typeName, fieldName);
|
||||
var isNotShort = objTypeName != "int16" && objTypeName != "short";
|
||||
if (isNotShort)
|
||||
method = getOtherNull.MakeGenericMethod(type);
|
||||
else
|
||||
method = getConvertInt16;
|
||||
break;
|
||||
default:
|
||||
method = getOtherNull.MakeGenericMethod(type); break;
|
||||
}
|
||||
generator.Emit(OpCodes.Call, method);
|
||||
}
|
||||
else
|
||||
{
|
||||
switch (typeName)
|
||||
{
|
||||
case "int":
|
||||
CheckType(intThrow, objTypeName, typeName, fieldName);
|
||||
var isNotInt = objTypeName != "int32";
|
||||
if (isNotInt)
|
||||
method = getOther.MakeGenericMethod(type);
|
||||
else
|
||||
method = getInt32; break;
|
||||
case "bool":
|
||||
if (objTypeName != "bool" && objTypeName != "boolean")
|
||||
method = getOther.MakeGenericMethod(type);
|
||||
else
|
||||
method = getBoolean; break;
|
||||
case "string":
|
||||
CheckType(stringThrow, objTypeName, typeName, fieldName);
|
||||
method = getString; break;
|
||||
case "dateTime":
|
||||
CheckType(dateThrow, objTypeName, typeName, fieldName);
|
||||
if (objTypeName != "datetime")
|
||||
method = getOther.MakeGenericMethod(type);
|
||||
else
|
||||
method = getDateTime; break;
|
||||
case "decimal":
|
||||
CheckType(decimalThrow, objTypeName, typeName, fieldName);
|
||||
var isNotDecimal = objTypeName != "decimal";
|
||||
if (isNotDecimal)
|
||||
method = getOther.MakeGenericMethod(type);
|
||||
else
|
||||
method = getDecimal; break;
|
||||
case "double":
|
||||
CheckType(doubleThrow, objTypeName, typeName, fieldName);
|
||||
var isNotDouble = objTypeName != "double";
|
||||
if (isNotDouble)
|
||||
method = getOther.MakeGenericMethod(type);
|
||||
else
|
||||
method = getDouble; break;
|
||||
case "guid":
|
||||
CheckType(guidThrow, objTypeName, typeName, fieldName);
|
||||
if (objTypeName != "guid")
|
||||
method = getOther.MakeGenericMethod(type);
|
||||
else
|
||||
method = getGuid; break;
|
||||
case "byte":
|
||||
method = getByte; break;
|
||||
case "enum":
|
||||
method = getEnum; break;
|
||||
case "short":
|
||||
CheckType(shortThrow, objTypeName, typeName, fieldName);
|
||||
var isNotShort = objTypeName != "int16" && objTypeName != "short";
|
||||
if (isNotShort)
|
||||
method = getOther.MakeGenericMethod(type);
|
||||
else
|
||||
method = getInt16;
|
||||
break;
|
||||
default: method = getOther.MakeGenericMethod(type); break; ;
|
||||
}
|
||||
generator.Emit(OpCodes.Call, method);
|
||||
if (method == getValueMethod)
|
||||
{
|
||||
generator.Emit(OpCodes.Unbox_Any, pro.PropertyType);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
@ -0,0 +1,156 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Data;
|
||||
namespace SqlSugar
|
||||
{
|
||||
public static partial class IDataRecordExtensions
|
||||
{
|
||||
public static bool? GetConvertBoolean(this IDataRecord dr, int i)
|
||||
{
|
||||
if (dr.IsDBNull(i))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
var reval = dr.GetBoolean(i);
|
||||
return reval;
|
||||
}
|
||||
|
||||
public static byte? GetConvertByte(this IDataRecord dr, int i)
|
||||
{
|
||||
if (dr.IsDBNull(i))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
var reval = dr.GetByte(i);
|
||||
return reval;
|
||||
}
|
||||
|
||||
public static char? GetConvertChar(this IDataRecord dr, int i)
|
||||
{
|
||||
if (dr.IsDBNull(i))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
var reval = dr.GetChar(i);
|
||||
return reval;
|
||||
}
|
||||
|
||||
public static DateTime? GetConvertDateTime(this IDataRecord dr, int i)
|
||||
{
|
||||
if (dr.IsDBNull(i))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
var reval = dr.GetDateTime(i);
|
||||
return reval;
|
||||
}
|
||||
|
||||
public static decimal? GetConvertDecimal(this IDataRecord dr, int i)
|
||||
{
|
||||
if (dr.IsDBNull(i))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
var reval = dr.GetDecimal(i);
|
||||
return reval;
|
||||
}
|
||||
|
||||
|
||||
public static double? GetConvertDouble(this IDataRecord dr, int i)
|
||||
{
|
||||
if (dr.IsDBNull(i))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
var reval = dr.GetDouble(i);
|
||||
return reval;
|
||||
}
|
||||
|
||||
public static Guid? GetConvertGuid(this IDataRecord dr, int i)
|
||||
{
|
||||
if (dr.IsDBNull(i))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
var reval = dr.GetGuid(i);
|
||||
return reval;
|
||||
}
|
||||
|
||||
public static short? GetConvertInt16(this IDataRecord dr, int i)
|
||||
{
|
||||
if (dr.IsDBNull(i))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
var reval = dr.GetInt16(i);
|
||||
return reval;
|
||||
}
|
||||
|
||||
|
||||
public static Int32? GetConvertInt32(this IDataRecord dr, int i)
|
||||
{
|
||||
if (dr.IsDBNull(i))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
var reval = dr.GetInt32(i);
|
||||
return reval;
|
||||
}
|
||||
|
||||
public static long? GetConvetInt64(this IDataRecord dr, int i)
|
||||
{
|
||||
if (dr.IsDBNull(i))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
var reval = dr.GetInt64(i);
|
||||
return reval;
|
||||
}
|
||||
|
||||
public static float? GetConvertFloat(this IDataRecord dr, int i)
|
||||
{
|
||||
if (dr.IsDBNull(i))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
var reval = dr.GetFloat(i);
|
||||
return reval;
|
||||
}
|
||||
|
||||
public static Nullable<T> GetOtherNull<T>(this IDataReader dr, int i) where T : struct
|
||||
{
|
||||
if (dr.IsDBNull(i))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return (T)Convert.ChangeType(dr.GetValue(i), typeof(T));
|
||||
|
||||
}
|
||||
|
||||
public static T GetOther<T>(this IDataReader dr, int i)
|
||||
{
|
||||
return (T)Convert.ChangeType(dr.GetValue(i), typeof(T));
|
||||
}
|
||||
|
||||
public static Nullable<T> GetConvertEnum_Null<T>(this IDataReader dr, int i) where T : struct
|
||||
{
|
||||
if (dr.IsDBNull(i))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
object value = dr.GetValue(i);
|
||||
T t = (T)Enum.ToObject(typeof(T), value);
|
||||
return t;
|
||||
}
|
||||
|
||||
public static T GetEnum<T>(this IDataReader dr, int i) where T : struct
|
||||
{
|
||||
object value = dr.GetValue(i);
|
||||
T t = (T)Enum.ToObject(typeof(T), value);
|
||||
return t;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
11
SqlSugar/Abstract/DbProvider/DbFirstProvider.cs
Normal file
11
SqlSugar/Abstract/DbProvider/DbFirstProvider.cs
Normal file
@ -0,0 +1,11 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
namespace SqlSugar
|
||||
{
|
||||
public abstract partial class DbFirstProvider : IDbFirst
|
||||
{
|
||||
public virtual SqlSugarClient Context { get; set; }
|
||||
}
|
||||
}
|
175
SqlSugar/Abstract/DbProvider/DbMaintenanceProvider.cs
Normal file
175
SqlSugar/Abstract/DbProvider/DbMaintenanceProvider.cs
Normal file
@ -0,0 +1,175 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
namespace SqlSugar
|
||||
{
|
||||
public abstract partial class DbMaintenanceProvider : IDbMaintenance
|
||||
{
|
||||
public SqlSugarClient Context { get; set; }
|
||||
|
||||
protected abstract string GetViewInfoListSql { get; }
|
||||
public List<DbTableInfo> GetViewInfoList()
|
||||
{
|
||||
if (this.IsSystemTables())
|
||||
{
|
||||
string key = "DbMaintenanceProvider.GetViewInfoList";
|
||||
return GetListOrCache<DbTableInfo>(key, this.GetViewInfoListSql);
|
||||
}
|
||||
else
|
||||
{
|
||||
return new List<DbTableInfo>();
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract string GetTableInfoListSql { get; }
|
||||
public List<DbTableInfo> GetTableInfoList()
|
||||
{
|
||||
if (this.IsSystemTables())
|
||||
{
|
||||
string key = "DbMaintenanceProvider.GetTableInfoList";
|
||||
return GetListOrCache<DbTableInfo>(key, this.GetTableInfoListSql);
|
||||
}
|
||||
else
|
||||
{
|
||||
return CacheFactory.Func<List<DbTableInfo>>("DbMaintenanceProvider.GetTableInfoList2",
|
||||
(cm, key) =>
|
||||
{
|
||||
return cm[key];
|
||||
},
|
||||
(cm, key) =>
|
||||
{
|
||||
List<DbTableInfo> reval = new List<DbTableInfo>();
|
||||
var classes = Assembly.Load(this.Context.EntityNamespace.Split('.').First()).GetTypes();
|
||||
foreach (var item in classes)
|
||||
{
|
||||
if (item.Namespace == this.Context.EntityNamespace)
|
||||
{
|
||||
var sugarTableObj = item.GetCustomAttributes(typeof(SugarTable), true).Where(it => it is SugarTable).SingleOrDefault();
|
||||
if (sugarTableObj.IsNullOrEmpty())
|
||||
{
|
||||
reval.Add(new DbTableInfo() { Name = item.Name });
|
||||
}
|
||||
else
|
||||
{
|
||||
var sugarTable = (SugarTable)sugarTableObj;
|
||||
reval.Add(new DbTableInfo() { Name = sugarTable.TableName });
|
||||
}
|
||||
}
|
||||
}
|
||||
return reval;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract string GetColumnInfosByTableNameSql { get; }
|
||||
public virtual List<DbColumnInfo> GetColumnInfosByTableName(string tableName)
|
||||
{
|
||||
if (this.IsSystemTables())
|
||||
{
|
||||
string key = "DbMaintenanceProvider.GetColumnInfosByTableName." + tableName.ToLower();
|
||||
return GetListOrCache<DbColumnInfo>(key, this.GetColumnInfosByTableNameSql);
|
||||
}
|
||||
else
|
||||
{
|
||||
string cacheKey = "DbMaintenanceProvider.GetColumnInfosByTableName." + tableName.ToLower() + "2";
|
||||
return CacheFactory.Func<List<DbColumnInfo>>(cacheKey,
|
||||
(cm, key) =>
|
||||
{
|
||||
return cm[key];
|
||||
},
|
||||
(cm, key) =>
|
||||
{
|
||||
List<DbColumnInfo> reval = new List<DbColumnInfo>();
|
||||
if (this.Context.MappingTables.IsNullOrEmpty())
|
||||
{
|
||||
this.GetTableInfoList();
|
||||
}
|
||||
var entity = this.Context.MappingTables.SingleOrDefault(it => it.DbTableName.Equals(tableName, StringComparison.CurrentCultureIgnoreCase));
|
||||
var entityName = entity == null ? tableName : entity.EntityName;
|
||||
var assembly = Assembly.Load(this.Context.EntityNamespace.Split('.').First());
|
||||
foreach (var item in assembly.GetType(this.Context.EntityNamespace + "." + entityName).GetProperties())
|
||||
{
|
||||
var isVirtual = item.GetGetMethod().IsVirtual;
|
||||
if (isVirtual) continue;
|
||||
var sugarColumn = item.GetCustomAttributes(typeof(SugarColumn), true)
|
||||
.Where(it => it is SugarColumn)
|
||||
.Select(it => (SugarColumn)it)
|
||||
.Where(it => it.ColumnName.IsValuable())
|
||||
.FirstOrDefault();
|
||||
if (sugarColumn.IsNullOrEmpty())
|
||||
{
|
||||
reval.Add(new DbColumnInfo() { ColumnName = item.Name });
|
||||
}
|
||||
else
|
||||
{
|
||||
if (sugarColumn.IsIgnore == false)
|
||||
{
|
||||
var columnInfo = new DbColumnInfo();
|
||||
columnInfo.ColumnName = sugarColumn.ColumnName.IsNullOrEmpty() ? item.Name : sugarColumn.ColumnName;
|
||||
columnInfo.IsPrimarykey = sugarColumn.IsPrimaryKey;
|
||||
columnInfo.IsIdentity = sugarColumn.IsIdentity;
|
||||
columnInfo.ColumnDescription = sugarColumn.ColumnDescription;
|
||||
columnInfo.TableName = entity.IsNullOrEmpty() ? tableName : entity.DbTableName;
|
||||
reval.Add(columnInfo);
|
||||
}
|
||||
}
|
||||
}
|
||||
return reval;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected abstract string AddColumnToTableSql { get; }
|
||||
public bool AddColumnToTable(string tableName, DbColumnInfo column)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
protected abstract string BackupDataBaseSql { get; }
|
||||
public bool BackupDataBase()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
protected abstract string CreateTableSql { get; }
|
||||
public virtual bool CreateTable(string tableName, List<DbColumnInfo> columns)
|
||||
{
|
||||
this.Context.Database.ExecuteCommand(this.CreateTableSql);
|
||||
return true;
|
||||
}
|
||||
|
||||
protected abstract string TruncateTableSql { get; }
|
||||
public virtual bool TruncateTable(string tableName)
|
||||
{
|
||||
this.Context.Database.ExecuteCommand(this.TruncateTableSql);
|
||||
return true;
|
||||
}
|
||||
|
||||
#region Private
|
||||
private List<T> GetListOrCache<T>(string cacheKey, string sql)
|
||||
{
|
||||
return CacheFactory.Func<List<T>>(cacheKey,
|
||||
(cm, key) =>
|
||||
{
|
||||
return cm[cacheKey];
|
||||
|
||||
}, (cm, key) =>
|
||||
{
|
||||
var isEnableLogEvent = this.Context.Database.IsEnableLogEvent;
|
||||
this.Context.Database.IsEnableLogEvent = false;
|
||||
var reval = this.Context.SqlQuery<T>(this.GetColumnInfosByTableNameSql);
|
||||
this.Context.Database.IsEnableLogEvent = isEnableLogEvent;
|
||||
return reval;
|
||||
});
|
||||
}
|
||||
private bool IsSystemTables()
|
||||
{
|
||||
var isSystemTables = Context.CurrentConnectionConfig is SystemTablesConfig;
|
||||
return isSystemTables;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
291
SqlSugar/Abstract/DbProvider/DbProvider.cs
Normal file
291
SqlSugar/Abstract/DbProvider/DbProvider.cs
Normal file
@ -0,0 +1,291 @@
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
namespace SqlSugar
|
||||
{
|
||||
public abstract partial class DbProvider : DbAccessory, IDb
|
||||
{
|
||||
public DbProvider()
|
||||
{
|
||||
this.IsEnableLogEvent = false;
|
||||
this.CommandType = CommandType.Text;
|
||||
this.IsClearParameters = true;
|
||||
this.CommandTimeOut = 30000;
|
||||
}
|
||||
|
||||
public IDbTransaction Transaction { get; set; }
|
||||
public virtual SqlSugarClient Context { get; set; }
|
||||
public virtual IConnectionConfig MasterConnectionConfig { get; set; }
|
||||
public virtual List<IConnectionConfig> SlaveConnectionConfigs { get; set; }
|
||||
public virtual IDbBind DbBind
|
||||
{
|
||||
get
|
||||
{
|
||||
if (base._DbBind == null)
|
||||
{
|
||||
IDbBind bind = InstanceFactory.GetDbBind(this.Context.CurrentConnectionConfig);
|
||||
base._DbBind = bind;
|
||||
bind.Context = this.Context;
|
||||
}
|
||||
return base._DbBind;
|
||||
}
|
||||
}
|
||||
public virtual IDbFirst DbFirst
|
||||
{
|
||||
get
|
||||
{
|
||||
if (base._DbFirst == null)
|
||||
{
|
||||
IDbFirst dbFirst = InstanceFactory.GetDbFirst(this.Context.CurrentConnectionConfig);
|
||||
base._DbFirst = dbFirst;
|
||||
dbFirst.Context = this.Context;
|
||||
}
|
||||
return base._DbFirst;
|
||||
}
|
||||
}
|
||||
public virtual ICodeFirst CodeFirst
|
||||
{
|
||||
get
|
||||
{
|
||||
if (base._CodeFirst == null)
|
||||
{
|
||||
ICodeFirst codeFirst = InstanceFactory.GetCodeFirst(this.Context.CurrentConnectionConfig);
|
||||
base._CodeFirst = codeFirst;
|
||||
codeFirst.Context = this.Context;
|
||||
}
|
||||
return base._CodeFirst;
|
||||
}
|
||||
}
|
||||
public virtual IDbMaintenance DbMaintenance
|
||||
{
|
||||
get
|
||||
{
|
||||
if (base._DbMaintenance == null)
|
||||
{
|
||||
IDbMaintenance maintenance = InstanceFactory.GetDbMaintenance(this.Context.CurrentConnectionConfig);
|
||||
base._DbMaintenance = maintenance;
|
||||
maintenance.Context = this.Context;
|
||||
}
|
||||
return base._DbMaintenance;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual int CommandTimeOut { get; set; }
|
||||
public virtual CommandType CommandType { get; set; }
|
||||
public virtual bool IsEnableLogEvent { get; set; }
|
||||
public virtual bool IsClearParameters { get; set; }
|
||||
public virtual Action<string, string> LogEventStarting { get; set; }
|
||||
public virtual Action<string, string> LogEventCompleted { get; set; }
|
||||
|
||||
|
||||
public virtual void Close()
|
||||
{
|
||||
if (this.Transaction != null)
|
||||
{
|
||||
this.Transaction.Commit();
|
||||
this.Transaction = null;
|
||||
}
|
||||
if (this.Connection != null && this.Connection.State != ConnectionState.Open)
|
||||
{
|
||||
this.Connection.Close();
|
||||
}
|
||||
}
|
||||
public virtual void Dispose()
|
||||
{
|
||||
if (this.Transaction != null)
|
||||
{
|
||||
this.Transaction.Commit();
|
||||
this.Transaction = null;
|
||||
}
|
||||
if (this.Connection != null && this.Connection.State != ConnectionState.Open)
|
||||
{
|
||||
this.Connection.Close();
|
||||
}
|
||||
if (this.Connection != null)
|
||||
{
|
||||
this.Connection.Dispose();
|
||||
}
|
||||
this.Connection = null;
|
||||
}
|
||||
public virtual void CheckConnection()
|
||||
{
|
||||
if (this.Connection.State != ConnectionState.Open)
|
||||
{
|
||||
this.Connection.Open();
|
||||
}
|
||||
}
|
||||
|
||||
public virtual string GetString(string sql, object pars)
|
||||
{
|
||||
return GetString(sql, this.GetParameters(pars));
|
||||
}
|
||||
public virtual string GetString(string sql, params SqlParameter[] pars)
|
||||
{
|
||||
return Convert.ToString(GetScalar(sql, pars));
|
||||
}
|
||||
public virtual int GetInt(string sql, object pars)
|
||||
{
|
||||
return GetInt(sql, this.GetParameters(pars));
|
||||
}
|
||||
public virtual int GetInt(string sql, params SqlParameter[] pars)
|
||||
{
|
||||
return Convert.ToInt32(GetScalar(sql, pars));
|
||||
}
|
||||
public virtual Double GetDouble(string sql, params SqlParameter[] pars)
|
||||
{
|
||||
return Convert.ToDouble(GetScalar(sql, pars));
|
||||
}
|
||||
public virtual decimal GetDecimal(string sql, params SqlParameter[] pars)
|
||||
{
|
||||
return Convert.ToDecimal(GetScalar(sql, pars));
|
||||
}
|
||||
public virtual DateTime GetDateTime(string sql, params SqlParameter[] pars)
|
||||
{
|
||||
return Convert.ToDateTime(GetScalar(sql, pars));
|
||||
}
|
||||
|
||||
public virtual SqlParameter[] GetParameters(object obj, PropertyInfo[] propertyInfo = null)
|
||||
{
|
||||
if (obj == null) return null;
|
||||
return base.GetParameters(obj, propertyInfo,this.Context.SqlBuilder.SqlParameterKeyWord);
|
||||
}
|
||||
|
||||
public virtual void BeginTran()
|
||||
{
|
||||
this.Connection.BeginTransaction();
|
||||
}
|
||||
public virtual void BeginTran(IsolationLevel iso)
|
||||
{
|
||||
this.Connection.BeginTransaction(iso);
|
||||
}
|
||||
public virtual void RollbackTran()
|
||||
{
|
||||
if (this.Transaction != null)
|
||||
{
|
||||
this.Transaction.Commit();
|
||||
this.Transaction = null;
|
||||
}
|
||||
}
|
||||
public virtual void CommitTran()
|
||||
{
|
||||
if (this.Transaction != null)
|
||||
{
|
||||
this.Transaction.Commit();
|
||||
this.Transaction = null;
|
||||
}
|
||||
}
|
||||
public abstract IDbDataParameter[] ToIDbDataParameter(params SqlParameter[] pars);
|
||||
public abstract void SetCommandToAdapter(IDataAdapter adapter, IDbCommand command);
|
||||
public abstract IDataAdapter GetAdapter();
|
||||
public abstract IDbCommand GetCommand(string sql, SqlParameter[] pars);
|
||||
public abstract IDbConnection Connection { get; set; }
|
||||
public abstract void BeginTran(string transactionName);//Only SqlServer
|
||||
public abstract void BeginTran(IsolationLevel iso, string transactionName);//Only SqlServer
|
||||
|
||||
#region Core
|
||||
public virtual int ExecuteCommand(string sql, params SqlParameter[] pars)
|
||||
{
|
||||
base.SetParSize(pars);
|
||||
ExecLogEvent(sql, pars, true);
|
||||
IDbCommand sqlCommand = GetCommand(sql, pars);
|
||||
int count = sqlCommand.ExecuteNonQuery();
|
||||
if (this.IsClearParameters)
|
||||
sqlCommand.Parameters.Clear();
|
||||
ExecLogEvent(sql, pars, false);
|
||||
return count;
|
||||
}
|
||||
public virtual IDataReader GetDataReader(string sql, params SqlParameter[] pars)
|
||||
{
|
||||
base.SetParSize(pars);
|
||||
ExecLogEvent(sql, pars, true);
|
||||
IDbCommand sqlCommand = GetCommand(sql, pars);
|
||||
IDataReader sqlDataReader = sqlCommand.ExecuteReader(CommandBehavior.CloseConnection);
|
||||
if (this.IsClearParameters)
|
||||
sqlCommand.Parameters.Clear();
|
||||
ExecLogEvent(sql, pars, false);
|
||||
return sqlDataReader;
|
||||
}
|
||||
public virtual DataSet GetDataSetAll(string sql, params SqlParameter[] pars)
|
||||
{
|
||||
base.SetParSize(pars);
|
||||
ExecLogEvent(sql, pars, true);
|
||||
IDataAdapter dataAdapter = this.GetAdapter();
|
||||
IDbCommand sqlCommand = GetCommand(sql, pars);
|
||||
this.SetCommandToAdapter(dataAdapter, sqlCommand);
|
||||
DataSet ds = new DataSet();
|
||||
dataAdapter.Fill(ds);
|
||||
if (this.IsClearParameters)
|
||||
sqlCommand.Parameters.Clear();
|
||||
ExecLogEvent(sql, pars, false);
|
||||
return ds;
|
||||
}
|
||||
public virtual object GetScalar(string sql, params SqlParameter[] pars)
|
||||
{
|
||||
base.SetParSize(pars);
|
||||
ExecLogEvent(sql, pars, true);
|
||||
IDbCommand sqlCommand = GetCommand(sql, pars);
|
||||
object scalar = sqlCommand.ExecuteScalar();
|
||||
scalar = (scalar == null ? 0 : scalar);
|
||||
if (this.IsClearParameters)
|
||||
sqlCommand.Parameters.Clear();
|
||||
ExecLogEvent(sql, pars, false);
|
||||
return scalar;
|
||||
}
|
||||
#endregion
|
||||
|
||||
public virtual DataTable GetDataTable(string sql, params SqlParameter[] pars)
|
||||
{
|
||||
var ds = GetDataSetAll(sql, pars);
|
||||
if (ds.Tables.Count != 0 && ds.Tables.Count > 0) return ds.Tables[0];
|
||||
return new DataTable();
|
||||
}
|
||||
public DataTable GetDataTable(string sql, object pars)
|
||||
{
|
||||
return GetDataTable(sql, this.GetParameters(pars));
|
||||
}
|
||||
public DataSet GetDataSetAll(string sql, object pars)
|
||||
{
|
||||
return GetDataSetAll(sql, this.GetParameters(pars));
|
||||
}
|
||||
public IDataReader GetDataReader(string sql, object pars)
|
||||
{
|
||||
return GetDataReader(sql, this.GetParameters(pars));
|
||||
}
|
||||
public virtual object GetScalar(string sql, object pars)
|
||||
{
|
||||
return GetScalar(sql, this.GetParameters(pars));
|
||||
}
|
||||
public virtual int ExecuteCommand(string sql, object pars)
|
||||
{
|
||||
return ExecuteCommand(sql, GetParameters(pars));
|
||||
}
|
||||
public virtual void ExecLogEvent(string sql, SqlParameter[] pars, bool isStarting = true)
|
||||
{
|
||||
if (this.IsEnableLogEvent)
|
||||
{
|
||||
Action<string, string> action = isStarting ? LogEventStarting : LogEventCompleted;
|
||||
if (action != null)
|
||||
{
|
||||
if (pars == null || pars.Length == 0)
|
||||
{
|
||||
action(sql, null);
|
||||
}
|
||||
else
|
||||
{
|
||||
action(sql, JsonConvert.SerializeObject(pars.Select(it => new { key = it.ParameterName, value = it.Value.ObjToString() })));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
public virtual void Open()
|
||||
{
|
||||
CheckConnection();
|
||||
}
|
||||
}
|
||||
}
|
63
SqlSugar/Abstract/DbProvider/SugarMappingAttribute.cs
Normal file
63
SqlSugar/Abstract/DbProvider/SugarMappingAttribute.cs
Normal file
@ -0,0 +1,63 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Reflection;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
[AttributeUsage(AttributeTargets.Class, Inherited = true)]
|
||||
public class SugarTable : Attribute {
|
||||
private SugarTable() { }
|
||||
public string TableName { get; set; }
|
||||
public SugarTable(string tableName) {
|
||||
this.TableName = tableName;
|
||||
}
|
||||
}
|
||||
[AttributeUsage(AttributeTargets.Property , Inherited = true)]
|
||||
public class SugarColumn : Attribute
|
||||
{
|
||||
private string _ColumnName;
|
||||
public string ColumnName
|
||||
{
|
||||
get { return _ColumnName; }
|
||||
set { _ColumnName = value; }
|
||||
}
|
||||
|
||||
private bool _IsIgnore;
|
||||
public bool IsIgnore
|
||||
{
|
||||
get { return _IsIgnore; }
|
||||
set { _IsIgnore = value; }
|
||||
}
|
||||
|
||||
private bool _IsPrimaryKey;
|
||||
public bool IsPrimaryKey
|
||||
{
|
||||
get { return _IsPrimaryKey; }
|
||||
set { _IsPrimaryKey = value; }
|
||||
}
|
||||
|
||||
private bool _IsIdentity;
|
||||
public bool IsIdentity
|
||||
{
|
||||
get { return _IsIdentity; }
|
||||
set { _IsIdentity = value; }
|
||||
}
|
||||
|
||||
private string _MappingKeys;
|
||||
public string MappingKeys
|
||||
{
|
||||
get { return _MappingKeys; }
|
||||
set { _MappingKeys = value; }
|
||||
}
|
||||
|
||||
private string _ColumnDescription;
|
||||
public string ColumnDescription
|
||||
{
|
||||
get { return _ColumnDescription; }
|
||||
set { _ColumnDescription = value; }
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
namespace SqlSugar
|
||||
{
|
||||
public partial class LambdaExpressionsProvider : ILambdaExpressions
|
||||
{
|
||||
public SqlSugarClient Context { get; set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,123 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
namespace SqlSugar
|
||||
{
|
||||
//局部类:拉姆达解析公用常量
|
||||
internal partial class ResolveExpress
|
||||
{
|
||||
/// <summary>
|
||||
/// 解析bool类型用到的字典
|
||||
/// </summary>
|
||||
public static List<ExpressBoolModel> ConstantBoolDictionary = new List<ExpressBoolModel>()
|
||||
{
|
||||
new ExpressBoolModel(){ Key=Guid.NewGuid(), OldValue="True", Type=PubConst.StringType},
|
||||
new ExpressBoolModel(){ Key=Guid.NewGuid(), OldValue="False",Type=PubConst.StringType},
|
||||
new ExpressBoolModel(){ Key=Guid.NewGuid(), OldValue="True",Type=PubConst.BoolType},
|
||||
new ExpressBoolModel(){ Key=Guid.NewGuid(), OldValue="False",Type=PubConst.BoolType}
|
||||
|
||||
};
|
||||
/// <summary>
|
||||
/// 字段名解析错误
|
||||
/// </summary>
|
||||
public const string FileldErrorMessage = "OrderBy、GroupBy、In、Min和Max等操作不是有效拉姆达格式 ,正确格式 it=>it.name 。";
|
||||
/// <summary>
|
||||
/// 拉姆达解析错误
|
||||
/// </summary>
|
||||
public const string ExpToSqlError= @"拉姆达解析出错,不是有效的函数,找不到合适函数你可以使用这种字符串写法.Where(""date>dateadd(@date)"",new{date=DateTime.Now}),
|
||||
支持的函数有(请复制到本地查看,数量比较多):
|
||||
db.Queryable<T>().Where(it => it.field == parValue.ObjToString());
|
||||
db.Queryable<T>().Where(it => it.field == parValue.ObjToDate());
|
||||
db.Queryable<T>().Where(it => it.field == parValue.ObjToInt())
|
||||
db.Queryable<T>().Where(it => it.field == parValue.ObjToDecimal())
|
||||
db.Queryable<T>().Where(it => it.field == parValue.ObjToMoney())
|
||||
db.Queryable<T>().Where(it => it.field == parValue.Trim())
|
||||
db.Queryable<T>().Where(it => it.field == parValue.ToString())
|
||||
db.Queryable<T>().Where(it => it.field == Convert.ToXXX(parValue))
|
||||
db.Queryable<T>().Where(it => it.field.Contains(parValue))
|
||||
db.Queryable<T>().Where(it => it.field.StartsWith(parValue))
|
||||
db.Queryable<T>().Where(it => it.field.EndsWith(parValue))
|
||||
db.Queryable<T>().Where(it => !string.IsNullOrEmpty(it.parValue))
|
||||
db.Queryable<T>().Where(it => arrayOrList.Contains(it.parValue))
|
||||
db.Queryable<T>().Where(it => it.field.Equals(it.parValue))
|
||||
db.Queryable<T>().Where(it => it.field.Length>10)
|
||||
db.Queryable<Student>().Where(c => c.field == parValue.ToLower()).ToList();
|
||||
db.Queryable<Student>().Where(c => c.field == parValue.ToUpper()).ToList();
|
||||
";
|
||||
/// <summary>
|
||||
/// 运算符错误
|
||||
/// </summary>
|
||||
public const string OperatorError = "拉姆达解析出错:不支持{0}此种运算符查找!";
|
||||
|
||||
/// <summary>
|
||||
/// 拉姆达解析唯一标识
|
||||
/// </summary>
|
||||
public static object ExpErrorUniqueKey = Guid.NewGuid();
|
||||
|
||||
/// <summary>
|
||||
/// 拉姆达函数错误
|
||||
/// </summary>
|
||||
public const string ExpMethodError = "拉姆达表达式中的函数用法不正确,正确写法 it=>it.name.{0}(参数) ,不支持的写法it=> 参数.{0}(it.name)。";
|
||||
|
||||
/// <summary>
|
||||
/// 拉姆达函数错误2
|
||||
/// </summary>
|
||||
public const string ExpMethodError2 = "拉姆达表达式中的函数用法不正确,正确写法 it=>it.name==参数.{0} ,不支持的写法it=>it.name.{0}==参数。";
|
||||
|
||||
/// <summary>
|
||||
/// 表达式不支持解析BlockExpression
|
||||
/// </summary>
|
||||
public const string ExpBlockExpression = "表达式不支持解析BlockExpression,错误信息:";
|
||||
|
||||
/// <summary>
|
||||
/// 表达式不支解析ConditionalExpression
|
||||
/// </summary>
|
||||
public const string ExpConditionalExpression = "表达式不支解析ConditionalExpression,错误信息:";
|
||||
|
||||
/// <summary>
|
||||
/// 拉姆达表达式内不支持new对象
|
||||
/// </summary>
|
||||
public const string ExpNew = "拉姆达表达式内不支持new对象,请提取变量后在赋值,错误信息:";
|
||||
|
||||
/// <summary>
|
||||
/// 不支持对象或属性
|
||||
/// </summary>
|
||||
public const string ExpNoSupportObjectOrAttr = "拉姆达解析不支持{0}对象,或该{1}的属性。";
|
||||
|
||||
/// <summary>
|
||||
/// 不支持属性扩展方法
|
||||
/// </summary>
|
||||
public const string ExpNoSupportAttExtMethod = "不支持属性扩展方法{0}。";
|
||||
}
|
||||
|
||||
|
||||
//局部类:Select表达式对象解析公用常量
|
||||
internal partial class ResolveSelect {
|
||||
|
||||
/// <summary>
|
||||
/// 解析对象不能为null
|
||||
/// </summary>
|
||||
public const string ExpSelectValueIsNull = "解析对象不能为null。";
|
||||
|
||||
/// <summary>
|
||||
///Select中不支持函数
|
||||
/// </summary>
|
||||
public const string ExpNoSupportMethod = "Select中不支持函数{0}。";
|
||||
|
||||
/// <summary>
|
||||
/// Select中不支持变量的运算
|
||||
/// </summary>
|
||||
public const string ExpNoSupportOperation = "Select中不支持变量的运算。";
|
||||
|
||||
/// <summary>
|
||||
/// 不支持外部传参
|
||||
/// </summary>
|
||||
public static string ExpNoSupportOutPars = "Select中的拉姆达表达式,不支持外部传参数,目前支持的写法: Where(\"1=1\",new {id=1}).Select(it=>{ id=\"@id\".ObjToInt()} 。";
|
||||
|
||||
/// <summary>
|
||||
/// 不支持ToString
|
||||
/// </summary>
|
||||
public static string ExpNoSupportToString = "Select中不支持ToString函数,请使用ObjectToString。";
|
||||
}
|
||||
}
|
@ -0,0 +1,305 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text.RegularExpressions;
|
||||
namespace SqlSugar
|
||||
{
|
||||
//局部类:拉姆达解析分类处理
|
||||
internal partial class ResolveExpress
|
||||
{
|
||||
private string UnaryExpression(Expression exp, ref MemberType type)
|
||||
{
|
||||
UnaryExpression ue = ((UnaryExpression)exp);
|
||||
var mex = ue.Operand;
|
||||
bool? isComparisonOperator = null;
|
||||
var isNot = ue.NodeType==ExpressionType.Not;
|
||||
if (mex.NodeType == ExpressionType.MemberAccess && isNot)
|
||||
{
|
||||
isComparisonOperator = false;
|
||||
}
|
||||
var cse = CreateSqlElements(mex, ref type, false,isComparisonOperator);
|
||||
if (type == MemberType.Value && isNot && cse.IsIn("1","0"))
|
||||
{
|
||||
cse = cse == "1" ? "0" : "1";
|
||||
|
||||
}else if (type == MemberType.None && isNot)
|
||||
{
|
||||
cse = " NOT " + cse;
|
||||
}
|
||||
return cse;
|
||||
}
|
||||
|
||||
private bool UnderNodeTypeIsConstantExpression(MemberExpression exp)
|
||||
{
|
||||
while (exp.Expression != null)
|
||||
{
|
||||
if (exp != null && exp.Expression != null)
|
||||
{
|
||||
if (exp.Expression is MemberExpression)
|
||||
{
|
||||
exp = (MemberExpression)exp.Expression;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return exp.Expression is ConstantExpression;
|
||||
}
|
||||
|
||||
private string MemberExpression(ref Expression exp, ref MemberType type, bool? isComparisonOperator)
|
||||
{
|
||||
|
||||
MemberExpression me = ((MemberExpression)exp);
|
||||
var isPro = (me.Member.Name == "Length") && me.Member.DeclaringType == PubConst.StringType;
|
||||
var proIsField = false;
|
||||
if (isPro) proIsField = me.Expression != null && !UnderNodeTypeIsConstantExpression(me);
|
||||
if (proIsField==false&&(me.Expression == null || me.Expression.NodeType != ExpressionType.Parameter))
|
||||
{
|
||||
type = MemberType.Value;
|
||||
object dynInv = null;
|
||||
|
||||
if (isPro)
|
||||
{
|
||||
exp = me.Expression;
|
||||
dynInv = CreateSqlElements(exp, ref type, true);
|
||||
}
|
||||
else {
|
||||
GetMemberValue(ref exp, me, ref dynInv);
|
||||
}
|
||||
if (isPro)return GetProMethod(me.Member.Name,dynInv.ObjToString(),false);
|
||||
if (dynInv!=null&&dynInv.GetType() == PubConst.BoolType)
|
||||
{
|
||||
dynInv = ConstantBoolDictionary.Where(it => it.Type == PubConst.BoolType).Single(it => it.OldValue.ToLower() == dynInv.ObjToString().ToLower()).NewValue;
|
||||
}
|
||||
if (dynInv == null) return null;
|
||||
else
|
||||
return dynInv.ToString();
|
||||
}
|
||||
else if (isComparisonOperator == false)
|
||||
{
|
||||
return "(" + me.Member.Name + "=1)";
|
||||
}
|
||||
else
|
||||
{
|
||||
if (Type == ResolveExpressType.Multiple)
|
||||
{
|
||||
type = MemberType.Key;
|
||||
var dbName = exp.ToString();
|
||||
if (isPro) return GetProMethod(me.Member.Name, dbName, true);
|
||||
return dbName;
|
||||
}
|
||||
//single T
|
||||
string name = me.Member.Name;
|
||||
if (isPro)
|
||||
{
|
||||
name = ((me.Expression) as MemberExpression).Member.Name;
|
||||
}
|
||||
type = MemberType.Key;
|
||||
if (isPro) return GetProMethod(me.Member.Name, name, true);
|
||||
return name;
|
||||
}
|
||||
}
|
||||
|
||||
private static void GetMemberValue(ref Expression exp, MemberExpression me, ref object dynInv)
|
||||
{
|
||||
// var dynInv = Expression.Lambda(exp).Compile().DynamicInvoke();原始写法性能极慢,下面写法性能提高了几十倍
|
||||
// var dynInv= Expression.Lambda(me.Expression as ConstantExpression).Compile().DynamicInvoke();
|
||||
SetMemberValueToDynInv(ref exp, me, ref dynInv);
|
||||
if (dynInv == ExpErrorUniqueKey)//特殊情况走原始写法
|
||||
{
|
||||
dynInv = Expression.Lambda(exp).Compile().DynamicInvoke();
|
||||
if (dynInv != null && dynInv.GetType().IsClass)
|
||||
{
|
||||
dynInv = Expression.Lambda(me).Compile().DynamicInvoke();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static string ConstantExpression(Expression exp, ref MemberType type, bool? isComparisonOperator)
|
||||
{
|
||||
type = MemberType.Value;
|
||||
ConstantExpression ce = ((ConstantExpression)exp);
|
||||
if (ce.Value == null)
|
||||
return "null";
|
||||
else if (ce.Value.ToString().IsIn("True", "False"))//是bool值
|
||||
{
|
||||
var ceType = ce.Value.GetType();
|
||||
var ceValue = ce.Value.ToString();
|
||||
if (isComparisonOperator==true)
|
||||
{
|
||||
var ceNewValue = ConstantBoolDictionary.Single(it => it.Type == ceType && it.OldValue.ToLower() == ceValue.ToLower());
|
||||
return ceNewValue.NewValue;
|
||||
}
|
||||
else
|
||||
{
|
||||
var ceNewValue = ConstantBoolDictionary.Single(it => it.Type == ceType && it.OldValue.ToLower() == ceValue.ToLower());
|
||||
return ceNewValue.Key.ToString();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return ce.Value.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
private string MethodCallExpression(Expression exp, ref MemberType type, bool isTure)
|
||||
{
|
||||
MethodCallExpression mce = (MethodCallExpression)exp;
|
||||
string methodName = mce.Method.Name;
|
||||
if (methodName == "Contains")
|
||||
{
|
||||
return Contains(methodName, mce, isTure);
|
||||
}
|
||||
else if (methodName == "StartsWith")
|
||||
{
|
||||
return StartsWith(methodName, mce, isTure);
|
||||
}
|
||||
else if (methodName == "EndsWith")
|
||||
{
|
||||
return EndWith(methodName, mce, isTure);
|
||||
}
|
||||
else if (methodName == "ToString")
|
||||
{
|
||||
type = MemberType.Value;
|
||||
return MethodToString(methodName, mce, ref type);
|
||||
}
|
||||
else if (methodName == "IsNullOrEmpty")
|
||||
{
|
||||
type = MemberType.Value;
|
||||
return IsNullOrEmpty(methodName, mce, isTure);
|
||||
}
|
||||
else if (methodName == "Equals")
|
||||
{
|
||||
return Equals(methodName, mce);
|
||||
}
|
||||
else
|
||||
{
|
||||
type = MemberType.Value;
|
||||
return ParMethodTo(methodName, mce, ref type);
|
||||
}
|
||||
}
|
||||
|
||||
private string BinaryExpression(Expression exp)
|
||||
{
|
||||
var expression = exp as BinaryExpression;
|
||||
var isComparisonOperator =
|
||||
expression.NodeType != ExpressionType.And &&
|
||||
expression.NodeType != ExpressionType.AndAlso &&
|
||||
expression.NodeType != ExpressionType.Or &&
|
||||
expression.NodeType != ExpressionType.OrElse;
|
||||
MemberType leftType = MemberType.None;
|
||||
MemberType rightType = MemberType.None;
|
||||
var leftIsDateTime = expression.Left.Type.ToString().Contains("System.DateTime");
|
||||
var rightIsDateTime = expression.Right.Type.ToString().Contains("System.DateTime");
|
||||
var left = CreateSqlElements(expression.Left, ref leftType, true, isComparisonOperator);
|
||||
var right = CreateSqlElements(expression.Right, ref rightType, true, isComparisonOperator);
|
||||
var oper = GetOperator(expression.NodeType);
|
||||
var isKeyOperValue = leftType == MemberType.Key && rightType == MemberType.Value;
|
||||
var isValueOperKey = rightType == MemberType.Key && leftType == MemberType.Value;
|
||||
#region 处理 null
|
||||
|
||||
if (isKeyOperValue && right.IsGuid() && ConstantBoolDictionary.Any(it => it.Key.ToString() == right))
|
||||
{
|
||||
right = ConstantBoolDictionary.Single(it => it.Key.ToString() == right).NewValue;
|
||||
}
|
||||
if (isValueOperKey && ConstantBoolDictionary.Any(it => it.Key.ToString() == left))
|
||||
{
|
||||
left = ConstantBoolDictionary.Single(it => it.Key.ToString() == left).NewValue;
|
||||
}
|
||||
|
||||
if (isKeyOperValue & (right == "null" || right == null) && oper.Trim() == "=")
|
||||
{
|
||||
var oldLeft = AddParas(ref left, right);
|
||||
return string.Format(" ({0} is null ) ", oldLeft);
|
||||
}
|
||||
else if (isKeyOperValue & (right == "null" || right == null) && oper.Trim() == "<>")
|
||||
{
|
||||
var oldLeft = AddParas(ref left, right);
|
||||
return string.Format(" ({0} is not null ) ", oldLeft);
|
||||
}
|
||||
else if (isValueOperKey & (left == "null" || left == null) && oper.Trim() == "=")
|
||||
{
|
||||
return string.Format(" ({0} is null ) ", right);
|
||||
}
|
||||
else if (isValueOperKey & (left == "null" || left == null) && oper.Trim() == "<>")
|
||||
{
|
||||
return string.Format(" ({0} is not null ) ", right);
|
||||
}
|
||||
#endregion
|
||||
else if (isKeyOperValue)
|
||||
{
|
||||
object parValue = null;
|
||||
if (leftIsDateTime && right != null && right.IsDate())
|
||||
{
|
||||
parValue = Convert.ToDateTime(right);
|
||||
}
|
||||
else
|
||||
{
|
||||
parValue = right;
|
||||
}
|
||||
if (left.Contains("("))
|
||||
{
|
||||
return string.Format(" ({0} {1} {2}) ",left, oper, right.ToSqlValue());
|
||||
}
|
||||
else
|
||||
{
|
||||
var oldLeft = AddParas(ref left, parValue);
|
||||
return string.Format(" ({0} {1} " + this.ParSymbol + "{2}) ",this.SqlBuilder.GetTranslationColumnName(oldLeft), oper, left);
|
||||
}
|
||||
}
|
||||
else if (isValueOperKey)
|
||||
{
|
||||
object parValue = null;
|
||||
if (rightIsDateTime && left != null && left.IsDate())
|
||||
{
|
||||
parValue = Convert.ToDateTime(left);
|
||||
}
|
||||
else
|
||||
{
|
||||
parValue = left;
|
||||
}
|
||||
if (right.Contains("("))
|
||||
{
|
||||
return string.Format(" ({0} {1} {2}) ", left.ToSqlValue(), oper, right);
|
||||
}
|
||||
else
|
||||
{
|
||||
var oldRight = AddParasReturnRight(parValue, ref right);
|
||||
return string.Format("( " + this.ParSymbol + "{0} {1} {2} )", right, oper, this.SqlBuilder.GetTranslationColumnName(oldRight));
|
||||
}
|
||||
}
|
||||
else if (leftType == MemberType.Value && rightType == MemberType.Value)
|
||||
{
|
||||
var isAndOr = oper.ObjToString().IsIn("AND","OR");
|
||||
if (isAndOr)
|
||||
{
|
||||
return string.Format("( {0} {1} {2} )", left, oper, right);
|
||||
}
|
||||
else
|
||||
{
|
||||
return string.Format("( '{0}' {1} '{2}' )", left, oper, right);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return string.Format("( {0} {1} {2} )", left, oper, right);
|
||||
}
|
||||
}
|
||||
|
||||
private string LambdaExpression(Expression exp)
|
||||
{
|
||||
LambdaExpression lambda = exp as LambdaExpression;
|
||||
var expression = lambda.Body;
|
||||
MemberType EleType = MemberType.None;
|
||||
if (expression.NodeType == ExpressionType.MemberAccess) {
|
||||
return "("+((MemberExpression)expression).Member.Name+"=1)";
|
||||
}
|
||||
return CreateSqlElements(expression, ref EleType, true);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,360 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
using System.Reflection;
|
||||
using System.Text.RegularExpressions;
|
||||
namespace SqlSugar
|
||||
{
|
||||
/// <summary>
|
||||
/// ** 描述:拉姆达解析类
|
||||
/// ** 创始时间:2015-7-20
|
||||
/// ** 修改时间:2016-9-26
|
||||
/// ** 作者:sunkaixuan
|
||||
/// ** qq:610262374
|
||||
/// ** 使用说明:使用请注名作者
|
||||
/// </summary>
|
||||
internal partial class ResolveExpress
|
||||
{
|
||||
public SqlSugarClient Context { get; set; }
|
||||
private ISqlBuilder SqlBuilder { get; set; }
|
||||
private ResolveExpress() { }
|
||||
private string ParSymbol { get; set; }
|
||||
public ResolveExpress(SqlSugarClient context)
|
||||
{
|
||||
this.Context = context;
|
||||
this.SqlBuilder = context.SqlBuilder;
|
||||
this.ParSymbol = context.SqlBuilder.SqlParameterKeyWord;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 解析拉姆达
|
||||
/// </summary>
|
||||
/// <param name="sameIndex">区分相同参数名的索引号</param>
|
||||
public ResolveExpress(int sameIndex = 1)
|
||||
{
|
||||
this.SameIndex = sameIndex;
|
||||
}
|
||||
|
||||
public string SqlWhere = null;
|
||||
public ResolveExpressType Type = ResolveExpressType.Single;
|
||||
public List<SqlParameter> Paras = new List<SqlParameter>();
|
||||
private int SameIndex = 1;
|
||||
private SqlSugarClient DB;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 解析表达式
|
||||
/// </summary>
|
||||
/// <param name="re">当前解析对象</param>
|
||||
/// <param name="exp">要解析的表达式</param>
|
||||
/// <param name="db">数据库访问对象</param>
|
||||
public void ResolveExpression(Expression exp)
|
||||
{
|
||||
DB = this.Context;
|
||||
//初始化表达式
|
||||
Init(this, exp);
|
||||
}
|
||||
|
||||
public string GetLeftString(Expression exp)
|
||||
{
|
||||
var reval = Regex.Match(exp.ToString(), @"\((.+?)\).+").Groups[1].Value;
|
||||
return reval;
|
||||
}
|
||||
public string[] GetLeftArray(Expression exp)
|
||||
{
|
||||
var reval = GetLeftString(exp).Split(',');
|
||||
return reval;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 初始化表达式
|
||||
/// </summary>
|
||||
/// <param name="re"></param>
|
||||
/// <param name="exp"></param>
|
||||
private void Init(ResolveExpress re, Expression exp)
|
||||
{
|
||||
ResolveExpress.MemberType type = ResolveExpress.MemberType.None;
|
||||
this.SqlWhere = string.Format(" {1} {0} ", re.CreateSqlElements(exp, ref type, true), Context.SqlBuilder.LambadaQueryBuilder.WhereInfos.Count == 0 ? "WHERE" : "AND");
|
||||
//还原bool值
|
||||
foreach (var item in ConstantBoolDictionary)
|
||||
{
|
||||
if (this.SqlWhere.IsValuable())
|
||||
{
|
||||
this.SqlWhere = this.SqlWhere.Replace(item.Key.ToString(), item.ConditionalValue);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 递归解析表达式路由计算
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private string CreateSqlElements(Expression exp, ref MemberType type, bool isTure, bool? isComparisonOperator = null)
|
||||
{
|
||||
//主入口
|
||||
if (exp is LambdaExpression)
|
||||
{
|
||||
return LambdaExpression(exp);
|
||||
}
|
||||
else if (exp is BinaryExpression)
|
||||
{
|
||||
return BinaryExpression(exp);
|
||||
}
|
||||
else if (exp is BlockExpression)
|
||||
{
|
||||
throw new SqlSugarException(ExpBlockExpression + exp.ToString());
|
||||
}
|
||||
else if (exp is ConditionalExpression)
|
||||
{
|
||||
throw new SqlSugarException(ExpConditionalExpression + exp.ToString());
|
||||
}
|
||||
else if (exp is MethodCallExpression)
|
||||
{
|
||||
return MethodCallExpression(exp, ref type, isTure);
|
||||
}
|
||||
else if (exp is ConstantExpression)
|
||||
{
|
||||
return ConstantExpression(exp, ref type, isComparisonOperator);
|
||||
}
|
||||
else if (exp is MemberExpression)
|
||||
{
|
||||
return MemberExpression(ref exp, ref type, isComparisonOperator);
|
||||
}
|
||||
else if (exp is UnaryExpression)
|
||||
{
|
||||
return UnaryExpression(exp, ref type);
|
||||
}
|
||||
else if (exp != null && exp.NodeType.IsIn(ExpressionType.New, ExpressionType.NewArrayBounds, ExpressionType.NewArrayInit))
|
||||
{
|
||||
throw new SqlSugarException(ExpNew + exp.ToString());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将解析值赋给dynInv
|
||||
/// </summary>
|
||||
/// <param name="exp"></param>
|
||||
/// <param name="me"></param>
|
||||
/// <param name="dynInv"></param>
|
||||
private static void SetMemberValueToDynInv(ref Expression exp, MemberExpression me, ref object dynInv)
|
||||
{
|
||||
var conExp = me.Expression as ConstantExpression;
|
||||
var fieldInfo = me.Member as System.Reflection.FieldInfo;
|
||||
if (conExp != null && fieldInfo != null)
|
||||
{
|
||||
dynInv = (fieldInfo).GetValue((me.Expression as ConstantExpression).Value);
|
||||
if (fieldInfo.FieldType.IsEnum)
|
||||
{
|
||||
dynInv = Convert.ToInt64(Enum.ToObject(fieldInfo.FieldType, dynInv));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
var memberInfos = new Stack<MemberInfo>();
|
||||
|
||||
// "descend" toward's the root object reference:
|
||||
while (exp is MemberExpression)
|
||||
{
|
||||
var memberExpr = exp as MemberExpression;
|
||||
memberInfos.Push(memberExpr.Member);
|
||||
if (memberExpr.Expression == null)
|
||||
{
|
||||
if (memberExpr.Member.MemberType == MemberTypes.Property)
|
||||
{
|
||||
PropertyInfo pro = (PropertyInfo)memberExpr.Member;
|
||||
dynInv = pro.GetValue(memberExpr.Member, null);
|
||||
if (dynInv != null && dynInv.GetType().IsClass)
|
||||
{
|
||||
var fieldName = me.Member.Name;
|
||||
var proInfo = dynInv.GetType().GetProperty(fieldName);
|
||||
if (proInfo != null)
|
||||
{
|
||||
dynInv = proInfo.GetValue(dynInv, null);
|
||||
}
|
||||
var fieInfo = dynInv.GetType().GetField(fieldName);
|
||||
if (fieInfo != null)
|
||||
{
|
||||
dynInv = fieInfo.GetValue(dynInv);
|
||||
}
|
||||
if (fieInfo == null && proInfo == null)
|
||||
{
|
||||
throw new SqlSugarException(string.Format(ExpNoSupportObjectOrAttr, dynInv.GetType().FullName, dynInv.GetType().FullName));
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
else if (memberExpr.Member.MemberType == MemberTypes.Field)
|
||||
{
|
||||
FieldInfo field = (FieldInfo)memberExpr.Member;
|
||||
dynInv = field.GetValue(memberExpr.Member);
|
||||
if (dynInv != null && dynInv.GetType().IsClass)
|
||||
{
|
||||
var fieldName = me.Member.Name;
|
||||
var proInfo = dynInv.GetType().GetProperty(fieldName);
|
||||
if (proInfo != null)
|
||||
{
|
||||
dynInv = proInfo.GetValue(dynInv, null);
|
||||
}
|
||||
var fieInfo = dynInv.GetType().GetField(fieldName);
|
||||
if (fieInfo != null)
|
||||
{
|
||||
dynInv = fieInfo.GetValue(dynInv);
|
||||
}
|
||||
if (fieInfo == null && proInfo == null)
|
||||
{
|
||||
throw new SqlSugarException(string.Format(ExpNoSupportObjectOrAttr, dynInv.GetType().FullName, dynInv.GetType().FullName));
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
if (memberExpr.Expression == null)
|
||||
{
|
||||
dynInv = ExpErrorUniqueKey;
|
||||
return;
|
||||
}
|
||||
exp = memberExpr.Expression;
|
||||
}
|
||||
|
||||
// fetch the root object reference:
|
||||
var constExpr = exp as ConstantExpression;
|
||||
if (constExpr == null)
|
||||
{
|
||||
dynInv = ExpErrorUniqueKey;
|
||||
return;
|
||||
}
|
||||
var objReference = constExpr.Value;
|
||||
|
||||
// "ascend" back whence we came from and resolve object references along the way:
|
||||
while (memberInfos.Count > 0) // or some other break condition
|
||||
{
|
||||
var mi = memberInfos.Pop();
|
||||
if (mi.MemberType == MemberTypes.Property)
|
||||
{
|
||||
var objProp = objReference.GetType().GetProperty(mi.Name);
|
||||
if (objProp == null)
|
||||
{
|
||||
dynInv = ExpErrorUniqueKey;
|
||||
return;
|
||||
}
|
||||
objReference = objProp.GetValue(objReference, null);
|
||||
}
|
||||
else if (mi.MemberType == MemberTypes.Field)
|
||||
{
|
||||
var objField = objReference.GetType().GetField(mi.Name);
|
||||
if (objField == null)
|
||||
{
|
||||
dynInv = ExpErrorUniqueKey;
|
||||
return;
|
||||
}
|
||||
objReference = objField.GetValue(objReference);
|
||||
}
|
||||
}
|
||||
dynInv = objReference;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加参数
|
||||
/// </summary>
|
||||
/// <param name="left"></param>
|
||||
/// <param name="right"></param>
|
||||
/// <returns></returns>
|
||||
private string AddParas(ref string left, object right)
|
||||
{
|
||||
string oldLeft = left;
|
||||
left = left + SameIndex;
|
||||
SameIndex++;
|
||||
if (Type != ResolveExpressType.Single)
|
||||
{
|
||||
left = left.Replace(".", "_");
|
||||
}
|
||||
if (right == null)
|
||||
{
|
||||
this.Paras.Add(new SqlParameter(this.ParSymbol + left, DBNull.Value));
|
||||
}
|
||||
else
|
||||
{
|
||||
this.Paras.Add(new SqlParameter(this.ParSymbol + left, right));
|
||||
}
|
||||
return oldLeft;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加参数并返回右边值
|
||||
/// </summary>
|
||||
/// <param name="left"></param>
|
||||
/// <param name="right"></param>
|
||||
/// <returns></returns>
|
||||
private string AddParasReturnRight(object left, ref string right)
|
||||
{
|
||||
string oldRight = right;
|
||||
right = right + SameIndex;
|
||||
SameIndex++;
|
||||
if (Type != ResolveExpressType.Single)
|
||||
{
|
||||
right = right.Replace(".", "_");
|
||||
}
|
||||
if (left == null)
|
||||
{
|
||||
this.Paras.Add(new SqlParameter(this.ParSymbol + right, DBNull.Value));
|
||||
}
|
||||
else
|
||||
{
|
||||
this.Paras.Add(new SqlParameter(this.ParSymbol + right, left));
|
||||
}
|
||||
return oldRight;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据条件生成对应的sql查询操作符
|
||||
/// </summary>
|
||||
/// <param name="expressiontype"></param>
|
||||
/// <returns></returns>
|
||||
private string GetOperator(ExpressionType expressiontype)
|
||||
{
|
||||
switch (expressiontype)
|
||||
{
|
||||
case ExpressionType.And:
|
||||
case ExpressionType.AndAlso:
|
||||
return " AND ";
|
||||
case ExpressionType.Equal:
|
||||
return " =";
|
||||
case ExpressionType.GreaterThan:
|
||||
return " >";
|
||||
case ExpressionType.GreaterThanOrEqual:
|
||||
return ">=";
|
||||
case ExpressionType.LessThan:
|
||||
return "<";
|
||||
case ExpressionType.LessThanOrEqual:
|
||||
return "<=";
|
||||
case ExpressionType.NotEqual:
|
||||
return "<>";
|
||||
case ExpressionType.Or:
|
||||
case ExpressionType.OrElse:
|
||||
return " OR ";
|
||||
case ExpressionType.Add:
|
||||
case ExpressionType.AddChecked:
|
||||
return "+";
|
||||
case ExpressionType.Subtract:
|
||||
case ExpressionType.SubtractChecked:
|
||||
return "-";
|
||||
case ExpressionType.Divide:
|
||||
return "/";
|
||||
case ExpressionType.Multiply:
|
||||
case ExpressionType.MultiplyChecked:
|
||||
return "*";
|
||||
default:
|
||||
throw new SqlSugarException(string.Format(OperatorError + expressiontype));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,274 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Linq.Expressions;
|
||||
using System.Collections;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
//局部类:解析函数
|
||||
internal partial class ResolveExpress
|
||||
{
|
||||
/// <summary>
|
||||
/// 是否相等
|
||||
/// </summary>
|
||||
/// <param name="methodName"></param>
|
||||
/// <param name="mce"></param>
|
||||
/// <returns></returns>
|
||||
private string Equals(string methodName, MethodCallExpression mce) {
|
||||
MemberType leftType = MemberType.None;
|
||||
MemberType rightType = MemberType.None;
|
||||
var left = CreateSqlElements(mce.Object, ref leftType,true);
|
||||
var right = mce.Arguments[0].NodeType.IsIn(ExpressionType.Constant, ExpressionType.MemberAccess) ? CreateSqlElements(mce.Arguments[0], ref rightType, true) : Expression.Lambda(mce.Arguments[0]).Compile().DynamicInvoke().ObjToString();
|
||||
Check.Exception(leftType == MemberType.Value, string.Format(ExpMethodError,methodName));
|
||||
var oldLeft = AddParas(ref left,right);
|
||||
return string.Format("({0} = " + this.ParSymbol + "{1})", this.SqlBuilder.GetTranslationColumnName(oldLeft), left);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 拉姆达StartsWith函数处理
|
||||
/// </summary>
|
||||
/// <param name="methodName"></param>
|
||||
/// <param name="mce"></param>
|
||||
/// <param name="isTure"></param>
|
||||
/// <returns></returns>
|
||||
private string StartsWith(string methodName, MethodCallExpression mce, bool isTure)
|
||||
{
|
||||
MemberType leftType = MemberType.None;
|
||||
MemberType rightType = MemberType.None;
|
||||
var left = CreateSqlElements(mce.Object, ref leftType,true);
|
||||
var right = mce.Arguments[0].NodeType.IsIn(ExpressionType.Constant, ExpressionType.MemberAccess) ? CreateSqlElements(mce.Arguments[0], ref rightType, true) : Expression.Lambda(mce.Arguments[0]).Compile().DynamicInvoke().ObjToString();
|
||||
Check.Exception(leftType == MemberType.Value, string.Format(ExpMethodError, methodName));
|
||||
var oldLeft = AddParas(ref left, right + '%');
|
||||
return string.Format("({0} {1} LIKE " + this.ParSymbol + "{2})", this.SqlBuilder.GetTranslationColumnName(oldLeft), null, left);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 拉姆达EndWith函数处理
|
||||
/// </summary>
|
||||
/// <param name="methodName"></param>
|
||||
/// <param name="mce"></param>
|
||||
/// <param name="isTure"></param>
|
||||
/// <returns></returns>
|
||||
private string EndWith(string methodName, MethodCallExpression mce, bool isTure)
|
||||
{
|
||||
MemberType leftType = MemberType.None;
|
||||
MemberType rightType = MemberType.None;
|
||||
var left = CreateSqlElements(mce.Object, ref leftType,true);
|
||||
var right = mce.Arguments[0].NodeType.IsIn(ExpressionType.Constant, ExpressionType.MemberAccess) ? CreateSqlElements(mce.Arguments[0], ref rightType, true) : Expression.Lambda(mce.Arguments[0]).Compile().DynamicInvoke().ObjToString();
|
||||
Check.Exception(leftType == MemberType.Value, string.Format(ExpMethodError, methodName));
|
||||
var oldLeft = AddParas(ref left, '%' + right);
|
||||
return string.Format("({0} {1} LIKE " + this.ParSymbol + "{2})", this.SqlBuilder.GetTranslationColumnName(oldLeft), null, left);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 拉姆达Contains函数处理
|
||||
/// </summary>
|
||||
/// <param name="methodName"></param>
|
||||
/// <param name="mce"></param>
|
||||
/// <param name="isTure"></param>
|
||||
/// <returns></returns>
|
||||
private string Contains(string methodName, MethodCallExpression mce, bool isTure)
|
||||
{
|
||||
MemberType leftType = MemberType.None;
|
||||
MemberType rightType = MemberType.None;
|
||||
var left = CreateSqlElements(mce.Object, ref leftType,true);
|
||||
var right = mce.Arguments[0].NodeType.IsIn(ExpressionType.Constant, ExpressionType.MemberAccess) ? CreateSqlElements(mce.Arguments[0], ref rightType, true) : Expression.Lambda(mce.Arguments[0]).Compile().DynamicInvoke().ObjToString();
|
||||
if (left.IsCollectionsList() || right.IsStringArray() || right.IsEnumerable())
|
||||
{
|
||||
object containsValue = null;
|
||||
string fieldName = "";
|
||||
if (left.IsCollectionsList())
|
||||
{
|
||||
fieldName = right;
|
||||
MemberExpression mbx = ((MemberExpression)mce.Object);
|
||||
Expression exp = mce.Object;
|
||||
SetMemberValueToDynInv(ref exp, mbx, ref containsValue);
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
MemberExpression mbx = ((MemberExpression)mce.Arguments[0]);
|
||||
Expression exp = mce.Arguments[0];
|
||||
SetMemberValueToDynInv(ref exp, mbx, ref containsValue);
|
||||
fieldName = CreateSqlElements(mce.Arguments[1], ref rightType,true);
|
||||
}
|
||||
List<string> inArray = new List<string>();
|
||||
foreach (var item in (IEnumerable)containsValue)
|
||||
{
|
||||
inArray.Add(item.ObjToString());
|
||||
}
|
||||
if (inArray.Count == 0)
|
||||
{
|
||||
return (" (1=2) ");
|
||||
}
|
||||
var inValue = inArray.ToArray().ToJoinSqlInVals();
|
||||
return string.Format("({0} IN ({1}))", this.SqlBuilder.GetTranslationColumnName(fieldName), inValue);
|
||||
}
|
||||
else if (mce.Arguments.Count == 2) { //两个值
|
||||
//object containsValue = null;
|
||||
//MemberExpression mbx = ((MemberExpression)mce.Arguments[0]);
|
||||
//Expression exp = mce.Arguments[0];
|
||||
//SetMemberValueToDynInv(ref exp, mbx, ref containsValue);
|
||||
//var fieldName = CreateSqlElements(mce.Arguments[1], ref rightType);
|
||||
//return null;
|
||||
throw new SqlSugarException("请将数组提取成变量,不能直接写在表达式中。");
|
||||
}
|
||||
else
|
||||
{
|
||||
Check.Exception(leftType == MemberType.Value, string.Format(ExpMethodError, methodName));
|
||||
var oldLeft = AddParas(ref left, '%' + right + '%');
|
||||
return string.Format("({0} {1} LIKE " + this.ParSymbol + "{2})", this.SqlBuilder.GetTranslationColumnName(oldLeft), null, left);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 非空验证
|
||||
/// </summary>
|
||||
/// <param name="methodName"></param>
|
||||
/// <param name="mce"></param>
|
||||
/// <param name="isTure"></param>
|
||||
/// <returns></returns>
|
||||
private string IsNullOrEmpty(string methodName, MethodCallExpression mce, bool isTure)
|
||||
{
|
||||
MemberType leftType = MemberType.None;
|
||||
MemberType rightType = MemberType.None;
|
||||
var isConstant = mce.Arguments.First().NodeType == ExpressionType.Constant;
|
||||
var left = CreateSqlElements(mce.Object, ref leftType,true);
|
||||
var right = mce.Arguments[0].NodeType.IsIn(ExpressionType.Constant, ExpressionType.MemberAccess) ? CreateSqlElements(mce.Arguments[0], ref rightType, true) : Expression.Lambda(mce.Arguments[0]).Compile().DynamicInvoke().ObjToString();
|
||||
if (right == "null")
|
||||
{
|
||||
right = "";
|
||||
}
|
||||
if (isConstant)
|
||||
{
|
||||
var oldLeft = AddParas(ref left, right);
|
||||
if (isTure)
|
||||
{
|
||||
return string.Format("(" + this.ParSymbol + "{0} is null OR " + this.ParSymbol + "{0}='' )", left);
|
||||
}
|
||||
else
|
||||
{
|
||||
return string.Format("(" + this.ParSymbol + "{0} is not null AND " + this.ParSymbol + "{0}<>'' )", left);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (isTure)
|
||||
{
|
||||
if (rightType == MemberType.Key)
|
||||
{
|
||||
return string.Format("({0} is null OR {0}='' )", right.ToSqlFilter());
|
||||
}
|
||||
else
|
||||
{
|
||||
return string.Format("('{0}' is null OR '{0}'='' )", right.ToSqlFilter());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (rightType == MemberType.Key)
|
||||
{
|
||||
return string.Format("({0} is not null AND {0}<>'' )", right.ToSqlFilter());
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
return string.Format("('{0}' is not null AND '{0}'<>'' )", right.ToSqlFilter());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 参数函数
|
||||
/// </summary>
|
||||
/// <param name="methodName"></param>
|
||||
/// <param name="mce"></param>
|
||||
/// <param name="type"></param>
|
||||
/// <returns></returns>
|
||||
private string ParMethodTo(string methodName, MethodCallExpression mce, ref MemberType type)
|
||||
{
|
||||
//参数函数
|
||||
MemberType rightType = MemberType.None;
|
||||
object right =null;
|
||||
if (mce.Arguments.IsValuable() && !methodName.IsIn("AddDays", "AddYears", "AddMonths"))
|
||||
{
|
||||
right = CreateSqlElements(mce.Arguments[0], ref rightType, true);
|
||||
}
|
||||
else {
|
||||
right = CreateSqlElements(mce.Object, ref rightType, true);
|
||||
}
|
||||
Check.Exception(rightType != MemberType.Value, string.Format(ExpMethodError2, methodName));
|
||||
string value = string.Empty;
|
||||
if (mce.Arguments.IsValuable())
|
||||
{
|
||||
value = right.ToString();
|
||||
}
|
||||
else
|
||||
{
|
||||
value = MethodToString(methodName, mce, ref type); ;
|
||||
}
|
||||
if (methodName.IsIn("AddDays", "AddYears", "AddMonths"))
|
||||
{
|
||||
if (value.IsValuable())
|
||||
{
|
||||
var parValue = CreateSqlElements(mce.Arguments[0], ref rightType, true).ObjToInt();
|
||||
switch (methodName)
|
||||
{
|
||||
case "AddDays": value = value.ObjToDate().AddDays(parValue).ObjToString(); break;
|
||||
case "AddYears": value = value.ObjToDate().AddYears(parValue).ObjToString(); break;
|
||||
case "AddMonths": value = value.ObjToDate().AddMonths(parValue).ObjToString(); break;
|
||||
|
||||
}
|
||||
}
|
||||
return value;
|
||||
}
|
||||
if (methodName == "ToDateTime" || methodName == "ObjToDate")
|
||||
{
|
||||
return Convert.ToDateTime(value).ToString();
|
||||
}
|
||||
else if (methodName.StartsWith("ToInt"))
|
||||
{
|
||||
return Convert.ToInt32(value).ToString();
|
||||
}
|
||||
else if (methodName.StartsWith("Trim"))
|
||||
{
|
||||
return (value.ObjToString()).Trim();
|
||||
}
|
||||
else if (methodName.StartsWith("ObjTo"))
|
||||
{
|
||||
return value;
|
||||
}
|
||||
else if (methodName == "ToLower") {
|
||||
if (value == null) return value;
|
||||
else
|
||||
return value.ToLower();
|
||||
}
|
||||
else if (methodName == "ToUpper")
|
||||
{
|
||||
if (value == null) return value;
|
||||
else
|
||||
return value.ToUpper();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new SqlSugarException("不支持当前函数:" + methodName + "\r\n" + ResolveExpress.ExpToSqlError);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 拉姆达ToString函数处理
|
||||
/// </summary>
|
||||
/// <param name="methodName"></param>
|
||||
/// <param name="mce"></param>
|
||||
/// <param name="type"></param>
|
||||
/// <returns></returns>
|
||||
private string MethodToString(string methodName, MethodCallExpression mce, ref MemberType type)
|
||||
{
|
||||
return CreateSqlElements(mce.Object, ref type,true);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,66 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
//局部类:解析用到的实体
|
||||
internal partial class ResolveExpress
|
||||
{
|
||||
/// <summary>
|
||||
/// 拉姆达成员类型
|
||||
/// </summary>
|
||||
public enum MemberType
|
||||
{
|
||||
None = 0,
|
||||
Key = 1,
|
||||
Value = 2
|
||||
}
|
||||
/// <summary>
|
||||
/// 用来处理bool类型的实体
|
||||
/// </summary>
|
||||
public class ExpressBoolModel
|
||||
{
|
||||
/// <summary>
|
||||
/// 唯一标识
|
||||
/// </summary>
|
||||
public Guid Key { get; set; }
|
||||
/// <summary>
|
||||
/// 数据类型
|
||||
/// </summary>
|
||||
public Type Type { get; set; }
|
||||
/// <summary>
|
||||
/// 原始值
|
||||
/// </summary>
|
||||
public string OldValue { get; set; }
|
||||
/// <summary>
|
||||
/// 处事后的值
|
||||
/// </summary>
|
||||
public string NewValue
|
||||
{
|
||||
get
|
||||
{
|
||||
if (Type == PubConst.BoolType)
|
||||
{
|
||||
return Convert.ToBoolean(OldValue) ? "1" : "0";
|
||||
}
|
||||
else
|
||||
{
|
||||
return OldValue.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 处理后的运算对象
|
||||
/// </summary>
|
||||
public string ConditionalValue
|
||||
{
|
||||
get
|
||||
{
|
||||
return Convert.ToBoolean(OldValue) ? "(1=1)" : "(1=2)";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
//局部类:解析属性
|
||||
internal partial class ResolveExpress
|
||||
{
|
||||
private string GetProMethod(string methodName, string value, bool isField)
|
||||
{
|
||||
switch (methodName)
|
||||
{
|
||||
case "Length":
|
||||
return ProLength(value, isField);
|
||||
default: throw new SqlSugarException(string.Format(ExpNoSupportAttExtMethod, methodName));
|
||||
}
|
||||
}
|
||||
private string ProLength(string value, bool isField)
|
||||
{
|
||||
if (isField)
|
||||
{
|
||||
return string.Format("LEN({0})", this.SqlBuilder.GetTranslationColumnName(value));
|
||||
}
|
||||
else
|
||||
{
|
||||
return string.Format("{0}", value.ObjToString().Length);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,84 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
//局部类 解析字段名
|
||||
internal partial class ResolveExpress
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取拉姆达表达式的字段值
|
||||
/// </summary>
|
||||
/// <param name="exp"></param>
|
||||
/// <param name="db">数据库访问对象</param>
|
||||
/// <returns></returns>
|
||||
public string GetExpressionRightField(Expression exp, SqlSugarClient db)
|
||||
{
|
||||
DB = db;
|
||||
string reval = "";
|
||||
LambdaExpression lambda = exp as LambdaExpression;
|
||||
var isConvet = lambda.Body.NodeType.IsIn(ExpressionType.Convert);
|
||||
var isMember = lambda.Body.NodeType.IsIn(ExpressionType.MemberAccess);
|
||||
if (!isConvet && !isMember)
|
||||
{
|
||||
throw new SqlSugarException(FileldErrorMessage);
|
||||
}
|
||||
try
|
||||
{
|
||||
if (isConvet)
|
||||
{
|
||||
var memberExpr =((UnaryExpression)lambda.Body).Operand as MemberExpression;
|
||||
reval= memberExpr.Member.Name;
|
||||
}
|
||||
else//isMember
|
||||
{
|
||||
reval= (lambda.Body as MemberExpression).Member.Name;
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
throw new SqlSugarException(FileldErrorMessage);
|
||||
}
|
||||
return reval;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取拉姆达表达式的字段值多个T模式
|
||||
/// </summary>
|
||||
/// <param name="exp"></param>
|
||||
/// <param name="db">数据库访问对象</param>
|
||||
/// <returns></returns>
|
||||
public string GetExpressionRightFieldByNT(Expression exp, SqlSugarClient db)
|
||||
{
|
||||
DB = db;
|
||||
string reval = "";
|
||||
LambdaExpression lambda = exp as LambdaExpression;
|
||||
var isConvet = lambda.Body.NodeType.IsIn(ExpressionType.Convert);
|
||||
var isMember = lambda.Body.NodeType.IsIn(ExpressionType.MemberAccess);
|
||||
if (!isConvet && !isMember)
|
||||
{
|
||||
throw new SqlSugarException(FileldErrorMessage);
|
||||
}
|
||||
try
|
||||
{
|
||||
if (isConvet)
|
||||
{
|
||||
var memberExpr = ((UnaryExpression)lambda.Body).Operand as MemberExpression;
|
||||
reval= memberExpr.ToString();
|
||||
}
|
||||
else//isMember
|
||||
{
|
||||
reval= lambda.Body.ToString();
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
throw new SqlSugarException(FileldErrorMessage);
|
||||
}
|
||||
return reval;
|
||||
}
|
||||
}
|
||||
}
|
47
SqlSugar/Abstract/QueryableProvider/QueryableAccessory.cs
Normal file
47
SqlSugar/Abstract/QueryableProvider/QueryableAccessory.cs
Normal file
@ -0,0 +1,47 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class QueryableAccessory
|
||||
{
|
||||
protected List<SqlParameter> _Pars;
|
||||
protected void AddPars(object whereObj, SqlSugarClient context)
|
||||
{
|
||||
var sqlParsArray = context.Database.GetParameters(whereObj);
|
||||
if (_Pars == null)
|
||||
_Pars = new List<SqlParameter>();
|
||||
if (sqlParsArray != null)
|
||||
_Pars.AddRange(sqlParsArray);
|
||||
}
|
||||
protected void AddPars(List<SqlParameter> pars, SqlSugarClient context)
|
||||
{
|
||||
if (_Pars == null)
|
||||
_Pars = new List<SqlParameter>();
|
||||
if (pars != null)
|
||||
_Pars.AddRange(pars);
|
||||
}
|
||||
protected void Where<T>(Expression<Func<T, bool>> expression, ResolveExpressType type, SqlSugarClient context) where T : class, new()
|
||||
{
|
||||
var sqlBuilder = context.SqlBuilder;
|
||||
var items = sqlBuilder.LambadaQueryBuilder;
|
||||
ResolveExpress resolveExpress = new ResolveExpress(context);
|
||||
items.WhereIndex = items.WhereIndex + 100;
|
||||
resolveExpress.ResolveExpression(expression);
|
||||
this.AddPars(resolveExpress.Paras, context);
|
||||
items.WhereInfos.Add(resolveExpress.SqlWhere);
|
||||
}
|
||||
|
||||
protected void Where<T>(string whereString, object whereObj, SqlSugarClient context) where T : class, new()
|
||||
{
|
||||
var SqlBuilder = context.SqlBuilder;
|
||||
var whereValue = SqlBuilder.LambadaQueryBuilder.WhereInfos;
|
||||
whereValue.Add(SqlBuilder.AppendWhereOrAnd(whereValue.Count == 0, whereString));
|
||||
this.AddPars(whereObj, context);
|
||||
}
|
||||
}
|
||||
}
|
332
SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs
Normal file
332
SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs
Normal file
@ -0,0 +1,332 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public partial class QueryableProvider<T> : QueryableAccessory, ISugarQueryable<T> where T : class, new()
|
||||
{
|
||||
public SqlSugarClient Context { get; set; }
|
||||
public IDb Db { get { return Context.Database; } }
|
||||
public IDbBind Bind { get { return this.Db.DbBind; } }
|
||||
public ISqlBuilder SqlBuilder { get { return this.Context.SqlBuilder; } }
|
||||
public List<SqlParameter> Pars
|
||||
{
|
||||
get { return PubMethod.IsNullReturnNew<List<SqlParameter>>(base._Pars); }
|
||||
set { base._Pars = value; }
|
||||
}
|
||||
public void Clear()
|
||||
{
|
||||
Pars = null;
|
||||
SqlBuilder.LambadaQueryBuilder.Clear();
|
||||
}
|
||||
|
||||
public virtual ISugarQueryable<T> Where(Expression<Func<T, bool>> expression)
|
||||
{
|
||||
base.Where<T>(expression, ResolveExpressType.Single, this.Context);
|
||||
return this;
|
||||
}
|
||||
|
||||
public ISugarQueryable<T> Where(string whereString, object whereObj = null)
|
||||
{
|
||||
base.Where<T>(whereString, whereObj, this.Context);
|
||||
return this;
|
||||
}
|
||||
|
||||
public ISugarQueryable<T> Where<T2>(string whereString, object whereObj = null) where T2 : class, new()
|
||||
{
|
||||
base.Where<T2>(whereString, whereObj, this.Context);
|
||||
return this;
|
||||
}
|
||||
|
||||
public ISugarQueryable<T> Where<T2>(Expression<Func<T, T2, bool>> expression) where T2 : class, new()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public ISugarQueryable<T> Where<T2, T3>(Expression<Func<T, T2, T3, bool>> expression) where T2 : class, new() where T3 : class, new()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public ISugarQueryable<T> Where<T2, T3, T4>(Expression<Func<T, T2, T3, T4, bool>> expression) where T2 : class, new() where T3 : class, new() where T4 : class, new()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public ISugarQueryable<T> Where<T2, T3, T4, T5>(Expression<Func<T, T2, T3, T4, T5, bool>> expression) where T2 : class, new() where T3 : class, new() where T4 : class, new() where T5 : class, new()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public ISugarQueryable<T> In(params object[] pkValues)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public T InSingle(object pkValue)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public ISugarQueryable<T> In<FieldType>(string InFieldName, params FieldType[] inValues)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public ISugarQueryable<T> In<FieldType>(Expression<Func<T, object>> expression, params FieldType[] inValues)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public ISugarQueryable<T> In<FieldType>(Expression<Func<T, object>> expression, List<FieldType> inValues)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public ISugarQueryable<T> In<FieldType>(string InFieldName, List<FieldType> inValues)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public ISugarQueryable<T> OrderBy(string orderFileds)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public ISugarQueryable<T> OrderBy(Expression<Func<T, object>> expression, OrderByType type = OrderByType.Asc)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public ISugarQueryable<T> OrderBy<T2>(Expression<Func<T, T2, object>> expression, OrderByType type = OrderByType.Asc)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public ISugarQueryable<T> GroupBy(Expression<Func<T, object>> expression)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public ISugarQueryable<T> GroupBy(string groupFileds)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public ISugarQueryable<T> Skip(int index)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public ISugarQueryable<T> Take(int num)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public T Single()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public T SingleOrDefault()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public T Single(Expression<Func<T, bool>> expression)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public T SingleOrDefault(Expression<Func<T, bool>> expression)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public T First()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public T FirstOrDefault()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public T First(Expression<Func<T, bool>> expression)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public T FirstOrDefault(Expression<Func<T, bool>> expression)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public bool Any(Expression<Func<T, bool>> expression)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public bool Any()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public ISugarQueryable<TResult> Select<T2, TResult>(Expression<Func<T, T2, TResult>> expression) where TResult : class, new()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public ISugarQueryable<TResult> Select<T2, T3, TResult>(Expression<Func<T, T2, T3, TResult>> expression) where TResult : class, new()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public ISugarQueryable<TResult> Select<T2, T3, T4, TResult>(Expression<Func<T, T2, T3, T4, TResult>> expression) where TResult : class, new()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public ISugarQueryable<TResult> Select<T2, T3, T4, T5, TResult>(Expression<Func<T, T2, T3, T4, T5, TResult>> expression) where TResult : class, new()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public ISugarQueryable<TResult> Select<TResult>(Expression<Func<T, TResult>> expression) where TResult : class, new()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public ISugarQueryable<TResult> Select<TResult>(string select) where TResult : class, new()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public ISugarQueryable<T> Select(string select)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public int Count()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public TResult Max<TResult>(string maxField)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public object Max(Expression<Func<T, object>> expression)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public TResult Min<TResult>(string minField)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public object Min(Expression<Func<T, object>> expression)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public List<T> ToList()
|
||||
{
|
||||
string sql = SqlBuilder.LambadaQueryBuilder.ToSqlString();
|
||||
using (var dataReader = this.Db.GetDataReader(sql, this.Pars.ToArray()))
|
||||
{
|
||||
var reval = this.Bind.DataReaderToList<T>(typeof(T), dataReader, SqlBuilder.LambadaQueryBuilder.SelectValue);
|
||||
this.Clear();
|
||||
return reval;
|
||||
}
|
||||
}
|
||||
public string ToJson()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public string ToJsonPage(int pageIndex, int pageSize)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public string ToJsonPage(int pageIndex, int pageSize, ref int pageCount)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public KeyValuePair<string, Dictionary<string, string>> ToSql()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public DataTable ToDataTable()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public DataTable ToDataTablePage(int pageIndex, int pageSize)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public DataTable ToDataTablePage(int pageIndex, int pageSize, ref int pageCount)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public List<T> ToPageList(int pageIndex, int pageSize)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public List<T> ToPageList(int pageIndex, int pageSize, ref int pageCount)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public ISugarQueryable<T> JoinTable<T2>(Expression<Func<T, T2, object>> expression, JoinType type = JoinType.Left)
|
||||
{
|
||||
var sqlBuilder = this.Context.SqlBuilder;
|
||||
var items = sqlBuilder.LambadaQueryBuilder;
|
||||
items.WhereIndex = items.WhereIndex + 100;
|
||||
items.ResolveType = ResolveExpressType.Multiple;
|
||||
ResolveExpress re = new ResolveExpress();
|
||||
re.Context = this.Context;
|
||||
var exLeftArray = re.GetLeftArray(expression);
|
||||
re.ResolveExpression(expression);
|
||||
JoinQueryInfo joinInfo = new JoinQueryInfo()
|
||||
{
|
||||
JoinType = type,
|
||||
JoinIndex = items.JoinIndex,
|
||||
JoinWhere = re.SqlWhere,
|
||||
PreShortName= exLeftArray.First(),
|
||||
ShortName= exLeftArray.Last(),
|
||||
TableName=sqlBuilder.GetTranslationTableName(typeof(T2).Name)
|
||||
};
|
||||
items.JoinIndex++;
|
||||
items.JoinQueryInfos.Add(joinInfo);
|
||||
base.AddPars(re.Paras,this.Context);
|
||||
return this;
|
||||
}
|
||||
|
||||
public ISugarQueryable<T> JoinTable<T2, T3>(Expression<Func<T, T2, T3, object>> expression, JoinType type = JoinType.Left)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public ISugarQueryable<T> JoinTable(string tableName, string shortName, string onWhere, object whereObj, JoinType type = JoinType.Left)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class DeleteBuilder : IDMLBuilder
|
||||
{
|
||||
public SqlSugarClient Conext
|
||||
{
|
||||
get
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
public StringBuilder Sql
|
||||
{
|
||||
get
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
public string SqlTemplate
|
||||
{
|
||||
get
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public string ToSqlString()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class InsertBuilder : IDMLBuilder
|
||||
{
|
||||
public SqlSugarClient Conext
|
||||
{
|
||||
get
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
public StringBuilder Sql
|
||||
{
|
||||
get
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
public string SqlTemplate
|
||||
{
|
||||
get
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public string ToSqlString()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,148 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
namespace SqlSugar
|
||||
{
|
||||
public abstract class LambadaQueryBuilder : IDMLBuilder
|
||||
{
|
||||
public LambadaQueryBuilder()
|
||||
{
|
||||
|
||||
}
|
||||
private List<SqlParameter> _QueryPars;
|
||||
private List<JoinQueryInfo> _JoinQueryInfos;
|
||||
private List<string> _WhereInfos;
|
||||
private string _TableNameString;
|
||||
|
||||
public StringBuilder Sql { get; set; }
|
||||
public SqlSugarClient Conext { get; set; }
|
||||
|
||||
public int? Skip { get; set; }
|
||||
public int? Take { get; set; }
|
||||
public string OrderByValue { get; set; }
|
||||
public string SelectValue { get; set; }
|
||||
public Type EntityType { get; set; }
|
||||
public string EntityName { get { return this.EntityType.Name; } }
|
||||
public string TableWithString { get; set; }
|
||||
public string GroupByValue { get; set; }
|
||||
public int WhereIndex { get; set; }
|
||||
public int JoinIndex { get; set; }
|
||||
public ResolveExpressType ResolveType { get; set; }
|
||||
|
||||
public virtual string SqlTemplate
|
||||
{
|
||||
get
|
||||
{
|
||||
return "SELECT {0} FROM {1} {2}";
|
||||
}
|
||||
}
|
||||
public virtual string JoinTemplate
|
||||
{
|
||||
get
|
||||
{
|
||||
return " {0} JOIN {1} {2} ON {3} ";
|
||||
}
|
||||
}
|
||||
public virtual string GetTableNameString
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.Conext.SqlBuilder.GetTranslationTableName(EntityType.Name);
|
||||
}
|
||||
}
|
||||
public virtual string GetSelectValueString
|
||||
{
|
||||
get
|
||||
{
|
||||
if (this.SelectValue.IsNullOrEmpty())
|
||||
{
|
||||
return string.Join(",", this.Conext.Database.DbMaintenance.GetColumnInfosByTableName(this.EntityName).Select(it => this.Conext.SqlBuilder.GetTranslationColumnName(it.ColumnName)));
|
||||
}
|
||||
else return this.SelectValue;
|
||||
}
|
||||
}
|
||||
public virtual string GetWhereValueString
|
||||
{
|
||||
get
|
||||
{
|
||||
if (this.WhereInfos == null) return null;
|
||||
else
|
||||
{
|
||||
return string.Join(" ", this.WhereInfos);
|
||||
}
|
||||
}
|
||||
}
|
||||
public virtual string GetJoinValueString
|
||||
{
|
||||
get
|
||||
{
|
||||
if (this.JoinQueryInfos.IsNullOrEmpty()) return null;
|
||||
else {
|
||||
return string.Join(" ", this.JoinQueryInfos.Select(it => this.ToJoinString(it)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public virtual string ToSqlString()
|
||||
{
|
||||
Sql = new StringBuilder();
|
||||
var tableString = GetTableNameString;
|
||||
if (this.JoinQueryInfos.IsValuable()) {
|
||||
tableString = tableString + " " + GetJoinValueString;
|
||||
}
|
||||
Sql.AppendFormat(SqlTemplate, GetSelectValueString, tableString , GetWhereValueString);
|
||||
return Sql.ToString();
|
||||
}
|
||||
public virtual string ToJoinString(JoinQueryInfo joinInfo)
|
||||
{
|
||||
return string.Format(
|
||||
this.JoinTemplate,
|
||||
joinInfo.JoinIndex == 0 ? (joinInfo.PreShortName + " " + joinInfo.JoinType.ToString()+" ") : (joinInfo.JoinType.ToString() + " JOIN "),
|
||||
joinInfo.TableName,
|
||||
joinInfo.ShortName + " " + TableWithString,
|
||||
joinInfo.JoinWhere);
|
||||
}
|
||||
public virtual List<string> WhereInfos
|
||||
{
|
||||
get
|
||||
{
|
||||
_WhereInfos = PubMethod.IsNullReturnNew(_WhereInfos);
|
||||
return _WhereInfos;
|
||||
}
|
||||
set { _WhereInfos = value; }
|
||||
}
|
||||
|
||||
public virtual List<SqlParameter> QueryPars
|
||||
{
|
||||
get
|
||||
{
|
||||
_QueryPars = PubMethod.IsNullReturnNew(_QueryPars);
|
||||
return _QueryPars;
|
||||
}
|
||||
set { _QueryPars = value; }
|
||||
}
|
||||
public virtual List<JoinQueryInfo> JoinQueryInfos
|
||||
{
|
||||
get
|
||||
{
|
||||
_JoinQueryInfos = PubMethod.IsNullReturnNew(_JoinQueryInfos);
|
||||
return _JoinQueryInfos;
|
||||
}
|
||||
set { _JoinQueryInfos = value; }
|
||||
}
|
||||
public virtual void Clear()
|
||||
{
|
||||
this.Skip = 0;
|
||||
this.Take = 0;
|
||||
this.Sql = null;
|
||||
this.WhereIndex = 0;
|
||||
this.QueryPars = null;
|
||||
this.GroupByValue = null;
|
||||
this._TableNameString = null;
|
||||
this.WhereInfos = null;
|
||||
this.JoinQueryInfos = null;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,64 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class SqlQueryBuilder:IDMLBuilder
|
||||
{
|
||||
private string _Fields { get; set; }
|
||||
public string Fields
|
||||
{
|
||||
get
|
||||
{
|
||||
if (this._Fields.IsNullOrEmpty())
|
||||
{
|
||||
this._Fields = Regex.Match(this.Sql.ToString(), @"select(.*?)from", RegexOptions.IgnoreCase).Groups[1].Value;
|
||||
if (this._Fields.IsNullOrEmpty())
|
||||
{
|
||||
this._Fields = "*";
|
||||
}
|
||||
}
|
||||
return this._Fields;
|
||||
}
|
||||
set
|
||||
{
|
||||
_Fields = value;
|
||||
}
|
||||
}
|
||||
private StringBuilder _Sql;
|
||||
public StringBuilder Sql
|
||||
{
|
||||
get {
|
||||
_Sql = PubMethod.IsNullReturnNew(_Sql);
|
||||
return _Sql;
|
||||
}
|
||||
set {
|
||||
_Sql = value;
|
||||
}
|
||||
}
|
||||
|
||||
public SqlSugarClient Conext { get; set; }
|
||||
|
||||
public string SqlTemplate
|
||||
{
|
||||
get
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
public string ToSqlString()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
this.Sql = null;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,67 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class SqlableQueryBuilder : IDMLBuilder
|
||||
{
|
||||
public SqlBuilderProvider BuilderConext
|
||||
{
|
||||
get
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
public SqlSugarClient Conext
|
||||
{
|
||||
get
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
public StringBuilder Sql
|
||||
{
|
||||
get
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
public string SqlTemplate
|
||||
{
|
||||
get
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public string ToSqlString()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,54 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class UpdateBuilder : IDMLBuilder
|
||||
{
|
||||
public SqlSugarClient Conext
|
||||
{
|
||||
get
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
public StringBuilder Sql
|
||||
{
|
||||
get
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
public string SqlTemplate
|
||||
{
|
||||
get
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public string ToSqlString()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
19
SqlSugar/Abstract/SqlBuilderProvider/SqlBuilderAccessory.cs
Normal file
19
SqlSugar/Abstract/SqlBuilderProvider/SqlBuilderAccessory.cs
Normal file
@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
namespace SqlSugar
|
||||
{
|
||||
public partial class SqlBuilderAccessory
|
||||
{
|
||||
protected DeleteBuilder _DeleteBuilder;
|
||||
|
||||
protected InsertBuilder _InsertBuilder;
|
||||
|
||||
protected LambadaQueryBuilder _LambadaQueryBuilder;
|
||||
|
||||
protected SqlQueryBuilder _SqlQueryBuilder;
|
||||
|
||||
protected UpdateBuilder _UpdateBuilder;
|
||||
}
|
||||
}
|
80
SqlSugar/Abstract/SqlBuilderProvider/SqlBuilderProvider.cs
Normal file
80
SqlSugar/Abstract/SqlBuilderProvider/SqlBuilderProvider.cs
Normal file
@ -0,0 +1,80 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
namespace SqlSugar
|
||||
{
|
||||
public abstract partial class SqlBuilderProvider : SqlBuilderAccessory, ISqlBuilder
|
||||
{
|
||||
public SqlBuilderProvider()
|
||||
{
|
||||
}
|
||||
|
||||
public CommandType CommandType { get; set; }
|
||||
public virtual string SqlParameterKeyWord { get { return "@"; } }
|
||||
public abstract string GetTranslationTableName(string name);
|
||||
public abstract string GetTranslationColumnName(string name);
|
||||
|
||||
public string AppendWhereOrAnd(bool isWhere, string sqlString)
|
||||
{
|
||||
return isWhere ? (" WHERE " + sqlString ):( " AND " + sqlString);
|
||||
}
|
||||
|
||||
public DeleteBuilder DeleteBuilder
|
||||
{
|
||||
get
|
||||
{
|
||||
base._DeleteBuilder = PubMethod.IsNullReturnNew(base._DeleteBuilder);
|
||||
// base._DeleteBuilder.Conext = this.Context;
|
||||
return base._DeleteBuilder;
|
||||
}
|
||||
set { base._DeleteBuilder = value; }
|
||||
}
|
||||
|
||||
public InsertBuilder InsertBuilder
|
||||
{
|
||||
get
|
||||
{
|
||||
base._InsertBuilder = PubMethod.IsNullReturnNew(base._InsertBuilder);
|
||||
// base._InsertBuilder.Conext = this.Context;
|
||||
return base._InsertBuilder;
|
||||
}
|
||||
set { base._InsertBuilder = value; }
|
||||
}
|
||||
|
||||
public LambadaQueryBuilder LambadaQueryBuilder
|
||||
{
|
||||
get;set;
|
||||
}
|
||||
|
||||
public SqlQueryBuilder SqlQueryBuilder
|
||||
{
|
||||
get
|
||||
{
|
||||
base._SqlQueryBuilder = PubMethod.IsNullReturnNew(base._SqlQueryBuilder);
|
||||
// base._SqlQueryBuilder.Conext = this.Context;
|
||||
return base._SqlQueryBuilder;
|
||||
}
|
||||
set { base._SqlQueryBuilder = value; }
|
||||
}
|
||||
|
||||
public UpdateBuilder UpdateBuilder
|
||||
{
|
||||
get
|
||||
{
|
||||
base._UpdateBuilder = PubMethod.IsNullReturnNew(base._UpdateBuilder);
|
||||
// base._UpdateBuilder.Conext = this.Context;
|
||||
return base._UpdateBuilder;
|
||||
}
|
||||
set { base._UpdateBuilder = value; }
|
||||
}
|
||||
|
||||
public SqlSugarClient Context
|
||||
{
|
||||
get;set;
|
||||
}
|
||||
}
|
||||
}
|
12
SqlSugar/Abstract/SqlableProvider.cs
Normal file
12
SqlSugar/Abstract/SqlableProvider.cs
Normal file
@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public partial class SqlableProvider : ISugarSqlable
|
||||
{
|
||||
public SqlSugarClient Context { get; set; }
|
||||
}
|
||||
}
|
92
SqlSugar/Common/CacheManager.cs
Normal file
92
SqlSugar/Common/CacheManager.cs
Normal file
@ -0,0 +1,92 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Collections;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
internal class CacheManager<V>
|
||||
{
|
||||
readonly System.Collections.Concurrent.ConcurrentDictionary<string, V> InstanceCache = new System.Collections.Concurrent.ConcurrentDictionary<string, V>();
|
||||
private static CacheManager<V> _instance = null;
|
||||
private static readonly object _instanceLock = new object();
|
||||
private CacheManager() { }
|
||||
|
||||
public V this[string key]
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.Get(key);
|
||||
}
|
||||
}
|
||||
|
||||
public bool ContainsKey(string key)
|
||||
{
|
||||
return this.InstanceCache.ContainsKey(key);
|
||||
}
|
||||
|
||||
public V Get(string key)
|
||||
{
|
||||
if (this.ContainsKey(key))
|
||||
return this.InstanceCache[key];
|
||||
else
|
||||
return default(V);
|
||||
}
|
||||
|
||||
public static CacheManager<V> GetInstance()
|
||||
{
|
||||
if (_instance == null)
|
||||
lock (_instanceLock)
|
||||
if (_instance == null)
|
||||
_instance = new CacheManager<V>();
|
||||
return _instance;
|
||||
}
|
||||
|
||||
public void Add(string key, V value)
|
||||
{
|
||||
this.InstanceCache.GetOrAdd(key, value);
|
||||
}
|
||||
|
||||
public void Add(string key, V value, int cacheDurationInSeconds)
|
||||
{
|
||||
Add(key, value);
|
||||
}
|
||||
|
||||
public void Remove(string key)
|
||||
{
|
||||
V val;
|
||||
this.InstanceCache.TryRemove(key, out val);
|
||||
}
|
||||
|
||||
public IEnumerable<string> GetAllKey()
|
||||
{
|
||||
return this.InstanceCache.Keys;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
internal class CacheFactory {
|
||||
public static void Action<T>(string cacheKey, Action<CacheManager<T>, string> successAction, Func<CacheManager<T>, string, T> errorAction)
|
||||
{
|
||||
var cm = CacheManager<T>.GetInstance();
|
||||
if (cm.ContainsKey(cacheKey)) successAction(cm, cacheKey);
|
||||
else
|
||||
{
|
||||
cm.Add(cacheKey, errorAction(cm, cacheKey));
|
||||
}
|
||||
}
|
||||
public static T Func<T>(string cacheKey, Func<CacheManager<T>, string, T> successAction, Func<CacheManager<T>, string, T> errorAction)
|
||||
{
|
||||
var cm = CacheManager<T>.GetInstance();
|
||||
if (cm.ContainsKey(cacheKey)) return successAction(cm, cacheKey);
|
||||
else
|
||||
{
|
||||
var reval = errorAction(cm, cacheKey);
|
||||
cm.Add(cacheKey, reval);
|
||||
return reval;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
42
SqlSugar/Common/Check.cs
Normal file
42
SqlSugar/Common/Check.cs
Normal file
@ -0,0 +1,42 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class Check
|
||||
{
|
||||
public static void ThrowNotSupportedException(string message)
|
||||
{
|
||||
message = message.IsNullOrEmpty() ? new NotSupportedException().Message : message;
|
||||
throw new SqlSugarException("SqlSugarException.NotSupportedException:" + message);
|
||||
}
|
||||
|
||||
public static void ConnectionConfig(IConnectionConfig config)
|
||||
{
|
||||
if (config == null || config.ConnectionString.IsNullOrEmpty() || config.DbType.IsNullOrEmpty())
|
||||
{
|
||||
throw new SqlSugarException("SqlSugarException.ArgumentNullException:" + ErrorMessage.ConnectionConfigIsNull);
|
||||
}
|
||||
}
|
||||
|
||||
public static void ArgumentNullException(object checkObj, string message)
|
||||
{
|
||||
if (checkObj == null)
|
||||
throw new SqlSugarException("SqlSugarException.ArgumentNullException:" + message);
|
||||
}
|
||||
|
||||
public static void Exception(bool isException, string message, params string[] args)
|
||||
{
|
||||
if (isException)
|
||||
throw new SqlSugarException(string.Format(message, args));
|
||||
}
|
||||
|
||||
internal static void Exception(bool v, object xX)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
12
SqlSugar/Common/CommonExtensions.cs
Normal file
12
SqlSugar/Common/CommonExtensions.cs
Normal file
@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public static class CommonExtensions
|
||||
{
|
||||
|
||||
}
|
||||
}
|
45
SqlSugar/Common/DbExtensions.cs
Normal file
45
SqlSugar/Common/DbExtensions.cs
Normal file
@ -0,0 +1,45 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
namespace SqlSugar
|
||||
{
|
||||
public static class DbExtensions
|
||||
{
|
||||
public static string ToJoinSqlInVals<T>(this T[] array)
|
||||
{
|
||||
if (array == null || array.Length == 0)
|
||||
{
|
||||
return ToSqlValue(string.Empty);
|
||||
}
|
||||
else
|
||||
{
|
||||
return string.Join(",", array.Where(c => c != null).Select(it => (it + "").ToSqlValue()));
|
||||
}
|
||||
}
|
||||
|
||||
public static string ToSqlValue(this string value)
|
||||
{
|
||||
return string.Format("'{0}'", value.ToSqlFilter());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///Sql Filter
|
||||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
/// <returns></returns>
|
||||
public static string ToSqlFilter(this string value)
|
||||
{
|
||||
if (!value.IsNullOrEmpty())
|
||||
{
|
||||
if (Regex.IsMatch(value, @"%\d.*%|0x.+0", RegexOptions.IgnoreCase))
|
||||
{
|
||||
throw new SqlSugarException(ErrorMessage.FilterError);
|
||||
}
|
||||
value = value.Replace("'", "''");
|
||||
}
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
78
SqlSugar/Common/ErrorMessage.cs
Normal file
78
SqlSugar/Common/ErrorMessage.cs
Normal file
@ -0,0 +1,78 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
namespace SqlSugar
|
||||
{
|
||||
internal static partial class ErrorMessage
|
||||
{
|
||||
internal static string FilterError
|
||||
{
|
||||
get
|
||||
{
|
||||
return GetThrowMessage("The query parameter does not allow the existence of special combination, for example: %+number+anyword+% Or 0x+anyword+0",
|
||||
"参数不能允许存在特殊组合,例如 :% + 数字 + 任意字符 + % 或者 0x + 任意字符 + 0");
|
||||
}
|
||||
}
|
||||
|
||||
internal static string EntityNamespaceError
|
||||
{
|
||||
get
|
||||
{
|
||||
return GetThrowMessage("ConnectionConfig.EntityNamespace cannot be null.",
|
||||
"ConnectionConfig.EntityNamespace 不能为null。");
|
||||
}
|
||||
}
|
||||
|
||||
internal static string ConnectionConfigIsNull
|
||||
{
|
||||
get
|
||||
{
|
||||
return GetThrowMessage("CurrentConnectionConfig and CurrentConnectionConfig attributes can't be null",
|
||||
"CurrentConnectionConfig和它的属性不能为null。");
|
||||
}
|
||||
}
|
||||
internal static string ObjNotExist
|
||||
{
|
||||
get
|
||||
{
|
||||
return GetThrowMessage("{0} does not exist.",
|
||||
"{0}不存在。");
|
||||
}
|
||||
}
|
||||
internal static string EntityMappingError
|
||||
{
|
||||
get
|
||||
{
|
||||
return GetThrowMessage("Entity mapping error.{0}",
|
||||
"实体与表映射出错。{0}");
|
||||
}
|
||||
}
|
||||
|
||||
public static string NotSupportedDictionary
|
||||
{
|
||||
get
|
||||
{
|
||||
return GetThrowMessage("This type of Dictionary is not supported for the time being. You can try Dictionary<string, string>, or contact the author!!",
|
||||
"暂时不支持该类型的Dictionary 你可以试试 Dictionary<string ,string>或者联系作者!!");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static string NotSupportedArray
|
||||
{
|
||||
get
|
||||
{
|
||||
return GetThrowMessage("This type of Array is not supported for the time being. You can try object[] or contact the author!!",
|
||||
"暂时不支持该类型的Array 你可以试试 object[] 或者联系作者!!");
|
||||
}
|
||||
}
|
||||
|
||||
internal static string GetThrowMessage(string enMessage, string cnMessage, params string[] args)
|
||||
{
|
||||
List<string> formatArgs = new List<string>() { enMessage, cnMessage };
|
||||
formatArgs.AddRange(args);
|
||||
return string.Format("\r\n English Message : {0}\r\n Chinese Message : {1}", formatArgs.ToArray());
|
||||
}
|
||||
}
|
||||
}
|
285
SqlSugar/Common/IsWhatExtensions.cs
Normal file
285
SqlSugar/Common/IsWhatExtensions.cs
Normal file
@ -0,0 +1,285 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
namespace SqlSugar
|
||||
{
|
||||
internal static class IsWhatExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// 值在的范围?
|
||||
/// </summary>
|
||||
/// <param name="thisValue"></param>
|
||||
/// <param name="begin">大于等于begin</param>
|
||||
/// <param name="end">小于等于end</param>
|
||||
/// <returns></returns>
|
||||
public static bool IsInRange(this int thisValue, int begin, int end)
|
||||
{
|
||||
return thisValue >= begin && thisValue <= end;
|
||||
}
|
||||
/// <summary>
|
||||
/// 值在的范围?
|
||||
/// </summary>
|
||||
/// <param name="thisValue"></param>
|
||||
/// <param name="begin">大于等于begin</param>
|
||||
/// <param name="end">小于等于end</param>
|
||||
/// <returns></returns>
|
||||
public static bool IsInRange(this DateTime thisValue, DateTime begin, DateTime end)
|
||||
{
|
||||
return thisValue >= begin && thisValue <= end;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 在里面吗?
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="thisValue"></param>
|
||||
/// <param name="values"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsIn<T>(this T thisValue, params T[] values)
|
||||
{
|
||||
return values.Contains(thisValue);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 在里面吗?
|
||||
/// </summary>
|
||||
/// <param name="thisValue"></param>
|
||||
/// <param name="inValues"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsContainsIn(this string thisValue, params string[] inValues)
|
||||
{
|
||||
return inValues.Any(it => thisValue.Contains(it));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是null或""?
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static bool IsNullOrEmpty(this object thisValue)
|
||||
{
|
||||
if (thisValue == null || thisValue == DBNull.Value) return true;
|
||||
return thisValue.ToString() == "";
|
||||
}
|
||||
/// <summary>
|
||||
/// 是null或""?
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static bool IsNullOrEmpty(this Guid? thisValue)
|
||||
{
|
||||
if (thisValue == null) return true;
|
||||
return thisValue == Guid.Empty;
|
||||
}
|
||||
/// <summary>
|
||||
/// 是null或""?
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static bool IsNullOrEmpty(this Guid thisValue)
|
||||
{
|
||||
if (thisValue == null) return true;
|
||||
return thisValue == Guid.Empty;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是null或""?
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static bool IsNullOrEmpty(this IEnumerable<object> thisValue)
|
||||
{
|
||||
if (thisValue == null || thisValue.Count() == 0) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 有值?(与IsNullOrEmpty相反)
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static bool IsValuable(this object thisValue)
|
||||
{
|
||||
if (thisValue == null || thisValue == DBNull.Value) return false;
|
||||
return thisValue.ToString() != "";
|
||||
}
|
||||
/// <summary>
|
||||
/// 有值?(与IsNullOrEmpty相反)
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static bool IsValuable(this IEnumerable<object> thisValue)
|
||||
{
|
||||
if (thisValue == null || thisValue.Count() == 0) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是零?
|
||||
/// </summary>
|
||||
/// <param name="thisValue"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsZero(this object thisValue)
|
||||
{
|
||||
return (thisValue == null || thisValue.ToString() == "0");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是INT?
|
||||
/// </summary>
|
||||
/// <param name="thisValue"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsInt(this object thisValue)
|
||||
{
|
||||
if (thisValue == null) return false;
|
||||
return Regex.IsMatch(thisValue.ToString(), @"^\d+$");
|
||||
}
|
||||
/// <summary>
|
||||
/// 不是INT?
|
||||
/// </summary>
|
||||
/// <param name="thisValue"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsNoInt(this object thisValue)
|
||||
{
|
||||
if (thisValue == null) return true;
|
||||
return !Regex.IsMatch(thisValue.ToString(), @"^\d+$");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是金钱?
|
||||
/// </summary>
|
||||
/// <param name="thisValue"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsMoney(this object thisValue)
|
||||
{
|
||||
if (thisValue == null) return false;
|
||||
double outValue = 0;
|
||||
return double.TryParse(thisValue.ToString(), out outValue);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是GUID?
|
||||
/// </summary>
|
||||
/// <param name="thisValue"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsGuid(this object thisValue)
|
||||
{
|
||||
if (thisValue == null) return false;
|
||||
Guid outValue = Guid.Empty;
|
||||
return Guid.TryParse(thisValue.ToString(), out outValue);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是时间?
|
||||
/// </summary>
|
||||
/// <param name="thisValue"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsDate(this object thisValue)
|
||||
{
|
||||
if (thisValue == null) return false;
|
||||
DateTime outValue = DateTime.MinValue;
|
||||
return DateTime.TryParse(thisValue.ToString(), out outValue);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是邮箱?
|
||||
/// </summary>
|
||||
/// <param name="thisValue"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsEamil(this object thisValue)
|
||||
{
|
||||
if (thisValue == null) return false;
|
||||
return Regex.IsMatch(thisValue.ToString(), @"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是手机?
|
||||
/// </summary>
|
||||
/// <param name="thisValue"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsMobile(this object thisValue)
|
||||
{
|
||||
if (thisValue == null) return false;
|
||||
return Regex.IsMatch(thisValue.ToString(), @"^\d{11}$");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是座机?
|
||||
/// </summary>
|
||||
public static bool IsTelephone(this object thisValue)
|
||||
{
|
||||
if (thisValue == null) return false;
|
||||
return System.Text.RegularExpressions.Regex.IsMatch(thisValue.ToString(), @"^(\(\d{3,4}\)|\d{3,4}-|\s)?\d{8}$");
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是身份证?
|
||||
/// </summary>
|
||||
/// <param name="thisValue"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsIDcard(this object thisValue)
|
||||
{
|
||||
if (thisValue == null) return false;
|
||||
return System.Text.RegularExpressions.Regex.IsMatch(thisValue.ToString(), @"^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是传真?
|
||||
/// </summary>
|
||||
/// <param name="thisValue"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsFax(this object thisValue)
|
||||
{
|
||||
if (thisValue == null) return false;
|
||||
return System.Text.RegularExpressions.Regex.IsMatch(thisValue.ToString(), @"^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是适合正则匹配?
|
||||
/// </summary>
|
||||
/// <param name="thisValue"></param>
|
||||
/// <param name="pattern"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsMatch(this object thisValue, string pattern)
|
||||
{
|
||||
if (thisValue == null) return false;
|
||||
Regex reg = new Regex(pattern);
|
||||
return reg.IsMatch(thisValue.ToString());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否是动态类型
|
||||
/// </summary>
|
||||
/// <param name="type"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsAnonymousType(this Type type)
|
||||
{
|
||||
string typeName = type.Name;
|
||||
return typeName.Contains("<>") && typeName.Contains("__") && typeName.Contains("AnonymousType");
|
||||
}
|
||||
/// <summary>
|
||||
/// 是List类型
|
||||
/// </summary>
|
||||
/// <param name="thisValue"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsCollectionsList(this string thisValue)
|
||||
{
|
||||
return (thisValue + "").StartsWith("System.Collections.Generic.List");
|
||||
}
|
||||
/// <summary>
|
||||
/// 是string[]类型
|
||||
/// </summary>
|
||||
/// <param name="thisValue"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsStringArray(this string thisValue)
|
||||
{
|
||||
return (thisValue + "").IsMatch(@"System\.[a-z,A-Z,0-9]+?\[\]");
|
||||
}
|
||||
/// <summary>
|
||||
/// 是Enumerable
|
||||
/// </summary>
|
||||
/// <param name="thisValue"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsEnumerable(this string thisValue)
|
||||
{
|
||||
return (thisValue + "").StartsWith("System.Linq.Enumerable");
|
||||
}
|
||||
}
|
||||
}
|
28
SqlSugar/Common/PubConst.cs
Normal file
28
SqlSugar/Common/PubConst.cs
Normal file
@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
namespace SqlSugar
|
||||
{
|
||||
internal static class PubConst
|
||||
{
|
||||
internal const string AssemblyName = "SqlSugar";
|
||||
internal static Type StringType = typeof(string);
|
||||
internal static Type IntType = typeof(int);
|
||||
internal static Type DecType = typeof(decimal);
|
||||
internal static Type GuidType = typeof(Guid);
|
||||
internal static Type DateType = typeof(DateTime);
|
||||
internal static Type ByteType = typeof(Byte);
|
||||
internal static Type BoolType = typeof(bool);
|
||||
internal static Type ObjType = typeof(object);
|
||||
internal static Type Dob = typeof(double);
|
||||
internal static Type DicSS = typeof(KeyValuePair<string, string>);
|
||||
internal static Type DicSi = typeof(KeyValuePair<string, int>);
|
||||
internal static Type Dicii = typeof(KeyValuePair<int, int>);
|
||||
internal static Type DicOO = typeof(KeyValuePair<object, object>);
|
||||
internal static Type DicSo = typeof(KeyValuePair<string, object>);
|
||||
internal static Type DicIS = typeof(KeyValuePair<int, string>);
|
||||
internal static Type DicArraySS = typeof(Dictionary<string, string>);
|
||||
internal static Type DicArraySO = typeof(Dictionary<string, object>);
|
||||
}
|
||||
}
|
112
SqlSugar/Common/PubConvert.cs
Normal file
112
SqlSugar/Common/PubConvert.cs
Normal file
@ -0,0 +1,112 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
namespace SqlSugar
|
||||
{
|
||||
public static class PubConvert
|
||||
{
|
||||
public static int ObjToInt(this object thisValue)
|
||||
{
|
||||
int reval = 0;
|
||||
if (thisValue == null) return 0;
|
||||
if (thisValue != null && thisValue != DBNull.Value && int.TryParse(thisValue.ToString(), out reval))
|
||||
{
|
||||
return reval;
|
||||
}
|
||||
return reval;
|
||||
}
|
||||
|
||||
public static int ObjToInt(this object thisValue, int errorValue)
|
||||
{
|
||||
int reval = 0;
|
||||
if (thisValue != null &&thisValue != DBNull.Value&& int.TryParse(thisValue.ToString(), out reval))
|
||||
{
|
||||
return reval;
|
||||
}
|
||||
return errorValue;
|
||||
}
|
||||
|
||||
public static double ObjToMoney(this object thisValue)
|
||||
{
|
||||
double reval = 0;
|
||||
if (thisValue != null &&thisValue != DBNull.Value&& double.TryParse(thisValue.ToString(), out reval))
|
||||
{
|
||||
return reval;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static double ObjToMoney(this object thisValue, double errorValue)
|
||||
{
|
||||
double reval = 0;
|
||||
if (thisValue != null &&thisValue != DBNull.Value&& double.TryParse(thisValue.ToString(), out reval))
|
||||
{
|
||||
return reval;
|
||||
}
|
||||
return errorValue;
|
||||
}
|
||||
|
||||
public static string ObjToString(this object thisValue)
|
||||
{
|
||||
if (thisValue != null) return thisValue.ToString().Trim();
|
||||
return "";
|
||||
}
|
||||
|
||||
public static string ObjToString(this object thisValue, string errorValue)
|
||||
{
|
||||
if (thisValue != null) return thisValue.ToString().Trim();
|
||||
return errorValue;
|
||||
}
|
||||
|
||||
public static Decimal ObjToDecimal(this object thisValue)
|
||||
{
|
||||
Decimal reval = 0;
|
||||
if (thisValue != null &&thisValue != DBNull.Value&& decimal.TryParse(thisValue.ToString(), out reval))
|
||||
{
|
||||
return reval;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static Decimal ObjToDecimal(this object thisValue, decimal errorValue)
|
||||
{
|
||||
Decimal reval = 0;
|
||||
if (thisValue != null &&thisValue != DBNull.Value&& decimal.TryParse(thisValue.ToString(), out reval))
|
||||
{
|
||||
return reval;
|
||||
}
|
||||
return errorValue;
|
||||
}
|
||||
|
||||
public static DateTime ObjToDate(this object thisValue)
|
||||
{
|
||||
DateTime reval = DateTime.MinValue;
|
||||
if (thisValue != null &&thisValue != DBNull.Value&& DateTime.TryParse(thisValue.ToString(), out reval))
|
||||
{
|
||||
return reval;
|
||||
}
|
||||
return reval;
|
||||
}
|
||||
|
||||
public static DateTime ObjToDate(this object thisValue, DateTime errorValue)
|
||||
{
|
||||
DateTime reval = DateTime.MinValue;
|
||||
if (thisValue != null &&thisValue != DBNull.Value&& DateTime.TryParse(thisValue.ToString(), out reval))
|
||||
{
|
||||
return reval;
|
||||
}
|
||||
return errorValue;
|
||||
}
|
||||
|
||||
public static bool ObjToBool(this object thisValue)
|
||||
{
|
||||
bool reval = false;
|
||||
if (thisValue != null && thisValue != DBNull.Value && bool.TryParse(thisValue.ToString(), out reval))
|
||||
{
|
||||
return reval;
|
||||
}
|
||||
return reval;
|
||||
}
|
||||
}
|
||||
}
|
39
SqlSugar/Common/PubMethod.cs
Normal file
39
SqlSugar/Common/PubMethod.cs
Normal file
@ -0,0 +1,39 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class PubMethod
|
||||
{
|
||||
internal static Type GetUnderType(PropertyInfo propertyInfo, ref bool isNullable)
|
||||
{
|
||||
Type unType = Nullable.GetUnderlyingType(propertyInfo.PropertyType);
|
||||
isNullable = unType != null;
|
||||
unType = unType ?? propertyInfo.PropertyType;
|
||||
return unType;
|
||||
}
|
||||
|
||||
internal static T IsNullReturnNew<T>(T returnObj) where T : new()
|
||||
{
|
||||
if (returnObj.IsNullOrEmpty())
|
||||
{
|
||||
returnObj = new T();
|
||||
}
|
||||
return returnObj;
|
||||
}
|
||||
|
||||
internal static T ChangeType<T>(T obj,Type type)
|
||||
{
|
||||
return (T)Convert.ChangeType(obj, type);
|
||||
}
|
||||
|
||||
internal static T ChangeType<T>(T obj)
|
||||
{
|
||||
return (T)Convert.ChangeType(obj, typeof(T));
|
||||
}
|
||||
}
|
||||
}
|
58
SqlSugar/Common/SqlException.cs
Normal file
58
SqlSugar/Common/SqlException.cs
Normal file
@ -0,0 +1,58 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Newtonsoft.Json;
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class SqlSugarException : Exception
|
||||
{
|
||||
public SqlSugarException(string message)
|
||||
: base(message){}
|
||||
|
||||
public SqlSugarException(string message, string sql)
|
||||
: base(GetMessage(message, sql)) {}
|
||||
|
||||
public SqlSugarException(string message, string sql, object pars)
|
||||
: base(GetMessage(message, sql, pars)){}
|
||||
|
||||
public SqlSugarException(string message, object pars)
|
||||
: base(GetMessage(message, pars)){}
|
||||
|
||||
private static string GetMessage(string message, object pars)
|
||||
{
|
||||
var parsStr = string.Empty; ;
|
||||
if (pars != null)
|
||||
{
|
||||
parsStr = JsonConvert.SerializeObject(pars);
|
||||
}
|
||||
var reval = GetLineMessage("message", message) + GetLineMessage("function", parsStr);
|
||||
return reval;
|
||||
|
||||
}
|
||||
|
||||
private static string GetMessage(string message, string sql, object pars)
|
||||
{
|
||||
if (pars == null)
|
||||
{
|
||||
return GetMessage(message, sql);
|
||||
}
|
||||
else
|
||||
{
|
||||
var reval = GetLineMessage("message ", message) + GetLineMessage("ORM Sql", sql) + GetLineMessage("函数参数 ", JsonConvert.SerializeObject(pars));
|
||||
return reval;
|
||||
}
|
||||
}
|
||||
|
||||
private static string GetMessage(string message, string sql)
|
||||
{
|
||||
var reval = GetLineMessage("message ", message) + GetLineMessage("ORM Sql", sql);
|
||||
return reval;
|
||||
}
|
||||
|
||||
private static string GetLineMessage(string key, string value)
|
||||
{
|
||||
return string.Format("{0} : 【{1}】\r\n", key, value);
|
||||
}
|
||||
}
|
||||
}
|
11
SqlSugar/Databases/SqlServer/CodeFirst/SqlServerCodeFirst.cs
Normal file
11
SqlSugar/Databases/SqlServer/CodeFirst/SqlServerCodeFirst.cs
Normal file
@ -0,0 +1,11 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class SqlServerCodeFirst:CodeFirstProvider
|
||||
{
|
||||
}
|
||||
}
|
99
SqlSugar/Databases/SqlServer/Db/DbBind/SqlServerDbBind.cs
Normal file
99
SqlSugar/Databases/SqlServer/Db/DbBind/SqlServerDbBind.cs
Normal file
@ -0,0 +1,99 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class SqlServerDbBind : DbBindProvider
|
||||
{
|
||||
public override string ChangeDBTypeToCSharpType(string typeName)
|
||||
{
|
||||
string reval = string.Empty;
|
||||
switch (typeName.ToLower())
|
||||
{
|
||||
case "int":
|
||||
reval = "int";
|
||||
break;
|
||||
case "text":
|
||||
reval = "string";
|
||||
break;
|
||||
case "bigint":
|
||||
reval = "long";
|
||||
break;
|
||||
case "binary":
|
||||
reval = "object";
|
||||
break;
|
||||
case "bit":
|
||||
reval = "bool";
|
||||
break;
|
||||
case "char":
|
||||
reval = "string";
|
||||
break;
|
||||
case "datetime":
|
||||
case "date":
|
||||
case "datetime2":
|
||||
reval = "dateTime";
|
||||
break;
|
||||
case "single":
|
||||
case "decimal":
|
||||
reval = "decimal";
|
||||
break;
|
||||
case "float":
|
||||
reval = "double";
|
||||
break;
|
||||
case "image":
|
||||
reval = "byte[]";
|
||||
break;
|
||||
case "money":
|
||||
reval = "decimal";
|
||||
break;
|
||||
case "nchar":
|
||||
reval = "string";
|
||||
break;
|
||||
case "ntext":
|
||||
reval = "string";
|
||||
break;
|
||||
case "numeric":
|
||||
reval = "decimal";
|
||||
break;
|
||||
case "nvarchar":
|
||||
reval = "string";
|
||||
break;
|
||||
case "real":
|
||||
reval = "float";
|
||||
break;
|
||||
case "smalldatetime":
|
||||
reval = "dateTime";
|
||||
break;
|
||||
case "smallint":
|
||||
reval = "short";
|
||||
break;
|
||||
case "smallmoney":
|
||||
reval = "decimal";
|
||||
break;
|
||||
case "timestamp":
|
||||
reval = "dateTime";
|
||||
break;
|
||||
case "tinyint":
|
||||
reval = "byte";
|
||||
break;
|
||||
case "uniqueidentifier":
|
||||
reval = "guid";
|
||||
break;
|
||||
case "varbinary":
|
||||
reval = "byte[]";
|
||||
break;
|
||||
case "varchar":
|
||||
reval = "string";
|
||||
break;
|
||||
case "Variant":
|
||||
reval = "object";
|
||||
break;
|
||||
default:
|
||||
reval = "other";
|
||||
break;
|
||||
}
|
||||
return reval;
|
||||
}
|
||||
}
|
||||
}
|
11
SqlSugar/Databases/SqlServer/Db/DbFirst/SqlServerDbFirst.cs
Normal file
11
SqlSugar/Databases/SqlServer/Db/DbFirst/SqlServerDbFirst.cs
Normal file
@ -0,0 +1,11 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class SqlServerDbFirst:DbFirstProvider
|
||||
{
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class SqlServerBuilder : SqlBuilderProvider
|
||||
{
|
||||
public override string GetTranslationTableName(string name)
|
||||
{
|
||||
Check.ArgumentNullException(name, string.Format(ErrorMessage.ObjNotExist, "Table Name Or Column Name"));
|
||||
var context = this.Context;
|
||||
var mappingInfo = context
|
||||
.MappingTables
|
||||
.FirstOrDefault(it => it.EntityName.Equals(name, StringComparison.CurrentCultureIgnoreCase));
|
||||
return "[" + (mappingInfo == null ? name : mappingInfo.DbTableName) + "]";
|
||||
}
|
||||
public override string GetTranslationColumnName(string name)
|
||||
{
|
||||
Check.ArgumentNullException(name, string.Format(ErrorMessage.ObjNotExist, "Table Name Or Column Name"));
|
||||
var context = this.Context;
|
||||
var mappingInfo = context
|
||||
.MappingColumns
|
||||
.FirstOrDefault(it => it.EntityPropertyName.Equals(name, StringComparison.CurrentCultureIgnoreCase));
|
||||
return "[" + (mappingInfo == null ? name : mappingInfo.DbColumnName) + "]";
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,111 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class SqlServerDbMaintenance : DbMaintenanceProvider
|
||||
{
|
||||
protected override string AddColumnToTableSql
|
||||
{
|
||||
get
|
||||
{
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
protected override string BackupDataBaseSql
|
||||
{
|
||||
get
|
||||
{
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
protected override string CreateTableSql
|
||||
{
|
||||
get
|
||||
{
|
||||
return @"CREATE TABLE {0}
|
||||
{1}";
|
||||
}
|
||||
}
|
||||
|
||||
protected override string GetColumnInfosByTableNameSql
|
||||
{
|
||||
get
|
||||
{
|
||||
string sql = @"SELECT Sysobjects.name AS TableName,
|
||||
syscolumns.Id AS TableId,
|
||||
syscolumns.name AS ColumnName,
|
||||
systypes.name AS DataType,
|
||||
syscolumns.length AS [Length],
|
||||
sys.extended_properties.[value] AS [ColumnDescription],
|
||||
syscomments.text AS DefaultValue,
|
||||
syscolumns.isnullable AS IsNullable,
|
||||
columnproperty(syscolumns.id,syscolumns.name,'IsIdentity')as IsIdentity,
|
||||
(CASE
|
||||
WHEN EXISTS
|
||||
(SELECT 1
|
||||
FROM sysobjects
|
||||
WHERE xtype= 'pk'
|
||||
AND name IN
|
||||
(SELECT name
|
||||
FROM sysindexes
|
||||
WHERE indid IN
|
||||
(SELECT indid
|
||||
FROM sysindexkeys
|
||||
WHERE id = syscolumns.id
|
||||
AND colid=syscolumns.colid ))) THEN 1
|
||||
ELSE 0
|
||||
END) AS IsPrimaryKey
|
||||
FROM syscolumns
|
||||
INNER JOIN systypes ON syscolumns.xtype = systypes.xtype
|
||||
LEFT JOIN sysobjects ON syscolumns.id = sysobjects.id
|
||||
LEFT OUTER JOIN sys.extended_properties ON (sys.extended_properties.minor_id = syscolumns.colid
|
||||
AND sys.extended_properties.major_id = syscolumns.id)
|
||||
LEFT OUTER JOIN syscomments ON syscolumns.cdefault = syscomments.id
|
||||
WHERE syscolumns.id IN
|
||||
(SELECT id
|
||||
FROM sysobjects
|
||||
WHERE xtype IN('u',
|
||||
'v') )
|
||||
AND (systypes.name <> 'sysname')
|
||||
AND sysobjects.name='student'
|
||||
AND systypes.name<>'geometry'
|
||||
AND systypes.name<>'geography'
|
||||
ORDER BY syscolumns.colid";
|
||||
return sql;
|
||||
}
|
||||
}
|
||||
|
||||
protected override string GetTableInfoListSql
|
||||
{
|
||||
get
|
||||
{
|
||||
return @"SELECT Name
|
||||
FROM sysobjects
|
||||
WHERE xtype IN('U')";
|
||||
}
|
||||
}
|
||||
|
||||
protected override string GetViewInfoListSql
|
||||
{
|
||||
get
|
||||
{
|
||||
return @"SELECT Name
|
||||
FROM sysobjects
|
||||
WHERE xtype IN('V')";
|
||||
}
|
||||
}
|
||||
|
||||
protected override string TruncateTableSql
|
||||
{
|
||||
get
|
||||
{
|
||||
return "TRUNCATE TABLE {0}";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class SqlServerLambadaQueryBuilder: LambadaQueryBuilder
|
||||
{
|
||||
}
|
||||
}
|
81
SqlSugar/Databases/SqlServer/Db/SqlServerDb.cs
Normal file
81
SqlSugar/Databases/SqlServer/Db/SqlServerDb.cs
Normal file
@ -0,0 +1,81 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class SqlServerDb : DbProvider
|
||||
{
|
||||
public SqlServerDb() {}
|
||||
public override IDbConnection Connection
|
||||
{
|
||||
get
|
||||
{
|
||||
if (base._DbConnection == null)
|
||||
{
|
||||
base._DbConnection = new SqlConnection(base.Context.CurrentConnectionConfig.ConnectionString);
|
||||
}
|
||||
return base._DbConnection;
|
||||
}
|
||||
set
|
||||
{
|
||||
base._DbConnection = value;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Only SqlServer
|
||||
/// </summary>
|
||||
/// <param name="transactionName"></param>
|
||||
public override void BeginTran(string transactionName)
|
||||
{
|
||||
((SqlConnection)this.Connection).BeginTransaction(transactionName);
|
||||
}
|
||||
/// <summary>
|
||||
/// Only SqlServer
|
||||
/// </summary>
|
||||
/// <param name="iso"></param>
|
||||
/// <param name="transactionName"></param>
|
||||
public override void BeginTran(IsolationLevel iso, string transactionName)
|
||||
{
|
||||
((SqlConnection)this.Connection).BeginTransaction(iso, transactionName);
|
||||
}
|
||||
public override IDataAdapter GetAdapter()
|
||||
{
|
||||
return new SqlDataAdapter();
|
||||
}
|
||||
public override IDbCommand GetCommand(string sql, SqlParameter[] pars)
|
||||
{
|
||||
SqlCommand sqlCommand = new SqlCommand(sql, (SqlConnection)this.Connection);
|
||||
sqlCommand.CommandType = this.CommandType;
|
||||
sqlCommand.CommandTimeout = this.CommandTimeOut;
|
||||
if (this.Transaction != null)
|
||||
{
|
||||
sqlCommand.Transaction = (SqlTransaction)this.Transaction;
|
||||
}
|
||||
if (pars != null)
|
||||
{
|
||||
IDbDataParameter[] ipars= ToIDbDataParameter(pars);
|
||||
sqlCommand.Parameters.AddRange((SqlParameter[])ipars);
|
||||
}
|
||||
CheckConnection();
|
||||
return sqlCommand;
|
||||
}
|
||||
public override void SetCommandToAdapter(IDataAdapter dataAdapter, IDbCommand command)
|
||||
{
|
||||
((SqlDataAdapter)dataAdapter).SelectCommand = (SqlCommand)command;
|
||||
}
|
||||
/// <summary>
|
||||
/// if mysql return MySqlParameter[] pars
|
||||
/// if sqlerver return SqlParameter[] pars ...
|
||||
/// </summary>
|
||||
/// <param name="pars"></param>
|
||||
/// <returns></returns>
|
||||
public override IDbDataParameter[] ToIDbDataParameter(params SqlParameter[] pars)
|
||||
{
|
||||
return pars;
|
||||
}
|
||||
}
|
||||
}
|
12
SqlSugar/Databases/SqlServer/DbType.cs
Normal file
12
SqlSugar/Databases/SqlServer/DbType.cs
Normal file
@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public partial class DbType
|
||||
{
|
||||
public const string SqlServer = "SqlServer";
|
||||
}
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class SqlServerLambdaExpressions : LambdaExpressionsProvider
|
||||
{
|
||||
|
||||
}
|
||||
}
|
14
SqlSugar/Databases/SqlServer/Queryable/SqlServerQueryable.cs
Normal file
14
SqlSugar/Databases/SqlServer/Queryable/SqlServerQueryable.cs
Normal file
@ -0,0 +1,14 @@
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class SqlServerQueryable<T>:QueryableProvider<T> where T:class,new()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
11
SqlSugar/Databases/SqlServer/Sqlable/SqlServerSqlable.cs
Normal file
11
SqlSugar/Databases/SqlServer/Sqlable/SqlServerSqlable.cs
Normal file
@ -0,0 +1,11 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class SqlServerSqlable:SqlableProvider
|
||||
{
|
||||
}
|
||||
}
|
29
SqlSugar/Entities/ConnectionConfig.cs
Normal file
29
SqlSugar/Entities/ConnectionConfig.cs
Normal file
@ -0,0 +1,29 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
namespace SqlSugar
|
||||
{
|
||||
public interface IConnectionConfig
|
||||
{
|
||||
string DbType { get; set; }
|
||||
string ConnectionString { get; set; }
|
||||
bool IsAutoCloseConnection { get; set; }
|
||||
}
|
||||
|
||||
public class SystemTablesConfig : IConnectionConfig
|
||||
{
|
||||
public string DbType { get; set; }
|
||||
public string ConnectionString { get; set; }
|
||||
public bool IsAutoCloseConnection { get; set; }
|
||||
}
|
||||
|
||||
public class AttrbuitesCofnig : IConnectionConfig
|
||||
{
|
||||
public string EntityNamespace { get; set; }
|
||||
public string DbType { get; set; }
|
||||
public string ConnectionString { get; set; }
|
||||
public bool IsAutoCloseConnection { get; set; }
|
||||
}
|
||||
}
|
20
SqlSugar/Entities/DbColumnInfo.cs
Normal file
20
SqlSugar/Entities/DbColumnInfo.cs
Normal file
@ -0,0 +1,20 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class DbColumnInfo
|
||||
{
|
||||
public string TableName { get; set; }
|
||||
public int TableId { get; set; }
|
||||
public string ColumnName { get; set; }
|
||||
public string DataType { get; set; }
|
||||
public int Length { get; set; }
|
||||
public string ColumnDescription { get; set; }
|
||||
public string DefaultValue { get; set; }
|
||||
public bool IsNullable { get; set; }
|
||||
public bool IsIdentity { get; set; }
|
||||
public bool IsPrimarykey { get; set; }
|
||||
}
|
||||
}
|
12
SqlSugar/Entities/DbTableInfo.cs
Normal file
12
SqlSugar/Entities/DbTableInfo.cs
Normal file
@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class DbTableInfo
|
||||
{
|
||||
public string Name { get; set; }
|
||||
}
|
||||
}
|
18
SqlSugar/Entities/JoinQueryInfo.cs
Normal file
18
SqlSugar/Entities/JoinQueryInfo.cs
Normal file
@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class JoinQueryInfo
|
||||
{
|
||||
public JoinType JoinType{ get; set; }
|
||||
public string PreShortName { get; set; }
|
||||
public string TableName { get; set; }
|
||||
public string ShortName { get; set; }
|
||||
public int JoinIndex { get; set; }
|
||||
public string JoinWhere { get; set; }
|
||||
public string TableWithString { get; set; }
|
||||
}
|
||||
}
|
14
SqlSugar/Entities/Mapping/MappingColumn.cs
Normal file
14
SqlSugar/Entities/Mapping/MappingColumn.cs
Normal file
@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class MappingColumn
|
||||
{
|
||||
public string EntityPropertyName { get; set; }
|
||||
public string DbColumnName { get; set; }
|
||||
public string DbTableName { get; set; }
|
||||
}
|
||||
}
|
13
SqlSugar/Entities/Mapping/MappingTable.cs
Normal file
13
SqlSugar/Entities/Mapping/MappingTable.cs
Normal file
@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class MappingTable
|
||||
{
|
||||
public string EntityName { get; set; }
|
||||
public string DbTableName { get; set; }
|
||||
}
|
||||
}
|
15
SqlSugar/Entities/PageModel.cs
Normal file
15
SqlSugar/Entities/PageModel.cs
Normal file
@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class PageModel
|
||||
{
|
||||
public int PageIndex { get; set; }
|
||||
public int PageSize { get; set; }
|
||||
public int PageCount { get; set; }
|
||||
}
|
||||
}
|
25
SqlSugar/Entities/SugarList.cs
Normal file
25
SqlSugar/Entities/SugarList.cs
Normal file
@ -0,0 +1,25 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class MappingTableList : List<MappingTable>
|
||||
{
|
||||
public void Add(string entityName, string dbTableName)
|
||||
{
|
||||
this.RemoveAll(it => it.EntityName.Equals(entityName, StringComparison.CurrentCultureIgnoreCase));
|
||||
this.Add(new MappingTable() { EntityName = entityName, DbTableName = dbTableName });
|
||||
}
|
||||
}
|
||||
|
||||
public class MappingColumnList: List<MappingColumn>
|
||||
{
|
||||
public void Add(string EntityPropertyName, string dbColumnName,string dbTableName)
|
||||
{
|
||||
this.RemoveAll(it => it.EntityPropertyName.Equals(EntityPropertyName, StringComparison.CurrentCultureIgnoreCase));
|
||||
this.Add(new MappingColumn() { EntityPropertyName = EntityPropertyName, DbColumnName = dbColumnName, DbTableName= dbTableName });
|
||||
}
|
||||
}
|
||||
}
|
13
SqlSugar/Enum/ApplyType.cs
Normal file
13
SqlSugar/Enum/ApplyType.cs
Normal file
@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public enum ApplyType
|
||||
{
|
||||
Cross = 1,
|
||||
Outer = 2
|
||||
}
|
||||
}
|
15
SqlSugar/Enum/JoinType.cs
Normal file
15
SqlSugar/Enum/JoinType.cs
Normal file
@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public enum JoinType
|
||||
{
|
||||
Inner = 0,
|
||||
Left = 1,
|
||||
Right = 2
|
||||
}
|
||||
|
||||
}
|
13
SqlSugar/Enum/OrderByType.cs
Normal file
13
SqlSugar/Enum/OrderByType.cs
Normal file
@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public enum OrderByType
|
||||
{
|
||||
Asc = 0,
|
||||
Desc = 1
|
||||
}
|
||||
}
|
13
SqlSugar/Enum/ResolveExpressType.cs
Normal file
13
SqlSugar/Enum/ResolveExpressType.cs
Normal file
@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public enum ResolveExpressType
|
||||
{
|
||||
Single = 0,
|
||||
Multiple = 1,
|
||||
}
|
||||
}
|
28
SqlSugar/Expressions/ExpConst.cs
Normal file
28
SqlSugar/Expressions/ExpConst.cs
Normal file
@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
internal partial class ErrorMessage
|
||||
{
|
||||
internal static string OperatorError
|
||||
{
|
||||
get
|
||||
{
|
||||
return GetThrowMessage("拉姆达解析出错:不支持{0}此种运算符查找!",
|
||||
"Lambda parsing error: {0} does not support the operator to find!");
|
||||
}
|
||||
}
|
||||
internal static string ExpFileldError
|
||||
{
|
||||
get
|
||||
{
|
||||
return GetThrowMessage("Expression format error, correct format: it=>it.fieldName",
|
||||
"表达示格式错误,正确格式: it=>it.fieldName");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
11
SqlSugar/Expressions/ExpInfo.cs
Normal file
11
SqlSugar/Expressions/ExpInfo.cs
Normal file
@ -0,0 +1,11 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class ExpInfo
|
||||
{
|
||||
}
|
||||
}
|
64
SqlSugar/Expressions/ExpItems/BaseResolve.cs
Normal file
64
SqlSugar/Expressions/ExpItems/BaseResolve.cs
Normal file
@ -0,0 +1,64 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class BaseResolve
|
||||
{
|
||||
protected Expression Expression { get; set; }
|
||||
public ExpressionContext Context { get; set; }
|
||||
public string SqlWhere { get; set; }
|
||||
private BaseResolve()
|
||||
{
|
||||
|
||||
}
|
||||
public BaseResolve(Expression expression)
|
||||
{
|
||||
this.Expression = expression;
|
||||
}
|
||||
|
||||
public BaseResolve Start()
|
||||
{
|
||||
Expression exp = this.Expression;
|
||||
if (exp is LambdaExpression)
|
||||
{
|
||||
return new LambdaExpressionResolve(exp);
|
||||
}
|
||||
else if (exp is BinaryExpression)
|
||||
{
|
||||
return new BinaryExpressionResolve(exp);
|
||||
}
|
||||
else if (exp is BlockExpression)
|
||||
{
|
||||
Check.ThrowNotSupportedException("BlockExpression");
|
||||
}
|
||||
else if (exp is ConditionalExpression)
|
||||
{
|
||||
Check.ThrowNotSupportedException("ConditionalExpression");
|
||||
}
|
||||
else if (exp is MethodCallExpression)
|
||||
{
|
||||
return new MethodCallExpressionResolve(exp);
|
||||
}
|
||||
else if (exp is ConstantExpression)
|
||||
{
|
||||
return new ConstantExpressionResolve(exp);
|
||||
}
|
||||
else if (exp is MemberExpression)
|
||||
{
|
||||
return new MemberExpressionResolve(exp);
|
||||
}
|
||||
else if (exp is UnaryExpression)
|
||||
{
|
||||
return new UnaryExpressionResolve(exp);
|
||||
}
|
||||
else if (exp != null && exp.NodeType.IsIn(ExpressionType.New, ExpressionType.NewArrayBounds, ExpressionType.NewArrayInit))
|
||||
{
|
||||
Check.ThrowNotSupportedException("ExpressionType.New、ExpressionType.NewArrayBounds and ExpressionType.NewArrayInit");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
15
SqlSugar/Expressions/ExpItems/BinaryExpressionResolve.cs
Normal file
15
SqlSugar/Expressions/ExpItems/BinaryExpressionResolve.cs
Normal file
@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class BinaryExpressionResolve:BaseResolve
|
||||
{
|
||||
public BinaryExpressionResolve(Expression exp) : base(exp)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
15
SqlSugar/Expressions/ExpItems/BlockExpressionResolve.cs
Normal file
15
SqlSugar/Expressions/ExpItems/BlockExpressionResolve.cs
Normal file
@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class BlockExpressionResolve:BaseResolve
|
||||
{
|
||||
public BlockExpressionResolve(Expression exp):base(exp)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class ConditionalExpressionResolve:BaseResolve
|
||||
{
|
||||
public ConditionalExpressionResolve(Expression exp) : base(exp)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
15
SqlSugar/Expressions/ExpItems/ConstantExpressionResolve.cs
Normal file
15
SqlSugar/Expressions/ExpItems/ConstantExpressionResolve.cs
Normal file
@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class ConstantExpressionResolve:BaseResolve
|
||||
{
|
||||
public ConstantExpressionResolve(Expression exp) : base(exp)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
15
SqlSugar/Expressions/ExpItems/LambdaExpressionResolve.cs
Normal file
15
SqlSugar/Expressions/ExpItems/LambdaExpressionResolve.cs
Normal file
@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class LambdaExpressionResolve: BaseResolve
|
||||
{
|
||||
public LambdaExpressionResolve(Expression exp) : base(exp)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
16
SqlSugar/Expressions/ExpItems/MemberExpressionResolve.cs
Normal file
16
SqlSugar/Expressions/ExpItems/MemberExpressionResolve.cs
Normal file
@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class MemberExpressionResolve:BaseResolve
|
||||
{
|
||||
public MemberExpressionResolve(Expression exp) : base(exp)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
15
SqlSugar/Expressions/ExpItems/MethodCallExpressionResolve.cs
Normal file
15
SqlSugar/Expressions/ExpItems/MethodCallExpressionResolve.cs
Normal file
@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class MethodCallExpressionResolve:BaseResolve
|
||||
{
|
||||
public MethodCallExpressionResolve(Expression exp) : base(exp)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
15
SqlSugar/Expressions/ExpItems/UnaryExpressionResolve.cs
Normal file
15
SqlSugar/Expressions/ExpItems/UnaryExpressionResolve.cs
Normal file
@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class UnaryExpressionResolve:BaseResolve
|
||||
{
|
||||
public UnaryExpressionResolve(Expression exp) : base(exp)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
12
SqlSugar/Expressions/ExpResolveAccessory.cs
Normal file
12
SqlSugar/Expressions/ExpResolveAccessory.cs
Normal file
@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class ExpResolveAccessory
|
||||
{
|
||||
protected List<SqlParameter> _Parameters { get; set; }
|
||||
}
|
||||
}
|
49
SqlSugar/Expressions/ExpTool.cs
Normal file
49
SqlSugar/Expressions/ExpTool.cs
Normal file
@ -0,0 +1,49 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class ExpTool
|
||||
{
|
||||
public static string GetOperator(ExpressionType expressiontype)
|
||||
{
|
||||
switch (expressiontype)
|
||||
{
|
||||
case ExpressionType.And:
|
||||
case ExpressionType.AndAlso:
|
||||
return " AND ";
|
||||
case ExpressionType.Equal:
|
||||
return " =";
|
||||
case ExpressionType.GreaterThan:
|
||||
return " >";
|
||||
case ExpressionType.GreaterThanOrEqual:
|
||||
return ">=";
|
||||
case ExpressionType.LessThan:
|
||||
return "<";
|
||||
case ExpressionType.LessThanOrEqual:
|
||||
return "<=";
|
||||
case ExpressionType.NotEqual:
|
||||
return "<>";
|
||||
case ExpressionType.Or:
|
||||
case ExpressionType.OrElse:
|
||||
return " OR ";
|
||||
case ExpressionType.Add:
|
||||
case ExpressionType.AddChecked:
|
||||
return "+";
|
||||
case ExpressionType.Subtract:
|
||||
case ExpressionType.SubtractChecked:
|
||||
return "-";
|
||||
case ExpressionType.Divide:
|
||||
return "/";
|
||||
case ExpressionType.Multiply:
|
||||
case ExpressionType.MultiplyChecked:
|
||||
return "*";
|
||||
default:
|
||||
Check.ThrowNotSupportedException(string.Format(ErrorMessage.OperatorError, expressiontype.ToString()));
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user