8 ms·
'When you query with ReQL you tack on functions and “compose” your data, with SQL you tell the query engine the steps necessary (aka prescribe) to return your d
by army 11y ago
'When you query with ReQL you tack on functions and “compose” your data, with SQL you tell the query engine the steps necessary (aka prescribe) to return your data:'
... what? ReQL and SQL are both declarative query languages: I don't really see the author is getting at. Is there an implication that SQL isn't declarative?
The only real difference is that the API is based around chaining function calls rather than expressing what is needed as a string - there are many SQL query builder APIs that will let you build SQL queries by chaining together function calls.
- bayesianhorse 11y agoOne real difference is that you get most of the benefits of an ORM framework right in the driver, without depending on other packages or frameworks. And the API is very consistent across different languages. The biggest problem with composing SQL strings is that you have to be very very careful about SQL injections, and if you deal with that in a slightly sophisticated, reusable manner you are half-way to an ORM already. As far as I can determine, the ReQL drivers make injection attacks very difficult.
- kbenson 11y agoThe biggest problem with a different query language for every project is that when they get around to implementing some of the more esoteric (but extremely useful) SQL features it may not match well with what they've designed so far, and it may be hard to implement for the devs, conceptualize for the users, or just be plain weirdly tacked on causing a cognitive mismatch in how it's used (some side channel, for instance). Using a query builder (or ORM) of some sort still allows the escape hatch of raw SQL to do those really crazy things that are sometimes needed for performance, or just because what you are trying to do is rather weird. SQL is a very mature language, it's unlikely you are going to run into something someone else hasn't before.
- nickik 11y agoDatomic uses datalog but also exposes lower levels of the data accses api. This allows people with special needs to drop down and do there own thing, while others can use datalog. It seams like a good idea to allow diffrent layers of data access.
- nulltype 11y agoRaw SQL still needs to be parsed and converted into some data structure. The difference I think is that in RQL you are just making the data structure on the client side then sending it to the server.
- bayesianhorse 11y agoAnother point of ORMs is to make queries reusable. In Django for example, you can reuse the same queryset in views, forms, templating and the admin. You can process and edit the queryset much easier than composed SQL strings. This should also be possible to build on top of ReQL, though I don't know any examples.
- WorldWideWayne 11y agoComposing SQL strings at runtime should be the very last resort in my opinion. This is what stored procedures and parameterized queries are for. Even if I am going to do dynamic SQL, I do it in a stored procedure if I can.
- bayesianhorse 11y agoThen you are one of the rare SQL-englightened beings. I still don't see how you can pass user input from, say, a python string into a stored procedure call without worrying about injections. Or converting between your app's data structures and whatever string is necessary for your stored procedure.
- PuercoPop 11y agolookup prepared statements.
- danneu 11y agoYour driver should be able to handle parameterized queries for you. query('SELECT * FROM users WHERE id = ANY ($1::int[])', [1, 2, 3]); query('SELECT * FROM users WHERE lower(uname) = lower($1)', 'foo'); Where's the injection vulnerability?
- bayesianhorse 11y agoSo I may not be an SQL expert, but why would it be difficult to produce an injection string for $1? Of course, if you supply it "guaranteed" integers, then you can't. Injections normally happen with user inputs, not constants.
- WorldWideWayne 11y agoBecause SQL query compilers generally don't execute the parameters. They do not just concatenate the given parameter strings into the query template and then run that. Instead, parameters are always treated as parameters, the query template is compiled and the parameters are passed into that compiled representation of the query where they are simply regarded as variables, not eval'd.
- TylerE 11y agoLet me give you an arbitrary, unknown sql string in a variable `s`, and I'll do the same with a ReQL query. The challenge is to make sure the column/field `age` is more than 20. My code is: query.filter(r.row['age'] > 20) What's yours? (Hint: start by writing a compliant SQL parser)
- joevandyk 11y agousing ActiveRecord: query.where('age > ?', 20) If I'm understanding you correctly.
- TylerE 11y agoThat's moving the goalposts. The original challenge was take an arbitrary SQL query stored in a string.
- scott_s 11y agoI believe army's point was that when using an SQL query builder API, one does not start with a string, but something which allows them to do a similar check that you showed. I'm also not sure how your comment replies to army's point. The point, as I understood it, is that it is not accurate to characterize SQL queries as steps that tell the engine what to do. SQL is declarative, and leaves the execution plan up to the database itself. army's comments about the API and strings were trying to point out the only perceived difference, which is not relevant to the question of declarative versus imperative.
- apalmer 11y agoWait what? Not sure what you are asking for "SELECT * FROM table WHERE age > 20"?
- pests 11y agoYou already have an existing SQL query in a string variable. You need to add the age > 20 condition to that query.
- wereHamster 11y ago> The only real difference is that the API is based around chaining function calls rather than expressing what is needed as a string The query in RethinkDB is very much an expression. In the JavaScript driver you build this expression with function calls. There are other drivers which let you build the expression in a much more declarative way (like my Haskell driver).