Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > [ANN] SqlStatement 2.0 -- Generate complex SQL statementsprogrammatically

Reply
Thread Tools

[ANN] SqlStatement 2.0 -- Generate complex SQL statementsprogrammatically

 
 
Ken Bloom
Guest
Posts: n/a
 
      01-20-2008
sqlstatement - Generate complex SQL statements programmatically
================================================== =============

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. I have nevertheless added INSERT…VALUES
statements, and will add other statements soon, for consistency.

This library is inspired by CLSQL for Common LISP, or SchemeQL for
Scheme, although it is very different from these two libraries. 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. The Criteria
library for Ruby has attempted this, but in a more limited manner than
SchemeQL or CLSQL. My library aims to cover much of the functionality in
these libraries.

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.

This library can be downloaded from rubyforge.org/projects/sqlstatement

Changes in this release
=======================
* Vastly redesign the DSL. The methods for adding components to an
SQL statement have all been renamed, and internal state is no longer
directly exposed to the outside. The interface is now much more intutive
and consistent. Please read the documentation to find out how this
version of the library works.
* Left joins have been added
* There‘s no more SelectParts class — the corresponding semantics
have been built directly into the Select class.
* Select.new do |s| … end syntax has been added
* Select statements now remember the order of fields and tables. This
makes the next two changes feasible.
o Support for Mysql‘s STRAIGHT_JOIN modifier has been added
o Unit tests have been added

Example
=======
If we wanted to generate SQL code similar to

SELECT `value1`,`value2`
FROM `jointable`,`dictionary1`,`dictionary2`
WHERE `jointable`.`id1`=`dictionary1`.`id` AND
`jointable`.`id2`=`dictionary2`.`id`

we could generate it as follows:

def foo(x)
Select.new do |s|
s.field :"value#{x}"
s.table :"dictionary#{x}"
s.condition string_func("`jointable`.`id#{x}`=`dictionary#{x}
`.`id`")
end
end

stmt=Select.new
stmt.table :jointable
(1..2).each do |x|
stmt << foo(x)
end
dbh.execute(stmt)

or like this:
stmt=Select.new do |s|
s.table :jointable
[1,2].each do |num|
s.field "value#{num}".dbid
s.table "dictionary#{num}".dbid
s.condition string_func("`jointable`.`id#{num}`=`dictionary#{n um}
`.`id`")
end
end

dbh.execute stmt


--
Ken (Chanoch) Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu/~kbloom1/
 
Reply With Quote
 
 
 
 
Ken Bloom
Guest
Posts: n/a
 
      01-20-2008
On Sun, 20 Jan 2008 11:40:31 -0600, Ken Bloom wrote:

> sqlstatement - Generate complex SQL statements programmatically
> ================================================== =============


I forgot to mention that the documentation is online at
http://sqlstatement.rubyforge.org/

> Example
> =======
> If we wanted to generate SQL code similar to
>
> SELECT `value1`,`value2`
> FROM `jointable`,`dictionary1`,`dictionary2` WHERE
> `jointable`.`id1`=`dictionary1`.`id` AND
> `jointable`.`id2`=`dictionary2`.`id`


The examples actually generate slightly more verbose SQL code, using
INNER JOIN instead of a comma to indicate a join. (This is to avoid
precedence issues when performing LEFT JOINs).

--Ken

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

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Failed to generate a user instance of SQL Server. Only an integratedconnection can generate a user instance. Harlan Messinger ASP .Net 2 03-28-2010 06:51 PM
wsdl2java: method parameter a complex type that extends another complex type Robert Mark Bram Java 0 02-04-2007 10:06 AM
[ANN] SqlStatement 1.0.0 - hide the syntax of SQL behind familiarruby syntax Ken Bloom Ruby 3 10-09-2006 06:46 PM
[XML Schema] Content type of complex type definition with complex content Stanimir Stamenkov XML 2 10-25-2005 10:16 AM
For expert on complex loops (reposted) - complex looping problem news.amnet.net.au Java 1 04-13-2004 07:10 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57