Skip to content

Commit

Permalink
Merge pull request #13 from ergon/feature/dope-209-delete-clause
Browse files Browse the repository at this point in the history
DOPE-209: added DeleteClause
  • Loading branch information
jansigi authored May 3, 2024
2 parents e44ddd1 + 90ef6a7 commit e5cd66e
Show file tree
Hide file tree
Showing 19 changed files with 396 additions and 148 deletions.
12 changes: 8 additions & 4 deletions core/src/main/kotlin/ch/ergon/dope/QueryBuilder.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package ch.ergon.dope

import ch.ergon.dope.resolvable.clause.FromClause
import ch.ergon.dope.resolvable.clause.SelectClause
import ch.ergon.dope.resolvable.clause.SelectDistinctClause
import ch.ergon.dope.resolvable.clause.SelectRawClause
import ch.ergon.dope.resolvable.clause.model.DeleteClause
import ch.ergon.dope.resolvable.clause.model.FromClause
import ch.ergon.dope.resolvable.clause.model.SelectClause
import ch.ergon.dope.resolvable.clause.model.SelectDistinctClause
import ch.ergon.dope.resolvable.clause.model.SelectRawClause
import ch.ergon.dope.resolvable.expression.AsteriskExpression
import ch.ergon.dope.resolvable.expression.Expression
import ch.ergon.dope.resolvable.expression.SingleExpression
import ch.ergon.dope.resolvable.fromable.Bucket
import ch.ergon.dope.resolvable.fromable.Fromable

