This commit is contained in:
sunkaixuan
2017-07-24 14:48:46 +08:00
parent bd13c8dd28
commit 7f87f7d75f
3 changed files with 48 additions and 13 deletions

View File

@@ -8,7 +8,7 @@ using System.Threading.Tasks;
namespace OrmTest.UnitTest
{
public class Mapping:UnitTestBase
public class Mapping : UnitTestBase
{
private Mapping() { }
public Mapping(int eachCount)
@@ -16,30 +16,53 @@ namespace OrmTest.UnitTest
this.Count = eachCount;
}
public void Init() {
public void Init()
{
var db = GetInstance();
var t1= db.Queryable<Student>().Where(it=>it.Id==1).ToSql();
base.Check("SELECT [ID],[SchoolId],[Name],[CreateTime] FROM [STudent] WHERE ( [ID] = @Id0 ) ", null, t1.Key, null,"Mapping t1 error");
var t1 = db.Queryable<Student>().Where(it => it.Id == 1).ToSql();
base.Check("SELECT [ID],[SchoolId],[Name],[CreateTime] FROM [STudent] WHERE ( [ID] = @Id0 ) ", null, t1.Key, null, "Mapping t1 error");
db.MappingColumns.Add("Id", "id", "School");
var t2 = db.Queryable<Student, School>((st, sc) => new object[] {
JoinType.Left,st.SchoolId==sc.Id
})
.Where(st => st.Id == 1)
.Where((st,sc) => sc.Id == 1)
.Where((st,sc) => sc.Id == st.Id)
.Where((st, sc) => sc.Id == 1)
.Where((st, sc) => sc.Id == st.Id)
.GroupBy(st => st.Id)
.GroupBy((st,sc) => sc.Id).OrderBy(st => st.Id,OrderByType.Asc)
.Select((st,sc)=> new { stid=st.Id,scid=sc.Id}).ToSql();
.GroupBy((st, sc) => sc.Id).OrderBy(st => st.Id, OrderByType.Asc)
.Select((st, sc) => new { stid = st.Id, scid = sc.Id }).ToSql();
base.Check(@"SELECT [st].[ID] AS [stid] , [sc].[id] AS [scid] FROM [STudent] st Left JOIN School sc ON ( [st].[SchoolId] = [sc].[id] ) WHERE ( [st].[ID] = @Id0 ) AND ( [sc].[id] = @Id1 ) AND ( [sc].[id] = [st].[ID] )GROUP BY [st].[ID],[sc].[id]ORDER BY [st].[ID] ASC ",
null, t2.Key, null, " Mapping t2 error");
var x2 = GetInstance();
var q = x2.Queryable<Student>().AS("t");
var t3 = q.ToSql();
var t4 = q.ToSql();
base.Check("SELECT [ID],[SchoolId],[Name],[CreateTime] FROM [t] ", null, t3.Key, null, "Mapping t3 error");
base.Check("SELECT [ID],[SchoolId],[Name],[CreateTime] FROM [t] ", null, t4.Key, null, "Mapping t3 error");
var x3 = GetInstance2();
x3.MappingTables.Add("Student", "studenT");
int count = 0;
var t5 = x3.Queryable<Student>().ToPageList(1,2,ref count);
}
public new SqlSugarClient GetInstance()
{
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() {InitKeyType=InitKeyType.Attribute, ConnectionString = Config.ConnectionString, DbType = DbType.SqlServer, IsAutoCloseConnection = true });
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { InitKeyType = InitKeyType.Attribute, ConnectionString = Config.ConnectionString, DbType = DbType.SqlServer, IsAutoCloseConnection = true });
return db;
}
public SqlSugarClient GetInstance2()
{
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { InitKeyType = InitKeyType.Attribute, ConnectionString = Config.ConnectionString, DbType = DbType.SqlServer, IsAutoCloseConnection = true });
db.Ado.IsEnableLogEvent = true;
db.Ado.LogEventStarting = (sql, pars) =>
{
Console.WriteLine(sql + "\r\n" + db.RewritableMethods.SerializeObject(pars));
Console.WriteLine();
};
return db;
}
}

