public static boolean isOracle(EntityManager em) { return em.getProvider() instanceof OracleDatabaseProvider; }
public final <T extends RawEntity<K>, K> T create(Class<T> type, Map<String, Object> params) { try { return entityManager.create(type, params); } catch (SQLException e) { throw new ActiveObjectsSqlException(entityManager, e); } }
/** * Cleverly overloaded method to return a single entity of the specified type rather than an array in the case where * only one ID is passed. This method meerly delegates the call to the overloaded <code>get</code> method and * functions as syntactical sugar. * * @param type The type of the entity instance to retrieve. * @param key The primary key corresponding to the entity to be retrieved. * @return An entity instance of the given type corresponding to the specified primary key, or <code>null</code> if * the entity does not exist in the database. * @see #get(Class, Object[]) */ public <T extends RawEntity<K>, K> T get(Class<T> type, K key) throws SQLException { return get(type, toArray(key))[0]; }
/** * Get the table name of the given class entity * * @param entityType the class of the entity * @param escape whether or not to escape the table name * @return the table name */ public static String getTableName(EntityManager em, Class<? extends RawEntity<?>> entityType, boolean escape) { final String tableName = em.getProvider().shorten(em.getTableNameConverter().getName(entityType)); return escape ? em.getProvider().withSchema(tableName) : tableName; }
@Override public void evaluate() throws Throwable { before(method); final boolean useTransaction = useTransaction(method); Connection c = null; try { if (useTransaction) { c = entityManager.getProvider().startTransaction(); } base.evaluate(); } finally { if (useTransaction && c != null) { entityManager.getProvider().rollbackTransaction(c); // make it closeable if (c instanceof DelegateConnection) ((DelegateConnection) c).setCloseable(true); // close it closeQuietly(c); } entityManager.flushAll(); after(method); } } };
EntityInfo<T, K> entityInfo = resolveEntityInfo(type); try { conn = provider.getConnection(); final String sql = query.toSQL(entityInfo, provider, getTableNameConverter(), false); final String[] canonicalFields = query.getCanonicalFields(entityInfo); while (res.next()) { final T entity = peer(entityInfo, primaryKeyType.getLogicalType().pullFromDatabase(this, res, primaryKeyClassType, field)); final Map<String, Object> values = new HashMap<String, Object>(); for (String name : canonicalFields) { final EntityProxy<?, ?> proxy = getProxyForEntity(entity); proxy.updateValues(values);
protected String indexName(String tableName, String indexName) { final IndexNameConverter indexNameConverter = entityManager.getNameConverters().getIndexNameConverter(); final DatabaseProvider provider = entityManager.getProvider(); return indexNameConverter.getName(provider.shorten(tableName), provider.shorten(indexName)); }
public final <T extends RawEntity<K>, K> T[] find(Class<T> type) { try { return entityManager.find(type); } catch (SQLException e) { throw new ActiveObjectsSqlException(entityManager, e); } }
public static String getFieldName(EntityManager em, Class<? extends RawEntity<?>> entityType, String methodName) { return em.getProvider().shorten(em.getFieldNameConverter().getName(findMethod(entityType, methodName))); }
/** * <p>Returns an array of entities of the specified type corresponding to the * varargs primary keys. If an in-memory reference already exists to a corresponding * entity (of the specified type and key), it is returned rather than creating * a new instance.</p> * * <p>If the entity is known to exist in the database, then no checks are performed * and the method returns extremely quickly. However, for any key which has not * already been verified, a query to the database is performed to determine whether * or not the entity exists. If the entity does not exist, then <code>null</code> * is returned.</p> * * @param type The type of the entities to retrieve. * @param keys The primary keys corresponding to the entities to retrieve. All * keys must be typed according to the generic type parameter of the entity's * {@link RawEntity} inheritence (if inheriting from {@link Entity}, this is <code>Integer</code> * or <code>int</code>). Thus, the <code>keys</code> array is type-checked at compile * time. * @return An array of entities of the given type corresponding with the specified * primary keys. Any entities which are non-existent will correspond to a <code>null</code> * value in the resulting array. */ public <T extends RawEntity<K>, K> T[] get(final Class<T> type, K... keys) throws SQLException { EntityInfo<T, K> entityInfo = resolveEntityInfo(type); final String primaryKeyField = entityInfo.getPrimaryKey().getName(); return get(type, findByPrimaryKey(type, primaryKeyField), keys); }
EntityInfo<T, K> entityInfo = resolveEntityInfo(type); try { conn = provider.getConnection(); final String sql = query.toSQL(entityInfo, provider, getTableNameConverter(), false); ReadOnlyEntityProxy<T, K> proxy = createReadOnlyProxy(entityInfo, primaryKey); T entity = type.cast(Proxy.newProxyInstance(type.getClassLoader(), new Class[]{type}, proxy));
private FieldNameConverter getFieldNameConverter() { return manager.getNameConverters().getFieldNameConverter(); }
/** * Counts all entities of the specified type. This method is actually a delegate for: <code>count(Class<? * extends Entity>, Query)</code> * * @param type The type of the entities which should be counted. * @return The number of entities of the specified type. */ public <K> int count(Class<? extends RawEntity<K>> type) throws SQLException { return count(type, Query.select()); }
/** * <p>Optimised read for large datasets. This method will stream all rows for the given type to the given * callback.</p> * * <p>Please see {@link #stream(Class, Query, EntityStreamCallback)} for details / limitations. * * @param type The type of the entities to retrieve. * @param streamCallback The receiver of the data, will be passed one entity per returned row */ public <T extends RawEntity<K>, K> void stream(Class<T> type, EntityStreamCallback<T, K> streamCallback) throws SQLException { final EntityInfo<T, K> entityInfo = resolveEntityInfo(type); final Set<String> valueFields = getValueFieldsNames(entityInfo, nameConverters.getFieldNameConverter()); final Query query = Query.select(); query.setFields(valueFields.toArray(new String[valueFields.size()])); stream(type, query, streamCallback); }
/** * Counts all entities of the specified type matching the given {@link Query} instance. The SQL runs as a * <code>SELECT COUNT(*)</code> to ensure maximum performance. * * @param type The type of the entities which should be counted. * @param query The {@link Query} instance used to determine the result set which will be counted. * @return The number of entities of the given type which match the specified query. */ public <K> int count(Class<? extends RawEntity<K>> type, Query query) throws SQLException { EntityInfo entityInfo = resolveEntityInfo(type); Connection connection = null; PreparedStatement stmt = null; ResultSet res = null; try { connection = provider.getConnection(); final String sql = query.toSQL(entityInfo, provider, getTableNameConverter(), true); stmt = provider.preparedStatement(connection, sql); provider.setQueryStatementProperties(stmt, query); query.setParameters(this, stmt); res = stmt.executeQuery(); return res.next() ? res.getInt(1) : -1; } finally { closeQuietly(res); closeQuietly(stmt); closeQuietly(connection); } }
EntityInfo<T, K> entityInfo = resolveEntityInfo(type); return find(type, selectField, query);
public final void flushAll() { entityManager.flushAll(); }
protected <T extends RawEntity<K>, K> T[] peer(final EntityInfo<T, K> entityInfo, K... keys) throws SQLException { return get(entityInfo.getEntityType(), new Function<T, K>() { public T invoke(K key) { return getAndInstantiate(entityInfo, key); } }, keys); }
public final <T extends RawEntity<K>, K> void stream(Class<T> type, EntityStreamCallback<T, K> streamCallback) { try { entityManager.stream(type, streamCallback); } catch (SQLException e) { throw new ActiveObjectsSqlException(entityManager, e); } }