提交 e3c00364 编写于 作者: G guolindev

Add API support for transaction.

上级 44e25031
......@@ -54,6 +54,26 @@ object LitePal {
@JvmStatic
fun getDatabase(): SQLiteDatabase = Operator.getDatabase()
/**
* Begins a transaction in EXCLUSIVE mode.
*/
@JvmStatic
fun beginTransaction() = Operator.beginTransaction()
/**
* End a transaction.
*/
@JvmStatic
fun endTransaction() = Operator.endTransaction()
/**
* Marks the current transaction as successful. Do not do any more database work between calling this and calling endTransaction.
* Do as little non-database work as possible in that situation too.
* If any errors are encountered between this and endTransaction the transaction will still be committed.
*/
@JvmStatic
fun setTransactionSuccessful() = Operator.setTransactionSuccessful()
/**
* Switch the using database to the one specified by parameter.
* @param litePalDB
......
......@@ -95,6 +95,29 @@ public class Operator {
return Connector.getDatabase();
}
/**
* Begins a transaction in EXCLUSIVE mode.
*/
public static void beginTransaction() {
getDatabase().beginTransaction();
}
/**
* End a transaction.
*/
public static void endTransaction() {
getDatabase().endTransaction();
}
/**
* Marks the current transaction as successful. Do not do any more database work between calling this and calling endTransaction.
* Do as little non-database work as possible in that situation too.
* If any errors are encountered between this and endTransaction the transaction will still be committed.
*/
public static void setTransactionSuccessful() {
getDatabase().setTransactionSuccessful();
}
/**
* Switch the using database to the one specified by parameter.
* @param litePalDB
......@@ -1191,8 +1214,17 @@ public class Operator {
*/
public static int deleteAll(Class<?> modelClass, String... conditions) {
synchronized (LitePalSupport.class) {
DeleteHandler deleteHandler = new DeleteHandler(Connector.getDatabase());
return deleteHandler.onDeleteAll(modelClass, conditions);
int rowsAffected;
SQLiteDatabase db = Connector.getDatabase();
db.beginTransaction();
try {
DeleteHandler deleteHandler = new DeleteHandler(db);
rowsAffected = deleteHandler.onDeleteAll(modelClass, conditions);
db.setTransactionSuccessful();
return rowsAffected;
} finally {
db.endTransaction();
}
}
}
......
......@@ -203,13 +203,18 @@ public class LitePalSupport {
*/
public int update(long id) {
synchronized (LitePalSupport.class) {
SQLiteDatabase db = Connector.getDatabase();
db.beginTransaction();
try {
UpdateHandler updateHandler = new UpdateHandler(Connector.getDatabase());
int rowsAffected = updateHandler.onUpdate(this, id);
getFieldsToSetToDefault().clear();
db.setTransactionSuccessful();
return rowsAffected;
} catch (Exception e) {
throw new LitePalSupportException(e.getMessage(), e);
} finally {
db.endTransaction();
}
}
}
......@@ -272,13 +277,18 @@ public class LitePalSupport {
*/
public int updateAll(String... conditions) {
synchronized (LitePalSupport.class) {
SQLiteDatabase db = Connector.getDatabase();
db.beginTransaction();
try {
UpdateHandler updateHandler = new UpdateHandler(Connector.getDatabase());
int rowsAffected = updateHandler.onUpdateAll(this, conditions);
getFieldsToSetToDefault().clear();
db.setTransactionSuccessful();
return rowsAffected;
} catch (Exception e) {
throw new LitePalSupportException(e.getMessage(), e);
} finally {
db.endTransaction();
}
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册