<commit_before>package nl.svia.pilsremote.data;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import java.util.ArrayList;

import nl.svia.pilsremote.adapters.PurchaseAdapter;
import nl.svia.pilsremote.misc.PurchaseModel;

public class DatabaseHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "pilsremote.db";
    private static final int DATABASE_VERSION = 2;

    private static final String TAG = "DatabaseHelper";

    private static DatabaseHelper helperInstance;

    public static synchronized DatabaseHelper getInstance(Context context) {
        // Use the application context, which will ensure that you
        // don't accidentally leak an Activity's context.
        // See this article for more information: http://bit.ly/6LRzfx
        if (helperInstance == null) {
            helperInstance = new DatabaseHelper(context.getApplicationContext());
        }

        return helperInstance;
    }

    private DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        final String SQL_CREATE_PURCHASE_TABLE = "CREATE TABLE " + PurchaseEntry.TABLE_NAME + " (" +
                PurchaseEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
                PurchaseEntry.COLUMN_USER + " INTEGER NOT NULL, " +
                PurchaseEntry.COLUMN_PRODUCT + " INTEGER NOT NULL, " +
                PurchaseEntry.COLUMN_AMOUNT + " INTEGER NOT NULL, " +
                PurchaseEntry.COLUMN_PRICE + " INTEGER NOT NULL, " +
                PurchaseEntry.COLUMN_OLD_BALANCE + " INTEGER NOT NULL, " +
                PurchaseEntry.COLUMN_TIMESTAMP + " INTEGER NOT NULL " +
                " );";

        db.execSQL(SQL_CREATE_PURCHASE_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
        // TODO implement this when actually changing the database setup
        sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + PurchaseEntry.TABLE_NAME);
        onCreate(sqLiteDatabase);
    }

    // Insert a post into the database
    public void addPurchase(PurchaseDbModel purchase) {
        // Create and/or open the database for writing
        SQLiteDatabase db = getWritableDatabase();

        // It's a good idea to wrap our insert in a transaction. This helps with performance and ensures
        // consistency of the database.
        db.beginTransaction();
        try {
            ContentValues values = new ContentValues();
            values.put(PurchaseEntry.COLUMN_USER, purchase.userId);
            values.put(PurchaseEntry.COLUMN_PRODUCT, purchase.productId);
            values.put(PurchaseEntry.COLUMN_AMOUNT, purchase.amount);
            values.put(PurchaseEntry.COLUMN_PRICE, purchase.price);
            values.put(PurchaseEntry.COLUMN_OLD_BALANCE, purchase.balance);
            values.put(PurchaseEntry.COLUMN_TIMESTAMP, System.currentTimeMillis());

            db.insertOrThrow(PurchaseEntry.TABLE_NAME, null, values);
            db.setTransactionSuccessful();
        } catch (Exception e) {
            Log.d(TAG, "Error while trying to add post to database");
        } finally {
            db.endTransaction();
        }
    }

    public int getLastBalance(int userId) {
        SQLiteDatabase db = getReadableDatabase();

        String[] selectionArgs = {Integer.toString(userId)};

        Cursor c = db.rawQuery("SELECT balance_before - (price * amount) AS last_balance, user_id " +
                "FROM Purchase " +
                "WHERE user_id= ?  " +
                "ORDER BY _id " +
                "DESC LIMIT 1", selectionArgs);

        int balance = -1;

        if (c.moveToFirst()) {
            balance = c.getInt(0);<commit_msg>Small fix for when there is no history<commit_after>package nl.svia.pilsremote.data;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import java.util.ArrayList;

import nl.svia.pilsremote.adapters.PurchaseAdapter;
import nl.svia.pilsremote.misc.PurchaseModel;

public class DatabaseHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "pilsremote.db";
    private static final int DATABASE_VERSION = 2;

    private static final String TAG = "DatabaseHelper";

    private static DatabaseHelper helperInstance;

    public static synchronized DatabaseHelper getInstance(Context context) {
        // Use the application context, which will ensure that you
        // don't accidentally leak an Activity's context.
        // See this article for more information: http://bit.ly/6LRzfx
        if (helperInstance == null) {
            helperInstance = new DatabaseHelper(context.getApplicationContext());
        }

        return helperInstance;
    }

    private DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        final String SQL_CREATE_PURCHASE_TABLE = "CREATE TABLE " + PurchaseEntry.TABLE_NAME + " (" +
                PurchaseEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
                PurchaseEntry.COLUMN_USER + " INTEGER NOT NULL, " +
                PurchaseEntry.COLUMN_PRODUCT + " INTEGER NOT NULL, " +
                PurchaseEntry.COLUMN_AMOUNT + " INTEGER NOT NULL, " +
                PurchaseEntry.COLUMN_PRICE + " INTEGER NOT NULL, " +
                PurchaseEntry.COLUMN_OLD_BALANCE + " INTEGER NOT NULL, " +
                PurchaseEntry.COLUMN_TIMESTAMP + " INTEGER NOT NULL " +
                " );";

        db.execSQL(SQL_CREATE_PURCHASE_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
        // TODO implement this when actually changing the database setup
        sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + PurchaseEntry.TABLE_NAME);
        onCreate(sqLiteDatabase);
    }

    // Insert a post into the database
    public void addPurchase(PurchaseDbModel purchase) {
        // Create and/or open the database for writing
        SQLiteDatabase db = getWritableDatabase();

        // It's a good idea to wrap our insert in a transaction. This helps with performance and ensures
        // consistency of the database.
        db.beginTransaction();
        try {
            ContentValues values = new ContentValues();
            values.put(PurchaseEntry.COLUMN_USER, purchase.userId);
            values.put(PurchaseEntry.COLUMN_PRODUCT, purchase.productId);
            values.put(PurchaseEntry.COLUMN_AMOUNT, purchase.amount);
            values.put(PurchaseEntry.COLUMN_PRICE, purchase.price);
            values.put(PurchaseEntry.COLUMN_OLD_BALANCE, purchase.balance);
            values.put(PurchaseEntry.COLUMN_TIMESTAMP, System.currentTimeMillis());

            db.insertOrThrow(PurchaseEntry.TABLE_NAME, null, values);
            db.setTransactionSuccessful();
        } catch (Exception e) {
            Log.d(TAG, "Error while trying to add post to database");
        } finally {
            db.endTransaction();
        }
    }

    public int getLastBalance(int userId) {
        SQLiteDatabase db = getReadableDatabase();

        String[] selectionArgs = {Integer.toString(userId)};

        Cursor c = db.rawQuery("SELECT balance_before - (price * amount) AS last_balance, user_id " +
                "FROM Purchase " +
                "WHERE user_id= ?  " +
                "ORDER BY _id " +
                "DESC LIMIT 1", selectionArgs);

        int balance = 0;

        if (c.moveToFirst()) {
            balance = c.getInt(0);
{'fraction_non_alphanumeric': 0.08535973928302833, 'fraction_numerical': 0.00125344697919278, 'mean_word_length': 2.6120416478044364, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4439910', 'n_tokens_mistral': 2218, 'n_tokens_neox': 2132, 'n_words': 544}