I need to create a graphical interface with which users can select several attributes that will be used to query the database to find suitable people. I am looking for ideas on how to dynamically generate a database query according to user choices.
Query will contain several fields, but to get this idea, I will give only three of the following:
An age parameter is optional in the request. In addition, the user can indicate whether age is a desired parameter. If this is not required, and the person has no age, this is his profile, age criteria are ignored for this person.
Request example:
No criteria were given:
select * from persons
Given only lesson:
select * from persons where occupation = 'dentist'
Several classes were provided:
select * from persons where (occupation = 'dentist' or occupation = 'engineer')
Age was set as greater than the value, and it must exist in the personal profile:
select * from persons where age >= 30
Height is set as a range, and it does not have to exist in your personal profile:
select * from persons where (height is null or (height >= 30 and height <= 40))
Combination of various criteria:
select * from persons where occupation = 'dentist' and age >= 30 and (height is null or (height >= 30 and height <= 40))
I have already implemented code capable of generating queries as strings, but this, of course, is not too pretty. I am looking for ideas that would be the most effective and beautiful way to achieve this.