From 1dab5919a3ca68fa735a503faa24914fb9df05f4 Mon Sep 17 00:00:00 2001 From: Tony Green Date: Wed, 12 Sep 2018 11:06:06 +0800 Subject: [PATCH] Add registerDatabaseListener() api to listen Database create and upgrade events. --- litepal/src/main/java/org/litepal/LitePal.kt | 12 +++ .../tablemanager/LitePalOpenHelper.java | 92 ------------------- .../litepal/tablemanager/LitePalOpenHelper.kt | 73 +++++++++++++++ .../callback/DatabaseListener.java | 30 ++++++ 4 files changed, 115 insertions(+), 92 deletions(-) delete mode 100644 litepal/src/main/java/org/litepal/tablemanager/LitePalOpenHelper.java create mode 100644 litepal/src/main/java/org/litepal/tablemanager/LitePalOpenHelper.kt create mode 100644 litepal/src/main/java/org/litepal/tablemanager/callback/DatabaseListener.java diff --git a/litepal/src/main/java/org/litepal/LitePal.kt b/litepal/src/main/java/org/litepal/LitePal.kt index a270e47..fc02470 100644 --- a/litepal/src/main/java/org/litepal/LitePal.kt +++ b/litepal/src/main/java/org/litepal/LitePal.kt @@ -29,6 +29,7 @@ import org.litepal.exceptions.LitePalSupportException import org.litepal.parser.LitePalAttr import org.litepal.parser.LitePalParser import org.litepal.tablemanager.Connector +import org.litepal.tablemanager.callback.DatabaseListener import org.litepal.util.BaseUtility import org.litepal.util.Const import org.litepal.util.DBUtility @@ -49,6 +50,8 @@ object LitePal { private val handler = Handler(Looper.getMainLooper()) + private var dbListener: DatabaseListener? = null + /** * Initialize to make LitePal ready to work. If you didn't configure LitePalApplication * in the AndroidManifest.xml, make sure you call this method as soon as possible. In @@ -1451,4 +1454,13 @@ object LitePal { return conditions != null && where(*conditions).count(modelClass) > 0 } + /** + * Register a listener to listen database create and upgrade events. + */ + @JvmStatic fun registerDatabaseListener(listener: DatabaseListener) { + dbListener = listener + } + + @JvmStatic internal fun getDBListener() = dbListener + } \ No newline at end of file diff --git a/litepal/src/main/java/org/litepal/tablemanager/LitePalOpenHelper.java b/litepal/src/main/java/org/litepal/tablemanager/LitePalOpenHelper.java deleted file mode 100644 index af7abcb..0000000 --- a/litepal/src/main/java/org/litepal/tablemanager/LitePalOpenHelper.java +++ /dev/null @@ -1,92 +0,0 @@ -/* - * Copyright (C) Tony Green, LitePal Framework Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.litepal.tablemanager; - -import android.content.Context; -import android.database.sqlite.SQLiteDatabase; -import android.database.sqlite.SQLiteDatabase.CursorFactory; -import android.database.sqlite.SQLiteOpenHelper; - -import org.litepal.LitePalApplication; -import org.litepal.parser.LitePalAttr; -import org.litepal.util.SharedUtil; - -/** - * The database helper to generate and manage the tables. It will automate - * create or upgrade the database file depends on the parameters passed in. - * - * LitePal makes it easy for managing tables. It used the dynamic features of - * Java with reflection API to achieve that. Developers won't need to write - * their own SQL for managing tables, LitePal will do that for them. Developers - * just need to write their model classes and add right associations. LitePal - * will take all the rest job to manager tables in database. - * - * @author Tony Green - * @since 1.0 - */ -class LitePalOpenHelper extends SQLiteOpenHelper { - public static final String TAG = "LitePalHelper"; - - /** - * The standard constructor for SQLiteOpenHelper. - * - * @param context - * To use to open or create the database. - * @param name - * The database file. - * @param factory - * To use for creating cursor objects, or null for the default - * version number of the database (starting at 1); if the - * database is older, onUpgrade. - * @param version - * (SQLiteDatabase, int, int) will be used to upgrade the - * database; if the database is newer, - * onDowngrade(SQLiteDatabase, int, int) will be used to - * downgrade the database - */ - LitePalOpenHelper(Context context, String name, CursorFactory factory, int version) { - super(context, name, factory, version); - } - - /** - * A simple constructor for SQLiteOpenHelper with null for CursorFactory as - * default. - * - * @param dbName - * The database file. - * @param version - * (SQLiteDatabase, int, int) will be used to upgrade the - * database; if the database is newer, - * onDowngrade(SQLiteDatabase, int, int) will be used to - * downgrade the database - */ - LitePalOpenHelper(String dbName, int version) { - this(LitePalApplication.getContext(), dbName, null, version); - } - - @Override - public void onCreate(SQLiteDatabase db) { - Generator.create(db); - } - - @Override - public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { - Generator.upgrade(db); - SharedUtil.updateVersion(LitePalAttr.getInstance().getExtraKeyName(), newVersion); - } - -} diff --git a/litepal/src/main/java/org/litepal/tablemanager/LitePalOpenHelper.kt b/litepal/src/main/java/org/litepal/tablemanager/LitePalOpenHelper.kt new file mode 100644 index 0000000..528688a --- /dev/null +++ b/litepal/src/main/java/org/litepal/tablemanager/LitePalOpenHelper.kt @@ -0,0 +1,73 @@ +package org.litepal.tablemanager + +import android.content.Context +import android.database.sqlite.SQLiteDatabase +import android.database.sqlite.SQLiteOpenHelper +import org.litepal.LitePal +import org.litepal.LitePalApplication +import org.litepal.parser.LitePalAttr +import org.litepal.util.SharedUtil + +/** + * The database helper to generate and manage the tables. It will automate + * create or upgrade the database file depends on the parameters passed in. + * + * LitePal makes it easy for managing tables. It used the dynamic features of + * Java with reflection API to achieve that. Developers won't need to write + * their own SQL for managing tables, LitePal will do that for them. Developers + * just need to write their model classes and add right associations. LitePal + * will take all the rest job to manager tables in database. + * + * @author Tony Green + * @since 1.0 + */ +internal class LitePalOpenHelper +/** + * The standard constructor for SQLiteOpenHelper. + * + * @param context + * To use to open or create the database. + * @param name + * The database file. + * @param factory + * To use for creating cursor objects, or null for the default + * version number of the database (starting at 1); if the + * database is older, onUpgrade. + * @param version + * (SQLiteDatabase, int, int) will be used to upgrade the + * database; if the database is newer, + * onDowngrade(SQLiteDatabase, int, int) will be used to + * downgrade the database + */ +(context: Context, name: String, factory: SQLiteDatabase.CursorFactory?, version: Int) : SQLiteOpenHelper(context, name, factory, version) { + + /** + * A simple constructor for SQLiteOpenHelper with null for CursorFactory as + * default. + * + * @param dbName + * The database file. + * @param version + * (SQLiteDatabase, int, int) will be used to upgrade the + * database; if the database is newer, + * onDowngrade(SQLiteDatabase, int, int) will be used to + * downgrade the database + */ + constructor(dbName: String, version: Int) : this(LitePalApplication.getContext(), dbName, null, version) + + override fun onCreate(db: SQLiteDatabase) { + Generator.create(db) + LitePal.getDBListener()?.onCreate() + } + + override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) { + Generator.upgrade(db) + SharedUtil.updateVersion(LitePalAttr.getInstance().extraKeyName, newVersion) + LitePal.getDBListener()?.onUpgrade(oldVersion, newVersion) + } + + companion object { + const val TAG = "LitePalHelper" + } + +} \ No newline at end of file diff --git a/litepal/src/main/java/org/litepal/tablemanager/callback/DatabaseListener.java b/litepal/src/main/java/org/litepal/tablemanager/callback/DatabaseListener.java new file mode 100644 index 0000000..8ab519d --- /dev/null +++ b/litepal/src/main/java/org/litepal/tablemanager/callback/DatabaseListener.java @@ -0,0 +1,30 @@ +/* + * Copyright (C) Tony Green, LitePal Framework Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.litepal.tablemanager.callback; + +/** + * Callback for listening database create and upgrade events. + * @author Tony Green + * @since 2.0 + */ +public interface DatabaseListener { + + void onCreate(); + + void onUpgrade(int oldVersion, int newVersion); + +} \ No newline at end of file -- GitLab