Package org.apache.metamodel

Examples of org.apache.metamodel.UpdateScript


    final List<Object[]> values = source.getValues();

    assertEquals(3, values.size());

    intercepted.executeUpdate(new UpdateScript() {
      @Override
      public void run(UpdateCallback callback) {
        callback.insertInto("schema.table").value(0, 1).value(1, "2")
            .execute();
        callback.insertInto("schema.table").value(0, 3).value(1, "4")
View Full Code Here


        assertEquals(Integer.class, ds.getRow().getValue(0).getClass());
        assertEquals(3, ds.getRow().getValue(0));
        assertFalse(ds.next());
        ds.close();

        dc.executeUpdate(new UpdateScript() {
            @Override
            public void run(UpdateCallback callback) {
                callback.insertInto(table).value("foo", 4).value("bar", "mrrrrh").execute();
            }
        });
View Full Code Here

        assertEquals(2, ds.getRow().getValue(0));
        assertTrue(ds.next());
        assertEquals(3, ds.getRow().getValue(0));
        assertFalse(ds.next());

        dc.executeUpdate(new UpdateScript() {
            @Override
            public void run(UpdateCallback callback) {
                callback.insertInto(table).value("foo", 4).value("bar", "heidiho!").execute();
            }
        });

        ds = dc.executeQuery(q);
        assertTrue(ds.next());
        assertEquals(1, ds.getRow().getValue(0));
        assertTrue(ds.next());
        assertEquals(2, ds.getRow().getValue(0));
        assertTrue(ds.next());
        assertEquals(3, ds.getRow().getValue(0));
        assertTrue(ds.next());
        assertEquals(4, ds.getRow().getValue(0));
        assertFalse(ds.next());
       
        assertEquals(4, physicalValuesList.size());
        for (Object[] physicalValues : physicalValuesList) {
            assertEquals("foo is expected to be string", String.class, physicalValues[0].getClass());
        }
       
        dc.executeUpdate(new UpdateScript() {
            @Override
            public void run(UpdateCallback callback) {
                callback.insertInto(table).value("foo", 5).value("bar", "hejsa...").execute();
                callback.update(table).where("foo").lessThan(3).value("foo", 100).execute();
            }
View Full Code Here

        final String tableName = "Account";
        final String insertedName = "MetaModel TESTER contact";

        // INSERT

        dc.executeUpdate(new UpdateScript() {
            @Override
            public void run(UpdateCallback callback) {
                callback.insertInto(tableName).value("name", insertedName).execute();
            }
        });

        final List<String> ids = new ArrayList<String>();

        DataSet ds;
        ds = dc.query().from(tableName).select("id", "name").where("name").eq(insertedName).execute();
        assertTrue(ds.next());
        Row row = ds.getRow();
        assertNotNull(row.getValue(0));
        ids.add(row.getValue(0).toString());

        while (ds.next()) {
            row = ds.getRow();
            ids.add(row.getValue(0).toString());
            assertEquals("MetaModel TESTER contact", row.getValue(1));
        }

        ds.close();

        // UPDATE

        dc.executeUpdate(new UpdateScript() {
            @Override
            public void run(UpdateCallback callback) {
                callback.update(tableName).where("id").eq(ids.get(0)).value("name", "Another test value").execute();
            }
        });

        ds = dc.query().from(tableName).select("name").where("id").eq(ids.get(0)).execute();
        assertTrue(ds.next());
        assertEquals("Another test value", ds.getRow().getValue(0));
        assertFalse(ds.next());
        ds.close();

        // UPDATE (a record that does not exist)

        dc.executeUpdate(new UpdateScript() {
            @Override
            public void run(UpdateCallback callback) {
                callback.update(tableName).where("id").eq("fooooooobaaaaaaaar")
                        .value("name", "A test value that should never occur").execute();
            }
        });

        // DELETE

        dc.executeUpdate(new UpdateScript() {
            @Override
            public void run(UpdateCallback callback) {
                callback.deleteFrom(tableName).where("id").in(ids).execute();
            }
        });
View Full Code Here

        final Date dateValue = dateFormat.parse(dateString);
        assertEquals("1980-08-08 05:10:22", dateFormat.format(dateValue));

        final Table table = dc.getTableByQualifiedLabel(tableName);

        dc.executeUpdate(new UpdateScript() {
            @Override
            public void run(UpdateCallback callback) {
                callback.insertInto(table).value("FirstName", firstName).value("LastName", lastName)
                        .value("BirthDate", dateValue).execute();
            }
View Full Code Here

                FileHelper.DEFAULT_ENCODING, CsvConfiguration.DEFAULT_SEPARATOR_CHAR, CsvConfiguration.NOT_A_CHAR,
                CsvConfiguration.DEFAULT_ESCAPE_CHAR);
        final CsvDataContext dc = new CsvDataContext(file, csvConfiguration);
        assertEquals(1, dc.getDefaultSchema().getTableCount());

        dc.executeUpdate(new UpdateScript() {

            @Override
            public void run(UpdateCallback callback) {
                callback.createTable(dc.getDefaultSchema(), "new_table").withColumn("COL_1").withColumn("COL_2")
                        .execute();
View Full Code Here

        final Table table1 = dc.getDefaultSchema().getTables()[0];
        assertEquals("testEmptyFileNoColumnHeaderLine.csv", table1.getName());
        assertEquals(0, table1.getColumnCount());

        dc.executeUpdate(new UpdateScript() {
            @Override
            public void run(UpdateCallback callback) {
                callback.dropTable(dc.getDefaultSchema().getTable(0)).execute();
                callback.createTable(dc.getDefaultSchema(), "newtable1").withColumn("foo").withColumn("bar").execute();
            }
        });

        assertEquals("\"foo\",\"bar\"", FileHelper.readFileAsString(file));

        dc.executeUpdate(new UpdateScript() {
            @Override
            public void run(UpdateCallback callback) {
                // fire additional create table statements
                callback.createTable(dc.getDefaultSchema(), "newtable2").withColumn("foo").withColumn("bar").execute();
                callback.createTable(dc.getDefaultSchema(), "newtable3").withColumn("bar").withColumn("baz").execute();
View Full Code Here

                .replaceAll("\n", "!LINEBREAK!"));

        final CsvDataContext dc = new CsvDataContext(targetFile);
        final Table table = dc.getDefaultSchema().getTables()[0];

        dc.executeUpdate(new UpdateScript() {
            @Override
            public void run(UpdateCallback callback) {
                callback.insertInto(table).value(0, "1234").value(1, "5678").execute();
            }
        });
View Full Code Here

        FileHelper.copy(new File("src/test/resources/csv_to_be_truncated.csv"), file);

        CsvDataContext dc = new CsvDataContext(file, new CsvConfiguration(1, "UTF8", ',', '"', '\\', true));
        assertEquals("[id, name, gender, age]", Arrays.toString(dc.getDefaultSchema().getTable(0).getColumnNames()));

        dc.executeUpdate(new UpdateScript() {
            @Override
            public void run(UpdateCallback callback) {
                callback.deleteFrom("csv_delete_all_records.txt").execute();
            }
        });
View Full Code Here

        final Schema schema = dc.getDefaultSchema();
        assertEquals(0, schema.getTableCount());

        final MutableRef<Table> tableRef = new MutableRef<Table>();

        dc.executeUpdate(new UpdateScript() {
            @Override
            public void run(UpdateCallback cb) {
                Table table = cb.createTable(schema, "foobar").withColumn("foo").withColumn("bar").execute();
                tableRef.set(table);
                assertEquals(schema, table.getSchema());
                assertEquals(schema.getTables()[0], table);
                assertTrue(file.exists());

                assertEquals("[foo, bar]", Arrays.toString(table.getColumnNames()));

                cb.insertInto(table).value(0, "f").value(1, "b").execute();
                cb.insertInto(table).value(0, "o").value(table.getColumnByName("bar"), "a").execute();
                cb.insertInto(table).value(0, "o").value("bar", "r").execute();
            }
        });

        // query the file to check results
        final Table readTable = schema.getTables()[0];
        assertEquals(tableRef.get(), readTable);
        assertEquals("[foo, bar]", Arrays.toString(readTable.getColumnNames()));

        final Query query = dc.query().from(readTable).select("bar").and("foo").toQuery();
        DataSet ds = dc.executeQuery(query);

        assertTrue(ds.next());
        assertEquals("Row[values=[b, f]]", ds.getRow().toString());
        assertTrue(ds.next());
        assertEquals("Row[values=[a, o]]", ds.getRow().toString());
        assertTrue(ds.next());
        assertEquals("Row[values=[r, o]]", ds.getRow().toString());
        assertFalse(ds.next());

        // do the same trick on an existing file
        dc = new CsvDataContext(file);
        dc.executeUpdate(new UpdateScript() {
            @Override
            public void run(UpdateCallback cb) {
                cb.insertInto(tableRef.get()).value("foo", "hello").value("bar", "world").execute();
            }
        });

        ds = dc.executeQuery(query);

        assertTrue(ds.next());
        assertEquals("Row[values=[b, f]]", ds.getRow().toString());
        assertTrue(ds.next());
        assertEquals("Row[values=[a, o]]", ds.getRow().toString());
        assertTrue(ds.next());
        assertEquals("Row[values=[r, o]]", ds.getRow().toString());
        assertTrue(ds.next());
        assertEquals("Row[values=[world, hello]]", ds.getRow().toString());
        assertFalse(ds.next());
        ds.close();

        dc.executeUpdate(new UpdateScript() {
            @Override
            public void run(UpdateCallback callback) {
                callback.deleteFrom(readTable).where("bar").eq("a").execute();
                callback.deleteFrom(readTable).where("bar").eq("r").execute();
            }
        });

        ds = dc.executeQuery(query);
        assertTrue(ds.next());
        assertEquals("Row[values=[b, f]]", ds.getRow().toString());
        assertTrue(ds.next());
        assertEquals("Row[values=[world, hello]]", ds.getRow().toString());
        assertFalse(ds.next());
        ds.close();

        dc.executeUpdate(new UpdateScript() {
            @Override
            public void run(UpdateCallback callback) {
                callback.update(readTable).value("foo", "universe").execute();
                callback.update(readTable).value("bar", "c").where("bar").isEquals("b").execute();
            }
        });

        ds = dc.executeQuery(query);
        assertTrue(ds.next());
        assertEquals("Row[values=[world, universe]]", ds.getRow().toString());
        assertTrue(ds.next());
        assertEquals("Row[values=[c, universe]]", ds.getRow().toString());
        assertFalse(ds.next());
        ds.close();

        // drop table
        dc.executeUpdate(new UpdateScript() {
            @Override
            public void run(UpdateCallback callback) {
                callback.dropTable(readTable).execute();
            }
        });
View Full Code Here

TOP

Related Classes of org.apache.metamodel.UpdateScript

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.