On Fri, 25 Aug 2006 17:30:02 +0200, Robert Klemme wrote:

> On 25.08.2006 17:00, Ken Bloom wrote:
>> I'm writing a library for generating fairly complicated SQL statements in
>> a relatively clean way. The current version of the library can be found at
>> http://lingcog.iit.edu/~bloom/sqlstatement.rb and some sample code which
>> is fairly representative of what I plan to use the library for is at
>> http://lingcog.iit.edu/~bloom/newstuff.rb
>> 
>> I was curious if anybody had suggestions about ways they might make the
>> library easier to use and/or more maintainable.
> 
> Some things you could do to make this more usable:
> 
> - Spend more whitespace, especially blank lines, to mare the code more 
> readable.
> 
> - Add an introductory comment at the beginning, especially state the 
> purpose of the lib
> 
> - Add a simple example that demonstrates usage of the code
> 
> Another general remark: SQL is extremely flexible, so any tool that 
> generates SQL either has a GUI or restricts the output to a subset of 
> SQL statements or both.  Without the restriction and a GUI such a 
> library can easily get as complex as SQL itself and in that case I'd 
> probably rather write SQL directly unless I get something else from such 
> a lib (for example database independence).  My 0.02EUR

The main goal of this library is to be able to construct an SQL statement
from "slices" that concern different aspects of the final query (perhaps
in different places in your code) and then combine them all together into
one statement easily.

Another important goal of this library is to give some consistent Ruby
syntax to three statements (INSERT, SELECT, and UPDATE) that seem to have
different enough syntax that one has two write different code to generate
each kind of statement.

I use my SQL database (specifically MySQL) largely as a bulk data
processing engine, by doing INSERT...SELECT or CREATE TABLE...SELECT
statements. This library is intended to make that kind of coding easier. I
expect that Object Relational mappers (such as ActiveRecord) are more
useful for most people, who are performing queries and
inserting/updating/querying for individual records. In time, I'll probably
add classes to help with these too, to have some consistency

An interesting library that I've seen is CLSQL[1] for Common LISP, or
SchemeQL[2] for Scheme that have similar goals. Scheme and LISP's use
of s-expressions make it very easy to construct an entire sublanguage for
the WHERE clause, simply by list parsing. I'm not sure if it's
possible to do that in Ruby (if anyone has, I'd like to know. Criteria[3]
perhaps.), but this library covers some basic steps in ironing out SQL's
complexities. I also can't get the compile time checking that SchemeQL
advertises, mostly because of duck typing, but also to an extent becasue
Ruby isn't compiled.

This library doesn't try to abstract out the limitations of your DBMS, and
I think that the SQL it uses should be fairly portable, in large measure
because it hasn't attempted to deal with serious CREATE TABLE statements,
where a lot of syntax concerning types, keys and sequences is much more
variable.

--Ken

[1] http://clsql.b9.com/
[2] http://schematics.sourceforge.net/schemeql.html
http://repository.readscheme.org/ftp/papers/sw2002/schemeunit-schemeql.pdf
[3] http://mephle.org/Criteria/
(via http://onestepback.org/index.cgi/Tech/Ruby/Criteria.rdoc)

-- 
Ken Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu/~kbloom1/