Note that there are some explanatory texts on larger screens.

plurals
  1. POmacro-like function as filter in postgresql
    primarykey
    data
    text
    <p>I have a table with some RDF statements inside, like <code>Quads(graph, subject, verb, object)</code> and I find myself doing queries like:</p> <pre><code>select * from quads where verb = 'rdf:type' and object = 'smtg:Type' select * from quads where verb = 'rdf:label' and object = 'bla bla' </code></pre> <p>I would like to express this in a form like</p> <pre><code>select * from quads where type('smtg:Type') select * from quads where label('bla bla') </code></pre> <p>While this seems a "trivial" text substitution, I have no idea of how and if this would be implementable in postgresql, though I sort of expect this to be impossible. </p> <p>To the best of my knowledge I can use a function to hardcode the constants, and do</p> <pre><code>select * from quads where rdftype() = verb and object = 'smtg:Type' </code></pre> <p>or I can use a function as the <code>from</code> argument like</p> <pre><code>select * from typed('smtg:Type') </code></pre> <p>sadly, the former is quite verbose, and the latter does not seem to be amendable to combination e.g. I can't do an hypothetical</p> <pre><code>select * from quads where type('smtg:Type') and inGraph('mygraph') </code></pre> <p>Is there a way to do what I want? It seems impossible, but I wonder if there is a way.</p> <p><strong>EDIT</strong>: a sqlfiddle instance: <a href="http://sqlfiddle.com/#!1/40b2c/3" rel="nofollow">http://sqlfiddle.com/#!1/40b2c/3</a> . </p> <p>To be more clear, the reason why this seems unfeasible is that the macro-ish function would be returning a boolean (it is used in the <code>where</code> clause, as if it where a <code>select $1=somevalue</code>) but using an "implicit" row argument in which it would access multiple fields at once, and I have not seen such an example in the pg docs. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload