go icon indicating copy to clipboard operation
go copied to clipboard

proposal: database/sql: add query builder

Open bksamsung opened this issue 1 year ago • 3 comments

Proposal Details

I propose to add a query builder that would help us to write more dynamic SQL queries.

The problem

When writing webapps, it's common to have dynamic queries that are useful, for example, sorting or filtering. Writing

To achieve that we have a few options like:

  1. using a query builder (https://github.com/huandu/go-sqlbuilder, https://doug-martin.github.io/goqu/)
  2. joining strings dynamically
  3. using an ORM

The second option is the most error probe and may be good source for bugs. The option with an ORM doesn't seem like a good option because many developers don't wan to use any ORM at all. A query builder looks like a sweet spot between the flexibility, safety of syntax of the query and the speed of development and building final queries.

Related issues:

  • https://github.com/golang/go/issues/18478

bksamsung avatar Sep 17 '24 08:09 bksamsung

see https://go.dev/doc/faq#x_in_std

given the existence of third party libraries that already do this, and the numerous SQL dialects that SQL builders need to support, I don't think this would be an appropriate fit for the standard library

seankhliao avatar Sep 17 '24 09:09 seankhliao

This proposal should define a new API that you suggest that we implement in the standard library.

To be clear, I don't know whether we should do this. But we can't decide without seeing what exactly is being proposed. Thanks.

ianlancetaylor avatar Sep 18 '24 04:09 ianlancetaylor

Shouldn't strings.Builder be enough? I don't think trying to add support for every SQL dialect to build a query concerns the std. As @ianlancetaylor mentioned, maybe showing an API would make things clearer.

fontseca avatar Sep 18 '24 16:09 fontseca

Timed out in state WaitingForInfo. Closing.

(I am just a bot, though. Please speak up if this is a mistake or you have the requested information.)

gopherbot avatar Nov 16 '24 21:11 gopherbot