class QueryBuilder {
Expand All @@ -20,4 +22,6 @@ class QueryBuilder {
fun selectRaw(expression: SingleExpression): SelectRawClause = SelectRawClause(expression)

fun selectFrom(fromable: Fromable): FromClause = SelectClause(AsteriskExpression()).from(fromable)

fun deleteFrom(bucket: Bucket): DeleteClause = DeleteClause(bucket)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package ch.ergon.dope.resolvable.clause

import ch.ergon.dope.resolvable.clause.model.DeleteLimitClause
import ch.ergon.dope.resolvable.clause.model.DeleteOffsetClause
import ch.ergon.dope.resolvable.clause.model.DeleteWhereClause
import ch.ergon.dope.resolvable.clause.model.ReturningClause
import ch.ergon.dope.resolvable.expression.TypeExpression
import ch.ergon.dope.resolvable.expression.unaliased.type.Field
import ch.ergon.dope.resolvable.expression.unaliased.type.toNumberType
import ch.ergon.dope.validtype.BooleanType
import ch.ergon.dope.validtype.NumberType
import ch.ergon.dope.validtype.ValidType

interface IDeleteReturningClause : Clause

interface IDeleteOffsetClass : IDeleteReturningClause {
fun returning(field: Field<out ValidType>, vararg fields: Field<out ValidType>) = ReturningClause(field, *fields, parentClause = this)
}

interface IDeleteLimitClass : IDeleteOffsetClass {
fun offset(numberExpression: TypeExpression<NumberType>) = DeleteOffsetClause(numberExpression, this)
fun offset(number: Number): DeleteOffsetClause = offset(number.toNumberType())
}

interface IDeleteWhereClause : IDeleteLimitClass {
fun limit(numberExpression: TypeExpression<NumberType>) = DeleteLimitClause(numberExpression, this)
fun limit(number: Number): DeleteLimitClause = limit(number.toNumberType())
}

interface IDeleteClause : IDeleteWhereClause {
fun where(booleanExpression: TypeExpression<BooleanType>) = DeleteWhereClause(booleanExpression, this)
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
package ch.ergon.dope.resolvable.clause

import ch.ergon.dope.resolvable.clause.model.FromClause
import ch.ergon.dope.resolvable.clause.model.GroupByClause
import ch.ergon.dope.resolvable.clause.model.InnerJoinClause
import ch.ergon.dope.resolvable.clause.model.LeftJoinClause
import ch.ergon.dope.resolvable.clause.model.OrderByType
import ch.ergon.dope.resolvable.clause.model.RightJoinClause
import ch.ergon.dope.resolvable.clause.model.SelectLimitClause
import ch.ergon.dope.resolvable.clause.model.SelectOffsetClause
import ch.ergon.dope.resolvable.clause.model.SelectOrderByClause
import ch.ergon.dope.resolvable.clause.model.SelectOrderByTypeClause
import ch.ergon.dope.resolvable.clause.model.SelectWhereClause
import ch.ergon.dope.resolvable.clause.model.StandardJoinClause
import ch.ergon.dope.resolvable.expression.TypeExpression
import ch.ergon.dope.resolvable.expression.unaliased.type.Field
import ch.ergon.dope.resolvable.expression.unaliased.type.toNumberType
Expand All @@ -11,32 +23,34 @@ import ch.ergon.dope.validtype.NumberType
import ch.ergon.dope.validtype.StringType
import ch.ergon.dope.validtype.ValidType

interface IOffsetClause : Clause
interface ISelectOffsetClause : Clause

interface ILimitClause : IOffsetClause {
fun offset(numberExpression: TypeExpression<NumberType>): OffsetClause = OffsetClause(numberExpression, this)
interface ISelectLimitClause : ISelectOffsetClause {
fun offset(numberExpression: TypeExpression<NumberType>): SelectOffsetClause = SelectOffsetClause(numberExpression, this)
fun offset(number: Number): SelectOffsetClause = offset(number.toNumberType())
}

interface IOrderByClause : ILimitClause {
fun limit(numberExpression: TypeExpression<NumberType>): LimitClause = LimitClause(numberExpression, this)
fun limit(number: Number): LimitClause = limit(number.toNumberType())
interface ISelectOrderByClause : ISelectLimitClause {
fun limit(numberExpression: TypeExpression<NumberType>): SelectLimitClause = SelectLimitClause(numberExpression, this)
fun limit(number: Number): SelectLimitClause = limit(number.toNumberType())
}

interface IGroupByClause : IOrderByClause {
fun orderBy(stringField: Field<StringType>): OrderByClause = OrderByClause(stringField, this)
fun orderBy(stringField: Field<StringType>, orderByType: OrderByType): OrderByTypeClause = OrderByTypeClause(stringField, orderByType, this)
interface ISelectGroupByClause : ISelectOrderByClause {
fun orderBy(stringField: Field<StringType>): SelectOrderByClause = SelectOrderByClause(stringField, this)
fun orderBy(stringField: Field<StringType>, orderByType: OrderByType): SelectOrderByTypeClause =
SelectOrderByTypeClause(stringField, orderByType, this)
}

interface IWhereClause : IGroupByClause {
interface ISelectWhereClause : ISelectGroupByClause {
fun groupBy(field: Field<out ValidType>, vararg fields: Field<out ValidType>): GroupByClause =
GroupByClause(field, *fields, parentClause = this)
}

interface IFromClause : IWhereClause {
fun where(whereExpression: TypeExpression<BooleanType>) = WhereClause(whereExpression, this)
interface ISelectFromClause : ISelectWhereClause {
fun where(whereExpression: TypeExpression<BooleanType>) = SelectWhereClause(whereExpression, this)
}

interface IJoinClause : IFromClause {
interface ISelectJoinClause : ISelectFromClause {
fun join(bucket: Bucket, onCondition: TypeExpression<BooleanType>) = StandardJoinClause(this, bucket, onCondition)
fun join(bucket: Bucket, onKeys: Field<out ValidType>) = StandardJoinClause(this, bucket, onKeys)

Expand All @@ -50,7 +64,7 @@ interface IJoinClause : IFromClause {
fun rightJoin(bucket: Bucket, onKeys: Field<out ValidType>) = RightJoinClause(this, bucket, onKeys)
}

interface ISelectClause : IFromClause {
interface ISelectClause : ISelectFromClause {
fun from(fromable: Fromable) = FromClause(fromable, this)

fun alias(alias: String) = AliasedSelectClause(alias, this)
Expand Down
53 changes: 0 additions & 53 deletions core/src/main/kotlin/ch/ergon/dope/resolvable/clause/JoinClause.kt

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package ch.ergon.dope.resolvable.clause.model

import ch.ergon.dope.resolvable.clause.IDeleteClause
import ch.ergon.dope.resolvable.fromable.Bucket

class DeleteClause(private val bucket: Bucket) : IDeleteClause {
override fun toQueryString(): String = "DELETE FROM ${bucket.toQueryString()}"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package ch.ergon.dope.resolvable.clause.model

import ch.ergon.dope.resolvable.clause.ISelectClause
import ch.ergon.dope.resolvable.clause.ISelectJoinClause
import ch.ergon.dope.resolvable.formatToQueryString
import ch.ergon.dope.resolvable.fromable.Fromable

class FromClause(private val fromable: Fromable, private val parentClause: ISelectClause) : ISelectJoinClause {
override fun toQueryString(): String = formatToQueryString(parentClause, "FROM", fromable)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package ch.ergon.dope.resolvable.clause.model

import ch.ergon.dope.resolvable.clause.ISelectGroupByClause
import ch.ergon.dope.resolvable.clause.ISelectWhereClause
import ch.ergon.dope.resolvable.expression.unaliased.type.Field
import ch.ergon.dope.resolvable.formatToQueryString
import ch.ergon.dope.validtype.ValidType

class GroupByClause(
private val field: Field<out ValidType>,
private vararg val fields: Field<out ValidType>,
private val parentClause: ISelectWhereClause,
) : ISelectGroupByClause {
override fun toQueryString(): String = formatToQueryString(parentClause, "GROUP BY", field, *fields)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package ch.ergon.dope.resolvable.clause.model

import ch.ergon.dope.resolvable.clause.ISelectFromClause
import ch.ergon.dope.resolvable.clause.ISelectJoinClause
import ch.ergon.dope.resolvable.expression.TypeExpression
import ch.ergon.dope.resolvable.expression.unaliased.type.Field
import ch.ergon.dope.resolvable.fromable.Bucket
import ch.ergon.dope.validtype.BooleanType
import ch.ergon.dope.validtype.ValidType

sealed class SelectJoinClause : ISelectJoinClause {
private val queryString: String

constructor(parentClause: ISelectFromClause, joinType: String, bucket: Bucket, onCondition: TypeExpression<BooleanType>) {
queryString = "${parentClause.toQueryString()} $joinType ${bucket.toQueryString()} ON ${onCondition.toQueryString()}"
}

constructor(parentClause: ISelectFromClause, joinType: String, bucket: Bucket, key: Field<out ValidType>) {
queryString = "${parentClause.toQueryString()} $joinType ${bucket.toQueryString()} ON KEYS ${key.toQueryString()}"
}

override fun toQueryString(): String = queryString
}

class StandardJoinClause : SelectJoinClause {
constructor(parentClause: ISelectFromClause, bucket: Bucket, onCondition: TypeExpression<BooleanType>) :
super(parentClause, "JOIN", bucket, onCondition)

constructor(parentClause: ISelectFromClause, bucket: Bucket, onKeys: Field<out ValidType>) :
super(parentClause, "JOIN", bucket, onKeys)
}

class LeftJoinClause : SelectJoinClause {
constructor(parentClause: ISelectFromClause, bucket: Bucket, onCondition: TypeExpression<BooleanType>) :
super(parentClause, "LEFT JOIN", bucket, onCondition)

constructor(parentClause: ISelectFromClause, bucket: Bucket, onKeys: Field<out ValidType>) :
super(parentClause, "LEFT JOIN", bucket, onKeys)
}

class InnerJoinClause : SelectJoinClause {
constructor(parentClause: ISelectFromClause, bucket: Bucket, onCondition: TypeExpression<BooleanType>) :
super(parentClause, "INNER JOIN", bucket, onCondition)

constructor(parentClause: ISelectFromClause, bucket: Bucket, onKeys: Field<out ValidType>) :
super(parentClause, "INNER JOIN", bucket, onKeys)
}

class RightJoinClause : SelectJoinClause {
constructor(parentClause: ISelectFromClause, bucket: Bucket, onCondition: TypeExpression<BooleanType>) :
super(parentClause, "RIGHT JOIN", bucket, onCondition)

constructor(parentClause: ISelectFromClause, bucket: Bucket, onKeys: Field<out ValidType>) :
super(parentClause, "RIGHT JOIN", bucket, onKeys)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package ch.ergon.dope.resolvable.clause.model

import ch.ergon.dope.resolvable.clause.IDeleteLimitClass
import ch.ergon.dope.resolvable.clause.IDeleteWhereClause
import ch.ergon.dope.resolvable.clause.ISelectLimitClause
import ch.ergon.dope.resolvable.clause.ISelectOrderByClause
import ch.ergon.dope.resolvable.expression.TypeExpression
import ch.ergon.dope.resolvable.formatToQueryString
import ch.ergon.dope.validtype.NumberType

private const val LIMIT = "LIMIT"

class SelectLimitClause(private val numberExpression: TypeExpression<NumberType>, private val parentClause: ISelectOrderByClause) :
ISelectLimitClause {
override fun toQueryString(): String = formatToQueryString(parentClause, LIMIT, numberExpression)
}

class DeleteLimitClause(private val numberExpression: TypeExpression<NumberType>, private val parentClause: IDeleteWhereClause) :
IDeleteLimitClass {
override fun toQueryString(): String = formatToQueryString(parentClause, LIMIT, numberExpression)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package ch.ergon.dope.resolvable.clause.model

import ch.ergon.dope.resolvable.clause.IDeleteLimitClass
import ch.ergon.dope.resolvable.clause.IDeleteOffsetClass
import ch.ergon.dope.resolvable.clause.ISelectLimitClause
import ch.ergon.dope.resolvable.clause.ISelectOffsetClause
import ch.ergon.dope.resolvable.expression.TypeExpression
import ch.ergon.dope.resolvable.formatToQueryString
import ch.ergon.dope.validtype.NumberType

private const val OFFSET = "OFFSET"

class SelectOffsetClause(private val numberExpression: TypeExpression<NumberType>, private val parentClause: ISelectLimitClause) :
ISelectOffsetClause {
override fun toQueryString(): String = formatToQueryString(parentClause, OFFSET, numberExpression)
}

class DeleteOffsetClause(private val numberExpression: TypeExpression<NumberType>, private val parentClause: IDeleteLimitClass) :
IDeleteOffsetClass {
override fun toQueryString(): String = formatToQueryString(parentClause, OFFSET, numberExpression)
}
Loading

0 comments on commit e5cd66e

Please sign in to comment.