Green Sandbox

ITにまつわる話を書いていきます

greenDAOでテーブル名とカラム名を指定する方法

greenDAOでは指定のプロパティ名のTableNameやColumnNameをデフォルトでuppercaseに変換してテーブルDDLを作成してくれる。

http://greendao-orm.com/documentation/modelling-entities/

greenDAO tries to work with reasonable defaults, so developers don’t have to configure each and every bit. For example the table and column name on the database side are derived from the entity and property names. Instead of the camel case style used in Java, the default database names are in uppercase using an underscore to separate word. For example, a property called “creationDate” will become a database column “CREATION_DATE”.

あらかじめテーブル定義が決められている場合など、これでは困るという場合も当然ある。そういう場合は、スクリプト側のテーブル定義にテーブル名やカラム名を書き込めば、指定の内容で作成を行うことができる。

指定しない場合のジェネレート結果

Entity tableEntity = schema.addEntity("tableName");
tableEntity.addIntProperty("propertyId").primaryKey().notNull();
db.execSQL("CREATE TABLE " + constraint + "'TABLE_NAME' (" + //
"'PROPERTY_ID' INTEGER PRIMARY KEY NOT NULL );"); // 0: propertyId

指定した場合のジェネレート結果

Entity tableEntity = schema.addEntity("tableName");
tableEntity.setTableName('tableName');
tableEntity.addIntProperty("propertyId").primaryKey().notNull().columnName('propertyId');
db.execSQL("CREATE TABLE " + constraint + "'tableName' (" + //
"'propertyId' INTEGER PRIMARY KEY NOT NULL );"); // 0: propertyId