use of org.greenrobot.greendao.database.DatabaseStatement in project greenDAO by greenrobot.
the class AbstractDao method insertInTx.
/**
* Inserts the given entities in the database using a transaction. The given entities will become tracked if the PK
* is set.
*
* @param entities The entities to insert.
* @param setPrimaryKey if true, the PKs of the given will be set after the insert; pass false to improve
* performance.
*/
public void insertInTx(Iterable<T> entities, boolean setPrimaryKey) {
DatabaseStatement stmt = statements.getInsertStatement();
executeInsertInTx(stmt, entities, setPrimaryKey);
}
use of org.greenrobot.greendao.database.DatabaseStatement in project greenDAO by greenrobot.
the class AbstractDao method updateInTx.
/**
* Updates the given entities in the database using a transaction.
*
* @param entities The entities to insert.
*/
public void updateInTx(Iterable<T> entities) {
DatabaseStatement stmt = statements.getUpdateStatement();
db.beginTransaction();
// txEx: just to preserve original exception in case another exceptions is thrown in endTransaction()
RuntimeException txEx = null;
try {
synchronized (stmt) {
if (identityScope != null) {
identityScope.lock();
}
try {
if (isStandardSQLite) {
SQLiteStatement rawStmt = (SQLiteStatement) stmt.getRawStatement();
for (T entity : entities) {
updateInsideSynchronized(entity, rawStmt, false);
}
} else {
for (T entity : entities) {
updateInsideSynchronized(entity, stmt, false);
}
}
} finally {
if (identityScope != null) {
identityScope.unlock();
}
}
}
db.setTransactionSuccessful();
} catch (RuntimeException e) {
txEx = e;
} finally {
try {
db.endTransaction();
} catch (RuntimeException e) {
if (txEx != null) {
DaoLog.w("Could not end transaction (rethrowing initial exception)", e);
throw txEx;
} else {
throw e;
}
}
}
if (txEx != null) {
throw txEx;
}
}
use of org.greenrobot.greendao.database.DatabaseStatement in project greenDAO by greenrobot.
the class TableStatements method getInsertStatement.
public DatabaseStatement getInsertStatement() {
if (insertStatement == null) {
String sql = SqlUtils.createSqlInsert("INSERT INTO ", tablename, allColumns);
DatabaseStatement newInsertStatement = db.compileStatement(sql);
synchronized (this) {
if (insertStatement == null) {
insertStatement = newInsertStatement;
}
}
if (insertStatement != newInsertStatement) {
newInsertStatement.close();
}
}
return insertStatement;
}
use of org.greenrobot.greendao.database.DatabaseStatement in project greenDAO by greenrobot.
the class TableStatements method getInsertOrReplaceStatement.
public DatabaseStatement getInsertOrReplaceStatement() {
if (insertOrReplaceStatement == null) {
String sql = SqlUtils.createSqlInsert("INSERT OR REPLACE INTO ", tablename, allColumns);
DatabaseStatement newInsertOrReplaceStatement = db.compileStatement(sql);
synchronized (this) {
if (insertOrReplaceStatement == null) {
insertOrReplaceStatement = newInsertOrReplaceStatement;
}
}
if (insertOrReplaceStatement != newInsertOrReplaceStatement) {
newInsertOrReplaceStatement.close();
}
}
return insertOrReplaceStatement;
}
use of org.greenrobot.greendao.database.DatabaseStatement in project greenDAO by greenrobot.
the class TableStatements method getUpdateStatement.
public DatabaseStatement getUpdateStatement() {
if (updateStatement == null) {
String sql = SqlUtils.createSqlUpdate(tablename, allColumns, pkColumns);
DatabaseStatement newUpdateStatement = db.compileStatement(sql);
synchronized (this) {
if (updateStatement == null) {
updateStatement = newUpdateStatement;
}
}
if (updateStatement != newUpdateStatement) {
newUpdateStatement.close();
}
}
return updateStatement;
}
Aggregations