Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kotlin Support #357

Open
tomansill opened this issue Sep 3, 2021 · 1 comment
Open

Kotlin Support #357

tomansill opened this issue Sep 3, 2021 · 1 comment
Labels

Comments

@tomansill
Copy link

Hello,

If this is not appropriate place to ask, please go ahead and close this ticket. I apologize in the advance if that's the case.

Does anyone know if Kotlin can be used with this library to execute Kotlin code on Postgres server? I mean, Kotlin runs on JVM and I'm assuming this library uses JVM inside of Postgres server, right?

@jcflack
Copy link
Contributor

jcflack commented Sep 3, 2021

There should be no particular difficulty. The Kotlin compiler can produce class files, and you can put those into a jar file and load that into PL/Java. Functions that you intend to call from PostgreSQL must be compiled into Java static methods, which is possible as described in Calling Kotlin from Java.

PostgreSQL then needs SQL CREATE FUNCTION commands that declare functions to be available in SQL and map them to named static methods in your jar with compatible signatures. You can include such commands in a deployment descriptor file in the jar, so that PL/Java can declare the SQL functions at the same time it loads your jar.

Writing the SQL commands for the deployment descriptor can be a bit like repeating yourself, as they are really just SQL-language declarations of the same methods and argument types that you have already declared in Java (or Kotlin). And if you write them by hand, it is possible not to keep them up to date with code changes, and the problems might not be detected until the jar is loaded into PostgreSQL and you try to call the functions.

To avoid repeating yourself, when you are writing in Java, you can use certain Java annotations to cause a correct deployment descriptor to be written at the same time the Java sources are compiled. If there would be declaration inconsistencies, those can be reported at compile time, instead of only discovered after loading the code into PostgreSQL.

I am not very knowledgeable about annotation support in Kotlin, or whether it would be easy or hard to make use of the existing Java annotations to generate deployment code at compile time, or easy or hard to develop similar annotations for Kotlin specifically. That might be an interesting area that you could explore. In any case, you can always write the SQL declarations by hand, which is exactly how PL/Java was always used before the Java annotation support was added.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants