summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/sql/compiler.py
Commit message (Collapse)AuthorAgeFilesLines
...
* Post black reformattingMike Bayer2019-01-061-17/+14
| | | | | | | | | | | | | Applied on top of a pure run of black -l 79 in I7eda77fed3d8e73df84b3651fd6cfcfe858d4dc9, this set of changes resolves all remaining flake8 conditions for those codes we have enabled in setup.cfg. Included are resolutions for all remaining flake8 issues including shadowed builtins, long lines, import order, unused imports, duplicate imports, and docstring issues. Change-Id: I4f72d3ba1380dd601610ff80b8fb06a2aff8b0fe
* Run black -l 79 against all source filesMike Bayer2019-01-061-829/+1201
| | | | | | | | | | | | | | This is a straight reformat run using black as is, with no edits applied at all. The black run will format code consistently, however in some cases that are prevalent in SQLAlchemy code it produces too-long lines. The too-long lines will be resolved in the following commit that will resolve all remaining flake8 issues including shadowed builtins, long lines, import order, unused imports, duplicate imports, and docstring issues. Change-Id: I7eda77fed3d8e73df84b3651fd6cfcfe858d4dc9
* Allow optional *args with base AnsiFunctionMike Bayer2018-11-291-6/+11
| | | | | | | | | | | | | Amended the :class:`.AnsiFunction` class, the base of common SQL functions like ``CURRENT_TIMESTAMP``, to accept positional arguments like a regular ad-hoc function. This to suit the case that many of these functions on specific backends accept arguments such as "fractional seconds" precision and such. If the function is created with arguments, it renders the the parenthesis and the arguments. If no arguents are present, the compiler generates the non-parenthesized form. Fixes: #4386 Change-Id: Ic492ef177e4987cec99ec4d95f55292be8daa087
* Merge "Add Sequence to StrSQLCompiler"mike bayer2018-11-131-0/+3
|\
| * Add Sequence to StrSQLCompilerMike Bayer2018-11-101-0/+3
| | | | | | | | | | | | | | | | | | | | Added :class:`.Sequence` to the "string SQL" system that will render a meaningful string expression (``"<next sequence value: my_sequence>"``) when stringifying without a dialect a statement that includes a "sequence nextvalue" expression, rather than raising a compilation error. Fixes: #4144 Change-Id: Ia910f0e22008a7cde7597365954ede324101cf4d
* | Add new "all columns" naming convention tokensMike Bayer2018-11-101-16/+26
|/ | | | | | | | | | | | | | | | | | | | | | | | | | Added new naming convention tokens ``column_0N_name``, ``column_0_N_name``, etc., which will render the names / keys / labels for all columns referenced by a particular constraint in a sequence. In order to accommodate for the length of such a naming convention, the SQL compiler's auto-truncation feature now applies itself to constraint names as well, which creates a shortened, deterministically generated name for the constraint that will apply to a target backend without going over the character limit of that backend. Additional notes: 1. the SQLite dialect had a format_index method that was apparently not used, removed. 2. the naming convention logic has been applying the foreign key remote column spec to the naming convention, and not the actual column name. In the case where the referenced Table object uses .key inside the columns and these are what ForeignKey() references, the naming convention was doing the wrong thing. The patch here fixes this, however this isn't noted in the migration notes. Fixes: #3989 Change-Id: Ib24f4754b886676096c480fc54b2e5c2463ac99a
* Support tuples of heterogeneous types for empty expanding INMike Bayer2018-10-031-1/+1
| | | | | | | | | Pass a list of all the types for the left side of an IN expression to the visit_empty_set_expr() method, so that the "empty expanding IN" can produce clauses for each element. Fixes: #4271 Change-Id: I2738b9df2292ac01afda37f16d4fa56ae7bf9147
* Allow dialects to customize group by clause compilationSamuel Chou2018-09-201-9/+13
| | | | | | | | | | | Refactored :class:`.SQLCompiler` to expose a :meth:`.SQLCompiler.group_by_clause` method similar to the :meth:`.SQLCompiler.order_by_clause` and :meth:`.SQLCompiler.limit_clause` methods, which can be overridden by dialects to customize how GROUP BY renders. Pull request courtesy Samuel Chou. Change-Id: I0a7238e55032558c27a0c56a72907c7b883456f1 Pull-request: https://github.com/zzzeek/sqlalchemy/pull/474
* Add concept of "implicit boolean", treat as nativeMike Bayer2018-08-151-2/+4
| | | | | | | | | | | | | | | Fixed issue that is closely related to :ticket:`3639` where an expression rendered in a boolean context on a non-native boolean backend would be compared to 1/0 even though it is already an implcitly boolean expression, when :meth:`.ColumnElement.self_group` were used. While this does not affect the user-friendly backends (MySQL, SQLite) it was not handled by Oracle (and possibly SQL Server). Whether or not the expression is implicitly boolean on any database is now determined up front as an additional check to not generate the integer comparison within the compliation of the statement. Fixes: #4320 Change-Id: Iae0a65e5c01bd576e64733c3651e1e1a1a1b240c
* Fixed issue with :meth:`.TypeEngine.bind_expression` andMike Bayer2018-08-081-7/+16
| | | | | | | | | | | | :meth:`.TypeEngine.column_expression` methods where these methods would not work if the target type were part of a :class:`.Variant`, or other target type of a :class:`.TypeDecorator`. Additionally, the SQL compiler now calls upon the dialect-level implementation when it renders these methods so that dialects can now provide for SQL-level processing for built-in types. Change-Id: Ic7b39575184db582e628e6ecee48dcda7d03a817 Fixes: #3981
* Add support of empty list in exanding of bindparamNicolas Rolin2018-08-071-0/+6
| | | | | | | | | | | Added new logic to the "expanding IN" bound parameter feature whereby if the given list is empty, a special "empty set" expression that is specific to different backends is generated, thus allowing IN expressions to be fully dynamic including empty IN expressions. Fixes: #4271 Change-Id: Icc3c73bbd6005206b9d06baaeb14a097af5edd36 Pull-request: https://github.com/zzzeek/sqlalchemy/pull/432
* Include UPDATE/DELETE extra_froms in correlationMike Bayer2018-08-041-12/+15
| | | | | | | | | | | | | | | | | Fixed bug where the multi-table support for UPDATE and DELETE statements did not consider the additional FROM elements as targets for correlation, when a correlated SELECT were also combined with the statement. This change now includes that a SELECT statement in the WHERE clause for such a statement will try to auto-correlate back to these additional tables in the parent UPDATE/DELETE or unconditionally correlate if :meth:`.Select.correlate` is used. Note that auto-correlation raises an error if the SELECT statement would have no FROM clauses as a result, which can now occur if the parent UPDATE/DELETE specifies the same tables in its additional set of tables ; specify :meth:`.Select.correlate` explicitly to resolve. Change-Id: Ie11eaad7e49af3f59df11691b104d6359341bdae Fixes: #4313
* support functions "as binary comparison"Mike Bayer2018-07-091-0/+3
| | | | | | | | | Added new feature :meth:`.FunctionElement.as_comparison` which allows a SQL function to act as a binary comparison operation that can work within the ORM. Change-Id: I07018e2065d09775c0406cabdd35fc38cc0da699 Fixes: #3831
* Merge "Support JOIN in UPDATE..FROM"mike bayer2018-06-251-4/+13
|\
| * Support JOIN in UPDATE..FROMMike Bayer2018-06-131-4/+13
| | | | | | | | | | | | | | | | | | | | The :class:`.Update` construct now accommodates a :class:`.Join` object as supported by MySQL for UPDATE..FROM. As the construct already accepted an alias object for a similar purpose, the feature of UPDATE against a non-table was already implied so this has been added. Change-Id: I7b2bca627849384d5377abb0c94626463e4fad04 Fixes: #3645
* | render WITH clause after INSERT for INSERT..SELECT on Oracle, MySQLMike Bayer2018-06-181-2/+7
|/ | | | | | | | | | | | | | | | Fixed INSERT FROM SELECT with CTEs for the Oracle and MySQL dialects, where the CTE was being placed above the entire statement as is typical with other databases, however Oracle and MariaDB 10.2 wants the CTE underneath the "INSERT" segment. Note that the Oracle and MySQL dialects don't yet work when a CTE is applied to a subquery inside of an UPDATE or DELETE statement, as the CTE is still applied to the top rather than inside the subquery. Also adds test suite support CTEs against backends. Change-Id: I8ac337104d5c546dd4f0cd305632ffb56ac8bf90 Fixes: #4275 Fixes: #4230
* Ensure all visit_sequence accepts **kw argsMike Bayer2018-04-041-1/+1
| | | | | | | | | | Fixed issue where the compilation of an INSERT statement with the "literal_binds" option that also uses an explicit sequence and "inline" generation, as on Postgresql and Oracle, would fail to accommodate the extra keyword argument within the sequence processing routine. Change-Id: Ibdab7d340aea7429a210c9535ccf1a3e85f074fb Fixes: #4231
* Track if we're rendering within the CTE recursivelyMike Bayer2018-03-141-1/+9
| | | | | | | | | | Fixed a regression that occurred from the previous fix to :ticket:`4204` in version 1.2.5, where a CTE that refers to itself after the :meth:`.CTE.alias` method has been called would not refer to iself correctly. Change-Id: Iaa63d65ad2b90c8693f9953fbb32dbb10c73a037 Fixes: #4204
* Check existing CTE for an alias name when rendering FROM clauseMike Bayer2018-03-011-50/+56
| | | | | | | | | | Fixed bug in CTE rendering where a :class:`.CTE` that was also turned into an :class:`.Alias` would not render its "ctename AS aliasname" clause appropriately if there were more than one reference to the CTE in a FROM clause. Change-Id: If8cff27a2f4faa5eceb59aa86398db6edb3b9e72 Fixes: #4204
* Quote cte alias if neededEric Atkin2018-02-221-0/+2
| | | | | | | | | | Fixed bug where CTE expressions would not have their name or alias name quoted when the given name is case sensitive or otherwise requires quoting. Pull request courtesy Eric Atkin. Fixes: #4197 Change-Id: Ib8573e82b9a1ca94b50c7c5d73ee98b79465d689 Pull-request: https://github.com/zzzeek/sqlalchemy/pull/426
* Merge "Make column-level collation quoting dialect-specific"mike bayer2018-01-121-1/+12
|\
| * Make column-level collation quoting dialect-specificMike Bayer2018-01-121-1/+12
| | | | | | | | | | | | | | | | | | | | | | | | | | Fixed regression in 1.2 where newly repaired quoting of collation names in :ticket:`3785` breaks SQL Server, which explicitly does not understand a quoted collation name. Whether or not mixed-case collation names are quoted or not is now deferred down to a dialect-level decision so that each dialect can prepare these identifiers directly. Change-Id: Iaf0a8123d9bf4711219e320896bb28c5d2649304 Fixes: #4154
* | happy new yearMike Bayer2018-01-121-1/+1
|/ | | | Change-Id: I3ef36bfd0cb0ba62b3123c8cf92370a43156cf8f
* Implement an error lookupMike Bayer2017-12-271-5/+5
| | | | | | | | Add codes to commonly raised error messages and classes that link back to fixed documentation sections giving background on these messages. Change-Id: I78d0660add7026bb662e20305a59283b20616954
* Allow delete where clause to refer multiple tables.inytar2017-12-051-6/+55
| | | | | | | | | | | | | | | | | | | | | Implemented "DELETE..FROM" syntax for Postgresql, MySQL, MS SQL Server (as well as within the unsupported Sybase dialect) in a manner similar to how "UPDATE..FROM" works. A DELETE statement that refers to more than one table will switch into "multi-table" mode and render the appropriate "USING" or multi-table "FROM" clause as understood by the database. Pull request courtesy Pieter Mulder. For SQL syntaxes see: Postgresql: https://www.postgresql.org/docs/current/static/sql-delete.html MySQL: https://dev.mysql.com/doc/refman/5.7/en/delete.html#multiple-table_syntax MSSQL: https://docs.microsoft.com/en-us/sql/t-sql/statements/delete-transact-sql Sybase: http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.infocenter.dc00801.1510/html/iqrefso/X315721.htm Co-authored by: Mike Bayer <mike_mp@zzzcomputing.com> Change-Id: I6dfd57b49e44a095d076dc493cd2360bb5d920d3 Pull-request: https://github.com/zzzeek/sqlalchemy/pull/392 Fixes: #959
* Implement placeholders for CUBE, ROLLUP, GROUPING SETSreview/mike_bayer/pr_github_383Spitcyn2017-09-131-1/+4
| | | | | | | | Fixes: #3429 Co-authored-by: Mike Bayer <mike_mp@zzzcomputing.com> Change-Id: I870ee7dc801d553c5309c291402ec468b671e9a9 Pull-request: https://github.com/zzzeek/sqlalchemy/pull/383
* Allow for multiple FOLLOWING/PRECEDING in a window rangeMike Bayer2017-08-251-2/+11
| | | | | | | | | | | Altered the range specification for window functions to allow for two of the same PRECEDING or FOLLOWING keywords in a range by allowing for the left side of the range to be positive and for the right to be negative, e.g. (1, 3) is "1 FOLLOWING AND 3 FOLLOWING". Change-Id: I7d3a6c641151bb49219104968d18dac2266f3db8 Fixes: #4053
* Add support for CACHE and ORDER to sequencesDavid Moore2017-07-051-0/+4
| | | | | | | | | | | Added new keywords :paramref:`.Sequence.cache` and :paramref:`.Sequence.order` to :class:`.Sequence`, to allow rendering of the CACHE parameter understood by Oracle and PostgreSQL, and the ORDER parameter understood by Oracle. Pull request courtesy David Moore. Change-Id: I082c3f8ef56ef89dbaad5da9d5695be5313b0614 Pull-request: https://bitbucket.org/zzzeek/sqlalchemy/pull-requests/96
* Add new "expanding" feature to bindparam()Mike Bayer2017-04-071-6/+28
| | | | | | | | | | | | | | | | Added a new kind of :func:`.bindparam` called "expanding". This is for use in ``IN`` expressions where the list of elements is rendered into individual bound parameters at statement execution time, rather than at statement compilation time. This allows both a single bound parameter name to be linked to an IN expression of multiple elements, as well as allows query caching to be used with IN expressions. The new feature allows the related features of "select in" loading and "polymorphic in" loading to make use of the baked query extension to reduce call overhead. This feature should be considered to be **experimental** for 1.2. Fixes: #3953 Change-Id: Ie708414a3ab9c0af29998a2c7f239ff7633b1f6e
* Double percent signs based on paramstyle, not dialectMike Bayer2017-04-051-8/+20
| | | | | | | | | | | | This patch moves the "doubling" of percent signs into the base compiler and makes it completely a product of whether or not the paramstyle is format/pyformat or not. Without this paramstyle, percent signs are not doubled across text(), literal_column(), and column(). Change-Id: Ie2f278ab1dbb94b5078f85c0096d74dbfa049197 Fixes: #3740
* Apply SQL compilation to sqltext for column-level CHECK constraintMike Bayer2017-04-041-1/+3
| | | | | | | | | | | | | Fixed bug where a column-level :class:`.CheckConstraint` would fail to compile the SQL expression using the underlying dialect compiler as well as apply proper flags to generate literal values as inline, in the case that the sqltext is a Core expression and not just a plain string. This was long-ago fixed for table-level check constraints in 0.9 as part of :ticket:`2742`, which more commonly feature Core SQL expressions as opposed to plain string expressions. Change-Id: I1301ba4b40063e91bc47726aecc5f4990ffcaeda Fixes: #3957
* New features from python 2.7Катаев Денис2017-03-171-5/+5
| | | | | | | After bump minimum supported version to 2.7 (1da9d3752160430c91534a8868ceb8c5ad1451d4), we can use new syntax. Change-Id: Ib064c75a00562e641d132f9c57e5e69744200e05 Pull-request: https://github.com/zzzeek/sqlalchemy/pull/347
* Implement comments for tables, columnsFrazer McLean2017-03-171-3/+26
| | | | | | | | | | | | | | Added support for SQL comments on :class:`.Table` and :class:`.Column` objects, via the new :paramref:`.Table.comment` and :paramref:`.Column.comment` arguments. The comments are included as part of DDL on table creation, either inline or via an appropriate ALTER statement, and are also reflected back within table reflection, as well as via the :class:`.Inspector`. Supported backends currently include MySQL, Postgresql, and Oracle. Co-authored-by: Mike Bayer <mike_mp@zzzcomputing.com> Fixes: #1546 Change-Id: Ib90683850805a2b4ee198e420dc294f32f15d35d
* Add "empty in" strategies; default to "static"Mike Bayer2017-03-141-0/+24
| | | | | | | | | | | | | | | The longstanding behavior of the :meth:`.Operators.in_` and :meth:`.Operators.not_in_` operators emitting a warning when the right-hand condition is an empty sequence has been revised; a new flag :paramref:`.create_engine.empty_in_strategy` allows an empty "IN" expression to generate a simple boolean expression, or to invoke the previous behavior of dis-equating the expression to itself, with or without a warning. The default behavior is now to emit the simple boolean expression, allowing an empty IN to be evaulated without any performance penalty. Change-Id: I65cc37f2d7cf65a59bf217136c42fee446929352 Fixes: #3907
* Don't cache savepoint identifiersMike Bayer2017-03-061-1/+7
| | | | | | | | | | | | | | | | | | | | | | | | Fixed bug in compiler where the string identifier of a savepoint would be cached in the identifier quoting dictionary; as these identifiers are arbitrary, a small memory leak could occur if a single :class:`.Connection` had an unbounded number of savepoints used, as well as if the savepoint clause constructs were used directly with an unbounded umber of savepoint names. The memory leak does **not** impact the vast majority of cases as normally the :class:`.Connection`, which renders savepoint names with a simple counter starting at "1", is used on a per-transaction or per-fixed-number-of-transactions basis before being discarded. The savepoint name in virtually all cases does not require quoting at all, however to support potential third party use cases the "check for quotes needed" logic is retained, at a small performance cost. Uncondtionally quoting the name is another option, but this would turn the name into a case sensitive name which runs the risk of poor interactions with existing deployments that may be looking at these names in other contexts. Change-Id: I6b53c96abf7fdf1840592bbca5da81347911844c Fixes: #3931
* Support python3.6Mike Bayer2017-01-131-1/+1
| | | | | | | | | | | Corrects some warnings and adds tox config. Adds DeprecationWarning to the error category. Large sweep for string literals w/ backslashes as this is common in docstrings Co-authored-by: Andrii Soldatenko Fixes: #3886 Change-Id: Ia7c838dfbbe70b262622ed0803d581edc736e085 Pull-request: https://github.com/zzzeek/sqlalchemy/pull/337
* Merge "Tighten rules for order_by(Label) resolution"mike bayer2017-01-091-6/+6
|\
| * Tighten rules for order_by(Label) resolutionMike Bayer2017-01-061-6/+6
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | - Fixed bug originally introduced in 0.9 via :ticket:`1068` where order_by(<some Label()>) would order by the label name based on name alone, that is, even if the labeled expression were not at all the same expression otherwise present, implicitly or explicitly, in the selectable. The logic that orders by label now ensures that the labeled expression is related to the one that resolves to that name before ordering by the label name; additionally, the name has to resolve to an actual label explicit in the expression elsewhere, not just a column name. This logic is carefully kept separate from the order by(textual name) feature that has a slightly different purpose. Change-Id: I44fc36dab34380cc238c1e79ecbe23f1628d588a Fixes: #3882
* | update for 2017 copyrightMike Bayer2017-01-041-1/+1
|/ | | | Change-Id: I4e8c2aa8fe817bb2af8707410fa0201f938781de
* Propagate execution_options at compile stageMike Bayer2016-10-051-0/+15
| | | | | | | | Compiler can now set up execution options and additionally will propagate autocommit from embedded CTEs. Change-Id: I19db7b8fe4d84549ea95342e8d2040189fed1bbe Fixes: #3805
* Add "eager_parenthesis" late-compilation rule, use w/ PG JSON/HSTOREMike Bayer2016-10-011-4/+24
| | | | | | | | | | | | Added compiler-level flags used by Postgresql to place additional parenthesis than would normally be generated by precedence rules around operations involving JSON, HSTORE indexing operators as well as within their operands since it has been observed that Postgresql's precedence rules for at least the HSTORE indexing operator is not consistent between 9.4 and 9.5. Fixes: #3806 Change-Id: I5899677b330595264543b055abd54f3c76bfabf2
* Escape literal string values passed to server_defaultMike Bayer2016-09-301-1/+2
| | | | | | | | | | | A string sent as a column default via the :paramref:`.Column.server_default` parameter is now escaped for quotes. This change is backwards compatible with code that may have been working around this previously. Change-Id: I341298a76cc67bc0a53df4ab51ab9379f2294cdd Fixes: #3809
* Allow stringify compiler to render unnamed columnMike Bayer2016-09-021-3/+8
| | | | | | | | | | Stringify of expression with unnamed :class:`.Column` objects, as occurs in lots of situations including ORM error reporting, will now render the name in string context as "<name unknown>" rather than raising a compile error. Change-Id: I76f637c5eb4cfdb1b526964cb001565b97e296da Fixes: #3789
* Check for supports_execution at ClauseElement baseMike Bayer2016-08-311-1/+4
| | | | | | | | | | Raise a more descriptive exception / message when ClauseElement or non-SQLAlchemy objects that are not "executable" are erroneously passed to ``.execute()``; a new exception ObjectNotExecutableError is raised consistently in all cases. Change-Id: I2dd393121e2c7e5b6b9e40286a2f25670876e8e4 Fixes: #3786
* Work w/ prefetch even for selects, if presentMike Bayer2016-07-121-0/+6
| | | | | | | | | | | | | | | | | | | Fixed bug in new CTE feature for update/insert/delete stated as a CTE inside of an enclosing statement (typically SELECT) whereby oninsert and onupdate values weren't called upon for the embedded statement. This is accomplished by consulting prefetch for all statements. The collection is also broken into separate insert/update collections so that we don't need to consult toplevel self.isinsert to determine if the prefetch is for an insert or an update. What we don't yet test for are CTE combinations that have both insert/update in one statement, though these should now work in theory provided the underlying database supports such a statement. Change-Id: I3b6a860e22c86743c91c56a7ec751ff706f66f64 Fixes: #3745
* Don't reorder PrimaryKeyConstraint columns if explicitMike Bayer2016-06-201-1/+3
| | | | | | | | | | | Dialed back the "order the primary key columns per auto-increment" described in :ref:`change_mysql_3216` a bit, so that if the :class:`.PrimaryKeyConstraint` is explicitly defined, the order of columns is maintained exactly, allowing control of this behavior when necessary. Change-Id: I9e7902c57a96c15968a6abf53e319acf15680da0 Fixes: #3726
* Add DDLCompiler.create_table_suffix()Mark Sandan2016-06-161-1/+10
| | | | | | | Allows custom dialects to add keywords after the CREATE TABLE section. Change-Id: I6fa66dfcf00ef95122f491a9115410df2746cf88
* Add TABLESAMPLE clause support.saarni2016-06-151-0/+11
| | | | | | | | | | The TABLESAMPLE clause allows randomly selecting an approximate percentage of rows from a table. At least DB2, Microsoft SQL Server and recent Postgresql support this standard clause. Fixes: #3718 Change-Id: I3fb8b9223e12a57100df30876b461884c58d72fa Pull-request: https://github.com/zzzeek/sqlalchemy/pull/277
* Add ON CONFLICT support for PostgresqlRobin Thomas2016-06-141-0/+6
| | | | | | | Fixes: #3529 Co-authored-by: Mike Bayer <mike_mp@zzzcomputing.com> Change-Id: Ie3bf6ad70d9be9f0e44938830e922db03573991a Pull-request: https://github.com/zzzeek/sqlalchemy/pull/258
* Support range specification in window functionPhillip Cloud2016-06-071-2/+27
| | | | | | | Fixes: #3049 Co-authored-by: Mike Bayer <mike_mp@zzzcomputing.com> Change-Id: Ie572095c3e25f70a1e72e1af6858e5edd89fd25e Pull-request: https://github.com/zzzeek/sqlalchemy/pull/264