commonMain.io.github.jan.supabase.postgrest.query.PostgrestUpdate.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of postgrest-kt-jvm Show documentation
Show all versions of postgrest-kt-jvm Show documentation
Extends supabase-kt with a Postgrest Client
package io.github.jan.supabase.postgrest.query
import io.github.jan.supabase.SupabaseSerializer
import io.github.jan.supabase.annotations.SupabaseInternal
import io.github.jan.supabase.encodeToJsonElement
import io.github.jan.supabase.postgrest.PropertyConversionMethod
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.JsonNull
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonPrimitive
import kotlin.reflect.KProperty1
/**
* Represents a postgrest update query
*/
class PostgrestUpdate(@PublishedApi internal val propertyConversionMethod: PropertyConversionMethod, @PublishedApi internal val serializer: SupabaseSerializer) {
@PublishedApi
internal val map = mutableMapOf()
/**
* Sets the value of the column with the name of the [KProperty1] to [value]
*/
inline infix fun KProperty1.setTo(value: V?) {
if(value == null) {
setToNull(propertyConversionMethod(this))
} else {
set(propertyConversionMethod(this), value)
}
}
/**
* Sets the value of the column with the name of the [KProperty1] to [value]
*/
infix fun KProperty1.setTo(value: String?) = set(propertyConversionMethod(this), value)
/**
* Sets the value of the column with the name of the [KProperty1] to [value]
*/
infix fun KProperty1.setTo(value: Int?) = set(propertyConversionMethod(this), value)
/**
* Sets the value of the column with the name of the [KProperty1] to [value]
*/
infix fun KProperty1.setTo(value: Long?) = set(propertyConversionMethod(this), value)
/**
* Sets the value of the column with the name of the [KProperty1] to [value]
*/
infix fun KProperty1.setTo(value: Float?) = set(propertyConversionMethod(this), value)
/**
* Sets the value of the column with the name of the [KProperty1] to [value]
*/
infix fun KProperty1.setTo(value: Double?) = set(propertyConversionMethod(this), value)
/**
* Sets the value of the column with the name of the [KProperty1] to [value]
*/
infix fun KProperty1.setTo(value: Boolean?) = set(propertyConversionMethod(this), value)
/**
* Sets the value of the [column] to [value]
*/
operator fun set(column: String, value: String?) {
map[column] = JsonPrimitive(value)
}
/**
* Sets the value of the [column] to [value]
*/
operator fun set(column: String, value: Int?) {
map[column] = JsonPrimitive(value)
}
/**
* Sets the value of the [column] to [value]
*/
operator fun set(column: String, value: Long?) {
map[column] = JsonPrimitive(value)
}
/**
* Sets the value of the [column] to [value]
*/
operator fun set(column: String, value: Float?) {
map[column] = JsonPrimitive(value)
}
/**
* Sets the value of the [column] to [value]
*/
operator fun set(column: String, value: Double?) {
map[column] = JsonPrimitive(value)
}
/**
* Sets the value of the [column] to [value]
*/
operator fun set(column: String, value: Boolean?) {
map[column] = JsonPrimitive(value)
}
/**
* Sets the value of the [column] to null
*/
fun setToNull(column: String) {
map[column] = JsonNull
}
/**
* Sets the value of the [column] to [value]
*/
inline operator fun set(column: String, value: T?) {
if(value == null) {
setToNull(column)
} else {
map[column] = serializer.encodeToJsonElement(value)
}
}
@PublishedApi internal fun toJson() = JsonObject(map)
}
@SupabaseInternal
inline fun buildPostgrestUpdate(propertyConversionMethod: PropertyConversionMethod = PropertyConversionMethod.SERIAL_NAME, serializer: SupabaseSerializer, block: PostgrestUpdate.() -> Unit): JsonObject {
val update = PostgrestUpdate(propertyConversionMethod, serializer)
update.block()
return update.toJson()
}