criteriaquerytranslator.cs
来自「NHibernate NET开发者所需的」· CS 代码 · 共 702 行 · 第 1/2 页
CS
702 行
{
ICriteria subcriteria = GetAliasedCriteria((string)me.Key);
lockModes[GetSQLAlias(subcriteria)] = me.Value;
}
foreach (CriteriaImpl.Subcriteria subcriteria in rootCriteria.IterateSubcriteria())
{
LockMode lm = subcriteria.LockMode;
if (lm != null)
{
lockModes[GetSQLAlias(subcriteria)] = lm;
}
}
return new QueryParameters(
typeArray,
valueArray,
lockModes,
selection,
rootCriteria.Cacheable,
rootCriteria.CacheRegion,
string.Empty, // TODO H3: rootCriteria.Comment,
rootCriteria.IsLookupByNaturalKey(),
null
);
}
public bool HasProjection
{
get { return rootCriteria.Projection != null; }
}
public SqlString GetGroupBy()
{
if (rootCriteria.Projection.IsGrouped)
{
return rootCriteria.Projection
.ToGroupSqlString(rootCriteria.ProjectionCriteria, this, new CollectionHelper.EmptyMapClass<string, IFilter>());
}
else
{
return SqlString.Empty;
}
}
public SqlString GetSelect(IDictionary<string, IFilter> enabledFilters)
{
return rootCriteria.Projection.ToSqlString(
rootCriteria.ProjectionCriteria,
0,
this,
enabledFilters
);
}
public IType[] ProjectedTypes
{
get { return rootCriteria.Projection.GetTypes(rootCriteria, this); }
}
public string[] ProjectedColumnAliases
{
get { return rootCriteria.Projection.GetColumnAliases(0); }
}
public string[] ProjectedAliases
{
get { return rootCriteria.Projection.Aliases; }
}
public SqlString GetWhereCondition(IDictionary<string, IFilter> enabledFilters)
{
SqlStringBuilder condition = new SqlStringBuilder(30);
bool first = true;
foreach (CriteriaImpl.CriterionEntry entry in rootCriteria.IterateExpressionEntries())
{
if (!first)
{
condition.Add(" and ");
}
first = false;
SqlString sqlString = entry.Criterion.ToSqlString(entry.Criteria, this, enabledFilters);
condition.Add(sqlString);
}
return condition.ToSqlString();
}
public string GetOrderBy()
{
StringBuilder orderBy = new StringBuilder(30);
bool first = true;
foreach (CriteriaImpl.OrderEntry oe in rootCriteria.IterateOrderings())
{
if (!first)
{
orderBy.Append(", ");
}
first = false;
orderBy.Append(oe.Order.ToSqlString(oe.Criteria, this));
}
return orderBy.ToString();
}
public ISessionFactoryImplementor Factory
{
get { return sessionFactory; }
}
[CLSCompliant(false)] // TODO: Why does this cause a problem in 1.1
public string RootSQLAlias
{
get { return rootSQLAlias; }
}
public string GetSQLAlias(ICriteria criteria)
{
return criteriaSQLAliasMap[criteria];
}
public string GetEntityName(ICriteria criteria)
{
string result;
criteriaEntityNames.TryGetValue(criteria, out result);
return result;
}
public string GetColumn(ICriteria criteria, string propertyName)
{
string[] cols = GetColumns(propertyName, criteria);
if (cols.Length != 1)
{
throw new QueryException("property does not map to a single column: " + propertyName);
}
return cols[0];
}
/// <summary>
/// Get the names of the columns constrained
/// by this criterion.
/// </summary>
public string[] GetColumnsUsingProjection(
ICriteria subcriteria,
string propertyName)
{
try
{
return GetColumns(propertyName, subcriteria);
}
catch (HibernateException)
{
//not found in inner query , try the outer query
if (outerQueryTranslator != null)
{
return outerQueryTranslator.GetColumnsUsingProjection(subcriteria, propertyName);
}
else
{
throw;
}
}
}
/// <summary>
/// Get the aliases of the columns constrained
/// by this criterion (for use in ORDER BY clause).
/// </summary>
public string[] GetColumnAliasesUsingProjection(
ICriteria subcriteria,
string propertyName)
{
//first look for a reference to a projection alias
IProjection projection = rootCriteria.Projection;
string[] projectionColumns = projection == null ?
null :
projection.GetColumnAliases(propertyName, 0);
if (projectionColumns == null)
{
//it does not refer to an alias of a projection,
//look for a property
try
{
return GetColumns(propertyName, subcriteria);
}
catch (HibernateException)
{
//not found in inner query , try the outer query
if (outerQueryTranslator != null)
{
return outerQueryTranslator.GetColumnAliasesUsingProjection(subcriteria, propertyName);
}
else
{
throw;
}
}
}
else
{
//it refers to an alias of a projection
return projectionColumns;
}
}
public string[] GetIdentifierColumns(ICriteria subcriteria)
{
string[] idcols =
((ILoadable)GetPropertyMapping(GetEntityName(subcriteria))).IdentifierColumnNames;
return StringHelper.Qualify(GetSQLAlias(subcriteria), idcols);
}
public IType GetIdentifierType(ICriteria subcriteria)
{
return ((ILoadable)GetPropertyMapping(GetEntityName(subcriteria))).IdentifierType;
}
public TypedValue GetTypedIdentifierValue(ICriteria subcriteria, object value)
{
ILoadable loadable = (ILoadable)GetPropertyMapping(GetEntityName(subcriteria));
return new TypedValue(loadable.IdentifierType, value, EntityMode.Poco);
}
private string[] GetColumns(string propertyName, ICriteria subcriteria)
{
string entName = GetEntityName(subcriteria, propertyName);
if (entName == null)
throw new QueryException("Could not find property " + propertyName + " on " + subcriteria.CriteriaClass);
return GetPropertyMapping(GetEntityName(subcriteria, propertyName)).ToColumns(GetSQLAlias(subcriteria, propertyName), GetPropertyName(propertyName));
}
public IType GetTypeUsingProjection(ICriteria subcriteria, string propertyName)
{
//first look for a reference to a projection alias
IProjection projection = rootCriteria.Projection;
IType[] projectionTypes = projection == null ?
null :
projection.GetTypes(propertyName, subcriteria, this);
if (projectionTypes == null)
{
try
{
//it does not refer to an alias of a projection,
//look for a property
return GetType(subcriteria, propertyName);
}
catch (HibernateException)
{
//not found in inner query , try the outer query
if (outerQueryTranslator != null)
{
return outerQueryTranslator.GetType(subcriteria, propertyName);
}
else
{
throw;
}
}
}
else
{
if (projectionTypes.Length != 1)
{
//should never happen, i think
throw new QueryException("not a single-length projection: " + propertyName);
}
return projectionTypes[0];
}
}
public IType GetType(ICriteria subcriteria, string propertyName)
{
return GetPropertyMapping(GetEntityName(subcriteria, propertyName))
.ToType(GetPropertyName(propertyName));
}
/// <summary>
/// Get the a typed value for the given property value.
/// </summary>
public TypedValue GetTypedValue(ICriteria subcriteria, string propertyName, object value)
{
// Detect discriminator values...
if (value is System.Type)
{
System.Type entityClass = (System.Type)value;
IQueryable q = SessionFactoryHelper.FindQueryableUsingImports(sessionFactory, entityClass.FullName);
if (q != null)
{
// NH Different implementation : We are using strongly typed parameter for SQL query (see DiscriminatorValue comment)
return new TypedValue(q.DiscriminatorType, q.DiscriminatorValue, EntityMode.Poco);
}
}
// Otherwise, this is an ordinary value.
return new TypedValue(GetTypeUsingProjection(subcriteria, propertyName), value, EntityMode.Poco);
}
private IPropertyMapping GetPropertyMapping(string entityName)
{
return (IPropertyMapping)sessionFactory.GetEntityPersister(entityName);
}
//TODO: use these in methods above
public string GetEntityName(ICriteria subcriteria, string propertyName)
{
if (propertyName.IndexOf('.') > 0)
{
string root = StringHelper.Root(propertyName);
ICriteria crit = GetAliasedCriteria(root);
if (crit != null)
{
return GetEntityName(crit);
}
}
return GetEntityName(subcriteria);
}
public string GetSQLAlias(ICriteria criteria, string propertyName)
{
if (propertyName.IndexOf('.') > 0)
{
string root = StringHelper.Root(propertyName);
ICriteria subcriteria = GetAliasedCriteria(root);
if (subcriteria != null)
{
return GetSQLAlias(subcriteria);
}
}
return GetSQLAlias(criteria);
}
public string GetPropertyName(string propertyName)
{
if (propertyName.IndexOf('.') > 0)
{
string root = StringHelper.Root(propertyName);
ICriteria crit = GetAliasedCriteria(root);
if (crit != null)
{
return propertyName.Substring(root.Length + 1);
}
}
return propertyName;
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?