I sometimes build long complex SQL statements from code, and it takes a lot of effort to code and test, so figured why not build a class to do it for me?
Here's a very simple class (the example is in B4J) to build SQL statements from code. It consists of a very basic screen (only needed for the example) and a small SQL builder class.
The parameters for the class are hard-coded for the example, but you can easily change it to (for example) allow the user to select fields from a list boxes, etc.
This basic version returns a SELECT statement with the ability to JOIN a second table and to ORDER the results.
The idea is that you could build the SQL from within your code, and then execute the result to produce screens, reports, etc.
Some ideas for extending it could be:
My questions are
Here's a very simple class (the example is in B4J) to build SQL statements from code. It consists of a very basic screen (only needed for the example) and a small SQL builder class.
The parameters for the class are hard-coded for the example, but you can easily change it to (for example) allow the user to select fields from a list boxes, etc.
This basic version returns a SELECT statement with the ability to JOIN a second table and to ORDER the results.
The idea is that you could build the SQL from within your code, and then execute the result to produce screens, reports, etc.
Some ideas for extending it could be:
- Where clause
- Allow any number of tables
- Aliases
- COUNT, AVG, SUM
- Wildcards
- etc.
My questions are
- Would it be worth building it out to a library that includes more complex statements to be built from code (i.e. would people actually use it)?
- What features should be included?
Attachments
Last edited: