Skip to content
This repository was archived by the owner on Jun 27, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
#7[30m]. Add created_at column to Accounts table. Add time column to …
…Transfers table.
  • Loading branch information
Evgenii Kanivets committed Feb 10, 2016
commit bea856c80d02d3c938f87ac74d6db50cb19f425a
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ private boolean prepareRecord() {
e.printStackTrace();
}

if (price >= 0 && price <= 1000000000) {
if (price >= 0 && price <= 1000000000 && spinnerAccount.getSelectedItemPosition() >= 0) {
Account account = accountController.getAccounts().get(spinnerAccount.getSelectedItemPosition());
return doRecord(title, category, price, account);
} else return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ public List<Account> getAccounts() {
cursor.getInt(curSumColIndex),
cursor.getString(currencyColIndex));

if (account.getTitle().equals(DbHelper.DEFAULT_ACCOUNT)) continue;

//Add account to list
accountList.add(account);
} while (cursor.moveToNext());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,15 @@ public class DbHelper extends SQLiteOpenHelper {

public static final String ACCOUNT_ID_COLUMN = "account_id";
public static final String CUR_SUM_COLUMN = "cur_sum";
public static final String DEFAULT_ACCOUNT = "default_account";
public static final String DEFAULT_ACCOUNT = "Default";
public static final String DEFAULT_ACCOUNT_CURRENCY = "NON";

public static final String TABLE_TRANSFERS = "transfers";
public static final String FROM_ACCOUNT_ID_COLUMN = "from_account_id";
public static final String TO_ACCOUND_ID_COLUMN = "to_account_id";
public static final String FROM_AMOUNT_COLUMN = "from_amount";
public static final String TO_AMOUNT_COLUMN = "to_amount";
public static final String CREATED_AT_COLUMN = "created_at";

public DbHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
Expand All @@ -50,23 +58,18 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion == 1 && newVersion == 2) {
db.beginTransaction();

/* Create accounts table */
db.execSQL("CREATE TABLE " + TABLE_ACCOUNTS + "("
+ ID_COLUMN + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ TITLE_COLUMN + " TEXT,"
+ CUR_SUM_COLUMN + " INTEGER);");
createAccountsTable(db);

/* Add account_id column into the records table */
db.execSQL("ALTER TABLE " + TABLE_RECORDS + " ADD COLUMN " + ACCOUNT_ID_COLUMN + " INTEGER;");

createTransfersTable(db);

/* Insert default account for all records from DB_VERSION = 1 */
ContentValues contentValues = new ContentValues();
contentValues.put(TITLE_COLUMN, DEFAULT_ACCOUNT);
contentValues.put(CUR_SUM_COLUMN, 0);
int id = (int) db.insert(TABLE_ACCOUNTS, null, contentValues);
long id = insertDefaultAccount(db);

/* Set the default account for all records from DB_VERSION = 1 */
contentValues = new ContentValues();
ContentValues contentValues = new ContentValues();
contentValues.put(ACCOUNT_ID_COLUMN, id);
db.update(DbHelper.TABLE_RECORDS, contentValues, null, null);

Expand Down Expand Up @@ -105,17 +108,40 @@ private void createDbVersion2(SQLiteDatabase db) {
+ ID_COLUMN + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ NAME_COLUMN + " TEXT" + ");");

createAccountsTable(db);

createTransfersTable(db);

insertDefaultAccount(db);
}

private void createAccountsTable(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_ACCOUNTS + "("
+ ID_COLUMN + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ CREATED_AT_COLUMN + " INTEGER,"
+ TITLE_COLUMN + " TEXT,"
+ CUR_SUM_COLUMN + " INTEGER,"
+ CURRENCY_COLUMN + " TEXT );");
}

private void createTransfersTable(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_TRANSFERS + "("
+ ID_COLUMN + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ TIME_COLUMN + " INTEGER,"
+ FROM_ACCOUNT_ID_COLUMN + " INTEGER,"
+ TO_ACCOUND_ID_COLUMN + " INTEGER,"
+ FROM_AMOUNT_COLUMN + " INTEGER,"
+ TO_AMOUNT_COLUMN + " INTEGER);");
}

private long insertDefaultAccount(SQLiteDatabase db) {
/* Insert default account for all records from DB_VERSION = 1 */
ContentValues contentValues = new ContentValues();
contentValues.put(TITLE_COLUMN, DEFAULT_ACCOUNT);
contentValues.put(CUR_SUM_COLUMN, 0);
contentValues.put(CURRENCY_COLUMN, DEFAULT_ACCOUNT_CURRENCY);
contentValues.put(CREATED_AT_COLUMN, System.currentTimeMillis());

db.insert(TABLE_ACCOUNTS, null, contentValues);
return db.insert(TABLE_ACCOUNTS, null, contentValues);
}
}
}