Search in sources :

Example 6 with Quad

use of org.apache.jena.sparql.core.Quad in project jena by apache.

the class TestUpdateOperations method delete_insert_where_01.

// Check constant and template quads 
@Test
public void delete_insert_where_01() {
    DatasetGraph dsg0 = DatasetGraphFactory.create();
    UpdateRequest req = UpdateFactory.create("INSERT DATA { <x> <p> 2 . <z> <q> 2 . <z> <q> 3 . }");
    UpdateAction.execute(req, dsg0);
    assertEquals(3, dsg0.getDefaultGraph().size());
    AtomicLong counterIns = new AtomicLong(0);
    AtomicLong counterDel = new AtomicLong(0);
    DatasetGraph dsg = new DatasetGraphWrapper(dsg0) {

        @Override
        public void add(Quad quad) {
            counterIns.incrementAndGet();
            super.add(quad);
        }

        @Override
        public void delete(Quad quad) {
            counterDel.incrementAndGet();
            super.delete(quad);
        }
    };
    // WHERE clause doubles the effect.
    String s = "DELETE { ?x <p> 2 . <z> <q> 2 } INSERT { ?x <p> 1 . <x> <q> 1  } WHERE { ?x <p> ?o {} UNION {} }";
    req = UpdateFactory.create(s);
    UpdateAction.execute(req, dsg);
    // 3 : 1 constant, 2 from template.
    assertEquals(3, counterIns.get());
    assertEquals(3, counterIns.get());
    assertEquals(3, dsg.getDefaultGraph().size());
}
Also used : Quad(org.apache.jena.sparql.core.Quad) AtomicLong(java.util.concurrent.atomic.AtomicLong) DatasetGraphWrapper(org.apache.jena.sparql.core.DatasetGraphWrapper) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) Test(org.junit.Test) BaseTest(org.apache.jena.atlas.junit.BaseTest)

Example 7 with Quad

use of org.apache.jena.sparql.core.Quad in project jena by apache.

the class AbstractTestUpdateGraph method testModify1.

@Test
public void testModify1() {
    DatasetGraph gStore = getEmptyDatasetGraph();
    defaultGraphData(gStore, data2());
    namedGraphData(gStore, graphIRI, Factory.createDefaultGraph());
    UpdateModify modify = new UpdateModify();
    Element element = QueryFactory.createElement("{ ?s <http://example/p> ?o }");
    modify.setElement(element);
    modify.getInsertAcc().addQuad(new Quad(graphIRI, triple1));
    modify.getDeleteAcc().addTriple(SSE.parseTriple("(?s <http://example/p> ?o)"));
    modify.getDeleteAcc().addQuad(SSE.parseQuad("(<http://example/graph> ?s <http://example/p> ?o)"));
    UpdateAction.execute(modify, gStore);
    assertFalse(graphEmpty(gStore.getGraph(graphIRI)));
    assertTrue(graphEmpty(gStore.getDefaultGraph()));
    assertTrue(graphContains(gStore.getGraph(graphIRI), triple1));
}
Also used : Quad(org.apache.jena.sparql.core.Quad) Element(org.apache.jena.sparql.syntax.Element) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) Test(org.junit.Test)

Example 8 with Quad

use of org.apache.jena.sparql.core.Quad in project jena by apache.

the class AbstractJenaConnectionTests method connection_prepared_statement_select_03.

/**
     * Tests use of prepared statements
     * 
     * @throws SQLException
     */
@Test
public void connection_prepared_statement_select_03() throws SQLException {
    // Prepare a dataset
    Dataset ds = DatasetFactory.createTxnMem();
    ds.asDatasetGraph().add(new Quad(NodeFactory.createURI("http://example/graph"), NodeFactory.createURI("http://example/subject"), NodeFactory.createURI("http://example/predicate"), NodeFactory.createLiteral("value")));
    // Work with the connection
    JenaConnection conn = this.getConnection(ds);
    conn.setJdbcCompatibilityLevel(JdbcCompatibility.HIGH);
    PreparedStatement stmt = conn.prepareStatement("SELECT * WHERE { GRAPH ?g { ?s ?p ? } }");
    ParameterMetaData metadata = stmt.getParameterMetaData();
    Assert.assertEquals(1, metadata.getParameterCount());
    stmt.setNString(1, "value");
    ResultSet rset = stmt.executeQuery();
    Assert.assertNotNull(rset);
    Assert.assertFalse(rset.isClosed());
    Assert.assertTrue(rset.isBeforeFirst());
    // Check result set metadata
    checkSelectMetadata(rset, 3);
    // Should have a row
    Assert.assertTrue(rset.next());
    Assert.assertTrue(rset.isFirst());
    Assert.assertEquals(1, rset.getRow());
    // Should be no further rows
    Assert.assertFalse(rset.next());
    Assert.assertTrue(rset.isAfterLast());
    Assert.assertFalse(rset.isClosed());
    // Close things
    rset.close();
    Assert.assertTrue(rset.isClosed());
    stmt.close();
    Assert.assertTrue(stmt.isClosed());
    conn.close();
    Assert.assertTrue(conn.isClosed());
}
Also used : Quad(org.apache.jena.sparql.core.Quad) ResultSet(java.sql.ResultSet) Test(org.junit.Test)

Example 9 with Quad

use of org.apache.jena.sparql.core.Quad in project jena by apache.

the class AbstractJenaConnectionTests method connection_prepared_statement_select_02.

