Skip to content

Commit 2271061

Browse files
authored
Support multi-line SQL comments (#345)
1 parent fe82f35 commit 2271061

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

internal/dinosql/gen.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,7 @@ func (r Result) columnsToStruct(name string, columns []goColumn, settings config
762762
if o, ok := suffixes[c.id]; ok {
763763
suffix = o
764764
} else if v := seen[c.Name]; v > 0 {
765-
suffix = v+1
765+
suffix = v + 1
766766
}
767767
suffixes[c.id] = suffix
768768
if suffix > 0 {
@@ -1065,7 +1065,7 @@ import (
10651065
)
10661066
10671067
{{range .Enums}}
1068-
{{if .Comment}}// {{.Comment}}{{end}}
1068+
{{if .Comment}}{{comment .Comment}}{{end}}
10691069
type {{.Name}} string
10701070
10711071
const (
@@ -1081,7 +1081,7 @@ func (e *{{.Name}}) Scan(src interface{}) error {
10811081
{{end}}
10821082
10831083
{{range .Structs}}
1084-
{{if .Comment}}// {{.Comment}}{{end}}
1084+
{{if .Comment}}{{comment .Comment}}{{end}}
10851085
type {{.Name}} struct { {{- range .Fields}}
10861086
{{- if .Comment}}
10871087
// {{.Comment}}{{else}}
@@ -1224,9 +1224,14 @@ func LowerTitle(s string) string {
12241224
return string(a)
12251225
}
12261226

1227+
func DoubleSlashComment(s string) string {
1228+
return "// " + strings.ReplaceAll(s, "\n", "\n// ")
1229+
}
1230+
12271231
func Generate(r Generateable, settings config.CombinedSettings) (map[string]string, error) {
12281232
funcMap := template.FuncMap{
12291233
"lowerTitle": LowerTitle,
1234+
"comment": DoubleSlashComment,
12301235
"imports": Imports(r, settings),
12311236
}
12321237

internal/dinosql/kotlin/gen.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,6 @@ func ktParamName(c core.Column, number int) string {
667667
return fmt.Sprintf("dollar_%d", number)
668668
}
669669

670-
671670
func ktColumnName(c core.Column, pos int) string {
672671
if c.Name != "" {
673672
return c.Name
@@ -812,7 +811,7 @@ package {{.Package}}
812811
{{end}}
813812
814813
{{range .Enums}}
815-
{{if .Comment}}// {{.Comment}}{{end}}
814+
{{if .Comment}}{{comment .Comment}}{{end}}
816815
enum class {{.Name}}(val value: String) {
817816
{{- range $i, $e := .Constants}}
818817
{{- if $i }},{{end}}
@@ -827,7 +826,7 @@ enum class {{.Name}}(val value: String) {
827826
{{end}}
828827
829828
{{range .KtDataClasses}}
830-
{{if .Comment}}// {{.Comment}}{{end}}
829+
{{if .Comment}}{{comment .Comment}}{{end}}
831830
data class {{.Name}} ( {{- range $i, $e := .Fields}}
832831
{{- if $i }},{{end}}
833832
{{- if .Comment}}
@@ -974,6 +973,7 @@ func ktFormat(s string) string {
974973
func KtGenerate(r KtGenerateable, settings config.CombinedSettings) (map[string]string, error) {
975974
funcMap := template.FuncMap{
976975
"lowerTitle": dinosql.LowerTitle,
976+
"comment": dinosql.DoubleSlashComment,
977977
"imports": KtImports(r, settings),
978978
"offset": Offset,
979979
}
@@ -984,12 +984,12 @@ func KtGenerate(r KtGenerateable, settings config.CombinedSettings) (map[string]
984984

985985
pkg := settings.Package
986986
tctx := ktTmplCtx{
987-
Settings: settings.Global,
988-
Q: `"""`,
989-
Package: pkg.Gen.Kotlin.Package,
990-
KtQueries: r.KtQueries(settings),
991-
Enums: r.KtEnums(settings),
992-
KtDataClasses: r.KtDataClasses(settings),
987+
Settings: settings.Global,
988+
Q: `"""`,
989+
Package: pkg.Gen.Kotlin.Package,
990+
KtQueries: r.KtQueries(settings),
991+
Enums: r.KtEnums(settings),
992+
KtDataClasses: r.KtDataClasses(settings),
993993
}
994994

995995
output := map[string]string{}
@@ -1014,8 +1014,8 @@ func KtGenerate(r KtGenerateable, settings config.CombinedSettings) (map[string]
10141014
return nil, err
10151015
}
10161016
if err := execute("Queries.kt", ifaceFile); err != nil {
1017-
return nil, err
1018-
}
1017+
return nil, err
1018+
}
10191019
if err := execute("QueriesImpl.kt", sqlFile); err != nil {
10201020
return nil, err
10211021
}

0 commit comments

Comments
 (0)