Skip to content

Commit 253a45a

Browse files
committed
1 parent 8af8f79 commit 253a45a

File tree

7 files changed

+58
-14
lines changed

7 files changed

+58
-14
lines changed

internal/gen.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ type tmplCtx struct {
3939
EmitAllEnumValues bool
4040
UsesCopyFrom bool
4141
UsesBatch bool
42+
OmitSqlcVersion bool
4243
BuildTags string
4344
}
4445

@@ -185,6 +186,7 @@ func generate(req *plugin.GenerateRequest, options *opts.Options, enums []Enum,
185186
Structs: structs,
186187
SqlcVersion: req.SqlcVersion,
187188
BuildTags: options.BuildTags,
189+
OmitSqlcVersion: options.OmitSqlcVersion,
188190
}
189191

190192
if tctx.UsesCopyFrom && !tctx.SQLDriver.IsPGX() && options.SqlDriver != SQLDriverGoSQLDriverMySQL {

internal/go_type.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func goInnerType(req *plugin.GenerateRequest, options *opts.Options, col *plugin
8585
case "postgresql":
8686
return postgresType(req, options, col)
8787
case "sqlite":
88-
return sqliteType(req, col)
88+
return sqliteType(req, options, col)
8989
default:
9090
return "interface{}"
9191
}

internal/imports.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ func (i *importer) queryImports(filename string) fileImports {
390390
}
391391

392392
sqlpkg := parseDriver(i.Options.SqlPackage)
393-
if sqlcSliceScan() {
393+
if sqlcSliceScan() && !sqlpkg.IsPGX() {
394394
std["strings"] = struct{}{}
395395
}
396396
if sliceScan() && !sqlpkg.IsPGX() {

internal/opts/options.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type Options struct {
2424
EmitPointersForNullTypes bool `json:"emit_pointers_for_null_types" yaml:"emit_pointers_for_null_types"`
2525
EmitEnumValidMethod bool `json:"emit_enum_valid_method,omitempty" yaml:"emit_enum_valid_method"`
2626
EmitAllEnumValues bool `json:"emit_all_enum_values,omitempty" yaml:"emit_all_enum_values"`
27+
EmitSqlAsComment bool `json:"emit_sql_as_comment,omitempty" yaml:"emit_sql_as_comment"`
2728
JsonTagsCaseStyle string `json:"json_tags_case_style,omitempty" yaml:"json_tags_case_style"`
2829
Package string `json:"package" yaml:"package"`
2930
Out string `json:"out" yaml:"out"`
@@ -34,11 +35,12 @@ type Options struct {
3435
OutputBatchFileName string `json:"output_batch_file_name,omitempty" yaml:"output_batch_file_name"`
3536
OutputDbFileName string `json:"output_db_file_name,omitempty" yaml:"output_db_file_name"`
3637
OutputModelsFileName string `json:"output_models_file_name,omitempty" yaml:"output_models_file_name"`
37-
OutputQuerierFileName string `json:"output_querier_file_name,omitempty" yaml:"output_queries_file_name"`
38+
OutputQuerierFileName string `json:"output_querier_file_name,omitempty" yaml:"output_querier_file_name"`
3839
OutputCopyfromFileName string `json:"output_copyfrom_file_name,omitempty" yaml:"output_copyfrom_file_name"`
3940
OutputFilesSuffix string `json:"output_files_suffix,omitempty" yaml:"output_files_suffix"`
4041
InflectionExcludeTableNames []string `json:"inflection_exclude_table_names,omitempty" yaml:"inflection_exclude_table_names"`
4142
QueryParameterLimit *int32 `json:"query_parameter_limit,omitempty" yaml:"query_parameter_limit"`
43+
OmitSqlcVersion bool `json:"omit_sqlc_version,omitempty" yaml:"omit_sqlc_version"`
4244
OmitUnusedStructs bool `json:"omit_unused_structs,omitempty" yaml:"omit_unused_structs"`
4345
BuildTags string `json:"build_tags,omitempty" yaml:"build_tags"`
4446
}

internal/result.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package golang
22

33
import (
4+
"bufio"
45
"fmt"
56
"sort"
67
"strings"
@@ -199,14 +200,30 @@ func buildQueries(req *plugin.GenerateRequest, options *opts.Options, structs []
199200
constantName = sdk.LowerTitle(query.Name)
200201
}
201202

203+
comments := query.Comments
204+
if options.EmitSqlAsComment {
205+
if len(comments) == 0 {
206+
comments = append(comments, query.Name)
207+
}
208+
comments = append(comments, " ")
209+
scanner := bufio.NewScanner(strings.NewReader(query.Text))
210+
for scanner.Scan() {
211+
line := scanner.Text()
212+
comments = append(comments, " "+line)
213+
}
214+
if err := scanner.Err(); err != nil {
215+
return nil, err
216+
}
217+
}
218+
202219
gq := Query{
203220
Cmd: query.Cmd,
204221
ConstantName: constantName,
205222
FieldName: sdk.LowerTitle(query.Name) + "Stmt",
206223
MethodName: query.Name,
207224
SourceName: query.Filename,
208225
SQL: query.Text,
209-
Comments: query.Comments,
226+
Comments: comments,
210227
Table: query.InsertIntoTable,
211228
}
212229
sqlpkg := parseDriver(options.SqlPackage)

internal/sqlite_type.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,23 @@ import (
77
"github.com/sqlc-dev/plugin-sdk-go/plugin"
88
"github.com/sqlc-dev/plugin-sdk-go/sdk"
99
"github.com/sqlc-dev/sqlc-gen-go/internal/debug"
10+
"github.com/sqlc-dev/sqlc-gen-go/internal/opts"
1011
)
1112

12-
func sqliteType(req *plugin.GenerateRequest, col *plugin.Column) string {
13+
func sqliteType(req *plugin.GenerateRequest, options *opts.Options, col *plugin.Column) string {
1314
dt := strings.ToLower(sdk.DataType(col.Type))
1415
notNull := col.NotNull || col.IsArray
16+
emitPointersForNull := options.EmitPointersForNullTypes
1517

1618
switch dt {
1719

1820
case "int", "integer", "tinyint", "smallint", "mediumint", "bigint", "unsignedbigint", "int2", "int8":
1921
if notNull {
2022
return "int64"
2123
}
24+
if emitPointersForNull {
25+
return "*int64"
26+
}
2227
return "sql.NullInt64"
2328

2429
case "blob":
@@ -28,18 +33,27 @@ func sqliteType(req *plugin.GenerateRequest, col *plugin.Column) string {
2833
if notNull {
2934
return "float64"
3035
}
36+
if emitPointersForNull {
37+
return "*float64"
38+
}
3139
return "sql.NullFloat64"
3240

3341
case "boolean", "bool":
3442
if notNull {
3543
return "bool"
3644
}
45+
if emitPointersForNull {
46+
return "*bool"
47+
}
3748
return "sql.NullBool"
3849

3950
case "date", "datetime", "timestamp":
4051
if notNull {
4152
return "time.Time"
4253
}
54+
if emitPointersForNull {
55+
return "*time.Time"
56+
}
4357
return "sql.NullTime"
4458

4559
case "any":
@@ -60,12 +74,18 @@ func sqliteType(req *plugin.GenerateRequest, col *plugin.Column) string {
6074
if notNull {
6175
return "string"
6276
}
77+
if emitPointersForNull {
78+
return "*string"
79+
}
6380
return "sql.NullString"
6481

6582
case strings.HasPrefix(dt, "decimal"), dt == "numeric":
6683
if notNull {
6784
return "float64"
6885
}
86+
if emitPointersForNull {
87+
return "*float64"
88+
}
6989
return "sql.NullFloat64"
7090

7191
default:

internal/templates/template.tmpl

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
//go:build {{.BuildTags}}
44

55
{{end}}// Code generated by sqlc. DO NOT EDIT.
6-
// versions:
6+
{{if not .OmitSqlcVersion}}// versions:
77
// sqlc {{.SqlcVersion}}
8+
{{end}}
89

910
package {{.Package}}
1011

@@ -33,8 +34,9 @@ import (
3334
//go:build {{.BuildTags}}
3435

3536
{{end}}// Code generated by sqlc. DO NOT EDIT.
36-
// versions:
37+
{{if not .OmitSqlcVersion}}// versions:
3738
// sqlc {{.SqlcVersion}}
39+
{{end}}
3840

3941
package {{.Package}}
4042

@@ -61,8 +63,9 @@ import (
6163
//go:build {{.BuildTags}}
6264

6365
{{end}}// Code generated by sqlc. DO NOT EDIT.
64-
// versions:
66+
{{if not .OmitSqlcVersion}}// versions:
6567
// sqlc {{.SqlcVersion}}
68+
{{end}}
6669

6770
package {{.Package}}
6871

@@ -158,9 +161,9 @@ type {{.Name}} struct { {{- range .Fields}}
158161
//go:build {{.BuildTags}}
159162

160163
{{end}}// Code generated by sqlc. DO NOT EDIT.
161-
// versions:
164+
{{if not .OmitSqlcVersion}}// versions:
162165
// sqlc {{.SqlcVersion}}
163-
// source: {{.SourceName}}
166+
{{end}}// source: {{.SourceName}}
164167

165168
package {{.Package}}
166169

@@ -187,9 +190,9 @@ import (
187190
//go:build {{.BuildTags}}
188191

189192
{{end}}// Code generated by sqlc. DO NOT EDIT.
190-
// versions:
193+
{{if not .OmitSqlcVersion}}// versions:
191194
// sqlc {{.SqlcVersion}}
192-
// source: {{.SourceName}}
195+
{{end}}// source: {{.SourceName}}
193196

194197
package {{.Package}}
195198

@@ -216,9 +219,9 @@ import (
216219
//go:build {{.BuildTags}}
217220

218221
{{end}}// Code generated by sqlc. DO NOT EDIT.
219-
// versions:
222+
{{if not .OmitSqlcVersion}}// versions:
220223
// sqlc {{.SqlcVersion}}
221-
// source: {{.SourceName}}
224+
{{end}}// source: {{.SourceName}}
222225

223226
package {{.Package}}
224227

0 commit comments

Comments
 (0)