Skip to content

Instantly share code, notes, and snippets.

@paragtokopedia
Last active July 13, 2018 06:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paragtokopedia/45e1774b3b576943e754b77b8bf6e3f5 to your computer and use it in GitHub Desktop.
Save paragtokopedia/45e1774b3b576943e754b77b8bf6e3f5 to your computer and use it in GitHub Desktop.
Another complex query example
//select * from application a where a.created_at >= '2018-01-01' AND a.created_at <= '2019-01-01' AND a.id >=0 AND (a.status = 'some_status' OR a.id =2 )
func GenerateQuery(ctx context.Context, queryParams QueryParams) (query string) {
var dqb DynamicQueryBuilder
query = dqb.And(
dqb.NewExp("a.created_at", ">=", queryParams["start_date"]+" "+queryParams["start_time"]), //if empty it will not be added in where clause
dqb.NewExp("a.created_at", "<=", queryParams["end_date"]+" "+queryParams["end_time"]),
"a.id >= 0",
"a.status IN ('pending','complete','initiated')",
dqb.OR(
dqb.NewExp("a.status","=",queryParams["status"]),
dqb.NewExp("a.id","=",queryParams.GetInt("id")),
),
).BindSql("select * from application a")
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment