Building WHERE clauses
A WHERE
clause can be a part of a SELECT
, UPDATE
, or DELETE
statement. Here, I will show a SELECT
example, and you can extend this to apply to UPDATE
and DELETE
. Be careful with the arguments as an UPDATE
statement will include arguments for update column values as well.
How to do it...
This example shows the case where AND is used in the search criteria:
- You need a data structure that gives which columns to include in the
WHERE
clause. Take the following example:type UserSearchRequest struct { Ids []uint64 Name *string LoggedInBefore *time.Time LoggedInAfter *time.Time AvatarURL *string }
With this structure, the search function looks as follows:
func SearchUsers(ctx context.Context, db *sql.DB, req *UserSearchRequest...