Search in sources :

Example 26 with RepositoryConnection

use of org.eclipse.rdf4j.repository.RepositoryConnection in project rdf4j by eclipse.

the class ContextAwareConnectionTest method testQuery.

@Test
public void testQuery() throws Exception {
    RepositoryConnection stub = new RepositoryConnectionStub() {

        @Override
        public Query prepareQuery(QueryLanguage ql, String query, String baseURI) throws MalformedQueryException, RepositoryException {
            assertEquals(SPARQL, ql);
            assertEquals(queryString, query);
            return new QueryStub() {

                @Override
                public void setDataset(Dataset dataset) {
                    Set<IRI> contexts = Collections.singleton(context);
                    assertEquals(contexts, dataset.getDefaultGraphs());
                    super.setDataset(dataset);
                }
            };
        }
    };
    Repository repo = stub.getRepository();
    ContextAwareConnection con = new ContextAwareConnection(repo, stub);
    con.setReadContexts(context);
    con.setQueryLanguage(SERQL);
    con.prepareQuery(SPARQL, queryString, null);
}
Also used : RepositoryConnection(org.eclipse.rdf4j.repository.RepositoryConnection) IRI(org.eclipse.rdf4j.model.IRI) Repository(org.eclipse.rdf4j.repository.Repository) Dataset(org.eclipse.rdf4j.query.Dataset) QueryLanguage(org.eclipse.rdf4j.query.QueryLanguage) Test(org.junit.Test)

Example 27 with RepositoryConnection

use of org.eclipse.rdf4j.repository.RepositoryConnection in project rdf4j by eclipse.

the class InterceptorTest method testRemove.

@Test
public void testRemove() throws Exception {
    ValueFactory vf = SimpleValueFactory.getInstance();
    final IRI uri = vf.createIRI("http://example.com/");
    final RepositoryConnection stub = new RepositoryConnectionStub() {

        protected void removeWithoutCommit(Resource subject, IRI predicate, Value object, Resource... contexts) throws RepositoryException {
            fail();
        }
    };
    Repository repo = stub.getRepository();
    InterceptingRepositoryConnection con = new InterceptingRepositoryConnectionWrapper(repo, stub);
    con.addRepositoryConnectionInterceptor(new RepositoryConnectionInterceptorAdapter() {

        public boolean remove(RepositoryConnection conn, Resource subject, IRI predicate, Value object, Resource... contexts) {
            assertEquals(stub, conn);
            assertEquals(uri, subject);
            assertEquals(uri, predicate);
            assertEquals(uri, object);
            assertEquals(0, contexts.length);
            return true;
        }
    });
    con.remove(con.getValueFactory().createStatement(uri, uri, uri));
}
Also used : RepositoryConnection(org.eclipse.rdf4j.repository.RepositoryConnection) IRI(org.eclipse.rdf4j.model.IRI) Repository(org.eclipse.rdf4j.repository.Repository) Resource(org.eclipse.rdf4j.model.Resource) Value(org.eclipse.rdf4j.model.Value) RepositoryConnectionInterceptorAdapter(org.eclipse.rdf4j.repository.event.base.RepositoryConnectionInterceptorAdapter) ValueFactory(org.eclipse.rdf4j.model.ValueFactory) SimpleValueFactory(org.eclipse.rdf4j.model.impl.SimpleValueFactory) InterceptingRepositoryConnectionWrapper(org.eclipse.rdf4j.repository.event.base.InterceptingRepositoryConnectionWrapper) Test(org.junit.Test)

Example 28 with RepositoryConnection

use of org.eclipse.rdf4j.repository.RepositoryConnection in project rdf4j by eclipse.

the class NotifyingTest method testUpdate.

@Test
public void testUpdate() throws Exception {
    final Update updateStub = new UpdateStub();
    final RepositoryConnection stub = new RepositoryConnectionStub() {

        public Update prepareUpdate(QueryLanguage ql, String query, String baseURI) throws MalformedQueryException, RepositoryException {
            return updateStub;
        }
    };
    Repository repo = stub.getRepository();
    NotifyingRepositoryConnection con = new NotifyingRepositoryConnectionWrapper(repo, stub);
    con.addRepositoryConnectionListener(new RepositoryConnectionListenerAdapter() {

        public void execute(RepositoryConnection conn, QueryLanguage ql, String update, String baseURI, Update operation) {
            assertEquals(stub, conn);
            assertEquals(SPARQL, ql);
            assertEquals("DELETE DATA { <> <> <> }", update);
            assertEquals("http://example.com/", baseURI);
            assertEquals(updateStub, operation);
        }
    });
    Update update = con.prepareUpdate(SPARQL, "DELETE DATA { <> <> <> }", "http://example.com/");
    update.execute();
}
Also used : RepositoryConnection(org.eclipse.rdf4j.repository.RepositoryConnection) Repository(org.eclipse.rdf4j.repository.Repository) NotifyingRepositoryConnectionWrapper(org.eclipse.rdf4j.repository.event.base.NotifyingRepositoryConnectionWrapper) QueryLanguage(org.eclipse.rdf4j.query.QueryLanguage) AbstractUpdate(org.eclipse.rdf4j.query.impl.AbstractUpdate) Update(org.eclipse.rdf4j.query.Update) RepositoryConnectionListenerAdapter(org.eclipse.rdf4j.repository.event.base.RepositoryConnectionListenerAdapter) Test(org.junit.Test)

