Skip to content

Commit

Permalink
Refactor applyTo
Browse files Browse the repository at this point in the history
The QueryColumn nullable property can be null, true or false
We can always assign queryColumn.copy(nullable = nullableColumn) when the column matches
  • Loading branch information
griffio committed Nov 25, 2023
1 parent 4dccbf5 commit 2490329
Showing 1 changed file with 9 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,20 @@ internal abstract class AlterTableAlterColumnMixin(
get() = children.filterIsInstance<SqlColumnName>().first()

override fun applyTo(lazyQuery: LazyQuery): LazyQuery {
val hasDropNotNull = columnNotNullClause?.let { it.firstChild.elementType == SqlTypes.DROP }
val hasSetNotNull = columnNotNullClause?.let { it.firstChild.elementType == SqlTypes.SET }
val nullableColumn: Boolean? = columnNotNullClause?.let {
when (it.firstChild.elementType) {
SqlTypes.DROP -> true
SqlTypes.SET -> false
else -> null
}
}

return LazyQuery(
tableName = lazyQuery.tableName,
query = {
val columns = lazyQuery.query.columns.map {
if (it.element.textMatches(columnName)) {
when {
(hasDropNotNull == true) -> it.copy(nullable = true)
(hasSetNotNull == true) -> it.copy(nullable = false)
else -> it
}
} else {
it
}
val columns = lazyQuery.query.columns.map { queryColumn ->
if (queryColumn.element.textMatches(columnName)) queryColumn.copy(nullable = nullableColumn) else queryColumn
}

lazyQuery.query.copy(columns = columns)
},
)
Expand Down

0 comments on commit 2490329

Please sign in to comment.