I want to create an enum within an enum for sql query in java. For example I want to say table.create and it would return CREATE TABLE or database.create and it would return CREATE DATABASE. How can I do this?
enum SQL {
    table("ALTER,CREATE"),
    database("CREATE");
}
                Define an enum within the enum:
public static enum SQL {
    table(Command.ALTER,Command.CREATE),
    database(Command.CREATE);
    public static enum Command {
        CREATE,
        ALTER
    }
    private final Command[] commands;
    private SQL (Command... commands) {
        this.commands = commands;
    }
    public Command[] getCommands() {
        return commands;
    }
}
Although you can do this, it would be better to declare the Command enum in its own class/file. I haven't seen anyone declare an enum inside another enum before... I almost like it.
Why make it an enum within an enum?
Table.java
public enum Table {
    CREATE("CREATE TABLE"),
    ALTER("ALTER TABLE");
    private String cmd;
    Table(String cmd) {
       this.cmd = cmd;
    }
    @Override
    public String toString() {
        return cmd;
    }
}
Database.java
public enum Database {
    CREATE("CREATE DATABASE");
    private String cmd;
    Database(String cmd) {
       this.cmd = cmd;
    }
    @Override
    public String toString() {
        return cmd;
    }
}
With this example, System.out.println(Table.CREATE); prints CREATE TABLE.
This will also aid in readabilty becuase you can produce code like:
String query = Table.CREATE + "(Column1 " + DbType.INTEGER + " " + Column.UNIQUE + " " + Column.PRIMARY_KEY + ")";
Which would be a bit easier to read and understand, IMO.
EDIT
To attempt to get close to what you're after you could do something like:
public enum SQL {
    TABLE("TABLE", SQL.CREATE_FLAG | SQL.ALTER_FLAG),
    DATABASE("DATABASE", SQL.CREATE_FLAG);
    public static final int CREATE_FLAG = 1;
    public static final int ALTER_FLAG = 2;
    public static final String CREATE_STRING = "CREATE";
    public static final String ALTER_STRING = "ALTER";
    public static final String INVALID = "INVALID";
    private String name;
    private boolean create;
    private boolean alter;
    SQL(String name, int flags) {
        create = (flags & CREATE_FLAG) != 0;
        alter = (flags & ALTER_FLAG) != 0;
        this.name = name;
    }
    public String create() {
        if (create)
            return CREATE_STRING + " " + name;
        else
            return INVALID;
    }
    public String alter() {
        if (alter)
            return ALTER_STRING + " " + name;
        else
            return INVALID;
    }
}
Where you can call:
System.out.println(SQL.TABLE.create());    // CREATE TABLE
System.out.println(SQL.TABLE.alter());     // ALTER TABLE
System.out.println(SQL.DATABASE.alter());  // INVALID
System.out.println(SQL.DATABASE.create()); // CREATE DATABASE
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With