tags:

views:

118

answers:

6

Sql is the standard in query languages, however it is sometime a bit verbose. I am currently writing limited query language that will make my common queries quicker to write and with a bit less mental overhead.

If you write a query over a good database schema, essentially you will be always joining over the primary key, foreign key fields so I think it should be unnecessary to have to state them each time.

So a query could look like.

select s.name, region.description from shop s
where monthly_sales.amount > 4000 and s.staff < 10

The relations would be

shop -- many to one -- region,

shop -- one to many -- monthly_sales

The sql that would be eqivilent to would be

select distinct s.name, r.description 
from shop s 
join region r on shop.region_id = region.region_id 
join monthly_sales ms on ms.shop_id = s.shop_id  
where ms.sales.amount > 4000 and s.staff < 10

(the distinct is there as you are joining to a one to many table (monthly_sales) and you are not selecting off fields from that table)

I understand that original query above may be ambiguous for certain schemas i.e if there the two relationship routes between two of the tables. However there are ways around (most) of these especially if you limit the schema allowed. Most possible schema's are not worth considering anyway.

I was just wondering if there any attempts to do something like this? (I have seen most orm solutions to making some queries easier)

EDIT: I actually really like sql. I have used orm solutions and looked at linq. The best I have seen so far is SQLalchemy (for python). However, as far as I have seen they do not offer what I am after.

+2  A: 

Hibernate and LinqToSQL do exactly what you want

Nir Levy
LINQ-to-SQL doesn't have a query language of its own; and it doesn't provide a query syntax either - that is provided by LINQ more generally... i.e. it is more a **consumer** than a **provider** of this (although obviously the mapping etc involved is non-trivial).
Marc Gravell
The linq stuff that I have seen seems very limited and does not write the sql how is best always. The orms as I have said do not seem to be what I am after. The best I have seen is for python (sqlalchemy).
David Raznick
(i'll repeat what was said above: sorry, I assumed you were a newbie. I retract my answer, but my Inner Rep Whore is going to leave it up)Now seriously, what you are asking for is near magical, sort of "guess what is wanted to do and do it" query language. If you find something like this then please let us know as i would love to use one myself :0). I think hibernate does actually a rather good job with HQL, but not as clever as you want it.
Nir Levy
It would not be magical if the query language knew what the schema was. It does, as it knows the primary key and foreign key fields. Hql does quite a good job. The where clause above you could go shop.monthly_sales.amount > 4000. You still have to remember the join path and join property name though, especially for 2 or more joins deep. The mental overhead is low for one query but when automating various queries or doing 20 in a day it gets tiring especially when most of the time there is a systematic way of finding it. I would be happy in the face of ambiguity "an error".
David Raznick
+2  A: 

I believe that any (decent) ORM would be of help here..

Mez
+1  A: 

Entity SQL is slightly higher level (in places) than Transact SQL. Other than that, HQL, etc. For object-model approaches, LINQ (IQueryable<T>) is much higher level, allowing simple navigation:

var qry = from cust in db.Customers
          select cust.Orders.Sum(o => o.OrderValue);

etc

Marc Gravell
+3  A: 

I think you'd be better off spending your time just writing more SQL and becoming more comfortable with it. Most developers I know have gone through just this progression, where their initial exposure to SQL inspires them to bypass it entirely by writing their own ORM or set of helper classes that auto-generates the SQL for them. Usually they continue adding to it and refining it until it's just as complex (if not more so) than SQL. The results are sometimes fairly comical - I inherited one application that had classes named "And.cs" and "Or.cs", whose main functions were to add the words " AND " and " OR ", respectively, to a string.

SQL is designed to handle a wide variety of complexity. If your application's data design is simple, then the SQL to manipulate that data will be simple as well. It doesn't make much sense to use a different sort of query language for simple things, and then use SQL for the complex things, when SQL can handle both kinds of thing well.

MusiGenesis
I have been doing sql for analytics for years and actually love the language. However when developing with it quickly I have noticed many, many repetitive things I keep on doing that end up wasting a lot of my time. The best sql people I know generate it with perl. I was wondering if there was more of a standard higher level solutions for people that DO know what they are doing with sql.
David Raznick
Sorry, I assumed you were a newbie. I retract my answer, but my Inner Rep Whore is going to leave it up.
MusiGenesis
A: 

Martin Fowler plumbed a whole load of energy into this and produced the Active Record pattern. I think this is what you're looking for?

Gav
A: 

Not sure if this falls in what you are looking for but I've been generating SQL dynamically from the definition of the Data Access Objects; the idea is to reflect on the class and by default assume that its name is the table name and all properties are columns. I also have search criteria objects to build the where part. The DAOs may contain lists of other DAO classes and that directs the joins.

Since you asked for something to take care of most of the repetitive SQL, this approach does it. And when it doesn't, I just fall back on handwritten SQL or stored procedures.

Otávio Décio