org.apache.commons.dbutils.handlers.ColumnListHandler Java Examples

The following examples show how to use org.apache.commons.dbutils.handlers.ColumnListHandler. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Db2ReOrgStatementExecutorIT.java    From obevo with Apache License 2.0 6 votes vote down vote up
private void createTable(Connection conn, String tableName, boolean requireReorg) {
    try {
        executorJdbc.update(conn, "DROP TABLE " + tableName);
    } catch (Exception ignore) {
        // Ignoring the exception, as no clear "DROP TABLE IF EXISTS" is
        // available in DB2
    }

    executorJdbc.update(conn, "create table " + tableName + " (a integer, b integer, c integer, d integer, e integer) ");
    executorJdbc.update(conn, "insert into " + tableName + " (a) values (3)");
    MutableList<String> expectedColumns;
    if (requireReorg) {
        executorJdbc.update(conn, "alter table " + tableName + " drop column b");
        executorJdbc.update(conn, "alter table " + tableName + " drop column c");
        executorJdbc.update(conn, "alter table " + tableName + " drop column d");
        expectedColumns = Lists.mutable.with("A", "E");
    } else {
        expectedColumns = Lists.mutable.with("A", "B", "C", "D", "E");
    }
    // Assert the columns which are available in table A
    String columnListSql = "select colname from syscat.COLUMNS where tabschema = '" + physicalSchema.getPhysicalName() + "' AND tabname = '"
            + tableName + "'";
    List<String> columnsInTableA = this.db2JdbcTemplate.query(conn, columnListSql,
            new ColumnListHandler<String>());
    assertEquals(expectedColumns, FastList.newList(columnsInTableA));
}
 
Example #2
Source File: EmptyDatabaseTest.java    From CodeDefenders with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Checks whether the {@code empty.db} script creates an empty database.
 */
@Test
public void testCleanDB() throws Exception {
    try (Connection conn = db.getConnection()) {
        QueryRunner qr = new QueryRunner();

        List<String> results = qr.query(conn, "SELECT * FROM classes;", new ColumnListHandler<>());
        assertEquals(0, results.size());

        final String query2 = String.join("\n",
                "SELECT *",
                "FROM games",
                "WHERE ID > 0"
        );
        results = qr.query(conn, query2, new ColumnListHandler<>());
        assertEquals(0, results.size());

        results = qr.query(conn, "SELECT * FROM mutants;", new ColumnListHandler<>());
        assertEquals(0, results.size());

        results = qr.query(conn, "SELECT * FROM tests;", new ColumnListHandler<>());
        assertEquals(0, results.size());
    }
}
 
Example #3
Source File: Db2MetadataDialect.java    From obevo with Apache License 2.0 5 votes vote down vote up
@Override
public ImmutableSet<String> getGroupNamesOptional(Connection conn, PhysicalSchema physicalSchema) throws SQLException {
    return Sets.immutable
            .withAll(jdbc.query(conn, "select ROLENAME from sysibm.SYSROLES", new ColumnListHandler<String>()))
            .newWithAll(jdbc.query(conn, "select GRANTEE from sysibm.SYSDBAUTH", new ColumnListHandler<String>()))
            .collect(StringFunctions.trim());  // db2 sometimes has whitespace in its return results that needs trimming
}
 
Example #4
Source File: DataFactory.java    From xiaoyaoji with GNU General Public License v3.0 5 votes vote down vote up
@Override
public List<String> getAllProjectValidIds() {
    return process(new Handler<List<String>>() {
        @Override
        public List<String> handle(Connection connection, QueryRunner qr) throws SQLException {
            return qr.query(connection,"select id from project where status='VALID' and permission='PUBLIC' order by createTime desc ",new ColumnListHandler<String>("id"));
        }
    });
}
 
Example #5
Source File: DataFactory.java    From xiaoyaoji with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void initUserThirdlyBinds(final User user) {
    process(new Handler<Object>() {
        @Override
        public Object handle(Connection connection, QueryRunner qr) throws SQLException {
            List<String> columns = qr.query(connection,"select type from user_third where userId = ?",new ColumnListHandler<String>("type"),user.getId());
            if(columns!=null && columns.size()>0){
                for(String type:columns){
                    user.getBindingMap().put(type,true);
                }
            }
            return null;
        }
    });
}
 
Example #6
Source File: DefaultDataAccessor.java    From smart-framework with Apache License 2.0 5 votes vote down vote up
@Override
public <T> List<T> queryColumnList(String sql, Object... params) {
    List<T> list;
    try {
        list = queryRunner.query(sql, new ColumnListHandler<T>(), params);
    } catch (SQLException e) {
        logger.error("查询出错!", e);
        throw new RuntimeException(e);
    }
    printSQL(sql);
    return list;
}
 
