Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce new experimental DSL for Postgrest Columns #761

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
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 {

Check warning

Code scanning / detekt

Public classes, interfaces and objects require documentation. Warning

BasicColumnsBuilder is missing required documentation.

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

fun named(vararg columns: String) {

Check warning

Code scanning / detekt

Public functions require documentation. Warning

The function named is missing documentation.
this.columns.addAll(columns)
}

fun all() {

Check warning

Code scanning / detekt

Public functions require documentation. Warning

The function all is missing documentation.
columns.add("*")
}

fun json(column: String, key: String, returnAsText: Boolean = false) {

Check warning

Code scanning / detekt

Public functions require documentation. Warning

The function json is missing documentation.
val operator = if(returnAsText) "->>" else "->"
columns.add("$column$operator$key")
}

fun foreign(name: String, columnsBuilder: ForeignColumnsBuilder.() -> Unit = {}) {

Check warning

Code scanning / detekt

Public functions require documentation. Warning

The function foreign is missing documentation.
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"

Check warning

Code scanning / detekt

Public functions require documentation. Warning

The function withAlias is missing documentation.

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

Check warning

Code scanning / detekt

Public functions require documentation. Warning

The function withFunction is missing documentation.

infix fun String.withType(type: String) = "$this::$type"

Check warning

Code scanning / detekt

Public functions require documentation. Warning

The function withType is missing documentation.

fun avg() = "avg()"

Check warning

Code scanning / detekt

Public functions require documentation. Warning

The function avg is missing documentation.

Check warning

Code scanning / detekt

A function that only returns a constant is misleading. Consider declaring a constant instead. Warning

avg is returning a constant. Prefer declaring a constant instead.
Fixed Show fixed Hide fixed

fun count() = "count()"

Check warning

Code scanning / detekt

Public functions require documentation. Warning

The function count is missing documentation.

Check warning

Code scanning / detekt

A function that only returns a constant is misleading. Consider declaring a constant instead. Warning

count is returning a constant. Prefer declaring a constant instead.
Fixed Show fixed Hide fixed

fun max() = "max()"

Check warning

Code scanning / detekt

Public functions require documentation. Warning

The function max is missing documentation.

Check warning

Code scanning / detekt

A function that only returns a constant is misleading. Consider declaring a constant instead. Warning

max is returning a constant. Prefer declaring a constant instead.
Fixed Show fixed Hide fixed

fun min() = "min()"

Check warning

Code scanning / detekt

Public functions require documentation. Warning

The function min is missing documentation.

Check warning

Code scanning / detekt

A function that only returns a constant is misleading. Consider declaring a constant instead. Warning

min is returning a constant. Prefer declaring a constant instead.
Fixed Show fixed Hide fixed

fun sum() = "sum()"

Check warning

Code scanning / detekt

Public functions require documentation. Warning

The function sum is missing documentation.

Check warning

Code scanning / detekt

A function that only returns a constant is misleading. Consider declaring a constant instead. Warning

sum is returning a constant. Prefer declaring a constant instead.
Fixed Show fixed Hide fixed

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

Check warning

Code scanning / detekt

Public functions require documentation. Warning

The function build is missing documentation.

}

class ForeignColumnsBuilder(): BasicColumnsBuilder() {

Check warning

Code scanning / detekt

Public classes, interfaces and objects require documentation. Warning

ForeignColumnsBuilder is missing required documentation.

Check warning

Code scanning / detekt

Empty block of code detected. As they serve no purpose they should be removed. Warning

An empty default constructor can be removed.

var spread = false

Check warning

Code scanning / detekt

Public properties require documentation. Warning

The property spread is missing documentation.
var key: String? = null

Check warning

Code scanning / detekt

Public properties require documentation. Warning

The property key is missing documentation.

}

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("")
}
Loading