Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
8 changes: 7 additions & 1 deletion examples/authors/ydb/query.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions internal/codegen/golang/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ func generate(req *plugin.GenerateRequest, options *opts.Options, enums []Enum,
}
}

isYDBEngine := func() bool { return options.Engine == "ydb" }

funcMap := template.FuncMap{
"lowerTitle": sdk.LowerTitle,
"comment": sdk.DoubleSlashComment,
Expand All @@ -232,6 +234,7 @@ func generate(req *plugin.GenerateRequest, options *opts.Options, enums []Enum,
"emitPreparedQueries": tctx.codegenEmitPreparedQueries,
"queryMethod": tctx.codegenQueryMethod,
"queryRetval": tctx.codegenQueryRetval,
"isYDBEngine": isYDBEngine,
}

tmpl := template.Must(
Expand Down
16 changes: 14 additions & 2 deletions internal/codegen/golang/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,10 @@ func buildImports(options *opts.Options, queries []Query, uses func(string) bool
if uses("pgvector.Vector") && !overrideVector {
pkg[ImportSpec{Path: "github.com/pgvector/pgvector-go"}] = struct{}{}
}
_, overrideDecimal := overrideTypes["types.Decimal"]
if uses("types.Decimal") && !overrideDecimal {
pkg[ImportSpec{Path: "github.com/ydb-platform/ydb-go-sdk/v3/table/types"}] = struct{}{}
}

// Custom imports
for _, override := range options.Overrides {
Expand Down Expand Up @@ -404,7 +408,7 @@ func (i *importer) queryImports(filename string) fileImports {
}

sqlpkg := parseDriver(i.Options.SqlPackage)
if sqlcSliceScan() && !sqlpkg.IsPGX() && !sqlpkg.IsYDBGoSDK() {
if sqlcSliceScan() && !sqlpkg.IsPGX() && !sqlpkg.IsYDBGoSDK() && i.Options.Engine != "ydb" {
std["strings"] = struct{}{}
}
if sliceScan() && !sqlpkg.IsPGX() && !sqlpkg.IsYDBGoSDK() {
Expand All @@ -413,17 +417,25 @@ func (i *importer) queryImports(filename string) fileImports {

if sqlpkg.IsYDBGoSDK() {
hasParams := false
hasManyQueries := false
for _, q := range gq {
if !q.Arg.isEmpty() {
hasParams = true
break
}
if q.Cmd == metadata.CmdMany {
hasManyQueries = true
}
}
if hasParams {
pkg[ImportSpec{Path: "github.com/ydb-platform/ydb-go-sdk/v3"}] = struct{}{}
}
pkg[ImportSpec{Path: "github.com/ydb-platform/ydb-go-sdk/v3/query"}] = struct{}{}
pkg[ImportSpec{Path: "github.com/ydb-platform/ydb-go-sdk/v3/pkg/xerrors"}] = struct{}{}

if hasManyQueries {
std["errors"] = struct{}{}
std["io"] = struct{}{}
}
}

if i.Options.WrapErrors {
Expand Down
6 changes: 6 additions & 0 deletions internal/codegen/golang/opts/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type Options struct {
Initialisms *[]string `json:"initialisms,omitempty" yaml:"initialisms"`

InitialismsMap map[string]struct{} `json:"-" yaml:"-"`
Engine string `json:"-" yaml:"-"`
}

type GlobalOptions struct {
Expand All @@ -72,6 +73,11 @@ func Parse(req *plugin.GenerateRequest) (*Options, error) {
}
maps.Copy(options.Rename, global.Rename)
}

if req.Settings != nil {
options.Engine = req.Settings.Engine
}

return options, nil
}

Expand Down
27 changes: 26 additions & 1 deletion internal/codegen/golang/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ func ydbBuilderMethodForColumnType(dbType string) string {
return "Int32"
case "uint16":
return "Uint16"
case "int16", "smallserial","serial2":
case "int16", "smallserial", "serial2":
return "Int16"
case "uint8":
return "Uint8"
Expand Down Expand Up @@ -342,8 +342,18 @@ func ydbBuilderMethodForColumnType(dbType string) string {
case "yson":
return "YSON"

case "integer": // LIMIT/OFFSET parameters support
return "Uint64"

case "Integer": // Invalid YDB type - fallback to Uint64
return "Uint64"

//TODO: support other types
default:
// Check for decimal types
if strings.HasPrefix(baseType, "decimal") {
return "Decimal"
}
return ""
}
}
Expand Down Expand Up @@ -405,6 +415,21 @@ func (v QueryValue) YDBParamsBuilder() string {
isPtr := strings.HasPrefix(goType, "*")
isArray := field.Column.IsArray || field.Column.IsSqlcSlice

if method == "Decimal" {
if isArray {
lines = append(lines, fmt.Sprintf("\tvar list = parameters.Param(%s).BeginList()", paramName))
lines = append(lines, fmt.Sprintf("\tfor _, param := range %s {", variable))
lines = append(lines, "\t\tlist = list.Add().Decimal(param.Bytes, param.Precision, param.Scale)")
lines = append(lines, "\t}")
lines = append(lines, "\tparameters = list.EndList()")
} else if isPtr {
lines = append(lines, fmt.Sprintf("\tparameters = parameters.Param(%s).BeginOptional().Decimal(&%s.Bytes, %s.Precision, %s.Scale).EndOptional()", paramName, variable, variable, variable))
} else {
lines = append(lines, fmt.Sprintf("\tparameters = parameters.Param(%s).Decimal(%s.Bytes, %s.Precision, %s.Scale)", paramName, variable, variable, variable))
}
return true
}

if isArray {
lines = append(lines, fmt.Sprintf("\tvar list = parameters.Param(%s).BeginList()", paramName))
lines = append(lines, fmt.Sprintf("\tfor _, param := range %s {", variable))
Expand Down
2 changes: 1 addition & 1 deletion internal/codegen/golang/templates/stdlib/queryCode.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (q *Queries) {{.MethodName}}(ctx context.Context, {{ dbarg }} {{.Arg.Pair}}
{{end}}

{{define "queryCodeStdExec"}}
{{- if .Arg.HasSqlcSlices }}
{{- if and (not isYDBEngine) .Arg.HasSqlcSlices }}
query := {{.ConstantName}}
var queryParams []interface{}
{{- if .Arg.Struct }}
Expand Down
6 changes: 5 additions & 1 deletion internal/codegen/golang/templates/ydb-go-sdk/queryCode.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,11 @@ func (q *Queries) {{.MethodName}}(ctx context.Context, {{if $.EmitMethodsWithDBA
{{else}}
var items []{{.Ret.DefineType}}
{{end -}}
for row, err := range result.Rows(ctx) {
for {
row, err := result.NextRow(ctx)
if errors.Is(err, io.EOF) {
break
}
if err != nil {
{{- if $.WrapErrors}}
return nil, xerrors.WithStackTrace(fmt.Errorf("query {{.MethodName}}: %w", err))
Expand Down
32 changes: 31 additions & 1 deletion internal/codegen/golang/ydb_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func YDBType(req *plugin.GenerateRequest, options *opts.Options, col *plugin.Col
}
return "*string"

case "date", "date32", "datetime", "timestamp", "tzdate", "tztimestamp", "tzdatetime":
case "date", "date32", "datetime", "timestamp", "tzdate", "tztimestamp", "tzdatetime", "datetime64", "timestamp64", "tzdate32", "tzdatetime64", "tztimestamp64":
if notNull {
return "time.Time"
}
Expand All @@ -172,6 +172,15 @@ func YDBType(req *plugin.GenerateRequest, options *opts.Options, col *plugin.Col
}
return "*time.Time"

case "interval", "interval64":
if notNull {
return "time.Duration"
}
if emitPointersForNull {
return "*time.Duration"
}
return "*time.Duration"

case "uuid":
if notNull {
return "uuid.UUID"
Expand All @@ -197,7 +206,28 @@ func YDBType(req *plugin.GenerateRequest, options *opts.Options, col *plugin.Col
case "any":
return "interface{}"

case "integer":
// integer type is used for LIMIT/OFFSET parameters - use uint64 for semantic correctness
if notNull {
return "uint64"
}
if emitPointersForNull {
return "*uint64"
}
return "*uint64"

default:
if strings.HasPrefix(columnType, "decimal") {
if notNull {
return "types.Decimal"
}

if emitPointersForNull {
return "*types.Decimal"
}
return "*types.Decimal"
}

if debug.Active {
log.Printf("unknown YDB type: %s\n", columnType)
}
Expand Down
31 changes: 31 additions & 0 deletions internal/endtoend/testdata/alias/ydb/stdlib/go/db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions internal/endtoend/testdata/alias/ydb/stdlib/go/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions internal/endtoend/testdata/alias/ydb/stdlib/go/query.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions internal/endtoend/testdata/alias/ydb/stdlib/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- name: AliasBar :one
SELECT * FROM bar AS b
WHERE b.id = $i;



1 change: 1 addition & 0 deletions internal/endtoend/testdata/alias/ydb/stdlib/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE TABLE bar (id Int32 NOT NULL, PRIMARY KEY (id));
12 changes: 12 additions & 0 deletions internal/endtoend/testdata/alias/ydb/stdlib/sqlc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": "1",
"packages": [
{
"path": "go",
"engine": "ydb",
"name": "querytest",
"schema": "schema.sql",
"queries": "query.sql"
}
]
}
26 changes: 26 additions & 0 deletions internal/endtoend/testdata/alias/ydb/ydb-go-sdk/go/db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions internal/endtoend/testdata/alias/ydb/ydb-go-sdk/go/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions internal/endtoend/testdata/alias/ydb/ydb-go-sdk/go/query.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions internal/endtoend/testdata/alias/ydb/ydb-go-sdk/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- name: AliasBar :one
SELECT * FROM bar AS b
WHERE b.id = $i;



Loading
Loading