<commit_before>package com.jrvermeer.psalter.infrastructure

import android.content.Context
import android.database.Cursor
import android.database.DatabaseUtils
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteQueryBuilder
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent
import com.jrvermeer.psalter.R
import com.jrvermeer.psalter.helpers.DownloadHelper
import com.jrvermeer.psalter.models.Psalter
import com.jrvermeer.psalter.models.SqLiteQuery
import com.readystatesoftware.sqliteasset.SQLiteAssetHelper
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import java.util.*

/**
 * Created by Jonathan on 3/27/2017.
 */
// SQLiteAssetHelper: https://github.com/jgilfelt/android-sqlite-asset-helper
class PsalterDb(private val context: Context,
                private val scope: CoroutineScope,
                private val downloader: DownloadHelper) : SQLiteAssetHelper(context.applicationContext, DATABASE_NAME, null, DATABASE_VERSION), LifecycleObserver {

    private val db: SQLiteDatabase
    private var nextRandom : Psalter? = null

    init {
        setForcedUpgrade(DATABASE_VERSION)
        db = readableDatabase // must be AFTER setForcedUpgrade()
        scope.launch { loadNextRandom() }
    }

    companion object {
        private const val DATABASE_VERSION = 29
        private const val DATABASE_NAME = "psalter.sqlite"
        private const val TABLE_NAME = "psalter"
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    fun onDestroy() {
        Logger.d("OnDestroy - PsalterDb")
        db.close()
    }

    fun getCount(): Int {
        return try {
            DatabaseUtils.queryNumEntries(db, TABLE_NAME).toInt()
        } catch (ex: Exception) { 0 }
    }

    fun getIndex(index: Int): Psalter? {
        val results = queryPsalter("_id = $index", null, "1")<commit_msg>db version<commit_after>package com.jrvermeer.psalter.infrastructure

import android.content.Context
import android.database.Cursor
import android.database.DatabaseUtils
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteQueryBuilder
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent
import com.jrvermeer.psalter.R
import com.jrvermeer.psalter.helpers.DownloadHelper
import com.jrvermeer.psalter.models.Psalter
import com.jrvermeer.psalter.models.SqLiteQuery
import com.readystatesoftware.sqliteasset.SQLiteAssetHelper
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import java.util.*

/**
 * Created by Jonathan on 3/27/2017.
 */
// SQLiteAssetHelper: https://github.com/jgilfelt/android-sqlite-asset-helper
class PsalterDb(private val context: Context,
                private val scope: CoroutineScope,
                private val downloader: DownloadHelper) : SQLiteAssetHelper(context.applicationContext, DATABASE_NAME, null, DATABASE_VERSION), LifecycleObserver {

    private val db: SQLiteDatabase
    private var nextRandom : Psalter? = null

    init {
        setForcedUpgrade(DATABASE_VERSION)
        db = readableDatabase // must be AFTER setForcedUpgrade()
        scope.launch { loadNextRandom() }
    }

    companion object {
        private const val DATABASE_VERSION = 30
        private const val DATABASE_NAME = "psalter.sqlite"
        private const val TABLE_NAME = "psalter"
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    fun onDestroy() {
        Logger.d("OnDestroy - PsalterDb")
        db.close()
    }

    fun getCount(): Int {
        return try {
            DatabaseUtils.queryNumEntries(db, TABLE_NAME).toInt()
        } catch (ex: Exception) { 0 }
    }

    fun getIndex(index: Int): Psalter? {
        val results = queryPsalter("_id = $index", null, "1")
{'fraction_non_alphanumeric': 0.08657562483895903, 'fraction_numerical': 0.0056686421025508886, 'mean_word_length': 3.6771084337349396, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '375383', 'n_tokens_mistral': 1212, 'n_tokens_neox': 1151, 'n_words': 244}