404 lines
19 KiB
Kotlin
404 lines
19 KiB
Kotlin
package solutions.bitbadger.documents.java
|
|
|
|
import solutions.bitbadger.documents.Results
|
|
import solutions.bitbadger.documents.common.Field
|
|
import solutions.bitbadger.documents.common.FieldMatch
|
|
import solutions.bitbadger.documents.common.Parameter
|
|
import solutions.bitbadger.documents.common.ParameterType
|
|
import java.sql.Connection
|
|
import solutions.bitbadger.documents.common.query.orderBy
|
|
|
|
/**
|
|
* Functions to find and retrieve documents
|
|
*/
|
|
object Find {
|
|
|
|
/**
|
|
* Retrieve all documents in the given table, ordering results by the optional given fields
|
|
*
|
|
* @param tableName The table from which documents should be retrieved
|
|
* @param orderBy Fields by which the query should be ordered (optional, defaults to no ordering)
|
|
* @param conn The connection over which documents should be retrieved
|
|
* @return A list of documents from the given table
|
|
*/
|
|
inline fun <reified TDoc> all(tableName: String, orderBy: Collection<Field<*>>? = null, conn: Connection) =
|
|
conn.customList<TDoc>(all(tableName) + (orderBy?.let(::orderBy) ?: ""), mapFunc = Results::fromData)
|
|
|
|
/**
|
|
* Retrieve all documents in the given table
|
|
*
|
|
* @param tableName The table from which documents should be retrieved
|
|
* @param orderBy Fields by which the query should be ordered (optional, defaults to no ordering)
|
|
* @return A list of documents from the given table
|
|
*/
|
|
inline fun <reified TDoc> all(tableName: String, orderBy: Collection<Field<*>>? = null) =
|
|
Configuration.dbConn().use { all<TDoc>(tableName, orderBy, it) }
|
|
|
|
/**
|
|
* Retrieve all documents in the given table
|
|
*
|
|
* @param tableName The table from which documents should be retrieved
|
|
* @param conn The connection over which documents should be retrieved
|
|
* @return A list of documents from the given table
|
|
*/
|
|
inline fun <reified TDoc> all(tableName: String, conn: Connection) =
|
|
all<TDoc>(tableName, null, conn)
|
|
|
|
/**
|
|
* Retrieve a document by its ID
|
|
*
|
|
* @param tableName The table from which the document should be retrieved
|
|
* @param docId The ID of the document to retrieve
|
|
* @param conn The connection over which documents should be retrieved
|
|
* @return The document if it is found, `null` otherwise
|
|
*/
|
|
inline fun <TKey, reified TDoc> byId(tableName: String, docId: TKey, conn: Connection) =
|
|
conn.customSingle<TDoc>(
|
|
byId(tableName, docId),
|
|
Parameters.addFields(listOf(Field.equal(Configuration.idField, docId, ":id"))),
|
|
Results::fromData
|
|
)
|
|
|
|
/**
|
|
* Retrieve a document by its ID
|
|
*
|
|
* @param tableName The table from which the document should be retrieved
|
|
* @param docId The ID of the document to retrieve
|
|
* @return The document if it is found, `null` otherwise
|
|
*/
|
|
inline fun <TKey, reified TDoc> byId(tableName: String, docId: TKey) =
|
|
Configuration.dbConn().use { byId<TKey, TDoc>(tableName, docId, it) }
|
|
|
|
/**
|
|
* Retrieve documents using a field comparison, ordering results by the given fields
|
|
*
|
|
* @param tableName The table from which documents should be retrieved
|
|
* @param fields The fields which should be compared
|
|
* @param howMatched How the fields should be matched
|
|
* @param orderBy Fields by which the query should be ordered (optional, defaults to no ordering)
|
|
* @param conn The connection over which documents should be retrieved
|
|
* @return A list of documents matching the field comparison
|
|
*/
|
|
inline fun <reified TDoc> byFields(
|
|
tableName: String,
|
|
fields: Collection<Field<*>>,
|
|
howMatched: FieldMatch? = null,
|
|
orderBy: Collection<Field<*>>? = null,
|
|
conn: Connection
|
|
): List<TDoc> {
|
|
val named = Parameters.nameFields(fields)
|
|
return conn.customList<TDoc>(
|
|
byFields(tableName, named, howMatched) + (orderBy?.let(::orderBy) ?: ""),
|
|
Parameters.addFields(named),
|
|
Results::fromData
|
|
)
|
|
}
|
|
|
|
/**
|
|
* Retrieve documents using a field comparison, ordering results by the given fields
|
|
*
|
|
* @param tableName The table from which documents should be retrieved
|
|
* @param fields The fields which should be compared
|
|
* @param howMatched How the fields should be matched
|
|
* @param orderBy Fields by which the query should be ordered (optional, defaults to no ordering)
|
|
* @return A list of documents matching the field comparison
|
|
*/
|
|
inline fun <reified TDoc> byFields(
|
|
tableName: String,
|
|
fields: Collection<Field<*>>,
|
|
howMatched: FieldMatch? = null,
|
|
orderBy: Collection<Field<*>>? = null
|
|
) =
|
|
Configuration.dbConn().use { byFields<TDoc>(tableName, fields, howMatched, orderBy, it) }
|
|
|
|
/**
|
|
* Retrieve documents using a field comparison
|
|
*
|
|
* @param tableName The table from which documents should be retrieved
|
|
* @param fields The fields which should be compared
|
|
* @param howMatched How the fields should be matched
|
|
* @param conn The connection over which documents should be retrieved
|
|
* @return A list of documents matching the field comparison
|
|
*/
|
|
inline fun <reified TDoc> byFields(
|
|
tableName: String,
|
|
fields: Collection<Field<*>>,
|
|
howMatched: FieldMatch? = null,
|
|
conn: Connection
|
|
) =
|
|
byFields<TDoc>(tableName, fields, howMatched, null, conn)
|
|
|
|
/**
|
|
* Retrieve documents using a field comparison
|
|
*
|
|
* @param tableName The table from which documents should be retrieved
|
|
* @param fields The fields which should be compared
|
|
* @param howMatched How the fields should be matched
|
|
* @return A list of documents matching the field comparison
|
|
*/
|
|
inline fun <reified TDoc> byFields(
|
|
tableName: String,
|
|
fields: Collection<Field<*>>,
|
|
howMatched: FieldMatch? = null
|
|
) =
|
|
Configuration.dbConn().use { byFields<TDoc>(tableName, fields, howMatched, null, it) }
|
|
|
|
/**
|
|
* Retrieve documents using a JSON containment query, ordering results by the given fields (PostgreSQL only)
|
|
*
|
|
* @param tableName The table from which documents should be retrieved
|
|
* @param criteria The object for which JSON containment should be checked
|
|
* @param orderBy Fields by which the query should be ordered (optional, defaults to no ordering)
|
|
* @param conn The connection over which documents should be retrieved
|
|
* @return A list of documents matching the JSON containment query
|
|
* @throws DocumentException If called on a SQLite connection
|
|
*/
|
|
inline fun <reified TDoc, reified TContains> byContains(
|
|
tableName: String,
|
|
criteria: TContains,
|
|
orderBy: Collection<Field<*>>? = null,
|
|
conn: Connection
|
|
) =
|
|
conn.customList<TDoc>(
|
|
Find.byContains(tableName) + (orderBy?.let(::orderBy) ?: ""),
|
|
listOf(Parameters.json(":criteria", criteria)),
|
|
Results::fromData
|
|
)
|
|
|
|
/**
|
|
* Retrieve documents using a JSON containment query, ordering results by the given fields (PostgreSQL only)
|
|
*
|
|
* @param tableName The table from which documents should be retrieved
|
|
* @param criteria The object for which JSON containment should be checked
|
|
* @param orderBy Fields by which the query should be ordered (optional, defaults to no ordering)
|
|
* @return A list of documents matching the JSON containment query
|
|
* @throws DocumentException If called on a SQLite connection
|
|
*/
|
|
inline fun <reified TDoc, reified TContains> byContains(
|
|
tableName: String,
|
|
criteria: TContains,
|
|
orderBy: Collection<Field<*>>? = null
|
|
) =
|
|
Configuration.dbConn().use { byContains<TDoc, TContains>(tableName, criteria, orderBy, it) }
|
|
|
|
/**
|
|
* Retrieve documents using a JSON containment query (PostgreSQL only)
|
|
*
|
|
* @param tableName The table from which documents should be retrieved
|
|
* @param criteria The object for which JSON containment should be checked
|
|
* @param conn The connection over which documents should be retrieved
|
|
* @return A list of documents matching the JSON containment query
|
|
* @throws DocumentException If called on a SQLite connection
|
|
*/
|
|
inline fun <reified TDoc, reified TContains> byContains(tableName: String, criteria: TContains, conn: Connection) =
|
|
byContains<TDoc, TContains>(tableName, criteria, null, conn)
|
|
|
|
/**
|
|
* Retrieve documents using a JSON containment query (PostgreSQL only)
|
|
*
|
|
* @param tableName The table from which documents should be retrieved
|
|
* @param criteria The object for which JSON containment should be checked
|
|
* @return A list of documents matching the JSON containment query
|
|
* @throws DocumentException If called on a SQLite connection
|
|
*/
|
|
inline fun <reified TDoc, reified TContains> byContains(tableName: String, criteria: TContains) =
|
|
Configuration.dbConn().use { byContains<TDoc, TContains>(tableName, criteria, it) }
|
|
|
|
/**
|
|
* Retrieve documents using a JSON Path match query, ordering results by the given fields (PostgreSQL only)
|
|
*
|
|
* @param tableName The table from which documents should be retrieved
|
|
* @param path The JSON path comparison to match
|
|
* @param orderBy Fields by which the query should be ordered (optional, defaults to no ordering)
|
|
* @param conn The connection over which documents should be retrieved
|
|
* @return A list of documents matching the JSON Path match query
|
|
* @throws DocumentException If called on a SQLite connection
|
|
*/
|
|
inline fun <reified TDoc> byJsonPath(
|
|
tableName: String,
|
|
path: String,
|
|
orderBy: Collection<Field<*>>? = null,
|
|
conn: Connection
|
|
) =
|
|
conn.customList<TDoc>(
|
|
byJsonPath(tableName) + (orderBy?.let(::orderBy) ?: ""),
|
|
listOf(Parameter(":path", ParameterType.STRING, path)),
|
|
Results::fromData
|
|
)
|
|
|
|
/**
|
|
* Retrieve documents using a JSON Path match query, ordering results by the given fields (PostgreSQL only)
|
|
*
|
|
* @param tableName The table from which documents should be retrieved
|
|
* @param path The JSON path comparison to match
|
|
* @param orderBy Fields by which the query should be ordered (optional, defaults to no ordering)
|
|
* @return A list of documents matching the JSON Path match query
|
|
* @throws DocumentException If called on a SQLite connection
|
|
*/
|
|
inline fun <reified TDoc> byJsonPath(tableName: String, path: String, orderBy: Collection<Field<*>>? = null) =
|
|
Configuration.dbConn().use { byJsonPath<TDoc>(tableName, path, orderBy, it) }
|
|
|
|
/**
|
|
* Retrieve documents using a JSON Path match query (PostgreSQL only)
|
|
*
|
|
* @param tableName The table from which documents should be retrieved
|
|
* @param path The JSON path comparison to match
|
|
* @param conn The connection over which documents should be retrieved
|
|
* @return A list of documents matching the JSON Path match query
|
|
* @throws DocumentException If called on a SQLite connection
|
|
*/
|
|
inline fun <reified TDoc> byJsonPath(tableName: String, path: String, conn: Connection) =
|
|
byJsonPath<TDoc>(tableName, path, null, conn)
|
|
|
|
/**
|
|
* Retrieve the first document using a field comparison and optional ordering fields
|
|
*
|
|
* @param tableName The table from which documents should be retrieved
|
|
* @param fields The fields which should be compared
|
|
* @param howMatched How the fields should be matched (optional, defaults to `FieldMatch.ALL`)
|
|
* @param orderBy Fields by which the query should be ordered (optional, defaults to no ordering)
|
|
* @param conn The connection over which documents should be retrieved
|
|
* @return The first document matching the field comparison, or `null` if no matches are found
|
|
*/
|
|
inline fun <reified TDoc> firstByFields(
|
|
tableName: String,
|
|
fields: Collection<Field<*>>,
|
|
howMatched: FieldMatch? = null,
|
|
orderBy: Collection<Field<*>>? = null,
|
|
conn: Connection
|
|
): TDoc? {
|
|
val named = Parameters.nameFields(fields)
|
|
return conn.customSingle<TDoc>(
|
|
byFields(tableName, named, howMatched) + (orderBy?.let(::orderBy) ?: ""),
|
|
Parameters.addFields(named),
|
|
Results::fromData
|
|
)
|
|
}
|
|
|
|
/**
|
|
* Retrieve the first document using a field comparison and optional ordering fields
|
|
*
|
|
* @param tableName The table from which documents should be retrieved
|
|
* @param fields The fields which should be compared
|
|
* @param howMatched How the fields should be matched (optional, defaults to `FieldMatch.ALL`)
|
|
* @param orderBy Fields by which the query should be ordered (optional, defaults to no ordering)
|
|
* @return The first document matching the field comparison, or `null` if no matches are found
|
|
*/
|
|
inline fun <reified TDoc> firstByFields(
|
|
tableName: String,
|
|
fields: Collection<Field<*>>,
|
|
howMatched: FieldMatch? = null,
|
|
orderBy: Collection<Field<*>>? = null
|
|
) =
|
|
Configuration.dbConn().use { firstByFields<TDoc>(tableName, fields, howMatched, orderBy, it) }
|
|
|
|
/**
|
|
* Retrieve the first document using a field comparison
|
|
*
|
|
* @param tableName The table from which documents should be retrieved
|
|
* @param fields The fields which should be compared
|
|
* @param howMatched How the fields should be matched (optional, defaults to `FieldMatch.ALL`)
|
|
* @param conn The connection over which documents should be retrieved
|
|
* @return The first document matching the field comparison, or `null` if no matches are found
|
|
*/
|
|
inline fun <reified TDoc> firstByFields(
|
|
tableName: String,
|
|
fields: Collection<Field<*>>,
|
|
howMatched: FieldMatch? = null,
|
|
conn: Connection
|
|
) =
|
|
firstByFields<TDoc>(tableName, fields, howMatched, null, conn)
|
|
|
|
/**
|
|
* Retrieve the first document using a JSON containment query and optional ordering fields (PostgreSQL only)
|
|
*
|
|
* @param tableName The table from which documents should be retrieved
|
|
* @param criteria The object for which JSON containment should be checked
|
|
* @param orderBy Fields by which the query should be ordered (optional, defaults to no ordering)
|
|
* @param conn The connection over which documents should be retrieved
|
|
* @return The first document matching the JSON containment query, or `null` if no matches are found
|
|
* @throws DocumentException If called on a SQLite connection
|
|
*/
|
|
inline fun <reified TDoc, reified TContains> firstByContains(
|
|
tableName: String,
|
|
criteria: TContains,
|
|
orderBy: Collection<Field<*>>? = null,
|
|
conn: Connection
|
|
) =
|
|
conn.customSingle<TDoc>(
|
|
Find.byContains(tableName) + (orderBy?.let(::orderBy) ?: ""),
|
|
listOf(Parameters.json(":criteria", criteria)),
|
|
Results::fromData
|
|
)
|
|
|
|
/**
|
|
* Retrieve the first document using a JSON containment query (PostgreSQL only)
|
|
*
|
|
* @param tableName The table from which documents should be retrieved
|
|
* @param criteria The object for which JSON containment should be checked
|
|
* @param conn The connection over which documents should be retrieved
|
|
* @return The first document matching the JSON containment query, or `null` if no matches are found
|
|
* @throws DocumentException If called on a SQLite connection
|
|
*/
|
|
inline fun <reified TDoc, reified TContains> firstByContains(tableName: String, criteria: TContains, conn: Connection) =
|
|
firstByContains<TDoc, TContains>(tableName, criteria, null, conn)
|
|
|
|
/**
|
|
* Retrieve the first document using a JSON containment query and optional ordering fields (PostgreSQL only)
|
|
*
|
|
* @param tableName The table from which documents should be retrieved
|
|
* @param criteria The object for which JSON containment should be checked
|
|
* @param orderBy Fields by which the query should be ordered (optional, defaults to no ordering)
|
|
* @return The first document matching the JSON containment query, or `null` if no matches are found
|
|
* @throws DocumentException If called on a SQLite connection
|
|
*/
|
|
inline fun <reified TDoc, reified TContains> firstByContains(tableName: String, criteria: TContains, orderBy: Collection<Field<*>>? = null) =
|
|
Configuration.dbConn().use { firstByContains<TDoc, TContains>(tableName, criteria, orderBy, it) }
|
|
|
|
/**
|
|
* Retrieve the first document using a JSON Path match query and optional ordering fields (PostgreSQL only)
|
|
*
|
|
* @param tableName The table from which documents should be retrieved
|
|
* @param path The JSON path comparison to match
|
|
* @param orderBy Fields by which the query should be ordered (optional, defaults to no ordering)
|
|
* @param conn The connection over which documents should be retrieved
|
|
* @return The first document matching the JSON Path match query, or `null` if no matches are found
|
|
* @throws DocumentException If called on a SQLite connection
|
|
*/
|
|
inline fun <reified TDoc> firstByJsonPath(
|
|
tableName: String,
|
|
path: String,
|
|
orderBy: Collection<Field<*>>? = null,
|
|
conn: Connection
|
|
) =
|
|
conn.customSingle<TDoc>(
|
|
byJsonPath(tableName) + (orderBy?.let(::orderBy) ?: ""),
|
|
listOf(Parameter(":path", ParameterType.STRING, path)),
|
|
Results::fromData
|
|
)
|
|
|
|
/**
|
|
* Retrieve the first document using a JSON Path match query (PostgreSQL only)
|
|
*
|
|
* @param tableName The table from which documents should be retrieved
|
|
* @param path The JSON path comparison to match
|
|
* @param conn The connection over which documents should be retrieved
|
|
* @return The first document matching the JSON Path match query, or `null` if no matches are found
|
|
* @throws DocumentException If called on a SQLite connection
|
|
*/
|
|
inline fun <reified TDoc> firstByJsonPath(tableName: String, path: String, conn: Connection) =
|
|
firstByJsonPath<TDoc>(tableName, path, null, conn)
|
|
|
|
/**
|
|
* Retrieve the first document using a JSON Path match query and optional ordering fields (PostgreSQL only)
|
|
*
|
|
* @param tableName The table from which documents should be retrieved
|
|
* @param path The JSON path comparison to match
|
|
* @param orderBy Fields by which the query should be ordered (optional, defaults to no ordering)
|
|
* @return The first document matching the JSON Path match query, or `null` if no matches are found
|
|
* @throws DocumentException If called on a SQLite connection
|
|
*/
|
|
inline fun <reified TDoc> firstByJsonPath(tableName: String, path: String, orderBy: Collection<Field<*>>? = null) =
|
|
Configuration.dbConn().use { firstByJsonPath<TDoc>(tableName, path, orderBy, it) }
|
|
}
|