Commit Graph

10740 Commits

Author SHA1 Message Date
mike bayer 6201b4d886 Merge "ORM bulk insert via execute" into main 2022-09-26 01:17:44 +00:00
mike bayer f81fdd9a90 Merge "implement batched INSERT..VALUES () () for executemany" into main 2022-09-26 01:17:27 +00:00
mike bayer 6072bebd19 Merge "warn for local-only column in remote side" into main 2022-09-25 23:15:46 +00:00
Mike Bayer 29838ef584 warn for local-only column in remote side
A warning is emitted in ORM configurations when an explicit
:func:`_orm.remote` annotation is applied to columns that are local to the
immediate mapped class, when the referenced class does not include any of
the same table columns. Ideally this would raise an error at some point as
it's not correct from a mapping point of view.

Fixes: #7094
Fixes: #8575
Change-Id: Ia31be24aebe143161e19dc311b52c08fd5014d33
2022-09-25 16:17:33 -04:00
Federico Caselli 65aa9b8a98 Use aggregate order by instead of order by in subquery
Fixes: #8561
Change-Id: I2d9f6bd895061bf8fbc66723930716670791d896
2022-09-25 18:20:22 +02:00
Mike Bayer a8029f5a7e ORM bulk insert via execute
* ORM Insert now includes "bulk" mode that will run
  essentially the same process as session.bulk_insert_mappings;
  interprets the given list of values as ORM attributes for
  key names
* ORM UPDATE has a similar feature, without RETURNING support,
  for session.bulk_update_mappings
* Added support for upserts to do RETURNING ORM objects as well
* ORM UPDATE/DELETE with list of parameters + WHERE criteria
  is a not implemented; use connection
* ORM UPDATE/DELETE defaults to "auto" synchronize_session;
  use fetch if RETURNING is present, evaluate if not, as
  "fetch" is much more efficient (no expired object SELECT problem)
  and less error prone if RETURNING is available
  UPDATE: howver this is inefficient!   please continue to
  use evaluate for simple cases, auto can move to fetch
  if criteria not evaluable
* "Evaluate" criteria will now not preemptively
  unexpire and SELECT attributes that were individually
  expired. Instead, if evaluation of the criteria indicates that
  the necessary attrs were expired, we expire the object
  completely (delete) or expire the SET attrs unconditionally
  (update). This keeps the object in the same unloaded state
  where it will refresh those attrs on the next pass, for
  this generally unusual case.  (originally #5664)
* Core change! update/delete rowcount comes from len(rows)
  if RETURNING was used.  SQLite at least otherwise did not
  support this.  adjusted test_rowcount accordingly
* ORM DELETE with a list of parameters at all is also a not
  implemented as this would imply "bulk", and there is no
  bulk_delete_mappings (could be, but we dont have that)
* ORM insert().values() with single or multi-values translates
  key names based on ORM attribute names
* ORM returning() implemented for insert, update, delete;
  explcit returning clauses now interpret rows in an ORM
  context, with support for qualifying loader options as well
* session.bulk_insert_mappings() assigns polymorphic identity
  if not set.
* explicit RETURNING + synchronize_session='fetch' is now
  supported with UPDATE and DELETE.
* expanded return_defaults() to work with DELETE also.
* added support for composite attributes to be present
  in the dictionaries used by bulk_insert_mappings and
  bulk_update_mappings, which is also the new ORM bulk
  insert/update feature, that will expand the composite
  values into their individual mapped attributes the way they'd
  be on a mapped instance.
* bulk UPDATE supports "synchronize_session=evaluate", is the
  default.  this does not apply to session.bulk_update_mappings,
  just the new version
* both bulk UPDATE and bulk INSERT, the latter with or without
  RETURNING, support *heterogenous* parameter sets.
  session.bulk_insert/update_mappings did this, so this feature
  is maintained.  now cursor result can be both horizontally
  and vertically spliced :)

This is now a long story with a lot of options, which in
itself is a problem to be able to document all of this
in some way that makes sense.  raising exceptions for
use cases we haven't supported is pretty important here
too, the tradition of letting unsupported things just not work
is likely not a good idea at this point, though there
are still many cases that aren't easily avoidable

Fixes: #8360
Fixes: #7864
Fixes: #7865
Change-Id: Idf28379f8705e403a3c6a937f6a798a042ef2540
2022-09-24 11:18:01 -04:00
Mike Bayer 2bcc97da42 implement batched INSERT..VALUES () () for executemany
the feature is enabled for all built in backends
when RETURNING is used,
except for Oracle that doesn't need it,  and on
psycopg2 and mssql+pyodbc it is used for all INSERT statements,
not just those that use RETURNING.

