📄 pathexpressionparser.cs
字号:
///
/// </summary>
/// <returns></returns>
protected string[] CurrentColumns()
{
string p = PropertyPath;
string[] propertyColumns = PropertyMapping.ToColumns(currentName, p);
if (propertyColumns == null)
{
throw new QueryException("could not resolve property columns: " + p);
}
return propertyColumns;
}
private void Reset(QueryTranslator q)
{
//join = q.CreateJoinFragment( useThetaStyleJoin );
dotcount = 0;
currentName = null;
currentProperty = null;
collectionName = null;
collectionRole = null;
componentPath.Length = 0;
type = null;
collectionName = null;
columns = null;
expectingCollectionIndex = false;
continuation = false;
currentPropertyMapping = null;
}
/// <summary>
///
/// </summary>
/// <param name="q"></param>
public void Start(QueryTranslator q)
{
if (!continuation)
{
Reset(q);
path.Length = 0;
joinSequence = new JoinSequence(q.Factory).SetUseThetaStyle(useThetaStyleJoin);
}
}
/// <summary>
///
/// </summary>
/// <param name="q"></param>
public virtual void End(QueryTranslator q)
{
ignoreInitialJoin = false;
IType propertyType = PropertyType;
if (propertyType != null && propertyType.IsCollectionType)
{
collectionRole = ((CollectionType) propertyType).Role;
collectionName = q.CreateNameForCollection(collectionRole);
PrepareForIndex(q);
}
else
{
columns = CurrentColumns();
SetType(q);
}
//important!!
continuation = false;
}
private void PrepareForIndex(QueryTranslator q)
{
IQueryableCollection collPersister = q.GetCollectionPersister(collectionRole);
if (!collPersister.HasIndex)
{
throw new QueryException("unindexed collection before []");
}
string[] indexCols = collPersister.IndexColumnNames;
if (indexCols.Length != 1)
{
throw new QueryException("composite-index appears in []: " + path);
}
JoinSequence fromJoins = new JoinSequence(q.Factory)
.SetUseThetaStyle(useThetaStyleJoin)
.SetRoot(collPersister, collectionName)
.SetNext(joinSequence.Copy());
if (!continuation)
{
AddJoin(collectionName, collPersister.CollectionType);
}
joinSequence.AddCondition(new SqlString(collectionName + '.' + indexCols[0] + " = "));
CollectionElement elem = new CollectionElement();
elem.ElementColumns = collPersister.GetElementColumnNames(collectionName);
elem.Type = collPersister.ElementType;
elem.IsOneToMany = collPersister.IsOneToMany;
elem.Alias = collectionName;
elem.JoinSequence = joinSequence;
collectionElements.Add(elem); //addlast
SetExpectingCollectionIndex();
q.AddCollection(collectionName, collectionRole);
q.AddJoin(collectionName, fromJoins);
}
/// <summary></summary>
public sealed class CollectionElement // struct?
{
/// <summary></summary>
public IType Type;
/// <summary></summary>
public bool IsOneToMany;
/// <summary></summary>
public string Alias;
/// <summary></summary>
public string[] ElementColumns;
/// <summary></summary>
public JoinSequence JoinSequence;
/// <summary></summary>
public SqlStringBuilder IndexValue = new SqlStringBuilder();
}
private bool expectingCollectionIndex;
private ArrayList collectionElements = new ArrayList();
/// <summary></summary>
public CollectionElement LastCollectionElement()
{
CollectionElement ce = (CollectionElement) collectionElements[collectionElements.Count - 1];
collectionElements.RemoveAt(collectionElements.Count - 1);
return ce; //remove last
}
/// <summary></summary>
public void SetLastCollectionElementIndexValue(SqlString value)
{
((CollectionElement) collectionElements[collectionElements.Count - 1]).IndexValue.Add(value); //getlast
}
/// <summary></summary>
public bool IsExpectingCollectionIndex
{
get { return expectingCollectionIndex; }
set { expectingCollectionIndex = value; }
}
/// <summary></summary>
protected virtual void SetExpectingCollectionIndex()
{
expectingCollectionIndex = true;
}
public JoinSequence WhereJoin
{
get { return joinSequence; }
}
/// <summary></summary>
public string WhereColumn
{
get
{
if (columns.Length != 1)
{
throw new QueryException("path expression ends in a composite value");
}
return columns[0];
}
}
/// <summary></summary>
public string[] WhereColumns
{
get { return columns; }
}
/// <summary></summary>
public IType WhereColumnType
{
get { return type; }
}
/// <summary></summary>
public string Name
{
get { return currentName == null ? collectionName : currentName; }
}
/// <summary></summary>
public string GetCollectionSubquery(IDictionary<string, IFilter> enabledFilters)
{
return CollectionSubqueryFactory.CreateCollectionSubquery(
joinSequence, enabledFilters, CurrentColumns());
}
/// <summary></summary>
public bool IsCollectionValued
{
// TODO: Is there a better way
get { return collectionName != null && !PropertyType.IsCollectionType; }
}
/// <summary>
///
/// </summary>
/// <param name="q"></param>
public void AddAssociation(QueryTranslator q)
{
q.AddJoin(Name, joinSequence);
}
/// <summary>
///
/// </summary>
/// <param name="q"></param>
/// <returns></returns>
public string AddFromAssociation(QueryTranslator q)
{
if (IsCollectionValued)
{
return AddFromCollection(q);
}
else
{
q.AddFrom(currentName, joinSequence);
return currentName;
}
}
/// <summary>
///
/// </summary>
/// <param name="q"></param>
/// <returns></returns>
public string AddFromCollection(QueryTranslator q)
{
IType collectionElementType = PropertyType;
if (collectionElementType == null)
{
throw new QueryException(
string.Format("must specify 'elements' for collection valued property in from clause: {0}", path));
}
if (collectionElementType.IsEntityType)
{
// an association
IQueryableCollection collectionPersister = q.GetCollectionPersister(collectionRole);
IQueryable entityPersister = (IQueryable) collectionPersister.ElementPersister;
string clazz = entityPersister.EntityName;
string elementName;
if (collectionPersister.IsOneToMany)
{
elementName = collectionName;
// allow index() function
q.DecoratePropertyMapping(elementName, collectionPersister);
}
else
{
// many to many
q.AddCollection(collectionName, collectionRole);
elementName = q.CreateNameFor(clazz);
AddJoin(elementName, (IAssociationType) collectionElementType);
}
q.AddFrom(elementName, clazz, joinSequence);
currentPropertyMapping = new CollectionPropertyMapping(collectionPersister);
return elementName;
}
else
{
// collection of values
q.AddFromCollection(collectionName, collectionRole, joinSequence);
return collectionName;
}
}
/// <summary></summary>
public string CollectionName
{
get { return collectionName; }
}
/// <summary></summary>
public string CollectionRole
{
get { return collectionRole; }
}
/// <summary></summary>
public String CollectionOwnerName
{
get { return collectionOwnerName; }
}
/// <summary></summary>
public string CurrentName
{
get { return currentName; }
}
/// <summary></summary>
public string CurrentProperty
{
get { return currentProperty; }
}
/// <summary>
///
/// </summary>
/// <param name="q"></param>
/// <param name="entityName"></param>
public void Fetch(QueryTranslator q, string entityName)
{
if (IsCollectionValued)
{
q.AddCollectionToFetch(CollectionRole, CollectionName, CollectionOwnerName, entityName);
}
else
{
q.AddEntityToFetch(entityName, oneToOneOwnerName, ownerAssociationType);
}
}
public string ProcessedPath
{
// NH: Added to use it in exceptions
get { return path.ToString(); }
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -