Skip to content

Commit 7d3f599

Browse files
authored
update (#29)
1 parent 1ca86f7 commit 7d3f599

File tree

7 files changed

+27
-20
lines changed

7 files changed

+27
-20
lines changed

fs_type.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"github.com/swuecho/sqlc-fs/internal/sdk"
99
)
1010

11-
1211
func fsType(req *plugin.CodeGenRequest, col *plugin.Column) string {
1312
typ := postgresType(req, col)
1413
if !col.NotNull {
@@ -83,7 +82,7 @@ func postgresType(req *plugin.CodeGenRequest, col *plugin.Column) string {
8382
return "string"
8483

8584
case "jsonb":
86-
return "string"
85+
return "jsonb"
8786

8887
case "tsvector":
8988
return "string"

gen.go

+1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ func generate(req *plugin.CodeGenRequest, structs []Struct, queries []Query, opt
9292
"escape": sdk.EscapeBacktick,
9393
"hasPrefix": strings.HasPrefix,
9494
"type2readerFunc": type2readerFunc,
95+
"json2str": jsonb2Str,
9596
}
9697

9798
tmpl := template.Must(

result.go

+17-10
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ import (
1010
"github.com/swuecho/sqlc-fs/internal/sdk"
1111
)
1212

13+
func jsonb2Str(s string) string {
14+
if s == "jsonb" {
15+
return "string"
16+
} else {
17+
return s
18+
}
19+
20+
}
21+
1322
func buildStructs(req *plugin.CodeGenRequest) []Struct {
1423
var structs []Struct
1524
for _, schema := range req.Catalog.Schemas {
@@ -24,7 +33,7 @@ func buildStructs(req *plugin.CodeGenRequest) []Struct {
2433
for _, column := range table.Columns {
2534
s.Fields = append(s.Fields, Field{
2635
Name: FieldName(column.Name, req.Settings),
27-
Type: fsType(req, column),
36+
Type: jsonb2Str(fsType(req, column)),
2837
Comment: column.Comment,
2938
})
3039
}
@@ -46,11 +55,9 @@ func npgsqlQuery(q *plugin.Query) string {
4655
return sql
4756
}
4857

49-
50-
5158
func buildQueries(req *plugin.CodeGenRequest, structs []Struct) ([]Query, error) {
5259
qs := make([]Query, 0, len(req.Queries))
53-
60+
5461
for _, query := range req.Queries {
5562
if query.Name == "" {
5663
continue
@@ -76,7 +83,7 @@ func buildQueries(req *plugin.CodeGenRequest, structs []Struct) ([]Query, error)
7683
p := query.Params[0]
7784
gq.Arg = QueryValue{
7885
Name: paramName(p),
79-
Typ: fsType(req, p.Column),
86+
Typ: jsonb2Str(fsType(req, p.Column)),
8087
}
8188
} else if len(query.Params) > 1 {
8289
var cols []column
@@ -121,7 +128,7 @@ func buildQueries(req *plugin.CodeGenRequest, structs []Struct) ([]Query, error)
121128
}
122129
gq.Ret = QueryValue{
123130
Name: name,
124-
Typ: fsType(req, c),
131+
Typ: jsonb2Str(fsType(req, c)),
125132
}
126133
} else if len(query.Columns) > 1 {
127134
var gs *Struct
@@ -135,7 +142,7 @@ func buildQueries(req *plugin.CodeGenRequest, structs []Struct) ([]Query, error)
135142
for i, f := range s.Fields {
136143
c := query.Columns[i]
137144
sameName := f.Name == columnName(c, i)
138-
sameType := f.Type == fsType(req, c)
145+
sameType := f.Type == jsonb2Str(fsType(req, c))
139146
sameTable := sdk.SameTableName(c.Table, &s.Table, req.Catalog.DefaultSchema)
140147
if !sameName || !sameType || !sameTable {
141148
same = false
@@ -229,9 +236,9 @@ func columnsToStruct(req *plugin.CodeGenRequest, name string, columns []column,
229236
fieldName = fmt.Sprintf("%s_%d", fieldName, suffix)
230237
}
231238
gs.Fields = append(gs.Fields, Field{
232-
Name: fieldName,
233-
DBName: colName,
234-
Type: fsType(req, c.Column),
239+
Name: fieldName,
240+
DBName: colName,
241+
Type: fsType(req, c.Column),
235242
})
236243
if _, found := seen[baseFieldName]; !found {
237244
seen[baseFieldName] = []int{i}

templates/query.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ let {{.ConstantName}} = {{$.Q}}-- name: {{.MethodName}} {{.Cmd}}
2323

2424
{{if .Arg.EmitStruct}}
2525
type {{.Arg.Type}} = { {{- range .Arg.UniqueFields}}
26-
{{.Name | pascalCase }}: {{.Type}};
26+
{{.Name | pascalCase }}: {{.Type | json2str }};
2727
{{- end}}
2828
}
2929
{{- end -}}

testdata/gen/chat_log.sql.fs

+2-2
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ let CreateChatLog (db: NpgsqlConnection) (arg: CreateChatLogParams) =
7777
db
7878
|> Sql.existingConnection
7979
|> Sql.query createChatLog
80-
|> Sql.parameters [ "@session", Sql.string arg.Session; "@question", Sql.string arg.Question; "@answer", Sql.string arg.Answer ]
80+
|> Sql.parameters [ "@session", Sql.jsonb arg.Session; "@question", Sql.jsonb arg.Question; "@answer", Sql.jsonb arg.Answer ]
8181
|> Sql.executeRow reader
8282

8383

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

246246

testdata/gen/chat_message.sql.fs

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ let CreateChatMessage (db: NpgsqlConnection) (arg: CreateChatMessageParams) =
6363
db
6464
|> Sql.existingConnection
6565
|> Sql.query createChatMessage
66-
|> 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 ]
66+
|> 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 ]
6767
|> Sql.executeRow reader
6868

6969

testdata/gen/chat_snapshot.sql.fs

+4-4
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ type ChatSnapshotMetaByUserIDRow = {
105105
Uuid: string;
106106
Title: string;
107107
Summary: string;
108-
Tags: string;
108+
Tags: jsonb;
109109
CreatedAt: DateTime;
110110
}
111111

@@ -115,7 +115,7 @@ let ChatSnapshotMetaByUserID (db: NpgsqlConnection) (userId: int32) =
115115
Uuid = read.string "uuid"
116116
Title = read.string "title"
117117
Summary = read.string "summary"
118-
Tags = read.string "tags"
118+
Tags = read.jsonb "tags"
119119
CreatedAt = read.dateTime "created_at"}
120120
db
121121
|> Sql.existingConnection
@@ -218,7 +218,7 @@ let CreateChatSnapshot (db: NpgsqlConnection) (arg: CreateChatSnapshotParams)
218218
db
219219
|> Sql.existingConnection
220220
|> Sql.query createChatSnapshot
221-
|> 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 ]
221+
|> 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 ]
222222
|> Sql.executeRow reader
223223

224224

@@ -428,7 +428,7 @@ let UpdateChatSnapshot (db: NpgsqlConnection) (arg: UpdateChatSnapshotParams)
428428
db
429429
|> Sql.existingConnection
430430
|> Sql.query updateChatSnapshot
431-
|> 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 ]
431+
|> 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 ]
432432
|> Sql.executeRow reader
433433

434434

0 commit comments

Comments
 (0)