third party dialects would need to opt in to the new feature
by setting use_insertmanyvalues to True.

Also adds dialect-level guards against using returning
with executemany where we dont have an implementation to
suit it.   execute single w/ returning still defers to the
server without us checking.

Fixes: #6047
Fixes: #7907
Change-Id: I3936d3c00003f02e322f2e43fb949d0e6e568304
2022-09-24 11:15:32 -04:00
mike bayer beb4376c5e Merge "Tighten password security by removing URL.__str__" into main 2022-09-24 01:02:16 +00:00
Yassen Damyanov 3333c6623f Tighten password security by removing URL.__str__
For improved security, the :class:`_url.URL` object will now use password
obfuscation by default when ``str(url)`` is called. To stringify a URL with
cleartext password, the :meth:`_url.URL.render_as_string` may be used,
passing the :paramref:`_url.URL.render_as_string.hide_password` parameter
as ``False``. Thanks to our contributors for this pull request.

Fixes: #8567
Closes: #8563
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/8563
Pull-request-sha: d1f1127f75

Change-Id: If756c8073ff99ac83876d9833c8fe1d7c76211f9
2022-09-23 17:27:30 -04:00
Mike Bayer 57b400f079 remove should_nest behavior for contains_eager()
Fixed regression for 1.4 in :func:`_orm.contains_eager` where the "wrap in
subquery" logic of :func:`_orm.joinedload` would be inadvertently triggered
for use of the :func:`_orm.contains_eager` function with similar statements
(e.g. those that use ``distinct()``, ``limit()`` or ``offset()``). This is
not appropriate for :func:`_orm.contains_eager` which has always had the
contract that the user-defined SQL statement is unmodified with the
exception of adding the appropriate columns.

Also includes an adjustment to the assertion in Label._make_proxy()
which was there to prevent a fixed label name from being anonymized;
if the label is already anonymous, the change should proceed.
This logic was being hit before the contains_eager behavior was
adjusted. With the adjustment, this code is not used.

Fixes: #8569
Change-Id: I161e65041c0162fd2b83cbef40f57a50fcfaf0fd
2022-09-23 17:17:02 -04:00
Mike Bayer 9ae645d5d1 auto-cast PG range types
Range type handling has been enhanced so that it automatically
renders type casts, so that in-place round trips for statements that don't
provide the database with any context don't require the :func:`_sql.cast`
construct to be explicit for the database to know the desired type.

Change-Id: Id630b726f8a23059dd2f4cbc410bf5229d89cbfb
References: #8540
2022-09-20 12:34:46 -04:00
mike bayer 214d1ad7c3 Merge "break out text() from TextualSelect for col matching" into main 2022-09-20 02:34:31 +00:00
Mike Bayer c9af2ebf5e break out text() from TextualSelect for col matching
Fixed issue where mixing "*" with additional explicitly-named column
expressions within the columns clause of a :func:`_sql.select` construct
would cause result-column targeting to sometimes consider the label name or
other non-repeated names to be an ambiguous target.

Fixes: #8536
Change-Id: I3c845eaf571033e54c9208762344f67f4351ac3a
2022-09-19 18:39:18 -04:00
Mike Bayer 9020e104eb add raiseload to load_only()
currently this can't be finessed in another way,
at least not easily.   The deferred() that it sets up
doesn't seem to be cancellable.  in any case, this is more consistent
API with that of defer().

Change-Id: I30fca1cc371c9102d013cda8e23a522eea1033f8
2022-09-19 18:27:28 -04:00
mike bayer e7700b4a88 Merge "change verbiage stating exact compliance with RFC-1738" into main 2022-09-17 17:40:11 +00:00
Mike Bayer c88bb2167b change verbiage stating exact compliance with RFC-1738
As long as we aren't using urlparse() to parse URLs,
we are not RFC-1738 compliant.   As we accept underscores
in the scheme and not dashes or dots, we are not
RFC-1738 compliant, so emulate language like
that of PostgreSQL [1] that we "generally follow" this
scheme but include some exceptions.

[1] https://www.postgresql.org/docs/current/libpq-connect.html#id-1.7.3.8.3.6
Fixes: #8519

Change-Id: I2d7e55d9df17aed122cebb2c4c315f56c06a3da5
2022-09-17 10:41:48 -04:00
Federico Caselli 02fe382d6b Improve array_agg and Array processing
The :class:`_functions.array_agg` will now set the array dimensions to 1.
Improved :class:`_types.ARRAY` processing to accept ``None`` values as
value of a multi-array.

