Skip to content
Merged
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions internal/codegen/golang/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,12 +344,12 @@ func (i *importer) queryImports(filename string) fileImports {
if !q.Arg.isEmpty() {
if q.Arg.IsStruct() {
for _, f := range q.Arg.Struct.Fields {
if strings.HasPrefix(f.Type, "[]") && f.Type != "[]byte" {
if strings.HasPrefix(f.Type, "[]") && f.Type != "[]byte" && !f.HasSqlcSlice() {
return true
}
}
} else {
if strings.HasPrefix(q.Arg.Type(), "[]") && q.Arg.Type() != "[]byte" {
if strings.HasPrefix(q.Arg.Type(), "[]") && q.Arg.Type() != "[]byte" && !q.Arg.HasSqlcSlices() {
return true
}
}
Expand All @@ -375,7 +375,8 @@ func (i *importer) queryImports(filename string) fileImports {
sqlpkg := parseDriver(i.Settings.Go.SqlPackage)
if sqlcSliceScan() {
std["strings"] = struct{}{}
} else if sliceScan() && !sqlpkg.IsPGX() {
}
if sliceScan() && !sqlpkg.IsPGX() {
pkg[ImportSpec{Path: "github.com/lib/pq"}] = struct{}{}
}

Expand Down
16 changes: 12 additions & 4 deletions internal/codegen/golang/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,9 @@ func (v QueryValue) Params() string {
} else {
for _, f := range v.Struct.Fields {
if !f.HasSqlcSlice() && strings.HasPrefix(f.Type, "[]") && f.Type != "[]byte" && !v.SQLDriver.IsPGX() {
out = append(out, "pq.Array("+v.Name+"."+f.Name+")")
} else if !v.EmitStruct() && v.IsStruct() {
out = append(out, toLowerCase(f.Name))
out = append(out, "pq.Array("+v.VariableForField(f)+")")
} else {
out = append(out, v.Name+"."+f.Name)
out = append(out, v.VariableForField(f))
}
}
}
Expand Down Expand Up @@ -189,6 +187,16 @@ func (v QueryValue) Scan() string {
return "\n" + strings.Join(out, ",\n")
}

func (v QueryValue) VariableForField(f Field) string {
if !v.IsStruct() {
return v.Name
}
if !v.EmitStruct() {
return toLowerCase(f.Name)
}
return v.Name + "." + f.Name
}

// A struct used to generate methods and fields on the Queries struct
type Query struct {
Cmd string
Expand Down
8 changes: 4 additions & 4 deletions internal/codegen/golang/templates/stdlib/queryCode.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,16 @@ func (q *Queries) {{.MethodName}}(ctx context.Context, {{ dbarg }} {{.Arg.Pair}}
{{- $arg := .Arg }}
{{- range .Arg.Struct.Fields }}
{{- if .HasSqlcSlice }}
if len({{$arg.Name}}.{{.Name}}) > 0 {
for _, v := range {{$arg.Name}}.{{.Name}} {
if len({{$arg.VariableForField .}}) > 0 {
for _, v := range {{$arg.VariableForField .}} {
queryParams = append(queryParams, v)
}
query = strings.Replace(query, "/*SLICE:{{.Column.Name}}*/?", strings.Repeat(",?", len({{$arg.Name}}.{{.Name}}))[1:], 1)
query = strings.Replace(query, "/*SLICE:{{.Column.Name}}*/?", strings.Repeat(",?", len({{$arg.VariableForField .}}))[1:], 1)
} else {
query = strings.Replace(query, "/*SLICE:{{.Column.Name}}*/?", "NULL", 1)
}
{{- else }}
queryParams = append(queryParams, {{$arg.Name}}.{{.Name}})
queryParams = append(queryParams, {{$arg.VariableForField .}})
{{- end }}
{{- end }}
{{- else }}
Expand Down
6 changes: 6 additions & 0 deletions internal/compiler/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
DataType: dataType,
IsNamedParam: isNamed,
NotNull: p.NotNull(),
IsSqlcSlice: p.IsSqlcSlice(),
},
})
continue
Expand Down Expand Up @@ -220,6 +221,7 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
Length: c.Length,
Table: table,
IsNamedParam: isNamed,
IsSqlcSlice: p.IsSqlcSlice(),
},
})
}
Expand Down Expand Up @@ -284,6 +286,7 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
IsArray: c.IsArray,
Table: table,
IsNamedParam: isNamed,
IsSqlcSlice: p.IsSqlcSlice(),
},
})
}
Expand Down Expand Up @@ -352,6 +355,7 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
DataType: "any",
IsNamedParam: isNamed,
NotNull: p.NotNull(),
IsSqlcSlice: p.IsSqlcSlice(),
},
})
continue
Expand Down Expand Up @@ -392,6 +396,7 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
DataType: dataType(paramType),
NotNull: p.NotNull(),
IsNamedParam: isNamed,
IsSqlcSlice: p.IsSqlcSlice(),
},
})
}
Expand Down Expand Up @@ -457,6 +462,7 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
Table: &ast.TableName{Schema: schema, Name: rel},
Length: c.Length,
IsNamedParam: isNamed,
IsSqlcSlice: p.IsSqlcSlice(),
},
})
} else {
Expand Down

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

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

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ CREATE TABLE authors (
id BIGSERIAL PRIMARY KEY,
name text NOT NULL,
bio text,
country_code CHAR(2) NOT NULL
country_code CHAR(2) NOT NULL,
titles TEXT[]
);

-- name: GetAuthor :one
Expand All @@ -16,12 +17,19 @@ ORDER BY name;

-- name: CreateAuthor :one
INSERT INTO authors (
name, bio, country_code
name, bio, country_code, titles
) VALUES (
$1, $2, $3
$1, $2, $3, $4
)
RETURNING *;

-- name: DeleteAuthor :exec
DELETE FROM authors
WHERE id = $1;

-- name: DeleteAuthors :exec
DELETE FROM authors
WHERE id IN (sqlc.slice(ids)) AND name = $1;

-- name: CreateAuthorOnlyTitles :one
INSERT INTO authors (name, titles) VALUES ($1, $2) RETURNING *;

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