6

I'm looking for a way to define queries on sets independently from a programming language or the kind of sets.

In detail this would be a language definition and implementations for common languages like Java, C++, Python etc.

As commented I'm not looking for a database or any implementation of a set-representation but only a way to define a query for elements from e.g. a std::set/vector a Python set() or any linear structure which can be seen as a set.

A close example would be something like jLinq but without being tied to JSON or javascript and with a well defined string representation.

Of course without knowing the kind of data structure you would have to implement any conditional filter for every problem and every programming language, but the way you construct query strings and how you evaluate them would be clear and you would not have to write parsers.

So what I'd like to write in Java or C++ is something like

q = query()
.created_after("14.03.2010")
.and(contains("hello")
     .or(contains("hallo")))
.sort("caption")

or written as a string:

"(created_after("14.03.2010") and ( contains("hello") or contains("hallo"))) sort("caption")"

(this is not thought through - just to show what an interface could look like)

A good example for a different problem would be JSON or XML: clear language definition and parsers/tools for any platform or programming language.

frans
  • 8,868
  • 11
  • 58
  • 132
  • So, a database? There's literally a billion dollar industry and several major open source projects solving many variations of this problem. –  Mar 28 '14 at 14:11
  • No, I'm not looking for a database but a standard query language for generic sets (rather than databases with tables and fields) – frans Mar 28 '14 at 14:41
  • Why can't you model these sets as tables and write SQL queries? –  Mar 28 '14 at 15:27
  • This would indeed be an option. But I'd like to see something less verbose than SQL and more dedicated to what I want to achieve. So if you see this query language as an interface to, let's say an std::set I don't want to come up with dummy tables or fields just to be conform with a SQL database (which would be more a workaround than good design) – frans Mar 28 '14 at 16:00
  • For java/scala at least, there is http://metamodel.apache.org/ which seems to offer something somewhat similar: (EDIT: see their example on that page). I feel like this could be easily generalized to other languages (hopefully something that's on their roadmap) – acros Jun 17 '17 at 20:03

2 Answers2

1

I know this is an old question, but I think I know what you mean and I was actually looking for something similar. What you need is a "search query parser".

I found search-query-parser for nodejs (I'm not the author). Haven't tried it yet but looks promising.The example in the docs is very illustraring, you would receive an input string from the UI

from:hi@retrace.io,foo@gmail.com to:me subject:vacations date:1/10/2013-15/04/2014 photos

And the library would parse it to a structured json object

{
  from: ['hi@retrace.io', 'foo@gmail.com'],
  to: 'me',
  subject: 'vacations',
  date: {
    from: '1/10/2013',
    to: '15/04/2014'
    },
  text: 'photos'
}

And the from that object you could construct and issue a query command to your database. As you can see it handles lists and ranges. Right away I can't see any boolean operator (AND,OR) but I guess could be easily implemented.

Hope this helps.

Community
  • 1
  • 1
Isaac
  • 636
  • 7
  • 9
0

RSQL is a good option these days. There are plenty of parsers available and the queries are URL friendly.

Phyxx
  • 15,730
  • 13
  • 73
  • 112