Skip to content

Commit

Permalink
update (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
swuecho authored May 14, 2023
1 parent 1ca86f7 commit 7d3f599
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 20 deletions.
3 changes: 1 addition & 2 deletions fs_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/swuecho/sqlc-fs/internal/sdk"
)


func fsType(req *plugin.CodeGenRequest, col *plugin.Column) string {
typ := postgresType(req, col)
if !col.NotNull {
Expand Down Expand Up @@ -83,7 +82,7 @@ func postgresType(req *plugin.CodeGenRequest, col *plugin.Column) string {
return "string"

case "jsonb":
return "string"
return "jsonb"

case "tsvector":
return "string"
Expand Down
1 change: 1 addition & 0 deletions gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ func generate(req *plugin.CodeGenRequest, structs []Struct, queries []Query, opt
"escape": sdk.EscapeBacktick,
"hasPrefix": strings.HasPrefix,
"type2readerFunc": type2readerFunc,
"json2str": jsonb2Str,
}

tmpl := template.Must(
Expand Down
27 changes: 17 additions & 10 deletions result.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ import (
"github.com/swuecho/sqlc-fs/internal/sdk"
)

func jsonb2Str(s string) string {
if s == "jsonb" {
return "string"
} else {
return s
}

}

func buildStructs(req *plugin.CodeGenRequest) []Struct {
var structs []Struct
for _, schema := range req.Catalog.Schemas {
Expand All @@ -24,7 +33,7 @@ func buildStructs(req *plugin.CodeGenRequest) []Struct {
for _, column := range table.Columns {
s.Fields = append(s.Fields, Field{
Name: FieldName(column.Name, req.Settings),
Type: fsType(req, column),
Type: jsonb2Str(fsType(req, column)),
Comment: column.Comment,
})
}
Expand All @@ -46,11 +55,9 @@ func npgsqlQuery(q *plugin.Query) string {
return sql
}



func buildQueries(req *plugin.CodeGenRequest, structs []Struct) ([]Query, error) {
qs := make([]Query, 0, len(req.Queries))

for _, query := range req.Queries {
if query.Name == "" {
continue
Expand All @@ -76,7 +83,7 @@ func buildQueries(req *plugin.CodeGenRequest, structs []Struct) ([]Query, error)
p := query.Params[0]
gq.Arg = QueryValue{
Name: paramName(p),
Typ: fsType(req, p.Column),
Typ: jsonb2Str(fsType(req, p.Column)),
}
} else if len(query.Params) > 1 {
var cols []column
Expand Down Expand Up @@ -121,7 +128,7 @@ func buildQueries(req *plugin.CodeGenRequest, structs []Struct) ([]Query, error)
}
gq.Ret = QueryValue{
Name: name,
Typ: fsType(req, c),
Typ: jsonb2Str(fsType(req, c)),
}
} else if len(query.Columns) > 1 {
var gs *Struct
Expand All @@ -135,7 +142,7 @@ func buildQueries(req *plugin.CodeGenRequest, structs []Struct) ([]Query, error)
for i, f := range s.Fields {
c := query.Columns[i]
sameName := f.Name == columnName(c, i)
sameType := f.Type == fsType(req, c)
sameType := f.Type == jsonb2Str(fsType(req, c))
sameTable := sdk.SameTableName(c.Table, &s.Table, req.Catalog.DefaultSchema)
if !sameName || !sameType || !sameTable {
same = false
Expand Down Expand Up @@ -229,9 +236,9 @@ func columnsToStruct(req *plugin.CodeGenRequest, name string, columns []column,
fieldName = fmt.Sprintf("%s_%d", fieldName, suffix)
}
gs.Fields = append(gs.Fields, Field{
Name: fieldName,
DBName: colName,
Type: fsType(req, c.Column),
Name: fieldName,
DBName: colName,
Type: fsType(req, c.Column),
})
if _, found := seen[baseFieldName]; !found {
seen[baseFieldName] = []int{i}
Expand Down
2 changes: 1 addition & 1 deletion templates/query.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ let {{.ConstantName}} = {{$.Q}}-- name: {{.MethodName}} {{.Cmd}}

{{if .Arg.EmitStruct}}
type {{.Arg.Type}} = { {{- range .Arg.UniqueFields}}
{{.Name | pascalCase }}: {{.Type}};
{{.Name | pascalCase }}: {{.Type | json2str }};
{{- end}}
}
{{- end -}}
Expand Down
4 changes: 2 additions & 2 deletions testdata/gen/chat_log.sql.fs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ let CreateChatLog (db: NpgsqlConnection) (arg: CreateChatLogParams) =
db
|> Sql.existingConnection
|> Sql.query createChatLog
|> Sql.parameters [ "@session", Sql.string arg.Session; "@question", Sql.string arg.Question; "@answer", Sql.string arg.Answer ]
|> Sql.parameters [ "@session", Sql.jsonb arg.Session; "@question", Sql.jsonb arg.Question; "@answer", Sql.jsonb arg.Answer ]
|> Sql.executeRow reader


Expand Down Expand Up @@ -240,7 +240,7 @@ let UpdateChatLog (db: NpgsqlConnection) (arg: UpdateChatLogParams) =
db
|> Sql.existingConnection
|> Sql.query updateChatLog
|> Sql.parameters [ "@id", Sql.int arg.Id; "@session", Sql.string arg.Session; "@question", Sql.string arg.Question; "@answer", Sql.string arg.Answer ]
|> Sql.parameters [ "@id", Sql.int arg.Id; "@session", Sql.jsonb arg.Session; "@question", Sql.jsonb arg.Question; "@answer", Sql.jsonb arg.Answer ]
|> Sql.executeRow reader


Expand Down
2 changes: 1 addition & 1 deletion testdata/gen/chat_message.sql.fs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ let CreateChatMessage (db: NpgsqlConnection) (arg: CreateChatMessageParams) =
db
|> Sql.existingConnection
|> Sql.query createChatMessage
|> Sql.parameters [ "@chat_session_uuid", Sql.string arg.ChatSessionUuid; "@uuid", Sql.string arg.Uuid; "@role", Sql.string arg.Role; "@content", Sql.string arg.Content; "@token_count", Sql.int arg.TokenCount; "@score", Sql.double arg.Score; "@user_id", Sql.int arg.UserId; "@created_by", Sql.int arg.CreatedBy; "@updated_by", Sql.int arg.UpdatedBy; "@raw", Sql.string arg.Raw ]
|> Sql.parameters [ "@chat_session_uuid", Sql.string arg.ChatSessionUuid; "@uuid", Sql.string arg.Uuid; "@role", Sql.string arg.Role; "@content", Sql.string arg.Content; "@token_count", Sql.int arg.TokenCount; "@score", Sql.double arg.Score; "@user_id", Sql.int arg.UserId; "@created_by", Sql.int arg.CreatedBy; "@updated_by", Sql.int arg.UpdatedBy; "@raw", Sql.jsonb arg.Raw ]
|> Sql.executeRow reader


Expand Down
8 changes: 4 additions & 4 deletions testdata/gen/chat_snapshot.sql.fs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ type ChatSnapshotMetaByUserIDRow = {
Uuid: string;
Title: string;
Summary: string;
Tags: string;
Tags: jsonb;
CreatedAt: DateTime;
}

Expand All @@ -115,7 +115,7 @@ let ChatSnapshotMetaByUserID (db: NpgsqlConnection) (userId: int32) =
Uuid = read.string "uuid"
Title = read.string "title"
Summary = read.string "summary"
Tags = read.string "tags"
Tags = read.jsonb "tags"
CreatedAt = read.dateTime "created_at"}
db
|> Sql.existingConnection
Expand Down Expand Up @@ -218,7 +218,7 @@ let CreateChatSnapshot (db: NpgsqlConnection) (arg: CreateChatSnapshotParams)
db
|> Sql.existingConnection
|> Sql.query createChatSnapshot
|> Sql.parameters [ "@uuid", Sql.string arg.Uuid; "@user_id", Sql.int arg.UserId; "@title", Sql.string arg.Title; "@model", Sql.string arg.Model; "@summary", Sql.string arg.Summary; "@tags", Sql.string arg.Tags; "@conversation", Sql.string arg.Conversation; "@session", Sql.string arg.Session; "@text", Sql.string arg.Text ]
|> Sql.parameters [ "@uuid", Sql.string arg.Uuid; "@user_id", Sql.int arg.UserId; "@title", Sql.string arg.Title; "@model", Sql.string arg.Model; "@summary", Sql.string arg.Summary; "@tags", Sql.jsonb arg.Tags; "@conversation", Sql.jsonb arg.Conversation; "@session", Sql.jsonb arg.Session; "@text", Sql.string arg.Text ]
|> Sql.executeRow reader


Expand Down Expand Up @@ -428,7 +428,7 @@ let UpdateChatSnapshot (db: NpgsqlConnection) (arg: UpdateChatSnapshotParams)
db
|> Sql.existingConnection
|> Sql.query updateChatSnapshot
|> Sql.parameters [ "@id", Sql.int arg.Id; "@uuid", Sql.string arg.Uuid; "@user_id", Sql.int arg.UserId; "@title", Sql.string arg.Title; "@summary", Sql.string arg.Summary; "@tags", Sql.string arg.Tags; "@conversation", Sql.string arg.Conversation; "@created_at", Sql.date arg.CreatedAt ]
|> Sql.parameters [ "@id", Sql.int arg.Id; "@uuid", Sql.string arg.Uuid; "@user_id", Sql.int arg.UserId; "@title", Sql.string arg.Title; "@summary", Sql.string arg.Summary; "@tags", Sql.jsonb arg.Tags; "@conversation", Sql.jsonb arg.Conversation; "@created_at", Sql.date arg.CreatedAt ]
|> Sql.executeRow reader


Expand Down

0 comments on commit 7d3f599

Please sign in to comment.