TimotheeJeannin / ProviGen

Easily make a ContentProvider from an annotated ContractClass.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

PRIMARY KEY without AUTOINCREMENT

VincentJousse opened this issue · comments

Hi,

I need a database table whose primary key will be a UUID saved in a STRING.
The TableBuilder adds automatically the AUTOINCREMENT keyword while it is only allowed on an INTEGER PRIMARY KEY.

An annotation parameter may be added to the "@id" annotation to make the PRIMARY KEY automatically generated or not.

Keep in mind that SQLite supports a limited subset of the ALTER TABLE command and column modification is not supported. If we had a @Id(autoincrement = false) annotation, a developer would be able to modify the @Id annotation after the initial table creation to @Id(autoincrement = true). The change would have no effect as we can't modify an existing column. So if we want the annotated contract to actually reflect the table schema we need to re-create the table with the AUTOINCREMENT option and provide to the developer a way to handle all the already present data that conflicts with the new primary key column. This will lead to some complicated logic and it will be difficult to understand what does ProviGen do and when.

I feel like it makes more sense to modify the TableBuilder so that it can handle the AUTOINCREMENT option and let the developer manage table creation himself.

The same issue exists for the @id annotation itself, right ?!! If a developer moves the @id annotation from one field to another, we would have to re-create the table ...

Hi @VincentFTS,

If you move an @id annotation, ProviGen will continue to work and you don't necessarily need to re-create the table. Basically the last segment of the uri would be used to match the newly annotated column.

If you really need to create the initial table differently, you can just use your own SQLiteOpenHelper.

public class MyContentProvider extends ProviGenProvider {

    @Override
    public Class[] contractClasses() {
        return new Class[]{MyContract.class};
    }

    @Override
    public SQLiteOpenHelper openHelper(Context context) {

        return new SQLiteOpenHelper(getContext(), "databaseName", null, 1) {

            @Override
            public void onCreate(SQLiteDatabase database) {
                // Create the table without the AUTOINCREMENT here.
            }
        };
    }
}

Another option would be to fork ProviGen and modify the TableBuilder so that the AUTOINCREMENT is optional.

@VincentFTS Did you find a solution to your problem ?