Package org.apache.metamodel

Examples of org.apache.metamodel.UpdateScript


                .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

        file.delete();
        assertFalse(file.exists());

        final CsvDataContext dc = new CsvDataContext(file, new CsvConfiguration(
                CsvConfiguration.DEFAULT_COLUMN_NAME_LINE, "UTF8", '|', '?', '!'));
        dc.executeUpdate(new UpdateScript() {
            @Override
            public void run(UpdateCallback cb) {
                Table table = cb.createTable(dc.getDefaultSchema(), "table").withColumn("id").withColumn("name")
                        .execute();
                cb.insertInto(table).value("id", 1).value("name", "Kasper").execute();
View Full Code Here

    public void testCannotWriteToReadOnly() throws Exception {
        final CsvDataContext dc = new CsvDataContext(new FileInputStream("src/test/resources/empty_file.csv"),
                new CsvConfiguration());
        try {
            dc.executeUpdate(new UpdateScript() {
                @Override
                public void run(UpdateCallback cb) {
                    cb.createTable(dc.getDefaultSchema(), "foo");
                }
            });
View Full Code Here

        }

        final CouchDbDataContext dc = new CouchDbDataContext(couchDbInstance);

        // first delete the manually created database!
        dc.executeUpdate(new UpdateScript() {
            @Override
            public void run(UpdateCallback callback) {
                callback.dropTable(TEST_DATABASE_NAME).execute();
            }
        });

        assertNull(dc.getDefaultSchema().getTableByName(TEST_DATABASE_NAME));

        dc.executeUpdate(new UpdateScript() {
            @Override
            public void run(UpdateCallback callback) {
                Table table = callback.createTable(dc.getDefaultSchema(), TEST_DATABASE_NAME).withColumn("foo")
                        .ofType(ColumnType.VARCHAR).withColumn("greeting").ofType(ColumnType.VARCHAR).execute();
                assertEquals("[_id, _rev, foo, greeting]", Arrays.toString(table.getColumnNames()));
            }
        });

        dc.executeUpdate(new UpdateScript() {
            @Override
            public void run(UpdateCallback callback) {
                callback.insertInto(TEST_DATABASE_NAME).value("foo", "bar").value("greeting", "hello").execute();
                callback.insertInto(TEST_DATABASE_NAME).value("foo", "baz").value("greeting", "hi").execute();
            }
        });

        DataSet ds = dc.query().from(TEST_DATABASE_NAME).select("_id", "foo", "greeting").execute();
        assertTrue(ds.next());
        assertNotNull(ds.getRow().getValue(0));
        assertEquals("bar", ds.getRow().getValue(1));
        assertEquals("hello", ds.getRow().getValue(2));
        assertTrue(ds.next());
        assertNotNull(ds.getRow().getValue(0));
        assertEquals("baz", ds.getRow().getValue(1));
        assertEquals("hi", ds.getRow().getValue(2));
        assertFalse(ds.next());
        ds.close();

        dc.executeUpdate(new UpdateScript() {
            @Override
            public void run(UpdateCallback callback) {
                callback.update(TEST_DATABASE_NAME).value("greeting", "howdy").where("foo").isEquals("baz").execute();

                callback.update(TEST_DATABASE_NAME).value("foo", "foo").where("foo").isEquals("bar").execute();
View Full Code Here

        assertTrue(ds.next());
        assertEquals(0, ((Number) ds.getRow().getValue(0)).intValue());
        assertFalse(ds.next());
        ds.close();

        dc.executeUpdate(new UpdateScript() {
            @Override
            public void run(UpdateCallback callback) {
                callback.insertInto(TEST_DATABASE_NAME).value("name", "foo").value("gender", 'M').execute();
                callback.insertInto(TEST_DATABASE_NAME).value("name", "bar").value("age", 32).execute();
            }
View Full Code Here

        assertTrue(file.exists());

        ExcelDataContext dc = new ExcelDataContext(file);
        final Table table = dc.getDefaultSchema().getTables()[0];
        final Column nameColumn = table.getColumnByName("name");
        dc.executeUpdate(new UpdateScript() {
            @Override
            public void run(UpdateCallback cb) {
                Style clownStyle = new StyleBuilder().bold().foreground(255, 0, 0).background(0, 0, 255).create();

                Style thirtyStyle = new StyleBuilder().italic().underline().centerAligned().foreground(10, 10, 200).create();
View Full Code Here

    private void runCreateTableTest(File file) {
        if (file.exists()) {
            assertTrue(file.delete());
        }
        final ExcelDataContext dc = new ExcelDataContext(file);
        dc.executeUpdate(new UpdateScript() {
            @Override
            public void run(UpdateCallback cb) {
                Schema schema = dc.getDefaultSchema();
                Table table1 = cb.createTable(schema, "my_table_1").withColumn("foo").withColumn("bar").withColumn("baz")
                        .execute();

                assertEquals(1, schema.getTableCount());
                assertSame(table1.getSchema(), schema);
                assertSame(table1, schema.getTables()[0]);

                Table table2 = cb.createTable(schema, "my_table_2").withColumn("foo").withColumn("bar").withColumn("baz")
                        .execute();

                assertSame(table2.getSchema(), schema);
                assertSame(table2, schema.getTables()[1]);
                assertEquals(2, schema.getTableCount());

                cb.insertInto(table1).value("foo", 123.0).value("bar", "str 1").value("baz", true).execute();
            }
        });

        dc.refreshSchemas();

        Schema schema = dc.getDefaultSchema();
        assertEquals(2, schema.getTableCount());
        assertEquals("[my_table_1, my_table_2]", Arrays.toString(schema.getTableNames()));

        dc.executeUpdate(new UpdateScript() {
            @Override
            public void run(UpdateCallback cb) {
                cb.insertInto(dc.getTableByQualifiedLabel("my_table_1")).value("foo", 456.2)
                .value("bar", "парфюмерия +и косметика").value("baz", false).execute();
            }
        });

        dc.executeUpdate(new UpdateScript() {
            @Override
            public void run(UpdateCallback cb) {
                cb.insertInto("my_table_1").value("foo", 789).value("bar", DateUtils.get(2011, Month.JULY, 8))
                        .value("baz", false).execute();
            }
        });

        DataSet ds = dc.query().from("my_table_1").select("foo").and("bar").and("baz").execute();
        assertTrue(ds.next());
        assertEquals("Row[values=[123, str 1, true]]", ds.getRow().toString());
        assertTrue(ds.next());
        assertEquals("Row[values=[456.2, парфюмерия +и косметика, false]]", ds.getRow().toString());
        assertTrue(ds.next());
        assertEquals("Row[values=[789, 2011-07-08 00:00:00, false]]", ds.getRow().toString());
        assertFalse(ds.next());
        ds.close();

        dc.executeUpdate(new UpdateScript() {
            @Override
            public void run(UpdateCallback callback) {
                callback.deleteFrom("my_table_1").where("foo").greaterThan("124").execute();
            }
        });

        assertEquals("1", MetaModelHelper.executeSingleRowQuery(dc, dc.query().from("my_table_1").selectCount().toQuery())
                .getValue(0).toString());

        ds = dc.query().from("my_table_1").select("foo").and("bar").and("baz").execute();
        assertTrue(ds.next());
        assertEquals("Row[values=[123, str 1, true]]", ds.getRow().toString());
        assertFalse(ds.next());
        ds.close();

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

    public void testCreateAndWriteData() throws Exception {
        if (proceedWithUnitTest()) {
            final MongoDbDataContext dc = new MongoDbDataContext(db);
            final Schema defaultSchema = dc.getDefaultSchema();

            dc.executeUpdate(new UpdateScript() {
                @Override
                public void run(UpdateCallback callback) {
                    Table[] tables = defaultSchema.getTables();
                    for (Table table : tables) {
                        callback.deleteFrom(table).execute();
                    }
                }
            });

            assertEquals(0, defaultSchema.getTableCount());

            dc.executeUpdate(new UpdateScript() {

                @Override
                public void run(UpdateCallback callback) {
                    Table table = callback.createTable(defaultSchema, "some_entries").withColumn("foo").withColumn("bar")
                            .withColumn("baz").withColumn("list").execute();

                    callback.insertInto(table).value("foo", 1).value("bar", "hello").execute();
                    callback.insertInto(table).value("foo", 2).value("bar", "world").execute();
                    callback.insertInto(table).value("foo", 3).value("bar", "hi").execute();

                    Map<String, Object> nestedObj = new HashMap<String, Object>();
                    nestedObj.put("foo", "bar");
                    nestedObj.put("123", 456);

                    callback.insertInto(table).value("foo", 4).value("bar", "there").value("baz", nestedObj)
                            .value("list", Arrays.asList(1, 2, 3)).execute();
                }
            });

            DataSet dataSet;
            assertEquals(1, defaultSchema.getTableCount());

            // "Pure" SELECT COUNT(*) query
            dataSet = dc.query().from("some_entries").selectCount().execute();
            dataSet.close();
            assertTrue(dataSet.next());
            assertEquals(1, dataSet.getSelectItems().length);
            assertEquals(SelectItem.getCountAllItem(), dataSet.getSelectItems()[0]);
            assertEquals(4l, dataSet.getRow().getValue(SelectItem.getCountAllItem()));
            assertFalse(dataSet.next());
            assertEquals(InMemoryDataSet.class, dataSet.getClass());

            // A conditional SELECT COUNT(*) query
            dataSet = dc.query().from("some_entries").selectCount().where("foo").greaterThan(2).execute();
            dataSet.close();
            assertTrue(dataSet.next());
            assertEquals(1, dataSet.getSelectItems().length);
            assertEquals(SelectItem.getCountAllItem(), dataSet.getSelectItems()[0]);
            assertEquals(2l, dataSet.getRow().getValue(SelectItem.getCountAllItem()));
            assertFalse(dataSet.next());
            assertEquals(InMemoryDataSet.class, dataSet.getClass());

            // Select columns
            dataSet = dc.query().from("some_entries").select("foo").and("bar").and("baz").and("list").execute();
            assertTrue(dataSet.next());
            assertEquals("Row[values=[1, hello, null, null]]", dataSet.getRow().toString());
            assertTrue(dataSet.next());
            assertEquals("Row[values=[2, world, null, null]]", dataSet.getRow().toString());
            assertTrue(dataSet.next());
            assertEquals("Row[values=[3, hi, null, null]]", dataSet.getRow().toString());
            assertTrue(dataSet.next());
            assertEquals("Row[values=[4, there, {123=456, foo=bar}, [ 1 , 2 , 3]]]", dataSet.getRow().toString());
            assertFalse(dataSet.next());
            dataSet.close();
            assertEquals(MongoDbDataSet.class, dataSet.getClass());

            // delete some records
            dc.executeUpdate(new UpdateScript() {
                @Override
                public void run(UpdateCallback callback) {
                    callback.deleteFrom("some_entries").where("foo").greaterThan(2).where("baz").isNotNull().execute();
                }
            });

            dataSet = dc.query().from("some_entries").select("foo").execute();
            assertTrue(dataSet.next());
            assertEquals("Row[values=[1]]", dataSet.getRow().toString());
            assertTrue(dataSet.next());
            assertEquals("Row[values=[2]]", dataSet.getRow().toString());
            assertTrue(dataSet.next());
            assertEquals("Row[values=[3]]", dataSet.getRow().toString());
            assertFalse(dataSet.next());
            dataSet.close();
            assertEquals(MongoDbDataSet.class, dataSet.getClass());

            // drop the collection
            dc.executeUpdate(new UpdateScript() {
                @Override
                public void run(UpdateCallback callback) {
                    callback.dropTable("some_entries").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.