-
Notifications
You must be signed in to change notification settings - Fork 261
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support raw-string queries #9
Comments
Is there a way that this could be combined with the SQL generators in SQLAlchemy drivers in order to add support for any databases that SQLALCHEMY supports? like oracle or odbc ones |
@mungojam The problem isn't the SQL generation, it's the drivers. The standard database drivers are all based on a thread-concurrency model, rather than the more co-operative concurrency (async). The interface layer (DBAPI) between SQLAlchemy and the database drivers is itself also inherently a thread-concurrency interface. The main point of the "databases" package is to wrap up some of the newer async drivers (asyncpg, aiomysql, etc.) together with SQLAlchemy's query generation and type coercion all together in a nice interface. |
When is it achieved? |
Yes, I'm missing this bit as well or even probably some details. It's not fully impossible though, as example, it seems to be ok to pass a raw query as
but it feels a bit hacky in this case, instead of I also would be happy to contribute, especially with some hints to align it with whole concept. UPDATE: Apparently there's a way to adjust the text object, considering the example above:
UPDATE 2: Oh, apparently it was already answered in encode/starlette#329. |
I'm not necessarily against us providing an interface onto the raw driver for cases where you want to drop down into a non-cross-database-compatible interface. Happy to consider any API suggestions there. Alternatively if there are more types of operation that we should be providing but currently aren't, then we should provide a cross-database-compatible interface onto those. Not familiar with |
It definitely sounds reasonable and I also think it's a better practice. Raw calls to the driver shouldn't be considered as a recommended approach, but can take a place for say 0.1-0.5% of cases when a developer is ready to be responsible for the action. While it'll take a bit of time for me to come up with a thorough suggestion about cross-db interfaces, I'd like to draft a PR for raw calls to discuss how it might look. |
I drafted some approaches, happy for any comments, suggestions, complains. There's also a subtle issue with using |
Yeah, I think we'll want to make sure integer indexing into the mapping still works for text, but document column name indexing as being unsupported in that case. (Also we might want to think about if we really want the records to be a Mapping interface, or an unpack-able Sequence.) ie. |
The problem only applies to |
Yeah - run
I think what we may want here will end up being a couple of different things...
def execute(query: typing.Union[Query,str], **kwargs: typing.Any):
if isinstance(query, str):
query = sqlalchemy.text(query)
query.bindparams(**kwargs)
elif kwargs:
query = query.values(**kwargs)
Support for this: Inside either of those block you are garunteed to be holding onto a single connection from the pool throughout. I'd been thinking we might want this in any case. Our existing style acquires and releases the connection as needed, and only strictly holds onto it if you're inside a transaction. This works great because we're in autocommit mode, so there's no particular need for each query to strictly be on the same connection, unless the developer explicitly requests a transaction around a block of queries. However I could imagine that there might be cases where a developer doesn't want a transaction, but does want to strictly acquire-and-hold the connection over an entire endpoint.
We'd probably address those each independantly, in turn. |
That sounds like a brilliant plan. So, for p. 3 I'd like to suggest the following PR.
Yeah, I think it's quite possible, at least UPDATE: Thanks for the review, fix the comments first. |
Might worth considering to expose |
No description provided.
The text was updated successfully, but these errors were encountered: