Skip to content

Add a Kotlin language backend #287

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

Closed
mightyguava opened this issue Jan 22, 2020 · 4 comments
Closed

Add a Kotlin language backend #287

mightyguava opened this issue Jan 22, 2020 · 4 comments
Labels
enhancement New feature or request

Comments

@mightyguava
Copy link
Contributor

I saw there's Typescript support in the works (is there a tracking issue?). It'd be neat if it could support Kotlin too. There's something similar for Kotlin called SQLDelight, but it only supports sqlite and was built for Android. JVM support for Java/Kotlin would be really cool.

I might be able to contribute if you are open to this.

@kyleconroy
Copy link
Collaborator

I saw there's Typescript support in the works (is there a tracking issue?)

There wasn't, but I just created #296

JVM support for Java/Kotlin would be really cool.

Agreed! Do you have some example code that you'd hope sqlc would generate for you?

Java/Kotlin support is far off. We're still working through supporting both MySQL and PostgreSQL for Go. Have you looked at jOOQ? It may fit your need.

jOOQ generates Java code from your database and lets you build type safe SQL queries through its fluent API

I'm going to go ahead and close this for now. Once we're ready to add Java/Kotlin, we'll re-open it.

@kyleconroy kyleconroy added the future In the year 3000... label Jan 24, 2020
@mightyguava
Copy link
Contributor Author

mightyguava commented Jan 24, 2020

Yes we use jOOQ. Its code generation is pretty limited, only going as far as introspecting the DB to generate the models. The fluent API makes it similar to squirrel, but with more type safety. But it's the SQL => Function code generation that really makes sqlc powerful imo.

I think the code (for Kotlin at least), looks quite similar to Go. Here's what I think createAuthor() could look like.

package com.example.testmodule

import java.sql.Connection
import java.sql.SQLException

data class Author(val id: Int, val name: String, val bio: String?);
data class CreateAuthorParams(val name: String, val bio: String?)

const val createAuthor = """-- name: CreateAuthor :one
INSERT INTO authors (
          name, bio
) VALUES (
  \$1, \$2
)
RETURNING id, name, bio
"""

class Queries(private val conn: Connection) {
  @Throws(SQLException::class)
  fun createAuthor(arg: CreateAuthorParams): Author {
    val stmt = conn.prepareStatement(createAuthor)
    stmt.setString(1, arg.name)
    stmt.setString(2, arg.bio)

    val rs = stmt.executeQuery()
    rs.next()
    val author = Author(rs.getInt(1), rs.getString(2), rs.getString(3))
    if (rs.next()) {
      throw SQLException("expected one row, but got many")
    }
    return author
  }
}

SQLDelight has a much fancier take on it, though I'm not sure if sqlc needs to do the same

I understand not wanting to take on too much for such a young project. But maybe naively I don't think Kotlin support would be all that more complex than Typescript support. I spend a lot of time in Go and Kotlin, and they are similar in certain aspects. And Kotlin <=> Java is completely interoperable.

Again, if I might be able to convince you this is worthwhile to do soon, I would definitely love to contribute!

@mightyguava
Copy link
Contributor Author

Spent some time building a more complete example: https://gist.github.com/mightyguava/f2b7b0c3d1385df8eac509cc93f8d4f2

@mightyguava mightyguava mentioned this issue Jan 26, 2020
7 tasks
@kyleconroy kyleconroy added enhancement New feature or request and removed future In the year 3000... labels Jan 30, 2020
@kyleconroy kyleconroy changed the title Kotlin support Add a Kotlin language backend Jan 30, 2020
@kyleconroy kyleconroy reopened this Jan 30, 2020
@kyleconroy kyleconroy modified the milestone: v1.0.0 Feb 10, 2020
@kyleconroy
Copy link
Collaborator

The first version of this has now landed! I'm going to mark this as complete. I've added a new kotlin label for future issues.

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

No branches or pull requests

2 participants