/** * Generate sql from a Query * * @param q the Query * @return an SQL String * @throws ObjectStoreException if something goes wrong */ public String generateSql(Query q) throws ObjectStoreException { Connection c = null; try { c = getConnection(); return generateSql(c, q, 0, Integer.MAX_VALUE); } catch (SQLException e) { throw new ObjectStoreException("Failed to get connection", e); } finally { releaseConnection(c); } }
/** * Sets up the instance variables * * @throws BuildException if something is wrong */ public void setUp() { if (alias == null && objectStore == null) { throw new BuildException("exactly one of alias and objectStore must be set"); } if (objectStore == null) { try { objectStore = ObjectStoreFactory.getObjectStore(alias); } catch (Exception e) { throw new BuildException("Exception while creating ObjectStore", e); } } if (objectStore instanceof ObjectStoreInterMineImpl) { ObjectStoreInterMineImpl osii = ((ObjectStoreInterMineImpl) objectStore); database = osii.getDatabase(); schema = osii.getSchema(); } else { // change comment on setAlias() when this changes throw new BuildException("the alias (" + alias + ") does not refer to an " + "ObjectStoreInterMineImpl"); } }
/** * Precomputes a template query if it is not already precomputed, returning whether precomputing * was necessary. * * @param t the TemplateQuery to precompute * @return true if the template was not already precomputed * @throws ObjectStoreException if there is a problem precomputing */ public boolean precomputeTemplate(TemplateQuery t) throws ObjectStoreException { List<QueryNode> indexes = new ArrayList<QueryNode>(); Query q = TemplatePrecomputeHelper.getPrecomputeQuery(t, indexes); ObjectStoreInterMineImpl osimi = (ObjectStoreInterMineImpl) os; if (!osimi.isPrecomputed(q, "template")) { osimi.precompute(q, indexes, "template"); return true; } return false; }
/** * Create temporary tables for the bag in the BagConstraints of the given Query. * Entries are placed in the bagConstraintTables Map, which is a * WeakHashMap from BagConstraint -> table name. When the BagConstraint is garbage- * collected, or when the JVM exits, the table associated with the table name is dropped from * the database. * * @param c a Connection to use * @param q the Query * @throws ObjectStoreException if an error occurs */ private void createTempBagTablesForQuery(Connection c, Query q) throws ObjectStoreException { if (getMinBagTableSize() != -1) { // We have a strong reference to the Query, and therefore all the BagConstraints. We can // count on the bagConstraintTables Map to be sane. createTempBagTables(c, q); flushOldTempBagTables(c); } }
/** * {@inheritDoc} */ public int count(Query q, Map<Object, Integer> sequence) throws ObjectStoreException { Connection c = null; try { c = getConnection(); return countWithConnection(c, q, sequence); } catch (SQLException e) { throw new ObjectStoreException("Could not get connection to database", e); } finally { releaseConnection(c); } }
/** * {@inheritDoc} */ public ResultsInfo estimate(Query q) throws ObjectStoreException { Connection c = null; try { c = getConnection(); return estimateWithConnection(c, q); } catch (SQLException e) { throw new ObjectStoreException("Could not get connection to database", e); } finally { releaseConnection(c); } }
throws ObjectStoreException { if (explain) { checkStartLimit(start, limit, q); checkSequence(sequence, q, "Execute (START " + start + " LIMIT " + limit + ") "); if (getMinBagTableSize() != -1) { createTempBagTables(c, q); flushOldTempBagTables(c); long startOptimiseTime = System.currentTimeMillis(); ExplainResult explainResult = null; if (optimise && everOptimise()) { if (goFasterTables == null) { goFasterTables = goFasterMap.get(q); if (explainResult.getTime() > getMaxTime()) { throw (new ObjectStoreQueryDurationException("Estimated time to run query(" + explainResult.getTime() + ") greater than permitted maximum (" + getMaxTime() + "): IQL query: " + q + ", SQL query: " + sql)); if (getLogBeforeExecute()) { SQLLOGGER.info("(BEFORE EXECUTE) iql: " + q + "\n" + "generated sql: " + generatedSql + "\n" registerStatement(s); ResultSet sqlResults; try { sqlResults = s.executeQuery(sql);
@Test public void testCancelMethods1() throws Exception { Object id = "flibble1"; Connection c = ((ObjectStoreInterMineImpl) os).getConnection(); try { Statement s = c.createStatement(); ((ObjectStoreInterMineImpl) os).registerRequestId(id); ((ObjectStoreInterMineImpl) os).registerStatement(s); ((ObjectStoreInterMineImpl) os).deregisterStatement(s); ((ObjectStoreInterMineImpl) os).deregisterRequestId(id); } finally { ((ObjectStoreInterMineImpl) os).releaseConnection(c); } }
String sql = null; try { int tableNumber = getUniqueInteger(c); if (getMinBagTableSize() != -1) { createTempBagTables(c, q); flushOldTempBagTables(c); Query subQ = ((QueryCollectionPathExpression) qs).getQuery(null); if (subQ.getConstraint() != null) { retval.addAll(precomputeWithConnection(c, subQ, Collections.singleton((QueryNode) (subQ.getSelect().get(0))), allFields, category)); getSchema().isMissingNotXml()); if (subQ.getConstraint() != null) { retval.addAll(precomputeWithConnection(c, subQ, Collections.singleton((QueryNode) (subQ.getSelect().get(0))), allFields, category));
Connection c = os.getConnection(); c.createStatement().execute("ANALYSE"); os.releaseConnection(c); Query q1 = new Query(); QueryClass qc1 = new QueryClass(Department.class); q1.setDistinct(false); long time1 = System.currentTimeMillis(); Results r1 = os.execute(q1, 1000, true, false, true); int counter = 0; Iterator rowIter = r1.iterator(); long time2 = System.currentTimeMillis(); System.out.println("Access to results took " + (time2 - time1) + " ms"); ((ObjectStoreInterMineImpl) os).precompute(q2, "test"); long time3 = System.currentTimeMillis(); System.out.println("Precomputing took " + (time3 - time2) + " ms"); Results r2 = os.execute(q2, 1000, true, false, true); counter = 0; rowIter = r2.iterator(); toRemove.add(newD); Query q3 = QueryCloner.cloneQuery(q1); Results r3 = os.execute(q3, 1000, true, false, true); counter = 0; rowIter = r3.iterator();
/** Returns profile format version from database. Version 0 corresponds to * situation when constraint values are saved in the internal format. Internal * format is format where '*' are replaced with % and other changes. * @see #Util.wildcardSqlToUser() * @return saved format version or "0" if not saved in database */ private static String getProfileVersion(ObjectStore os) { Database database = ((ObjectStoreInterMineImpl) os).getDatabase(); try { String ret = MetadataManager.retrieve(database, MetadataManager.PROFILE_FORMAT_VERSION); LOG.info("Userprofile database has version \"" + ret + "\""); if (ret == null) { ret = ZERO_PROFILE_VERSION; } return ret; } catch (SQLException e) { throw new RuntimeException("Error during retrieving profile version from database.", e); } }
/** * Creates a temporary bag table for the given BagConstraint. * * @param bagConstraint a BagConstraint * @throws ObjectStoreException if an error occurs */ public void createTempBagTable(ConstraintWithBag bagConstraint) throws ObjectStoreException { Connection c = null; try { c = getConnection(); createTempBagTable(c, bagConstraint, false, null); } catch (SQLException e) { throw new ObjectStoreException("Could not get connection to database", e); } finally { releaseConnection(c); } }
/** * Gets an ID number which is unique in the database. * * @return an Integer * @throws ObjectStoreException if a problem occurs */ public Integer getSerial() throws ObjectStoreException { try { Connection c = null; try { c = getConnection(); return getSerialWithConnection(c); } finally { releaseConnection(c); } } catch (SQLException e) { throw new ObjectStoreException("Error generating serial number", e); } }
/** * Makes a certain Query go faster, using extra resources. The user should release * the resources later by calling releaseGoFaster on the same Query. Failure to release * resources may result in an overall degradation in performance. * * @param q the Query to speed up * @throws ObjectStoreException if something is wrong */ public void goFaster(Query q) throws ObjectStoreException { Connection c = null; try { c = getConnection(); goFasterWithConnection(q, c); } catch (SQLException e) { throw new ObjectStoreException("Could not get connection to database", e); } finally { releaseConnection(c); } }
/** * Checks if a query is precomputed or not for the given type * * @param query the query * @param type the type * @return true if and only if the given query is (already) precomputed * @throws ObjectStoreException if the is a database problem */ public boolean isPrecomputed(Query query, String type) throws ObjectStoreException { Connection c = null; try { c = getConnection(); return isPrecomputedWithConnection(c, query, type); } catch (SQLException e) { throw new ObjectStoreException("Could not get connection to database", e); } finally { releaseConnection(c); } }
/** * {@inheritDoc} * * This method is overridden in order to improve the performance of the operation - this * implementation does not bother with the EXPLAIN call to the underlying SQL database. */ @Override protected InterMineObject internalGetObjectById(Integer id, Class<? extends InterMineObject> clazz) throws ObjectStoreException { if (schema.isFlatMode(clazz)) { return super.internalGetObjectById(id, clazz); } Connection c = null; try { c = getConnection(); return internalGetObjectByIdWithConnection(c, id, clazz); } catch (SQLException e) { throw new ObjectStoreException("Could not get connection to database", e); } finally { releaseConnection(c); } }
/** * Creates precomputed tables for the given query. * * @param q the Query for which to create the precomputed tables * @param indexes a Collection of QueryNodes for which to create indexes * @param allFields true if all fields of QueryClasses in the SELECT list should be included in * the precomputed table's SELECT list. If the indexes parameter is null, then indexes will * be created for every field as well * @param category a String describing the category of the precomputed tables * @return the names of the new precomputed tables * @throws ObjectStoreException if anything goes wrong */ public List<String> precompute(Query q, Collection<? extends QueryNode> indexes, boolean allFields, String category) throws ObjectStoreException { Connection c = null; try { c = getConnection(); return precomputeWithConnection(c, q, indexes, allFields, category); } catch (SQLException e) { throw new ObjectStoreException("Could not get connection to database", e); } finally { releaseConnection(c); } }
List<String> precomputes = objectStoreInterMineImpl.precompute(q, indexes, "test"); String tableName = String.valueOf(precomputes.get(0)); Map indexMap = new HashMap(); try { con = ((ObjectStoreInterMineImpl) os).getConnection(); Statement s = con.createStatement(); ResultSet r = s.executeQuery("SELECT * FROM pg_indexes WHERE tablename = '" + tableName + "'"); ((ObjectStoreInterMineImpl) os).releaseConnection(con);
@Test public void testPrecomputeWithNegatives() throws Exception { Types t1 = new Types(); t1.setLongObjType(new Long(-765187651234L)); t1.setIntObjType(new Integer(278652)); t1.setName("Fred"); storeDataWriter.store(t1); Query q = new Query(); QueryClass qc = new QueryClass(Types.class); QueryField into = new QueryField(qc, "intObjType"); QueryField longo = new QueryField(qc, "longObjType"); q.addFrom(qc); q.addToSelect(into); q.addToSelect(longo); q.addToSelect(qc); q.setDistinct(false); ((ObjectStoreInterMineImpl) os).precompute(q, "test"); Results r = os.execute(q, 1, true, true, true); SqlGenerator.registerOffset(q, 1, ((ObjectStoreInterMineImpl) os).getSchema(), ((ObjectStoreInterMineImpl) os).db, new Integer(278651), new HashMap()); ResultsRow row = (ResultsRow) r.get(1); InterMineObject o = (InterMineObject) row.get(2); Assert.assertEquals("Expected " + t1.toString() + " but got " + o.toString(), t1.getId(), o.getId()); try { r.get(2); Assert.fail("Expected size to be 2"); } catch (Exception e) { } Assert.assertEquals(2, r.size()); storeDataWriter.delete(t1); }
Connection conn = null; PrintStream out = System.out; Database db = ((ObjectStoreInterMineImpl) uos).getDatabase(); try { conn = ((ObjectStoreInterMineImpl) uos).getConnection();