From: Damyan Ivanov Date: Sat, 8 Dec 2018 12:12:24 +0000 (+0000) Subject: move DB structure initialization in a sql_0.sql script X-Git-Tag: v0.3~288 X-Git-Url: https://git.ktnx.net/?a=commitdiff_plain;h=d6418fb0d2e9e8abd843997a7aa2257f1a5d6cae;p=mobile-ledger.git move DB structure initialization in a sql_0.sql script this avoids several "create foo if not exists" statements executed on each start --- diff --git a/app/src/main/java/net/ktnx/mobileledger/AccountSummary.java b/app/src/main/java/net/ktnx/mobileledger/AccountSummary.java index 7b4d2579..82104b5e 100644 --- a/app/src/main/java/net/ktnx/mobileledger/AccountSummary.java +++ b/app/src/main/java/net/ktnx/mobileledger/AccountSummary.java @@ -145,9 +145,7 @@ public class AccountSummary extends AppCompatActivity { else { MobileLedgerDB.setDb_filename(MobileLedgerDB.DATABASE_NAME); } - MobileLedgerDB.initDB(); - - MobileLedgerDB.applyRevisions(getResources(), getPackageName()); + MobileLedgerDB.initDB(getResources(), getPackageName()); account_list_last_updated = MobileLedgerDB.get_option_value("last_refresh", (long) 0); diff --git a/app/src/main/java/net/ktnx/mobileledger/MobileLedgerDB.java b/app/src/main/java/net/ktnx/mobileledger/MobileLedgerDB.java index 141b4a32..a4a078e7 100644 --- a/app/src/main/java/net/ktnx/mobileledger/MobileLedgerDB.java +++ b/app/src/main/java/net/ktnx/mobileledger/MobileLedgerDB.java @@ -25,20 +25,9 @@ class MobileLedgerDB { MobileLedgerDB.db_filename = db_filename; } - static void initDB() { + static void initDB(Resources rm, String pkg_name) { db = SQLiteDatabase.openOrCreateDatabase(db_filename, null); - - db.execSQL("create table if not exists accounts(name varchar);"); - db.execSQL("create index if not exists idx_accounts_name on accounts(name);"); - db.execSQL("create table if not exists options(name varchar, value varchar);"); - db.execSQL("create unique index if not exists idx_options_name on options(name);"); - db.execSQL("create table if not exists account_values(account varchar not null, currency varchar not null, value decimal(18,2) not null);"); - db.execSQL("create index if not exists idx_account_values_account on account_values(account);"); - db.execSQL("create unique index if not exists un_account_values on account_values(account,currency);"); - } - - static void applyRevisions(Resources rm, String pkg_name) { - int cur_ver = Integer.parseInt(get_option_value(OPT_DB_REVISION, "0")); + int cur_ver = Integer.parseInt(get_option_value(OPT_DB_REVISION, "-1")); Log.d("db", "Current DB revision is "+String.valueOf(cur_ver)); diff --git a/app/src/main/res/raw/sql_0.sql b/app/src/main/res/raw/sql_0.sql new file mode 100644 index 00000000..3d05c510 --- /dev/null +++ b/app/src/main/res/raw/sql_0.sql @@ -0,0 +1,7 @@ +create table if not exists accounts(name varchar); +create index if not exists idx_accounts_name on accounts(name); +create table if not exists options(name varchar, value varchar); +create unique index if not exists idx_options_name on options(name); +create table if not exists account_values(account varchar not null, currency varchar not null, value decimal(18,2) not null); +create index if not exists idx_account_values_account on account_values(account); +create unique index if not exists un_account_values on account_values(account,currency);