01: package biz.hammurapi.sql;
02:
03: import java.sql.Connection;
04: import java.sql.SQLException;
05: import java.text.MessageFormat;
06:
07: /**
08: * Generates identities by executing specified SQL statement
09: * @author Pavel Vlasov
10: *
11: */
12: public class GenericIdentityGenerator implements IdentityGenerator {
13:
14: private boolean invariant;
15: private MessageFormat mf;
16: private Object[] args = new Object[] { null };
17: private String sql;
18:
19: /**
20: * Constructor
21: * @param pattern - SQL statement pattern. {0} stands for table name.
22: */
23: public GenericIdentityGenerator(String pattern) {
24: invariant = pattern.indexOf("{0}") == -1;
25: if (invariant) {
26: this .sql = pattern;
27: } else {
28: mf = new MessageFormat(pattern);
29: }
30: }
31:
32: public int generate(Connection con, String name)
33: throws SQLException {
34: String esql;
35: if (invariant) {
36: esql = this .sql;
37: } else {
38: esql = mf.format(args, new StringBuffer(), null).toString();
39: }
40:
41: return new SQLProcessor(con, null).projectSingleInt(esql, null);
42: }
43:
44: }
|