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

@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