Skip to content

Instantly share code, notes, and snippets.

@GibsonRuitiari
Created February 2, 2025 17:28
Show Gist options
  • Save GibsonRuitiari/a938f33b891a6f1e78e84765dc3b20c4 to your computer and use it in GitHub Desktop.
Save GibsonRuitiari/a938f33b891a6f1e78e84765dc3b20c4 to your computer and use it in GitHub Desktop.
sealed class SqlStatement
// SELECT statement
data class Select(
val selectList: SelectList,
val tableReference: TableReference,
val whereCondition: Condition? = null,
val groupBy: ColumnList? = null,
val orderBy: OrderList? = null,
val orderByOption: OrderByOption? = null,
val limit: Int? = null
) : SqlStatement()
// INSERT statement
data class Insert(
val table: Identifier,
val columns: ColumnList,
val values: ValueList
) : SqlStatement()
// UPDATE statement
data class Update(
val table: Identifier,
val assignments: List<ColumnAssignment>,
val whereCondition: Condition? = null
) : SqlStatement()
// DELETE statement
data class Delete(
val table: Identifier,
val whereCondition: Condition? = null
) : SqlStatement()
// TABLE REFERENCES (supports JOINs)
data class TableReference(
val table: Identifier,
val join: Join? = null
)
// JOIN structure
data class Join(
val table: Identifier,
val condition: Condition
)
// SELECT LIST (* or column list)
sealed class SelectList
object SelectAll : SelectList()
data class SelectColumns(val columns: ColumnList) : SelectList()
// COLUMN LIST
data class ColumnList(val columns: List<Identifier>)
// ORDER BY Options
enum class OrderByOption { ASC, DESC }
data class OrderList(val columns: List<Identifier>)
// COLUMN ASSIGNMENT (for UPDATE)
data class ColumnAssignment(val column: Identifier, val value: Value)
// CONDITIONS (WHERE Clause)
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()
// VALUE LIST (for INSERT)
data class ValueList(val values: List<Value>)
// VALUE (can be literal or expression)
sealed class Value
data class SqlLiteral(val value: String) : Value() // 'John', 12, NULL
data class ExpressionValue(val expression: Expression) : Value()
// EXPRESSION (identifiers, literals, functions, and operations)
sealed class Expression
data class Identifier(val name: String) : Expression() // Column or table names
data class FunctionCall(val name: String, val arguments: List<Expression>) : Expression()
data class BinaryExpression(
val left: Expression,
val operator: Operator,
val right: Expression
) : Expression()
// SQL OPERATORS
sealed class Operator
enum class ComparisonOperator { EQUALS, NOT_EQUALS, GREATER, LESS, GREATER_EQUALS, LESS_EQUALS }
enum class LogicalOperator { AND, OR, NOT }
enum class ArithmeticOperator { ADD, MULTIPLY, DIVIDE }
// SQL LITERAL TYPES
sealed class SqlLiteralType
data class StringLiteral(val value: String) : SqlLiteralType() // "John"
data class NumberLiteral(val value: Double) : SqlLiteralType() // 123.45
object NullLiteral : SqlLiteralType() // NULL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment