5

We are trying to build a SQL query builder that would allow users to make queries using visual aid .
We have got the front end all worked up with the user being able to select tables add condition etc . However our backend is a mess as we are trying to make these queries as string taking the conditions from the user , and then making appropriate SQL strings in our models .
This would eventually become very buggy and difficult to maintain as we add support for more and more complex queries . Is anybody aware of a sql builder plugin that we can use , or a better way to approach this .

NM.
  • 1,909
  • 1
  • 13
  • 21
  • Have a look at QueryBuilder: http://zenadmin.org/en/zafu/page443.html. You have to write the AST tree processor, but it's a pretty easy task and you can do it gradually as you add more features. – Anna B May 11 '11 at 14:47

4 Answers4

3

Check out arel. A fork of this project is now used in Rails 3 to help with ORM agnosticism.

hgmnz
  • 13,208
  • 4
  • 37
  • 41
1

The railscast about searchlogic for advanced search forms shows how to chain together named_scopes and use them to do complex dynamic queries.

Here's the searchlogic repo, too.

wesgarrison
  • 6,975
  • 5
  • 30
  • 39
0

I also tried to build one of these. In fact I am trying to build one right now. It is very frustrating, buggy. These links might be helpful.

Using record filter to make complex queries makes sense. I am trying an approach in which I take the query string from the user in Json format. Then convert this string into valid record filter syntax, this will require parsing the whole string. After we have parsed the string and if it is in correct syntax, I just eval it and get the results.

Waseem
  • 8,232
  • 9
  • 43
  • 54
0

You should look at the condition builder plugin:

http://blog.inquirylabs.com/2007/01/04/condition-builder-10-released/

Not sure if it will answer all your needs, but it's been working well for me.