Fixes: #7083
Change-Id: Iafec4f77fde9719ccc7c8535bf6235dbfbc62102
2022-09-17 13:12:35 +02:00
mike bayer f582618afe Merge "Ignore max_overflow when pool_size is 0" into main 2022-09-16 22:28:45 +00:00
mike bayer 27e0225656 Merge "Support GROUP BY ROLLUP on MySql/MariaDB" into main 2022-09-16 22:27:14 +00:00
mike bayer 8f8398a485 Merge "catch exception for system_views also" into main 2022-09-16 22:26:25 +00:00
Federico Caselli 20917c5918 Ignore max_overflow when pool_size is 0
The :class:`_pool.QueuePool` now ignores ``max_overflow`` when
``pool_size=0``, properly making the pool unlimited in all cases.

Fixes: #8523
Change-Id: Ifc32eb47a281c4b3acf357352f07b9b8a73d1b6f
2022-09-15 22:42:08 +02:00
Federico Caselli 04ea009a83 Merge "Improved usage of `asyncio.shield()`" into main 2022-09-15 20:18:58 +00:00
Federico Caselli d9ecad5af6 Merge "Use `; instead of select 1` to ping PostgreSQL" into main 2022-09-15 20:18:39 +00:00
Federico Caselli 2e302b2d8f Use `; instead of select 1` to ping PostgreSQL
Fixes: #8491
Change-Id: I941d2a3cf92e5609e2045a53cec94522340951db
2022-09-15 09:31:44 -04:00
Federico Caselli 939f77c94b Improved usage of `asyncio.shield()`
Fixes: #8516
Change-Id: Ifd8f5e5f42d9fbcd5b8d00bddc81ff6be690a75e
2022-09-15 09:02:33 -04:00
Mike Bayer 0ee7d693b8 catch exception for system_views also
Fixed yet another regression in SQL Server isolation level fetch (see
🎫`8231`, 🎫`8475`), this time with "Microsoft Dynamics CRM
Database via Azure Active Directory", which apparently lacks the
``system_views`` view entirely. Error catching has been extended that under
no circumstances will this method ever fail, provided database connectivity
is present.

Fixes: #8525
Change-Id: I76a429e3329926069a0367d2e77ca1124b9a059d
2022-09-15 08:42:34 -04:00
Mike Bayer 14b634d706 Add type awareness to evaluator
Fixed regression where using ORM update() with synchronize_session='fetch'
would fail due to the use of evaluators that are now used to determine the
in-Python value for expressions in the the SET clause when refreshing
objects; if the evaluators make use of math operators against non-numeric
values such as PostgreSQL JSONB, the non-evaluable condition would fail to
be detected correctly. The evaluator now limits the use of math mutation
operators to numeric types only, with the exception of "+" that continues
to work for strings as well. SQLAlchemy 2.0 may alter this further by
fetching the SET values completely rather than using evaluation.

Fixes: #8507
Change-Id: Icf7120ccbf4266499df6bb3e05159c9f50971d69
2022-09-13 11:18:19 -04:00
Federico Caselli efbe38d8e8 Support GROUP BY ROLLUP on MySql/MariaDB
The ``ROLLUP`` function will now correctly render ``WITH ROLLUP`` on
MySql and MariaDB, allowing the use of group by rollup with these
backend.

Fixes: #8503
Change-Id: I9289af3a39ca667a2f0f84f73346ebd4b091fedd
2022-09-09 21:29:53 +02:00
mike bayer 479dbc99e7 Merge "additional de-stringify pass for unions" into main 2022-09-08 19:00:08 +00:00
mike bayer b0e5667a79 Merge "implement icontains, istartswith, iendswith operators" into main 2022-09-08 18:19:46 +00:00
Mike Bayer fcd298e1af additional de-stringify pass for unions
the change in c3cfee5b00 took out
a pass for de-stringify that broke some un-tested cases
for Optional with future annotations mode.   Adding tests
for this revealed that this was a subset of
a more general case where Union is presented
with ForwardRefs inside of it matching up within the type
map, which wasn't working before either, fixed that as well with
an additional de-stringify for elements within the Union.

