Skip to content

Commit

Permalink
fix: long function names (#27)
Browse files Browse the repository at this point in the history
Close #26
  • Loading branch information
tamimattafi authored Jun 4, 2024
1 parent 9b2cc56 commit e89e894
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import org.gradle.api.publish.maven.MavenPomScm

class PublishConventions : Plugin<Project> {

private val version = "0.1.0-alpha11"
private val version = "0.1.0-alpha12"
private val group = "com.attafitamim.kabin"

override fun apply(project: Project) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ private const val CYCLES_START_INDEX = 0
private const val HASH_CODE_INTERVAL = 31

fun SQLQuery.getQueryIdentifier(): Int? = when (this) {
is SQLQuery.Columns -> value.getUniqueQueryIdentifier()
is SQLQuery.Parameters -> value.getUniqueQueryIdentifier()
is SQLQuery.Columns -> value.getUniqueHashCode()
is SQLQuery.Parameters -> value.getUniqueHashCode()
is SQLQuery.Raw -> null
}

// TODO: optimize this
fun String.getUniqueQueryIdentifier(): Int? {
fun String.getUniqueHashCode(): Int? {
val generatedId = generatedIds[this]
if (generatedId != null) {
return generatedId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.attafitamim.kabin.compiler.sql.syntax.SQLQuery
import com.attafitamim.kabin.compiler.sql.utils.poet.dao.getColumnAccessChain
import com.attafitamim.kabin.compiler.sql.utils.poet.simpleNameString
import com.attafitamim.kabin.compiler.sql.utils.poet.sqldelight.getQueryIdentifier
import com.attafitamim.kabin.compiler.sql.utils.poet.sqldelight.getUniqueHashCode
import com.attafitamim.kabin.compiler.sql.utils.poet.toPascalCase
import com.attafitamim.kabin.processor.ksp.options.KabinOptions
import com.attafitamim.kabin.specs.column.ColumnSpec
Expand Down Expand Up @@ -89,7 +90,7 @@ private fun EntitySpec.getQueryByColumnsName(
isNullable: Boolean,
parent: CompoundSpec?
): String {
val id = query.getQueryIdentifier()?.toHexString()
val id = query.getQueryIdentifier()
return if (query.columns.isEmpty()) {
declaration.getQueryByNoParametersName(isNullable, parent, id)
} else declaration.buildQueryFunctionName(isNullable, parent, id) {
Expand Down Expand Up @@ -175,7 +176,7 @@ fun KSClassDeclaration.getQueryByParametersName(
isNullable: Boolean,
parent: CompoundSpec?
): String {
val id = query.getQueryIdentifier()?.toHexString()
val id = query.getQueryIdentifier()
return if (query.queryParameters.isEmpty()) {
getQueryByNoParametersName(isNullable, parent, id)
} else buildQueryFunctionName(isNullable, parent, id) {
Expand All @@ -192,16 +193,14 @@ fun KSClassDeclaration.getQueryByParametersName(
fun KSClassDeclaration.getQueryByNoParametersName(
isNullable: Boolean,
parent: CompoundSpec?,
postFix: String? = null,
): String = buildQueryFunctionName(isNullable, parent, postFix) {
append("NoParameters")
}
id: Int? = null,
): String = buildQueryFunctionName(isNullable, parent, id)

fun KSClassDeclaration.buildQueryFunctionName(
isNullable: Boolean = false,
parent: CompoundSpec?,
postFix: String? = null,
builder: StringBuilder.() -> Unit
id: Int? = null,
builder: StringBuilder.() -> Unit = {}
): String = buildString {
append("query")

Expand All @@ -216,11 +215,16 @@ fun KSClassDeclaration.buildQueryFunctionName(
append(parent.declaration.simpleNameString.toPascalCase())
}

append("By")
builder()
if (id != null) {
append(id.toHexString().uppercase())
}

if (!postFix.isNullOrBlank()) {
append(postFix.toPascalCase())
val parameters = StringBuilder().apply(builder).toString()
if (parameters.isNotBlank()) {
val uniqueHashCode = parameters.getUniqueHashCode()
if (uniqueHashCode != null) {
append("ByParameters", uniqueHashCode.toHexString().uppercase())
}
}
}

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.attafitamim.kabin.compiler

Check notice on line 1 in library/compiler/src/jvmTest/kotlin/com/attafitamim/kabin/compiler/TestQueryIdentifiers.kt

View workflow job for this annotation

GitHub Actions / JUnit Test Report

TestQueryIdentifiers.unique identifier for fixed query[jvm]

unique identifier for fixed query[jvm]

Check notice on line 1 in library/compiler/src/jvmTest/kotlin/com/attafitamim/kabin/compiler/TestQueryIdentifiers.kt

View workflow job for this annotation

GitHub Actions / JUnit Test Report

TestQueryIdentifiers.unique identifiers for random queries[jvm]

unique identifiers for random queries[jvm]

import com.attafitamim.kabin.compiler.sql.utils.poet.sqldelight.getUniqueQueryIdentifier
import com.attafitamim.kabin.compiler.sql.utils.poet.sqldelight.getUniqueHashCode
import java.util.UUID
import org.junit.Assert
import org.junit.Test
Expand All @@ -12,7 +12,7 @@ class TestQueryIdentifiers {
IntRange(0, MAX_CYCLES).forEach { index ->
val userId = UUID.randomUUID().toString()
val randomQuery = "SELECT * FROM UserEntity WHERE id = $userId"
val queryIdentifier = randomQuery.getUniqueQueryIdentifier()
val queryIdentifier = randomQuery.getUniqueHashCode()
Assert.assertNotNull(
"$index - Couldn't find a unique query identifier for random query $randomQuery",
queryIdentifier
Expand All @@ -26,7 +26,7 @@ class TestQueryIdentifiers {
val fixedQuery = "SELECT * FROM UserEntity WHERE id = $userId"

IntRange(0, MAX_CYCLES).forEach { index ->
val queryIdentifier = fixedQuery.getUniqueQueryIdentifier()
val queryIdentifier = fixedQuery.getUniqueHashCode()
Assert.assertNotNull(
"$index - Couldn't find a unique query identifier for fixed query $fixedQuery",
queryIdentifier
Expand Down

0 comments on commit e89e894

Please sign in to comment.