All Downloads are FREE. Search and download functionalities are using the official Maven repository.

app.cash.sqldelight.dialects.postgresql.grammar.mixins.AlterTableRenameColumnMixin.kt Maven / Gradle / Ivy

There is a newer version: 2.0.2
Show newest version
package app.cash.sqldelight.dialects.postgresql.grammar.mixins

import app.cash.sqldelight.dialects.postgresql.grammar.psi.PostgreSqlAlterTableRenameColumn
import com.alecstrong.sql.psi.core.SqlAnnotationHolder
import com.alecstrong.sql.psi.core.psi.AlterTableApplier
import com.alecstrong.sql.psi.core.psi.LazyQuery
import com.alecstrong.sql.psi.core.psi.NamedElement
import com.alecstrong.sql.psi.core.psi.QueryElement
import com.alecstrong.sql.psi.core.psi.SqlColumnAlias
import com.alecstrong.sql.psi.core.psi.SqlColumnName
import com.alecstrong.sql.psi.core.psi.SqlCompositeElementImpl
import com.alecstrong.sql.psi.core.psi.alterStmt
import com.intellij.lang.ASTNode

internal abstract class AlterTableRenameColumnMixin(
  node: ASTNode,
) : SqlCompositeElementImpl(node),
  PostgreSqlAlterTableRenameColumn,
  AlterTableApplier {
  private val columnName
    get() = children.filterIsInstance().single()

  private val columnAlias
    get() = children.filterIsInstance().single()

  override fun applyTo(lazyQuery: LazyQuery): LazyQuery {
    return LazyQuery(
      tableName = lazyQuery.tableName,
      query = {
        val columns = lazyQuery.query.columns
        val column = QueryElement.QueryColumn(element = columnAlias)
        val replace = columns.singleOrNull {
          (it.element as NamedElement).textMatches(columnName)
        }
        lazyQuery.query.copy(
          columns = lazyQuery.query.columns.map { if (it == replace) column else it },
        )
      },
    )
  }

  override fun annotate(annotationHolder: SqlAnnotationHolder) {
    super.annotate(annotationHolder)

    if (tablesAvailable(this)
        .filter { it.tableName.textMatches(alterStmt.tableName) }
        .flatMap { it.query.columns }
        .none { (it.element as? SqlColumnName)?.textMatches(columnName) == true }
    ) {
      annotationHolder.createErrorAnnotation(
        element = columnName,
        message = "No column found to modify with name ${columnName.text}",
      )
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy