Skip to content

Commit

Permalink
add Sturct & Field
Browse files Browse the repository at this point in the history
  • Loading branch information
kfly8 committed Aug 4, 2024
1 parent 959dd3a commit 7b04fcd
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 6 deletions.
74 changes: 70 additions & 4 deletions internal/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"bufio"
"bytes"
"context"
"sort"

//"errors"
//"fmt"
Expand All @@ -21,11 +22,26 @@ import (


type tmplCtx struct {
Q string
Package string
Structs []Struct
SqlcVersion string
SourceName string
}

type Struct struct {
Table *plugin.Identifier
Name string
Fields []Field
Comment string
}

type Field struct {
Name string // TODO: CamelCase ? snaked-case?
DBName string // Name as used in the DB
Type string
Comment string
}

func Generate(ctx context.Context, req *plugin.GenerateRequest) (*plugin.GenerateResponse, error) {
options, err := opts.Parse(req)
if err != nil {
Expand All @@ -36,15 +52,18 @@ func Generate(ctx context.Context, req *plugin.GenerateRequest) (*plugin.Generat
return nil, err
}

return generate(req, options)
structs := buildStructs(req, options)

return generate(req, options, structs)
}


func generate(req *plugin.GenerateRequest, options *opts.Options) (*plugin.GenerateResponse, error) {
func generate(req *plugin.GenerateRequest, options *opts.Options, structs []Struct) (*plugin.GenerateResponse, error) {

tctx := tmplCtx{
Q: "`",
Package: options.Package,
SqlcVersion: req.SqlcVersion,
Structs: structs,
}

funcMap := template.FuncMap{
Expand Down Expand Up @@ -103,3 +122,50 @@ func generate(req *plugin.GenerateRequest, options *opts.Options) (*plugin.Gener
return &resp, nil
}

func buildStructs(req *plugin.GenerateRequest, options *opts.Options) []Struct {
var structs []Struct
for _, schema := range req.Catalog.Schemas {
if schema.Name == "pg_catalog" || schema.Name == "information_schema" {
continue
}
for _, table := range schema.Tables {
var tableName string
if schema.Name == req.Catalog.DefaultSchema {
tableName = table.Rel.Name
} else {
tableName = schema.Name + "_" + table.Rel.Name
}

s := Struct{
Table: &plugin.Identifier{Schema: schema.Name, Name: table.Rel.Name},
Name: structName(tableName, options),
Comment: table.Comment,
}
for _, column := range table.Columns {
s.Fields = append(s.Fields, Field{
Name: fieldName(column.Name, options),
Type: perlType(req, options, column),
Comment: column.Comment,
})
}
structs = append(structs, s)
}
}
if len(structs) > 0 {
sort.Slice(structs, func(i, j int) bool { return structs[i].Name < structs[j].Name })
}
return structs
}

func structName(name string, options *opts.Options) string {
return sdk.LowerTitle(name)
}

func fieldName(name string, options *opts.Options) string {
return sdk.LowerTitle(name)
}

func perlType(req *plugin.GenerateRequest, options *opts.Options, column *plugin.Column) string {
// TODO: implement
return "Str"
}
1 change: 1 addition & 0 deletions internal/opts/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
)

type Options struct {
Package string `json:"package" yaml:"package"`
OutputModelsFileName string `json:"output_models_file_name,omitempty" yaml:"output_models_file_name"`
}

Expand Down
16 changes: 14 additions & 2 deletions internal/templates/template.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,28 @@
# Code generated by sqlc. DO NOT EDIT.
# versions:
# sqlc: {{.SqlcVersion}}
# source: {{.SourceName}}

package Models;
package {{.Package}};
use strict;
use warnings;

use Types::Standard -types;

{{template "modelsCode" .}}

1;
{{end}}

{{define "modelsCode"}}
Hello

{{range .Structs}}
{{if .Comment}}{comment .Comment}{{end}}
use kote {{.Name}} => Dict[ {{- range .Fields}}
{{- if .Comment}}{{comment .Comment}}{{end}}
{{.Name}} => {{.Type}},
{{- end}}
];
{{end}}

{{end}}

0 comments on commit 7b04fcd

Please sign in to comment.