Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.jan.supabase.postgrest.query

import io.github.jan.supabase.annotations.SupabaseExperimental
import io.github.jan.supabase.postgrest.classPropertyNames
import kotlin.jvm.JvmInline

Expand All @@ -17,6 +18,14 @@ value class Columns @PublishedApi internal constructor(val value: String) {
*/
val ALL = Columns("*")

/**
* TODO
*/
@SupabaseExperimental
inline operator fun invoke(builder: BasicColumnsBuilder.() -> Unit): Columns {
return Columns(BasicColumnsBuilder().apply(builder).build())
}

/**
* Select all columns given in the [value] parameter
* @param value The columns to select, separated by a comma
Expand All @@ -41,21 +50,6 @@ value class Columns @PublishedApi internal constructor(val value: String) {
*/
inline fun <reified T> type() = list(classPropertyNames<T>())

private fun String.clean(): String {
var quoted = false
val regex = Regex("\\s")
return this.map {
if (it == '"') {
quoted = !quoted
}
if (regex.matches(it.toString()) && !quoted) {
""
} else {
it
}
}.joinToString("")
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package io.github.jan.supabase.postgrest.query

open class BasicColumnsBuilder {

internal val columns: MutableList<String> = mutableListOf<String>()

fun named(vararg columns: String) {
this.columns.addAll(columns)
}

fun all() {
columns.add("*")
}

fun json(column: String, key: String, returnAsText: Boolean = false) {
val operator = if(returnAsText) "->>" else "->"
columns.add("$column$operator$key")
}

fun foreign(name: String, columnsBuilder: ForeignColumnsBuilder.() -> Unit = {}) {
val foreignColumns = ForeignColumnsBuilder().apply(columnsBuilder)
val spread = if(foreignColumns.spread) "..." else ""
val key = if(foreignColumns.key != null) "!${foreignColumns.key}" else ""
columns.add("$spread$name$key(${foreignColumns.build()})")
}

infix fun String.withAlias(alias: String) = "$alias:$this"

infix fun String.withFunction(name: String) = "$this.$name"

infix fun String.withType(type: String) = "$this::$type"
Comment on lines +92 to +98

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about the names here, maybe applyFunction or useFunction and castAs would be better


fun avg() = "avg()"
Comment thread Fixed

fun count() = "count()"
Comment thread Fixed

fun max() = "max()"
Comment thread Fixed

fun min() = "min()"
Comment thread Fixed

fun sum() = "sum()"
Comment thread Fixed

fun build() = columns.joinToString(",").also(::println)

}

class ForeignColumnsBuilder(): BasicColumnsBuilder() {

var spread = false
var key: String? = null

}

internal fun String.clean(): String {
var quoted = false
val regex = Regex("\\s")
return this.map {
if (it == '"') {
quoted = !quoted
}
if (regex.matches(it.toString()) && !quoted) {
""
} else {
it
}
}.joinToString("")
}