So I tried to hard code adhoc to False in the python sdk code. This resulted in no prepared statements being created. So adhoc=True and adhoc=False both behave like ahdoc=True and thus no prepared statments are ever created or used.
Looking a bit deeper, if adhoc=False:
return self._parent._n1ql_query(self._params.encoded, not self._params.adhoc, cross_bucket=self._params.cross_bucket)
not self._params.adhoc become equal to True
This code is then called:
This will set int prepared to 1 corresponding to "not adhoc" being true.
Then this is called:
This will call lcb_cmdquery_adhoc(cmd, 1) if prepared=1, which it does.
The lcb_cmdquery_adhoc code: https://github.com/couchbase/libcouchbase/blob/7a5e90542e59fc32f410a8d6c23fc3e597afc45f/src/n1ql/n1ql.cc#L203
This will turn tell LCB to run the query as an adhoc statement.
So by setting adhoc=False in python sdk, lcb will actually see a request for adhoc=True. This looks like it comes from the python sdk c library that switches terminology up a bit by translating adhoc to a variable called prepared by negativing which is then transalted to adhoc in lcb.
If I set adhoc=False in the sdk, then each query should check in the prepared cache for a corresponding prepared statement. If a statment is found, execute and if not found, prepare then execute.