⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 abstractcollectionpersister.java

📁 hibernate-3.1.3-all-src.zip 面向对象的访问数据库工具
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
	}

	/**
	 * Generate a list of collection index, key and element columns
	 */
	public String selectFragment(String alias, String columnSuffix) {
		SelectFragment frag = generateSelectFragment( alias, columnSuffix );
		appendElementColumns( frag, alias );
		appendIndexColumns( frag, alias );
		appendIdentifierColumns( frag, alias );

		return frag.toFragmentString()
				.substring( 2 ); //strip leading ','
	}

	protected String generateSelectSizeString(boolean isIntegerIndexed) {
		String selectValue = isIntegerIndexed ? 
			"max(" + getIndexColumnNames()[0] + ") + 1": //lists, arrays
			"count(" + getElementColumnNames()[0] + ")"; //sets, maps, bags
		return new SimpleSelect(dialect)
				.setTableName( getTableName() )
				.addCondition( getKeyColumnNames(), "=?" )
				.addColumn(selectValue)
				.toStatementString();
	}

	protected String generateDetectRowByIndexString() {
		if ( !hasIndex() ) return null;
		return new SimpleSelect(dialect)
				.setTableName( getTableName() )
				.addCondition( getKeyColumnNames(), "=?" )
				.addCondition( getIndexColumnNames(), "=?" )
				.addCondition( indexFormulas, "=?" )
				.addColumn("1")
				.toStatementString();
	}

	protected String generateSelectRowByIndexString() {
		if ( !hasIndex() ) return null;
		return new SimpleSelect(dialect)
				.setTableName( getTableName() )
				.addCondition( getKeyColumnNames(), "=?" )
				.addCondition( getIndexColumnNames(), "=?" )
				.addCondition( indexFormulas, "=?" )
				.addColumns( getElementColumnNames(), elementColumnAliases )
				.addColumns( indexFormulas, elementColumnAliases )
				.toStatementString();
	}

	protected String generateDetectRowByElementString() {
		return new SimpleSelect(dialect)
				.setTableName( getTableName() )
				.addCondition( getKeyColumnNames(), "=?" )
				.addCondition( getElementColumnNames(), "=?" )
				.addCondition( elementFormulas, "=?" )
				.addColumn("1")
				.toStatementString();
	}

	protected SelectFragment generateSelectFragment(String alias, String columnSuffix) {
		SelectFragment frag = new SelectFragment()
				.setSuffix( columnSuffix )
				.addColumns( alias, keyColumnNames, keyColumnAliases );
		return frag;
	}

	protected void appendElementColumns(SelectFragment frag, String elemAlias) {
		for ( int i=0; i<elementColumnIsSettable.length; i++ ) {
			if ( elementColumnIsSettable[i] ) {
				frag.addColumn( elemAlias, elementColumnNames[i], elementColumnAliases[i] );
			}
			else {
				frag.addFormula( elemAlias, elementFormulaTemplates[i], elementColumnAliases[i] );
			}
		}
	}

	protected void appendIndexColumns(SelectFragment frag, String alias) {
		if ( hasIndex ) {
			for ( int i=0; i<indexColumnIsSettable.length; i++ ) {
				if ( indexColumnIsSettable[i] ) {
					frag.addColumn( alias, indexColumnNames[i], indexColumnAliases[i] );
				}
				else {
					frag.addFormula( alias, indexFormulaTemplates[i], indexColumnAliases[i] );
				}
			}
		}
	}

	protected void appendIdentifierColumns(SelectFragment frag, String alias) {
		if ( hasIdentifier ) frag.addColumn( alias, identifierColumnName, identifierColumnAlias );
	}

	public String[] getIndexColumnNames() {
		return indexColumnNames;
	}

	public String[] getIndexFormulas() {
		return indexFormulas;
	}

	public String[] getIndexColumnNames(String alias) {
		return qualify(alias, indexColumnNames, indexFormulaTemplates);

	}

	public String[] getElementColumnNames(String alias) {
		return qualify(alias, elementColumnNames, elementFormulaTemplates);
	}
	
	private static final String[] qualify(String alias, String[] columnNames, String[] formulaTemplates) {
		int span = columnNames.length;
		String[] result = new String[span];
		for (int i=0; i<span; i++) {
			if ( columnNames[i]==null ) {
				result[i] = StringHelper.replace( formulaTemplates[i], Template.TEMPLATE, alias );
			}
			else {
				result[i] = StringHelper.qualify( alias, columnNames[i] );
			}
		}
		return result;
	}

	public String[] getElementColumnNames() {
		return elementColumnNames; //TODO: something with formulas...
	}

	public String[] getKeyColumnNames() {
		return keyColumnNames;
	}

	public boolean hasIndex() {
		return hasIndex;
	}

	public boolean isLazy() {
		return isLazy;
	}

	public boolean isInverse() {
		return isInverse;
	}

	public String getTableName() {
		return qualifiedTableName;
	}

	public void remove(Serializable id, SessionImplementor session) throws HibernateException {

		if ( !isInverse && isRowDeleteEnabled() ) {

			if ( log.isDebugEnabled() ) {
				log.debug( 
						"Deleting collection: " + 
						MessageHelper.collectionInfoString( this, id, getFactory() ) 
					);
			}

			// Remove all the old entries

			try {
				int offset = 1;
				PreparedStatement st = null;
				if ( isDeleteCallable() ) {
					CallableStatement callstatement = session.getBatcher()
						.prepareBatchCallableStatement( getSQLDeleteString() );
					callstatement.registerOutParameter( offset++, Types.NUMERIC ); // TODO: should we require users to return number of update rows ?
					st = callstatement;
				}
				else {
					st = session.getBatcher().prepareBatchStatement( getSQLDeleteString() );
				}

				try {
					writeKey( st, id, offset, session );
					session.getBatcher().addToBatch( -1 );
				}
				catch ( SQLException sqle ) {
					session.getBatcher().abortBatch( sqle );
					throw sqle;
				}

				if ( log.isDebugEnabled() ) log.debug( "done deleting collection" );
			}
			catch ( SQLException sqle ) {
				throw JDBCExceptionHelper.convert(
				        sqlExceptionConverter,
				        sqle,
				        "could not delete collection: " + 
				        MessageHelper.collectionInfoString( this, id, getFactory() ),
				        getSQLDeleteString()
					);
			}

		}

	}

	public void recreate(PersistentCollection collection, Serializable id, SessionImplementor session)
			throws HibernateException {

		if ( !isInverse && isRowInsertEnabled() ) {

			if ( log.isDebugEnabled() ) {
				log.debug( 
						"Inserting collection: " + 
						MessageHelper.collectionInfoString( this, id, getFactory() ) 
					);
			}

			try {
				//create all the new entries
				Iterator entries = collection.entries(this);
				if ( entries.hasNext() ) {
					try {
						collection.preInsert( this );
						int i = 0;
						int count = 0;
						while ( entries.hasNext() ) {

							final Object entry = entries.next();
							if ( collection.entryExists( entry, i ) ) {
								int offset = 1;
								PreparedStatement st = null;
								if ( isInsertCallable() ) {
									CallableStatement callstatement = session.getBatcher()
										.prepareBatchCallableStatement( getSQLInsertRowString() );
									callstatement.registerOutParameter( offset++, Types.NUMERIC ); // TODO: should we require users to return number of update rows ?
									st = callstatement;
								}
								else {
									st = session.getBatcher().prepareBatchStatement( getSQLInsertRowString() );
								}
								//TODO: copy/paste from insertRows()
								int loc = writeKey( st, id, offset, session );
								if ( hasIdentifier ) {
									loc = writeIdentifier( st, collection.getIdentifier(entry, i), loc, session );
								}
								if ( hasIndex /*&& !indexIsFormula*/ ) {
									loc = writeIndex( st, collection.getIndex(entry, i, this), loc, session );
								}
								//if ( !elementIsFormula ) {
									loc = writeElement(st, collection.getElement(entry), loc, session );
								//}
								session.getBatcher().addToBatch( 1 );
								collection.afterRowInsert( this, entry, i );
								count++;
							}
							i++;
						}
						if ( log.isDebugEnabled() ) log.debug( "done inserting collection: " + count + " rows inserted" );
					}
					catch ( SQLException sqle ) {
						session.getBatcher().abortBatch( sqle );
						throw sqle;
					}

				}
				else {
					if ( log.isDebugEnabled() ) log.debug( "collection was empty" );
				}
			}
			catch ( SQLException sqle ) {
				throw JDBCExceptionHelper.convert(
				        sqlExceptionConverter,
				        sqle,
				        "could not insert collection: " + 
				        MessageHelper.collectionInfoString( this, id, getFactory() ),
				        getSQLInsertRowString()
					);
			}
		}
	}
	
	protected boolean isRowDeleteEnabled() {
		return true;
	}

	public void deleteRows(PersistentCollection collection, Serializable id, SessionImplementor session)
			throws HibernateException {

		if ( !isInverse && isRowDeleteEnabled() ) {

			if ( log.isDebugEnabled() ) {
				log.debug( 
						"Deleting rows of collection: " + 
						MessageHelper.collectionInfoString( this, id, getFactory() ) 
					);
			}
			
			boolean deleteByIndex = !isOneToMany() && hasIndex && !indexContainsFormula;
			
			try {
				//delete all the deleted entries
				Iterator deletes = collection.getDeletes( this, !deleteByIndex );
				if ( deletes.hasNext() ) {
					int offset = 1;
					int count = 0;
					PreparedStatement st = null;
					if ( isDeleteCallable() ) {
						CallableStatement callstatement = session.getBatcher()
							.prepareBatchCallableStatement( getSQLDeleteRowString() );
						callstatement.registerOutParameter( offset++, Types.NUMERIC ); // TODO: should we require users to return number of update rows ?
						st = callstatement;
					}
					else {
						st = session.getBatcher().prepareBatchStatement( getSQLDeleteRowString() );
					}

					try {
						int i=0;
						while ( deletes.hasNext() ) {
							Object entry = deletes.next();
							int loc = offset;
							if ( hasIdentifier ) {
								loc = writeIdentifier( st, entry, loc, session );
							}
							else {
								//if ( !isOneToMany() ) {
									loc = writeKey( st, id, loc, session );
								//}
								if (deleteByIndex) {
									loc = writeIndexToWhere( st, entry, loc, session );
								}
								else {
									loc = writeElementToWhere( st, entry, loc, session );
								}
							}
							session.getBatcher().addToBatch( -1 );
							count++;
							i++;
						}
					}
					catch ( SQLException sqle ) {
						session.getBatcher().abortBatch( sqle );
						throw sqle;
					}

					if ( log.isDebugEnabled() ) log.debug( "done deleting collection rows: " + count + " deleted" );
				}
				else {
					if ( log.isDebugEnabled() ) log.debug( "no rows to delete" );
				}
			}
			catch ( SQLException sqle ) {
				throw JDBCExceptionHelper.convert(
				        sqlExceptionConverter,
				        sqle,
				        "could not delete collection rows: " + 
				        MessageHelper.collectionInfoString( this, id, getFactory() ),
				        getSQLDeleteRowString()
					);
			}
		}
	}
	
	protected boolean isRowInsertEnabled() {
		return true;
	}

	public void insertRows(PersistentCollection collection, Serializable id, SessionImplementor session)
			throws HibernateException {

		if ( !isInverse && isRowInsertEnabled() ) {

			if ( log.isDebugEnabled() ) {
				log.debug( 
						"Inserting rows of collection: " + 
						MessageHelper.collectionInfoString( this, id, getFactory() ) 
					);
			}

			try {
				//insert all the new entries
				Iterator entries = collection.entries(this);
				boolean callable = isInsertCallable();
				try {
					collection.preInsert( this );
					int i = 0;
					int count = 0;
					int offset = 1;
					while ( entries.hasNext() ) {
						Object entry = entries.next();
						PreparedStatement st = null;
						if ( collection.needsInserting( entry, i, elementType ) ) {
							if ( st == null ) {
								if ( callable ) {
									CallableStatement callstatement = session.getBatcher()
										.prepareBatchCallableStatement( getSQLInsertRowString() );
									callstatement.registerOutParameter( offset++, Types.NUMERIC ); // TODO: should we require users to return number of update rows ?
									st = callstatement;
								}
								else {
									st = session.getBatcher().prepareBatchStatement( getSQLInsertRowString() );

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -