提交 1dab5919 编写于 作者: guolin's avatar guolin

Add registerDatabaseListener() api to listen Database create and upgrade events.

上级 145a6a73
......@@ -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
/*
* 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);
}
}
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
/*
* 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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册