Add common throwaway db; all ITs now work in both

This commit is contained in:
2025-02-27 17:32:00 -05:00
parent ad3b7d2316
commit 250e216ae8
18 changed files with 315 additions and 115 deletions

View File

@@ -21,4 +21,7 @@ class Comparison<T>(val op: Op, val value: T) {
}
return toCheck is Byte || toCheck is Short || toCheck is Int || toCheck is Long
}
override fun toString() =
"$op $value"
}

View File

@@ -114,3 +114,24 @@ inline fun <reified TDoc> Connection.findAll(tableName: String) =
*/
inline fun <reified TDoc> Connection.findAll(tableName: String, orderBy: Collection<Field<*>>) =
Find.all<TDoc>(tableName, orderBy, this)
// ~~~ DOCUMENT DELETION QUERIES ~~~
/**
* Delete a document by its ID
*
* @param tableName The name of the table from which documents should be deleted
* @param docId The ID of the document to be deleted
*/
fun <TKey> Connection.byId(tableName: String, docId: TKey) =
Delete.byId(tableName, docId, this)
/**
* Delete documents using a field comparison
*
* @param tableName The name of the table from which documents should be deleted
* @param fields The fields which should be compared
* @param howMatched How the fields should be matched
*/
fun Connection.deleteByFields(tableName: String, fields: Collection<Field<*>>, howMatched: FieldMatch? = null) =
Delete.byField(tableName, fields, howMatched, this)

55
src/main/kotlin/Delete.kt Normal file
View File

@@ -0,0 +1,55 @@
package solutions.bitbadger.documents
import solutions.bitbadger.documents.query.Delete
import java.sql.Connection
/**
* Functions to delete documents
*/
object Delete {
/**
* Delete a document by its ID
*
* @param tableName The name of the table from which documents should be deleted
* @param docId The ID of the document to be deleted
* @param conn The connection on which the deletion should be executed
*/
fun <TKey> byId(tableName: String, docId: TKey, conn: Connection) =
conn.customNonQuery(
Delete.byId(tableName, docId),
Parameters.addFields(listOf(Field.equal(Configuration.idField, docId)))
)
/**
* Delete a document by its ID
*
* @param tableName The name of the table from which documents should be deleted
* @param docId The ID of the document to be deleted
*/
fun <TKey> byId(tableName: String, docId: TKey) =
Configuration.dbConn().use { byId(tableName, docId, it) }
/**
* Delete documents using a field comparison
*
* @param tableName The name of the table from which documents should be deleted
* @param fields The fields which should be compared
* @param howMatched How the fields should be matched
* @param conn The connection on which the deletion should be executed
*/
fun byField(tableName: String, fields: Collection<Field<*>>, howMatched: FieldMatch? = null, conn: Connection) {
val named = Parameters.nameFields(fields)
conn.customNonQuery(Delete.byFields(tableName, named, howMatched), Parameters.addFields(named))
}
/**
* Delete documents using a field comparison
*
* @param tableName The name of the table from which documents should be deleted
* @param fields The fields which should be compared
* @param howMatched How the fields should be matched
*/
fun byField(tableName: String, fields: Collection<Field<*>>, howMatched: FieldMatch? = null) =
Configuration.dbConn().use { byField(tableName, fields, howMatched, it) }
}

View File

@@ -92,6 +92,9 @@ class Field<T> private constructor(
}
}
override fun toString() =
"Field ${parameterName ?: "<unnamed>"} $comparison${qualifier?.let { " (qualifier $it)"} ?: ""}"
companion object {
/**

View File

@@ -39,6 +39,30 @@ object Parameters {
inline fun <reified T> json(name: String, value: T) =
Parameter(name, ParameterType.JSON, Configuration.json.encodeToString(value))
/**
* Add field parameters to the given set of parameters
*
* @param fields The fields being compared in the query
* @param existing Any existing parameters for the query (optional, defaults to empty collection)
* @return A collection of parameters for the query
*/
fun addFields(
fields: Collection<Field<*>>,
existing: MutableCollection<Parameter<*>> = mutableListOf()
): MutableCollection<Parameter<*>> {
existing.addAll(
fields
.filter { it.comparison.op != Op.EXISTS && it.comparison.op != Op.NOT_EXISTS }
.map {
Parameter(
it.parameterName!!,
if (it.comparison.isNumeric) ParameterType.NUMBER else ParameterType.STRING,
it.comparison.value
)
})
return existing
}
/**
* Replace the parameter names in the query with question marks
*
@@ -47,7 +71,7 @@ object Parameters {
* @return The query, with name parameters changed to `?`s
*/
fun replaceNamesInQuery(query: String, parameters: Collection<Parameter<*>>) =
parameters.sortedByDescending { it.name.length }.fold(query) { acc, param -> acc.replace(param.name, "?") }.also(::println)
parameters.sortedByDescending { it.name.length }.fold(query) { acc, param -> acc.replace(param.name, "?") }
/**
* Apply the given parameters to the given query, returning a prepared statement