Remedial Query JOINs

September 13, 2007 22:39

Support for SQL JOIN syntax is probably our most requested Query feature addition. As you might imagine, fully supporting JOINs while maintaining the simplicity of the SubSonic query syntax is no small chore.

Rob and I chatted about this late last week, and we both agreed that if SubSonic were to support JOINs, it should do so in a way that makes them easy to work with, and not merely port the SQL syntax into the parser. Personally, I find SQL JOIN syntax convoluted and truly believe that there must a more friendly and readable way to the same ends. In Revision 188, you'll find my first stab at making this happen.

First off, a few caveats:

  • The capability is currently supported only on Sql Server
  • Supplementary syntax (WHERE, ORDER BY) etc. are not supported. If they do actually work, it's only out of pure luck
  • LEFT INNER JOIN support only

Creating a JOINed Query

To facilitate JOIN support, a new Query constructor overload has been added. This overload takes a table schema reference, and one or more table columns as parameters.

Query qry = new Query(Product.Schema, Product.ProductIDColumn, 
Product
.ProductNameColumn, Supplier.CompanyNameColumn, Category.CategoryIDColumn);

 

The column properties represent new static properties in the generated classes, so you'll need to regenerate any existing classes in order to perform these queries. This query syntax ultimately generates the following SQL and result set:

SELECT ProductID, ProductName, [J2].[CompanyName], [J3].[CategoryID] 
FROM [dbo].[Products]
INNER JOIN [Suppliers] J2 ON [Products].[SupplierID] = [J2].[SupplierID]
INNER
JOIN [Categories] J3 ON [Products].[CategoryID] = [J3].[CategoryID]


 

image

So consider this just a first pass, with more to come. As always, we welcome any feedback and ideas, so please add your comments!


14 Comments
Actions: E-mail | Permalink | Comment RSSRSS comment feed

Related posts

Comments

September 14. 2007 01:02

Stu Allen (photoz)

Long last, the join is back on the table.

Has there been any thought as to how this could be output in any other way other than just an IReader?

Would be cool to somehow come up with dynamic types that are hybrid model object/collections.

Stu Allen (photoz)

September 14. 2007 03:07

Keep it coming, this looks really great eric!

Dave

September 14. 2007 08:46

Jon Galloway

I like it. I've had that exact same thought - I've already defined my foreign keys to the database, why does SQL require me to spell them out?

Very cool, looking forward to see where you take this.

Jon Galloway

September 14. 2007 13:33

kevin

+1

I like it. Makes sense (the overload). Reads well.

Glad to see this moving forward.

kevin

September 14. 2007 18:56

Ibleif

Looking forward to this! The site has problems showing your "code-examples".

Ibleif

September 14. 2007 19:35

Eric Kemp

Yeah, sorry about that... Not too happy with my current code formatter. Hopefully it's a little easier to read now...

Eric Kemp

September 15. 2007 03:36

wuchang

SubSonic can use in winform application?

wuchang

September 20. 2007 22:34

Zharfan Mazli

Great stuff. Keep up

Zharfan Mazli

November 21. 2007 08:59

Alvin Moreno

This is great! When will this be available? We badly need it for our project.

Thanks!

Alvin Moreno

January 4. 2008 18:45

Steve

I am currently using 2.03 version, where I do not see the overloaded method for QUery mentioned here. IS it still in beta phase. How can I use it?

Steve

January 11. 2008 09:49

Jonas

I can't find this overload, is it there yet?

Jonas

July 2. 2008 22:30

Hannah

I have the same question as Steve's, and I don't see the overloaded method for Query metnioned here. Where can I find it??? Please help. Thx.

Hannah

February 27. 2009 01:28

Alfredo Cerrillo

And what about joins between tables from different databases? In SQL is simple as this: select t1.somefield, t2.otherfield from table1 t1 join otherdatabase..table2 t2 on ... I can't find any way to do this with Subsonic... and finally I had write an store procedure.

Alfredo Cerrillo

July 20. 2011 22:44

pingback

Pingback from sfmlsdeals.bestmedicament.com

Syntax convoluted | Sfmlsdeals

sfmlsdeals.bestmedicament.com

Comments are closed