Package org.apache.metamodel

Examples of org.apache.metamodel.UpdateScript


        JdbcDataContext dc = new JdbcDataContext(getConnection());

        final Schema schema = dc.getDefaultSchema();

        dc.executeUpdate(new UpdateScript() {
            @Override
            public void run(UpdateCallback cb) {
                Table table = cb.createTable(schema, "my_table").withColumn("id").ofType(ColumnType.INTEGER)
                        .ofNativeType("SERIAL").nullable(false).withColumn("some_bool").ofType(ColumnType.BOOLEAN)
                        .nullable(false).execute();
                assertEquals("my_table", table.getName());

                cb.insertInto(table).value("id", 1).value("some_bool", true).execute();
                cb.insertInto(table).value("id", 2).value("some_bool", false).execute();
            }
        });

        DataSet ds = dc.query().from("my_table").select("some_bool").execute();

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

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


        }

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

        dc.executeUpdate(new UpdateScript() {
            @Override
            public void run(UpdateCallback cb) {
                Table table = cb.createTable(schema, "my_table").withColumn("id").ofType(ColumnType.INTEGER)
                        .ofNativeType("SERIAL").nullable(false).withColumn("some_bytes").ofType(ColumnType.BLOB)
                        .execute();
                assertEquals("my_table", table.getName());
            }
        });

        try {
            dc.refreshSchemas();
            final Column column = dc.getColumnByQualifiedLabel("my_table.some_bytes");
            assertEquals("Column[name=some_bytes,columnNumber=1,type=BINARY,nullable=true,"
                    + "nativeType=bytea,columnSize=2147483647]", column.toString());

            final Table table = column.getTable();

            dc.executeUpdate(new UpdateScript() {
                @Override
                public void run(UpdateCallback callback) {
                    callback.insertInto(table).value(column, new byte[] { 1, 2, 3 }).execute();
                    callback.insertInto(table).value(column, "hello world".getBytes()).execute();
                }
            });

            byte[] bytes;

            DataSet ds = dc.query().from(table).select(table.getColumns()).execute();

            assertTrue(ds.next());
            assertEquals(1, ds.getRow().getValue(0));
            bytes = (byte[]) ds.getRow().getValue(1);
            assertEquals(3, bytes.length);
            assertEquals(1, bytes[0]);
            assertEquals(2, bytes[1]);
            assertEquals(3, bytes[2]);

            assertTrue(ds.next());
            assertEquals(2, ds.getRow().getValue(0));
            bytes = (byte[]) ds.getRow().getValue(1);

            assertEquals("hello world", new String(bytes));
            assertFalse(ds.next());

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

        }

        JdbcDataContext dc = new JdbcDataContext(getConnection());
        final Schema schema = dc.getDefaultSchema();
        try {
            dc.executeUpdate(new UpdateScript() {
                @Override
                public void run(UpdateCallback cb) {
                    Table table = cb.createTable(schema, "my_table").withColumn("id").ofType(ColumnType.INTEGER)
                            .ofNativeType("SERIAL").nullable(false).withColumn("person name").ofSize(255)
                            .withColumn("age").ofType(ColumnType.INTEGER).execute();
                    assertEquals("[id, person name, age]", Arrays.toString(table.getColumnNames()));
                    assertEquals(
                            "Column[name=id,columnNumber=0,type=INTEGER,nullable=false,nativeType=serial,columnSize=10]",
                            table.getColumnByName("id").toString());
                    assertEquals(
                            "Column[name=person name,columnNumber=1,type=VARCHAR,nullable=true,nativeType=varchar,columnSize=255]",
                            table.getColumnByName("person name").toString());
                    assertEquals(
                            "Column[name=age,columnNumber=2,type=INTEGER,nullable=true,nativeType=int4,columnSize=10]",
                            table.getColumnByName("age").toString());

                    cb.insertInto(table).value("person name", "John Doe").value("age", 42).execute();
                    cb.insertInto(table).value("age", 43).value("person name", "Jane Doe").execute();

                }
            });

            final Table table = schema.getTableByName("my_table");
            Query query = dc.query().from(table).select(table.getColumns()).toQuery();
            DataSet ds = dc.executeQuery(query);
            assertTrue(ds.next());
            assertEquals("Row[values=[1, John Doe, 42]]", ds.getRow().toString());
            assertTrue(ds.next());
            assertEquals("Row[values=[2, Jane Doe, 43]]", ds.getRow().toString());
            assertFalse(ds.next());
            ds.close();

            dc.executeUpdate(new UpdateScript() {

                @Override
                public void run(UpdateCallback callback) {
                    callback.update(table).value("age", 102).where("id").eq(1).execute();
                    callback.deleteFrom(table).where("id").eq(2).execute();
                }
            });

            ds = dc.executeQuery(query);
            assertTrue(ds.next());
            assertEquals("Row[values=[1, John Doe, 102]]", ds.getRow().toString());
            assertFalse(ds.next());
            ds.close();
        } finally {
            dc.executeUpdate(new UpdateScript() {
                @Override
                public void run(UpdateCallback callback) {
                    callback.dropTable("my_table").execute();
                }
            });
View Full Code Here

        }

        JdbcDataContext dc = new JdbcDataContext(getConnection());
        final Schema schema = dc.getDefaultSchema();
        try {
            dc.executeUpdate(new UpdateScript() {
                @Override
                public void run(UpdateCallback cb) {
                    Table table = cb.createTable(schema, "my_table").withColumn("id").ofType(ColumnType.INTEGER)
                            .ofNativeType("SERIAL").nullable(false).withColumn("person name").ofSize(255)
                            .withColumn("age").ofType(ColumnType.INTEGER).execute();
                    assertEquals("[id, person name, age]", Arrays.toString(table.getColumnNames()));
                    assertEquals(
                            "Column[name=id,columnNumber=0,type=INTEGER,nullable=false,nativeType=serial,columnSize=10]",
                            table.getColumnByName("id").toString());
                    assertEquals(
                            "Column[name=person name,columnNumber=1,type=VARCHAR,nullable=true,nativeType=varchar,columnSize=255]",
                            table.getColumnByName("person name").toString());
                    assertEquals(
                            "Column[name=age,columnNumber=2,type=INTEGER,nullable=true,nativeType=int4,columnSize=10]",
                            table.getColumnByName("age").toString());

                    cb.insertInto(table).value("person name", "John Doe").value("age", 42.4673).execute();
                    cb.insertInto(table).value("age", 43.5673).value("person name", "Jane Doe").execute();
                }
            });

            Table table = schema.getTableByName("my_table");
            Query query = dc.query().from(table).select(table.getColumns()).toQuery();
            DataSet ds = dc.executeQuery(query);
            assertTrue(ds.next());
            // Float value input will be rounded down into integer number.
            assertEquals("Row[values=[1, John Doe, 42]]", ds.getRow().toString());
            assertTrue(ds.next());
            // The age will be incremented as float value input will be rounded
            // up.
            assertEquals("Row[values=[2, Jane Doe, 44]]", ds.getRow().toString());
            assertFalse(ds.next());

            ds.close();
        } finally {
            dc.executeUpdate(new UpdateScript() {
                @Override
                public void run(UpdateCallback cb) {
                    cb.dropTable("my_table").execute();
                }
            });
View Full Code Here

                    "Could not execute batch: INSERT INTO \"public\".\"my_table\" (\"person name\",age) VALUES ('John Doe','42'): Batch entry 0 INSERT INTO \"public\".\"my_table\" (\"person name\",age) VALUES ('John Doe','42') was aborted.  Call getNextException to see the cause.",
                    message);
        } finally {
            dc.refreshSchemas();
            if (dc.getTableByQualifiedLabel("my_table") != null) {
                dc.executeUpdate(new UpdateScript() {
                    @Override
                    public void run(UpdateCallback cb) {
                        cb.dropTable("my_table").execute();
                    }
                });
View Full Code Here

        }

        JdbcDataContext dc = new JdbcDataContext(getConnection());
        final Schema schema = dc.getDefaultSchema();
        try {
            dc.executeUpdate(new UpdateScript() {
                @Override
                public void run(UpdateCallback cb) {
                    Table table = cb.createTable(schema, "my_table").withColumn("id").ofType(ColumnType.INTEGER)
                            .ofNativeType("SERIAL").nullable(false).withColumn("person name").ofSize(255)
                            .withColumn("age").ofType(ColumnType.INTEGER).execute();
                    assertEquals("[id, person name, age]", Arrays.toString(table.getColumnNames()));
                    assertEquals(
                            "Column[name=id,columnNumber=0,type=INTEGER,nullable=false,nativeType=serial,columnSize=10]",
                            table.getColumnByName("id").toString());
                    assertEquals(
                            "Column[name=person name,columnNumber=1,type=VARCHAR,nullable=true,nativeType=varchar,columnSize=255]",
                            table.getColumnByName("person name").toString());
                    assertEquals(
                            "Column[name=age,columnNumber=2,type=INTEGER,nullable=true,nativeType=int4,columnSize=10]",
                            table.getColumnByName("age").toString());

                    for (int i = 0; i < 1000000; i++) {
                        cb.insertInto(table).value("person name", "John Doe").value("age", i + 10).execute();
                    }

                }
            });

            Table table = schema.getTableByName("my_table");
            Query query = dc.query().from(table).selectCount().toQuery();
            DataSet ds = dc.executeQuery(query);
            assertTrue(ds.next());
            assertEquals("Row[values=[1000000]]", ds.getRow().toString());
            assertFalse(ds.next());
            ds.close();
        } finally {
            dc.executeUpdate(new UpdateScript() {
                @Override
                public void run(UpdateCallback cb) {
                    cb.dropTable("my_table").execute();
                }
            });
View Full Code Here

        }

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

        dc.executeUpdate(new UpdateScript() {
            @Override
            public void run(UpdateCallback cb) {
                Table table = cb.createTable(schema, "my_table").withColumn("id").ofType(ColumnType.INTEGER)
                        .ofNativeType("IDENTITY").nullable(false).withColumn("name").ofType(ColumnType.VARCHAR).ofSize(10)
                        .withColumn("foo").ofType(ColumnType.BOOLEAN).nullable(true).withColumn("bar").ofType(ColumnType.BOOLEAN)
                        .nullable(true).execute();

                assertEquals("MY_TABLE", table.getName());
            }
        });

        try {
            dc.executeUpdate(new UpdateScript() {
                @Override
                public void run(UpdateCallback callback) {
                    callback.insertInto("my_table").value("name", "row 1").value("foo", true).execute();

                    callback.insertInto("my_table").value("name", "row 2").value("bar", true).execute();
View Full Code Here

    }

    public void testBothFirstRowAndMaxRows() throws Exception {
        final JdbcDataContext dc = new JdbcDataContext(conn);
        final Schema schema = dc.getDefaultSchema();
        dc.executeUpdate(new UpdateScript() {

            @Override
            public void run(UpdateCallback cb) {
                JdbcCreateTableBuilder createTableBuilder = (JdbcCreateTableBuilder) cb.createTable(schema, "test_table");
                Table writtenTable = createTableBuilder.withColumn("id").asPrimaryKey().ofType(ColumnType.INTEGER).execute();

                for (int i = 0; i < 10; i++) {
                    cb.insertInto(writtenTable).value("id", i + 1).execute();
                }
            }
        });

        Query q = dc.query().from("test_table").select("id").toQuery();
        q.setFirstRow(2);
        q.setMaxRows(4);
        DataSet ds = dc.executeQuery(q);
        assertTrue(ds.next());
        assertEquals("Row[values=[2]]", ds.getRow().toString());
        assertTrue(ds.next());
        assertEquals("Row[values=[3]]", ds.getRow().toString());
        assertTrue(ds.next());
        assertEquals("Row[values=[4]]", ds.getRow().toString());
        assertTrue(ds.next());
        assertEquals("Row[values=[5]]", ds.getRow().toString());
        assertFalse(ds.next());

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

        final Schema schema = dc.getDefaultSchema();
        Table readTable = dc.getDefaultSchema().getTableByName("test_table");
        assertNull(readTable);

        final MutableRef<Table> writtenTableRef = new MutableRef<Table>();
        dc.executeUpdate(new UpdateScript() {

            @Override
            public void run(UpdateCallback cb) {
                JdbcCreateTableBuilder createTableBuilder = (JdbcCreateTableBuilder) cb.createTable(schema, "test_table");
                Table writtenTable = createTableBuilder.withColumn("id").asPrimaryKey().ofType(ColumnType.INTEGER)
                        .withColumn("name").ofSize(255).ofType(ColumnType.VARCHAR).withColumn("age").ofType(ColumnType.INTEGER)
                        .execute();
                String sql = createTableBuilder.createSqlStatement();
                assertEquals("CREATE TABLE PUBLIC.test_table (id INTEGER PRIMARY KEY, name VARCHAR(255), age INTEGER)", sql);
                assertNotNull(writtenTable);
                assertEquals("[ID, NAME, AGE]", Arrays.toString(writtenTable.getColumnNames()));

                writtenTableRef.set(writtenTable);
            }
        });

        assertEquals("[TEST_TABLE]", Arrays.toString(dc.getDefaultSchema().getTableNames()));

        readTable = dc.getDefaultSchema().getTableByName("test_table");
        assertEquals("[ID, NAME, AGE]", Arrays.toString(readTable.getColumnNames()));
        assertEquals("[Column[name=ID,columnNumber=0,type=INTEGER,nullable=false,nativeType=INTEGER,columnSize=10]]",
                Arrays.toString(readTable.getPrimaryKeys()));
        assertEquals(writtenTableRef.get(), readTable);

        assertFalse(conn.isReadOnly());

        dc = new JdbcDataContext(conn);
        assertSame(conn, dc.getConnection());

        readTable = dc.getDefaultSchema().getTableByName("test_table");
        assertEquals("[ID, NAME, AGE]", Arrays.toString(readTable.getColumnNames()));
        assertTrue(writtenTableRef.get().getQualifiedLabel().equalsIgnoreCase(readTable.getQualifiedLabel()));

        dc.executeUpdate(new UpdateScript() {
            @Override
            public void run(UpdateCallback cb) {
                cb.insertInto(writtenTableRef.get()).value("age", 14).value("name", "hello").value("id", 1).execute();
                JdbcInsertBuilder insertBuilder = (JdbcInsertBuilder) cb.insertInto(writtenTableRef.get()).value("age", 15)
                        .value("name", "wor'ld").value("id", 2);
                assertEquals("INSERT INTO PUBLIC.\"TEST_TABLE\" (ID,NAME,AGE) VALUES (?,?,?)", insertBuilder.createSqlStatement());
                insertBuilder.execute();
                cb.insertInto(writtenTableRef.get()).value("age", 16).value("name", "escobar!").value("id", 3).execute();
            }
        });

        DataSet ds = dc.query().from(readTable).select(readTable.getColumns()).orderBy("id").execute();
        assertTrue(ds.next());
        assertEquals("Row[values=[1, hello, 14]]", ds.getRow().toString());
        assertTrue(ds.next());
        assertEquals("Row[values=[2, wor'ld, 15]]", ds.getRow().toString());
        assertTrue(ds.next());
        assertEquals("Row[values=[3, escobar!, 16]]", ds.getRow().toString());
        assertFalse(ds.next());
        ds.close();

        dc.executeUpdate(new UpdateScript() {
            @Override
            public void run(UpdateCallback callback) {
                JdbcUpdateBuilder updateCallback = (JdbcUpdateBuilder) callback.update("test_table").value("age", 18).where("id")
                        .greaterThan(1);
                assertEquals("UPDATE PUBLIC.\"TEST_TABLE\" SET AGE=? WHERE \"TEST_TABLE\".\"ID\" > ?",
                        updateCallback.createSqlStatement());
                updateCallback.execute();
            }
        });

        ds = dc.query().from(readTable).select(readTable.getColumns()).orderBy("id").execute();
        assertTrue(ds.next());
        assertEquals("Row[values=[1, hello, 14]]", ds.getRow().toString());
        assertTrue(ds.next());
        assertEquals("Row[values=[2, wor'ld, 18]]", ds.getRow().toString());
        assertTrue(ds.next());
        assertEquals("Row[values=[3, escobar!, 18]]", ds.getRow().toString());
        assertFalse(ds.next());
        ds.close();

        dc.executeUpdate(new UpdateScript() {
            @Override
            public void run(UpdateCallback callback) {
                callback.deleteFrom("test_table").where("age").greaterThan(15).execute();
            }
        });

        ds = dc.query().from(readTable).select(readTable.getColumns()).orderBy("id").execute();
        assertTrue(ds.next());
        assertEquals("Row[values=[1, hello, 14]]", ds.getRow().toString());
        assertFalse(ds.next());
        ds.close();

        assertEquals("[TEST_TABLE]", Arrays.toString(dc.getDefaultSchema().getTableNames()));

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

        } catch (SQLException e) {
            // do nothing
        }

        final JdbcDataContext dc = new JdbcDataContext(conn);
        dc.executeUpdate(new UpdateScript() {
            @Override
            public void run(UpdateCallback callback) {
                Table table = callback.createTable(dc.getDefaultSchema(), "test_table").withColumn("foo")
                        .ofType(ColumnType.INTEGER).withColumn("bar").ofType(ColumnType.VARCHAR).execute();
                callback.insertInto(table).value("foo", 1).value("bar", "hello").execute();
                callback.insertInto(table).value("foo", 2).value("bar", "there").execute();
                callback.insertInto(table).value("foo", 3).value("bar", "world").execute();
            }
        });

        Table table = dc.getTableByQualifiedLabel("test_table");
        Query query = new Query().from(table, "a").from(table, "b");
        query.select(table.getColumnByName("foo"), query.getFromClause().getItem(0));
        query.select(table.getColumnByName("foo"), query.getFromClause().getItem(1));
        query.where(new SelectItem(table.getColumnByName("bar"), query.getFromClause().getItem(0)), OperatorType.EQUALS_TO,
                "hello");

        assertEquals(
                "SELECT a.\"FOO\", b.\"FOO\" FROM PUBLIC.\"TEST_TABLE\" a, PUBLIC.\"TEST_TABLE\" b WHERE a.\"BAR\" = 'hello'",
                query.toSql());

        DataSet ds = dc.executeQuery(query);
        assertTrue(ds.next());
        assertEquals("Row[values=[1, 1]]", ds.getRow().toString());
        assertTrue(ds.next());
        assertEquals("Row[values=[1, 2]]", ds.getRow().toString());
        assertTrue(ds.next());
        assertEquals("Row[values=[1, 3]]", ds.getRow().toString());
        assertFalse(ds.next());
        ds.close();

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