On Mon, Apr 6, 2020, at 12:31 PM, Massimiliano della Rovere wrote:
> 
> 
> 
> Il giorno lun 6 apr 2020 alle ore 16:30 Mike Bayer <mike...@zzzcomputing.com> 
> ha scritto:
>> __
>> 
>> On Mon, Apr 6, 2020, at 5:42 AM, Massimiliano della Rovere wrote:
>>> In SQLAlchemy CORE is there any way to use bindparam() to late-bind the 
>>> type_ parameter of the func.cast function instead of a column?
>>> 
>>> I wrote a function returning a "baked query", that extracts settings stored 
>>> in a XML column. I want the extracted value to be cast to a type depending 
>>> on the specific extracted value (it depends on the xpath bound parameter).
>> 
>> OK so, this is actually the "type" within a CAST so this is not something 
>> that gets "bound" into a SQL query, "bound parameters" are only used for 
>> data values, not syntactical elements of a query as a type would be.
>> 
>> what's not clear here is when it is that you know what this type would be. 
>> do you know what the type would be before you invoke the query, or is the 
>> type a product of something in the data that's being read? if the latter 
>> case, you'd need to use a CASE so that the determination is made within the 
>> SQL process.
> 
> Hi Mike, thanks for your answer.
> The type is related to the xpath as in bindparam("xpath").
> I know the type only when the query is passed to connection.execute(),and 
> only the caller of bake_extract_settings_query() knows the type; what I am 
> writing is at framework-level, someone else - with the knowledge of <xpath, 
> types> pairing will write the code with the .execute().
> That said, I should add that in theory the xpath should contain not just the 
> value as text() but also an attribute specifying the type, coded with symbols 
> known to the application, e.g. 'DH' = TIMESTAMP, but the presence of the 
> attribute is not guaranteed.
> So it would be best to force the caller of .execute() to pass both xpath=... 
> and output_type=...
> 
> This is the reason why I did not use CASE. 
> Also we are using asyncio and sqlalchemy-aio, so CPU-bound activities should 
> be limited as much as possible, that's the reason why I'd like the cast to be 
> performed by the DB and not in python.

if the caller of bake_extract_settings_query() knows the type then it is most 
expedient that they pass it right there. 

Otherwise, you have to invent some way for this parameter to be sent somewhere, 
it can be in lots of places, like 
connection.execution_options(my_special_parameter="foo") or something like 
that, then use either replacement traversal with before_execute() or string 
replacement with before_cursor_execute().

however if you are using sqlalchemy-aio, I have no idea what event hooks that 
system supports as those tools typically have re-implemented the statement 
execution internals.







> 
> 

> --
>  SQLAlchemy - 
>  The Python SQL Toolkit and Object Relational Mapper
> 
> http://www.sqlalchemy.org/
> 
>  To post example code, please provide an MCVE: Minimal, Complete, and 
> Verifiable Example. See http://stackoverflow.com/help/mcve for a full 
> description.
>  --- 
>  You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
>  To unsubscribe from this group and stop receiving emails from it, send an 
> email to sqlalchemy+unsubscr...@googlegroups.com.
>  To view this discussion on the web visit 
> https://groups.google.com/d/msgid/sqlalchemy/CADKhPGSCcVraBZRc3CgxXNkypH%3D2u%2Bi%3DmqH4Zf2rdPQQW6RmSQ%40mail.gmail.com
>  
> <https://groups.google.com/d/msgid/sqlalchemy/CADKhPGSCcVraBZRc3CgxXNkypH%3D2u%2Bi%3DmqH4Zf2rdPQQW6RmSQ%40mail.gmail.com?utm_medium=email&utm_source=footer>.

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/f43043c8-b771-4a30-88e5-2d10ceabc49a%40www.fastmail.com.

Reply via email to