Skip to content

Commit

Permalink
Poorva | Refactor codegenerator. (#207)
Browse files Browse the repository at this point in the history
 Extract Command and Query classes
  • Loading branch information
Poorva17 authored Oct 31, 2021
1 parent 8dc8ae2 commit 8a5d96e
Show file tree
Hide file tree
Showing 18 changed files with 359 additions and 237 deletions.
4 changes: 1 addition & 3 deletions cli/src/main/kotlin/norm/api/NormApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@ class NormApi(
connection: Connection
) {
private val sqlAnalyzer = SqlAnalyzer(connection)
private val codeGenerator = CodeGenerator()

/**
* Generates code of classes against given SQL Query
*/
fun generate(query: String, packageName: String, baseName: String): String {
val sqlModel = sqlAnalyzer.sqlModel(query)
return codeGenerator.generate(sqlModel, packageName, baseName)
return CodeGenerator.generate(listOf(sqlModel), packageName, baseName)
}
}
2 changes: 1 addition & 1 deletion cli/src/test/kotlin/norm/test/utils/TestUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fun toArgs(str: String): Array<String> = str.split(" ").toTypedArray()

fun codegen(conn: Connection, query: String, pkg: String, base: String): String {
val sqlModel = SqlAnalyzer(conn).sqlModel(query)
return CodeGenerator().generate(sqlModel, pkg, base).trim()
return CodeGenerator.generate(listOf(sqlModel), pkg, base).trim()
}

fun String.readAsResource(): String = object {}.javaClass.getResource(this).readText().trim()
12 changes: 6 additions & 6 deletions cli/src/test/resources/gen/array-any.expected.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,6 @@ public class FooParamSetter : ParamSetter<FooParams> {
}
}

public data class FooResult(
public val id: Int,
public val firstName: String?,
public val lastName: String?
)

public class FooRowMapper : RowMapper<FooResult> {
public override fun map(rs: ResultSet): FooResult = FooResult(
id = rs.getObject("id") as kotlin.Int,
Expand All @@ -40,3 +34,9 @@ public class FooQuery : Query<FooParams, FooResult> {

public override val paramSetter: ParamSetter<FooParams> = FooParamSetter()
}

public data class FooResult(
public val id: Int,
public val firstName: String?,
public val lastName: String?
)
11 changes: 5 additions & 6 deletions cli/src/test/resources/gen/array-columns-mapping.expected.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@ public class FooParamSetter : ParamSetter<FooParams> {
}
}

public data class FooResult(
public val id: Int,
public val colors: Array<String>?,
public val details: PGobject?
)

public class FooRowMapper : RowMapper<FooResult> {
public override fun map(rs: ResultSet): FooResult = FooResult(
id = rs.getObject("id") as kotlin.Int,
Expand All @@ -39,3 +33,8 @@ public class FooQuery : Query<FooParams, FooResult> {
public override val paramSetter: ParamSetter<FooParams> = FooParamSetter()
}

public data class FooResult(
public val id: Int,
public val colors: Array<String>?,
public val details: PGobject?
)
10 changes: 5 additions & 5 deletions cli/src/test/resources/gen/array-contains.expected.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ public class FooParamSetter : ParamSetter<FooParams> {
}
}

public data class FooResult(
public val id: Int,
public val colors: Array<String>?
)

public class FooRowMapper : RowMapper<FooResult> {
public override fun map(rs: ResultSet): FooResult = FooResult(
id = rs.getObject("id") as kotlin.Int,
Expand All @@ -38,3 +33,8 @@ public class FooQuery : Query<FooParams, FooResult> {

public override val paramSetter: ParamSetter<FooParams> = FooParamSetter()
}

public data class FooResult(
public val id: Int,
public val colors: Array<String>?
)
13 changes: 7 additions & 6 deletions cli/src/test/resources/gen/empty-params-class.expected.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@ public class FooParamSetter : ParamSetter<FooParams> {
}
}

public data class FooResult(
public val id: Int,
public val firstName: String?,
public val lastName: String?
)

public class FooRowMapper : RowMapper<FooResult> {
public override fun map(rs: ResultSet): FooResult = FooResult(
id = rs.getObject("id") as kotlin.Int,
Expand All @@ -36,3 +30,10 @@ public class FooQuery : Query<FooParams, FooResult> {

public override val paramSetter: ParamSetter<FooParams> = FooParamSetter()
}

public data class FooResult(
public val id: Int,
public val firstName: String?,
public val lastName: String?
)

20 changes: 10 additions & 10 deletions cli/src/test/resources/gen/left-joined-nullable-check.expected.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,6 @@ public class FooParamSetter : ParamSetter<FooParams> {
}
}

public data class FooResult(
public val id: Int,
public val firstName: String?,
public val lastName: String?,
public val departmentId: Int?,
public val name: String?,
public val combinationsId: Int?,
public val colors: Array<String>?
)

public class FooRowMapper : RowMapper<FooResult> {
public override fun map(rs: ResultSet): FooResult = FooResult(
id = rs.getObject("id") as kotlin.Int,
Expand All @@ -50,3 +40,13 @@ public class FooQuery : Query<FooParams, FooResult> {

public override val paramSetter: ParamSetter<FooParams> = FooParamSetter()
}

public data class FooResult(
public val id: Int,
public val firstName: String?,
public val lastName: String?,
public val departmentId: Int?,
public val name: String?,
public val combinationsId: Int?,
public val colors: Array<String>?
)
12 changes: 6 additions & 6 deletions cli/src/test/resources/gen/query-class-generator.expected.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@ public class FooParamSetter : ParamSetter<FooParams> {
}
}

public data class FooResult(
public val id: Int,
public val firstName: String?,
public val lastName: String?
)

public class FooRowMapper : RowMapper<FooResult> {
public override fun map(rs: ResultSet): FooResult = FooResult(
id = rs.getObject("id") as kotlin.Int,
Expand All @@ -41,3 +35,9 @@ public class FooQuery : Query<FooParams, FooResult> {

public override val paramSetter: ParamSetter<FooParams> = FooParamSetter()
}

public data class FooResult(
public val id: Int,
public val firstName: String?,
public val lastName: String?
)
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,6 @@ public class FooParamSetter : ParamSetter<FooParams> {
}
}

public data class FooResult(
public val id: UUID,
public val columnName: String?,
public val oldValue: String?,
public val newValue: String?,
public val capturedAt: OffsetDateTime?
)

public class FooRowMapper : RowMapper<FooResult> {
public override fun map(rs: ResultSet): FooResult = FooResult(
id = rs.getObject("id") as java.util.UUID,
Expand All @@ -41,3 +33,11 @@ public class FooQuery : Query<FooParams, FooResult> {

public override val paramSetter: ParamSetter<FooParams> = FooParamSetter()
}

public data class FooResult(
public val id: UUID,
public val columnName: String?,
public val oldValue: String?,
public val newValue: String?,
public val capturedAt: OffsetDateTime?
)
Loading

0 comments on commit 8a5d96e

Please sign in to comment.