Skip to content

Commit

Permalink
Generate UDT writers with specific PGTypes
Browse files Browse the repository at this point in the history
  • Loading branch information
kdubb committed Apr 10, 2020
1 parent 3674915 commit 724730e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import java.sql.SQLType;


public interface PGAnyType extends SQLType {
public interface PGAnyType extends SQLType {

String VENDOR_NAME = "PostgreSQL";

Expand Down
12 changes: 8 additions & 4 deletions udt-gen/src/main/kotlin/UDTGenerator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.impossibl.postgres.tools

import com.impossibl.postgres.api.jdbc.PGAnyType
import com.impossibl.postgres.api.jdbc.PGConnection
import com.impossibl.postgres.api.jdbc.PGType
import com.impossibl.postgres.types.QualifiedName
import com.squareup.javapoet.*
import com.xenomachina.argparser.*
Expand Down Expand Up @@ -364,11 +365,14 @@ class UDTGenerator(
attrSqlType.javaType.writerTypeName == "Object" ->
CodeBlock.of("out.writeObject(this.\$L, \$T.\$L);\n", attrPropName, JDBCType::class.java, "STRUCT")

attrTypeName.box().isBoxedPrimitive ->
CodeBlock.of("out.writeObject(this.\$L, \$T.\$L);\n", attrPropName, JDBCType::class.java, attrTypeName.primitiveJDBCType)

PGType.values().any { it.vendorTypeNumber == attrSqlType.vendorTypeNumber } ->
CodeBlock.of("out.writeObject(this.\$L, \$T.\$L);\n", attrPropName, PGType::class.java, PGType.valueOf(attrSqlType.vendorTypeNumber).name)

else ->
if (attrTypeName.box().isBoxedPrimitive)
CodeBlock.of("out.writeObject(this.\$L, \$T.\$L);\n", attrPropName, JDBCType::class.java, attrTypeName.primitiveJDBCType)
else
CodeBlock.of("out.write\$L(this.\$L);\n", attrSqlType.javaType.readerTypeName, attrPropName)
CodeBlock.of("out.write\$L(this.\$L);\n", attrSqlType.javaType.readerTypeName, attrPropName)
}
)

Expand Down
10 changes: 6 additions & 4 deletions udt-gen/src/test/kotlin/UDTGeneratorTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import org.junit.jupiter.api.Test
import java.io.File
import java.sql.DriverManager
import java.util.*
import javax.tools.Diagnostic
import kotlin.random.Random

class UDTGeneratorTest {
Expand All @@ -31,7 +32,7 @@ class UDTGeneratorTest {

@Test
fun testCompile() {
System.out.println("Generating code from $url")
println("Generating code from $url")

DriverManager.getConnection(url, props).use { connection ->

Expand All @@ -41,7 +42,7 @@ class UDTGeneratorTest {
connection.createStatement().use { stmt ->
stmt.execute("CREATE SCHEMA $schemaName")
stmt.execute("CREATE TYPE $schemaName.title as enum ('mr', 'mrs', 'ms', 'dr')")
stmt.execute("CREATE TYPE $schemaName.address as (street text, city text, state char(2), zip char(5))")
stmt.execute("CREATE TYPE $schemaName.address as (street varchar, city text, state char(2), zip char(5))")
stmt.execute("CREATE TYPE $schemaName.v_card as (id int4, name text, title $schemaName.title, addresses $schemaName.address[])")
stmt.execute("SET SEARCH_PATH = public, $schemaName")
}
Expand All @@ -55,6 +56,7 @@ class UDTGeneratorTest {
val result = javac()
.compile(files + JavaFileObjects.forResource("VCardTest.java"))

assertThat(result.errors(), equalTo(emptyList<Diagnostic<*>>()))
assertThat(result.status(), equalTo(Compilation.Status.SUCCESS))

}
Expand All @@ -69,7 +71,7 @@ class UDTGeneratorTest {

@Test
fun testGenerate() {
System.out.println("Generating code from $url")
println("Generating code from $url")

DriverManager.getConnection(url, props).use { connection ->

Expand All @@ -93,7 +95,7 @@ class UDTGeneratorTest {
.generate(outDirectory)

val pkgFileNames = File(outDirectory, pkgName.replace('.', '/'))
.listFiles()
.listFiles()!!
.map { it.name }

assertThat(pkgFileNames.size, equalTo(3))
Expand Down

0 comments on commit 724730e

Please sign in to comment.