String profile = Data.profile.get().getUuid();
try {
- SQLiteDatabase db = MLDB.getWritableDatabase();
+ SQLiteDatabase db = MLDB.getDatabase();
db.beginTransaction();
try {
for (LedgerAccount acc : params[0].accountList) {
Map<String, Boolean> unique = new HashMap<>();
Log.d("descriptions", "Starting refresh");
- SQLiteDatabase db = MLDB.getWritableDatabase();
+ SQLiteDatabase db = MLDB.getDatabase();
Data.backgroundTaskCount.incrementAndGet();
try {
default:
throw new HTTPException(http.getResponseCode(), http.getResponseMessage());
}
- try (SQLiteDatabase db = MLDB.getWritableDatabase()) {
+ try (SQLiteDatabase db = MLDB.getDatabase()) {
try (InputStream resp = http.getInputStream()) {
if (http.getResponseCode() != 200)
throw new IOException(String.format("HTTP error %d", http.getResponseCode()));
throw new HTTPException(http.getResponseCode(), http.getResponseMessage());
}
publishProgress(progress);
- try (SQLiteDatabase db = MLDB.getWritableDatabase()) {
- try (InputStream resp = http.getInputStream()) {
- if (http.getResponseCode() != 200)
- throw new IOException(String.format("HTTP error %d", http.getResponseCode()));
+ SQLiteDatabase db = MLDB.getDatabase();
+ try (InputStream resp = http.getInputStream()) {
+ if (http.getResponseCode() != 200)
+ throw new IOException(String.format("HTTP error %d", http.getResponseCode()));
- db.beginTransaction();
- try {
- profile.markAccountsAsNotPresent(db);
+ db.beginTransaction();
+ try {
+ profile.markAccountsAsNotPresent(db);
- AccountListParser parser = new AccountListParser(resp);
- ArrayList<LedgerAccount> accountList = new ArrayList<>();
+ AccountListParser parser = new AccountListParser(resp);
+ ArrayList<LedgerAccount> accountList = new ArrayList<>();
- LedgerAccount prevAccount = null;
+ LedgerAccount prevAccount = null;
- while (true) {
- throwIfCancelled();
- ParsedLedgerAccount parsedAccount = parser.nextAccount();
- if (parsedAccount == null) break;
+ while (true) {
+ throwIfCancelled();
+ ParsedLedgerAccount parsedAccount = parser.nextAccount();
+ if (parsedAccount == null) break;
- LedgerAccount acc = profile.tryLoadAccount(db, parsedAccount.getAname());
- if (acc == null) acc = new LedgerAccount(parsedAccount.getAname());
- else acc.removeAmounts();
+ LedgerAccount acc = profile.tryLoadAccount(db, parsedAccount.getAname());
+ if (acc == null) acc = new LedgerAccount(parsedAccount.getAname());
+ else acc.removeAmounts();
- profile.storeAccount(db, acc);
- for (ParsedBalance b : parsedAccount.getAebalance()) {
- profile.storeAccountValue(db, acc.getName(), b.getAcommodity(),
- b.getAquantity().asFloat());
- }
-
- if (acc.isVisible(accountList)) accountList.add(acc);
+ profile.storeAccount(db, acc);
+ for (ParsedBalance b : parsedAccount.getAebalance()) {
+ profile.storeAccountValue(db, acc.getName(), b.getAcommodity(),
+ b.getAquantity().asFloat());
+ }
- if (prevAccount != null) {
- prevAccount.setHasSubAccounts(
- acc.getName().startsWith(prevAccount.getName() + ":"));
- }
+ if (acc.isVisible(accountList)) accountList.add(acc);
- prevAccount = acc;
+ if (prevAccount != null) {
+ prevAccount.setHasSubAccounts(
+ acc.getName().startsWith(prevAccount.getName() + ":"));
}
- throwIfCancelled();
- profile.deleteNotPresentAccounts(db);
- throwIfCancelled();
- db.setTransactionSuccessful();
- Data.accounts.set(accountList);
- }
- finally {
- db.endTransaction();
+ prevAccount = acc;
}
+ throwIfCancelled();
+
+ profile.deleteNotPresentAccounts(db);
+ throwIfCancelled();
+ db.setTransactionSuccessful();
+ Data.accounts.set(accountList);
+ }
+ finally {
+ db.endTransaction();
}
}
default:
throw new HTTPException(http.getResponseCode(), http.getResponseMessage());
}
- try (SQLiteDatabase db = MLDB.getWritableDatabase()) {
+ try (SQLiteDatabase db = MLDB.getDatabase()) {
try (InputStream resp = http.getInputStream()) {
if (http.getResponseCode() != 200)
throw new IOException(String.format("HTTP error %d", http.getResponseCode()));
if (onlyStarred) sql += " AND a.hidden = 0";
sql += " ORDER BY a.name";
- SQLiteDatabase db = MLDB.getReadableDatabase();
+ SQLiteDatabase db = MLDB.getDatabase();
try (Cursor cursor = db.rawQuery(sql, new String[]{profileUUID})) {
while (cursor.moveToNext()) {
final String accName = cursor.getString(0);
}
Log.d("UTT", sql);
- SQLiteDatabase db = MLDB.getReadableDatabase();
+ SQLiteDatabase db = MLDB.getDatabase();
String lastDateString = Globals.formatLedgerDate(new Date());
Date lastDate = Globals.parseLedgerDate(lastDateString);
boolean odd = true;
public static MobileLedgerProfile loadAllFromDB(String currentProfileUUID) {
MobileLedgerProfile result = null;
List<MobileLedgerProfile> list = new ArrayList<>();
- SQLiteDatabase db = MLDB.getReadableDatabase();
+ SQLiteDatabase db = MLDB.getDatabase();
try (Cursor cursor = db.rawQuery("SELECT uuid, name, url, use_authentication, auth_user, " +
"auth_password, permit_posting, theme, order_no FROM " +
"profiles order by order_no", null))
return result;
}
public static void storeProfilesOrder() {
- SQLiteDatabase db = MLDB.getWritableDatabase();
+ SQLiteDatabase db = MLDB.getDatabase();
db.beginTransaction();
try {
int orderNo = 0;
setAuthPassword(String.valueOf(text));
}
public void storeInDB() {
- SQLiteDatabase db = MLDB.getWritableDatabase();
+ SQLiteDatabase db = MLDB.getDatabase();
db.beginTransaction();
try {
// Log.d("profiles", String.format("Storing profile in DB: uuid=%s, name=%s, " +
Log.d("profile", String.format("Transaction %d stored", tr.getId()));
}
public String getOption(String name, String default_value) {
- SQLiteDatabase db = MLDB.getReadableDatabase();
+ SQLiteDatabase db = MLDB.getDatabase();
try (Cursor cursor = db.rawQuery("select value from options where profile = ? and name=?",
new String[]{uuid, name}))
{
}
public void setOption(String name, String value) {
Log.d("profile", String.format("setting option %s=%s", name, value));
- SQLiteDatabase db = MLDB.getWritableDatabase();
+ SQLiteDatabase db = MLDB.getDatabase();
db.execSQL("insert or replace into options(profile, name, value) values(?, ?, ?);",
new String[]{uuid, name, value});
}
setOption(name, String.valueOf(value));
}
public void removeFromDB() {
- SQLiteDatabase db = MLDB.getWritableDatabase();
+ SQLiteDatabase db = MLDB.getDatabase();
Log.d("db", String.format("removing profile %s from DB", uuid));
try {
db.beginTransaction();
}
@NonNull
public LedgerAccount loadAccount(String name) {
- SQLiteDatabase db = MLDB.getReadableDatabase();
+ SQLiteDatabase db = MLDB.getDatabase();
return loadAccount(db, name);
}
@Nullable
public LedgerAccount tryLoadAccount(String acct_name) {
- SQLiteDatabase db = MLDB.getReadableDatabase();
+ SQLiteDatabase db = MLDB.getDatabase();
return tryLoadAccount(db, acct_name);
}
@NonNull
}
public LedgerTransaction loadTransaction(int transactionId) {
LedgerTransaction tr = new LedgerTransaction(transactionId, this.uuid);
- tr.loadData(MLDB.getReadableDatabase());
+ tr.loadData(MLDB.getDatabase());
return tr;
}
}
public List<LedgerAccount> loadChildAccountsOf(LedgerAccount acc) {
List<LedgerAccount> result = new ArrayList<>();
- SQLiteDatabase db = MLDB.getReadableDatabase();
+ SQLiteDatabase db = MLDB.getDatabase();
try (Cursor c = db.rawQuery(
"SELECT a.name FROM accounts a WHERE a.profile = ? and a.name like ?||':%'",
new String[]{uuid, acc.getName()}))
ArrayList<LedgerAccount> visibleList = new ArrayList<>();
visibleList.add(acc);
- SQLiteDatabase db = MLDB.getReadableDatabase();
+ SQLiteDatabase db = MLDB.getDatabase();
try (Cursor c = db.rawQuery(
"SELECT a.name FROM accounts a WHERE a.profile = ? and a.name like ?||':%'",
new String[]{uuid, acc.getName()}))
ViewPropertyAnimator animator = arrow.animate();
acc.toggleExpanded();
- Data.profile.get().storeAccount(MLDB.getWritableDatabase(), acc);
+ Data.profile.get().storeAccount(MLDB.getDatabase(), acc);
if (wasExpanded) {
Log.d("accounts", String.format("Collapsing account '%s'", acc.getName()));
Log.d("descr selected", description);
if (!inputStateIsInitial()) return;
- try (Cursor c = MLDB.getReadableDatabase().rawQuery(
+ try (Cursor c = MLDB.getDatabase().rawQuery(
"select profile, id from transactions where description=? order by date desc " +
"limit 1", new String[]{description}))
{
LedgerTransaction tr = p[0].transaction;
boolean odd = p[0].odd;
- SQLiteDatabase db = MLDB.getReadableDatabase();
+ SQLiteDatabase db = MLDB.getDatabase();
tr.loadData(db);
publishProgress(new TransactionLoaderStep(p[0].holder, p[0].position, tr, odd));
import java.io.InputStreamReader;
import java.util.Locale;
-import static net.ktnx.mobileledger.utils.MLDB.DatabaseMode.READ;
-import static net.ktnx.mobileledger.utils.MLDB.DatabaseMode.WRITE;
-
public final class MLDB {
public static final String ACCOUNTS_TABLE = "accounts";
public static final String DESCRIPTION_HISTORY_TABLE = "description_history";
@NonNls
public static final String OPT_PROFILE_UUID = "profile_uuid";
private static final String NO_PROFILE = "-";
- private static MobileLedgerDatabase helperForReading, helperForWriting;
+ private static MobileLedgerDatabase dbHelper;
private static Application context;
private static void checkState() {
if (context == null)
throw new IllegalStateException("First call init with a valid context");
}
- public static synchronized SQLiteDatabase getDatabase(DatabaseMode mode) {
+ public static synchronized SQLiteDatabase getDatabase() {
checkState();
SQLiteDatabase db;
- if (mode == READ) {
- if (helperForReading == null) helperForReading = new MobileLedgerDatabase(context);
- db = helperForReading.getReadableDatabase();
- }
- else {
- if (helperForWriting == null) helperForWriting = new MobileLedgerDatabase(context);
- db = helperForWriting.getWritableDatabase();
- }
+ if (dbHelper == null) dbHelper = new MobileLedgerDatabase(context);
+ db = dbHelper.getWritableDatabase();
db.execSQL("pragma case_sensitive_like=ON;");
return db;
}
- public static SQLiteDatabase getReadableDatabase() {
- return getDatabase(READ);
- }
- public static SQLiteDatabase getWritableDatabase() {
- return getDatabase(WRITE);
- }
static public int getIntOption(String name, int default_value) {
String s = getOption(name, String.valueOf(default_value));
try {
}
static public String getOption(String name, String default_value) {
Log.d("db", "about to fetch option " + name);
- SQLiteDatabase db = getReadableDatabase();
+ SQLiteDatabase db = getDatabase();
try (Cursor cursor = db.rawQuery("select value from options where profile = ? and name=?",
new String[]{NO_PROFILE, name}))
{
}
static public void setOption(String name, String value) {
Log.d("option", String.format("%s := %s", name, value));
- SQLiteDatabase db = MLDB.getWritableDatabase();
+ SQLiteDatabase db = MLDB.getDatabase();
db.execSQL("insert or replace into options(profile, name, value) values(?, ?, ?);",
new String[]{NO_PROFILE, name, value});
}
params = new String[]{str, str, str, str};
}
Log.d("autocompletion", sql);
- SQLiteDatabase db = MLDB.getReadableDatabase();
+ SQLiteDatabase db = MLDB.getDatabase();
try (Cursor matches = db.rawQuery(sql, params)) {
int i = 0;
MLDB.context = context;
}
public static void done() {
- if (helperForReading != null) helperForReading.close();
-
- if ((helperForWriting != helperForReading) && (helperForWriting != null))
- helperForWriting.close();
+ if (dbHelper != null) {
+ Log.d("db", "Closing DB helper");
+ dbHelper.close();
+ }
}
-
- public enum DatabaseMode {READ, WRITE}
}
class MobileLedgerDatabase extends SQLiteOpenHelper implements AutoCloseable {