From 8e42f9ea80da40dce6ccd71e7f3adb1d1c578e6d Mon Sep 17 00:00:00 2001 From: Yunchi Luo Date: Fri, 31 Jan 2020 08:58:48 -0500 Subject: [PATCH] kotlin: unbox query params --- .../kotlin/com/example/authors/QueriesImpl.kt | 11 +- .../booktest/postgresql/QueriesImpl.kt | 86 +++++------ .../main/kotlin/com/example/ondeck/Queries.kt | 17 ++- .../kotlin/com/example/ondeck/QueriesImpl.kt | 77 ++++------ .../com/example/authors/QueriesImplTest.kt | 26 ++-- .../booktest/postgresql/QueriesImplTest.kt | 14 +- .../com/example/ondeck/QueriesImplTest.kt | 10 +- internal/dinosql/kotlin/gen.go | 142 ++++++++---------- 8 files changed, 154 insertions(+), 229 deletions(-) diff --git a/examples/kotlin/src/main/kotlin/com/example/authors/QueriesImpl.kt b/examples/kotlin/src/main/kotlin/com/example/authors/QueriesImpl.kt index 80dd14284e..9ee42a6abf 100644 --- a/examples/kotlin/src/main/kotlin/com/example/authors/QueriesImpl.kt +++ b/examples/kotlin/src/main/kotlin/com/example/authors/QueriesImpl.kt @@ -14,11 +14,6 @@ INSERT INTO authors ( RETURNING id, name, bio """ -data class CreateAuthorParams ( - val name: String, - val bio: String? -) - const val deleteAuthor = """-- name: deleteAuthor :exec DELETE FROM authors WHERE id = ? @@ -37,10 +32,10 @@ ORDER BY name class QueriesImpl(private val conn: Connection) { @Throws(SQLException::class) - fun createAuthor(arg: CreateAuthorParams): Author { + fun createAuthor(name: String, bio: String?): Author { return conn.prepareStatement(createAuthor).use { stmt -> - stmt.setString(1, arg.name) - stmt.setString(2, arg.bio) + stmt.setString(1, name) + stmt.setString(2, bio) val results = stmt.executeQuery() if (!results.next()) { diff --git a/examples/kotlin/src/main/kotlin/com/example/booktest/postgresql/QueriesImpl.kt b/examples/kotlin/src/main/kotlin/com/example/booktest/postgresql/QueriesImpl.kt index 27a7c28602..f6e13ab8db 100644 --- a/examples/kotlin/src/main/kotlin/com/example/booktest/postgresql/QueriesImpl.kt +++ b/examples/kotlin/src/main/kotlin/com/example/booktest/postgresql/QueriesImpl.kt @@ -32,11 +32,6 @@ SELECT book_id, author_id, isbn, booktype, title, year, available, tags FROM boo WHERE title = ? AND year = ? """ -data class BooksByTitleYearParams ( - val title: String, - val year: Int -) - const val createAuthor = """-- name: createAuthor :one INSERT INTO authors (name) VALUES (?) RETURNING author_id, name @@ -63,16 +58,6 @@ INSERT INTO books ( RETURNING book_id, author_id, isbn, booktype, title, year, available, tags """ -data class CreateBookParams ( - val authorId: Int, - val isbn: String, - val booktype: BookType, - val title: String, - val year: Int, - val available: OffsetDateTime, - val tags: List -) - const val deleteBook = """-- name: deleteBook :exec DELETE FROM books WHERE book_id = ? @@ -94,31 +79,18 @@ SET title = ?, tags = ? WHERE book_id = ? """ -data class UpdateBookParams ( - val title: String, - val tags: List, - val bookId: Int -) - const val updateBookISBN = """-- name: updateBookISBN :exec UPDATE books SET title = ?, tags = ?, isbn = ? WHERE book_id = ? """ -data class UpdateBookISBNParams ( - val title: String, - val tags: List, - val isbn: String, - val bookId: Int -) - class QueriesImpl(private val conn: Connection) { @Throws(SQLException::class) - fun booksByTags(dollar_1: List): List { + fun booksByTags(dollar1: List): List { return conn.prepareStatement(booksByTags).use { stmt -> - stmt.setArray(1, conn.createArrayOf("pg_catalog.varchar", dollar_1.toTypedArray())) + stmt.setArray(1, conn.createArrayOf("pg_catalog.varchar", dollar1.toTypedArray())) val results = stmt.executeQuery() val ret = mutableListOf() @@ -136,10 +108,10 @@ class QueriesImpl(private val conn: Connection) { } @Throws(SQLException::class) - fun booksByTitleYear(arg: BooksByTitleYearParams): List { + fun booksByTitleYear(title: String, year: Int): List { return conn.prepareStatement(booksByTitleYear).use { stmt -> - stmt.setString(1, arg.title) - stmt.setInt(2, arg.year) + stmt.setString(1, title) + stmt.setInt(2, year) val results = stmt.executeQuery() val ret = mutableListOf() @@ -180,15 +152,22 @@ class QueriesImpl(private val conn: Connection) { } @Throws(SQLException::class) - fun createBook(arg: CreateBookParams): Book { + fun createBook( + authorId: Int, + isbn: String, + booktype: BookType, + title: String, + year: Int, + available: OffsetDateTime, + tags: List): Book { return conn.prepareStatement(createBook).use { stmt -> - stmt.setInt(1, arg.authorId) - stmt.setString(2, arg.isbn) - stmt.setObject(3, arg.booktype.value, Types.OTHER) - stmt.setString(4, arg.title) - stmt.setInt(5, arg.year) - stmt.setObject(6, arg.available) - stmt.setArray(7, conn.createArrayOf("pg_catalog.varchar", arg.tags.toTypedArray())) + stmt.setInt(1, authorId) + stmt.setString(2, isbn) + stmt.setObject(3, booktype.value, Types.OTHER) + stmt.setString(4, title) + stmt.setInt(5, year) + stmt.setObject(6, available) + stmt.setArray(7, conn.createArrayOf("pg_catalog.varchar", tags.toTypedArray())) val results = stmt.executeQuery() if (!results.next()) { @@ -267,23 +246,30 @@ class QueriesImpl(private val conn: Connection) { } @Throws(SQLException::class) - fun updateBook(arg: UpdateBookParams) { + fun updateBook( + title: String, + tags: List, + bookId: Int) { conn.prepareStatement(updateBook).use { stmt -> - stmt.setString(1, arg.title) - stmt.setArray(2, conn.createArrayOf("pg_catalog.varchar", arg.tags.toTypedArray())) - stmt.setInt(3, arg.bookId) + stmt.setString(1, title) + stmt.setArray(2, conn.createArrayOf("pg_catalog.varchar", tags.toTypedArray())) + stmt.setInt(3, bookId) stmt.execute() } } @Throws(SQLException::class) - fun updateBookISBN(arg: UpdateBookISBNParams) { + fun updateBookISBN( + title: String, + tags: List, + isbn: String, + bookId: Int) { conn.prepareStatement(updateBookISBN).use { stmt -> - stmt.setString(1, arg.title) - stmt.setArray(2, conn.createArrayOf("pg_catalog.varchar", arg.tags.toTypedArray())) - stmt.setString(3, arg.isbn) - stmt.setInt(4, arg.bookId) + stmt.setString(1, title) + stmt.setArray(2, conn.createArrayOf("pg_catalog.varchar", tags.toTypedArray())) + stmt.setString(3, isbn) + stmt.setInt(4, bookId) stmt.execute() } diff --git a/examples/kotlin/src/main/kotlin/com/example/ondeck/Queries.kt b/examples/kotlin/src/main/kotlin/com/example/ondeck/Queries.kt index c67fd0b83f..69debb00a7 100644 --- a/examples/kotlin/src/main/kotlin/com/example/ondeck/Queries.kt +++ b/examples/kotlin/src/main/kotlin/com/example/ondeck/Queries.kt @@ -9,10 +9,17 @@ import java.time.LocalDateTime interface Queries { @Throws(SQLException::class) - fun createCity(arg: CreateCityParams): City + fun createCity(name: String, slug: String): City @Throws(SQLException::class) - fun createVenue(arg: CreateVenueParams): Int + fun createVenue( + slug: String, + name: String, + city: String, + spotifyPlaylist: String, + status: Status, + statuses: List, + tags: List): Int @Throws(SQLException::class) fun deleteVenue(slug: String) @@ -21,7 +28,7 @@ interface Queries { fun getCity(slug: String): City @Throws(SQLException::class) - fun getVenue(arg: GetVenueParams): Venue + fun getVenue(slug: String, city: String): Venue @Throws(SQLException::class) fun listCities(): List @@ -30,10 +37,10 @@ interface Queries { fun listVenues(city: String): List @Throws(SQLException::class) - fun updateCityName(arg: UpdateCityNameParams) + fun updateCityName(name: String, slug: String) @Throws(SQLException::class) - fun updateVenueName(arg: UpdateVenueNameParams): Int + fun updateVenueName(name: String, slug: String): Int @Throws(SQLException::class) fun venueCountByCity(): List diff --git a/examples/kotlin/src/main/kotlin/com/example/ondeck/QueriesImpl.kt b/examples/kotlin/src/main/kotlin/com/example/ondeck/QueriesImpl.kt index 029e7eae1c..774b67cb4d 100644 --- a/examples/kotlin/src/main/kotlin/com/example/ondeck/QueriesImpl.kt +++ b/examples/kotlin/src/main/kotlin/com/example/ondeck/QueriesImpl.kt @@ -17,11 +17,6 @@ INSERT INTO city ( ) RETURNING slug, name """ -data class CreateCityParams ( - val name: String, - val slug: String -) - const val createVenue = """-- name: createVenue :one INSERT INTO venue ( slug, @@ -44,16 +39,6 @@ INSERT INTO venue ( ) RETURNING id """ -data class CreateVenueParams ( - val slug: String, - val name: String, - val city: String, - val spotifyPlaylist: String, - val status: Status, - val statuses: List, - val tags: List -) - const val deleteVenue = """-- name: deleteVenue :exec DELETE FROM venue WHERE slug = ? AND slug = ? @@ -71,11 +56,6 @@ FROM venue WHERE slug = ? AND city = ? """ -data class GetVenueParams ( - val slug: String, - val city: String -) - const val listCities = """-- name: listCities :many SELECT slug, name FROM city @@ -95,11 +75,6 @@ SET name = ? WHERE slug = ? """ -data class UpdateCityNameParams ( - val name: String, - val slug: String -) - const val updateVenueName = """-- name: updateVenueName :one UPDATE venue SET name = ? @@ -107,11 +82,6 @@ WHERE slug = ? RETURNING id """ -data class UpdateVenueNameParams ( - val name: String, - val slug: String -) - const val venueCountByCity = """-- name: venueCountByCity :many SELECT city, @@ -133,10 +103,10 @@ class QueriesImpl(private val conn: Connection) : Queries { // This is the third line @Throws(SQLException::class) - override fun createCity(arg: CreateCityParams): City { + override fun createCity(name: String, slug: String): City { return conn.prepareStatement(createCity).use { stmt -> - stmt.setString(1, arg.name) - stmt.setString(2, arg.slug) + stmt.setString(1, name) + stmt.setString(2, slug) val results = stmt.executeQuery() if (!results.next()) { @@ -154,15 +124,22 @@ class QueriesImpl(private val conn: Connection) : Queries { } @Throws(SQLException::class) - override fun createVenue(arg: CreateVenueParams): Int { + override fun createVenue( + slug: String, + name: String, + city: String, + spotifyPlaylist: String, + status: Status, + statuses: List, + tags: List): Int { return conn.prepareStatement(createVenue).use { stmt -> - stmt.setString(1, arg.slug) - stmt.setString(2, arg.name) - stmt.setString(3, arg.city) - stmt.setString(4, arg.spotifyPlaylist) - stmt.setObject(5, arg.status.value, Types.OTHER) - stmt.setArray(6, conn.createArrayOf("status", arg.statuses.map { v -> v.value }.toTypedArray())) - stmt.setArray(7, conn.createArrayOf("text", arg.tags.toTypedArray())) + stmt.setString(1, slug) + stmt.setString(2, name) + stmt.setString(3, city) + stmt.setString(4, spotifyPlaylist) + stmt.setObject(5, status.value, Types.OTHER) + stmt.setArray(6, conn.createArrayOf("status", statuses.map { v -> v.value }.toTypedArray())) + stmt.setArray(7, conn.createArrayOf("text", tags.toTypedArray())) val results = stmt.executeQuery() if (!results.next()) { @@ -207,10 +184,10 @@ class QueriesImpl(private val conn: Connection) : Queries { } @Throws(SQLException::class) - override fun getVenue(arg: GetVenueParams): Venue { + override fun getVenue(slug: String, city: String): Venue { return conn.prepareStatement(getVenue).use { stmt -> - stmt.setString(1, arg.slug) - stmt.setString(2, arg.city) + stmt.setString(1, slug) + stmt.setString(2, city) val results = stmt.executeQuery() if (!results.next()) { @@ -277,20 +254,20 @@ class QueriesImpl(private val conn: Connection) : Queries { } @Throws(SQLException::class) - override fun updateCityName(arg: UpdateCityNameParams) { + override fun updateCityName(name: String, slug: String) { conn.prepareStatement(updateCityName).use { stmt -> - stmt.setString(1, arg.name) - stmt.setString(2, arg.slug) + stmt.setString(1, name) + stmt.setString(2, slug) stmt.execute() } } @Throws(SQLException::class) - override fun updateVenueName(arg: UpdateVenueNameParams): Int { + override fun updateVenueName(name: String, slug: String): Int { return conn.prepareStatement(updateVenueName).use { stmt -> - stmt.setString(1, arg.name) - stmt.setString(2, arg.slug) + stmt.setString(1, name) + stmt.setString(2, slug) val results = stmt.executeQuery() if (!results.next()) { diff --git a/examples/kotlin/src/test/kotlin/com/example/authors/QueriesImplTest.kt b/examples/kotlin/src/test/kotlin/com/example/authors/QueriesImplTest.kt index 0a42a0dcc7..93608a1dfc 100644 --- a/examples/kotlin/src/test/kotlin/com/example/authors/QueriesImplTest.kt +++ b/examples/kotlin/src/test/kotlin/com/example/authors/QueriesImplTest.kt @@ -4,12 +4,13 @@ import com.example.dbtest.DbTestExtension import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Test import org.junit.jupiter.api.extension.RegisterExtension -import java.sql.Connection class QueriesImplTest() { companion object { - @JvmField @RegisterExtension val dbtest = DbTestExtension("src/main/resources/authors/schema.sql") + @JvmField + @RegisterExtension + val dbtest = DbTestExtension("src/main/resources/authors/schema.sql") } @Test @@ -19,12 +20,13 @@ class QueriesImplTest() { val initialAuthors = db.listAuthors() assert(initialAuthors.isEmpty()) - val params = CreateAuthorParams( - name = "Brian Kernighan", - bio = "Co-author of The C Programming Language and The Go Programming Language" + val name = "Brian Kernighan" + val bio = "Co-author of The C Programming Language and The Go Programming Language" + val insertedAuthor = db.createAuthor( + name = name, + bio = bio ) - val insertedAuthor = db.createAuthor(params) - val expectedAuthor = Author(insertedAuthor.id, params.name, params.bio) + val expectedAuthor = Author(insertedAuthor.id, name, bio) assertEquals(expectedAuthor, insertedAuthor) val fetchedAuthor = db.getAuthor(insertedAuthor.id) @@ -42,12 +44,10 @@ class QueriesImplTest() { val initialAuthors = db.listAuthors() assert(initialAuthors.isEmpty()) - val params = CreateAuthorParams( - name = "Brian Kernighan", - bio = null - ) - val insertedAuthor = db.createAuthor(params) - val expectedAuthor = Author(insertedAuthor.id, params.name, params.bio) + val name = "Brian Kernighan" + val bio = null + val insertedAuthor = db.createAuthor(name, bio) + val expectedAuthor = Author(insertedAuthor.id, name, bio) assertEquals(expectedAuthor, insertedAuthor) val fetchedAuthor = db.getAuthor(insertedAuthor.id) diff --git a/examples/kotlin/src/test/kotlin/com/example/booktest/postgresql/QueriesImplTest.kt b/examples/kotlin/src/test/kotlin/com/example/booktest/postgresql/QueriesImplTest.kt index f7c54daf7b..d85747431e 100644 --- a/examples/kotlin/src/test/kotlin/com/example/booktest/postgresql/QueriesImplTest.kt +++ b/examples/kotlin/src/test/kotlin/com/example/booktest/postgresql/QueriesImplTest.kt @@ -20,7 +20,6 @@ class QueriesImplTest { // Start a transaction conn.autoCommit = false db.createBook( - CreateBookParams( authorId = author.authorId, isbn = "1", title = "my book title", @@ -28,11 +27,9 @@ class QueriesImplTest { year = 2016, available = OffsetDateTime.now(), tags = listOf() - ) ) val b1 = db.createBook( - CreateBookParams( authorId = author.authorId, isbn = "2", title = "the second book", @@ -40,19 +37,15 @@ class QueriesImplTest { year = 2016, available = OffsetDateTime.now(), tags = listOf("cool", "unique") - ) ) db.updateBook( - UpdateBookParams( bookId = b1.bookId, title = "changed second title", tags = listOf("cool", "disastor") - ) ) val b3 = db.createBook( - CreateBookParams( authorId = author.authorId, isbn = "3", title = "the third book", @@ -60,11 +53,9 @@ class QueriesImplTest { year = 2001, available = OffsetDateTime.now(), tags = listOf("cool") - ) ) db.createBook( - CreateBookParams( authorId = author.authorId, isbn = "4", title = "4th place finisher", @@ -72,7 +63,6 @@ class QueriesImplTest { year = 2011, available = OffsetDateTime.now(), tags = listOf("other") - ) ) // Commit transaction @@ -82,15 +72,13 @@ class QueriesImplTest { // ISBN update fails because parameters are not in sequential order. After changing $N to ?, ordering is lost, // and the parameters are filled into the wrong slots. db.updateBookISBN( - UpdateBookISBNParams( bookId = b3.bookId, isbn = "NEW ISBN", title = "never ever gonna finish, a quatrain", tags = listOf("someother") - ) ) - val books0 = db.booksByTitleYear(BooksByTitleYearParams("my book title", 2016)) + val books0 = db.booksByTitleYear("my book title", 2016) val formatter = DateTimeFormatter.ISO_DATE_TIME for (book in books0) { diff --git a/examples/kotlin/src/test/kotlin/com/example/ondeck/QueriesImplTest.kt b/examples/kotlin/src/test/kotlin/com/example/ondeck/QueriesImplTest.kt index fdd1933862..2ab1ba3ac5 100644 --- a/examples/kotlin/src/test/kotlin/com/example/ondeck/QueriesImplTest.kt +++ b/examples/kotlin/src/test/kotlin/com/example/ondeck/QueriesImplTest.kt @@ -14,13 +14,10 @@ class QueriesImplTest { fun testQueries() { val q = QueriesImpl(dbtest.getConnection()) val city = q.createCity( - CreateCityParams( slug = "san-francisco", name = "San Francisco" - ) ) val venueId = q.createVenue( - CreateVenueParams( slug = "the-fillmore", name = "The Fillmore", city = city.slug, @@ -28,13 +25,10 @@ class QueriesImplTest { status = Status.OPEN, statuses = listOf(Status.OPEN, Status.CLOSED), tags = listOf("rock", "punk") - ) ) val venue = q.getVenue( - GetVenueParams( slug = "the-fillmore", city = city.slug - ) ) assertEquals(venueId, venue.id) @@ -43,8 +37,8 @@ class QueriesImplTest { assertEquals(listOf(city), q.listCities()) assertEquals(listOf(venue), q.listVenues(city.slug)) - q.updateCityName(UpdateCityNameParams(slug = city.slug, name = "SF")) - val id = q.updateVenueName(UpdateVenueNameParams(slug = venue.slug, name = "Fillmore")) + q.updateCityName(slug = city.slug, name = "SF") + val id = q.updateVenueName(slug = venue.slug, name = "Fillmore") assertEquals(venue.id, id) q.deleteVenue(venue.slug) diff --git a/internal/dinosql/kotlin/gen.go b/internal/dinosql/kotlin/gen.go index 6bae4fa240..f30b43ad5f 100644 --- a/internal/dinosql/kotlin/gen.go +++ b/internal/dinosql/kotlin/gen.go @@ -64,13 +64,6 @@ func (v KtQueryValue) isEmpty() bool { return v.Typ == (ktType{}) && v.Name == "" && v.Struct == nil } -func (v KtQueryValue) Pair() string { - if v.isEmpty() { - return "" - } - return v.Name + ": " + v.Type() -} - func (v KtQueryValue) Type() string { if v.Typ != (ktType{}) { return v.Typ.String() @@ -97,23 +90,35 @@ func jdbcSet(t ktType, idx int, name string) string { return fmt.Sprintf("stmt.set%s(%d, %s)", t.Name, idx, name) } -func (v KtQueryValue) Params() string { +type KtParams struct { + Struct *KtStruct +} + +func (v KtParams) isEmpty() bool { + return len(v.Struct.Fields) == 0 +} + +func (v KtParams) Args() string { if v.isEmpty() { return "" } var out []string - if v.Struct == nil { - repeat := 1 - if v.JDBCParamBindCount > 0 { - repeat = v.JDBCParamBindCount - } - for i := 1; i <= repeat; i++ { - out = append(out, jdbcSet(v.Typ, i, v.Name)) - } - } else { - for i, f := range v.Struct.JDBCParamBindings { - out = append(out, jdbcSet(f.Type, i+1, v.Name+"."+f.Name)) - } + for _, f := range v.Struct.Fields { + out = append(out, f.Name+": "+f.Type.String()) + } + if len(out) < 3 { + return strings.Join(out, ", ") + } + return "\n" + indent(strings.Join(out, ",\n"), 6, -1) +} + +func (v KtParams) Bindings() string { + if v.isEmpty() { + return "" + } + var out []string + for i, f := range v.Struct.JDBCParamBindings { + out = append(out, jdbcSet(f.Type, i+1, f.Name)) } return indent(strings.Join(out, "\n"), 6, 0) } @@ -143,7 +148,7 @@ func (v KtQueryValue) ResultSet() string { out = append(out, jdbcGet(f.Type, i+1)) } ret := indent(strings.Join(out, ",\n"), 4, -1) - ret = indent(v.Struct.Name + "(\n" + ret + "\n)", 8, 0) + ret = indent(v.Struct.Name+"(\n"+ret+"\n)", 8, 0) return ret } @@ -177,7 +182,7 @@ type KtQuery struct { SQL string SourceName string Ret KtQueryValue - Arg KtQueryValue + Arg KtParams } type KtGenerateable interface { @@ -221,8 +226,10 @@ func InterfaceKtImports(r KtGenerateable, settings dinosql.CombinedSettings) [][ } } if !q.Arg.isEmpty() { - if strings.HasPrefix(q.Arg.Type(), name) { - return true + for _, f := range q.Arg.Struct.Fields { + if strings.HasPrefix(f.Type.Name, name) { + return true + } } } } @@ -307,16 +314,11 @@ func QueryKtImports(r KtGenerateable, settings dinosql.CombinedSettings, filenam } } if !q.Arg.isEmpty() { - if q.Arg.EmitStruct() { - for _, f := range q.Arg.Struct.Fields { - if f.Type.Name == name { - return true - } + for _, f := range q.Arg.Struct.Fields { + if f.Type.Name == name { + return true } } - if q.Arg.Type() == name { - return true - } } } return false @@ -325,14 +327,8 @@ func QueryKtImports(r KtGenerateable, settings dinosql.CombinedSettings, filenam hasEnum := func() bool { for _, q := range gq { if !q.Arg.isEmpty() { - if q.Arg.IsStruct() { - for _, f := range q.Arg.Struct.Fields { - if f.Type.IsEnum { - return true - } - } - } else { - if q.Arg.Typ.IsEnum { + for _, f := range q.Arg.Struct.Fields { + if f.Type.IsEnum { return true } } @@ -624,18 +620,18 @@ type goColumn struct { core.Column } -func (r Result) ktColumnsToStruct(name string, columns []goColumn, settings dinosql.CombinedSettings) *KtStruct { +func (r Result) ktColumnsToStruct(name string, columns []goColumn, settings dinosql.CombinedSettings, namer func(core.Column, int) string) *KtStruct { gs := KtStruct{ Name: name, } idSeen := map[int]KtField{} nameSeen := map[string]int{} - for i, c := range columns { + for _, c := range columns { if binding, ok := idSeen[c.id]; ok { gs.JDBCParamBindings = append(gs.JDBCParamBindings, binding) continue } - fieldName := KtMemberName(ktColumnName(c.Column, i), settings) + fieldName := KtMemberName(namer(c.Column, c.id), settings) if v := nameSeen[c.Name]; v > 0 { fieldName = fmt.Sprintf("%s_%d", fieldName, v+1) } @@ -663,13 +659,14 @@ func ktArgName(name string) string { return out } -func ktParamName(p dinosql.Parameter) string { - if p.Column.Name != "" { - return ktArgName(p.Column.Name) +func ktParamName(c core.Column, number int) string { + if c.Name != "" { + return ktArgName(c.Name) } - return fmt.Sprintf("dollar_%d", p.Number) + return fmt.Sprintf("dollar_%d", number) } + func ktColumnName(c core.Column, pos int) string { if c.Name != "" { return c.Name @@ -716,20 +713,9 @@ func (r Result) KtQueries(settings dinosql.CombinedSettings) []KtQuery { Column: p.Column, }) } - params := r.ktColumnsToStruct(gq.ClassName+"Params", cols, settings) - if len(params.Fields) == 1 { - p := query.Params[0] - gq.Arg = KtQueryValue{ - Name: ktParamName(p), - Typ: r.ktType(p.Column, settings), - JDBCParamBindCount: len(params.JDBCParamBindings), - } - } else if len(params.Fields) > 1 { - gq.Arg = KtQueryValue{ - Emit: true, - Name: "arg", - Struct: params, - } + params := r.ktColumnsToStruct(gq.ClassName+"Bindings", cols, settings, ktParamName) + gq.Arg = KtParams{ + Struct: params, } if len(query.Columns) == 1 { @@ -771,7 +757,7 @@ func (r Result) KtQueries(settings dinosql.CombinedSettings) []KtQuery { Column: c, }) } - gs = r.ktColumnsToStruct(gq.ClassName+"Row", columns, settings) + gs = r.ktColumnsToStruct(gq.ClassName+"Row", columns, settings, ktColumnName) emit = true } gq.Ret = KtQueryValue{ @@ -800,16 +786,16 @@ interface Queries { {{- range .KtQueries}} @Throws(SQLException::class) {{- if eq .Cmd ":one"}} - fun {{.MethodName}}({{.Arg.Pair}}): {{.Ret.Type}} + fun {{.MethodName}}({{.Arg.Args}}): {{.Ret.Type}} {{- end}} {{- if eq .Cmd ":many"}} - fun {{.MethodName}}({{.Arg.Pair}}): List<{{.Ret.Type}}> + fun {{.MethodName}}({{.Arg.Args}}): List<{{.Ret.Type}}> {{- end}} {{- if eq .Cmd ":exec"}} - fun {{.MethodName}}({{.Arg.Pair}}) + fun {{.MethodName}}({{.Arg.Args}}) {{- end}} {{- if eq .Cmd ":execrows"}} - fun {{.MethodName}}({{.Arg.Pair}}): Int + fun {{.MethodName}}({{.Arg.Args}}): Int {{- end}} {{end}} } @@ -866,14 +852,6 @@ const val {{.ConstantName}} = {{$.Q}}-- name: {{.MethodName}} {{.Cmd}} {{.SQL}} {{$.Q}} -{{if .Arg.EmitStruct}} -data class {{.Arg.Type}} ( {{- range $i, $e := .Arg.Struct.Fields}} - {{- if $i }},{{end}} - val {{.Name}}: {{.Type}} - {{- end}} -) -{{end}} - {{if .Ret.EmitStruct}} data class {{.Ret.Type}} ( {{- range $i, $e := .Ret.Struct.Fields}} {{- if $i }},{{end}} @@ -890,9 +868,9 @@ class QueriesImpl(private val conn: Connection){{ if .EmitInterface }} : Queries {{end}} @Throws(SQLException::class) {{ if $.EmitInterface }}override {{ end -}} - fun {{.MethodName}}({{.Arg.Pair}}): {{.Ret.Type}} { + fun {{.MethodName}}({{.Arg.Args}}): {{.Ret.Type}} { return conn.prepareStatement({{.ConstantName}}).use { stmt -> - {{.Arg.Params}} + {{.Arg.Bindings}} val results = stmt.executeQuery() if (!results.next()) { @@ -912,9 +890,9 @@ class QueriesImpl(private val conn: Connection){{ if .EmitInterface }} : Queries {{end}} @Throws(SQLException::class) {{ if $.EmitInterface }}override {{ end -}} - fun {{.MethodName}}({{.Arg.Pair}}): List<{{.Ret.Type}}> { + fun {{.MethodName}}({{.Arg.Args}}): List<{{.Ret.Type}}> { return conn.prepareStatement({{.ConstantName}}).use { stmt -> - {{.Arg.Params}} + {{.Arg.Bindings}} val results = stmt.executeQuery() val ret = mutableListOf<{{.Ret.Type}}>() @@ -931,9 +909,9 @@ class QueriesImpl(private val conn: Connection){{ if .EmitInterface }} : Queries {{end}} @Throws(SQLException::class) {{ if $.EmitInterface }}override {{ end -}} - fun {{.MethodName}}({{.Arg.Pair}}) { + fun {{.MethodName}}({{.Arg.Args}}) { conn.prepareStatement({{.ConstantName}}).use { stmt -> - {{ .Arg.Params }} + {{ .Arg.Bindings }} stmt.execute() } @@ -945,9 +923,9 @@ class QueriesImpl(private val conn: Connection){{ if .EmitInterface }} : Queries {{end}} @Throws(SQLException::class) {{ if $.EmitInterface }}override {{ end -}} - fun {{.MethodName}}({{.Arg.Pair}}): Int { + fun {{.MethodName}}({{.Arg.Args}}): Int { return conn.prepareStatement({{.ConstantName}}).use { stmt -> - {{ .Arg.Params }} + {{ .Arg.Bindings }} stmt.execute() stmt.updateCount