From a686c3293dd0dbbe49db7fc0f287c451d6b2ecd8 Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Wed, 19 Dec 2018 20:48:48 +0000 Subject: [PATCH] transactions.id -> integer --- .../mobileledger/model/LedgerTransaction.java | 23 ++++++++----------- .../net/ktnx/mobileledger/utils/MLDB.java | 2 +- app/src/main/res/raw/sql_9.sql | 9 ++++++++ 3 files changed, 20 insertions(+), 14 deletions(-) create mode 100644 app/src/main/res/raw/sql_9.sql diff --git a/app/src/main/java/net/ktnx/mobileledger/model/LedgerTransaction.java b/app/src/main/java/net/ktnx/mobileledger/model/LedgerTransaction.java index fab4ff3e..c4127ab8 100644 --- a/app/src/main/java/net/ktnx/mobileledger/model/LedgerTransaction.java +++ b/app/src/main/java/net/ktnx/mobileledger/model/LedgerTransaction.java @@ -41,13 +41,13 @@ public class LedgerTransaction { return Float.compare(o1.getAmount(), o2.getAmount()); } }; - private String id; + private Integer id; private String date; private String description; private ArrayList items; private String dataHash; private boolean dataLoaded; - public LedgerTransaction(String id, String date, String description) { + public LedgerTransaction(Integer id, String date, String description) { this.id = id; this.date = date; this.description = description; @@ -55,9 +55,6 @@ public class LedgerTransaction { this.dataHash = null; dataLoaded = false; } - public LedgerTransaction(int id, String date, String description) { - this(String.valueOf(id), date, description); - } public LedgerTransaction(String date, String description) { this(null, date, description); } @@ -96,13 +93,13 @@ public class LedgerTransaction { } }; } - public String getId() { + public int getId() { return id; } public void insertInto(SQLiteDatabase db) { fillDataHash(); db.execSQL("INSERT INTO transactions(id, date, description, data_hash) values(?,?,?,?)", - new String[]{id, date, description}); + new Object[]{id, date, description, dataHash}); for (LedgerTransactionItem item : items) { db.execSQL("INSERT INTO transaction_accounts(transaction_id, account_name, amount, " + @@ -137,20 +134,20 @@ public class LedgerTransaction { public void loadData(SQLiteDatabase db) { if (dataLoaded) return; - try (Cursor cTr = db.rawQuery("SELECT date, description from transactions WHERE " + - "id=?",new String[]{id})) { + try (Cursor cTr = db.rawQuery("SELECT date, description from transactions WHERE id=?", + new String[]{String.valueOf(id)})) + { if (cTr.moveToFirst()) { date = cTr.getString(0); description = cTr.getString(1); try (Cursor cAcc = db.rawQuery("SELECT account_name, amount, currency FROM " + "transaction_accounts WHERE transaction_id = ?", - new String[]{id})) + new String[]{String.valueOf(id)})) { while (cAcc.moveToNext()) { - add_item( - new LedgerTransactionItem(cAcc.getString(0), cAcc.getFloat(1), - cAcc.getString(2))); + add_item(new LedgerTransactionItem(cAcc.getString(0), cAcc.getFloat(1), + cAcc.getString(2))); } dataLoaded = true; diff --git a/app/src/main/java/net/ktnx/mobileledger/utils/MLDB.java b/app/src/main/java/net/ktnx/mobileledger/utils/MLDB.java index 9d8950a6..2773dc9f 100644 --- a/app/src/main/java/net/ktnx/mobileledger/utils/MLDB.java +++ b/app/src/main/java/net/ktnx/mobileledger/utils/MLDB.java @@ -117,7 +117,7 @@ public final class MLDB { class MobileLedgerDatabase extends SQLiteOpenHelper implements AutoCloseable { public static final String DB_NAME = "mobile-ledger.db"; - public static final int LATEST_REVISION = 8; + public static final int LATEST_REVISION = 9; private final Context mContext; diff --git a/app/src/main/res/raw/sql_9.sql b/app/src/main/res/raw/sql_9.sql new file mode 100644 index 00000000..936cec67 --- /dev/null +++ b/app/src/main/res/raw/sql_9.sql @@ -0,0 +1,9 @@ +alter table transactions add keep boolean default 1 not null; +update transactions set keep = 1; +create table transactions_new(id integer, date varchar, description varchar, data_hash varchar, keep boolean); +insert into transactions_new(id, date, description, data_hash, keep) select cast(id as integer), date, description, data_hash, keep from transactions; +drop table transactions; +create table transactions(id integer primary key, date varchar, description varchar, data_hash varchar, keep boolean); +create unique index un_transactions_data_hash on transactions(data_hash); +insert into transactions(id, date, description, data_hash, keep) select id, date, description, data_hash, keep from transactions_new; +drop table transactions_new; \ No newline at end of file -- 2.39.5