75 lines
		
	
	
		
			3.2 KiB
		
	
	
	
		
			Kotlin
		
	
	
	
	
	
			
		
		
	
	
			75 lines
		
	
	
		
			3.2 KiB
		
	
	
	
		
			Kotlin
		
	
	
	
	
	
| 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: Collection<Parameter<*>>) =
 | |
|         when (Configuration.dialect("generate field removal query")) {
 | |
|             Dialect.POSTGRESQL -> "UPDATE $tableName SET data = data - ${toRemove.elementAt(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: Collection<Parameter<*>>, 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: Collection<Parameter<*>>,
 | |
|         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: Collection<Parameter<*>>) =
 | |
|         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: Collection<Parameter<*>>) =
 | |
|         statementWhere(removeFields(tableName, toRemove), Where.jsonPathMatches())
 | |
| }
 |