WIP on remove fields
This commit is contained in:
@@ -98,4 +98,23 @@ object Parameters {
|
||||
throw DocumentException("Error creating query / binding parameters: ${ex.message}", ex)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create parameters for field names to be removed from a document
|
||||
*
|
||||
* @param names The names of the fields to be removed
|
||||
* @param parameterName The parameter name to use for the query
|
||||
* @return A list of parameters to use for building the query
|
||||
*/
|
||||
fun fieldNames(names: Collection<String>, parameterName: String = ":name") =
|
||||
when (Configuration.dialect("generate field name parameters")) {
|
||||
Dialect.POSTGRESQL -> listOf(Parameter(parameterName, ParameterType.STRING, if (names.size == 1) {
|
||||
names.elementAt(0)
|
||||
} else {
|
||||
names.joinToString(",").let { "{$it}" }
|
||||
}))
|
||||
Dialect.SQLITE -> names.mapIndexed { index, name ->
|
||||
Parameter("$parameterName$index", ParameterType.STRING, name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,7 +51,7 @@ object Patch {
|
||||
* @param tableName The name of the table where the document is stored
|
||||
* @return A query to patch JSON documents by JSON containment
|
||||
*/
|
||||
fun <TKey> byContains(tableName: String) =
|
||||
fun byContains(tableName: String) =
|
||||
statementWhere(patch(tableName), Where.jsonContains())
|
||||
|
||||
/**
|
||||
@@ -60,6 +60,6 @@ object Patch {
|
||||
* @param tableName The name of the table where the document is stored
|
||||
* @return A query to patch JSON documents by JSON path match
|
||||
*/
|
||||
fun <TKey> byJsonPath(tableName: String) =
|
||||
fun byJsonPath(tableName: String) =
|
||||
statementWhere(patch(tableName), Where.jsonPathMatches())
|
||||
}
|
||||
|
||||
74
src/main/kotlin/query/RemoveFields.kt
Normal file
74
src/main/kotlin/query/RemoveFields.kt
Normal file
@@ -0,0 +1,74 @@
|
||||
package solutions.bitbadger.documents.query
|
||||
|
||||
import solutions.bitbadger.documents.*
|
||||
import solutions.bitbadger.documents.query.byFields as byFieldsBase
|
||||
import solutions.bitbadger.documents.query.byId as byIdBase
|
||||
|
||||
/**
|
||||
* Functions to create queries to remove fields from documents
|
||||
*/
|
||||
object RemoveFields {
|
||||
|
||||
/**
|
||||
* Create a query to remove fields based on the given parameters
|
||||
*
|
||||
* @param tableName The name of the table in which documents should have fields removed
|
||||
* @param toRemove The parameters for the fields to be removed
|
||||
* @return A query to remove fields from documents in the given table
|
||||
*/
|
||||
private fun removeFields(tableName: String, toRemove: List<Parameter<String>>) =
|
||||
when (Configuration.dialect("generate field removal query")) {
|
||||
Dialect.POSTGRESQL -> "UPDATE $tableName SET data = data - ${toRemove[0].name}::text[]"
|
||||
Dialect.SQLITE -> toRemove.joinToString(", ") { it.name }.let {
|
||||
"UPDATE $tableName SET data = json_remove(data, $it)"
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A query to patch (partially update) a JSON document by its ID
|
||||
*
|
||||
* @param tableName The name of the table where the document is stored
|
||||
* @param toRemove The parameters for the fields to be removed
|
||||
* @param docId The ID of the document to be updated (optional, used for type checking)
|
||||
* @return A query to patch a JSON document by its ID
|
||||
*/
|
||||
fun <TKey> byId(tableName: String, toRemove: List<Parameter<String>>, docId: TKey? = null) =
|
||||
byIdBase(removeFields(tableName, toRemove), docId)
|
||||
|
||||
/**
|
||||
* A query to patch (partially update) a JSON document using field match criteria
|
||||
*
|
||||
* @param tableName The name of the table where the documents are stored
|
||||
* @param toRemove The parameters for the fields to be removed
|
||||
* @param fields The field criteria
|
||||
* @param howMatched How the fields should be matched (optional, defaults to `ALL`)
|
||||
* @return A query to patch JSON documents by field match criteria
|
||||
*/
|
||||
fun byFields(
|
||||
tableName: String,
|
||||
toRemove: List<Parameter<String>>,
|
||||
fields: Collection<Field<*>>,
|
||||
howMatched: FieldMatch? = null
|
||||
) =
|
||||
byFieldsBase(removeFields(tableName, toRemove), fields, howMatched)
|
||||
|
||||
/**
|
||||
* A query to patch (partially update) a JSON document by JSON containment (PostgreSQL only)
|
||||
*
|
||||
* @param tableName The name of the table where the document is stored
|
||||
* @param toRemove The parameters for the fields to be removed
|
||||
* @return A query to patch JSON documents by JSON containment
|
||||
*/
|
||||
fun byContains(tableName: String, toRemove: List<Parameter<String>>) =
|
||||
statementWhere(removeFields(tableName, toRemove), Where.jsonContains())
|
||||
|
||||
/**
|
||||
* A query to patch (partially update) a JSON document by JSON path match (PostgreSQL only)
|
||||
*
|
||||
* @param tableName The name of the table where the document is stored
|
||||
* @param toRemove The parameters for the fields to be removed
|
||||
* @return A query to patch JSON documents by JSON path match
|
||||
*/
|
||||
fun byJsonPath(tableName: String, toRemove: List<Parameter<String>>) =
|
||||
statementWhere(removeFields(tableName, toRemove), Where.jsonPathMatches())
|
||||
}
|
||||
Reference in New Issue
Block a user