📄 pathexpressionparser.java
字号:
throw new QueryException("could not resolve property type: " + propertyPath); return propertyType; } protected String[] currentColumns() throws QueryException { String propertyPath = getPropertyPath(); String[] propertyColumns = getPropertyMapping().toColumns(currentName, propertyPath); if (propertyColumns==null) throw new QueryException("could not resolve property columns: " + propertyPath); return propertyColumns; } private void reset(QueryTranslator q) { join = q.createJoinFragment(useThetaStyleJoin); dotcount=0; currentName=null; currentProperty=null; collectionName = null; collectionRole = null; componentPath.setLength(0); type = null; collectionName = null; columns=null; expectingCollectionIndex = false; continuation = false; currentPropertyMapping = null; } public void start(QueryTranslator q) { if (!continuation) { reset(q); path.setLength(0); } } public void end(QueryTranslator q) throws QueryException { ignoreInitialJoin = false; Type propertyType = getPropertyType(); if ( propertyType!=null && propertyType.isPersistentCollectionType() ) { collectionRole = ( (PersistentCollectionType) propertyType ).getRole(); collectionName = q.createNameForCollection(collectionRole); prepareForIndex(q); } else { columns = currentColumns(); setType(); } //important!! continuation=false; } private void prepareForIndex(QueryTranslator q) throws QueryException { QueryableCollection collPersister = q.getCollectionPersister(collectionRole); if ( !collPersister.hasIndex() ) throw new QueryException("unindexed collection before []: " + path); String[] indexCols = collPersister.getIndexColumnNames(); if ( indexCols.length!=1 ) throw new QueryException("composite-index appears in []: " + path); String[] keyCols = collPersister.getKeyColumnNames(); JoinFragment ojf = q.createJoinFragment(useThetaStyleJoin); ojf.addCrossJoin( collPersister.getTableName(), collectionName ); ojf.addFromFragmentString( join.toFromFragmentString() ); if ( collPersister.isOneToMany() ) { Queryable persister = (Queryable) collPersister.getElementPersister(); ojf.addJoins( persister.fromJoinFragment(collectionName, true, false), persister.whereJoinFragment(collectionName, true, false) ); } if (!continuation) addJoin( collPersister.getTableName(), collectionName, keyCols ); join.addCondition(collectionName, indexCols, " = "); String[] eltCols = collPersister.getElementColumnNames(); CollectionElement elem = new CollectionElement(); elem.elementColumns = StringHelper.qualify(collectionName, eltCols); elem.elementType = collPersister.getElementType(); elem.isOneToMany = collPersister.isOneToMany(); elem.alias = collectionName; elem.joinFragment = join; collectionElements.addLast(elem); setExpectingCollectionIndex(); q.addCollection(collectionName, collectionRole); q.addJoin(collectionName, ojf); } static final class CollectionElement { Type elementType; boolean isOneToMany; String alias; String[] elementColumns; JoinFragment joinFragment; StringBuffer indexValue = new StringBuffer(); } private boolean expectingCollectionIndex; private LinkedList collectionElements = new LinkedList(); public CollectionElement lastCollectionElement() { return (CollectionElement) collectionElements.removeLast(); } public void setLastCollectionElementIndexValue(String value) { ( (CollectionElement) collectionElements.getLast() ).indexValue.append(value) ; } public boolean isExpectingCollectionIndex() { return expectingCollectionIndex; } protected void setExpectingCollectionIndex() throws QueryException { expectingCollectionIndex = true; } public JoinFragment getWhereJoin() { return join; } public String getWhereColumn() throws QueryException { if (columns.length!=1) throw new QueryException("path expression ends in a composite value: " + path); return columns[0]; } public String[] getWhereColumns() { return columns; } public Type getWhereColumnType() { return type; } public String getName() { return currentName==null ? collectionName : currentName; } public String getCollectionSubquery() throws QueryException { //TODO: refactor to .sql package return new StringBuffer( "select " ) .append( StringHelper.join( ", ", currentColumns() ) ) .append(" from ") /*.append(collectionTable) .append(' ') .append(collectionName)*/ .append( join.toFromFragmentString().substring(2) ) // remove initial ", " .append(" where ") .append( join.toWhereFragmentString().substring(5) ) // remove initial " and " .toString(); } public boolean isCollectionValued() throws QueryException { //TODO: is there a better way? return collectionName!=null && !getPropertyType().isPersistentCollectionType(); } public void addAssociation(QueryTranslator q) { q.addJoin( getName(), join ); } public String addFromAssociation(QueryTranslator q) throws QueryException { if ( isCollectionValued() ) { return addFromCollection(q); } else { q.addFrom(currentName, join); return currentName; } } public String addFromCollection(QueryTranslator q) throws QueryException { Type collectionElementType = getPropertyType(); if ( collectionElementType==null ) throw new QueryException( "must specify 'elements' for collection valued property in from clause: " + path ); if ( collectionElementType.isEntityType() ) { // an association QueryableCollection collectionPersister = q.getCollectionPersister(collectionRole); Queryable entityPersister = (Queryable) collectionPersister.getElementPersister(); Class clazz = entityPersister.getMappedClass(); String[] collectionElementColumns = currentColumns(); final 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); String[] keyColumnNames = entityPersister.getIdentifierColumnNames(); join.addJoin( entityPersister.getTableName(), elementName, collectionElementColumns, keyColumnNames, joinType); } q.addFrom(elementName, clazz, join); currentPropertyMapping = new CollectionPropertyMapping(collectionPersister); return elementName; } else { // collections of values q.addFromCollection(collectionName, collectionRole, join); return collectionName; } } String getCollectionName() { return collectionName; } String getCollectionRole() { return collectionRole; } String getCollectionOwnerName() { return collectionOwnerName; } String getOneToOneOwnerName() { return oneToOneOwnerName; } String getCurrentProperty() { return currentProperty; } String getCurrentName() { return currentName; } public void fetch(QueryTranslator q, String entityName) throws QueryException { if ( isCollectionValued() ) { q.setCollectionToFetch( getCollectionRole(), getCollectionName(), getCollectionOwnerName(), entityName ); } else { q.addEntityToFetch( entityName, getOneToOneOwnerName() ); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -