Examples of JdbcDataContext


Examples of org.apache.metamodel.jdbc.JdbcDataContext

        }
        JdbcTestTemplates.interpretationOfNulls(getConnection());
    }

    private JdbcDataContext createLimitAndOffsetTestData() {
        final JdbcDataContext dc = new JdbcDataContext(getConnection());

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

        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();
            }
        });

        dc.refreshSchemas();

        return dc;
    }
View Full Code Here

Examples of org.apache.metamodel.jdbc.JdbcDataContext

    public void testLimit() throws Exception {
        if (!isConfigured()) {
            return;
        }

        JdbcDataContext dc = createLimitAndOffsetTestData();
        Schema schema = dc.getDefaultSchema();
        Table productsTable = schema.getTableByName("test_table");

        DataSet ds = dc.query().from(productsTable).select("foo").limit(2).execute();
        assertTrue(ds.next());
        assertEquals("Row[values=[1]]", ds.getRow().toString());
        assertTrue(ds.next());
        assertEquals("Row[values=[2]]", ds.getRow().toString());
        assertFalse(ds.next());
View Full Code Here

Examples of org.apache.metamodel.jdbc.JdbcDataContext

    public void testOffset() throws Exception {
        if (!isConfigured()) {
            return;
        }

        JdbcDataContext dc = createLimitAndOffsetTestData();
        Schema schema = dc.getDefaultSchema();
        Table productsTable = schema.getTableByName("test_table");

        DataSet ds = dc.query().from(productsTable).select("foo").offset(1).execute();
        assertTrue(ds.next());
        assertEquals("Row[values=[2]]", ds.getRow().toString());
        assertTrue(ds.next());
        assertEquals("Row[values=[3]]", ds.getRow().toString());
        assertFalse(ds.next());
View Full Code Here

Examples of org.apache.metamodel.jdbc.JdbcDataContext

    public void testLimitAndOffset() throws Exception {
        if (!isConfigured()) {
            return;
        }
        JdbcDataContext dc = createLimitAndOffsetTestData();
        Schema schema = dc.getDefaultSchema();
        Table productsTable = schema.getTableByName("test_table");

        DataSet ds = dc.query().from(productsTable).select("foo").limit(1).offset(1).execute();
        assertTrue(ds.next());
        assertEquals("Row[values=[2]]", ds.getRow().toString());
        assertFalse(ds.next());

        ds.close();
View Full Code Here

Examples of org.apache.metamodel.jdbc.JdbcDataContext

            connection.createStatement().execute("DROP TABLE my_table");
        } catch (Exception e) {
            // do nothing
        }

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

        // create table
        dc.executeUpdate(new UpdateScript() {
            @Override
            public void run(UpdateCallback cb) {
                Table table = cb.createTable(schema, "my_table").withColumn("id").asPrimaryKey()
                        .ofType(ColumnType.INTEGER).ofNativeType("SERIAL").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());
            }
        });

        assertTrue(dc.getColumnByQualifiedLabel("my_table.id").isPrimaryKey());
        assertFalse(dc.getColumnByQualifiedLabel("my_table.name").isPrimaryKey());

        // insert records
        dc.executeUpdate(new UpdateScript() {
            @Override
            public void run(UpdateCallback callback) {
                RowInsertionBuilder builder = callback.insertInto("my_table").value("name", "row 1").value("foo", true);

                try {
                    Method method = builder.getClass().getDeclaredMethod("createSqlStatement");
                    method.setAccessible(true);
                    Object result = method.invoke(builder);
                    assertEquals("INSERT INTO \"public\".\"my_table\" (name,foo) VALUES (?,?)", result.toString());
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }

                builder.execute();

                callback.insertInto("my_table").value("name", "row 2").value("foo", false).execute();
            }
        });

        // query
        DataSet ds = dc.query().from("my_table").select("name").where("foo").eq(true).execute();
        assertTrue(ds.next());
        assertEquals("Row[values=[row 1]]", ds.getRow().toString());
        assertFalse(ds.next());
        ds.close();

        // drop
        dc.executeUpdate(new UpdateScript() {

            @Override
            public void run(UpdateCallback callback) {
                callback.dropTable("my_table").execute();
            }
View Full Code Here

Examples of org.apache.metamodel.jdbc.JdbcDataContext

            connection.createStatement().execute("DROP TABLE my_table");
        } catch (Exception e) {
            // do nothing
        }

        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("SERIAL").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();

                    callback.insertInto("my_table").value("name", "row 3").value("foo", true).execute();

                    callback.insertInto("my_table").value("name", "row 4").value("foo", true).execute();

                    callback.insertInto("my_table").value("name", "row 5").value("bar", true).execute();

                    callback.insertInto("my_table").value("name", "row 6").value("foo", true).value("bar", true)
                            .execute();

                    callback.insertInto("my_table").value("name", "row 7").value("foo", true).value("bar", true)
                            .execute();

                    callback.insertInto("my_table").value("name", "row 8").value("foo", false).value("bar", false)
                            .execute();
                }
            });

            DataSet ds = dc.query().from("my_table").select("id").and("name").execute();
            assertTrue(ds.next());
            assertEquals("Row[values=[1, row 1]]", ds.getRow().toString());
            assertTrue(ds.next());
            assertEquals("Row[values=[2, row 2]]", ds.getRow().toString());
            assertTrue(ds.next());
            assertEquals("Row[values=[3, row 3]]", ds.getRow().toString());
            assertTrue(ds.next());
            assertEquals("Row[values=[4, row 4]]", ds.getRow().toString());
            assertTrue(ds.next());
            assertEquals("Row[values=[5, row 5]]", ds.getRow().toString());
            assertTrue(ds.next());
            assertEquals("Row[values=[6, row 6]]", ds.getRow().toString());
            assertTrue(ds.next());
            assertEquals("Row[values=[7, row 7]]", ds.getRow().toString());
            assertTrue(ds.next());
            assertEquals("Row[values=[8, row 8]]", 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

Examples of org.apache.metamodel.jdbc.JdbcDataContext

    public void testBoolean() throws Exception {
        if (!isConfigured()) {
            return;
        }

        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

Examples of org.apache.metamodel.jdbc.JdbcDataContext

    public void testBlob() throws Exception {
        if (!isConfigured()) {
            return;
        }

        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

Examples of org.apache.metamodel.jdbc.JdbcDataContext

    public void testCreateTableAndWriteRecords() throws Exception {
        if (!isConfigured()) {
            return;
        }

        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();
                }
            });
            assertNull(dc.getTableByQualifiedLabel("my_table"));
        }
    }
View Full Code Here

Examples of org.apache.metamodel.jdbc.JdbcDataContext

    public void testCreateTableInsertValueFloatForIntColumn() throws Exception {
        if (!isConfigured()) {
            return;
        }

        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
TOP
Copyright © 2018 www.massapi.com. 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.