Example #7
Source File: DefaultDataAccessor.java    From smart-framework with Apache License 2.0 5 votes vote down vote up
@Override
public <T> List<T> queryColumnList(String sql, Object... params) {
    List<T> list;
    try {
        list = queryRunner.query(sql, new ColumnListHandler<T>(), params);
    } catch (SQLException e) {
        logger.error("查询出错!", e);
        throw new RuntimeException(e);
    }
    printSQL(sql);
    return list;
}
 
Example #8
Source File: OracleMetadataDialect.java    From obevo with Apache License 2.0 4 votes vote down vote up
@Override
public ImmutableSet<String> getGroupNamesOptional(Connection conn, PhysicalSchema physicalSchema) throws SQLException {
    return Sets.immutable
            .withAll(jdbc.query(conn, "select ROLE from DBA_ROLES", new ColumnListHandler<String>()))
            .collect(StringFunctions.trim());
}
 
Example #9
Source File: HsqlMetadataDialect.java    From obevo with Apache License 2.0 4 votes vote down vote up
@Override
public ImmutableSet<String> getGroupNamesOptional(Connection conn, PhysicalSchema physicalSchema) throws SQLException {
    return Sets.immutable.<String >withAll(jdbc.query(conn, "select ROLE_NAME from INFORMATION_SCHEMA.APPLICABLE_ROLES", new ColumnListHandler<String>()));
}
 
Example #10
Source File: HsqlMetadataDialect.java    From obevo with Apache License 2.0 4 votes vote down vote up
@Override
public ImmutableSet<String> getUserNamesOptional(Connection conn, PhysicalSchema physicalSchema) throws SQLException {
    return Sets.immutable.<String>withAll(jdbc.query(conn, "select USER_NAME from INFORMATION_SCHEMA.SYSTEM_USERS", new ColumnListHandler<String>()));
}
 
Example #11
Source File: PostgresqlMetadataDialect.java    From obevo with Apache License 2.0 4 votes vote down vote up
@Override
public ImmutableSet<String> getGroupNamesOptional(Connection conn, PhysicalSchema physicalSchema) throws SQLException {
    return Sets.immutable
            .withAll(jdbc.query(conn, "select rolname from pg_roles", new ColumnListHandler<String>()))
            .collect(StringFunctions.trim());
}
 
Example #12
Source File: SybaseAseMetadataDialect.java    From obevo with Apache License 2.0 4 votes vote down vote up
@Override
public ImmutableSet<String> getGroupNamesOptional(Connection conn, PhysicalSchema physicalSchema) throws SQLException {
    return ListAdapter.adapt(jdbc.query(conn, physicalSchema.getPhysicalName() + "..sp_helpgroup", new ColumnListHandler<String>("Group_name"))).toSet().toImmutable();
}
 
Example #13
Source File: SybaseAseMetadataDialect.java    From obevo with Apache License 2.0 4 votes vote down vote up
@Override
public ImmutableSet<String> getUserNamesOptional(Connection conn, PhysicalSchema physicalSchema) throws SQLException {
    return ListAdapter.adapt(jdbc.query(conn, physicalSchema.getPhysicalName() + "..sp_helpuser", new ColumnListHandler<String>("Users_name"))).toSet().toImmutable();
}
 