View File

@@ -18,6 +18,7 @@ namespace SqlSugar
public IDbBind Bind { get { return this.Db.DbBind; } }
public ISqlBuilder SqlBuilder { get; set; }
public MappingTableList OldMappingTableList { get; set; }
public MappingTableList QueryableMappingTableList { get; set; }
public bool IsAs { get; set; }
public QueryBuilder QueryBuilder
{
@@ -45,6 +46,7 @@ namespace SqlSugar
OldMappingTableList = this.Context.MappingTables;
this.Context.MappingTables = this.Context.RewritableMethods.TranslateCopy(this.Context.MappingTables);
this.Context.MappingTables.Add(entityName, tableName);
this.QueryableMappingTableList = this.Context.MappingTables;
return this;
}
public ISugarQueryable<T> AS(string tableName)
@@ -54,6 +56,7 @@ namespace SqlSugar
OldMappingTableList = this.Context.MappingTables;
this.Context.MappingTables = this.Context.RewritableMethods.TranslateCopy(this.Context.MappingTables);
this.Context.MappingTables.Add(entityName, tableName);
this.QueryableMappingTableList = this.Context.MappingTables;
return this;
}
public virtual ISugarQueryable<T> With(string withString)
@@ -374,7 +377,7 @@ namespace SqlSugar
{
return _Select<TResult>(expression);
}
public virtual ISugarQueryable<TResult> Select<TResult>(string selectValue)
public virtual ISugarQueryable<TResult> Select<TResult>(string selectValue)
{
var reval = InstanceFactory.GetQueryable<TResult>(this.Context.CurrentConnectionConfig);
reval.Context = this.Context;
@@ -390,7 +393,7 @@ namespace SqlSugar
public virtual int Count()
{
InitMapping();
var sql = string.Empty;
if (QueryBuilder.PartitionByValue.IsValuable())
{
@@ -467,6 +470,7 @@ namespace SqlSugar
public virtual DataTable ToDataTable()
{
InitMapping();
var sqlObj = this.ToSql();
RestoreMapping();
var result = this.Db.GetDataTable(sqlObj.Key, sqlObj.Value.ToArray());
@@ -499,6 +503,7 @@ namespace SqlSugar
public virtual List<T> ToList()
{
InitMapping();
return _ToList<T>();
}
public virtual List<T> ToPageList(int pageIndex, int pageSize)
@@ -532,6 +537,7 @@ namespace SqlSugar
public virtual KeyValuePair<string, List<SugarParameter>> ToSql()
{
InitMapping();
string sql = QueryBuilder.ToSqlString();
RestoreMapping();
return new KeyValuePair<string, List<SugarParameter>>(sql, QueryBuilder.Parameters);
@@ -676,6 +682,7 @@ namespace SqlSugar
return this.EntityInfo.Columns.Where(it => it.IsIdentity).Select(it => it.DbColumnName).ToList();
}
}
protected void RestoreMapping()
{
if (IsAs && _RestoreMapping)
@@ -683,6 +690,11 @@ namespace SqlSugar
this.Context.MappingTables = OldMappingTableList == null ? new MappingTableList() : OldMappingTableList;
}
}
protected void InitMapping()
{
if (this.QueryableMappingTableList != null)
this.Context.MappingTables = this.QueryableMappingTableList;
}
private void SetContextModel<TResult>(List<TResult> result, Type entityType)
{

View File

@@ -31,5 +31,5 @@ using System.Runtime.InteropServices;
// 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("4.2.1.8")]
[assembly: AssemblyFileVersion("4.2.1.8")]
[assembly: AssemblyVersion("4.2.1.9")]
[assembly: AssemblyFileVersion("4.2.1.9")]