queryimpl.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 1,262 行 · 第 1/3 页
JAVA
1,262 行
if (rs.next()) value = rs.getObject(1); else // jpa/1004 throw new NoResultException("Query returned no results for getSingleResult()"); // jpa/1005 if (rs.next()) throw new NonUniqueResultException("Query returned more than one result for getSingleResult()"); return value; } catch (RuntimeException e) { throw e; } catch (Exception e) { throw EJBExceptionWrapper.createRuntime(e); } finally { if (rs != null) { try { rs.close(); } catch (SQLException e) { log.log(Level.FINE, e.toString(), e); } // jpa/0h19 if (! _aConn.isActiveTransaction()) _aConn.detach(); } } } /** * Execute an update or delete. */ public int executeUpdate() { try { // jpa/1006 if (isSelectQuery()) throw new IllegalStateException(L.l("javax.persistence.Query.executeUpdate() cannot be applied to a SELECT statement")); if (_flushMode == FlushModeType.AUTO) _aConn.flushNoChecks(); return _userQuery.executeUpdate(); } catch (Exception e) { throw EJBExceptionWrapper.createRuntime(e); } } /** * Executes the query returning a result set. */ protected ResultSet executeQuery() throws SQLException { ResultSet rs = null; PreparedStatement pstmt = null; try { if (_flushMode == FlushModeType.AUTO) _aConn.flushNoChecks(); if (_nativeSql == null) { // JPA query. rs = _userQuery.executeQuery(); } else { // Native query stmt is prepared in setNativeSql(). rs = _nativeStmt.executeQuery(); } } catch (SQLException e) { if (rs != null) rs.close(); if (pstmt != null) _aConn.closeStatement(_nativeSql); throw e; } return rs; } /** * Sets the maximum result returned. */ public Query setMaxResults(int maxResults) { if (maxResults < 0) throw new IllegalArgumentException(L.l("setMaxResults() needs a non-negative argument, '{0}' is not allowed", maxResults)); _userQuery.setMaxResults(maxResults); return this; } /** * Sets the position of the first result. */ public Query setFirstResult(int startPosition) { if (startPosition < 0) throw new IllegalArgumentException("setFirstResult() requires a non-negative argument"); _userQuery.setFirstResult(startPosition); return this; } /** * Sets a hint. */ public Query setHint(String hintName, Object value) { return this; } /** * Sets a named parameter. */ public Query setParameter(String name, Object value) { ArrayList<String> mapping = _query.getPreparedMapping(); int n = mapping.size(); boolean found = false; for (int i=0; i < n; i++) { if (mapping.get(i).equals(name)) { // jpa/10ee ArgExpr args[] = _userQuery.getQuery().getArgList(); setInternalParameter(args[i], i+1, value); found = true; } } if (! found) throw new IllegalArgumentException(L.l("Parameter name '{0}' is invalid", name)); return this; } /** * Sets a date parameter. */ public Query setParameter(String name, Date value, TemporalType type) { ArrayList<String> mapping = _query.getPreparedMapping(); int n = mapping.size(); boolean found = false; for (int i=0; i < n; i++) { if (mapping.get(i).equals(name)) { setParameter(i+1, value, type); found = true; } } if (! found) throw new IllegalArgumentException(L.l("Parameter name '{0}' is invalid", name)); return this; } /** * Sets a calendar parameter. */ public Query setParameter(String name, Calendar value, TemporalType type) { ArrayList<String> mapping = _query.getPreparedMapping(); int n = mapping.size(); boolean found = false; for (int i=0; i < n; i++) { if (mapping.get(i).equals(name)) { setParameter(i+1, value, type); found = true; } } if (! found) throw new IllegalArgumentException(L.l("Parameter name '{0}' is invalid", name)); return this; } /** * Sets an indexed parameter. */ public Query setParameter(int index, Object value) { if (_nativeSql == null) { // jpa/141h ArgExpr arg = checkParameterIndex(index); setInternalParameter(arg, index, value); } else { setInternalParameter(_nativeStmt, null, index, value); } return this; } /** * Sets a date parameter. */ public Query setParameter(int index, Date value, TemporalType type) { try { checkParameterIndex(index); if (value == null) _userQuery.setNull(index, Types.JAVA_OBJECT); else { switch (type) { case TIME: _userQuery.setObject(index, value, UtilDateType.TEMPORAL_TIME_TYPE); break; case DATE: _userQuery.setObject(index, value, UtilDateType.TEMPORAL_DATE_TYPE); break; default: _userQuery.setObject(index, value, UtilDateType.TEMPORAL_TIMESTAMP_TYPE); } } return this; } catch (IndexOutOfBoundsException e) { throw new IllegalArgumentException(L.l("Parameter index '{0}' is not valid for setParameter()", index)); } } /** * Sets a calendar parameter. */ public Query setParameter(int index, Calendar value, TemporalType type) { try { checkParameterIndex(index); if (value == null) _userQuery.setNull(index, Types.JAVA_OBJECT); else { switch (type) { case TIME: _userQuery.setObject(index, value, CalendarType.TEMPORAL_TIME_TYPE); break; case DATE: _userQuery.setObject(index, value, CalendarType.TEMPORAL_DATE_TYPE); break; default: _userQuery.setObject(index, value, CalendarType.TEMPORAL_TIMESTAMP_TYPE); } } return this; } catch (IndexOutOfBoundsException e) { throw new IllegalArgumentException(L.l("Parameter index '{0}' is not valid for setParameter()", index)); } } /** * Sets the flush mode type. */ public Query setFlushMode(FlushModeType mode) { _flushMode = mode; return this; } // // extensions /** * Sets an indexed parameter. */ public Query setDouble(int index, double value) { _userQuery.setDouble(index, value); return this; } /** * Sets the sql for native queries. */ protected void setNativeSql(String sql) throws SQLException { _nativeSql = sql; _nativeStmt = _aConn.prepareStatement(sql); } /** * Sets the sql result set mapping for native queries. */ protected void setSqlResultSetMapping(SqlResultSetMappingConfig map) { _sqlResultSetMapping = map; } /** * Returns true for SELECT queries. */ private boolean isSelectQuery() { if (_query instanceof SelectQuery) return true; if (isNativeQuery()) { String sql = _nativeSql.trim().toUpperCase(); if (sql.startsWith("SELECT")) return true; } return false; } /** * Returns true for native queries. */ private boolean isNativeQuery() { return _nativeSql != null; } /** * Creates an error. */ private AmberException error(String msg) { msg += "\nin \"" + _query.getQueryString() + "\""; return new AmberException(msg); } /** * Native queries. Returns the object using the * result set mapping. */ private Object getInternalNative(ResultSet rs) throws Exception { // jpa/0y1- int oldEntityResult = _currEntityResult; ArrayList<EntityResultConfig> entityResults = _sqlResultSetMapping.getEntityResults(); if (oldEntityResult == entityResults.size()) { ArrayList<ColumnResultConfig> columnResults = _sqlResultSetMapping.getColumnResults(); int oldColumnResult = _currColumnResult; if (_currColumnResult == columnResults.size()) { _currColumnResult = 0; } // jpa/0y19 if (entityResults.size() == 0 || oldColumnResult < columnResults.size()) { _currColumnResult++; if (columnResults.size() > 0) { Object object = rs.getObject(_currIndex++); return object; } } oldEntityResult = 0; _currEntityResult = 0; } _currEntityResult++; EntityResultConfig entityResult = entityResults.get(oldEntityResult); String className = entityResult.getEntityClass(); EntityType entityType = _aConn.getPersistenceUnit().getEntityType(className); if (entityType == null) throw new IllegalStateException(L.l("Unable to locate entity '{0}' for native query.", className)); int oldIndex = _currIndex; // jpa/0y15 _currIndex++; /* jpa/0y14 EntityItem item = entityType.getHome().findItem(_aConn, rs, oldIndex); if (item == null) return null; Entity entity = item.getEntity(); */ int keyLength = entityType.getId().getKeyCount();
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?