Okay, on the common library I work on I have a database system, with implementations of MySQL and SQLite.
Because I like reusing code I use PreparedStatements like this.
I got the Statement enum:
And then I have the MySQL executeStatement:public enum Statement { // just an example SELECT("SELECT '?' FROM '?';"); private String statement = null; Statement(String statement) { this.statement = statement; } public String getStatement() { return statement; } }
Alright, so here's the thing, as this is a library I don't want to add new statements to the enum all the time, that just won't work.public int executeStatement(Statement statement, Object... args) { try { PreparedStatement ps = conn.prepareStatement(statement.getStatement()); statementsPrepared++; Logger.log(LogLevel.DEBUG, String.format("Prepared statement(%s): %s", statementsPrepared, statement.name())); for(int i = 1; i<args.length; i++) { ps.setObject(i, args[i]); } Logger.log(LogLevel.DEBUG, String.format("Set %s objects for statement(%s)", args.length, statementsPrepared)); Logger.log(LogLevel.DEBUG, String.format("Executing statement(%s)", statementsPrepared)); return ps.executeUpdate(); } catch(SQLException e) { Logger.log(LogLevel.ERROR, e.getMessage()); return e.getErrorCode(); } }
I've seen those posts around the web with a kind off dynamic enum, like the one there you implement the values() and valueOf() methods yourself. But they won't allow direct access like a real enum does, and that's what I want. Is there any approach for doing that?
Thanks in advance.