Created
February 15, 2025 15:34
-
-
Save GibsonRuitiari/5b2b756415fd805a284ad22dafde5f79 to your computer and use it in GitHub Desktop.
Complete Kotlin-classes representation of our SQL Grammar.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sealed class SqlStatement | |
// Select statement | |
data class Select( | |
val selectList: SelectList, | |
val from: TableReference, | |
val where: Condition? = null, | |
val groupBy: GroupBy? = null, | |
val orderBy: OrderBy? = null, | |
val limit: Limit? = null | |
) : SqlStatement() | |
// Insert statement | |
data class Insert( | |
val table: String, | |
val columns: List<String>, | |
val values: List<Expression> | |
) : SqlStatement() | |
// Update statement | |
data class Update( | |
val table: String, | |
val assignments: List<ColumnAssignment>, | |
val where: Condition? = null | |
) : SqlStatement() | |
// Delete statement | |
data class Delete( | |
val table: String, | |
val where: Condition? = null | |
) : SqlStatement() | |
// SELECT LIST | |
sealed class SelectList | |
object AllColumns : SelectList() | |
data class ColumnList(val columns: List<String>) : SelectList() | |
// TABLE REFERENCE | |
data class TableReference( | |
val table: String, | |
val join: JoinClause? = null | |
) | |
data class JoinClause( | |
val table: String, | |
val on: Condition | |
) | |
// CONDITIONS | |
sealed class Condition | |
data class ComparisonCondition( | |
val left: Expression, | |
val operator: ComparisonOperator, | |
val right: Expression | |
) : Condition() | |
data class LogicalCondition( | |
val left: Condition, | |
val operator: LogicalOperator, | |
val right: Condition | |
) : Condition() | |
data class ParenthesizedCondition(val condition: Condition) : Condition() | |
// EXPRESSIONS | |
sealed class Expression | |
data class IdentifierExpression(val identifier: String) : Expression() | |
data class LiteralExpression(val literal: SqlLiteral) : Expression() | |
data class FunctionCallExpression( | |
val functionName: String, | |
val arguments: List<Expression> | |
) : Expression() | |
data class BinaryOperation( | |
val left: Expression, | |
val operator: Operator, | |
val right: Expression | |
) : Expression() | |
// LITERALS | |
sealed class SqlLiteral | |
data class StringLiteral(val value: String) : SqlLiteral() | |
data class NumberLiteral( | |
val number: Double) : SqlLiteral() | |
object NullLiteral : SqlLiteral() | |
// CLAUSES | |
data class ColumnAssignment( | |
val column: String, | |
val value: Expression | |
) | |
data class OrderBy(val terms: List<OrderTerm>) | |
data class OrderTerm( | |
val column: String, | |
val direction: OrderDirection? = null | |
) | |
data class GroupBy(val columns: List<String>) | |
data class Limit(val count: NumberLiteral) | |
// ENUMS | |
enum class ComparisonOperator { | |
EQUAL, NOT_EQUAL, GREATER, LESS, GREATER_EQUAL, LESS_EQUAL | |
} | |
enum class LogicalOperator { | |
AND, OR, NOT | |
} | |
enum class ArithmeticOperator { | |
PLUS, MULTIPLY, DIVIDE | |
} | |
enum class OrderDirection { ASC, DESC } | |
// OPERATOR (combined) | |
sealed class Operator { | |
data class Comparison(val op: ComparisonOperator) : Operator() | |
data class Logical(val op: LogicalOperator) : Operator() | |
data class Arithmetic(val op: ArithmeticOperator) : Operator() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment