Skip to content

Commit

Permalink
fix: paramters count and binding issues, bump kotlin (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
tamimattafi authored May 22, 2024
1 parent 7860174 commit 7404e5e
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 81 deletions.
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -312,10 +312,11 @@ This list shows Room features, which are already supported by Kabin, or under de
- `@Junction` inside a compound is automatically created and inserted as well

## Plans and Priorities
1. [ ] Clean and refactor `compiler` and `processor` logic, make it more flexible and maintainable
2. [ ] Generate more optimized code
3. [ ] Fix bugs and issues
4. [ ] Implement more **Room** features, especially the essential ones for basic and simple apps
5. [ ] Add more features to make working with SQL easier and more interesting
6. [ ] Add multiplatform sample with UI
7. [ ] Make a stable release
1. [ ] Add Tests
2. [ ] Clean and refactor `compiler` and `processor` logic, make it more flexible and maintainable
3. [ ] Generate more optimized code
4. [ ] Fix bugs and issues
5. [ ] Implement more **Room** features, especially the essential ones for basic and simple apps
6. [ ] Add more features to make working with SQL easier and more interesting
7. [ ] Add multiplatform sample with UI
8. [ ] Make a stable release
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-alpha09"
private val version = "0.1.0-alpha10"
private val group = "com.attafitamim.kabin"

override fun apply(project: Project) {
Expand Down
10 changes: 5 additions & 5 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
[versions]
# Android
android-gradle-plugin = "8.3.1"
core-ktx = "1.12.0"
android-gradle-plugin = "8.3.2"
core-ktx = "1.13.1"
junit = "4.13.2"
androidx-test-ext-junit = "1.1.5"
espresso-core = "3.5.1"
appcompat = "1.6.1"
material = "1.11.0"

# Kotlin
kotlin = "1.9.23"
kotlin = "1.9.24"
kotlin-poet = "1.16.0"
kotlin-coroutines = "1.8.0"
kotlin-ksp = "1.9.23-1.0.19"
kotlin-ksp = "1.9.24-1.0.20"

# Docs
dokka = "1.9.10"
dokka = "1.9.20"

# Publishing
maven-publish = "0.28.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import com.attafitamim.kabin.compiler.sql.syntax.SQLQuery
import com.attafitamim.kabin.compiler.sql.utils.poet.SYMBOL_ACCESS_SIGN
import com.attafitamim.kabin.compiler.sql.utils.poet.asSpecs
import com.attafitamim.kabin.compiler.sql.utils.poet.buildSpec
import com.attafitamim.kabin.compiler.sql.utils.poet.dao.getAccessChain
import com.attafitamim.kabin.compiler.sql.utils.poet.dao.getAdapterReference
import com.attafitamim.kabin.compiler.sql.utils.poet.dao.getColumnAccessChain
import com.attafitamim.kabin.compiler.sql.utils.poet.dao.supportedBinders
Expand Down Expand Up @@ -552,7 +553,7 @@ class QueriesGenerator(
EXECUTE_FUNCTION,
) {
val requiredAdapters = addQueryEntityBinding(
query.columns,
query,
parameterName
)

Expand Down Expand Up @@ -708,7 +709,7 @@ class QueriesGenerator(
EXECUTE_FUNCTION
) {
val junctionAdapters = addQueryEntityBinding(
query.columns,
query,
junctionName
)

Expand Down Expand Up @@ -815,7 +816,7 @@ class QueriesGenerator(
EXECUTE_QUERY_FUNCTION
) {
val bindingAdapters = addQueryEntityBinding(
query.columns,
query,
entityParameter.name
)

Expand Down Expand Up @@ -901,16 +902,16 @@ class QueriesGenerator(
}

private fun CodeBlock.Builder.addQueryEntityBinding(
columns: Collection<ColumnSpec>,
query: SQLQuery.Columns,
parent: String? = null
): Set<ColumnAdapterReference> {
if (columns.isEmpty()) {
if (query.columns.isEmpty()) {
return emptySet()
}

beginControlFlow("")
val adapters = addQueryEntityColumnsBinding(
columns,
query,
parent
)

Expand All @@ -921,69 +922,41 @@ class QueriesGenerator(
private fun CodeBlock.Builder.addQueryBinding(
query: SQLQuery
): Set<ColumnAdapterReference> = when (query) {
is SQLQuery.Columns -> addQueryEntityBinding(query.columns)
is SQLQuery.Columns -> addQueryEntityBinding(query)
is SQLQuery.Parameters -> addQueryParametersBinding(query.queryParameters)
is SQLQuery.Raw -> emptySet()
}

private fun CodeBlock.Builder.addQueryEntityColumnsBinding(
columns: Collection<ColumnSpec>,
query: SQLQuery.Columns,
parent: String? = null,
initialIndex: Int = 0,
isParentNullable: Boolean = false
): Set<ColumnAdapterReference> {
val adapters = HashSet<ColumnAdapterReference>()

var currentIndex = initialIndex
columns.forEach { columnSpec ->
query.parameters.forEach { parameter ->
val columnAccessChain = query.columns
.getAccessChain(parameter)

val columnAccess = columnAccessChain.toParameterAccess(parent, isParentNullable)
val columnSpec = columnAccessChain.last()
val isNullable = isParentNullable || columnSpec.typeSpec.isNullable
val propertyName = columnSpec.declaration.simpleName.asString()
val propertyAccess = if (parent.isNullOrBlank()) {
propertyName
} else {
buildString {
append(parent)

if (isParentNullable) {
append("?")
}
val adapter = addQueryParameterBinding(
isNullable,
columnAccess,
currentIndex.toString(),
columnSpec.typeAffinity,
columnSpec.typeSpec.type
)

append(
SYMBOL_ACCESS_SIGN,
propertyName
)
}
if (adapter != null) {
adapters.add(adapter)
}

when (val dataType = columnSpec.typeSpec.dataType) {
is ColumnTypeSpec.DataType.Class -> {
val adapter = addQueryParameterBinding(
isNullable,
propertyAccess,
currentIndex.toString(),
columnSpec.typeAffinity,
columnSpec.typeSpec.type
)

if (adapter != null) {
adapters.add(adapter)
}

currentIndex++
}

is ColumnTypeSpec.DataType.Embedded -> {
val requiredAdapters = addQueryEntityColumnsBinding(
dataType.columns,
propertyAccess,
currentIndex,
isNullable
)

adapters.addAll(requiredAdapters)
currentIndex += getFlatColumns(dataType.columns).size
}
}
currentIndex++
}

return adapters
Expand Down Expand Up @@ -1014,7 +987,6 @@ class QueriesGenerator(
"$dynamicSizes + $previousSimpleParametersCount"
}


when (queryParameter.spec.typeSpec.dataType) {
is DataTypeSpec.DataType.Entity,
is DataTypeSpec.DataType.Stream,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ sealed interface SQLQuery {
override val value: String,
override val parametersSize: Int,
val columns: Collection<ColumnSpec>,
val parameters: Collection<String>,
val mutatedKeys: Set<String>,
override val queriedKeys: Set<String>
): SQLQuery
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,27 @@ fun EntitySpec.getColumnParameterAccess(columnName: String): String {
return chain.toParameterAccess()
}

fun List<ColumnSpec>.toParameterAccess(): String {
fun List<ColumnSpec>.toParameterAccess(
parent: String? = null,
isParentNullable: Boolean = false
): String {
val stringBuilder = StringBuilder()

if (!parent.isNullOrBlank()) {
stringBuilder.append(parent)

if (isParentNullable) {
stringBuilder.append("?")
}

stringBuilder.append(SYMBOL_ACCESS_SIGN)
}

for (columnIndex in 0 until lastIndex) {
val column = this[columnIndex]
stringBuilder.append(column.declaration.simpleNameString)

if (column.typeSpec.isNullable) {
if (column.typeSpec.isNullable || isParentNullable) {
stringBuilder.append("?")
}

Expand Down Expand Up @@ -149,7 +163,7 @@ fun ColumnSpec.getAccessChain(columnName: String): List<ColumnSpec> {
return chain
}

fun List<ColumnSpec>.getAccessChain(columnName: String): List<ColumnSpec> {
fun Collection<ColumnSpec>.getAccessChain(columnName: String): List<ColumnSpec> {
forEach { columnSpec ->
val chain = columnSpec.getAccessChain(columnName)
if (chain.isNotEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ fun FunSpec.Builder.addDriverRawQueryCode(
function: String,
binderCode: (CodeBlock.Builder.() -> Unit)? = null
): FunSpec.Builder = apply {
val logic = if (function == "executeQuery") {
val logic = if (function == EXECUTE_QUERY_FUNCTION) {
"""
|val result = driver.executeQuery(
| null,
Expand All @@ -82,7 +82,7 @@ fun FunSpec.Builder.addDriverRawQueryCode(
codeBlockBuilder.binderCode()
}

if (function == "executeQuery") {
if (function == EXECUTE_QUERY_FUNCTION) {
codeBlockBuilder.addStatement("return result")
}

Expand Down Expand Up @@ -201,21 +201,21 @@ fun FunSpec.Builder.addDriverQueryCode(
val codeBlockBuilder = CodeBlock.builder()

val identifier = query.hashCode()
val logic = if (function == "executeQuery") {
val logic = if (function == EXECUTE_QUERY_FUNCTION) {
"""
|val result = driver.executeQuery(
| $identifier,
| %P,
| mapper,
| ${parameters.size}
| ${query.parametersSize}
|)
""".trimMargin()
} else {
"""
|driver.execute(
| $identifier,
| %P,
| ${parameters.size}
| ${query.parametersSize}
|)
""".trimMargin()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ fun getSelectSQLQuery(
query,
parameters.size,
columns,
parameters,
mutatedKeys = emptySet(),
queriedKeys = setOf(entitySpec.tableName)
)
Expand All @@ -258,14 +259,11 @@ fun DaoActionSpec.Delete.getSQLQuery(
DELETE; FROM(entity.tableName); WHERE.equalParameters(parameters)
}

val columns = entity.columns.filter { columnSpec ->
parameters.contains(columnSpec.name)
}

return SQLQuery.Columns(
query,
parameters.size,
columns,
entity.columns,
parameters,
mutatedKeys = setOf(entity.tableName),
queriedKeys = emptySet()
)
Expand All @@ -286,6 +284,7 @@ fun DaoActionSpec.Insert.getSQLQuery(
query,
parameters.size,
entity.columns,
parameters,
mutatedKeys = setOf(entity.tableName),
queriedKeys = emptySet()
)
Expand All @@ -304,11 +303,12 @@ fun DaoActionSpec.Update.getSQLQuery(
WHERE.equalParameters(entity.primaryKeys)
}

val parametersSize = parameters.size + primaryKeys.size
val totalParameters = parameters + primaryKeys
return SQLQuery.Columns(
query,
parametersSize,
totalParameters.size,
entity.columns,
totalParameters,
mutatedKeys = setOf(entity.tableName),
queriedKeys = emptySet()
)
Expand All @@ -329,6 +329,7 @@ fun DaoActionSpec.Upsert.getSQLQuery(
query,
parameters.size,
actualEntitySpec.columns,
parameters,
mutatedKeys = setOf(entity.tableName),
queriedKeys = emptySet()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.attafitamim.kabin.annotations.RawQuery
import com.attafitamim.kabin.annotations.Transaction
import com.attafitamim.kabin.annotations.Update
import com.attafitamim.kabin.local.entities.bank.BankEntity
import com.attafitamim.kabin.local.entities.bank.CardEntity
import com.attafitamim.kabin.local.entities.user.MarriedCount
import com.attafitamim.kabin.local.entities.user.UserEntity
import kotlinx.coroutines.flow.Flow
Expand All @@ -34,6 +35,9 @@ interface UserDao {
@Update
suspend fun update(entity: UserEntity)

@Update
suspend fun update(entity: CardEntity)

@Delete
@Transaction
suspend fun delete(entity: UserEntity)
Expand Down

0 comments on commit 7404e5e

Please sign in to comment.