Skip to content

Instantly share code, notes, and snippets.

@toidiu
Created March 9, 2015 22:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toidiu/3264aabc0263aeb19a2e to your computer and use it in GitHub Desktop.
Save toidiu/3264aabc0263aeb19a2e to your computer and use it in GitHub Desktop.
ORMLite boilerplate
package co.touchlab.boomset.app.data;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
import org.jetbrains.annotations.NotNull;
import java.sql.SQLException;
/**
* Created by toidiu on 1/18/15.
*/
public class DatabaseHelper extends OrmLiteSqliteOpenHelper
{
//~=~=~=~=~=~=~=~=~=~=~=~=Constants
private static final String DATABASE_FILE_NAME = "TABLE_NAME";
private static final int VERSION = 1;
//~=~=~=~=~=~=~=~=~=~=~=~=Fields
private static DatabaseHelper instance;
// @reminder Ordering matters, create foreign key dependant classes later
private final Class[] tableClasses = new Class[] {ObjectModel1.class, ObjectModel2.class};
private DatabaseHelper(Context context)
{
super(context, DATABASE_FILE_NAME, null, VERSION);
}
@NotNull
public static synchronized DatabaseHelper getInstance(Context context)
{
if(instance == null)
{
instance = new DatabaseHelper(context);
}
return instance;
}
@Override
public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource)
{
try
{
for(Class mTableClass : tableClasses)
{
TableUtils.createTable(connectionSource, mTableClass);
}
}
catch(SQLException e)
{
throw new RuntimeException(e);
}
}
@Override
public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion)
{
for(int i = tableClasses.length - 1; i >= 0; i--)
{
Class tableClass = tableClasses[i];
try
{
TableUtils.dropTable(connectionSource, tableClass, true);
}
catch(SQLException e)
{
throw new RuntimeException(e);
}
}
onCreate(database, connectionSource);
}
public void runInTransaction(Runnable r) throws SQLException
{
SQLiteDatabase writableDatabase = getWritableDatabase();
writableDatabase.beginTransaction();
try
{
r.run();
writableDatabase.setTransactionSuccessful();
}
finally
{
writableDatabase.endTransaction();
}
}
public Dao<ObjectMode1, String> getObjectModel1Dao() throws SQLException
{
return getDao(ObjectModel1.class);
}
public Dao<ObjectMode2, Integer> getObjectModel2Dao() throws SQLException
{
return getDao(ObjectMode2.class);
}
}
//Be nice to your user and put this in a background task.
Dao<ObjectMode2, Integer> dao = DatabaseHelper.getInstance(context).getObjectModel2Dao();
ObjectMode2 obj = dao.queryForId(id);
public class ObjectMode1
{
@DatabaseField(id = true)//you have to give it an id
public String id;
@DatabaseField
public String title;
public ObjectMode1()
{
//ORM uses this for reflections
}
}
public class ObjectMode2
{
@DatabaseField(generatedId = true)//this will automatically generate an id.
public Integer id;
@DatabaseField
public String name;
public ObjectMode2()
{
//ORM uses this for reflections
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment