@Test public void testUpdateRow() throws Exception { final Schema schema = dataContext.getDefaultSchema(); final CreateTable createTable = new CreateTable(schema, "testCreateTable"); createTable.withColumn("foo").ofType(ColumnType.STRING); createTable.withColumn("bar").ofType(ColumnType.NUMBER); dataContext.executeUpdate(createTable); final Table table = schema.getTableByName("testCreateTable"); try { dataContext.executeUpdate( new UpdateScript() { @Override public void run(UpdateCallback callback) { callback.insertInto(table).value("foo", "hello").value("bar", 42).execute(); callback.insertInto(table).value("foo", "world").value("bar", 43).execute(); } }); dataContext.executeUpdate(new Update(table).value("foo", "howdy").where("bar").eq(42)); DataSet dataSet = dataContext.query().from(table).select("foo", "bar").orderBy("bar").execute(); assertTrue(dataSet.next()); assertEquals("Row[values=[howdy, 42]]", dataSet.getRow().toString()); assertTrue(dataSet.next()); assertEquals("Row[values=[world, 43]]", dataSet.getRow().toString()); assertFalse(dataSet.next()); dataSet.close(); } finally { dataContext.executeUpdate(new DropTable(table)); } }
@Test public void testDeleteByQuery() throws Exception { final Schema schema = dataContext.getDefaultSchema(); final CreateTable createTable = new CreateTable(schema, "testCreateTable"); createTable.withColumn("foo").ofType(ColumnType.STRING); createTable.withColumn("bar").ofType(ColumnType.NUMBER); dataContext.executeUpdate(createTable); final Table table = schema.getTableByName("testCreateTable"); dataContext.executeUpdate( new UpdateScript() { @Override public void run(UpdateCallback callback) { callback.insertInto(table).value("foo", "hello").value("bar", 42).execute(); callback.insertInto(table).value("foo", "world").value("bar", 43).execute(); } }); dataContext.executeUpdate(new DeleteFrom(table).where("foo").eq("hello").where("bar").eq(42)); Row row = MetaModelHelper.executeSingleRowQuery( dataContext, dataContext.query().from(table).select("foo", "bar").toQuery()); assertEquals("Row[values=[world, 43]]", row.toString()); dataContext.executeUpdate(new DropTable(table)); }
@Test public void testDeleteUnsupportedQueryType() throws Exception { final Schema schema = dataContext.getDefaultSchema(); final CreateTable createTable = new CreateTable(schema, "testCreateTable"); createTable.withColumn("foo").ofType(ColumnType.STRING); createTable.withColumn("bar").ofType(ColumnType.NUMBER); dataContext.executeUpdate(createTable); final Table table = schema.getTableByName("testCreateTable"); try { dataContext.executeUpdate( new UpdateScript() { @Override public void run(UpdateCallback callback) { callback.insertInto(table).value("foo", "hello").value("bar", 42).execute(); callback.insertInto(table).value("foo", "world").value("bar", 43).execute(); } }); // greater than is not yet supported try { dataContext.executeUpdate(new DeleteFrom(table).where("bar").gt(40)); fail("Exception expected"); } catch (UnsupportedOperationException e) { assertEquals( "Could not push down WHERE items to delete by query request: [testCreateTable.bar > 40]", e.getMessage()); } } finally { dataContext.executeUpdate(new DropTable(table)); } }
@Test public void testCreateTableInsertQueryAndDrop() throws Exception { final Schema schema = dataContext.getDefaultSchema(); final CreateTable createTable = new CreateTable(schema, "testCreateTable"); createTable.withColumn("foo").ofType(ColumnType.STRING); createTable.withColumn("bar").ofType(ColumnType.NUMBER); dataContext.executeUpdate(createTable); final Table table = schema.getTableByName("testCreateTable"); assertEquals( "[" + ElasticSearchDataContext.FIELD_ID + ", foo, bar]", Arrays.toString(table.getColumnNames())); final Column fooColumn = table.getColumnByName("foo"); final Column idColumn = table.getPrimaryKeys()[0]; assertEquals( "Column[name=_id,columnNumber=0,type=STRING,nullable=null,nativeType=null,columnSize=null]", idColumn.toString()); dataContext.executeUpdate( new UpdateScript() { @Override public void run(UpdateCallback callback) { callback.insertInto(table).value("foo", "hello").value("bar", 42).execute(); callback.insertInto(table).value("foo", "world").value("bar", 43).execute(); } }); dataContext.refreshSchemas(); try (DataSet ds = dataContext.query().from(table).selectAll().orderBy("bar").execute()) { assertTrue(ds.next()); assertEquals("hello", ds.getRow().getValue(fooColumn).toString()); assertNotNull(ds.getRow().getValue(idColumn)); assertTrue(ds.next()); assertEquals("world", ds.getRow().getValue(fooColumn).toString()); assertNotNull(ds.getRow().getValue(idColumn)); assertFalse(ds.next()); } dataContext.executeUpdate(new DropTable(table)); dataContext.refreshSchemas(); assertNull(dataContext.getTableByQualifiedLabel(table.getName())); }