<T> T nextval(final PrimaryKeyColumn pkcol, Class<T> type, Connection cx) throws IOException {
if (CharSequence.class.isAssignableFrom(type)) {
return type.cast(Util.uuid());
}
else if (Number.class.isAssignableFrom(type) && type == Long.class ||
type == Integer.class || type == Short.class || type == Byte.class ||
BigInteger.class.isAssignableFrom(type) || BigDecimal.class.isAssignableFrom(type)) {
return type.cast(pg.run(new DbOP<Number>() {
@Override
protected Number doRun(Connection cx) throws Exception {
SQL sql = new SQL("SELECT max(").name(pkcol.getName()).add(")+1 FROM ")
.name(table.schema(), table.name());
Statement st = open(cx.createStatement());
ResultSet rs = open(st.executeQuery(sql.toString()));
if (rs.next()) {
return (Number) rs.getObject(1);
}
return 1;
}
}, cx));
}
else {
throw new IllegalArgumentException(String.format(Locale.ROOT,
"Unable to generate value for %s.%s", schema().name(), pkcol.getName()));
}
}