/** * Builds a Set of all table names that are touched by a given query. * * @param q the Query * @param schema the DatabaseSchema in which to look up metadata * @return a Set of table names * @throws ObjectStoreException if something goes wrong */ public static Set<String> findTableNames(Query q, DatabaseSchema schema) throws ObjectStoreException { Set<Object> retvalO = findTableNames(q, schema, false); // If the last argument is false, we know that the result only contains Strings. Set<String> retval = (Set) retvalO; return retval; }
findTableNames(tablenames, subQ, schema, false, individualOsbs); } else if (fromElement instanceof QueryClassBag) { Collection<ProxyReference> empty = Collections.singleton(new ProxyReference(null, new Integer(1), InterMineObject.class)); findTableNames(tablenames, ((QueryCollectionPathExpression) selectable) .getQuery(empty), schema, addInterMineObject, individualOsbs); } else if (selectable instanceof QueryObjectPathExpression) { Collection<Integer> empty = Collections.singleton(new Integer(1)); findTableNames(tablenames, ((QueryObjectPathExpression) selectable) .getQuery(empty, schema.isMissingNotXml()), schema, addInterMineObject, individualOsbs); } else if (selectable instanceof PathExpressionField) { Collection<Integer> empty = Collections.singleton(new Integer(1)); findTableNames(tablenames, ((PathExpressionField) selectable).getQope() .getQuery(empty, schema.isMissingNotXml()), schema, addInterMineObject, individualOsbs);
/** * Builds a Set of all table names that are touched by a given query. * * @param q the Query * @param schema the DatabaseSchema in which to look up metadata * @return a Set of table names * @throws ObjectStoreException if something goes wrong */ public static Set<String> findTableNames(Query q, DatabaseSchema schema) throws ObjectStoreException { Set<Object> retvalO = findTableNames(q, schema, false); // If the last argument is false, we know that the result only contains Strings. Set<String> retval = (Set) retvalO; return retval; }
findTableNames(tablenames, subQ, schema, false, individualOsbs); } else if (fromElement instanceof QueryClassBag) { Collection<ProxyReference> empty = Collections.singleton(new ProxyReference(null, new Integer(1), InterMineObject.class)); findTableNames(tablenames, ((QueryCollectionPathExpression) selectable) .getQuery(empty), schema, addInterMineObject, individualOsbs); } else if (selectable instanceof QueryObjectPathExpression) { Collection<Integer> empty = Collections.singleton(new Integer(1)); findTableNames(tablenames, ((QueryObjectPathExpression) selectable) .getQuery(empty, schema.isMissingNotXml()), schema, addInterMineObject, individualOsbs); } else if (selectable instanceof PathExpressionField) { Collection<Integer> empty = Collections.singleton(new Integer(1)); findTableNames(tablenames, ((PathExpressionField) selectable).getQope() .getQuery(empty, schema.isMissingNotXml()), schema, addInterMineObject, individualOsbs);
/** * {@inheritDoc} */ @Override public Set<Object> getComponentsForQuery(Query q) { try { return SqlGenerator.findTableNames(q, getSchema(), true); } catch (ObjectStoreException e) { throw new RuntimeException(e); } }
findTableNames(tablenames, ((SubqueryConstraint) c).getQuery(), schema, false, individualOsbs); } else if (c instanceof SubqueryExistsConstraint) { findTableNames(tablenames, ((SubqueryExistsConstraint) c).getQuery(), schema, false, individualOsbs); } else if (c instanceof ContainsConstraint) {
/** * {@inheritDoc} */ @Override public Set<Object> getComponentsForQuery(Query q) { try { return SqlGenerator.findTableNames(q, getSchema(), true); } catch (ObjectStoreException e) { throw new RuntimeException(e); } }
findTableNames(tablenames, ((SubqueryConstraint) c).getQuery(), schema, false, individualOsbs); } else if (c instanceof SubqueryExistsConstraint) { findTableNames(tablenames, ((SubqueryExistsConstraint) c).getQuery(), schema, false, individualOsbs); } else if (c instanceof ContainsConstraint) {
/** * Builds a Set of all table names that are touched by a given query. * * @param q the Query * @param schema the DatabaseSchema in which to look up metadata * @param individualOsbs if true, adds individual ObjectStoreBags to the Set, otherwise just * adds the table name instead * @return a Set of table names * @throws ObjectStoreException if something goes wrong */ public static Set<Object> findTableNames(Query q, DatabaseSchema schema, boolean individualOsbs) throws ObjectStoreException { Map<Query, Set<Object>> schemaCache = getTablenamesCacheForSchema(schema); synchronized (q) { Set<Object> tablenames = schemaCache.get(q); if (tablenames == null) { tablenames = new HashSet<Object>(); findTableNames(tablenames, q, schema, true, individualOsbs); schemaCache.put(q, tablenames); } return tablenames; } }
/** * Builds a Set of all table names that are touched by a given query. * * @param q the Query * @param schema the DatabaseSchema in which to look up metadata * @param individualOsbs if true, adds individual ObjectStoreBags to the Set, otherwise just * adds the table name instead * @return a Set of table names * @throws ObjectStoreException if something goes wrong */ public static Set<Object> findTableNames(Query q, DatabaseSchema schema, boolean individualOsbs) throws ObjectStoreException { Map<Query, Set<Object>> schemaCache = getTablenamesCacheForSchema(schema); synchronized (q) { Set<Object> tablenames = schemaCache.get(q); if (tablenames == null) { tablenames = new HashSet<Object>(); findTableNames(tablenames, q, schema, true, individualOsbs); schemaCache.put(q, tablenames); } return tablenames; } }
/** * {@inheritDoc} */ @Override public void addToBagFromQuery(ObjectStoreBag osb, Query query) throws ObjectStoreException { List<QuerySelectable> select = query.getSelect(); if (select.size() != 1) { throw new IllegalArgumentException("Query has incorrect number of SELECT elements."); } Class<?> type = select.get(0).getType(); if (!(Integer.class.equals(type) || InterMineObject.class.isAssignableFrom(type))) { throw new IllegalArgumentException("The type of the result colum (" + type.getName() + ") is not an Integer or InterMineObject"); } try { Connection c = null; try { c = getConnection(); Set<String> readTables = SqlGenerator.findTableNames(query, getSchema()); readTables.add(INT_BAG_TABLE_NAME); batch.flush(c, readTables); addToBagFromQueryWithConnection(c, osb, query); } finally { releaseConnection(c); } } catch (SQLException e) { throw new ObjectStoreException("Could not get connection to database", e); } }
/** * {@inheritDoc} */ @Override public void addToBagFromQuery(ObjectStoreBag osb, Query query) throws ObjectStoreException { List<QuerySelectable> select = query.getSelect(); if (select.size() != 1) { throw new IllegalArgumentException("Query has incorrect number of SELECT elements."); } Class<?> type = select.get(0).getType(); if (!(Integer.class.equals(type) || InterMineObject.class.isAssignableFrom(type))) { throw new IllegalArgumentException("The type of the result colum (" + type.getName() + ") is not an Integer or InterMineObject"); } try { Connection c = null; try { c = getConnection(); Set<String> readTables = SqlGenerator.findTableNames(query, getSchema()); readTables.add(INT_BAG_TABLE_NAME); batch.flush(c, readTables); addToBagFromQueryWithConnection(c, osb, query); } finally { releaseConnection(c); } } catch (SQLException e) { throw new ObjectStoreException("Could not get connection to database", e); } }
/** * {@inheritDoc} * * This method is overridden in order to flush batches properly before the read. */ @Override public int count(Query q, Map<Object, Integer> sequence) throws ObjectStoreException { Connection c = null; try { c = getConnection(); Set<String> readTables = SqlGenerator.findTableNames(q, getSchema()); batch.flush(c, readTables); return countWithConnection(c, q, sequence); } catch (SQLException e) { throw new ObjectStoreException("Could not get connection to database", e); } finally { releaseConnection(c); } }
/** * {@inheritDoc} * * This method is overridden in order to flush batches properly before the read. */ @Override public List<ResultsRow<Object>> execute(Query q, int start, int limit, boolean optimise, boolean explain, Map<Object, Integer> sequence) throws ObjectStoreException { Connection c = null; try { c = getConnection(); Set<String> readTables = SqlGenerator.findTableNames(q, getSchema()); batch.flush(c, readTables); return executeWithConnection(c, q, start, limit, optimise, explain, sequence); } catch (SQLException e) { throw new ObjectStoreException("Could not get connection to database", e); } finally { releaseConnection(c); } }
/** * {@inheritDoc} * * This method is overridden in order to flush batches properly before the read. */ @Override public int count(Query q, Map<Object, Integer> sequence) throws ObjectStoreException { Connection c = null; try { c = getConnection(); Set<String> readTables = SqlGenerator.findTableNames(q, getSchema()); batch.flush(c, readTables); return countWithConnection(c, q, sequence); } catch (SQLException e) { throw new ObjectStoreException("Could not get connection to database", e); } finally { releaseConnection(c); } }
/** * {@inheritDoc} * * This method is overridden in order to flush batches properly before the read. */ @Override public List<ResultsRow<Object>> execute(Query q, int start, int limit, boolean optimise, boolean explain, Map<Object, Integer> sequence) throws ObjectStoreException { Connection c = null; try { c = getConnection(); Set<String> readTables = SqlGenerator.findTableNames(q, getSchema()); batch.flush(c, readTables); return executeWithConnection(c, q, start, limit, optimise, explain, sequence); } catch (SQLException e) { throw new ObjectStoreException("Could not get connection to database", e); } finally { releaseConnection(c); } }
assertEquals(results2.get(type), SqlGenerator.findTableNames(q, getSchema(), false));
public void testInvalidClass() throws Exception { Query q = new Query(); QueryClass c1 = new QueryClass(Company.class); q.addFrom(c1); q.addToSelect(c1); DatabaseSchema s = new DatabaseSchema(new Model("nothing", "", new HashSet()), Collections.EMPTY_LIST, false, Collections.EMPTY_SET, 1); try { SqlGenerator.generate(q, 0, Integer.MAX_VALUE, s, db, new HashMap()); fail("Expected: ObjectStoreException"); } catch (ObjectStoreException e) { assertEquals("interface org.intermine.model.testmodel.Company is not in the model", e.getMessage()); } try { SqlGenerator.findTableNames(q, s, false); fail("Expected: ObjectStoreException"); } catch (ObjectStoreException e) { assertEquals("interface org.intermine.model.testmodel.Company is not in the model", e.getMessage()); } }
public void testInvalidFromElement() throws Exception { Query q = new Query(); FromElement fe = new FromElement() {}; q.addFrom(fe); QueryClass qc = new QueryClass(Company.class); q.addFrom(qc); q.addToSelect(qc); try { SqlGenerator.generate(q, 0, Integer.MAX_VALUE, getSchema(), db, new HashMap()); fail("Expected: ObjectStoreException"); } catch (ObjectStoreException e) { assertTrue(e.getMessage().startsWith("Unknown FromElement: ")); } try { SqlGenerator.findTableNames(q, getSchema(), false); fail("Expected: ObjectStoreException"); } catch (ObjectStoreException e) { assertTrue(e.getMessage().startsWith("Unknown FromElement: ")); } }
public void testInvalidConstraintType() throws Exception { Query q = new Query(); QueryClass qc = new QueryClass(Company.class); q.addFrom(qc); q.addToSelect(qc); q.setConstraint(new Constraint() {}); try { SqlGenerator.generate(q, 0, Integer.MAX_VALUE, getSchema(), db, new HashMap()); fail("Expected: ObjectStoreException"); } catch (ObjectStoreException e) { assertTrue(e.getMessage(), e.getMessage().startsWith("Unrecognised object ")); } try { SqlGenerator.findTableNames(q, getSchema(), false); fail("Expected: ObjectStoreException"); } catch (ObjectStoreException e) { assertTrue(e.getMessage(), e.getMessage().startsWith("Unknown constraint ")); } }