Skip to content

Commit a47c4b2

Browse files
authored
keep field name (#18)
* keep field name * update
1 parent 7f2f667 commit a47c4b2

14 files changed

+248
-249
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
sqlc-fs is a [sqlc plugin](https://docs.sqlc.dev/en/stable/guides/plugins.html) to generate queries that are compatible with [Npgsql.Fsharp](https://github.com/Zaid-Ajaj/Npgsql.FSharp).
44

5-
This plugin is alpha. (current implementation assuming the table column is snake_case)
5+
This plugin is in alpha stage.
66

77
check https://github.com/swuecho/sqlc-fs/tree/main/testdata for demo.
88

struct.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,5 @@ func FieldName(name string, settings *plugin.Settings) string {
3333
if rename := settings.Rename[name]; rename != "" {
3434
return rename
3535
}
36-
out := sdk.ToPascalCase(name)
37-
return out
36+
return name
3837
}

templates/model.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
{{- end -}}
77
type {{.Name}} = {{ "{" }}
88
{{range .Fields -}}
9-
{{" "}} {{.Name}}: {{.Type}} {{- if .Comment -}} // {{.Comment}} {{- end }}
9+
{{" "}} {{.Name | pascalCase }}: {{.Type}} {{- if .Comment -}} // {{.Comment}} {{- end }}
1010
{{end -}}
1111
{{ "}\n" }}
1212
{{end}}

templates/query.tmpl

+5-5
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ let {{.ConstantName}} = {{$.Q}}-- name: {{.MethodName}} {{.Cmd}}
2020

2121
{{if .Arg.EmitStruct}}
2222
type {{.Arg.Type}} = { {{- range .Arg.UniqueFields}}
23-
{{.Name}}: {{.Type}};
23+
{{.Name | pascalCase }}: {{.Type}};
2424
{{- end}}
2525
}
2626
{{- end -}}
2727

2828
{{if .Ret.EmitStruct}}
2929
type {{.Ret.Type}} = { {{- range .Ret.Struct.Fields}}
30-
{{.Name}}: {{.Type}};
30+
{{.Name | pascalCase }}: {{.Type}};
3131
{{- end}}
3232
}
3333
{{- end -}}
@@ -39,12 +39,12 @@ let {{.MethodName}} (db: NpgsqlConnection) ({{.Arg.Pair}}) =
3939
{{ if .Ret.UniqueFieldsCountMultiple }}
4040
let reader = fun (read:RowReader) -> {
4141
{{- range $i, $e := .Ret.UniqueFields}}
42-
{{$e.Name}} = read.{{$e.Type | type2readerFunc }} "{{$e.Name | snakeCase }}"
42+
{{$e.Name | pascalCase }} = read.{{$e.Type | type2readerFunc }} "{{$e.Name }}"
4343
{{- end -}}
4444
}
4545
{{ else}}
4646
{{- range $i, $e := .Ret.UniqueFields}}
47-
let reader = fun (read:RowReader) -> read.{{$e.Type | type2readerFunc }} "{{$e.Name | snakeCase }}"
47+
let reader = fun (read:RowReader) -> read.{{$e.Type | type2readerFunc }} "{{$e.Name }}"
4848
{{- end -}}
4949
{{end}}
5050

@@ -61,7 +61,7 @@ let {{.MethodName}} (db: NpgsqlConnection) ({{.Arg.Pair}}) =
6161
let {{.MethodName}} (db: NpgsqlConnection) ({{.Arg.Pair}}) =
6262
let reader = fun (read:RowReader) -> {
6363
{{- range $i, $e := .Ret.UniqueFields}}
64-
{{$e.Name}} = read.{{$e.Type | type2readerFunc }} "{{$e.Name}}"
64+
{{$e.Name | pascalCase }} = read.{{$e.Type | type2readerFunc }} "{{$e.Name}}"
6565
{{- end -}}
6666
}
6767
db

testdata/gen/auth_user.sql.fs

+30-30
Original file line numberDiff line numberDiff line change
@@ -150,17 +150,17 @@ type GetAllAuthUsersRow = {
150150

151151
let GetAllAuthUsers (db: NpgsqlConnection) () =
152152
let reader = fun (read:RowReader) -> {
153-
Id = read.int "Id"
154-
Password = read.string "Password"
155-
LastLogin = read.dateTime "LastLogin"
156-
IsSuperuser = read.bool "IsSuperuser"
157-
Username = read.string "Username"
158-
FirstName = read.string "FirstName"
159-
LastName = read.string "LastName"
160-
Email = read.string "Email"
161-
IsStaff = read.bool "IsStaff"
162-
IsActive = read.bool "IsActive"
163-
DateJoined = read.dateTime "DateJoined"}
153+
Id = read.int "id"
154+
Password = read.string "password"
155+
LastLogin = read.dateTime "last_login"
156+
IsSuperuser = read.bool "is_superuser"
157+
Username = read.string "username"
158+
FirstName = read.string "first_name"
159+
LastName = read.string "last_name"
160+
Email = read.string "email"
161+
IsStaff = read.bool "is_staff"
162+
IsActive = read.bool "is_active"
163+
DateJoined = read.dateTime "date_joined"}
164164
db
165165
|> Sql.existingConnection
166166
|> Sql.query getAllAuthUsers
@@ -434,14 +434,14 @@ type GetUserStatsRow = {
434434

435435
let GetUserStats (db: NpgsqlConnection) (arg: GetUserStatsParams) =
436436
let reader = fun (read:RowReader) -> {
437-
FirstName = read.string "FirstName"
438-
LastName = read.string "LastName"
439-
UserEmail = read.string "UserEmail"
440-
TotalChatMessages = read.int64 "TotalChatMessages"
441-
TotalTokenCount = read.int64 "TotalTokenCount"
442-
TotalChatMessages3Days = read.int64 "TotalChatMessages3Days"
443-
TotalTokenCount3Days = read.int64 "TotalTokenCount3Days"
444-
RateLimit = read.int "RateLimit"}
437+
FirstName = read.string "first_name"
438+
LastName = read.string "last_name"
439+
UserEmail = read.string "user_email"
440+
TotalChatMessages = read.int64 "total_chat_messages"
441+
TotalTokenCount = read.int64 "total_token_count"
442+
TotalChatMessages3Days = read.int64 "total_chat_messages_3_days"
443+
TotalTokenCount3Days = read.int64 "total_token_count_3_days"
444+
RateLimit = read.int "rate_limit"}
445445
db
446446
|> Sql.existingConnection
447447
|> Sql.query getUserStats
@@ -485,17 +485,17 @@ type ListAuthUsersRow = {
485485

486486
let ListAuthUsers (db: NpgsqlConnection) (arg: ListAuthUsersParams) =
487487
let reader = fun (read:RowReader) -> {
488-
Id = read.int "Id"
489-
Password = read.string "Password"
490-
LastLogin = read.dateTime "LastLogin"
491-
IsSuperuser = read.bool "IsSuperuser"
492-
Username = read.string "Username"
493-
FirstName = read.string "FirstName"
494-
LastName = read.string "LastName"
495-
Email = read.string "Email"
496-
IsStaff = read.bool "IsStaff"
497-
IsActive = read.bool "IsActive"
498-
DateJoined = read.dateTime "DateJoined"}
488+
Id = read.int "id"
489+
Password = read.string "password"
490+
LastLogin = read.dateTime "last_login"
491+
IsSuperuser = read.bool "is_superuser"
492+
Username = read.string "username"
493+
FirstName = read.string "first_name"
494+
LastName = read.string "last_name"
495+
Email = read.string "email"
496+
IsStaff = read.bool "is_staff"
497+
IsActive = read.bool "is_active"
498+
DateJoined = read.dateTime "date_joined"}
499499
db
500500
|> Sql.existingConnection
501501
|> Sql.query listAuthUsers

testdata/gen/chat_log.sql.fs

+5-5
Original file line numberDiff line numberDiff line change
@@ -203,11 +203,11 @@ type ListChatLogsRow = {
203203

204204
let ListChatLogs (db: NpgsqlConnection) () =
205205
let reader = fun (read:RowReader) -> {
206-
Id = read.int "Id"
207-
Session = read.string "Session"
208-
Question = read.string "Question"
209-
Answer = read.string "Answer"
210-
CreatedAt = read.dateTime "CreatedAt"}
206+
Id = read.int "id"
207+
Session = read.string "session"
208+
Question = read.string "question"
209+
Answer = read.string "answer"
210+
CreatedAt = read.dateTime "created_at"}
211211
db
212212
|> Sql.existingConnection
213213
|> Sql.query listChatLogs

testdata/gen/chat_message.sql.fs

+60-60
Original file line numberDiff line numberDiff line change
@@ -216,21 +216,21 @@ type GetAllChatMessagesRow = {
216216

217217
let GetAllChatMessages (db: NpgsqlConnection) () =
218218
let reader = fun (read:RowReader) -> {
219-
Id = read.int "Id"
220-
Uuid = read.string "Uuid"
221-
ChatSessionUuid = read.string "ChatSessionUuid"
222-
Role = read.string "Role"
223-
Content = read.string "Content"
224-
Score = read.double "Score"
225-
UserId = read.int "UserId"
226-
CreatedAt = read.dateTime "CreatedAt"
227-
UpdatedAt = read.dateTime "UpdatedAt"
228-
CreatedBy = read.int "CreatedBy"
229-
UpdatedBy = read.int "UpdatedBy"
230-
IsDeleted = read.bool "IsDeleted"
231-
IsPin = read.bool "IsPin"
232-
TokenCount = read.int "TokenCount"
233-
Raw = read.string "Raw"}
219+
Id = read.int "id"
220+
Uuid = read.string "uuid"
221+
ChatSessionUuid = read.string "chat_session_uuid"
222+
Role = read.string "role"
223+
Content = read.string "content"
224+
Score = read.double "score"
225+
UserId = read.int "user_id"
226+
CreatedAt = read.dateTime "created_at"
227+
UpdatedAt = read.dateTime "updated_at"
228+
CreatedBy = read.int "created_by"
229+
UpdatedBy = read.int "updated_by"
230+
IsDeleted = read.bool "is_deleted"
231+
IsPin = read.bool "is_pin"
232+
TokenCount = read.int "token_count"
233+
Raw = read.string "raw"}
234234
db
235235
|> Sql.existingConnection
236236
|> Sql.query getAllChatMessages
@@ -480,21 +480,21 @@ type GetChatMessagesBySessionUUIDRow = {
480480

481481
let GetChatMessagesBySessionUUID (db: NpgsqlConnection) (arg: GetChatMessagesBySessionUUIDParams) =
482482
let reader = fun (read:RowReader) -> {
483-
Id = read.int "Id"
484-
Uuid = read.string "Uuid"
485-
ChatSessionUuid = read.string "ChatSessionUuid"
486-
Role = read.string "Role"
487-
Content = read.string "Content"
488-
Score = read.double "Score"
489-
UserId = read.int "UserId"
490-
CreatedAt = read.dateTime "CreatedAt"
491-
UpdatedAt = read.dateTime "UpdatedAt"
492-
CreatedBy = read.int "CreatedBy"
493-
UpdatedBy = read.int "UpdatedBy"
494-
IsDeleted = read.bool "IsDeleted"
495-
IsPin = read.bool "IsPin"
496-
TokenCount = read.int "TokenCount"
497-
Raw = read.string "Raw"}
483+
Id = read.int "id"
484+
Uuid = read.string "uuid"
485+
ChatSessionUuid = read.string "chat_session_uuid"
486+
Role = read.string "role"
487+
Content = read.string "content"
488+
Score = read.double "score"
489+
UserId = read.int "user_id"
490+
CreatedAt = read.dateTime "created_at"
491+
UpdatedAt = read.dateTime "updated_at"
492+
CreatedBy = read.int "created_by"
493+
UpdatedBy = read.int "updated_by"
494+
IsDeleted = read.bool "is_deleted"
495+
IsPin = read.bool "is_pin"
496+
TokenCount = read.int "token_count"
497+
Raw = read.string "raw"}
498498
db
499499
|> Sql.existingConnection
500500
|> Sql.query getChatMessagesBySessionUUID
@@ -700,21 +700,21 @@ type GetLastNChatMessagesRow = {
700700

701701
let GetLastNChatMessages (db: NpgsqlConnection) (arg: GetLastNChatMessagesParams) =
702702
let reader = fun (read:RowReader) -> {
703-
Id = read.int "Id"
704-
Uuid = read.string "Uuid"
705-
ChatSessionUuid = read.string "ChatSessionUuid"
706-
Role = read.string "Role"
707-
Content = read.string "Content"
708-
Score = read.double "Score"
709-
UserId = read.int "UserId"
710-
CreatedAt = read.dateTime "CreatedAt"
711-
UpdatedAt = read.dateTime "UpdatedAt"
712-
CreatedBy = read.int "CreatedBy"
713-
UpdatedBy = read.int "UpdatedBy"
714-
IsDeleted = read.bool "IsDeleted"
715-
IsPin = read.bool "IsPin"
716-
TokenCount = read.int "TokenCount"
717-
Raw = read.string "Raw"}
703+
Id = read.int "id"
704+
Uuid = read.string "uuid"
705+
ChatSessionUuid = read.string "chat_session_uuid"
706+
Role = read.string "role"
707+
Content = read.string "content"
708+
Score = read.double "score"
709+
UserId = read.int "user_id"
710+
CreatedAt = read.dateTime "created_at"
711+
UpdatedAt = read.dateTime "updated_at"
712+
CreatedBy = read.int "created_by"
713+
UpdatedBy = read.int "updated_by"
714+
IsDeleted = read.bool "is_deleted"
715+
IsPin = read.bool "is_pin"
716+
TokenCount = read.int "token_count"
717+
Raw = read.string "raw"}
718718
db
719719
|> Sql.existingConnection
720720
|> Sql.query getLastNChatMessages
@@ -775,21 +775,21 @@ type GetLatestMessagesBySessionUUIDRow = {
775775

776776
let GetLatestMessagesBySessionUUID (db: NpgsqlConnection) (arg: GetLatestMessagesBySessionUUIDParams) =
777777
let reader = fun (read:RowReader) -> {
778-
Id = read.int "Id"
779-
Uuid = read.string "Uuid"
780-
ChatSessionUuid = read.string "ChatSessionUuid"
781-
Role = read.string "Role"
782-
Content = read.string "Content"
783-
Score = read.double "Score"
784-
UserId = read.int "UserId"
785-
CreatedAt = read.dateTime "CreatedAt"
786-
UpdatedAt = read.dateTime "UpdatedAt"
787-
CreatedBy = read.int "CreatedBy"
788-
UpdatedBy = read.int "UpdatedBy"
789-
IsDeleted = read.bool "IsDeleted"
790-
IsPin = read.bool "IsPin"
791-
TokenCount = read.int "TokenCount"
792-
Raw = read.string "Raw"}
778+
Id = read.int "id"
779+
Uuid = read.string "uuid"
780+
ChatSessionUuid = read.string "chat_session_uuid"
781+
Role = read.string "role"
782+
Content = read.string "content"
783+
Score = read.double "score"
784+
UserId = read.int "user_id"
785+
CreatedAt = read.dateTime "created_at"
786+
UpdatedAt = read.dateTime "updated_at"
787+
CreatedBy = read.int "created_by"
788+
UpdatedBy = read.int "updated_by"
789+
IsDeleted = read.bool "is_deleted"
790+
IsPin = read.bool "is_pin"
791+
TokenCount = read.int "token_count"
792+
Raw = read.string "raw"}
793793
db
794794
|> Sql.existingConnection
795795
|> Sql.query getLatestMessagesBySessionUUID

testdata/gen/chat_model.sql.fs

+18-18
Original file line numberDiff line numberDiff line change
@@ -328,15 +328,15 @@ type ListChatModelsRow = {
328328

329329
let ListChatModels (db: NpgsqlConnection) () =
330330
let reader = fun (read:RowReader) -> {
331-
Id = read.int "Id"
332-
Name = read.string "Name"
333-
Label = read.string "Label"
334-
IsDefault = read.bool "IsDefault"
335-
Url = read.string "Url"
336-
ApiAuthHeader = read.string "ApiAuthHeader"
337-
ApiAuthKey = read.string "ApiAuthKey"
338-
UserId = read.int "UserId"
339-
EnablePerModeRatelimit = read.bool "EnablePerModeRatelimit"}
331+
Id = read.int "id"
332+
Name = read.string "name"
333+
Label = read.string "label"
334+
IsDefault = read.bool "is_default"
335+
Url = read.string "url"
336+
ApiAuthHeader = read.string "api_auth_header"
337+
ApiAuthKey = read.string "api_auth_key"
338+
UserId = read.int "user_id"
339+
EnablePerModeRatelimit = read.bool "enable_per_mode_ratelimit"}
340340
db
341341
|> Sql.existingConnection
342342
|> Sql.query listChatModels
@@ -374,15 +374,15 @@ type ListSystemChatModelsRow = {
374374

375375
let ListSystemChatModels (db: NpgsqlConnection) () =
376376
let reader = fun (read:RowReader) -> {
377-
Id = read.int "Id"
378-
Name = read.string "Name"
379-
Label = read.string "Label"
380-
IsDefault = read.bool "IsDefault"
381-
Url = read.string "Url"
382-
ApiAuthHeader = read.string "ApiAuthHeader"
383-
ApiAuthKey = read.string "ApiAuthKey"
384-
UserId = read.int "UserId"
385-
EnablePerModeRatelimit = read.bool "EnablePerModeRatelimit"}
377+
Id = read.int "id"
378+
Name = read.string "name"
379+
Label = read.string "label"
380+
IsDefault = read.bool "is_default"
381+
Url = read.string "url"
382+
ApiAuthHeader = read.string "api_auth_header"
383+
ApiAuthKey = read.string "api_auth_key"
384+
UserId = read.int "user_id"
385+
EnablePerModeRatelimit = read.bool "enable_per_mode_ratelimit"}
386386
db
387387
|> Sql.existingConnection
388388
|> Sql.query listSystemChatModels

0 commit comments

Comments
 (0)