Skip to content
This repository has been archived by the owner on Jun 27, 2023. It is now read-only.

Generated DB Schema

Alex edited this page Dec 17, 2015 · 2 revisions

With droitatedDB set up correctly there will be a generated db schema available which allows to reference the tables and columns from the java code. This allows you to reference them without any hard coded Strings which could easily break during a refactoring.

As an example this is the genrated db schema from our Notes example:

public interface DB {

    public static final String DB_NAME = "notes.db";
    public static final String DB_UPDATE_HOOK = "com.arconsis.notes.db.UpdateHook";
    public static final int DB_VERSION = 1;

    public static final EntityInfo UserInfo = new EntityInfo("com.arconsis.notes.db.User", "User", UserTable.class);
    public static final EntityInfo NoteInfo = new EntityInfo("com.arconsis.notes.db.Note", "Note", NoteTable.class);

    public interface UserTable {
        public static final String CLASS_NAME = "com.arconsis.notes.db.User";
        public static final String TABLE_NAME = "User";

        public static final IntegerAttribute _ID = new IntegerAttribute("_id", java.lang.Integer.class, 0);
        public static final TextAttribute NAME = new TextAttribute("name", java.lang.String.class, 1);
        public static final TextAttribute PASSWORD = new TextAttribute("password", java.lang.String.class, 2);

        public static final String SQL_CREATION = "CREATE TABLE User (_id Integer PRIMARY KEY AUTOINCREMENT, name Text, password Text)";
        public static final String SQL_INDEX = "CREATE INDEX user_idx on User (_id)";
        public static final String[] PROJECTION = new String[]{"_id", "name", "password"};
        public static final AbstractAttribute[] ATTRIBUTES = new AbstractAttribute[]{_ID, NAME, PASSWORD};

        public interface Associations {
            public static final ToManyAssociation NOTES = new ToManyAssociation("notes", com.arconsis.notes.db.Note.class, UserNoteAssociation.class);
        }
    }

    public interface NoteTable {
        public static final String CLASS_NAME = "com.arconsis.notes.db.Note";
        public static final String TABLE_NAME = "Note";

        public static final IntegerAttribute _ID = new IntegerAttribute("_id", java.lang.Integer.class, 0);
        public static final TextAttribute CONTENT = new TextAttribute("content", java.lang.String.class, 1);
        public static final IntegerAttribute CREATED = new IntegerAttribute("created", java.util.Date.class, 2);
        public static final TextAttribute TITLE = new TextAttribute("title", java.lang.String.class, 3);
        public static final IntegerAttribute FK_USER = new IntegerAttribute("user", "fk_user", com.arconsis.notes.db.User.class, 4);

        public static final String SQL_CREATION = "CREATE TABLE Note (_id Integer PRIMARY KEY AUTOINCREMENT, content Text, created Integer, title Text, fk_user Integer)";
        public static final String SQL_INDEX = "CREATE INDEX note_idx on Note (_id, fk_user)";
        public static final String[] PROJECTION = new String[]{"_id", "content", "created", "title", "fk_user"};
        public static final AbstractAttribute[] ATTRIBUTES = new AbstractAttribute[]{_ID, CONTENT, CREATED, TITLE};

        public interface Associations {
            public static final ToOneAssociation USER = new ToOneAssociation("user", com.arconsis.notes.db.User.class, FK_USER);
        }
    }

    public interface UserNoteAssociation {
        public static final String TABLE_NAME = "UserNoteAssociation";

        public static final IntegerAttribute FK_USER = new IntegerAttribute("", "fk_user", com.arconsis.notes.db.User.class, 0);
        public static final IntegerAttribute FK_NOTE = new IntegerAttribute("", "fk_note", com.arconsis.notes.db.Note.class, 1);

        public static final String SQL_CREATION = "CREATE TABLE UserNoteAssociation(fk_user Integer, fk_note Integer, UNIQUE(fk_user, fk_note) ON CONFLICT IGNORE)";
        public static final String SQL_INDEX = "CREATE INDEX usernote_idx on UserNoteAssociation (fk_user, fk_note)";
        public static final String[] PROJECTION = new String[]{"fk_user", "fk_note"};
        public static final AbstractAttribute[] ATTRIBUTES = new AbstractAttribute[]{FK_USER, FK_NOTE};
    }
}

With this db schema you can easily reference the column name for e.g the foreign key to the user in the note table

DB.NoteTable.FK_USER.columnName()

The generated db schema is also used for droitatedDB internal processing for faster access to the model information during runtime to avoid unnecessary reflection calls. With future releases there will probably be more information contained in the generated db schema.