Example 29 with RepositoryConnection

use of org.eclipse.rdf4j.repository.RepositoryConnection in project rdf4j by eclipse.

the class RepositoryConfigUtil method getRepositoryIDs.

@Deprecated
public static Set<String> getRepositoryIDs(Repository repository) throws RepositoryException {
    RepositoryConnection con = repository.getConnection();
    try {
        Set<String> idSet = new LinkedHashSet<String>();
        RepositoryResult<Statement> idStatementIter = con.getStatements(null, REPOSITORYID, null, true);
        try {
            while (idStatementIter.hasNext()) {
                Statement idStatement = idStatementIter.next();
                if (idStatement.getObject() instanceof Literal) {
                    Literal idLiteral = (Literal) idStatement.getObject();
                    idSet.add(idLiteral.getLabel());
                }
            }
        } finally {
            idStatementIter.close();
        }
        return idSet;
    } finally {
        con.close();
    }
}
Also used : RepositoryConnection(org.eclipse.rdf4j.repository.RepositoryConnection) LinkedHashSet(java.util.LinkedHashSet) Statement(org.eclipse.rdf4j.model.Statement) Literal(org.eclipse.rdf4j.model.Literal)

Example 30 with RepositoryConnection

use of org.eclipse.rdf4j.repository.RepositoryConnection in project rdf4j by eclipse.

the class RepositoryConfigUtil method removeRepositoryConfigs.

/**
 * Removes one or more Repository configurations from a Repository. Nothing happens when this Repository
 * does not contain configurations for these Repository IDs.
 *
 * @param repository
 *        The Repository to remove the configurations from.
 * @param repositoryIDs
 *        The IDs of the Repositories whose configurations need to be removed.
 * @throws RepositoryException
 *         Whenever access to the Repository's RepositoryConnection causes a RepositoryException.
 * @throws RepositoryConfigException
 */
@Deprecated
public static boolean removeRepositoryConfigs(Repository repository, String... repositoryIDs) throws RepositoryException, RepositoryConfigException {
    boolean changed = false;
    RepositoryConnection con = repository.getConnection();
    try {
        con.begin();
        for (String id : repositoryIDs) {
            Resource context = getContext(con, id);
            if (context != null) {
                con.clear(context);
                con.remove(context, RDF.TYPE, REPOSITORY_CONTEXT);
                changed = true;
            }
        }
        con.commit();
    } finally {
        con.close();
    }
    return changed;
}
Also used : RepositoryConnection(org.eclipse.rdf4j.repository.RepositoryConnection) Resource(org.eclipse.rdf4j.model.Resource)

Aggregations

RepositoryConnection (org.eclipse.rdf4j.repository.RepositoryConnection)34 Test (org.junit.Test)21 Repository (org.eclipse.rdf4j.repository.Repository)18 IRI (org.eclipse.rdf4j.model.IRI)6 Resource (org.eclipse.rdf4j.model.Resource)6 Statement (org.eclipse.rdf4j.model.Statement)6 Model (org.eclipse.rdf4j.model.Model)5 QueryLanguage (org.eclipse.rdf4j.query.QueryLanguage)5 RepositoryException (org.eclipse.rdf4j.repository.RepositoryException)4 RepositoryConfig (org.eclipse.rdf4j.repository.config.RepositoryConfig)4 Value (org.eclipse.rdf4j.model.Value)3 TreeModel (org.eclipse.rdf4j.model.impl.TreeModel)3 Dataset (org.eclipse.rdf4j.query.Dataset)3 ProxyRepositoryConfig (org.eclipse.rdf4j.repository.sail.config.ProxyRepositoryConfig)3 SailRepositoryConfig (org.eclipse.rdf4j.repository.sail.config.SailRepositoryConfig)3 ValueFactory (org.eclipse.rdf4j.model.ValueFactory)2 SimpleValueFactory (org.eclipse.rdf4j.model.impl.SimpleValueFactory)2 Update (org.eclipse.rdf4j.query.Update)2 AbstractUpdate (org.eclipse.rdf4j.query.impl.AbstractUpdate)2 InterceptingRepositoryConnectionWrapper (org.eclipse.rdf4j.repository.event.base.InterceptingRepositoryConnectionWrapper)2