Note, this is a work in progress
This is a python module which generates Android classes. It is most useful for creating an initial database and contentprovider for a project.
I plan on adding all necessary classes for a database with a contentprovider to start with. More types of classes might follow as I see a need for them.
The database structure is basically the same as the one I present in my tutorial.
Please see sample.py for an example of how to generate all the database files and the sample project in /sample/ where they are used. The sample is a modified version of the tutorial project mentioned above so check that out for a preview of the app itself. sample_with_triggers.py shows a simple example where a database trigger is defined as well.
An example generation can be seen below where quite a simple table is created and then a java OrmClass is generated.
fromAndroidCodeGenerator.db_tableimportTable, Column, ForeignKey, UniquefromAndroidCodeGenerator.dbitemimportDBItemt=Table('Album').add_cols(Column('albumname').text.not_null.default("''"), \
Column('artistname').text.not_null)\
.add_constraints(ForeignKey('artistname').references('artist', 'name')\
.on_delete_cascade,\
Unique('albumname').on_conflict_replace)
print(DBItem(t, pkg="com.example.appname.database"))Result:
packagecom.example.appname.database;
importandroid.content.ContentValues;
importandroid.content.UriMatcher;
importandroid.database.Cursor;
importandroid.net.Uri;
/** * Represents Album in the database. * */publicclassAlbumItemextendsDBItem {
publicstaticfinalStringTABLE_NAME = "Album";
publicstaticUriURI() {
returnUri.withAppendedPath(
Uri.parse(ItemProvider.SCHEME
+ ItemProvider.AUTHORITY), TABLE_NAME);
}
// Column namespublicstaticfinalStringCOL_ID = "_id";
publicstaticfinalStringCOL_ALBUMNAME = "albumname";
publicstaticfinalStringCOL_ARTISTNAME = "artistname";
// For database projection so order is consistentpublicstaticfinalString[] FIELDS = { COL_ID, COL_ALBUMNAME, COL_ARTISTNAME };
publiclong_id = -1;
publicStringalbumname = "";
publicStringartistname;
publicstaticfinalintBASEURICODE = 3993119;
publicstaticfinalintBASEITEMCODE = 1102568;
publicstaticvoidaddMatcherUris(UriMatchersURIMatcher) {
sURIMatcher.addURI(ItemProvider.AUTHORITY, TABLE_NAME, BASEURICODE);
sURIMatcher.addURI(ItemProvider.AUTHORITY, TABLE_NAME + "/#", BASEITEMCODE);
}
publicstaticfinalStringTYPE_DIR = "vnd.android.cursor.dir/vnd.example." + TABLE_NAME;
publicstaticfinalStringTYPE_ITEM = "vnd.android.cursor.item/vnd.example." + TABLE_NAME;
publicAlbumItem() {
super();
}
publicAlbumItem(finalCursorcursor) {
super();
// Projection expected to match FIELDS arraythis._id = cursor.getLong(0);
this.albumname = cursor.getString(1);
this.artistname = cursor.getString(2);
}
publicContentValuesgetContent() {
ContentValuesvalues = newContentValues();
values.put(COL_ALBUMNAME, albumname);
values.put(COL_ARTISTNAME, artistname);
returnvalues;
}
publicStringgetTableName() {
returnTABLE_NAME;
}
publicString[] getFields() {
returnFIELDS;
}
publiclonggetId() {
return_id;
}
publicvoidsetId(finallongid) {
_id = id;
}
publicstaticfinalStringCREATE_TABLE =
"CREATE TABLE Album"
+" (_id INTEGER PRIMARY KEY,"
+" albumname TEXT NOT NULL DEFAULT '',"
+" artistname TEXT NOT NULL",
+""
+" FOREIGN KEY (artistname) REFERENCES artist(name) ON DELETE CASCADE,"
+" UNIQUE (albumname) ON CONFLICT REPLACE)";
}The program can generate the java files directly, as follows:
"""Generate a sample project"""fromAndroidCodeGenerator.generatorimportGeneratorfromAndroidCodeGenerator.db_tableimportTable, Column, ForeignKey, Uniquepersons=Table('Person').cols(Column('firstname').text.not_null.default("''"),\
Column('lastname').text.not_null.default("''"),\
Column('bio').text.not_null.default("''"))
g=Generator(path='./sample/src/com/example/appname/database/')
g.add_tables(persons)
g.write()