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

📄 queryloader.java

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

	/**
	 * @param lockModes a collection of lock modes specified dynamically via the Query interface
	 */
	protected LockMode[] getLockModes(Map lockModes) {

		if ( lockModes==null || lockModes.size()==0 ) {
			return defaultLockModes;
		}
		else {
			// unfortunately this stuff can't be cached because
			// it is per-invocation, not constant for the
			// QueryTranslator instance

			LockMode[] lockModeArray = new LockMode[entityAliases.length];
			for ( int i = 0; i < entityAliases.length; i++ ) {
				LockMode lockMode = (LockMode) lockModes.get( entityAliases[i] );
				if ( lockMode == null ) {
					//NONE, because its the requested lock mode, not the actual! 
					lockMode = LockMode.NONE;
				}
				lockModeArray[i] = lockMode;
			}
			return lockModeArray;
		}
	}

	protected String applyLocks(String sql, Map lockModes, Dialect dialect)
			throws QueryException {

		if ( lockModes == null || lockModes.size() == 0 ) {
			return sql;
		}
		else {
			// can't cache this stuff either (per-invocation)

			//we are given a map of user alias -> lock mode
			//create a new map of sql alias -> lock mode
			final Map aliasedLockModes = new HashMap();
			final Iterator iter = lockModes.entrySet().iterator();
			while ( iter.hasNext() ) {
				Map.Entry me = ( Map.Entry ) iter.next();
				final String userAlias = ( String ) me.getKey();
				final String sqlAlias = (String) sqlAliasByEntityAlias.get( userAlias );
				if (sqlAlias==null) {
					throw new IllegalArgumentException("alias not found: " + userAlias);
				}
				aliasedLockModes.put( sqlAlias, me.getValue() );
			}

			//if necessary, create a map of sql alias -> key columns
			Map keyColumnNames = null;
			if ( dialect.forUpdateOfColumns() ) {
				final Loadable[] persisters = getEntityPersisters();
				keyColumnNames = new HashMap();
				for ( int i = 0; i < sqlAliases.length; i++ ) {
					keyColumnNames.put( sqlAliases[i], persisters[i].getIdentifierColumnNames() );
				}
			}

			return sql + new ForUpdateFragment( dialect, aliasedLockModes, keyColumnNames ).toFragmentString();

		}
	}

	protected boolean upgradeLocks() {
		return true;
	}

	protected Object getResultColumnOrRow(Object[] row, ResultSet rs, SessionImplementor session)
			throws SQLException, HibernateException {

		row = toResultRow( row );
		boolean isHolder = holderInstantiator.isRequired();
		if ( hasScalars ) {
			String[][] scalarColumns = scalarColumnNames;
			int queryCols = queryReturnTypes.length;
			if ( !isHolder && queryCols == 1 ) {
				return queryReturnTypes[0].nullSafeGet( rs, scalarColumns[0], session, null );
			}
			else {
				row = new Object[queryCols];
				for ( int i = 0; i < queryCols; i++ ) {
					row[i] = queryReturnTypes[i].nullSafeGet( rs, scalarColumns[i], session, null );
				}
				return row;
			}
		}
		else if ( !isHolder ) {
			return row.length == 1 ? row[0] : row;
		}
		else {
			return row;
		}

	}

	protected List getResultList(List results) throws QueryException {
		// meant to handle dynamic instantiation queries...
		if ( holderInstantiator.isRequired() ) {
			for ( int i = 0; i < results.size(); i++ ) {
				Object[] row = ( Object[] ) results.get( i );
				Object result = holderInstantiator.instantiate(row);
				results.set( i, result );
			}
		}
		return results;
	}

	// --- Query translator methods ---

	/**
	 * Delegats
	 *
	 * @param session
	 * @param queryParameters
	 * @return
	 * @throws HibernateException
	 */
	public List list(SessionImplementor session, QueryParameters queryParameters)
			throws HibernateException {
		return list( session, queryParameters, queryTranslator.getQuerySpaces(), queryReturnTypes );
	}

	/**
	 * Return the query results as an iterator
	 */
	public Iterator iterate(QueryParameters queryParameters, EventSource session)
			throws HibernateException {

		final boolean stats = session.getFactory().getStatistics().isStatisticsEnabled();
		long startTime = 0;
		if ( stats ) {
			startTime = System.currentTimeMillis();
		}

		try {

			final PreparedStatement st = prepareQueryStatement( queryParameters, false, session );

			if(queryParameters.isCallable()) {
				throw new QueryException("iterate() not supported for callable statements");
			}
			final ResultSet rs = getResultSet(st, queryParameters.hasAutoDiscoverScalarTypes(), false, queryParameters.getRowSelection(), session);
			final Iterator result = new IteratorImpl(
					rs,
			        st,
			        session,
			        queryReturnTypes,
			        queryTranslator.getColumnNames(),
			        holderInstantiator
				);

			if ( stats ) {
				session.getFactory().getStatisticsImplementor().queryExecuted(
//						"HQL: " + queryTranslator.getQueryString(),
						getQueryIdentifier(),
						0,
						System.currentTimeMillis() - startTime
				);
			}

			return result;

		}
		catch ( SQLException sqle ) {
			throw JDBCExceptionHelper.convert(
					getFactory().getSQLExceptionConverter(),
			        sqle,
			        "could not execute query using iterate",
			        getSQLString()
				);
		}

	}

	public ScrollableResults scroll(
			final QueryParameters queryParameters,
	        final SessionImplementor session) throws HibernateException {
		return scroll( queryParameters, queryReturnTypes, holderInstantiator, session );
	}

	// -- Implementation private methods --

	private Object[] toResultRow(Object[] row) {
		if ( selectLength == row.length ) {
			return row;
		}
		else {
			Object[] result = new Object[selectLength];
			int j = 0;
			for ( int i = 0; i < row.length; i++ ) {
				if ( includeInSelect[i] ) {
					result[j++] = row[i];
				}
			}
			return result;
		}
	}

	/**
	 * Returns the locations of all occurrences of the named parameter.
	 */
	public int[] getNamedParameterLocs(String name) throws QueryException {
		return queryTranslator.getParameterTranslations().getNamedParameterSqlLocations( name );
	}

	protected int bindNamedParameters(
			final PreparedStatement ps,
	        final Map namedParams,
	        final int start,
	        final SessionImplementor session) throws SQLException, HibernateException {
		// we override binding of named parameters here because the parser
		// keeps track of named-parameter SQL positions "absolutely"; or at
		// least it thinks it does.  But currently it does not know about filter
		// parameters.  Thus we need to account for the number of filter-injected
		// parameters and adjust the bind positions for named parameters
		// accordingly...
		final int filterParamCountAdjustment = start - queryTranslator.getParameterTranslations().getOrdinalParameterCount();
		if ( namedParams != null ) {
			// assumes that types are all of span 1
			Iterator iter = namedParams.entrySet().iterator();
			int result = 0;
			while ( iter.hasNext() ) {
				Map.Entry e = ( Map.Entry ) iter.next();
				String name = ( String ) e.getKey();
				TypedValue typedval = ( TypedValue ) e.getValue();
				int[] locs = getNamedParameterLocs( name );
				for ( int i = 0; i < locs.length; i++ ) {
					final int location = locs[i] + filterParamCountAdjustment;
					if ( log.isDebugEnabled() ) {
						log.debug(
								"bindNamedParameters() " +
								typedval.getValue() + " -> " + name +
								" [" + location + "]"
							);
					}
					typedval.getType().nullSafeSet( ps, typedval.getValue(), location, session );
				}
				result += locs.length;
			}
			return result;
		}
		else {
			return 0;
		}
	}
}

⌨️ 快捷键说明

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