Fixes: #8478
Change-Id: I8804cf6c67f14d10804584e1cddd2cfaa2376654
2022-09-08 14:15:50 -04:00
Matias Martinez Rebori 93aaf16727 implement icontains, istartswith, iendswith operators
Added long-requested case-insensitive string operators
:meth:`_sql.ColumnOperators.icontains`,
:meth:`_sql.ColumnOperators.istartswith`,
:meth:`_sql.ColumnOperators.iendswith`, which produce case-insensitive
LIKE compositions (using ILIKE on PostgreSQL, and the LOWER() function on
all other backends) to complement the existing LIKE composition operators
:meth:`_sql.ColumnOperators.contains`,
:meth:`_sql.ColumnOperators.startswith`, etc. Huge thanks to Matias
Martinez Rebori for their meticulous and complete efforts in implementing
these new methods.

Fixes: #3482
Closes: #8496
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/8496
Pull-request-sha: 7287e2c436

Change-Id: I9fcdd603716218067547cc92a2b07bd02a2c366b
2022-09-08 12:15:23 -04:00
Mike Bayer 885c61cf22 enable UPDATE..FROM for SQLite
The SQLite dialect now supports UPDATE..FROM syntax, for UPDATE statements
that may refer to additional tables within the WHERE criteria of the
statement without the need to use subqueries. This syntax is invoked
automatically when using the :class:`_dml.Update` construct when more than
one table or other entity or selectable is used.

Fixes: #7185
Change-Id: I27e94ace9ff761cc45e652fa1abff8cd1f42fec5
2022-09-07 19:10:53 -04:00
Federico Caselli 741534d840 Improve compiled extension detection
Ensure that all cython extension are imported by the compied detection logic.
This is required since cython extensions moduels are marked as optional
in the install, so it's possible that only some of them are compiled.
The extensions are enabled only if all of them are correctly compiled

Change-Id: I355cbac06f5c7a47d35661f42ebab3b0156c1965
2022-09-05 22:32:14 +02:00
mike bayer 5a606a10f7 Merge "Fix Azure Synapse connection error" into main 2022-09-02 23:21:14 +00:00
Gord Thompson 6459770884 Fix Azure Synapse connection error
Fixed regression caused by the fix for 🎫`8231` released in 1.4.40
where connection would fail if the user does not have permission to query
the dm_exec_sessions or dm_pdw_nodes_exec_sessions system view when trying
to determine the current transaction isolation level.

Fixes: #8475
Change-Id: Ie2bcda92f2ef2d12360ddda47eb6e896313c71f2
2022-09-02 09:25:45 -04:00
Peter Schutt c3cfee5b00 Detection of PEP 604 union syntax.
### Description

Fixes #8478

Handle `UnionType` as arguments to `Mapped`, e.g., `Mapped[str | None]`:

- adds `utils.typing.is_optional_union()` used to detect if a column should be nullable.
- adds `"UnionType"` to `utils.typing.is_optional()` names.
- uses `get_origin()` in `utils.typing.is_origin_of()` as `UnionType` has no `__origin__` attribute.
- tests with runtime type and postponed annotations and guard the tests running with `compat.py310`.

### Checklist
<!-- go over following points. check them with an `x` if they do apply, (they turn into clickable checkboxes once the PR is submitted, so no need to do everything at once)

-->

This pull request is:

- [ ] A documentation / typographical error fix
	- Good to go, no issue or tests are needed
- [x] A short code fix
	- please include the issue number, and create an issue if none exists, which
	  must include a complete example of the issue.  one line code fixes without an
	  issue and demonstration will not be accepted.
	- Please include: `Fixes: #<issue number>` in the commit message
	- please include tests.   one line code fixes without tests will not be accepted.
- [ ] A new feature implementation
	- please include the issue number, and create an issue if none exists, which must
	  include a complete example of how the feature would look.
	- Please include: `Fixes: #<issue number>` in the commit message
	- please include tests.

**Have a nice day!**

Closes: #8479
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/8479
Pull-request-sha: 1241765482

Change-Id: Ib3248043dd4a97324ac592c048385006536b2d49
2022-09-01 19:11:40 -04:00
Mike Bayer d3e0b8e750 run update_subclass anytime we add new clslevel dispatch
Fixed event listening issue where event listeners added to a superclass
would be lost if a subclass were created which then had its own listeners
associated. The practical example is that of the :class:`.sessionmaker`
class created after events have been associated with the
:class:`_orm.Session` class.

