The Phrasebook Design Pattern, SQL and YAML

Hi all,

I’ve been looking at the Phrasebook Design Pattern recently.
Specifically, I’d like to use it for externalizing SQL statements. Even
more specifically, I’d like to use Ruby with YAML (versus XML, for
example).

For a quick rundown on what I’m talking about, download the Sharon and
Pinchuk article:
http://jerry.cs.uiuc.edu/~plop/plop2k/proceedings/Pinchuk/Pinchuk.pdf

Also visit: http://www.perl.com/pub/a/2002/10/22/phrasebook.html
The article is perl specific, but generic enough to where I think you’ll
get the idea.

Also see http://lwn.net/Articles/14150/ for some issues with Perl’s
Class::Phrasebook that I wish to avoid at the outset.

Being a YAML neophyte, I’m trying to come up with a general ‘schema’ for
how the yaml should look for this, but I’m not entirely sure. Something
like:

sql.yml - very raw idea

main_query:
sql: select * from some_table, other_table where some_column = ? and
some_column = other_column
comment: run this first
vendor:
oracle:
hint: /*+ use some_other_index /
outer_join: true
postgres:
hint: /
I don’t know if postgres has hints */
outer_join: true

phone_query
sql: select tn from tn_table where tn = ?
comment: gets the telephone

I don’t think this parses the way I’d like. I’m now sure how it should
look and that’s where, for now, I need some help. The general idea is
to keep the SQL external from the program itself. Within the program,
with our imaginary API, you would do something like:

require "phrasebook/sql"
p = Phrasebook::SQL.new(sql.yml)

sth.execute( p.get(:sql => “main_query”,:vendor => “oracle”,:some_column
=> 5551212) )

Where p.get(:sql=>“main_query”) would return:

select /*+ use some_other index */
from some_table, other_table
where column = ?
and some_column = other_column (+)

Using this (theoretical) approach you can keep the vendor specific
portions of the SQL separate and let the phrasebook construct the sql as
appropriate. It would require some training to understand vendor
specific stuff - perhaps it could be put in a separate yaml file. :smiley:

So, in addition to comments about how the YAML should look, what do
folks think about a Ruby/SQL/YAML phrasebook in general? Comments,
suggestions, API specific ideas all welcome.

Regards,

Dan

···


a = [74, 117, 115, 116, 32, 65, 110, 111, 116, 104, 101, 114, 32, 82]
a.push(117,98, 121, 32, 72, 97, 99, 107, 101, 114)
puts a.pack(“C*”)

Hi all,

[elided a bunch of good stuff]

So, in addition to comments about how the YAML should look, what do
folks think about a Ruby/SQL/YAML phrasebook in general? Comments,
suggestions, API specific ideas all welcome.

I think this is a great idea. May I suggest that you register this with
the okay project? Having a standard for distributing a SQL phrase book
would really be nice.

-pate

···

On Sat, 29 Mar 2003, Daniel Berger wrote:

Regards,

Dan


a = [74, 117, 115, 116, 32, 65, 110, 111, 116, 104, 101, 114, 32, 82]
a.push(117,98, 121, 32, 72, 97, 99, 107, 101, 114)
puts a.pack(“C*”)

Yeah, cool. I’ve added a basic schema to your wiki page and a few comments.

My suggestion would be to take a look at okay/news.rb in the YAML.rb install.
Make an okay/sql.rb that simply provides loader/dumper/validator capability.
Also, I encourage all !okay types to be loaded as Ruby classes, not as Hashes
(or other underlying type). The schema should go at the end of the file.

The actual phrasebook translation should happen in a different module. So if
a person wanted to supply classes directly to the phrasebook, they could
easily do so. In addition, if someone wanted to supply a different backend
to okay/sql, it would be nicely decoupled for them.

That is all.

_why

···

On Friday 28 March 2003 02:55 pm, Daniel Berger wrote:

So, in addition to comments about how the YAML should look, what do
folks think about a Ruby/SQL/YAML phrasebook in general? Comments,
suggestions, API specific ideas all welcome.

For a quick rundown on what I’m talking about, download the Sharon
and Pinchuk article:
http://jerry.cs.uiuc.edu/~plop/plop2k/proceedings/Pinchuk/Pinchuk.pd
f

Also visit: The Phrasebook Design Pattern
The article is perl specific, but generic enough to where I think
you’ll get the idea.

Also see http://lwn.net/Articles/14150/ for some issues with
Perl’s Class::Phrasebook that I wish to avoid at the outset.

Huh. In my RSS implementation (ruby-rss.sourceforge.net), I’ve
basically been using the Phrasebook pattern for I18N error messages.

Cool.

I’m doing the same thing in the … attempt at making a PDF::Writer
that I’m doing.

Anyone know PDF really well?

-austin
– Austin Ziegler, austin@halostatue.ca on 2003.03.28 at 21:02:06

···

On Sat, 29 Mar 2003 06:55:32 +0900, Daniel Berger wrote:

Pat Eyler wrote:

Hi all,

[elided a bunch of good stuff]

So, in addition to comments about how the YAML should look, what do
folks think about a Ruby/SQL/YAML phrasebook in general? Comments,
suggestions, API specific ideas all welcome.

I think this is a great idea. May I suggest that you register this with
the okay project? Having a standard for distributing a SQL phrase book
would really be nice.

-pate

Sounds like a good idea. Just to make sure I understant properly, does
that simply entail creating a new type under the “Type Repository”
section call “!okay/sql”? If not, please correct me!

Regards,

Dan

···

On Sat, 29 Mar 2003, Daniel Berger wrote:


a = [74, 117, 115, 116, 32, 65, 110, 111, 116, 104, 101, 114, 32, 82]
a.push(117,98, 121, 32, 72, 97, 99, 107, 101, 114)
puts a.pack(“C*”)

Pat Eyler wrote:

Hi all,

[elided a bunch of good stuff]

So, in addition to comments about how the YAML should look, what do
folks think about a Ruby/SQL/YAML phrasebook in general? Comments,
suggestions, API specific ideas all welcome.

I think this is a great idea. May I suggest that you register this with
the okay project? Having a standard for distributing a SQL phrase book
would really be nice.

-pate

I’ve added the !okay/sql type to the OkayProject. Hope I did it right.

All - it’s a wiki - feel free to make comments, suggestions, etc.

Regards,

Dan

···

On Sat, 29 Mar 2003, Daniel Berger wrote:


a = [74, 117, 115, 116, 32, 65, 110, 111, 116, 104, 101, 114, 32, 82]
a.push(117,98, 121, 32, 72, 97, 99, 107, 101, 114)
puts a.pack(“C*”)

I’m doing the same thing in the … attempt at making a PDF::Writer
that I’m doing.

Anyone know PDF really well?

No, but if you haven’t you might wanna check out

http://www.reportlab.com/download.html

which has a PDF lib for Python which seems to have good reputation. A Ruby
extension with similar abilities would be cool imho.

Regards,

Robert Feldt

···

On Sat, 29 Mar 2003, Austin Ziegler wrote:

-austin
– Austin Ziegler, austin@halostatue.ca on 2003.03.28 at 21:02:06


Robert Feldt tel: +46-(0)31 772 5217 fax: +46-(0)31 772 3663
feldt@ce.chalmers.se or robert.feldt@computer.org
MSc, Ph.D. student
Chalmers Univ. of Technology, Dept. of Computer Engineering
Hörsalsvägen 11, SE-412 96 Gothenburg, Sweden