Skip to content

Commit

Permalink
Merge pull request #18 from ergon/feature/dope-214-add-backticks-to-b…
Browse files Browse the repository at this point in the history
…uckets-and-params

Feature/dope 214 add backticks to buckets and params
  • Loading branch information
jansigi authored May 24, 2024
2 parents db9504d + ebefa5a commit e2bc2fb
Show file tree
Hide file tree
Showing 21 changed files with 371 additions and 351 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fun formatToQueryStringWithBrackets(symbol: String, vararg argument: String) =

fun formatPathToQueryString(name: String, path: String) =
if (path.isBlank()) {
name
"`$name`"
} else {
"$path.$name"
"`$path`.`$name`"
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class AliasedExpression<T : ValidType>(
override fun toDopeQuery(): DopeQuery {
val unaliasedExpressionDopeQuery = unaliasedExpression.toDopeQuery()
return DopeQuery(
queryString = formatToQueryStringWithSymbol(unaliasedExpressionDopeQuery.queryString, "AS", alias),
queryString = formatToQueryStringWithSymbol(unaliasedExpressionDopeQuery.queryString, "AS", "`$alias`"),
parameters = unaliasedExpressionDopeQuery.parameters,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class AsteriskExpression : Expression {

constructor(bucket: Bucket) {
queryString = when (bucket) {
is AliasedBucket -> bucket.alias
is UnaliasedBucket -> bucket.name
is AliasedBucket -> "`${bucket.alias}`"
is UnaliasedBucket -> "`${bucket.name}`"
} + ".$ASTERISK_STRING"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import ch.ergon.dope.resolvable.expression.TypeExpression
import ch.ergon.dope.resolvable.formatPathToQueryString
import ch.ergon.dope.validtype.ValidType

class Field<T : ValidType>(private val name: String, private val path: String) : TypeExpression<T> {
open class Field<T : ValidType>(private val name: String, private val path: String) : TypeExpression<T> {
override fun toDopeQuery(): DopeQuery = DopeQuery(
queryString = formatPathToQueryString(name, path),
parameters = emptyMap(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,47 @@ import ch.ergon.dope.resolvable.expression.TypeExpression
import ch.ergon.dope.resolvable.expression.unaliased.type.Field
import ch.ergon.dope.resolvable.fromable.AliasedBucket
import ch.ergon.dope.resolvable.fromable.Bucket
import ch.ergon.dope.resolvable.fromable.UnaliasedBucket
import ch.ergon.dope.resolvable.operator.FunctionOperator
import ch.ergon.dope.validtype.NumberType
import ch.ergon.dope.validtype.StringType
import ch.ergon.dope.validtype.ValidType

class MetaExpression(private val bucket: Bucket) : TypeExpression<StringType> {
class MetaExpression(private val bucket: Bucket) : TypeExpression<StringType>, FunctionOperator {
override fun toDopeQuery(): DopeQuery {
val bucketDopeQuery = bucket.toDopeQuery()
return DopeQuery(
queryString = "META(${
when (bucket) {
is AliasedBucket -> bucket.alias
else -> bucketDopeQuery.queryString
}
})",
queryString = toFunctionQueryString(
symbol = "META",
when (bucket) {
is AliasedBucket -> "`${bucket.alias}`"
is UnaliasedBucket -> bucketDopeQuery.queryString
},
),
parameters = bucketDopeQuery.parameters,
)
}

val cas: Field<NumberType> = Field("cas", toDopeQuery().queryString)
val cas: Field<NumberType> = getMetaField("cas")

val expiration: Field<NumberType> = Field("expiration", toDopeQuery().queryString)
val expiration: Field<NumberType> = getMetaField("expiration")

val flags: Field<NumberType> = Field("flags", toDopeQuery().queryString)
val flags: Field<NumberType> = getMetaField("flags")

val id: Field<StringType> = Field("id", toDopeQuery().queryString)
val id: Field<StringType> = getMetaField("id")

val type: Field<StringType> = Field("type", toDopeQuery().queryString)
val type: Field<StringType> = getMetaField("type")

val keyspace: Field<StringType> = Field("keyspace", toDopeQuery().queryString)
val keyspace: Field<StringType> = getMetaField("keyspace")

private fun <T : ValidType> getMetaField(field: String): MetaField<T> = MetaField(field, toDopeQuery())

private class MetaField<T : ValidType>(private val name: String, private val dopeQuery: DopeQuery) : Field<T>(name, "") {
override fun toDopeQuery(): DopeQuery = DopeQuery(
queryString = "${dopeQuery.queryString}.`$name`",
parameters = dopeQuery.parameters,
)
}
}

fun meta(bucket: Bucket) = MetaExpression(bucket)
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class AliasedSelectClause(private val alias: String, private val selectClause: I
override fun toDopeQuery(): DopeQuery {
val selectClauseDopeQuery = selectClause.toDopeQuery()
return DopeQuery(
queryString = "(${selectClauseDopeQuery.queryString}) AS $alias",
queryString = "(${selectClauseDopeQuery.queryString}) AS `$alias`",
parameters = selectClauseDopeQuery.parameters,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import ch.ergon.dope.DopeQuery
import ch.ergon.dope.resolvable.expression.AsteriskExpression

sealed class Bucket(open val name: String) : Fromable {
override fun toDopeQuery(): DopeQuery = DopeQuery(name, emptyMap())
override fun toDopeQuery(): DopeQuery = DopeQuery("`$name`", emptyMap())
}

open class UnaliasedBucket(name: String) : Bucket(name) {
Expand All @@ -13,7 +13,7 @@ open class UnaliasedBucket(name: String) : Bucket(name) {

class AliasedBucket(name: String, val alias: String) : Bucket(name) {
override fun toDopeQuery(): DopeQuery = DopeQuery(
queryString = "$name AS $alias",
queryString = "`$name` AS `$alias`",
parameters = emptyMap(),
)
}
Expand Down
30 changes: 15 additions & 15 deletions core/src/test/kotlin/ch/ergon/dope/ArrayTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class ArrayTest {
@Test
fun `should support arrays`() {
val person = someBucket("person")
val expected = "SELECT [person.fname, stringField] FROM person"
val expected = "SELECT [`person`.`fname`, `stringField`] FROM `person`"

val actual: String = create
.select(
Expand All @@ -43,7 +43,7 @@ class ArrayTest {
@Test
fun `should support multiple arrays`() {
val person = someBucket("person")
val expected = "SELECT [stringField], [stringField] FROM person"
val expected = "SELECT [`stringField`], [`stringField`] FROM `person`"

val actual: String = create
.select(
Expand All @@ -59,7 +59,7 @@ class ArrayTest {
@Test
fun `should support arrays with multiple types`() {
val person = someBucket("person")
val expected = "SELECT [\"test\", 53, TRUE, stringField, person.age, booleanField] FROM person"
val expected = "SELECT [\"test\", 53, TRUE, `stringField`, `person`.`age`, `booleanField`] FROM `person`"

val actual: String = create
.select(
Expand All @@ -81,7 +81,7 @@ class ArrayTest {
@Test
fun `should support array aliased`() {
val person = someBucket("person")
val expected = "SELECT [stringField] AS test FROM person"
val expected = "SELECT [`stringField`] AS `test` FROM `person`"

val actual: String = create
.select(
Expand All @@ -96,7 +96,7 @@ class ArrayTest {
@Test
fun `should support multiple arrays aliased`() {
val person = someBucket("person")
val expected = "SELECT [stringField] AS fname, [stringField] AS true FROM person"
val expected = "SELECT [`stringField`] AS `fname`, [`stringField`] AS `true` FROM `person`"

val actual: String = create
.select(
Expand All @@ -112,7 +112,7 @@ class ArrayTest {
@Test
fun `should support nested arrays`() {
val person = someBucket("person")
val expected = "SELECT [stringField, [person.age, TRUE, \"string\"], 23] AS test FROM person"
val expected = "SELECT [`stringField`, [`person`.`age`, TRUE, \"string\"], 23] AS `test` FROM `person`"

val actual: String = create
.select(
Expand All @@ -135,7 +135,7 @@ class ArrayTest {
@Test
fun `should support string arrays`() {
val person = someBucket("person")
val expected = "SELECT [\"string\", \"hallo\"] FROM person"
val expected = "SELECT [\"string\", \"hallo\"] FROM `person`"

val actual: String = create
.select(
Expand All @@ -153,7 +153,7 @@ class ArrayTest {
@Test
fun `should support stringFunction in string arrays`() {
val person = someBucket("person")
val expected = "SELECT [CONCAT(\"string\", stringField), \"hallo\"] AS test, 23 FROM person"
val expected = "SELECT [CONCAT(\"string\", `stringField`), \"hallo\"] AS `test`, 23 FROM `person`"

val actual: String = create
.select(
Expand All @@ -172,7 +172,7 @@ class ArrayTest {
@Test
fun `should support in array`() {
val person = someBucket("person")
val expected = "SELECT TRUE IN [FALSE] FROM person"
val expected = "SELECT TRUE IN [FALSE] FROM `person`"

val actual: String = create
.select(
Expand All @@ -191,7 +191,7 @@ class ArrayTest {
@Test
fun `should support in array aliased`() {
val person = someBucket("person")
val expected = "SELECT TRUE IN [FALSE] AS test FROM person"
val expected = "SELECT TRUE IN [FALSE] AS `test` FROM `person`"

val actual: String = create
.select(
Expand All @@ -209,7 +209,7 @@ class ArrayTest {

@Test
fun `should support in array with boolean and collection`() {
val expected = "SELECT TRUE IN [FALSE, TRUE] AS test FROM person"
val expected = "SELECT TRUE IN [FALSE, TRUE] AS `test` FROM `person`"

val actual: String = create
.select(
Expand All @@ -228,7 +228,7 @@ class ArrayTest {

@Test
fun `should support in array with string and collection`() {
val expected = "SELECT \"test\" IN [stringField, \"string\"] AS test FROM person"
val expected = "SELECT \"test\" IN [`stringField`, \"string\"] AS `test` FROM `person`"

val actual: String = create
.select(
Expand All @@ -247,7 +247,7 @@ class ArrayTest {

@Test
fun `should support in array with number and collection`() {
val expected = "SELECT 3 IN [numberField, 23] AS test FROM someBucket"
val expected = "SELECT 3 IN [`numberField`, 23] AS `test` FROM `someBucket`"

val actual: String = create
.select(
Expand All @@ -266,7 +266,7 @@ class ArrayTest {

@Test
fun `should support in array with field and collection`() {
val expected = "SELECT numberField IN [23] AS test FROM person"
val expected = "SELECT `numberField` IN [23] AS `test` FROM `person`"

val actual: String = create
.select(
Expand All @@ -285,7 +285,7 @@ class ArrayTest {
@Test
fun `should support in array as whereClause`() {
val person = someBucket("person")
val expected = "SELECT * FROM person WHERE stringField IN [\"string\", \"hallo\"]"
val expected = "SELECT * FROM `person` WHERE `stringField` IN [\"string\", \"hallo\"]"

val actual: String = create
.selectFrom(
Expand Down
Loading

0 comments on commit e2bc2fb

Please sign in to comment.