How to parse and classify N1QL queries as read or write in Java SDK

My team is trying to work out how to separate N1QL read queries from write queries, as our system is permission-based and not all users that can input queries should be able to perform writes.

The docs haven’t been much help on this. So far we’ve only found one way to do this with the SDK: marking queries as read-only for users who don’t have write permissions. This way the server will reject a query if it tries to write on DB.

I feel this is insufficient. We’d like to know when we first receive the query from the user if it’s going to write so we can handle it appropriately even without actually querying the cluster. Furthermore, I feel “readonly” was intended as a performance optimization, not an access control, as that’s the only use the docs mention.

Does any SDK class actually parse the query? Is this functionality exposed somewhere? Is there any way to retrieve this information? If the answer is no, we will probably just use readonly, because mantaining our own N1QL parser seems pretty overkill.

tl;dr any way to tell from Java code if a N1QL query is “read” or “write”?

Thank you.

You can use RBAC permissions to control this. N1QL will not inform the query is readonly or write. First it needs to parse the query to do. If not prepare statement it must execute too.

Mostly SELECT statements are readonly, all other DDLs,DMLs are write

If you need that early try AST tree generation described by @eben AST Generator for N1QL make your own decision.