@Test public void join() { assertEquals("foo,bar,baz", StringHelper.join(",", new String[] {"foo", "bar", "baz"})); assertEquals("foobarbaz", StringHelper.join(null, new String[] {"foo", "bar", "baz"})); // TODO: is this the intended behavior? assertEquals("foo,,bar", StringHelper.join(",", new String[] {"foo", null, "bar"})); assertNull(StringHelper.join(null, null)); }
public static List<String> listDatabases(Params params) { DataSource datasource = createDataSource(params, params.basedb); JdbcTemplate jdbc = new JdbcTemplate(datasource); RowMapper<String> mapper = new RowMapper<String>() { public String mapRow(ResultSet rs, int rowNum) throws SQLException { return rs.getString("datname"); } }; List<String> databases = jdbc.query("select datname from pg_catalog.pg_database", mapper); System.out.println("databases: " + StringHelper.join(databases, ", ")); return databases; }
// some or all may be empty // remove empty ones, surround the rest with parentheses, and join with " AND " public static String joinSubqueries(String... subqueries) { List<String> items = StringHelper.clean(subqueries); items = StringHelper.wrap(items, "(", ")"); return StringHelper.join(items, " AND "); }