Skip to content

Commit

Permalink
Merge pull request #146 from morikuni/master
Browse files Browse the repository at this point in the history
Ignore HIDDEN columns by default
  • Loading branch information
sinmetal authored Dec 3, 2024
2 parents 50a38af + 4dd65c7 commit 44c1d63
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 8 deletions.
31 changes: 27 additions & 4 deletions v2/generator/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ func (a *Generator) newTemplateFuncs() template.FuncMap {
"shortName": a.shortName,
"nullcheck": a.nullcheck,

"hasColumn": a.hasColumn,
"columnNames": a.columnNames,
"columnNamesQuery": a.columnNamesQuery,
"columnPrefixNames": a.columnPrefixNames,
"hasColumn": a.hasColumn,
"columnNames": a.columnNames,
"columnNamesWithoutHidden": a.columnNamesWithoutHidden,
"columnNamesQuery": a.columnNamesQuery,
"columnPrefixNames": a.columnPrefixNames,

"hasField": a.hasField,
"fieldNames": a.fieldNames,
Expand Down Expand Up @@ -108,6 +109,28 @@ func (a *Generator) columnNames(fields []*models.Field) string {
return str
}

// columnNamesWithoutHidden creates a list of the column names found in fields
// excluding any models.Field with IsHidden set to true.
//
// Used to present a comma separated list of column names, that can be used in
// a SELECT, or UPDATE, or other SQL clause requiring a list of identifiers
// (ie, "field_1, field_2, field_3, ...").
func (a *Generator) columnNamesWithoutHidden(fields []*models.Field) string {
str := ""
i := 0
for _, f := range fields {
if f.IsHidden {
continue
}
if i != 0 {
str = str + ", "
}
str = str + internal.EscapeColumnName(f.ColumnName)
i++
}
return str
}

// columnNamesQuery creates a list of the column names in fields as a query and
// joined by sep, excluding any models.Field with Name contained in ignoreNames.
//
Expand Down
3 changes: 2 additions & 1 deletion v2/loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ func (tl *TypeLoader) LoadColumns(typeTpl *models.Type) error {
columnSet[column.ColumnName] = struct{}{}
}

for k, _ := range columnTypes {
for k := range columnTypes {
if _, ok := columnSet[k]; !ok {
return fmt.Errorf("unknown custom type column %s in the table %s", k, typeTpl.TableName)
}
Expand Down Expand Up @@ -282,6 +282,7 @@ func (tl *TypeLoader) LoadColumns(typeTpl *models.Type) error {
IsNotNull: c.NotNull,
IsPrimaryKey: c.IsPrimaryKey,
IsGenerated: c.IsGenerated,
IsHidden: c.IsHidden,
}

// set custom type
Expand Down
2 changes: 2 additions & 0 deletions v2/loader/schema_parser_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (

"github.com/cloudspannerecosystem/memefish"
"github.com/cloudspannerecosystem/memefish/ast"
"github.com/cloudspannerecosystem/memefish/token"
)

func extractName(path *ast.Path) (string, error) {
Expand Down Expand Up @@ -150,6 +151,7 @@ func (s *schemaParserSource) ColumnList(name string) ([]*SpannerColumn, error) {
NotNull: c.NotNull,
IsPrimaryKey: pk,
IsGenerated: c.GeneratedExpr != nil,
IsHidden: c.Hidden != token.InvalidPos,
})
}

Expand Down
1 change: 1 addition & 0 deletions v2/loader/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type SpannerColumn struct {
NotNull bool // not_null
IsPrimaryKey bool // is_primary_key
IsGenerated bool // is_generated
IsHidden bool // is_hidden
}

// SpannerIndex represents an index.
Expand Down
1 change: 1 addition & 0 deletions v2/models/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type Field struct {
IsNotNull bool // not_null
IsPrimaryKey bool // is_primary_key
IsGenerated bool // is_generated
IsHidden bool // is_hidden
}

// Index is a template item for a index into a table.
Expand Down
4 changes: 2 additions & 2 deletions v2/module/builtin/templates/index.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ func Find{{ .FuncName }}(ctx context.Context, db YODB{{ goParams .Fields true tr
{{- end }}
{{- if not .NullableFields }}
const sqlstr = "SELECT " +
"{{ columnNames .Type.Fields }} " +
"{{ columnNamesWithoutHidden .Type.Fields }} " +
"FROM {{ $table }}@{FORCE_INDEX={{ .IndexName }}} " +
"WHERE {{ columnNamesQuery .Fields " AND " }}"
{{- else }}
var sqlstr = "SELECT " +
"{{ columnNames .Type.Fields }} " +
"{{ columnNamesWithoutHidden .Type.Fields }} " +
"FROM {{ $table }}@{FORCE_INDEX={{ .IndexName }}} "

conds := make([]string, {{ len .Fields }})
Expand Down
9 changes: 8 additions & 1 deletion v2/module/builtin/templates/type.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
// {{ .Name }} represents a row from '{{ $table }}'.
type {{ .Name }} struct {
{{- range .Fields }}
{{- if eq (.SpannerDataType) (.ColumnName) }}
{{- if .IsHidden }}
{{- else if eq (.SpannerDataType) (.ColumnName) }}
{{ .Name }} string `spanner:"{{ .ColumnName }}" json:"{{ .ColumnName }}"` // {{ .ColumnName }} enum
{{- else }}
{{ .Name }} {{ .Type }} `spanner:"{{ .ColumnName }}" json:"{{ .ColumnName }}"` // {{ .ColumnName }}
Expand All @@ -23,7 +24,9 @@ func {{ .Name }}PrimaryKeys() []string {
func {{ .Name }}Columns() []string {
return []string{
{{- range .Fields }}
{{- if not .IsHidden }}
"{{ .ColumnName }}",
{{- end }}
{{- end }}
}
}
Expand All @@ -43,8 +46,10 @@ func ({{ $short }} *{{ .Name }}) columnsToPtrs(cols []string) ([]interface{}, er
for _, col := range cols {
switch col {
{{- range .Fields }}
{{- if not .IsHidden }}
case "{{ .ColumnName }}":
ret = append(ret, yoDecode(&{{ $short }}.{{ .Name }}))
{{- end }}
{{- end }}
default:
return nil, fmt.Errorf("unknown column: %s", col)
Expand All @@ -58,8 +63,10 @@ func ({{ $short }} *{{ .Name }}) columnsToValues(cols []string) ([]interface{},
for _, col := range cols {
switch col {
{{- range .Fields }}
{{- if not .IsHidden }}
case "{{ .ColumnName }}":
ret = append(ret, yoEncode({{ $short }}.{{ .Name }}))
{{- end }}
{{- end }}
default:
return nil, fmt.Errorf("unknown column: %s", col)
Expand Down

0 comments on commit 44c1d63

Please sign in to comment.