5

Nearly every project have search panel and custom filters in my company. It is hard to create when project has too much filter.

Is there any good design pattern for creating custom sql queries for using with filters?

I always write something like this:

commandText = "SELECT * FROM XXX "

innerJoinCommand = ""
whereCommand = ""

if (??.length > 0)
  whereCommand += "AND ??? "

if (??.Count > 0)
  innerJoinCommand += "??? "

//...

if (innerJoinCommand.length > 0)
  commandText += innerJoinCommand

if (whereCommand.length > 0)
  commandText += "WHERE " + whereCommand
4
  • 1
    "Design pattern"? You keep using this term. I do not think it means what you think it means. : D Sorry, had to. Commented Feb 7, 2012 at 13:43
  • so what do you prefer instead of "design pattern"? Commented Feb 7, 2012 at 13:56
  • 1
    I was joking. Within the object-oriented world, the term "design pattern" has evolved into something that fits, somewhat ironically, into a subset of coding archetypes. The big patterns don't really cover database interaction, which is why I said that. Commented Feb 7, 2012 at 14:23
  • Design patterns are not limited to object-oriented world. Any domain can have it. Commented Nov 30, 2016 at 18:33

3 Answers 3

11

This sort of thing is frequently done by using the Builder Pattern.

If you want to support very complex queries, it might be a rather complex builder, and other patterns might come into play, but this is where I would start.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks... and i found it with your help: codeproject.com/Articles/13419/…
Here is one such implementation: github.com/mladenb/sql-query-builder
1

I used the following design :

Is it a oop good design?

The little trick is to put a "WHERE 1 = 1" so you don't have to handle if ti's a AND or a WHERE you have to add.

1 Comment

Small , but really useful trick. Thanks for pointing that out.
0

this is how I do: (srp is an object that contains all the possible parameters)

   string query = "select * from TABLE";

        if (srp != null)
        {
            query += " Where ";
            bool firstParameter = true;

            if (srp.ID!= 0)
            {
                if (!firstParameter)
                {
                    query += " and ";
                }
                query += " ID= " + srp.ID.ToString();
                firstParameter = false;
            }
        }

then you can replicate the inner if as much as you need.

Of course, this will only work with AND parameters, still didn't have to create a more complex logic to use or

1 Comment

thanks for your answer but i think it is confusing too and hard to implement... it should be easier than i think :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.