+ 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"));
+
+ Log.d("db", "Current DB revision is "+String.valueOf(cur_ver));
+
+ while (applyRevision(rm, pkg_name, cur_ver+1)) {
+ cur_ver++;
+ }
+
+ Log.d("db", "Database revision is "+String.valueOf(cur_ver)+" now");
+ }
+ private static boolean applyRevision(Resources rm, String pkg_name, int rev_no) {
+ String rev_file = String.format(Locale.US, "sql_%d", rev_no);
+
+ int res_id = rm.getIdentifier(rev_file, "raw", pkg_name);
+ if (res_id == 0) {
+ Log.d("db", String.format(Locale.US, "No resource for revision %d", rev_no));
+ return false;
+ }
+ db.beginTransaction();
+ try (InputStream res = rm.openRawResource(res_id)) {
+ Log.d("db", "Applying revision " + String.valueOf(rev_no));
+ InputStreamReader isr = new InputStreamReader(res);
+ BufferedReader reader = new BufferedReader(isr);
+
+ String line;
+ while ((line = reader.readLine()) != null) {
+ db.execSQL(line);
+ }
+
+ set_option_value(OPT_DB_REVISION, rev_no);
+ db.setTransactionSuccessful();
+ } catch (Resources.NotFoundException e) {
+ Log.d("db", "SQL revision "+String.valueOf(rev_no)+" not found");
+ return false;
+ }
+ catch (SQLException e) {
+ Log.e("db", String.format(Locale.US, "Error applying revision %d: %s", rev_no, e.getMessage()));
+ return false;
+ }
+ catch (Exception e) {
+ Log.w("db", "Error reading revision" + String.valueOf(rev_no)+": "+e.getMessage());
+ return false;
+ }
+ finally {
+ db.endTransaction();
+ }
+
+ return true;