Most query-languages for databases are not as elegant or expressive as CSS.
Has anyone attempted to use CSS selectors as the query language for databases?
This idea lends itself naturally to document databases - let's say you have the following document:
<blogpost title="This Week's Crazy Idea">
<user name="Rasmus" class="author"/>
<body>
...
</body>
</blogpost>
In a relational database, "blogpost" and "user" would be tables, and "title" and "name" would be columns in those tables.
As for the class-attribute, in a relational database, this might be represented as a bitmask or boolean columns according to some given rule, for example, the "author" class might be represented as a "class_author" column on the "user" table.
To select blog posts, one might do something along the lines of this:
$posts = select('blogpost:has(user.author[name="Rasmus"])');
In the relational database example, this might translate to something along the lines of:
SELECT
blogpost.*, author.*
FROM
blogpost
INNER JOIN
user AS author ON author.id = blogpost.author_id
WHERE
author.name = 'Rasmus'
Of course, there are numerous problems in terms of object-relational impedance mismatch, so this idea probably lends itself best to graph and/or document databases.
Furthermore, this of course only addresses querying - I haven't yet given too much thought to whether this would somehow work for updates and projection queries.
Just throwing this idea out there... :-)
Funny, haven't seen this post before. I used this technique in some projects now and it's quite suitable to give users a more complex query language than simple filtering (native SQL would not be allowed, of course). I made a presentation on this topic (http://de.slideshare.net/CarstenHetzel/20130912-sfugcgn-cssselektoren), but it's on german and uses Symfony2 in PHP.
The thing is, CSS is for hierarchical structures, which relational model typically aren't. But relational data models can (in most cases) be "seen" as hierarchical:
would result in:
With most CSS paser libs you can go quite far along this road. Unfortunately in most cases you (or at least I) end up with a solution fitting your customers needs - a generic solution might be possible, but is it really worth the effort? What is with sub queries? Aggregate functions? Orderings? On the other hand CSS has lots of pseudo-classes etc. which clearly make no sense in queries, e.g. "hover".
Well, what would the CSS(3) selector look like for 'Show all authors with more than three books'?
Regards, Carsten