Fixes: #8467
Change-Id: I9bdba8769147e30110a09900d4a577e833ac3af9
2022-08-31 14:31:34 -04:00
mike bayer 18be87f911 Merge "include TableClause.schema in cache key" into main 2022-08-30 17:11:03 +00:00
Mike Bayer 613642d963 include TableClause.schema in cache key
Fixed issue where use of the :func:`_sql.table` construct, passing a string
for the :paramref:`_sql.table.schema` parameter, would fail to take the
"schema" string into account when producing a cache key, thus leading to
caching collisions if multiple, same-named :func:`_sql.table` constructs
with different schemas were used.

Fixes: #8441
Change-Id: Ic4b55b3e8ec53b4c88ba112691bdf60ea1d4c448
2022-08-30 10:47:24 -04:00
Mike Bayer e15cf451af implement event for merge/load=False for mutable state setup
Fixed issue in :mod:`sqlalchemy.ext.mutable` extension where collection
links to the parent object would be lost if the object were merged with
:meth:`.Session.merge` while also passing :paramref:`.Session.merge.load`
as False.

The event added here is currently private for expediency, but
is acceptable to become a public event at some point.

Fixes: #8446
Change-Id: I9e5b9f1f5a0c5a9781f51635d5e57b1134c9e866
2022-08-30 10:35:25 -04:00
Mike Bayer 2f146b172a apply consistent ORM mutable notes for all mutable SQL types
in
https://github.com/sqlalchemy/sqlalchemy/discussions/8447
I was surprised that we didnt have any notes about using Mutable
for ARRAY classes, since we have them for HSTORE and JSON.

Add a consistent topic box for these so we have something to
point towards.

Change-Id: Idfa1b2cbee67024545f4fa299e4c875075ec7d3f
2022-08-30 09:50:03 -04:00
Federico Caselli 6da8d32fb1 Merge "Use cibuildwheel to create wheels" into main 2022-08-30 13:13:59 +00:00
Mike Bayer b7f1fb5879 refine ruleset to determine when poly adaption should be used
Fixed regression appearing in the 1.4 series where a joined-inheritance
query placed as a subquery within an enclosing query for that same entity
would fail to render the JOIN correctly for the inner query. The issue
manifested in two different ways prior and subsequent to version 1.4.18
(related issue #6595), in one case rendering JOIN twice, in the other
losing the JOIN entirely. To resolve, the conditions under which
"polymorphic loading" are applied have been scaled back to not be invoked
for simple joined inheritance queries.

Fixes: #8456
Change-Id: Ie4332fadb1dfc670cd31d098a6586a9f6976bcf7
2022-08-29 23:42:18 -04:00
Trevor Gross 7b67920fcb Use cibuildwheel to create wheels
Using cibuildwheel the following wheels are created:
- windows x64 and x84
- macos x64 and arm
- linux x64 and arm on manylinux and mosulinux (for alpine)
- create a pure python wheel (for pypy and other archs)

Fixes: #6702
Fixes: #7607
Closes: #7992
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/7992
Pull-request-sha: 61d5e24e5b

Change-Id: If0c0b353766e0b61d421789d619eb2b940c08ad0
2022-08-29 17:21:51 +00:00
mike bayer 1ea3c783b6 Merge "integrate connection.terminate() for supporting dialects" into main 2022-08-24 18:08:17 +00:00
Mike Bayer 776abf43d7 integrate connection.terminate() for supporting dialects
Integrated support for asyncpg's ``terminate()`` method call for cases
where the connection pool is recycling a possibly timed-out connection,
where a connection is being garbage collected that wasn't gracefully
closed, as well as when the connection has been invalidated. This allows
asyncpg to abandon the connection without waiting for a response that may
incur long timeouts.

Fixes: #8419
Change-Id: Ia575af779d5733b483a72dff3690b8bbbad2bb05
2022-08-24 13:02:23 -04:00
mike bayer e475f8f4b1 Merge "include mssql_clustered dialect_options when reflecting - issue #8288" into main 2022-08-24 17:00:25 +00:00
John Lennox 08c96ff74c include mssql_clustered dialect_options when reflecting - issue #8288
Implemented reflection of the "clustered index" flag ``mssql_clustered``
for the SQL Server dialect. Pull request courtesy John Lennox.

Fixes: #8288
Closes: #8289
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/8289
Pull-request-sha: 1bb57352e3

Change-Id: Ife367066328f9e47ad823e4098647964a18e21e8
2022-08-23 17:52:46 -04:00
Mike Bayer 25fa4c0250 more function adjustments
the inherited-members feature works very poorly
and inconsistently in sphinx.  just dont use it here as it
refuses to exclude ColumnOperators methods

Change-Id: Ic50865c9901e7225a99ff7f33454da15ff91b12f
2022-08-22 13:55:08 -04:00