Example #14
Source File: Db2DeployerMainIT.java    From obevo with Apache License 2.0 4 votes vote down vote up
@Test
public void testDeploy() throws Exception {
    int stepsToRun = 3;  // this toggle is here to help w/ local testing

    DbDeployerAppContext dbDeployerAppContext = null;

    if (stepsToRun >= 0) {
        System.out.println("Running step 0");
        dbDeployerAppContext = getAppContext.valueOf(1);
        dbDeployerAppContext
                .setupEnvInfra()
                .cleanEnvironment()
                .deploy(new MainDeployerArgs().reason("try1").deployExecutionAttributes(Sets.immutable.with(
                        new DeployExecutionAttributeImpl("A", "aval"),
                        new DeployExecutionAttributeImpl("B", "bval")
                )))
        ;
    }

    if (stepsToRun >= 1) {
        System.out.println("Running step 1");
        dbDeployerAppContext = getAppContext.valueOf(1);
        dbDeployerAppContext
                .deploy(new MainDeployerArgs().reason("try1a-noop").deployExecutionAttributes(Sets.immutable.with(
                        new DeployExecutionAttributeImpl("A", "aval"),
                        new DeployExecutionAttributeImpl("B", "bval"),
                        new DeployExecutionAttributeImpl("C", "cval")
                )))
        ;
    }

    if (stepsToRun >= 2) {
        System.out.println("Running step 2");
        dbDeployerAppContext = getAppContext.valueOf(2);
        dbDeployerAppContext
                .setupEnvInfra()
                .deploy(new MainDeployerArgs().reason("try2").deployExecutionAttributes(Sets.immutable.with(
                        new DeployExecutionAttributeImpl("C", "cval2"),
                        new DeployExecutionAttributeImpl("E", "eval")
                )))
        ;
    }

    if (stepsToRun >= 3) {
        System.out.println("Running step 3");
        dbDeployerAppContext = getAppContext.valueOf(3);
        dbDeployerAppContext
                .setupEnvInfra()
                .deploy(new MainDeployerArgs().reason("try3").deployExecutionAttributes(Sets.immutable.with(
                        new DeployExecutionAttributeImpl("F", "fval")
                )))
        ;
    }

    String schema1 = "DBDEPLOY01";
    MutableList<DeployExecution> executions = dbDeployerAppContext.getDeployExecutionDao().getDeployExecutions(schema1).toSortedListBy(new Function<DeployExecution, Long>() {
        @Override
        public Long valueOf(DeployExecution deployExecution) {
            return deployExecution.getId();
        }
    });
    assertThat(executions, hasSize(4));
    DeployExecution execution4 = dbDeployerAppContext.getDeployExecutionDao().getLatestDeployExecution(schema1);
    verifyExecution1(executions.get(0));
    verifyExecution1a(executions.get(1));
    verifyExecution2(executions.get(2));
    verifyExecution3(executions.get(3));
    verifyExecution3(execution4);

    JdbcHelper db2JdbcTemplate = new JdbcHelper();

    // Assert the columns which are available in table TEST_TABLE
    String schema = dbDeployerAppContext.getEnvironment().getPhysicalSchema("DBDEPLOY01").getPhysicalName();
    String columnListSql = "select colname from syscat.COLUMNS where tabschema = '" + schema + "' AND tabname = 'TABLE_D'";
    Connection conn = ds.getConnection();
    try {
        List<String> columnsInTestTable = db2JdbcTemplate.query(conn, columnListSql, new ColumnListHandler<String>());
        Assert.assertEquals(Lists.mutable.with("D_ID"), FastList.newList(columnsInTestTable));
    } finally {
        DbUtils.closeQuietly(conn);
    }
}
 
Example #15
Source File: Db2ReOrgStatementExecutorIT.java    From obevo with Apache License 2.0 4 votes vote down vote up
private void performReorgExecution(final boolean autoReorgEnabled, final int errorCode, final boolean testBatchUpdate) {
    this.setupExecutor(autoReorgEnabled);
    this.executor.executeWithinContext(physicalSchema, new Procedure<Connection>() {
        @Override
        public void value(Connection conn) {
            try {
                executorJdbc.update(conn, "DROP TABLE a");
            } catch (Exception ignore) {
                // Ignoring the exception, as no clear "DROP TABLE IF EXISTS" is
                // available in DB2
            }

            executorJdbc.update(conn, "create table a (a integer, b integer, c integer, d integer, e integer) ");
            executorJdbc.update(conn, "insert into a (a) values (3)");
            executorJdbc.update(conn, "alter table a drop column b");
            executorJdbc.update(conn, "alter table a drop column c");
            executorJdbc.update(conn, "alter table a drop column d");

            MutableSet<String> expectedColumns = null;
            try {
                Object[][] batchArgs = new Object[][] { new Object[0] };  // need single row of empty args for the batchUpdate calls below
                // this next statement will fire a reorg
                switch (errorCode) {
                case 668:
                    expectedColumns = Sets.mutable.with("A", "E");
                    if (testBatchUpdate) {
                        executorJdbc.batchUpdate(conn, "insert into a (a) values (5)", batchArgs);
                    } else {
                        executorJdbc.update(conn, "insert into a (a) values (5)");
                    }
                    break;
                case 20054:
                    expectedColumns = Sets.mutable.with("A");
                    if (testBatchUpdate) {
                        executorJdbc.batchUpdate(conn, "alter table a drop column e", batchArgs);
                    } else {
                        executorJdbc.update(conn, "alter table a drop column e");
                    }
                    break;
                default:
                    throw new IllegalArgumentException("Unsupported error code for this test: " + errorCode);
                }

                if (!autoReorgEnabled) {
                    fail("Expected an exception here if we do not have autoReorgEnabled");
                }
            } catch (RuntimeException e) {
                if (autoReorgEnabled) {
                    fail("If reorg is enabled, then we should not have thrown an exception here: " + e.getMessage());
                } else {
                    return;
                }
            }

            // Assert the columns which are available in table A
            String columnListSql = "select colname from syscat.COLUMNS where tabschema = '" + physicalSchema.getPhysicalName() + "' AND tabname = 'A'";
            List<String> columnsInTableA = db2JdbcTemplate.query(conn, columnListSql,
                    new ColumnListHandler<String>());
            assertEquals(expectedColumns, Sets.mutable.withAll(columnsInTableA));
        }
    });
}