Skip to content

Commit

Permalink
Postgres - Composite key value fix (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tang8330 authored Feb 2, 2024
1 parent 5a6051a commit 00524c0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
17 changes: 10 additions & 7 deletions lib/postgres/queries/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,17 @@ type SelectTableQueryArgs struct {
}

func SelectTableQuery(args SelectTableQueryArgs) string {
orderByFragment := strings.Join(quotedIdentifiers(args.OrderBy), ",")
if args.Descending {
orderByFragment += " DESC"
var fragments []string
for _, orderBy := range args.OrderBy {
fragment := pgx.Identifier{orderBy}.Sanitize()
if args.Descending {
fragment += " DESC"
}

fragments = append(fragments, fragment)
}

// TODO: Make sure keys are being escaped properly
return fmt.Sprintf(`SELECT %s FROM %s ORDER BY %s LIMIT 1`,
strings.Join(args.Keys, ","), pgx.Identifier{args.Schema, args.TableName}.Sanitize(), orderByFragment)
return fmt.Sprintf(`SELECT %s FROM %s ORDER BY %s LIMIT 1`, strings.Join(args.Keys, ","),
pgx.Identifier{args.Schema, args.TableName}.Sanitize(), strings.Join(fragments, ","))
}

type RetrievePrimaryKeysArgs struct {
Expand Down
2 changes: 1 addition & 1 deletion lib/postgres/queries/queries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestSelectTableQuery(t *testing.T) {
OrderBy: []string{"e", "f", "g"},
Descending: true,
})
assert.Equal(t, `SELECT a,b,c FROM "schema"."table" ORDER BY "e","f","g" DESC LIMIT 1`, query)
assert.Equal(t, `SELECT a,b,c FROM "schema"."table" ORDER BY "e" DESC,"f" DESC,"g" DESC LIMIT 1`, query)
}
}

Expand Down

0 comments on commit 00524c0

Please sign in to comment.