Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Manually init classes list #692

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions library/src/main/java/com/orm/SchemaGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ public static SchemaGenerator getInstance() {
return new SchemaGenerator();
}

public void createDatabase(SQLiteDatabase sqLiteDatabase) {
List<Class> domainClasses = getDomainClasses();
public void createDatabase(SQLiteDatabase sqLiteDatabase, List<Class> modelsClasses) {
List<Class> domainClasses = modelsClasses == null ? getDomainClasses() : modelsClasses;
for (Class domain : domainClasses) {
createTable(domain, sqLiteDatabase);
afterTableCreated(domain,sqLiteDatabase);
Expand All @@ -59,8 +59,8 @@ public void afterTableCreated(Class<?> table, SQLiteDatabase sqLiteDatabase) {

}

public void doUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
List<Class> domainClasses = getDomainClasses();
public void doUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion, List<Class> modelsClasses) {
List<Class> domainClasses = modelsClasses == null ? getDomainClasses() : modelsClasses;
String sql = "select count(*) from sqlite_master where type='table' and name='%s';";

for (Class domain : domainClasses) {
Expand Down
6 changes: 6 additions & 0 deletions library/src/main/java/com/orm/SugarApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import android.app.Application;

import java.util.List;

public class SugarApp extends Application {

@Override
Expand All @@ -16,4 +18,8 @@ public void onTerminate() {
SugarContext.terminate();
}

public List<Class> getModelsClassesList() {
return null;
}

}
13 changes: 10 additions & 3 deletions library/src/main/java/com/orm/SugarContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.orm.util.ContextUtil;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;

Expand All @@ -15,8 +16,8 @@ public class SugarContext {
private SugarDb sugarDb;
private Map<Object, Long> entitiesMap;

private SugarContext() {
this.sugarDb = SugarDb.getInstance();
private SugarContext(List<Class> modelsClasses) {
this.sugarDb = SugarDb.getInstance(modelsClasses);
this.entitiesMap = Collections.synchronizedMap(new WeakHashMap<Object, Long>());
}

Expand All @@ -29,7 +30,13 @@ public static SugarContext getSugarContext() {

public static void init(Context context) {
ContextUtil.init(context);
instance = new SugarContext();
instance = new SugarContext(null);
dbConfiguration = null;
}

public static void init(Context context, List<Class> modelsClasses) {
ContextUtil.init(context);
instance = new SugarContext(modelsClasses);
dbConfiguration = null;
}

Expand Down
16 changes: 12 additions & 4 deletions library/src/main/java/com/orm/SugarDb.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import com.orm.helper.ManifestHelper;
import com.orm.util.SugarCursorFactory;

import java.util.List;

import static com.orm.util.ContextUtil.getContext;
import static com.orm.helper.ManifestHelper.getDatabaseVersion;
import static com.orm.helper.ManifestHelper.getDbName;
Expand All @@ -19,20 +21,26 @@ public class SugarDb extends SQLiteOpenHelper {
private final SchemaGenerator schemaGenerator;
private SQLiteDatabase sqLiteDatabase;
private int openedConnections = 0;
private List<Class> classes;

//Prevent instantiation
private SugarDb() {
private SugarDb(List<Class> modelsClasses) {
super(getContext(), getDbName(), new SugarCursorFactory(ManifestHelper.isDebugEnabled()), getDatabaseVersion());
this.classes = modelsClasses;
schemaGenerator = SchemaGenerator.getInstance();
}

public static SugarDb getInstance() {
return new SugarDb();
return new SugarDb(null);
}

public static SugarDb getInstance(List<Class> modelsClasses) {
return new SugarDb(modelsClasses);
}

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
schemaGenerator.createDatabase(sqLiteDatabase);
schemaGenerator.createDatabase(sqLiteDatabase, classes);
}

@Override
Expand All @@ -50,7 +58,7 @@ public void onConfigure(SQLiteDatabase db) {

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
schemaGenerator.doUpgrade(sqLiteDatabase, oldVersion, newVersion);
schemaGenerator.doUpgrade(sqLiteDatabase, oldVersion, newVersion, classes);
}

public synchronized SQLiteDatabase getDB() {
Expand Down
4 changes: 2 additions & 2 deletions library/src/test/java/com/orm/SchemaGeneratorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public void testAllTableCreation() {
SQLiteDatabase sqLiteDatabase = SugarContext.getSugarContext().getSugarDb().getDB();
SchemaGenerator schemaGenerator = SchemaGenerator.getInstance();

schemaGenerator.createDatabase(sqLiteDatabase);
schemaGenerator.createDatabase(sqLiteDatabase, null);
String sql = "select count(*) from sqlite_master where type='table';";

Cursor c = sqLiteDatabase.rawQuery(sql, null);
Expand All @@ -150,7 +150,7 @@ public void testDeleteAllTables() {
SQLiteDatabase sqLiteDatabase = SugarContext.getSugarContext().getSugarDb().getDB();
SchemaGenerator schemaGenerator = SchemaGenerator.getInstance();

schemaGenerator.createDatabase(sqLiteDatabase);
schemaGenerator.createDatabase(sqLiteDatabase, null);
schemaGenerator.deleteTables(sqLiteDatabase);

String sql = "select count(*) from sqlite_master where type='table';";
Expand Down