Package org.apache.metamodel

Examples of org.apache.metamodel.UpdateScript


        assertEquals("Row[values=[1000]]", ds.getRow().toString());
        assertTrue(ds.next());
        assertEquals("Row[values=[1002]]", ds.getRow().toString());
        assertFalse(ds.next());

        dc.executeUpdate(new UpdateScript() {
            @Override
            public void run(UpdateCallback callback) {
                callback.deleteFrom(table).where("col1").eq("1").execute();
            }
        });

        ds = dc.query().from("bar").select("col1", "col2", "col3").execute();
        assertTrue(ds.next());
        assertEquals("Row[values=[2, 1000, true]]", ds.getRow().toString());
        assertTrue(ds.next());
        assertEquals("Row[values=[2, 1003, false]]", ds.getRow().toString());
        assertTrue(ds.next());
        assertEquals("Row[values=[2, 1004, false]]", ds.getRow().toString());
        assertFalse(ds.next());

        dc.executeUpdate(new UpdateScript() {
            @Override
            public void run(UpdateCallback callback) {
                callback.insertInto(table).value("col1", "3").value("col2", 1005).value("col3", true).execute();
            }
        });

        ds = dc.query().from("bar").select("col1", "col2", "col3").execute();
        assertTrue(ds.next());
        assertEquals("Row[values=[2, 1000, true]]", ds.getRow().toString());
        assertTrue(ds.next());
        assertEquals("Row[values=[2, 1003, false]]", ds.getRow().toString());
        assertTrue(ds.next());
        assertEquals("Row[values=[2, 1004, false]]", ds.getRow().toString());
        assertTrue(ds.next());
        assertEquals("Row[values=[3, 1005, true]]", ds.getRow().toString());
        assertFalse(ds.next());

        dc.executeUpdate(new UpdateScript() {
            @Override
            public void run(UpdateCallback callback) {
                callback.dropTable(table).execute();
            }
        });

        assertEquals(0, schema.getTableCount());

        dc.executeUpdate(new UpdateScript() {
            @Override
            public void run(UpdateCallback callback) {
                callback.createTable(schema, "yo!").withColumn("foo").withColumn("bar").execute();
                callback.insertInto("yo!").value("foo", "1").value("bar", "2").execute();
            }
View Full Code Here


        if (dc.getTableByQualifiedLabel("test_table") != null) {
            dc.executeUpdate(new DropTable(schema, "test_table"));
        }

        dc.executeUpdate(new UpdateScript() {
            @Override
            public void run(UpdateCallback cb) {
                ColumnCreationBuilder createTableBuilder = cb.createTable(schema, "test_table").withColumn("id")
                        .ofType(ColumnType.FLOAT).withColumn("code").ofType(ColumnType.VARCHAR).ofSize(10);
                Table table = createTableBuilder.execute();
View Full Code Here

        if (dc.getTableByQualifiedLabel("test_table") != null) {
            dc.executeUpdate(new DropTable("test_table"));
        }

        dc.executeUpdate(new UpdateScript() {
            @Override
            public void run(UpdateCallback cb) {
                ColumnCreationBuilder createTableBuilder = cb.createTable(schema, "test_table").withColumn("id")
                        .ofType(ColumnType.FLOAT).withColumn("code").ofType(ColumnType.VARCHAR).ofSize(10);
                Table table = createTableBuilder.execute();
View Full Code Here

        final JdbcDataContext dc = new JdbcDataContext(conn);
        final Schema schema = dc.getDefaultSchema();

        if (dc.getTableByQualifiedLabel("test_table") != null) {
            dc.executeUpdate(new UpdateScript() {
                @Override
                public void run(UpdateCallback cb) {
                    cb.dropTable("test_table").execute();
                }
            });
        }

        dc.executeUpdate(new UpdateScript() {
            @Override
            public void run(UpdateCallback cb) {
                ColumnCreationBuilder createTableBuilder = cb.createTable(schema, "test_table").withColumn("id")
                        .ofType(ColumnType.INTEGER).withColumn("code").ofType(ColumnType.CHAR).ofSize(1);
                String sql = createTableBuilder.toSql();
View Full Code Here

        assertFalse(conn.isReadOnly());

        JdbcDataContext dc = new JdbcDataContext(conn);
        final Schema schema = dc.getDefaultSchema();

        dc.executeUpdate(new UpdateScript() {
            @Override
            public void run(UpdateCallback cb) {
                Table table = cb.createTable(schema, "test_table").withColumn("id").ofType(ColumnType.INTEGER)
                        .withColumn("birthdate1").ofType(ColumnType.DATE).withColumn("birthdate2").ofType(ColumnType.TIMESTAMP)
                        .execute();

                cb.insertInto(table).value("id", "1").value("birthdate1", null).execute();
                cb.insertInto(table).value("id", 2).value("birthdate1", "2011-12-21").value("birthdate2", "2011-12-21 14:00:00")
                        .execute();
            }
        });

        DataSet ds = dc.query().from("test_table").select("id").and("birthdate1").execute();
        assertTrue(ds.next());
        assertEquals("Row[values=[1, null]]", ds.getRow().toString());
        assertEquals("java.lang.Integer", ds.getRow().getValue(0).getClass().getName());
        assertTrue(ds.next());
        assertEquals("Row[values=[2, 2011-12-21]]", ds.getRow().toString());
        assertEquals("java.sql.Date", ds.getRow().getValue(1).getClass().getName());
        assertFalse(ds.next());
        ds.close();

        Query query = dc.query().from("test_table").select("id").where("birthdate2")
                .lessThan(DateUtils.get(2011, Month.DECEMBER, 20)).toQuery();
        try {
            ds = dc.executeQuery(query);
        } catch (Exception e) {
            System.out.println("Failing query was: " + dc.getQueryRewriter().rewriteQuery(query));
            throw e;
        }
        assertFalse(ds.next());
        ds.close();

        ds = dc.query().from("test_table").select("id").where("birthdate2").greaterThan(DateUtils.get(2011, Month.DECEMBER, 20))
                .execute();
        assertTrue(ds.next());
        assertEquals("Row[values=[2]]", ds.getRow().toString());
        assertFalse(ds.next());
        ds.close();

        dc.executeUpdate(new UpdateScript() {
            @Override
            public void run(UpdateCallback callback) {
                callback.deleteFrom("test_table").where("id").in(Arrays.<String> asList("1", "2")).execute();
            }
        });

        ds = dc.query().from("test_table").selectCount().where("id").eq(2).or("id").eq(1).execute();
        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.dropTable("test_table").execute();
            }
        });
View Full Code Here

    }

    public static void createInsertAndUpdateDateTypes(final JdbcDataContext dc, final Schema schema, final String tableName)
            throws Exception {
        if (schema.getTableByName(tableName) != null) {
            dc.executeUpdate(new UpdateScript() {
                @Override
                public void run(UpdateCallback callback) {
                    callback.dropTable(schema.getTableByName(tableName)).execute();
                }
            });
        }

        dc.executeUpdate(new BatchUpdateScript() {
            @Override
            public void run(UpdateCallback cb) {
                Table table = cb.createTable(schema, tableName).withColumn("id").asPrimaryKey().ofType(ColumnType.INTEGER)
                        .withColumn("birthdate").ofType(ColumnType.DATE).withColumn("wakemeup").ofType(ColumnType.TIME).execute();

                // insert record 1
                {
                    // create a 7:55 time.
                    Calendar cal = Calendar.getInstance();
                    cal.setTimeInMillis(0);
                    cal.set(Calendar.HOUR_OF_DAY, 7);
                    cal.set(Calendar.MINUTE, 55);
                    Date wakeUpTime = cal.getTime();
                    cb.insertInto(table).value("id", 1).value("birthdate", DateUtils.get(1982, Month.APRIL, 20))
                            .value("wakemeup", wakeUpTime).execute();
                }

                // insert record 2
                {
                    // create a 18:35 time.
                    Calendar cal = Calendar.getInstance();
                    cal.setTimeInMillis(0);
                    cal.set(Calendar.HOUR_OF_DAY, 18);
                    cal.set(Calendar.MINUTE, 35);
                    Date wakeUpTime = cal.getTime();
                    cb.insertInto(table).value("id", 2).value("birthdate", DateUtils.get(1982, Month.APRIL, 21))
                            .value("wakemeup", wakeUpTime).execute();
                }
            }
        });

        dc.executeUpdate(new UpdateScript() {
            @Override
            public void run(UpdateCallback cb) {
                cb.insertInto(schema.getTableByName(tableName)).value("id", 3).value("birthdate", "2011-12-21")
                        .value("wakemeup", "12:00").execute();
            }
        });

        DataSet ds = dc.query().from(schema.getTableByName(tableName)).select("id", "birthdate", "wakemeup").orderBy("id")
                .execute();
        assertTrue(ds.next());
        assertEquals("1", ds.getRow().getValue(0).toString());
        assertEquals("1982-04-20", ds.getRow().getValue(1).toString());
        assertTrue("Actual value was: " + ds.getRow().getValue(2), ds.getRow().getValue(2).toString().startsWith("07:55:00"));

        assertTrue(ds.next());
        assertEquals("2", ds.getRow().getValue(0).toString());
        assertEquals("1982-04-21", ds.getRow().getValue(1).toString());
        assertTrue("Actual value was: " + ds.getRow().getValue(2), ds.getRow().getValue(2).toString().startsWith("18:35:00"));

        assertTrue(ds.next());
        assertEquals("3", ds.getRow().getValue(0).toString());
        assertEquals("2011-12-21", ds.getRow().getValue(1).toString());
        assertTrue("Actual value was: " + ds.getRow().getValue(2), ds.getRow().getValue(2).toString().startsWith("12:00"));

        assertFalse(ds.next());
        ds.close();

        dc.executeUpdate(new UpdateScript() {
            @Override
            public void run(UpdateCallback callback) {
                // update record 1

                // create a 08:00 time.
                Calendar cal = Calendar.getInstance();
                cal.setTimeInMillis(0);
                cal.set(Calendar.HOUR_OF_DAY, 8);
                cal.set(Calendar.MINUTE, 00);
                Date wakeUpTime = cal.getTime();

                callback.update(schema.getTableByName(tableName)).value("birthdate", DateUtils.get(1982, Month.APRIL, 21))
                        .value("wakemeup", wakeUpTime).where("birthdate").isEquals(DateUtils.get(1982, Month.APRIL, 20))
                        .execute();
            }
        });

        ds = dc.query().from(schema.getTableByName(tableName)).select("id", "birthdate", "wakemeup").orderBy("id").execute();
        assertTrue(ds.next());
        assertEquals("1", ds.getRow().getValue(0).toString());
        assertEquals("1982-04-21", ds.getRow().getValue(1).toString());
        assertTrue("Actual value was: " + ds.getRow().getValue(2), ds.getRow().getValue(2).toString().startsWith("08:00:00"));

        assertTrue(ds.next());
        assertEquals("2", ds.getRow().getValue(0).toString());
        assertEquals("1982-04-21", ds.getRow().getValue(1).toString());
        assertTrue("Actual value was: " + ds.getRow().getValue(2), ds.getRow().getValue(2).toString().startsWith("18:35:00"));

        assertTrue(ds.next());
        assertEquals("3", ds.getRow().getValue(0).toString());
        assertEquals("2011-12-21", ds.getRow().getValue(1).toString());
        assertTrue("Actual value was: " + ds.getRow().getValue(2), ds.getRow().getValue(2).toString().startsWith("12:00"));

        assertFalse(ds.next());
        ds.close();

        if (schema.getTableByName(tableName) != null) {
            dc.executeUpdate(new UpdateScript() {
                @Override
                public void run(UpdateCallback callback) {
                    callback.dropTable(schema.getTableByName(tableName)).execute();
                }
            });
View Full Code Here

    public static void convertClobToString(JdbcDataContext dc) {
        System.setProperty(JdbcDataContext.SYSTEM_PROPERTY_CONVERT_LOBS, "");

        final Schema schema = dc.getDefaultSchema();

        dc.executeUpdate(new UpdateScript() {
            @Override
            public void run(UpdateCallback callback) {
                Table table = callback.createTable(schema, "clob_test_table").withColumn("id").ofType(ColumnType.INTEGER)
                        .asPrimaryKey().withColumn("foo").ofType(ColumnType.CLOB).execute();

                callback.insertInto(table).value("id", 1).value("foo", "baaaaz").execute();

                StringReader sr = new StringReader("foooooooabavlsdk\nflskmflsdk");
                callback.insertInto(table).value("id", 2).value("foo", sr).execute();
            }
        });

        DataSet ds;

        ds = dc.query().from(schema, "clob_test_table").selectCount().execute();
        assertTrue(ds.next());
        assertEquals("Row[values=[2]]", ds.getRow().toString());
        ds.close();

        ds = dc.query().from(schema, "clob_test_table").select("id", "foo").orderBy("id").execute();
        assertTrue(ds.next());
        assertEquals(1, ds.getRow().getValue(0));
        final Object clobValue1 = ds.getRow().getValue(1);
        assertTrue(clobValue1 instanceof Clob || clobValue1 instanceof String);
        assertTrue(ds.next());
        assertEquals(2, ds.getRow().getValue(0));
        final Object clobValue2 = ds.getRow().getValue(1);
        assertTrue(clobValue2 instanceof Clob || clobValue2 instanceof String);
        assertFalse(ds.next());
        ds.close();

        System.setProperty(JdbcDataContext.SYSTEM_PROPERTY_CONVERT_LOBS, "true");

        ds = dc.query().from(schema, "clob_test_table").select("id", "foo").orderBy("id").execute();
        assertTrue(ds.next());
        assertEquals("Row[values=[1, baaaaz]]", ds.getRow().toString());
        assertTrue(ds.next());
        assertEquals("Row[values=[2, foooooooabavlsdk\nflskmflsdk]]", ds.getRow().toString());
        assertFalse(ds.next());
        ds.close();

        dc.executeUpdate(new UpdateScript() {
            @Override
            public void run(UpdateCallback callback) {
                callback.dropTable(schema, "clob_test_table").execute();
            }
        });
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(getDatabaseName()).execute();
            }
        });

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

        dc.executeUpdate(new UpdateScript() {
            @Override
            public void run(UpdateCallback callback) {
                Table table = callback.createTable(dc.getDefaultSchema(), getDatabaseName()).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(getDatabaseName()).value("foo", "bar").value("greeting", "hello").execute();
                callback.insertInto(getDatabaseName()).value("foo", "baz").value("greeting", "hi").execute();
            }
        });

        DataSet ds = dc.query().from(getDatabaseName()).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(getDatabaseName()).value("greeting", "howdy").where("foo").isEquals("baz").execute();

                callback.update(getDatabaseName()).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(getDatabaseName()).value("name", "foo").value("gender", 'M').execute();
                callback.insertInto(getDatabaseName()).value("name", "bar").value("age", 32).execute();
            }
View Full Code Here

        assertEquals("Row[values=[1000]]", ds.getRow().toString());
        assertTrue(ds.next());
        assertEquals("Row[values=[1002]]", ds.getRow().toString());
        assertFalse(ds.next());

        dc.executeUpdate(new UpdateScript() {
            @Override
            public void run(UpdateCallback callback) {
                callback.deleteFrom(table).where("col1").eq("1").execute();
            }
        });

        ds = dc.query().from("bar").select("col1", "col2", "col3").execute();
        assertTrue(ds.next());
        assertEquals("Row[values=[2, 1000, true]]", ds.getRow().toString());
        assertTrue(ds.next());
        assertEquals("Row[values=[2, 1003, false]]", ds.getRow().toString());
        assertTrue(ds.next());
        assertEquals("Row[values=[2, 1004, false]]", ds.getRow().toString());
        assertFalse(ds.next());

        dc.executeUpdate(new UpdateScript() {
            @Override
            public void run(UpdateCallback callback) {
                callback.insertInto(table).value("col1", "3").value("col2", 1005).value("col3", true).execute();
            }
        });

        ds = dc.query().from("bar").select("col1", "col2", "col3").execute();
        assertTrue(ds.next());
        assertEquals("Row[values=[2, 1000, true]]", ds.getRow().toString());
        assertTrue(ds.next());
        assertEquals("Row[values=[2, 1003, false]]", ds.getRow().toString());
        assertTrue(ds.next());
        assertEquals("Row[values=[2, 1004, false]]", ds.getRow().toString());
        assertTrue(ds.next());
        assertEquals("Row[values=[3, 1005, true]]", ds.getRow().toString());
        assertFalse(ds.next());

        dc.executeUpdate(new UpdateScript() {
            @Override
            public void run(UpdateCallback callback) {
                callback.dropTable(table).execute();
            }
        });

        assertEquals(0, schema.getTableCount());

        dc.executeUpdate(new UpdateScript() {
            @Override
            public void run(UpdateCallback callback) {
                callback.createTable(schema, "yo!").withColumn("foo").withColumn("bar").execute();
                callback.insertInto("yo!").value("foo", "1").value("bar", "2").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.