Skip to content

Commit

Permalink
Merge branch 'feature/dao-references' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
EpicSquid committed Jan 20, 2023
2 parents 71b8334 + 986600f commit 6d07055
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
object PluginCoordinates {
const val ID = "com.jetbrains.exposed.gradle.plugin"
const val GROUP = "com.jetbrains.exposed.gradle"
const val VERSION = "0.2.2.2-SNAPSHOT"
const val VERSION = "0.2.2.3-SNAPSHOT"
const val IMPLEMENTATION_CLASS = "com.jetbrains.exposed.gradle.plugin.ExposedGradlePlugin"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ data class ExposedCodeGeneratorConfiguration(
val collate: String? = null,
val columnMappings: Map<String, String> = emptyMap(),
val dateTimeProvider: String? = null,
val useFullNames: Boolean = generateSingleFile
val useFullNames: Boolean = generateSingleFile,
val useDao: Boolean = false
)
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ open class ColumnBuilder(column: Column, private val data: TableBuilderData) {
val column = columnInfo.column
throw UnsupportedTypeException("Unable to map column ${column.name} of type ${column.columnDataType.fullName} to an Exposed column object.")
}
if (data.configuration.useDao && columnInfo.column.referencedColumn != null) {
return PropertySpec.builder(
getPropertyNameForColumn(columnInfo.column),
ExposedColumn::class.asTypeName().parameterizedBy(EntityID::class.asTypeName().parameterizedBy(
columnInfo.columnKClass!!.asTypeName().copy(nullable = columnInfo.nullable)))
)
}
return PropertySpec.builder(
getPropertyNameForColumn(columnInfo.column),
ExposedColumn::class.asTypeName().parameterizedBy(columnInfo.columnKClass!!.asTypeName().copy(nullable = columnInfo.nullable))
Expand All @@ -49,7 +56,7 @@ open class ColumnBuilder(column: Column, private val data: TableBuilderData) {
columnToTableSpec: Map<Column, TypeSpec>
) {
val initializerBlock = buildCodeBlock {
generateExposedColumnFunctionCall(columnInfo)
generateExposedColumnFunctionCall(columnInfo, columnToPropertySpec, columnToTableSpec)
generateExposedColumnConstraints(columnInfo, columnToPropertySpec, columnToTableSpec)
}

Expand All @@ -59,7 +66,11 @@ open class ColumnBuilder(column: Column, private val data: TableBuilderData) {
/**
* Generates an Exposed function call used to initialize the column from [columnInfo].
*/
open fun CodeBlock.Builder.generateExposedColumnFunctionCall(columnInfo: ColumnInfo) {
open fun CodeBlock.Builder.generateExposedColumnFunctionCall(
columnInfo: ColumnInfo,
columnToPropertySpec: Map<Column, PropertySpec>,
columnToTableSpec: Map<Column, TypeSpec>
) {
val column = columnInfo.column
val columnKClass = columnInfo.columnKClass!!
val columnExposedFunction = columnInfo.columnExposedFunction!!
Expand All @@ -71,7 +82,13 @@ open class ColumnBuilder(column: Column, private val data: TableBuilderData) {
}
val memberName = MemberName(packageName, columnExposedFunction.name)

if (columnInfo.columnExposedFunction!!.valueParameters.size > 1) {
if (data.configuration.useDao && column.referencedColumn != null) {
val referencedColumnTable = columnToTableSpec[column.referencedColumn]
add("%M(%S, %N)",
memberName,
columnInfo.columnName,
referencedColumnTable)
} else if (columnInfo.columnExposedFunction!!.valueParameters.size > 1) {
val arguments = getColumnFunctionArguments()
when (columnKClass) {
// decimal -> precision, scale
Expand Down Expand Up @@ -132,7 +149,7 @@ open class ColumnBuilder(column: Column, private val data: TableBuilderData) {
columnToPropertySpec: Map<Column, PropertySpec>,
columnToTableSpec: Map<Column, TypeSpec>
) {
if (column.referencedColumn != null) {
if (!data.configuration.useDao && column.referencedColumn != null) {
val referencedColumnProperty = columnToPropertySpec[column.referencedColumn]
?: throw ReferencedColumnNotFoundException(
"Column ${column.referencedColumn.fullName} referenced by ${column.fullName} not found."
Expand Down Expand Up @@ -351,7 +368,11 @@ open class MappedColumnBuilder(column: Column, private val columnMapping: String
ExposedColumn::class.asTypeName().parameterizedBy(mappedColumnType.asTypeName().copy(nullable = columnInfo.nullable))
)

override fun CodeBlock.Builder.generateExposedColumnFunctionCall(columnInfo: ColumnInfo) {
override fun CodeBlock.Builder.generateExposedColumnFunctionCall(
columnInfo: ColumnInfo,
columnToPropertySpec: Map<Column, PropertySpec>,
columnToTableSpec: Map<Column, TypeSpec>
) {
add(CodeBlock.of(columnMapping))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ data class ColumnInfo(val column: Column, private val data: TableBuilderData) {

fun <T : Any> initializeColumnParameters(columnClass: KClass<out T>, columnFunction: KFunction<ExposedColumn<T>>) {
columnKClass = columnClass
columnExposedFunction = columnFunction
columnExposedFunction = if (data.configuration.useDao && column.referencedColumn != null) getExposedFunction<T>("reference") else columnFunction
}

fun initializeInteger() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ abstract class ExposedGenerateCodeTask : DefaultTask() {
@get:Optional
abstract val useFullNames: Property<Boolean>

@get:Input
@get:Option(
option = "useDao",
description = "Enable the use of DAO classes when generating files."
)
@get:Optional
abstract val useDao: Property<Boolean>

@get:Input
@get:Option(
option = "generatedFileName",
Expand Down Expand Up @@ -158,6 +166,7 @@ abstract class ExposedGenerateCodeTask : DefaultTask() {
columnMappings.getOrElse(emptyMap()),
dateTimeProvider.orNull,
useFullNames.getOrElse(true),
useDao.getOrElse(false)
)
ExposedCodeGenerator(tables, config)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ abstract class ExposedGradleExtension @Inject constructor(project: Project) {
var dateTimeProvider: String? = null
var generateSingleFile: Boolean = false
var useFullNames: Boolean = true
var useDao: Boolean = false
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ abstract class ExposedGradlePlugin : Plugin<Project> {
it.packageName.set(getStringProperty(project, extension, "packageName"))
it.generateSingleFile.set(getProperty(project, extension, "generateSingleFile") as Boolean)
it.useFullNames.set(getProperty(project, extension, "useFullNames") as Boolean)
it.useDao.set(getProperty(project, extension, "useDao") as Boolean)
it.generatedFileName.set(getStringProperty(project, extension, "generatedFileName"))
it.collate.set(getStringProperty(project, extension, "collate"))
// TODO
Expand Down

0 comments on commit 6d07055

Please sign in to comment.