/**
     * Tests use of prepared statements
     * 
     * @throws SQLException
     */
@Test
public void connection_prepared_statement_select_02() throws SQLException {
    // Prepare a dataset
    Dataset ds = DatasetFactory.createTxnMem();
    ds.asDatasetGraph().add(new Quad(NodeFactory.createURI("http://example/graph"), NodeFactory.createURI("http://example/subject"), NodeFactory.createURI("http://example/predicate"), NodeFactory.createLiteral("value")));
    // Work with the connection
    JenaConnection conn = this.getConnection(ds);
    conn.setJdbcCompatibilityLevel(JdbcCompatibility.HIGH);
    PreparedStatement stmt = conn.prepareStatement("SELECT * WHERE { GRAPH ?g { ?s ?p ? } }");
    ParameterMetaData metadata = stmt.getParameterMetaData();
    Assert.assertEquals(1, metadata.getParameterCount());
    stmt.setString(1, "value");
    ResultSet rset = stmt.executeQuery();
    Assert.assertNotNull(rset);
    Assert.assertFalse(rset.isClosed());
    Assert.assertTrue(rset.isBeforeFirst());
    // Check result set metadata
    checkSelectMetadata(rset, 3);
    // Should have a row
    Assert.assertTrue(rset.next());
    Assert.assertTrue(rset.isFirst());
    Assert.assertEquals(1, rset.getRow());
    // Should be no further rows
    Assert.assertFalse(rset.next());
    Assert.assertTrue(rset.isAfterLast());
    Assert.assertFalse(rset.isClosed());
    // Close things
    rset.close();
    Assert.assertTrue(rset.isClosed());
    stmt.close();
    Assert.assertTrue(stmt.isClosed());
    conn.close();
    Assert.assertTrue(conn.isClosed());
}
Also used : Quad(org.apache.jena.sparql.core.Quad) ResultSet(java.sql.ResultSet) Test(org.junit.Test)

Example 10 with Quad

use of org.apache.jena.sparql.core.Quad in project jena by apache.

the class AbstractJenaConnectionTests method connection_prepared_statement_select_01.

/**
     * Tests use of prepared statements
     * 
     * @throws SQLException
     * @throws MalformedURLException
     */
@Test
public void connection_prepared_statement_select_01() throws SQLException, MalformedURLException {
    // Prepare a dataset
    Dataset ds = DatasetFactory.createTxnMem();
    ds.asDatasetGraph().add(new Quad(NodeFactory.createURI("http://example/graph"), NodeFactory.createURI("http://example/subject"), NodeFactory.createURI("http://example/predicate"), NodeFactory.createURI("http://example/object")));
    // Work with the connection
    JenaConnection conn = this.getConnection(ds);
    conn.setJdbcCompatibilityLevel(JdbcCompatibility.HIGH);
    PreparedStatement stmt = conn.prepareStatement("SELECT * WHERE { GRAPH ? { ?s ?p ?o } }");
    ParameterMetaData metadata = stmt.getParameterMetaData();
    Assert.assertEquals(1, metadata.getParameterCount());
    stmt.setURL(1, new URL("http://example/graph"));
    ResultSet rset = stmt.executeQuery();
    Assert.assertNotNull(rset);
    Assert.assertFalse(rset.isClosed());
    Assert.assertTrue(rset.isBeforeFirst());
    // Check result set metadata
    checkSelectMetadata(rset, 3);
    // Should have a row
    Assert.assertTrue(rset.next());
    Assert.assertTrue(rset.isFirst());
    Assert.assertEquals(1, rset.getRow());
    // Should be no further rows
    Assert.assertFalse(rset.next());
    Assert.assertTrue(rset.isAfterLast());
    Assert.assertFalse(rset.isClosed());
    // Close things
    rset.close();
    Assert.assertTrue(rset.isClosed());
    stmt.close();
    Assert.assertTrue(stmt.isClosed());
    conn.close();
    Assert.assertTrue(conn.isClosed());
}
Also used : Quad(org.apache.jena.sparql.core.Quad) ResultSet(java.sql.ResultSet) URL(java.net.URL) Test(org.junit.Test)

Aggregations

Quad (org.apache.jena.sparql.core.Quad)161 Test (org.junit.Test)91 DatasetGraph (org.apache.jena.sparql.core.DatasetGraph)49 Node (org.apache.jena.graph.Node)36 BaseTest (org.apache.jena.atlas.junit.BaseTest)32 Triple (org.apache.jena.graph.Triple)25 UpdateBuilder (org.apache.jena.arq.querybuilder.UpdateBuilder)14 Update (org.apache.jena.update.Update)14 QuadWritable (org.apache.jena.hadoop.rdf.types.QuadWritable)12 ResultSet (java.sql.ResultSet)8 ArrayList (java.util.ArrayList)8 TripleWritable (org.apache.jena.hadoop.rdf.types.TripleWritable)7 Var (org.apache.jena.sparql.core.Var)7 LongWritable (org.apache.hadoop.io.LongWritable)6 UpdateDataDelete (org.apache.jena.sparql.modify.request.UpdateDataDelete)6 UpdateDataInsert (org.apache.jena.sparql.modify.request.UpdateDataInsert)6 Element (org.apache.jena.sparql.syntax.Element)6 Graph (org.apache.jena.graph.Graph)5 BasicPattern (org.apache.jena.sparql.core.BasicPattern)4 IOException (java.io.IOException)3