Skip to content

Instantly share code, notes, and snippets.

@mindplay-dk
Created September 9, 2011 19:18
Show Gist options
  • Save mindplay-dk/1207086 to your computer and use it in GitHub Desktop.
Save mindplay-dk/1207086 to your computer and use it in GitHub Desktop.
CSS for database queries?

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... :-)

@Ibmurai
Copy link

Ibmurai commented Sep 15, 2011

I have a much better idea: Make CSS selectors more like SQL!

...Nah just kidding. Interesting idea... :)

Copy link

ghost commented Dec 2, 2013

Good idea.
What can I help for make t real?

Copy link

ghost commented Dec 2, 2013

and
select('blogpost:has(user.author[name="Rasmus"])')
.orderBy('name')

and
select('blogpost:has(user.author[name="Rasmus"])')
.groupBy('user.name')

@chetzel
Copy link

chetzel commented Aug 8, 2015

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:

Authors[lastname=Martin] Books

would result in:

select  b.*
from    books b
join      authors a
on        a.id = b.author_id
where  a.lastname = 'Martin';

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

@acoyiu
Copy link

acoyiu commented Oct 6, 2020

I am working on some similar things, wish css query becomes true, it’s good for all web developers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment