<commit_before>package com.squareup.sqldelight.core.compiler

import com.alecstrong.sql.psi.core.psi.SqlDeleteStmtLimited
import com.alecstrong.sql.psi.core.psi.SqlInsertStmt
import com.alecstrong.sql.psi.core.psi.SqlUpdateStmtLimited
import com.intellij.psi.util.PsiTreeUtil
import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.FunSpec
import com.squareup.kotlinpoet.KModifier
import com.squareup.kotlinpoet.ParameterSpec
import com.squareup.kotlinpoet.PropertySpec
import com.squareup.kotlinpoet.joinToCode
import com.squareup.sqldelight.core.compiler.model.NamedExecute
import com.squareup.sqldelight.core.compiler.model.NamedMutator
import com.squareup.sqldelight.core.compiler.model.NamedQuery
import com.squareup.sqldelight.core.lang.psi.StmtIdentifierMixin
import com.squareup.sqldelight.core.psi.SqlDelightStmtClojureStmtList

open class ExecuteQueryGenerator(private val query: NamedExecute) : QueryGenerator(query) {
  internal open fun queriesUpdated(): List<NamedQuery> {
    if (query.statement is SqlDelightStmtClojureStmtList) {
      return PsiTreeUtil.findChildrenOfAnyType(
        query.statement,
        SqlUpdateStmtLimited::class.java,
        SqlDeleteStmtLimited::class.java,
        SqlInsertStmt::class.java
      ).flatMap {
        MutatorQueryGenerator(
          when (it) {
            is SqlUpdateStmtLimited -> NamedMutator.Update(it, query.identifier as StmtIdentifierMixin)
            is SqlDeleteStmtLimited -> NamedMutator.Delete(it, query.identifier as StmtIdentifierMixin)
            is SqlInsertStmt -> NamedMutator.Insert(it, query.identifier as StmtIdentifierMixin)
            else -> throw IllegalArgumentException("Unexpected statement $it")
          }
        ).queriesUpdated()
      }.distinct()
    }
    return emptyList()
  }

  private fun FunSpec.Builder.notifyQueries(): FunSpec.Builder {
    val resultSetsUpdated = queriesUpdated()

    if (resultSetsUpdated.isEmpty()) return this

    // The list of effected queries:
    // (queryWrapper.dataQueries.selectForId + queryWrapper.otherQueries.selectForId)
    // TODO: Only notify queries that were dirtied (check using dirtied method).
    addStatement(
      "notifyQueries(%L, {%L})",
      query.id,
      resultSetsUpdated.map { it.queryProperty }.joinToCode(separator = " + ")
    )

    return this
  }

  /**
   * The public api to execute [query]
   */
  fun function(): FunSpec {
    return interfaceFunction()
      .addModifiers(KModifier.OVERRIDE)
      .addCode(executeBlock())
      .notifyQueries()
      .build()
  }

  fun interfaceFunction(): FunSpec.Builder {
    return FunSpec.builder(query.name)
      .also(this::addJavadoc)
      .addParameters(
        query.parameters.map {
          ParameterSpec.builder(it.name, it.argumentType()).build()
        }
      )
  }
<commit_msg>sort (#2349)<commit_after>package com.squareup.sqldelight.core.compiler

import com.alecstrong.sql.psi.core.psi.SqlDeleteStmtLimited
import com.alecstrong.sql.psi.core.psi.SqlInsertStmt
import com.alecstrong.sql.psi.core.psi.SqlUpdateStmtLimited
import com.intellij.psi.util.PsiTreeUtil
import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.FunSpec
import com.squareup.kotlinpoet.KModifier
import com.squareup.kotlinpoet.ParameterSpec
import com.squareup.kotlinpoet.PropertySpec
import com.squareup.kotlinpoet.joinToCode
import com.squareup.sqldelight.core.compiler.model.NamedExecute
import com.squareup.sqldelight.core.compiler.model.NamedMutator
import com.squareup.sqldelight.core.compiler.model.NamedQuery
import com.squareup.sqldelight.core.lang.psi.StmtIdentifierMixin
import com.squareup.sqldelight.core.psi.SqlDelightStmtClojureStmtList

open class ExecuteQueryGenerator(private val query: NamedExecute) : QueryGenerator(query) {
  internal open fun queriesUpdated(): List<NamedQuery> {
    if (query.statement is SqlDelightStmtClojureStmtList) {
      return PsiTreeUtil.findChildrenOfAnyType(
        query.statement,
        SqlUpdateStmtLimited::class.java,
        SqlDeleteStmtLimited::class.java,
        SqlInsertStmt::class.java
      ).flatMap {
        MutatorQueryGenerator(
          when (it) {
            is SqlUpdateStmtLimited -> NamedMutator.Update(it, query.identifier as StmtIdentifierMixin)
            is SqlDeleteStmtLimited -> NamedMutator.Delete(it, query.identifier as StmtIdentifierMixin)
            is SqlInsertStmt -> NamedMutator.Insert(it, query.identifier as StmtIdentifierMixin)
            else -> throw IllegalArgumentException("Unexpected statement $it")
          }
        ).queriesUpdated()
      }.distinct()
    }
    return emptyList()
  }

  private fun FunSpec.Builder.notifyQueries(): FunSpec.Builder {
    val resultSetsUpdated = queriesUpdated()

    if (resultSetsUpdated.isEmpty()) return this

    // The list of effected queries:
    // (queryWrapper.dataQueries.selectForId + queryWrapper.otherQueries.selectForId)
    // TODO: Only notify queries that were dirtied (check using dirtied method).
    addStatement(
      "notifyQueries(%L, {%L})",
      query.id,
      resultSetsUpdated.sortedBy { it.id }.map { it.queryProperty }.joinToCode(separator = " + ")
    )

    return this
  }

  /**
   * The public api to execute [query]
   */
  fun function(): FunSpec {
    return interfaceFunction()
      .addModifiers(KModifier.OVERRIDE)
      .addCode(executeBlock())
      .notifyQueries()
      .build()
  }

  fun interfaceFunction(): FunSpec.Builder {
    return FunSpec.builder(query.name)
      .also(this::addJavadoc)
      .addParameters(
        query.parameters.map {
          ParameterSpec.builder(it.name, it.argumentType()).build()
        }
      )
  }

{'fraction_non_alphanumeric': 0.09904996481351161, 'fraction_numerical': 0.0007037297677691766, 'mean_word_length': 3.580983078162772, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1896405', 'n_tokens_mistral': 1739, 'n_tokens_neox': 1726, 'n_words': 299}