From 7d3f59921242c55a13d4921a05dc79cc7897a2c2 Mon Sep 17 00:00:00 2001 From: Hao Wu Date: Sun, 14 May 2023 17:43:24 +0800 Subject: [PATCH] update (#29) --- fs_type.go | 3 +-- gen.go | 1 + result.go | 27 +++++++++++++++++---------- templates/query.tmpl | 2 +- testdata/gen/chat_log.sql.fs | 4 ++-- testdata/gen/chat_message.sql.fs | 2 +- testdata/gen/chat_snapshot.sql.fs | 8 ++++---- 7 files changed, 27 insertions(+), 20 deletions(-) diff --git a/fs_type.go b/fs_type.go index b301491..b463b1e 100644 --- a/fs_type.go +++ b/fs_type.go @@ -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 { @@ -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" diff --git a/gen.go b/gen.go index a563366..a5953f9 100644 --- a/gen.go +++ b/gen.go @@ -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( diff --git a/result.go b/result.go index dd59b51..6abf2a7 100644 --- a/result.go +++ b/result.go @@ -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 { @@ -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, }) } @@ -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 @@ -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 @@ -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 @@ -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 @@ -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} diff --git a/templates/query.tmpl b/templates/query.tmpl index ac81088..633e06f 100644 --- a/templates/query.tmpl +++ b/templates/query.tmpl @@ -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 -}} diff --git a/testdata/gen/chat_log.sql.fs b/testdata/gen/chat_log.sql.fs index f6f2222..c06bbe0 100644 --- a/testdata/gen/chat_log.sql.fs +++ b/testdata/gen/chat_log.sql.fs @@ -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 @@ -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 diff --git a/testdata/gen/chat_message.sql.fs b/testdata/gen/chat_message.sql.fs index 0ac5b94..da4e511 100644 --- a/testdata/gen/chat_message.sql.fs +++ b/testdata/gen/chat_message.sql.fs @@ -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 diff --git a/testdata/gen/chat_snapshot.sql.fs b/testdata/gen/chat_snapshot.sql.fs index 5c6c9e5..2cf1bfc 100644 --- a/testdata/gen/chat_snapshot.sql.fs +++ b/testdata/gen/chat_snapshot.sql.fs @@ -105,7 +105,7 @@ type ChatSnapshotMetaByUserIDRow = { Uuid: string; Title: string; Summary: string; - Tags: string; + Tags: jsonb; CreatedAt: DateTime; } @@ -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 @@ -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 @@ -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