This project now contains: - A generic JVM document library (with Kotlin extensions on the JDBC `Connection` object) - A Groovy library which adds extension methods to the `Connection` object - A Scala library, which uses native Scala collections and adds Scala-style extension methods to the `Connection` object - A Kotlin library which uses `kotlinx.serialization` (no reflection, reified generic types) along with `Connection` extensions Reviewed-on: #1
156 lines
6.2 KiB
Kotlin
156 lines
6.2 KiB
Kotlin
package solutions.bitbadger.documents.java
|
|
|
|
import solutions.bitbadger.documents.*
|
|
import solutions.bitbadger.documents.query.ExistsQuery
|
|
import java.sql.Connection
|
|
import kotlin.jvm.Throws
|
|
|
|
/**
|
|
* Functions to determine whether documents exist
|
|
*/
|
|
object Exists {
|
|
|
|
/**
|
|
* Determine a document's existence by its ID
|
|
*
|
|
* @param tableName The name of the table in which document existence should be checked
|
|
* @param docId The ID of the document to be checked
|
|
* @param conn The connection on which the existence check should be executed
|
|
* @return True if the document exists, false if not
|
|
* @throws DocumentException If no dialect has been configured
|
|
*/
|
|
@Throws(DocumentException::class)
|
|
@JvmStatic
|
|
fun <TKey> byId(tableName: String, docId: TKey, conn: Connection) =
|
|
Custom.scalar(
|
|
ExistsQuery.byId(tableName, docId),
|
|
Parameters.addFields(listOf(Field.equal(Configuration.idField, docId, ":id"))),
|
|
Boolean::class.java,
|
|
conn,
|
|
Results::toExists
|
|
)
|
|
|
|
/**
|
|
* Determine a document's existence by its ID
|
|
*
|
|
* @param tableName The name of the table in which document existence should be checked
|
|
* @param docId The ID of the document to be checked
|
|
* @return True if the document exists, false if not
|
|
* @throws DocumentException If no connection string has been set
|
|
*/
|
|
@Throws(DocumentException::class)
|
|
@JvmStatic
|
|
fun <TKey> byId(tableName: String, docId: TKey) =
|
|
Configuration.dbConn().use { byId(tableName, docId, it) }
|
|
|
|
/**
|
|
* Determine document existence using a field comparison
|
|
*
|
|
* @param tableName The name of the table in which document existence should be checked
|
|
* @param fields The fields which should be compared
|
|
* @param howMatched How the fields should be matched
|
|
* @param conn The connection on which the existence check should be executed
|
|
* @return True if any matching documents exist, false if not
|
|
* @throws DocumentException If no dialect has been configured, or if parameters are invalid
|
|
*/
|
|
@Throws(DocumentException::class)
|
|
@JvmStatic
|
|
@JvmOverloads
|
|
fun byFields(
|
|
tableName: String,
|
|
fields: Collection<Field<*>>,
|
|
howMatched: FieldMatch? = null,
|
|
conn: Connection
|
|
): Boolean {
|
|
val named = Parameters.nameFields(fields)
|
|
return Custom.scalar(
|
|
ExistsQuery.byFields(tableName, named, howMatched),
|
|
Parameters.addFields(named),
|
|
Boolean::class.java,
|
|
conn,
|
|
Results::toExists
|
|
)
|
|
}
|
|
|
|
/**
|
|
* Determine document existence using a field comparison
|
|
*
|
|
* @param tableName The name of the table in which document existence should be checked
|
|
* @param fields The fields which should be compared
|
|
* @param howMatched How the fields should be matched
|
|
* @return True if any matching documents exist, false if not
|
|
* @throws DocumentException If no connection string has been set, or if parameters are invalid
|
|
*/
|
|
@Throws(DocumentException::class)
|
|
@JvmStatic
|
|
@JvmOverloads
|
|
fun byFields(tableName: String, fields: Collection<Field<*>>, howMatched: FieldMatch? = null) =
|
|
Configuration.dbConn().use { byFields(tableName, fields, howMatched, it) }
|
|
|
|
/**
|
|
* Determine document existence using a JSON containment query (PostgreSQL only)
|
|
*
|
|
* @param tableName The name of the table in which document existence should be checked
|
|
* @param criteria The object for which JSON containment should be checked
|
|
* @param conn The connection on which the existence check should be executed
|
|
* @return True if any matching documents exist, false if not
|
|
* @throws DocumentException If called on a SQLite connection
|
|
*/
|
|
@Throws(DocumentException::class)
|
|
@JvmStatic
|
|
fun <TContains> byContains(tableName: String, criteria: TContains, conn: Connection) =
|
|
Custom.scalar(
|
|
ExistsQuery.byContains(tableName),
|
|
listOf(Parameters.json(":criteria", criteria)),
|
|
Boolean::class.java,
|
|
conn,
|
|
Results::toExists
|
|
)
|
|
|
|
/**
|
|
* Determine document existence using a JSON containment query (PostgreSQL only)
|
|
*
|
|
* @param tableName The name of the table in which document existence should be checked
|
|
* @param criteria The object for which JSON containment should be checked
|
|
* @return True if any matching documents exist, false if not
|
|
* @throws DocumentException If no connection string has been set, or if called on a SQLite connection
|
|
*/
|
|
@Throws(DocumentException::class)
|
|
@JvmStatic
|
|
fun <TContains> byContains(tableName: String, criteria: TContains) =
|
|
Configuration.dbConn().use { byContains(tableName, criteria, it) }
|
|
|
|
/**
|
|
* Determine document existence using a JSON Path match query (PostgreSQL only)
|
|
*
|
|
* @param tableName The name of the table in which document existence should be checked
|
|
* @param path The JSON path comparison to match
|
|
* @param conn The connection on which the existence check should be executed
|
|
* @return True if any matching documents exist, false if not
|
|
* @throws DocumentException If called on a SQLite connection
|
|
*/
|
|
@Throws(DocumentException::class)
|
|
@JvmStatic
|
|
fun byJsonPath(tableName: String, path: String, conn: Connection) =
|
|
Custom.scalar(
|
|
ExistsQuery.byJsonPath(tableName),
|
|
listOf(Parameter(":path", ParameterType.STRING, path)),
|
|
Boolean::class.java,
|
|
conn,
|
|
Results::toExists
|
|
)
|
|
|
|
/**
|
|
* Determine document existence using a JSON Path match query (PostgreSQL only)
|
|
*
|
|
* @param tableName The name of the table in which document existence should be checked
|
|
* @param path The JSON path comparison to match
|
|
* @return True if any matching documents exist, false if not
|
|
* @throws DocumentException If no connection string has been set, or if called on a SQLite connection
|
|
*/
|
|
@Throws(DocumentException::class)
|
|
@JvmStatic
|
|
fun byJsonPath(tableName: String, path: String) =
|
|
Configuration.dbConn().use { byJsonPath(tableName, path, it) }
|
|
}
|