Last active
July 13, 2018 06:21
-
-
Save paragtokopedia/45e1774b3b576943e754b77b8bf6e3f5 to your computer and use it in GitHub Desktop.
Another complex query example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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