Sqlalchemy - how to get raw sql from insert(), update() statements with binded params?

前端 未结 2 1200
Happy的楠姐
Happy的楠姐 2020-12-16 17:47

Example:

from sqlalchemy.dialects import mysql
from sqlalchemy import Integer, Column, update, insert
from sqlalchemy.ext.declarative import declarative_base         


        
2条回答
  •  -上瘾入骨i
    2020-12-16 18:22

    The solution using compile_kwargs -- as advertised elsewhere -- works only partially, i.e. for simple data types like Integer or String (as of SQLAlchemy v1.1.6).

    +-------------+----------------+--------+---------------+----------+
    | SQA version | compile_kwargs | SELECT | INSERT/UPDATE | datetime |
    +=============+================+========+===============+==========+
    | 0.7.9       |       --       |    --  |        --     |    --    |
    +-------------+----------------+--------+---------------+----------+
    | 0.9.4       |       √        |   1/2  |        --     |    --    |
    +-------------+----------------+--------+---------------+----------+
    | 1.0.11      |       √        |    √   |        --     |    --    |
    +-------------+----------------+--------+---------------+----------+
    | 1.0.13      |       √        |    √   |       1/2     |    --    |
    +-------------+----------------+--------+---------------+----------+
    | 1.1.6       |       √        |    √   |       1/2     |    --    |
    +-------------+----------------+--------+---------------+----------+
    

    Depending on requirements and restrictions, the following solutions will do the job:

    • If you can use SQLAlchemy v1.0.13 or above, the simplest solution is provided in this answer. The LiteralDialect class presented there is still needed to provide correct quoting of data types like datetime. Although it is not complete, adding missing data types is very straightforward. And unfortunately you have to fix None to null() for INSERT/UPDATE.

    • If you can use SQLAlchemy v1.0.11 and need only SELECT statements (not INSERT/UPDATE), you can also use the solution from the above answer. SQLAlchemy v1.0.11 is distributed with ubuntu 16.04 xenial, the current LTS version (as of Mar 2017).

    If you are stuck with some older version of SQLAlchemy, there is no sweet and simple approach to the problem.

    So I came up with some code supporting SQL compilation of queries (SELECT) as well as INSERT and UPDATE statements. The code works with SQLAlchemy v0.7.9 - v1.1.6 and Python2/Python3.

    A detailed (and hilarious) analysis of the various features is included as doctest.

    The relevant code sections are:

    • function sql_statement
    • class LiteralDialect
    • functions fix_null_params, fix_null_values, null_if_none
    • variable COMPILE_KWARGS
    • and as a bonus table_sql_dump

提交回复
热议问题