<commit_before>package kotlinx.android.koan

import android.content.ContentValues
import android.database.sqlite.SQLiteDatabase
import android.database.Cursor
import java.util.regex.Pattern

private fun Array<Pair<String, Any>>.toContentValues(): ContentValues {
    val values = ContentValues()
    for ((key, value) in this) {
        when(value) {
            is Boolean -> values.put(key, value)
            is Byte -> values.put(key, value)
            is ByteArray -> values.put(key, value)
            is Double -> values.put(key, value)
            is Float -> values.put(key, value)
            is Int -> values.put(key, value)
            is Long -> values.put(key, value)
            is Short -> values.put(key, value)
            is String -> values.put(key, value)
            else -> throw IllegalArgumentException("Non-supported value type: ${value.javaClass.getName()}")
        }
    }
    return values
}

public fun SQLiteDatabase.insert(tableName: String, vararg values: Pair<String, Any>): Long {
    return insert(tableName, null, values.toContentValues())
}

public fun SQLiteDatabase.insertOrThrow(tableName: String, vararg values: Pair<String, Any>): Long {
    return insertOrThrow(tableName, null, values.toContentValues())
}

public fun SQLiteDatabase.replace(tableName: String, vararg values: Pair<String, Any>): Long {
    return replace(tableName, null, values.toContentValues())
}

public fun SQLiteDatabase.replaceOrThrow(tableName: String, vararg values: Pair<String, Any>): Long {
    return replaceOrThrow(tableName, null, values.toContentValues())
}

public fun SQLiteDatabase.query(tableName: String, init: QueryBuilder.() -> Unit): Cursor {
    val builder = QueryBuilder(tableName)
    builder.init()
    return builder.exec(this)
}

public fun SQLiteDatabase.query(tableName: String, vararg columns: String, init: QueryBuilder.() -> Unit): Cursor {
    val builder = QueryBuilder(tableName)
    builder.columns(*columns)
    builder.init()
    return builder.exec(this)
}

public enum class SqlOrderDirection {
    ASC
    DESC
}

public class QueryBuilder(val tableName: String) {
    public var distinct: Boolean = false

    private val columns = arrayListOf<String>()
    private val groupBy = arrayListOf<String>()
    private val orderBy = arrayListOf<String>()

    private var havingApplied = false
    private var having: String? = null
    private var limit: String? = null

    private var selectionApplied = false
    private var useNativeSelection = false
    private var selection: String? = null
    private var nativeSelectionArgs: Array<String>? = null

    fun exec(db: SQLiteDatabase): Cursor {
        val finalSelection = if (selectionApplied) selection else null
        val finalSelectionArgs = if (selectionApplied && useNativeSelection) nativeSelectionArgs else null
        return db.query(distinct, tableName, columns.copyToArray(),
            finalSelection, finalSelectionArgs,
            groupBy.joinToString(", "), having, orderBy.joinToString(", "), limit)
    }

    public fun column(name: String) {
        columns.add(name)<commit_msg>Create and drop table helpers added.<commit_after>package kotlinx.android.koan

import android.content.ContentValues
import android.database.sqlite.SQLiteDatabase
import android.database.Cursor
import java.util.regex.Pattern

private fun Array<Pair<String, Any>>.toContentValues(): ContentValues {
    val values = ContentValues()
    for ((key, value) in this) {
        when(value) {
            is Boolean -> values.put(key, value)
            is Byte -> values.put(key, value)
            is ByteArray -> values.put(key, value)
            is Double -> values.put(key, value)
            is Float -> values.put(key, value)
            is Int -> values.put(key, value)
            is Long -> values.put(key, value)
            is Short -> values.put(key, value)
            is String -> values.put(key, value)
            else -> throw IllegalArgumentException("Non-supported value type: ${value.javaClass.getName()}")
        }
    }
    return values
}

public fun SQLiteDatabase.insert(tableName: String, vararg values: Pair<String, Any>): Long {
    return insert(tableName, null, values.toContentValues())
}

public fun SQLiteDatabase.insertOrThrow(tableName: String, vararg values: Pair<String, Any>): Long {
    return insertOrThrow(tableName, null, values.toContentValues())
}

public fun SQLiteDatabase.replace(tableName: String, vararg values: Pair<String, Any>): Long {
    return replace(tableName, null, values.toContentValues())
}

public fun SQLiteDatabase.replaceOrThrow(tableName: String, vararg values: Pair<String, Any>): Long {
    return replaceOrThrow(tableName, null, values.toContentValues())
}

public fun SQLiteDatabase.query(tableName: String, init: QueryBuilder.() -> Unit): Cursor {
    val builder = QueryBuilder(tableName)
    builder.init()
    return builder.exec(this)
}

public fun SQLiteDatabase.query(tableName: String, vararg columns: String, init: QueryBuilder.() -> Unit): Cursor {
    val builder = QueryBuilder(tableName)
    builder.columns(*columns)
    builder.init()
    return builder.exec(this)
}

public fun SQLiteDatabase.createTable(tableName: String, ifNotExists: Boolean = false, vararg columns: Pair<String, SqlOrderDirection>) {
    val escapedTableName = tableName.replace("`", "``")
    val ifNotExistsText = if (ifNotExists) "IF NOT EXISTS" else ""
    execSQL(
        columns.map { col ->
            "${col.first} ${col.second}"
        }.joinToString(", ", prefix = "CREATE TABLE $ifNotExistsText `$escapedTableName`(", postfix = ");")
    )
}

private fun SQLiteDatabase.dropTable(tableName: String, ifNotExists: Boolean = false) {
    val escapedTableName = tableName.replace("`", "``")
    val ifNotExistsText = if (ifNotExists) "IF NOT EXISTS" else ""
    execSQL("DROP TABLE $ifNotExistsText `$escapedTableName`;")
}

public enum class SqlOrderDirection {
    ASC
    DESC
}

public enum class SqlType(val text: String) {
    NULL : SqlType("NULL")
    INTEGER : SqlType("INT")
    REAL : SqlType("REAL")
    TEXT : SqlType("TEXT")
    BLOB : SqlType("BLOB")
}

public class QueryBuilder(val tableName: String) {
    public var distinct: Boolean = false

    private val columns = arrayListOf<String>()
    private val groupBy = arrayListOf<String>()
    private val orderBy = arrayListOf<String>()

    private var havingApplied = false
    private var having: String? = null
    private var limit: String? = null

    private var selectionApplied = false
    private var useNativeSelection = false
    private var selection: String? = null
    private var nativeSelectionArgs: Array<String>? = null

    fun exec(db: SQLiteDatabase): Cursor {
        val finalSelection = if (selectionApplied) selection else null
        val finalSelectionArgs = if (selectionApplied && useNativeSelection) nativeSelectionArgs else null
        return db.query(distinct, tableName, columns.copyToArray(),
            finalSelection, finalSelectionArgs,
            groupBy.joinToString(", "), having, orderBy.joinToString(", "), limit)
    }

    public fun column(name: String) {
        columns.add(name)
{'fraction_non_alphanumeric': 0.10967112597547381, 'fraction_numerical': 0.0, 'mean_word_length': 3.556825396825397, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 49, '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': '4598496', 'n_tokens_mistral': 2012, 'n_tokens_neox': 1984, 'n_words': 568}