Ancestors

Toot

Written by Mark Gritter on 2024-12-18 at 02:18

This is a very small peeve, and we should really be migrating to Bun, but go-pg is lacking a method that works like:

OrderBy( field, direction )

What it has instead is a method that takes the whole clause as string, like this:

Order( "my_field ASC" )

So code that uses it ends up frequently looking like

Order( fmt.Sprintf( "%s %s", column, order ) )

What happens internally? The Order() function parses the string and then calls

Query.OrderExpr( "? ?", pg.Ident(column), pg.Safe(order) )

https://github.com/go-pg/pg/blob/bce00adc25696cc87008ccc4daf10f6350bbdae8/orm/query.go#L721

So, the entire process looks like:

  1. Concatenate the two strings

  1. Parse the string to recover the two things that were concatenated

  1. Re-create the string from step 1 using a template and two parameters

OK, OK, maybe step 3 adds some quoting that wasn't there already. But today I finally wrote a utility function to avoid this round-trip through steps 1 and 2 by copying the OrderExpr call.

[#]golang #postgres

=> More informations about this toot | More toots from markgritter@mathstodon.xyz

Descendants

Proxy Information
Original URL
gemini://mastogem.picasoft.net/thread/113671425746452006
Status Code
Success (20)
Meta
text/gemini
Capsule Response Time
242.267764 milliseconds
Gemini-to-HTML Time
0.477063 milliseconds

This content has been proxied by September (3851b).