Initial Development (#1)
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
This commit was merged in pull request #1.
This commit is contained in:
885
src/core/src/main/kotlin/java/extensions/Connection.kt
Normal file
885
src/core/src/main/kotlin/java/extensions/Connection.kt
Normal file
@@ -0,0 +1,885 @@
|
||||
@file:JvmName("ConnExt")
|
||||
|
||||
package solutions.bitbadger.documents.java.extensions
|
||||
|
||||
import solutions.bitbadger.documents.*
|
||||
import solutions.bitbadger.documents.java.*
|
||||
import java.io.PrintWriter
|
||||
import java.sql.Connection
|
||||
import java.sql.ResultSet
|
||||
import kotlin.jvm.Throws
|
||||
|
||||
// ~~~ CUSTOM QUERIES ~~~
|
||||
|
||||
/**
|
||||
* Execute a query that returns a list of results
|
||||
*
|
||||
* @param query The query to retrieve the results
|
||||
* @param parameters Parameters to use for the query
|
||||
* @param clazz The class of the document to be returned
|
||||
* @param mapFunc The mapping function between the document and the domain item
|
||||
* @return A list of results for the given query
|
||||
* @throws DocumentException If parameters are invalid
|
||||
*/
|
||||
@Throws(DocumentException::class)
|
||||
fun <TDoc> Connection.customList(
|
||||
query: String,
|
||||
parameters: Collection<Parameter<*>> = listOf(),
|
||||
clazz: Class<TDoc>,
|
||||
mapFunc: (ResultSet, Class<TDoc>) -> TDoc
|
||||
) = Custom.list(query, parameters, clazz, this, mapFunc)
|
||||
|
||||
/**
|
||||
* Execute a query that returns a JSON array of results
|
||||
*
|
||||
* @param query The query to retrieve the results
|
||||
* @param parameters Parameters to use for the query
|
||||
* @param mapFunc The mapping function to extract the JSON from the query
|
||||
* @return A JSON array of results for the given query
|
||||
* @throws DocumentException If parameters are invalid
|
||||
*/
|
||||
@Throws(DocumentException::class)
|
||||
fun Connection.customJsonArray(
|
||||
query: String,
|
||||
parameters: Collection<Parameter<*>> = listOf(),
|
||||
mapFunc: (ResultSet) -> String
|
||||
) = Custom.jsonArray(query, parameters, this, mapFunc)
|
||||
|
||||
/**
|
||||
* Execute a query, writing its JSON array of results to the given `PrintWriter` (creates connection)
|
||||
*
|
||||
* @param query The query to retrieve the results
|
||||
* @param parameters Parameters to use for the query
|
||||
* @param writer The writer to which the results should be written
|
||||
* @param mapFunc The mapping function to extract the JSON from the query
|
||||
* @return A JSON array of results for the given query
|
||||
* @throws DocumentException If parameters are invalid
|
||||
*/
|
||||
@Throws(DocumentException::class)
|
||||
fun Connection.writeCustomJsonArray(
|
||||
query: String,
|
||||
parameters: Collection<Parameter<*>> = listOf(),
|
||||
writer: PrintWriter,
|
||||
mapFunc: (ResultSet) -> String
|
||||
) = Custom.writeJsonArray(query, parameters, writer, this, mapFunc)
|
||||
|
||||
/**
|
||||
* Execute a query that returns one or no results
|
||||
*
|
||||
* @param query The query to retrieve the results
|
||||
* @param parameters Parameters to use for the query
|
||||
* @param clazz The class of the document to be returned
|
||||
* @param mapFunc The mapping function between the document and the domain item
|
||||
* @return The document if one matches the query, `null` otherwise
|
||||
* @throws DocumentException If parameters are invalid
|
||||
*/
|
||||
@Throws(DocumentException::class)
|
||||
fun <TDoc> Connection.customSingle(
|
||||
query: String,
|
||||
parameters: Collection<Parameter<*>> = listOf(),
|
||||
clazz: Class<TDoc>,
|
||||
mapFunc: (ResultSet, Class<TDoc>) -> TDoc
|
||||
) = Custom.single(query, parameters, clazz, this, mapFunc)
|
||||
|
||||
/**
|
||||
* Execute a query that returns JSON for one or no documents
|
||||
*
|
||||
* @param query The query to retrieve the results
|
||||
* @param parameters Parameters to use for the query
|
||||
* @param mapFunc The mapping function between the document and the domain item
|
||||
* @return The JSON for the document if found, an empty object (`{}`) if not
|
||||
* @throws DocumentException If parameters are invalid
|
||||
*/
|
||||
@Throws(DocumentException::class)
|
||||
fun Connection.customJsonSingle(
|
||||
query: String,
|
||||
parameters: Collection<Parameter<*>> = listOf(),
|
||||
mapFunc: (ResultSet) -> String
|
||||
) = Custom.jsonSingle(query, parameters, this, mapFunc)
|
||||
|
||||
/**
|
||||
* Execute a query that returns no results
|
||||
*
|
||||
* @param query The query to retrieve the results
|
||||
* @param parameters Parameters to use for the query
|
||||
* @throws DocumentException If parameters are invalid
|
||||
*/
|
||||
@Throws(DocumentException::class)
|
||||
@JvmOverloads
|
||||
fun Connection.customNonQuery(query: String, parameters: Collection<Parameter<*>> = listOf()) =
|
||||
Custom.nonQuery(query, parameters, this)
|
||||
|
||||
/**
|
||||
* Execute a query that returns a scalar result
|
||||
*
|
||||
* @param query The query to retrieve the result
|
||||
* @param parameters Parameters to use for the query
|
||||
* @param clazz The class of the document to be returned
|
||||
* @param mapFunc The mapping function between the document and the domain item
|
||||
* @return The scalar value from the query
|
||||
* @throws DocumentException If parameters are invalid
|
||||
*/
|
||||
@Throws(DocumentException::class)
|
||||
fun <T : Any> Connection.customScalar(
|
||||
query: String,
|
||||
parameters: Collection<Parameter<*>> = listOf(),
|
||||
clazz: Class<T>,
|
||||
mapFunc: (ResultSet, Class<T>) -> T
|
||||
) = Custom.scalar(query, parameters, clazz, this, mapFunc)
|
||||
|
||||
// ~~~ DEFINITION QUERIES ~~~
|
||||
|
||||
/**
|
||||
* Create a document table if necessary
|
||||
*
|
||||
* @param tableName The table whose existence should be ensured (may include schema)
|
||||
* @throws DocumentException If the dialect is not configured
|
||||
*/
|
||||
@Throws(DocumentException::class)
|
||||
fun Connection.ensureTable(tableName: String) =
|
||||
Definition.ensureTable(tableName, this)
|
||||
|
||||
/**
|
||||
* Create an index on field(s) within documents in the specified table if necessary
|
||||
*
|
||||
* @param tableName The table to be indexed (may include schema)
|
||||
* @param indexName The name of the index to create
|
||||
* @param fields One or more fields to be indexed<
|
||||
* @throws DocumentException If any dependent process does
|
||||
*/
|
||||
@Throws(DocumentException::class)
|
||||
fun Connection.ensureFieldIndex(tableName: String, indexName: String, fields: Collection<String>) =
|
||||
Definition.ensureFieldIndex(tableName, indexName, fields, this)
|
||||
|
||||
/**
|
||||
* Create a document index on a table (PostgreSQL only)
|
||||
*
|
||||
* @param tableName The table to be indexed (may include schema)
|
||||
* @param indexType The type of index to ensure
|
||||
* @throws DocumentException If called on a SQLite connection
|
||||
*/
|
||||
@Throws(DocumentException::class)
|
||||
fun Connection.ensureDocumentIndex(tableName: String, indexType: DocumentIndex) =
|
||||
Definition.ensureDocumentIndex(tableName, indexType, this)
|
||||
|
||||
// ~~~ DOCUMENT MANIPULATION QUERIES ~~~
|
||||
|
||||
/**
|
||||
* Insert a new document
|
||||
*
|
||||
* @param tableName The table into which the document should be inserted (may include schema)
|
||||
* @param document The document to be inserted
|
||||
* @throws DocumentException If IDs are misconfigured, or if the database command fails
|
||||
*/
|
||||
@Throws(DocumentException::class)
|
||||
fun <TDoc> Connection.insert(tableName: String, document: TDoc) =
|
||||
Document.insert(tableName, document, this)
|
||||
|
||||
/**
|
||||
* Save a document, inserting it if it does not exist and updating it if it does (AKA "upsert")
|
||||
*
|
||||
* @param tableName The table in which the document should be saved (may include schema)
|
||||
* @param document The document to be saved
|
||||
* @throws DocumentException If the database command fails
|
||||
*/
|
||||
@Throws(DocumentException::class)
|
||||
fun <TDoc> Connection.save(tableName: String, document: TDoc) =
|
||||
Document.save(tableName, document, this)
|
||||
|
||||
/**
|
||||
* Update (replace) a document by its ID
|
||||
*
|
||||
* @param tableName The table in which the document should be replaced (may include schema)
|
||||
* @param docId The ID of the document to be replaced
|
||||
* @param document The document to be replaced
|
||||
* @throws DocumentException If no dialect has been configured, or if the database command fails
|
||||
*/
|
||||
@Throws(DocumentException::class)
|
||||
fun <TKey, TDoc> Connection.update(tableName: String, docId: TKey, document: TDoc) =
|
||||
Document.update(tableName, docId, document, this)
|
||||
|
||||
// ~~~ DOCUMENT COUNT QUERIES ~~~
|
||||
|
||||
/**
|
||||
* Count all documents in the table
|
||||
*
|
||||
* @param tableName The name of the table in which documents should be counted
|
||||
* @return A count of the documents in the table
|
||||
* @throws DocumentException If any dependent process does
|
||||
*/
|
||||
@Throws(DocumentException::class)
|
||||
fun Connection.countAll(tableName: String) =
|
||||
Count.all(tableName, this)
|
||||
|
||||
/**
|
||||
* Count documents using a field comparison
|
||||
*
|
||||
* @param tableName The name of the table in which documents should be counted
|
||||
* @param fields The fields which should be compared
|
||||
* @param howMatched How the fields should be matched
|
||||
* @return A count of the matching documents in the table
|
||||
* @throws DocumentException If the dialect has not been configured
|
||||
*/
|
||||
@Throws(DocumentException::class)
|
||||
@JvmOverloads
|
||||
fun Connection.countByFields(tableName: String, fields: Collection<Field<*>>, howMatched: FieldMatch? = null) =
|
||||
Count.byFields(tableName, fields, howMatched, this)
|
||||
|
||||
/**
|
||||
* Count documents using a JSON containment query (PostgreSQL only)
|
||||
*
|
||||
* @param tableName The name of the table in which documents should be counted
|
||||
* @param criteria The object for which JSON containment should be checked
|
||||
* @return A count of the matching documents in the table
|
||||
* @throws DocumentException If called on a SQLite connection
|
||||
*/
|
||||
@Throws(DocumentException::class)
|
||||
fun <TContains> Connection.countByContains(tableName: String, criteria: TContains) =
|
||||
Count.byContains(tableName, criteria, this)
|
||||
|
||||
/**
|
||||
* Count documents using a JSON Path match query (PostgreSQL only)
|
||||
*
|
||||
* @param tableName The name of the table in which documents should be counted
|
||||
* @param path The JSON path comparison to match
|
||||
* @return A count of the matching documents in the table
|
||||
* @throws DocumentException If called on a SQLite connection
|
||||
*/
|
||||
@Throws(DocumentException::class)
|
||||
fun Connection.countByJsonPath(tableName: String, path: String) =
|
||||
Count.byJsonPath(tableName, path, this)
|
||||
|
||||
// ~~~ DOCUMENT EXISTENCE QUERIES ~~~
|
||||
|
||||
/**
|
||||
* 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 dialect has been configured
|
||||
*/
|
||||
@Throws(DocumentException::class)
|
||||
fun <TKey> Connection.existsById(tableName: String, docId: TKey) =
|
||||
Exists.byId(tableName, docId, this)
|
||||
|
||||
/**
|
||||
* 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 dialect has been configured, or if parameters are invalid
|
||||
*/
|
||||
@Throws(DocumentException::class)
|
||||
@JvmOverloads
|
||||
fun Connection.existsByFields(tableName: String, fields: Collection<Field<*>>, howMatched: FieldMatch? = null) =
|
||||
Exists.byFields(tableName, fields, howMatched, this)
|
||||
|
||||
/**
|
||||
* 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 called on a SQLite connection
|
||||
*/
|
||||
@Throws(DocumentException::class)
|
||||
fun <TContains> Connection.existsByContains(tableName: String, criteria: TContains) =
|
||||
Exists.byContains(tableName, criteria, this)
|
||||
|
||||
/**
|
||||
* 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 called on a SQLite connection
|
||||
*/
|
||||
@Throws(DocumentException::class)
|
||||
fun Connection.existsByJsonPath(tableName: String, path: String) =
|
||||
Exists.byJsonPath(tableName, path, this)
|
||||
|
||||
// ~~~ DOCUMENT RETRIEVAL QUERIES (Domain Objects) ~~~
|
||||
|
||||
/**
|
||||
* 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 clazz The class of the document to be returned
|
||||
* @param orderBy Fields by which the query should be ordered (optional, defaults to no ordering)
|
||||
* @return A list of documents from the given table
|
||||
* @throws DocumentException If query execution fails
|
||||
*/
|
||||
@Throws(DocumentException::class)
|
||||
@JvmOverloads
|
||||
fun <TDoc> Connection.findAll(tableName: String, clazz: Class<TDoc>, orderBy: Collection<Field<*>>? = null) =
|
||||
Find.all(tableName, clazz, orderBy, this)
|
||||
|
||||
/**
|
||||
* 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 clazz The class of the document to be returned
|
||||
* @return An `Optional` item with the document if it is found
|
||||
* @throws DocumentException If no dialect has been configured
|
||||
*/
|
||||
@Throws(DocumentException::class)
|
||||
fun <TKey, TDoc> Connection.findById(tableName: String, docId: TKey, clazz: Class<TDoc>) =
|
||||
Find.byId(tableName, docId, clazz, this)
|
||||
|
||||
/**
|
||||
* Retrieve documents using a field comparison, ordering results by the optional given fields
|
||||
*
|
||||
* @param tableName The table from which the document should be retrieved
|
||||
* @param fields The fields which should be compared
|
||||
* @param clazz The class of the document to be returned
|
||||
* @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
|
||||
* @throws DocumentException If no dialect has been configured, or if parameters are invalid
|
||||
*/
|
||||
@Throws(DocumentException::class)
|
||||
@JvmOverloads
|
||||
fun <TDoc> Connection.findByFields(
|
||||
tableName: String,
|
||||
fields: Collection<Field<*>>,
|
||||
clazz: Class<TDoc>,
|
||||
howMatched: FieldMatch? = null,
|
||||
orderBy: Collection<Field<*>>? = null
|
||||
) = Find.byFields(tableName, fields, clazz, howMatched, orderBy, this)
|
||||
|
||||
/**
|
||||
* Retrieve documents using a JSON containment query, ordering results by the optional given fields (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 clazz The class of the document to be returned
|
||||
* @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
|
||||
*/
|
||||
@Throws(DocumentException::class)
|
||||
@JvmOverloads
|
||||
fun <TDoc, TContains> Connection.findByContains(
|
||||
tableName: String,
|
||||
criteria: TContains,
|
||||
clazz: Class<TDoc>,
|
||||
orderBy: Collection<Field<*>>? = null
|
||||
) = Find.byContains(tableName, criteria, clazz, orderBy, this)
|
||||
|
||||
/**
|
||||
* Retrieve documents using a JSON Path match query, ordering results by the optional given fields (PostgreSQL only)
|
||||
*
|
||||
* @param tableName The table from which documents should be retrieved
|
||||
* @param path The JSON path comparison to match
|
||||
* @param clazz The class of the document to be returned
|
||||
* @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
|
||||
*/
|
||||
@Throws(DocumentException::class)
|
||||
@JvmOverloads
|
||||
fun <TDoc> Connection.findByJsonPath(
|
||||
tableName: String,
|
||||
path: String,
|
||||
clazz: Class<TDoc>,
|
||||
orderBy: Collection<Field<*>>? = null
|
||||
) = Find.byJsonPath(tableName, path, clazz, orderBy, this)
|
||||
|
||||
/**
|
||||
* 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 clazz The class of the document to be returned
|
||||
* @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 An `Optional` item, with the first document matching the field comparison if found
|
||||
* @throws DocumentException If no dialect has been configured, or if parameters are invalid
|
||||
*/
|
||||
@Throws(DocumentException::class)
|
||||
@JvmOverloads
|
||||
fun <TDoc> Connection.findFirstByFields(
|
||||
tableName: String,
|
||||
fields: Collection<Field<*>>,
|
||||
clazz: Class<TDoc>,
|
||||
howMatched: FieldMatch? = null,
|
||||
orderBy: Collection<Field<*>>? = null
|
||||
) = Find.firstByFields(tableName, fields, clazz, howMatched, orderBy, this)
|
||||
|
||||
/**
|
||||
* 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 clazz The class of the document to be returned
|
||||
* @param orderBy Fields by which the query should be ordered (optional, defaults to no ordering)
|
||||
* @return An `Optional` item, with the first document matching the JSON containment query if found
|
||||
* @throws DocumentException If called on a SQLite connection
|
||||
*/
|
||||
@Throws(DocumentException::class)
|
||||
@JvmOverloads
|
||||
fun <TDoc, TContains> Connection.findFirstByContains(
|
||||
tableName: String,
|
||||
criteria: TContains,
|
||||
clazz: Class<TDoc>,
|
||||
orderBy: Collection<Field<*>>? = null
|
||||
) = Find.firstByContains(tableName, criteria, clazz, orderBy, this)
|
||||
|
||||
/**
|
||||
* 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 clazz The class of the document to be returned
|
||||
* @param orderBy Fields by which the query should be ordered (optional, defaults to no ordering)
|
||||
* @return An `Optional` item, with the first document matching the JSON Path match query if found
|
||||
* @throws DocumentException If called on a SQLite connection
|
||||
*/
|
||||
@Throws(DocumentException::class)
|
||||
@JvmOverloads
|
||||
fun <TDoc> Connection.findFirstByJsonPath(
|
||||
tableName: String,
|
||||
path: String,
|
||||
clazz: Class<TDoc>,
|
||||
orderBy: Collection<Field<*>>? = null
|
||||
) = Find.firstByJsonPath(tableName, path, clazz, orderBy, this)
|
||||
|
||||
// ~~~ DOCUMENT RETRIEVAL QUERIES (Raw JSON) ~~~
|
||||
|
||||
/**
|
||||
* 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)
|
||||
* @return A JSON array of documents from the given table
|
||||
* @throws DocumentException If query execution fails
|
||||
*/
|
||||
@Throws(DocumentException::class)
|
||||
@JvmOverloads
|
||||
fun Connection.jsonAll(tableName: String, orderBy: Collection<Field<*>>? = null) =
|
||||
Json.all(tableName, orderBy, this)
|
||||
|
||||
/**
|
||||
* 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 A JSON document if found, an empty JSON object if not found
|
||||
* @throws DocumentException If no dialect has been configured
|
||||
*/
|
||||
@Throws(DocumentException::class)
|
||||
fun <TKey> Connection.jsonById(tableName: String, docId: TKey) =
|
||||
Json.byId(tableName, docId, this)
|
||||
|
||||
/**
|
||||
* Retrieve documents using a field comparison, ordering results by the optional given fields
|
||||
*
|
||||
* @param tableName The table from which the document 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 JSON array of documents matching the field comparison
|
||||
* @throws DocumentException If no dialect has been configured, or if parameters are invalid
|
||||
*/
|
||||
@Throws(DocumentException::class)
|
||||
@JvmOverloads
|
||||
fun Connection.jsonByFields(
|
||||
tableName: String,
|
||||
fields: Collection<Field<*>>,
|
||||
howMatched: FieldMatch? = null,
|
||||
orderBy: Collection<Field<*>>? = null
|
||||
) = Json.byFields(tableName, fields, howMatched, orderBy, this)
|
||||
|
||||
/**
|
||||
* Retrieve documents using a JSON containment query, ordering results by the optional given fields (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 orderBy Fields by which the query should be ordered (optional, defaults to no ordering)
|
||||
* @return A JSON array of documents matching the JSON containment query
|
||||
* @throws DocumentException If called on a SQLite connection
|
||||
*/
|
||||
@Throws(DocumentException::class)
|
||||
@JvmOverloads
|
||||
fun <TContains> Connection.jsonByContains(
|
||||
tableName: String,
|
||||
criteria: TContains,
|
||||
orderBy: Collection<Field<*>>? = null
|
||||
) = Json.byContains(tableName, criteria, orderBy, this)
|
||||
|
||||
/**
|
||||
* Retrieve documents using a JSON Path match query, ordering results by the optional 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 JSON array of documents matching the JSON Path match query
|
||||
* @throws DocumentException If called on a SQLite connection
|
||||
*/
|
||||
@Throws(DocumentException::class)
|
||||
@JvmOverloads
|
||||
fun Connection.jsonByJsonPath(tableName: String, path: String, orderBy: Collection<Field<*>>? = null) =
|
||||
Json.byJsonPath(tableName, path, orderBy, this)
|
||||
|
||||
/**
|
||||
* 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 JSON document matching the field comparison if found, an empty JSON object otherwise
|
||||
* @throws DocumentException If no dialect has been configured, or if parameters are invalid
|
||||
*/
|
||||
@Throws(DocumentException::class)
|
||||
@JvmOverloads
|
||||
fun Connection.jsonFirstByFields(
|
||||
tableName: String,
|
||||
fields: Collection<Field<*>>,
|
||||
howMatched: FieldMatch? = null,
|
||||
orderBy: Collection<Field<*>>? = null
|
||||
) = Json.firstByFields(tableName, fields, howMatched, orderBy, this)
|
||||
|
||||
/**
|
||||
* 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 JSON document matching the JSON containment query if found, an empty JSON object otherwise
|
||||
* @throws DocumentException If called on a SQLite connection
|
||||
*/
|
||||
@Throws(DocumentException::class)
|
||||
@JvmOverloads
|
||||
fun <TContains> Connection.jsonFirstByContains(
|
||||
tableName: String,
|
||||
criteria: TContains,
|
||||
orderBy: Collection<Field<*>>? = null
|
||||
) = Json.firstByContains(tableName, criteria, orderBy, this)
|
||||
|
||||
/**
|
||||
* 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 JSON document matching the JSON Path match query if found, an empty JSON object otherwise
|
||||
* @throws DocumentException If called on a SQLite connection
|
||||
*/
|
||||
@Throws(DocumentException::class)
|
||||
@JvmOverloads
|
||||
fun Connection.jsonFirstByJsonPath(tableName: String, path: String, orderBy: Collection<Field<*>>? = null) =
|
||||
Json.firstByJsonPath(tableName, path, orderBy, this)
|
||||
|
||||
// ~~~ DOCUMENT RETRIEVAL QUERIES (Write raw JSON to output) ~~~
|
||||
|
||||
/**
|
||||
* Write all documents in the given table to the given `PrintWriter`, ordering results by the optional given fields
|
||||
*
|
||||
* @param tableName The table from which documents should be retrieved
|
||||
* @param writer The `PrintWriter` to which the results should be written
|
||||
* @param orderBy Fields by which the query should be ordered (optional, defaults to no ordering)
|
||||
* @throws DocumentException If query execution fails
|
||||
*/
|
||||
@Throws(DocumentException::class)
|
||||
@JvmOverloads
|
||||
fun Connection.writeJsonAll(tableName: String, writer: PrintWriter, orderBy: Collection<Field<*>>? = null) =
|
||||
Json.writeAll(tableName, writer, orderBy, this)
|
||||
|
||||
/**
|
||||
* Write a document to the given `PrintWriter` by its ID
|
||||
*
|
||||
* @param tableName The table from which the document should be retrieved
|
||||
* @param writer The `PrintWriter` to which the results should be written
|
||||
* @param docId The ID of the document to retrieve
|
||||
* @throws DocumentException If no dialect has been configured
|
||||
*/
|
||||
@Throws(DocumentException::class)
|
||||
fun <TKey> Connection.writeJsonById(tableName: String, writer: PrintWriter, docId: TKey) =
|
||||
Json.writeById(tableName, writer, docId, this)
|
||||
|
||||
/**
|
||||
* Write documents to the given `PrintWriter` using a field comparison, ordering results by the optional given fields
|
||||
*
|
||||
* @param tableName The table from which the document should be retrieved
|
||||
* @param writer The `PrintWriter` to which the results should be written
|
||||
* @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)
|
||||
* @throws DocumentException If no dialect has been configured, or if parameters are invalid
|
||||
*/
|
||||
@Throws(DocumentException::class)
|
||||
@JvmOverloads
|
||||
fun Connection.writeJsonByFields(
|
||||
tableName: String,
|
||||
writer: PrintWriter,
|
||||
fields: Collection<Field<*>>,
|
||||
howMatched: FieldMatch? = null,
|
||||
orderBy: Collection<Field<*>>? = null
|
||||
) = Json.writeByFields(tableName, writer, fields, howMatched, orderBy, this)
|
||||
|
||||
/**
|
||||
* Write documents to the given `PrintWriter` using a JSON containment query, ordering results by the optional given
|
||||
* fields (PostgreSQL only)
|
||||
*
|
||||
* @param tableName The name of the table in which document existence should be checked
|
||||
* @param writer The `PrintWriter` to which the results should be written
|
||||
* @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)
|
||||
* @throws DocumentException If called on a SQLite connection
|
||||
*/
|
||||
@Throws(DocumentException::class)
|
||||
@JvmOverloads
|
||||
fun <TContains> Connection.writeJsonByContains(
|
||||
tableName: String,
|
||||
writer: PrintWriter,
|
||||
criteria: TContains,
|
||||
orderBy: Collection<Field<*>>? = null
|
||||
) = Json.writeByContains(tableName, writer, criteria, orderBy, this)
|
||||
|
||||
/**
|
||||
* Write documents to the given `PrintWriter` using a JSON Path match query, ordering results by the optional given
|
||||
* fields (PostgreSQL only)
|
||||
*
|
||||
* @param tableName The table from which documents should be retrieved
|
||||
* @param writer The `PrintWriter` to which the results should be written
|
||||
* @param path The JSON path comparison to match
|
||||
* @param orderBy Fields by which the query should be ordered (optional, defaults to no ordering)
|
||||
* @throws DocumentException If called on a SQLite connection
|
||||
*/
|
||||
@Throws(DocumentException::class)
|
||||
@JvmOverloads
|
||||
fun Connection.writeJsonByJsonPath(
|
||||
tableName: String,
|
||||
writer: PrintWriter,
|
||||
path: String,
|
||||
orderBy: Collection<Field<*>>? = null
|
||||
) = Json.writeByJsonPath(tableName, writer, path, orderBy, this)
|
||||
|
||||
/**
|
||||
* Write the first document to the given `PrintWriter` using a field comparison and optional ordering fields
|
||||
*
|
||||
* @param tableName The table from which documents should be retrieved
|
||||
* @param writer The `PrintWriter` to which the results should be written
|
||||
* @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)
|
||||
* @throws DocumentException If no dialect has been configured, or if parameters are invalid
|
||||
*/
|
||||
@Throws(DocumentException::class)
|
||||
@JvmOverloads
|
||||
fun Connection.writeJsonFirstByFields(
|
||||
tableName: String,
|
||||
writer: PrintWriter,
|
||||
fields: Collection<Field<*>>,
|
||||
howMatched: FieldMatch? = null,
|
||||
orderBy: Collection<Field<*>>? = null
|
||||
) = Json.writeFirstByFields(tableName, writer, fields, howMatched, orderBy, this)
|
||||
|
||||
/**
|
||||
* Write the first document to the given `PrintWriter` using a JSON containment query and optional ordering fields
|
||||
* (PostgreSQL only)
|
||||
*
|
||||
* @param tableName The table from which documents should be retrieved
|
||||
* @param writer The `PrintWriter` to which the results should be written
|
||||
* @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)
|
||||
* @throws DocumentException If called on a SQLite connection
|
||||
*/
|
||||
@Throws(DocumentException::class)
|
||||
@JvmOverloads
|
||||
fun <TContains> Connection.writeJsonFirstByContains(
|
||||
tableName: String,
|
||||
writer: PrintWriter,
|
||||
criteria: TContains,
|
||||
orderBy: Collection<Field<*>>? = null
|
||||
) = Json.writeFirstByContains(tableName, writer, criteria, orderBy, this)
|
||||
|
||||
/**
|
||||
* Write the first document to the given `PrintWriter` using a JSON Path match query and optional ordering fields
|
||||
* (PostgreSQL only)
|
||||
*
|
||||
* @param tableName The table from which documents should be retrieved
|
||||
* @param writer The `PrintWriter` to which the results should be written
|
||||
* @param path The JSON path comparison to match
|
||||
* @param orderBy Fields by which the query should be ordered (optional, defaults to no ordering)
|
||||
* @throws DocumentException If called on a SQLite connection
|
||||
*/
|
||||
@Throws(DocumentException::class)
|
||||
@JvmOverloads
|
||||
fun Connection.writeJsonFirstByJsonPath(
|
||||
tableName: String,
|
||||
writer: PrintWriter,
|
||||
path: String,
|
||||
orderBy: Collection<Field<*>>? = null
|
||||
) = Json.writeFirstByJsonPath(tableName, writer, path, orderBy, this)
|
||||
|
||||
// ~~~ DOCUMENT PATCH (PARTIAL UPDATE) QUERIES ~~~
|
||||
|
||||
/**
|
||||
* Patch a document by its ID
|
||||
*
|
||||
* @param tableName The name of the table in which a document should be patched
|
||||
* @param docId The ID of the document to be patched
|
||||
* @param patch The object whose properties should be replaced in the document
|
||||
* @throws DocumentException If no dialect has been configured
|
||||
*/
|
||||
@Throws(DocumentException::class)
|
||||
fun <TKey, TPatch> Connection.patchById(tableName: String, docId: TKey, patch: TPatch) =
|
||||
Patch.byId(tableName, docId, patch, this)
|
||||
|
||||
/**
|
||||
* Patch documents using a field comparison
|
||||
*
|
||||
* @param tableName The name of the table in which documents should be patched
|
||||
* @param fields The fields which should be compared
|
||||
* @param patch The object whose properties should be replaced in the document
|
||||
* @param howMatched How the fields should be matched
|
||||
* @throws DocumentException If no dialect has been configured, or if parameters are invalid
|
||||
*/
|
||||
@Throws(DocumentException::class)
|
||||
@JvmOverloads
|
||||
fun <TPatch> Connection.patchByFields(
|
||||
tableName: String,
|
||||
fields: Collection<Field<*>>,
|
||||
patch: TPatch,
|
||||
howMatched: FieldMatch? = null
|
||||
) = Patch.byFields(tableName, fields, patch, howMatched, this)
|
||||
|
||||
/**
|
||||
* Patch documents using a JSON containment query (PostgreSQL only)
|
||||
*
|
||||
* @param tableName The name of the table in which documents should be patched
|
||||
* @param criteria The object against which JSON containment should be checked
|
||||
* @param patch The object whose properties should be replaced in the document
|
||||
* @throws DocumentException If called on a SQLite connection
|
||||
*/
|
||||
@Throws(DocumentException::class)
|
||||
fun <TContains, TPatch> Connection.patchByContains(
|
||||
tableName: String,
|
||||
criteria: TContains,
|
||||
patch: TPatch
|
||||
) = Patch.byContains(tableName, criteria, patch, this)
|
||||
|
||||
/**
|
||||
* Patch documents using a JSON Path match query (PostgreSQL only)
|
||||
*
|
||||
* @param tableName The name of the table in which documents should be patched
|
||||
* @param path The JSON path comparison to match
|
||||
* @param patch The object whose properties should be replaced in the document
|
||||
* @throws DocumentException If called on a SQLite connection
|
||||
*/
|
||||
@Throws(DocumentException::class)
|
||||
fun <TPatch> Connection.patchByJsonPath(tableName: String, path: String, patch: TPatch) =
|
||||
Patch.byJsonPath(tableName, path, patch, this)
|
||||
|
||||
// ~~~ DOCUMENT FIELD REMOVAL QUERIES ~~~
|
||||
|
||||
/**
|
||||
* Remove fields from a document by its ID
|
||||
*
|
||||
* @param tableName The name of the table in which the document's fields should be removed
|
||||
* @param docId The ID of the document to have fields removed
|
||||
* @param toRemove The names of the fields to be removed
|
||||
* @throws DocumentException If no dialect has been configured
|
||||
*/
|
||||
@Throws(DocumentException::class)
|
||||
fun <TKey> Connection.removeFieldsById(tableName: String, docId: TKey, toRemove: Collection<String>) =
|
||||
RemoveFields.byId(tableName, docId, toRemove, this)
|
||||
|
||||
/**
|
||||
* Remove fields from documents using a field comparison
|
||||
*
|
||||
* @param tableName The name of the table in which document fields should be removed
|
||||
* @param fields The fields which should be compared
|
||||
* @param toRemove The names of the fields to be removed
|
||||
* @param howMatched How the fields should be matched
|
||||
* @throws DocumentException If no dialect has been configured, or if parameters are invalid
|
||||
*/
|
||||
@Throws(DocumentException::class)
|
||||
@JvmOverloads
|
||||
fun Connection.removeFieldsByFields(
|
||||
tableName: String,
|
||||
fields: Collection<Field<*>>,
|
||||
toRemove: Collection<String>,
|
||||
howMatched: FieldMatch? = null
|
||||
) = RemoveFields.byFields(tableName, fields, toRemove, howMatched, this)
|
||||
|
||||
/**
|
||||
* Remove fields from documents using a JSON containment query (PostgreSQL only)
|
||||
*
|
||||
* @param tableName The name of the table in which document fields should be removed
|
||||
* @param criteria The object against which JSON containment should be checked
|
||||
* @param toRemove The names of the fields to be removed
|
||||
* @throws DocumentException If called on a SQLite connection
|
||||
*/
|
||||
@Throws(DocumentException::class)
|
||||
fun <TContains> Connection.removeFieldsByContains(
|
||||
tableName: String,
|
||||
criteria: TContains,
|
||||
toRemove: Collection<String>
|
||||
) = RemoveFields.byContains(tableName, criteria, toRemove, this)
|
||||
|
||||
/**
|
||||
* Remove fields from documents using a JSON Path match query (PostgreSQL only)
|
||||
*
|
||||
* @param tableName The name of the table in which document fields should be removed
|
||||
* @param path The JSON path comparison to match
|
||||
* @param toRemove The names of the fields to be removed
|
||||
* @throws DocumentException If called on a SQLite connection
|
||||
*/
|
||||
@Throws(DocumentException::class)
|
||||
fun Connection.removeFieldsByJsonPath(tableName: String, path: String, toRemove: Collection<String>) =
|
||||
RemoveFields.byJsonPath(tableName, path, toRemove, 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
|
||||
* @throws DocumentException If no dialect has been configured
|
||||
*/
|
||||
@Throws(DocumentException::class)
|
||||
fun <TKey> Connection.deleteById(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
|
||||
* @throws DocumentException If no dialect has been configured, or if parameters are invalid
|
||||
*/
|
||||
@Throws(DocumentException::class)
|
||||
@JvmOverloads
|
||||
fun Connection.deleteByFields(tableName: String, fields: Collection<Field<*>>, howMatched: FieldMatch? = null) =
|
||||
Delete.byFields(tableName, fields, howMatched, this)
|
||||
|
||||
/**
|
||||
* Delete documents using a JSON containment query (PostgreSQL only)
|
||||
*
|
||||
* @param tableName The name of the table from which documents should be deleted
|
||||
* @param criteria The object for which JSON containment should be checked
|
||||
* @throws DocumentException If called on a SQLite connection
|
||||
*/
|
||||
@Throws(DocumentException::class)
|
||||
fun <TContains> Connection.deleteByContains(tableName: String, criteria: TContains) =
|
||||
Delete.byContains(tableName, criteria, this)
|
||||
|
||||
/**
|
||||
* Delete documents using a JSON Path match query (PostgreSQL only)
|
||||
*
|
||||
* @param tableName The name of the table from which documents should be deleted
|
||||
* @param path The JSON path comparison to match
|
||||
* @throws DocumentException If called on a SQLite connection
|
||||
*/
|
||||
@Throws(DocumentException::class)
|
||||
fun Connection.deleteByJsonPath(tableName: String, path: String) =
|
||||
Delete.byJsonPath(tableName, path, this)
|
||||
Reference in New Issue
Block a user