using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Project.Data.MSSQLServer;
using Project.Core.Expressions.MSSQLQuery;
using Project.Core.Expressions;
using Project.Core.StructModel.Data;
using Project.CityMall.DAL.MSSQLServer.Structs;
using Project.Model;
using Project.API;
using Project.Core.StructModel;
using Project.CityMall.Model.IDML;
using System.Data;
using Project.CityMall.Model;
using System.Collections;
using Project.Data;
namespace Project.CityMall.DAL.MSSQLServer
{
public class PlaceHolderObject : IExpressionObject
{
public PlaceHolderObject(object value)
{
this.Value = value;
}
#region IExpressionObject 成员
public object Value
{
get;
set;
}
#endregion
}
public abstract class DataAccessBase<TEntity, TModelStruct> : DataAccessComponent where TEntity : EntityModel<TModelStruct>, new()
{
public readonly MSSQLQueryExpression QueryExpr = new MSSQLQueryExpression();
public IMallStruct Mall
{
get
{
//if (MyStruct.Mall == null)
//{
// MyStruct.Mall=new MallStruct();
//}
return MyStruct.Mall;
}
}
public abstract TModelStruct StructInstance
{
get;
}
private static readonly PlaceHolderObject COUNT_OPERATOR = new PlaceHolderObject("COUNT(*)");
private static readonly PlaceHolderObject REPLACE_SIGN = new PlaceHolderObject("{$0}");
#region 获取列表
public TR Gets<TR, TE>(
StdQueryExpression expr,
IExpressionObject[] fields,
IKeyStruct[] froms,
IExpression mappingExpression,
IExpressionObject terms,
IExpressionObject[] sorts,
int? topNum,
IPager pager,
TModelStruct modelStruct,
ResultSetWrapper<TR, TE> resultSetWrapper
)
where TR : IEnumerable, new()
where TE : EntityModel<TModelStruct>, new()
{
if (froms == null && modelStruct is IKeyStruct)
{
froms = new[] { (IKeyStruct)modelStruct };
}
if (pager != null)
{
Expression selectExpr;
Expression filterExpr;
Expression countExpr;
if (mappingExpression != null)
{
selectExpr = expr.Select(fields, expr.Join(froms, mappingExpression), expr.In(froms[0].GetPrimaryKey(), REPLACE_SIGN), sorts);
filterExpr = expr.Select(froms[0].GetPrimaryKey(), expr.Join(froms, mappingExpression), terms, sorts);
countExpr = expr.Select(COUNT_OPERATOR, expr.Join(froms, mappingExpression), terms, null);
}
else
{
selectExpr = expr.Select(fields, froms, expr.In(froms[0].GetPrimaryKey(), REPLACE_SIGN), sorts);
filterExpr = expr.Select(froms[0].GetPrimaryKey(), froms, terms, sorts);
countExpr = expr.Select(COUNT_OPERATOR, froms, terms, null);
}
string selectStr = selectExpr.Value.ToString();
string filterStr = filterExpr.Value.ToString();
string countStr = countExpr.Value.ToString();
using (DeluxeSqlHelper helper = this.CreateStoredProcedure("GetPagedRecords"))
{
helper.AddInParameter("@SqlString", SqlDbType.VarChar, 512, selectStr);
helper.AddInParameter("@InsertSqlString", SqlDbType.VarChar, 512, filterStr);
helper.AddInParameter("@CountSqlString", SqlDbType.VarChar, 512, countStr);
helper.AddPagedParameters(pager);
return helper.GetDatasAs<TR, TE, TModelStruct>(modelStruct, resultSetWrapper, pager);
}
}
else
{
object fromExpr;
if (mappingExpression != null)
{
fromExpr = expr.Join(froms, mappingExpression);
}
else
{
fromExpr = froms;
}
using (DeluxeSqlHelper helper = this.CreateTextSql(expr.Select(fields, fromExpr, terms, sorts, topNum).Value.ToString()))
{
return helper.GetDatasAs<TR, TE, TModelStruct>(modelStruct, resultSetWrapper);
}
}
}
public TResultSet Gets<TResultSet>(DataQueryMethod<TModelStruct> query, ResultSetWrapper<TResultSet, TEntity> resultSetWrapper) where TResultSet : IEnumerable, new()
{
QueryParam param = new QueryParam();
query(this.QueryExpr, param, this.StructInstance);
return this.Gets<TResultSet, TEntity>(this.QueryExpr, param.Fields, param.Froms, param.MappingExpression, param.Terms, param.Sorts, param.TopNum, param.Pager, this.StructInstance, resultSetWrapper);
}
public List<TE> Gets<TE>(
StdQueryExpression expr,
IExpressionObject[] fields,
IKeyStruct[] froms,
IExpression mappingExpression,
IExpressionObject terms,
IExpressionObject[] sorts,
int? topNum,
IPager pager,
TModelStruct modelStruct
) where TE : EntityModel<TModelStruct>, new()
{
return this.Gets<List<TE>, TE>(expr, fields, froms, mappingExpression, terms, sorts, topNum, pager, modelStruct, new ListResultSetWrapper<TE>());
}
public List<TE> Gets<TE>(DataQueryMethod<TModelStruct> query, QueryParam param, TModelStruct modelStruct)
where TE : EntityModel<TModelStruct>, new()
{
query(this.QueryExpr, param, modelStruct);
return this.Gets<TE>(this.QueryExpr, param.Fields, param.Froms, param.MappingExpression, param.Terms, param.Sorts, param.TopNum, param.Pager, modelStruct);
}
public List<TEntity> Gets(DataQueryMethod<TModelStruct> query, TModelStruct modelStruct)
{
return this.Gets<TEntity>(query, new QueryParam(), modelStruct);
}
public List<TEntity> Gets(DataQueryMethod<TModelStruct> query)
{
return this.Gets(query, this.StructInstance);
}
#endregion
#region 获取单条
public TE Get<TE>(
StdQueryExpression expr,
IExpressionObject[] fields,
IKeyStruct[] froms,
IExpression mappingExpression,
IExpressionObject terms,
TModelStruct modelStruct
) where TE : EntityModel<TModelStruct>, new()
{
if (froms == null && modelStruct is IKeyStruct)
{
froms = new[] { (IKeyStruct)modelStruct };
}
object fromExpr;
if (mappingExpression != null)
{
fromExpr = expr.Join(froms, mappingExpression);
}
else
{
fromExpr = froms;
}
using (DeluxeSqlHelper helper = this.CreateTextSql(expr.Select(fields, fromExpr, terms, null, 1).Value.ToString()))
{
return helper.GetDataAs<TE, TModelStruct>(modelStruct);
}
}
public TE Get<TE>(DataQueryMethod<TModelStruct> query, QueryParam param, TModelStruct modelStruct)
where TE : EntityModel<TModelStruct>, new()
{
query(this.QueryExpr, param, modelStruct);
return this.Get<TE>(this.QueryExpr, param.Fields, param.Froms, param.MappingExpression, param.Terms, modelStruct);
}
public TEntity Get(DataQueryMethod<TModelStruct> query, TModelStruct modelStruct)
{
return this.Get<TEntity>(query, new QueryParam(), modelStruct);
}
public TEntity Get(DataQueryMethod<TModelStruct> query)
{
return this.Get(query, this.StructInstance);
}
#endregion
#region 获取记录行数
public int GetCount(StdQueryExpression expr, IKeyStruct[] froms, IExpression mappingExpression, IExpressionObject terms, TModelStruct modelStruct)
{
if (froms == null && modelStruct is IKeyStruct)
{
froms = new[] { (IKeyStruct)modelStruct };
}
object fromExpr;
if (mappingExpression != null)
{
fromExpr = expr.Join(froms, mappingExpression);
}
else
{
fromExpr = froms;
}
using (DeluxeSqlHelper helper = this.CreateTextSql(expr.Select(COUNT_OPERATOR, fromExpr, terms, null).Value.ToString()))
{
return (int)helper.ExecuteScalar();
}
}
public int GetCount(DataQueryMethod<TModelStruct> query, QueryParam param, TModelStruct modelStruct)
{
query(this.QueryExpr, param, modelStruct);
return this.GetCount(this.QueryExpr, param.Froms, param.MappingExpression, param.Terms, modelStruct);
}
public int GetCount(DataQueryMethod<TModelStruct> query, TModelStruct modelStruct)
{
return this.GetCount(query, new QueryParam(), modelStruct);
}
public int GetCount(DataQueryMethod<TModelStruct> query)
{
return this.GetCount(query, this.StructInstance);
}
#endregion
}
}
注:经过测试,发现最终情况是:
using (DeluxeSqlHelper helper = this.CreateTextSql(expr.Select(fields, fromExpr, terms, sorts, topNum).Value.ToString()))
{
return helper.GetDatasAs<TR, TE, TModelStruct>(modelStruct, resultSetWrapper);
}
看到了希望,
expr.Select(fields, fromExpr, terms, sorts, topNum).Value.ToString()
"SELECT * FROM OrderDeliveries WHERE 29 = OrderDeliveries.OrderID"