diff options
| author | Federico Caselli <cfederico87@gmail.com> | 2020-09-02 23:46:06 +0200 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2020-09-08 17:13:48 -0400 |
| commit | e8600608669d90c4a6385b312d271aed63eb5854 (patch) | |
| tree | ef984a01c536b2c81d2283b3ca5d9f4395f41dd0 /lib/sqlalchemy/sql | |
| parent | 0d56a62f721ee6c91d8a8b6a407b959c9215b3b6 (diff) | |
| download | sqlalchemy-e8600608669d90c4a6385b312d271aed63eb5854.tar.gz | |
Update select usage to use the new 1.4 format
This change includes mainly that the bracketed use within
select() is moved to positional, and keyword arguments are
removed from calls to the select() function. it does not
yet fully address other issues such as keyword arguments passed
to the table.select().
Additionally, allows False / None to both be considered
as "disable" for all of select.correlate(), select.correlate_except(),
query.correlate(), which establishes consistency with
passing of ``False`` for the legact select(correlate=False)
argument.
Change-Id: Ie6c6e6abfbd3d75d4c8de504c0cf0159e6999108
Diffstat (limited to 'lib/sqlalchemy/sql')
| -rw-r--r-- | lib/sqlalchemy/sql/base.py | 2 | ||||
| -rw-r--r-- | lib/sqlalchemy/sql/dml.py | 8 | ||||
| -rw-r--r-- | lib/sqlalchemy/sql/elements.py | 102 | ||||
| -rw-r--r-- | lib/sqlalchemy/sql/functions.py | 31 | ||||
| -rw-r--r-- | lib/sqlalchemy/sql/operators.py | 16 | ||||
| -rw-r--r-- | lib/sqlalchemy/sql/selectable.py | 81 | ||||
| -rw-r--r-- | lib/sqlalchemy/sql/sqltypes.py | 28 | ||||
| -rw-r--r-- | lib/sqlalchemy/sql/visitors.py | 2 |
8 files changed, 131 insertions, 139 deletions
diff --git a/lib/sqlalchemy/sql/base.py b/lib/sqlalchemy/sql/base.py index dc2804691..67ee8c907 100644 --- a/lib/sqlalchemy/sql/base.py +++ b/lib/sqlalchemy/sql/base.py @@ -825,7 +825,7 @@ class Executable(Generative): The :meth:`execution_options` method is generative. A new instance of this statement is returned that contains the options:: - statement = select([table.c.x, table.c.y]) + statement = select(table.c.x, table.c.y) statement = statement.execution_options(autocommit=True) Note that only a subset of possible execution options can be applied diff --git a/lib/sqlalchemy/sql/dml.py b/lib/sqlalchemy/sql/dml.py index b7151ac7b..cd4bc17af 100644 --- a/lib/sqlalchemy/sql/dml.py +++ b/lib/sqlalchemy/sql/dml.py @@ -914,7 +914,7 @@ class Insert(ValuesBase): e.g.:: - sel = select([table1.c.a, table1.c.b]).where(table1.c.c > 5) + sel = select(table1.c.a, table1.c.b).where(table1.c.c > 5) ins = table2.insert().from_select(['a', 'b'], sel) :param names: a sequence of string column names or @@ -1116,7 +1116,7 @@ class Update(DMLWhereBase, ValuesBase): subquery:: users.update().values(name='ed').where( - users.c.name==select([addresses.c.email_address]).\ + users.c.name==select(addresses.c.email_address).\ where(addresses.c.user_id==users.c.id).\ scalar_subquery() ) @@ -1183,7 +1183,7 @@ class Update(DMLWhereBase, ValuesBase): the subquery to the outer table being updated:: users.update().values( - name=select([addresses.c.email_address]).\ + name=select(addresses.c.email_address).\ where(addresses.c.user_id==users.c.id).\ scalar_subquery() ) @@ -1334,7 +1334,7 @@ class Delete(DMLWhereBase, UpdateBase): subquery:: users.delete().where( - users.c.name==select([addresses.c.email_address]).\ + users.c.name==select(addresses.c.email_address).\ where(addresses.c.user_id==users.c.id).\ scalar_subquery() ) diff --git a/lib/sqlalchemy/sql/elements.py b/lib/sqlalchemy/sql/elements.py index a17612034..dc3e5f476 100644 --- a/lib/sqlalchemy/sql/elements.py +++ b/lib/sqlalchemy/sql/elements.py @@ -81,7 +81,7 @@ def between(expr, lower_bound, upper_bound, symmetric=False): E.g.:: from sqlalchemy import between - stmt = select([users_table]).where(between(users_table.c.id, 5, 7)) + stmt = select(users_table).where(between(users_table.c.id, 5, 7)) Would produce SQL resembling:: @@ -91,7 +91,7 @@ def between(expr, lower_bound, upper_bound, symmetric=False): :meth:`_expression.ColumnElement.between` method available on all SQL expressions, as in:: - stmt = select([users_table]).where(users_table.c.id.between(5, 7)) + stmt = select(users_table).where(users_table.c.id.between(5, 7)) All arguments passed to :func:`.between`, including the left side column expression, are coerced from Python scalar values if a @@ -470,7 +470,7 @@ class ClauseElement( t = table('t', column('x')) - s = select([t]).where(t.c.x == 5) + s = select(t).where(t.c.x == 5) print(s.compile(compile_kwargs={"literal_binds": True})) @@ -1037,7 +1037,7 @@ class BindParameter(roles.InElementRole, ColumnElement): from sqlalchemy import bindparam - stmt = select([users_table]).\ + stmt = select(users_table).\ where(users_table.c.name == bindparam('username')) Detailed discussion of how :class:`.BindParameter` is used is @@ -1107,7 +1107,7 @@ class BindParameter(roles.InElementRole, ColumnElement): from sqlalchemy import bindparam - stmt = select([users_table]).\ + stmt = select(users_table).\ where(users_table.c.name == bindparam('username')) The above statement, when rendered, will produce SQL similar to:: @@ -1161,7 +1161,7 @@ class BindParameter(roles.InElementRole, ColumnElement): along where it is later used within statement execution. If we invoke a statement like the following:: - stmt = select([users_table]).where(users_table.c.name == 'Wendy') + stmt = select(users_table).where(users_table.c.name == 'Wendy') result = connection.execute(stmt) We would see SQL logging output as:: @@ -1625,7 +1625,7 @@ class TextClause( a literal string SQL fragment is specified as part of a larger query, such as for the WHERE clause of a SELECT statement:: - s = select([users.c.id, users.c.name]).where(text("id=:user_id")) + s = select(users.c.id, users.c.name).where(text("id=:user_id")) result = connection.execute(s, user_id=12) :func:`_expression.text` is also used for the construction @@ -1821,7 +1821,7 @@ class TextClause( stmt = text("SELECT id, name FROM some_table") stmt = stmt.columns(column('id'), column('name')).subquery('st') - stmt = select([mytable]).\ + stmt = select(mytable).\ select_from( mytable.join(stmt, mytable.c.name == stmt.c.name) ).where(stmt.c.id > 5) @@ -1901,7 +1901,7 @@ class TextClause( stmt = stmt.columns(id=Integer, name=String).cte('st') - stmt = select([sometable]).where(sometable.c.id == stmt.c.id) + stmt = select(sometable).where(sometable.c.id == stmt.c.id) :param \*cols: A series of :class:`_expression.ColumnElement` objects, typically @@ -2000,23 +2000,23 @@ class False_(SingletonConstant, roles.ConstExprRole, ColumnElement): E.g.:: >>> from sqlalchemy import false - >>> print(select([t.c.x]).where(false())) + >>> print(select(t.c.x).where(false())) SELECT x FROM t WHERE false A backend which does not support true/false constants will render as an expression against 1 or 0:: - >>> print(select([t.c.x]).where(false())) + >>> print(select(t.c.x).where(false())) SELECT x FROM t WHERE 0 = 1 The :func:`.true` and :func:`.false` constants also feature "short circuit" operation within an :func:`.and_` or :func:`.or_` conjunction:: - >>> print(select([t.c.x]).where(or_(t.c.x > 5, true()))) + >>> print(select(t.c.x).where(or_(t.c.x > 5, true()))) SELECT x FROM t WHERE true - >>> print(select([t.c.x]).where(and_(t.c.x > 5, false()))) + >>> print(select(t.c.x).where(and_(t.c.x > 5, false()))) SELECT x FROM t WHERE false .. versionchanged:: 0.9 :func:`.true` and :func:`.false` feature @@ -2068,23 +2068,23 @@ class True_(SingletonConstant, roles.ConstExprRole, ColumnElement): E.g.:: >>> from sqlalchemy import true - >>> print(select([t.c.x]).where(true())) + >>> print(select(t.c.x).where(true())) SELECT x FROM t WHERE true A backend which does not support true/false constants will render as an expression against 1 or 0:: - >>> print(select([t.c.x]).where(true())) + >>> print(select(t.c.x).where(true())) SELECT x FROM t WHERE 1 = 1 The :func:`.true` and :func:`.false` constants also feature "short circuit" operation within an :func:`.and_` or :func:`.or_` conjunction:: - >>> print(select([t.c.x]).where(or_(t.c.x > 5, true()))) + >>> print(select(t.c.x).where(or_(t.c.x > 5, true()))) SELECT x FROM t WHERE true - >>> print(select([t.c.x]).where(and_(t.c.x > 5, false()))) + >>> print(select(t.c.x).where(and_(t.c.x > 5, false()))) SELECT x FROM t WHERE false .. versionchanged:: 0.9 :func:`.true` and :func:`.false` feature @@ -2327,7 +2327,7 @@ class BooleanClauseList(ClauseList, ColumnElement): from sqlalchemy import and_ - stmt = select([users_table]).where( + stmt = select(users_table).where( and_( users_table.c.name == 'wendy', users_table.c.enrolled == True @@ -2339,7 +2339,7 @@ class BooleanClauseList(ClauseList, ColumnElement): need to be parenthesized in order to function with Python operator precedence behavior):: - stmt = select([users_table]).where( + stmt = select(users_table).where( (users_table.c.name == 'wendy') & (users_table.c.enrolled == True) ) @@ -2350,7 +2350,7 @@ class BooleanClauseList(ClauseList, ColumnElement): times against a statement, which will have the effect of each clause being combined using :func:`.and_`:: - stmt = select([users_table]).\ + stmt = select(users_table).\ where(users_table.c.name == 'wendy').\ where(users_table.c.enrolled == True) @@ -2390,7 +2390,7 @@ class BooleanClauseList(ClauseList, ColumnElement): from sqlalchemy import or_ - stmt = select([users_table]).where( + stmt = select(users_table).where( or_( users_table.c.name == 'wendy', users_table.c.name == 'jack' @@ -2402,7 +2402,7 @@ class BooleanClauseList(ClauseList, ColumnElement): need to be parenthesized in order to function with Python operator precedence behavior):: - stmt = select([users_table]).where( + stmt = select(users_table).where( (users_table.c.name == 'wendy') | (users_table.c.name == 'jack') ) @@ -2536,7 +2536,7 @@ class Case(ColumnElement): from sqlalchemy import case - stmt = select([users_table]).\ + stmt = select(users_table).\ where( case( (users_table.c.name == 'wendy', 'W'), @@ -2576,7 +2576,7 @@ class Case(ColumnElement): from sqlalchemy import case - stmt = select([users_table]).\ + stmt = select(users_table).\ where( case( (users_table.c.name == 'wendy', 'W'), @@ -2603,7 +2603,7 @@ class Case(ColumnElement): compared against keyed to result expressions. The statement below is equivalent to the preceding statement:: - stmt = select([users_table]).\ + stmt = select(users_table).\ where( case( {"wendy": "W", "jack": "J"}, @@ -2800,9 +2800,7 @@ class Cast(WrapsColumnExpression, ColumnElement): from sqlalchemy import cast, Numeric - stmt = select([ - cast(product_table.c.unit_price, Numeric(10, 4)) - ]) + stmt = select(cast(product_table.c.unit_price, Numeric(10, 4))) Details on :class:`.Cast` usage is at :func:`.cast`. @@ -2834,9 +2832,7 @@ class Cast(WrapsColumnExpression, ColumnElement): from sqlalchemy import cast, Numeric - stmt = select([ - cast(product_table.c.unit_price, Numeric(10, 4)) - ]) + stmt = select(cast(product_table.c.unit_price, Numeric(10, 4))) The above statement will produce SQL resembling:: @@ -2930,7 +2926,7 @@ class TypeCoerce(WrapsColumnExpression, ColumnElement): from sqlalchemy import type_coerce - stmt = select([type_coerce(log_table.date_string, StringDateTime())]) + stmt = select(type_coerce(log_table.date_string, StringDateTime())) The above construct will produce a :class:`.TypeCoerce` object, which does not modify the rendering in any way on the SQL side, with the @@ -2950,9 +2946,9 @@ class TypeCoerce(WrapsColumnExpression, ColumnElement): In order to provide a named label for the expression, use :meth:`_expression.ColumnElement.label`:: - stmt = select([ + stmt = select( type_coerce(log_table.date_string, StringDateTime()).label('date') - ]) + ) A type that features bound-value handling will also have that behavior @@ -2966,7 +2962,7 @@ class TypeCoerce(WrapsColumnExpression, ColumnElement): # bound-value handling of MyStringType will be applied to the # literal value "some string" - stmt = select([type_coerce("some string", MyStringType)]) + stmt = select(type_coerce("some string", MyStringType)) When using :func:`.type_coerce` with composed expressions, note that **parenthesis are not applied**. If :func:`.type_coerce` is being @@ -3145,7 +3141,7 @@ class UnaryExpression(ColumnElement): from sqlalchemy import desc, nullsfirst - stmt = select([users_table]).order_by( + stmt = select(users_table).order_by( nullsfirst(desc(users_table.c.name))) The SQL expression from the above would resemble:: @@ -3158,7 +3154,7 @@ class UnaryExpression(ColumnElement): rather than as its standalone function version, as in:: - stmt = select([users_table]).order_by( + stmt = select(users_table).order_by( users_table.c.name.desc().nullsfirst()) .. seealso:: @@ -3189,7 +3185,7 @@ class UnaryExpression(ColumnElement): from sqlalchemy import desc, nullslast - stmt = select([users_table]).order_by( + stmt = select(users_table).order_by( nullslast(desc(users_table.c.name))) The SQL expression from the above would resemble:: @@ -3202,7 +3198,7 @@ class UnaryExpression(ColumnElement): rather than as its standalone function version, as in:: - stmt = select([users_table]).order_by( + stmt = select(users_table).order_by( users_table.c.name.desc().nullslast()) .. seealso:: @@ -3230,7 +3226,7 @@ class UnaryExpression(ColumnElement): from sqlalchemy import desc - stmt = select([users_table]).order_by(desc(users_table.c.name)) + stmt = select(users_table).order_by(desc(users_table.c.name)) will produce SQL as:: @@ -3242,7 +3238,7 @@ class UnaryExpression(ColumnElement): e.g.:: - stmt = select([users_table]).order_by(users_table.c.name.desc()) + stmt = select(users_table).order_by(users_table.c.name.desc()) :param column: A :class:`_expression.ColumnElement` (e.g. scalar SQL expression) @@ -3272,7 +3268,7 @@ class UnaryExpression(ColumnElement): e.g.:: from sqlalchemy import asc - stmt = select([users_table]).order_by(asc(users_table.c.name)) + stmt = select(users_table).order_by(asc(users_table.c.name)) will produce SQL as:: @@ -3284,7 +3280,7 @@ class UnaryExpression(ColumnElement): e.g.:: - stmt = select([users_table]).order_by(users_table.c.name.asc()) + stmt = select(users_table).order_by(users_table.c.name.asc()) :param column: A :class:`_expression.ColumnElement` (e.g. scalar SQL expression) @@ -3316,7 +3312,7 @@ class UnaryExpression(ColumnElement): as in:: from sqlalchemy import distinct, func - stmt = select([func.count(distinct(users_table.c.name))]) + stmt = select(func.count(distinct(users_table.c.name))) The above would produce an expression resembling:: @@ -3325,7 +3321,7 @@ class UnaryExpression(ColumnElement): The :func:`.distinct` function is also available as a column-level method, e.g. :meth:`_expression.ColumnElement.distinct`, as in:: - stmt = select([func.count(users_table.c.name.distinct())]) + stmt = select(func.count(users_table.c.name.distinct())) The :func:`.distinct` operator is different from the :meth:`_expression.Select.distinct` method of @@ -3403,7 +3399,7 @@ class CollectionAggregate(UnaryExpression): expr = 5 == any_(mytable.c.somearray) # mysql '5 = ANY (SELECT value FROM table)' - expr = 5 == any_(select([table.c.value])) + expr = 5 == any_(select(table.c.value)) .. versionadded:: 1.1 @@ -3434,7 +3430,7 @@ class CollectionAggregate(UnaryExpression): expr = 5 == all_(mytable.c.somearray) # mysql '5 = ALL (SELECT value FROM table)' - expr = 5 == all_(select([table.c.value])) + expr = 5 == all_(select(table.c.value)) .. versionadded:: 1.1 @@ -3933,12 +3929,12 @@ class WithinGroup(ColumnElement): the :meth:`.FunctionElement.within_group` method, e.g.:: from sqlalchemy import within_group - stmt = select([ + stmt = select( department.c.id, func.percentile_cont(0.5).within_group( department.c.salary.desc() ) - ]) + ) The above statement would produce SQL similar to ``SELECT department.id, percentile_cont(0.5) @@ -4279,7 +4275,7 @@ class ColumnClause( from sqlalchemy import column id, name = column("id"), column("name") - stmt = select([id, name]).select_from("user") + stmt = select(id, name).select_from("user") The above statement would produce SQL like:: @@ -4331,7 +4327,7 @@ class ColumnClause( from sqlalchemy import column id, name = column("id"), column("name") - stmt = select([id, name]).select_from("user") + stmt = select(id, name).select_from("user") The above statement would produce SQL like:: @@ -4345,7 +4341,7 @@ class ColumnClause( from sqlalchemy.sql import column id, name = column("id"), column("name") - stmt = select([id, name]).select_from("user") + stmt = select(id, name).select_from("user") The text handled by :func:`_expression.column` is assumed to be handled @@ -4375,7 +4371,7 @@ class ColumnClause( column("description"), ) - stmt = select([user.c.description]).where(user.c.name == 'wendy') + stmt = select(user.c.description).where(user.c.name == 'wendy') A :func:`_expression.column` / :func:`.table` construct like that illustrated diff --git a/lib/sqlalchemy/sql/functions.py b/lib/sqlalchemy/sql/functions.py index c1b8bbd27..c7ddcc18a 100644 --- a/lib/sqlalchemy/sql/functions.py +++ b/lib/sqlalchemy/sql/functions.py @@ -137,7 +137,7 @@ class FunctionElement(Executable, ColumnElement, FromClause): from sqlalchemy.sql import column - stmt = select([column('x'), column('y')]).\ + stmt = select(column('x'), column('y')).\ select_from(func.myfunction()) @@ -317,7 +317,7 @@ class FunctionElement(Executable, ColumnElement, FromClause): from sqlalchemy.sql import column - stmt = select([column('data_view')]).\ + stmt = select(column('data_view')).\ select_from(SomeTable).\ select_from(func.unnest(SomeTable.data).alias('data_view') ) @@ -343,7 +343,7 @@ class FunctionElement(Executable, ColumnElement, FromClause): This is shorthand for:: - s = select([function_element]) + s = select(function_element) """ s = Select._create_select(self) @@ -453,7 +453,7 @@ class _FunctionGenerator(object): The returned object is an instance of :class:`.Function`, and is a column-oriented SQL element like any other, and is used in that way:: - >>> print(select([func.count(table.c.id)])) + >>> print(select(func.count(table.c.id))) SELECT count(sometable.id) FROM sometable Any name can be given to :data:`.func`. If the function name is unknown to @@ -680,7 +680,7 @@ class GenericFunction(util.with_metaclass(_GenericMeta, Function)): class as_utc(GenericFunction): type = DateTime - print(select([func.as_utc()])) + print(select(func.as_utc())) User-defined generic functions can be organized into packages by specifying the "package" attribute when defining @@ -697,7 +697,7 @@ class GenericFunction(util.with_metaclass(_GenericMeta, Function)): The above function would be available from :data:`.func` using the package name ``time``:: - print(select([func.time.as_utc()])) + print(select(func.time.as_utc())) A final option is to allow the function to be accessed from one name in :data:`.func` but to render as a different name. @@ -887,7 +887,7 @@ class count(GenericFunction): my_table = table('some_table', column('id')) - stmt = select([func.count()]).select_from(my_table) + stmt = select(func.count()).select_from(my_table) Executing ``stmt`` would emit:: @@ -958,7 +958,7 @@ class array_agg(GenericFunction): e.g.:: - stmt = select([func.array_agg(table.c.values)[2:5]]) + stmt = select(func.array_agg(table.c.values)[2:5]) .. versionadded:: 1.1 @@ -1132,8 +1132,8 @@ class cube(GenericFunction): e.g. :meth:`_expression.Select.group_by`:: stmt = select( - [func.sum(table.c.value), table.c.col_1, table.c.col_2] - ).group_by(func.cube(table.c.col_1, table.c.col_2)) + func.sum(table.c.value), table.c.col_1, table.c.col_2 + ).group_by(func.cube(table.c.col_1, table.c.col_2)) .. versionadded:: 1.2 @@ -1149,7 +1149,7 @@ class rollup(GenericFunction): e.g. :meth:`_expression.Select.group_by`:: stmt = select( - [func.sum(table.c.value), table.c.col_1, table.c.col_2] + func.sum(table.c.value), table.c.col_1, table.c.col_2 ).group_by(func.rollup(table.c.col_1, table.c.col_2)) .. versionadded:: 1.2 @@ -1166,7 +1166,7 @@ class grouping_sets(GenericFunction): e.g. :meth:`_expression.Select.group_by`:: stmt = select( - [func.sum(table.c.value), table.c.col_1, table.c.col_2] + func.sum(table.c.value), table.c.col_1, table.c.col_2 ).group_by(func.grouping_sets(table.c.col_1, table.c.col_2)) In order to group by multiple sets, use the :func:`.tuple_` construct:: @@ -1174,10 +1174,9 @@ class grouping_sets(GenericFunction): from sqlalchemy import tuple_ stmt = select( - [ - func.sum(table.c.value), - table.c.col_1, table.c.col_2, - table.c.col_3] + func.sum(table.c.value), + table.c.col_1, table.c.col_2, + table.c.col_3 ).group_by( func.grouping_sets( tuple_(table.c.col_1, table.c.col_2), diff --git a/lib/sqlalchemy/sql/operators.py b/lib/sqlalchemy/sql/operators.py index 91a0792c3..ba03a6934 100644 --- a/lib/sqlalchemy/sql/operators.py +++ b/lib/sqlalchemy/sql/operators.py @@ -462,7 +462,7 @@ class ColumnOperators(Operators): E.g.:: - stmt = select([sometable]).\ + stmt = select(sometable).\ where(sometable.c.column.like("%foobar%")) :param other: expression to be compared @@ -491,7 +491,7 @@ class ColumnOperators(Operators): E.g.:: - stmt = select([sometable]).\ + stmt = select(sometable).\ where(sometable.c.column.ilike("%foobar%")) :param other: expression to be compared @@ -579,7 +579,7 @@ class ColumnOperators(Operators): stmt.where( column.in_( - select([othertable.c.y]). + select(othertable.c.y). where(table.c.x == othertable.c.x) ) ) @@ -682,7 +682,7 @@ class ColumnOperators(Operators): E.g.:: - stmt = select([sometable]).\ + stmt = select(sometable).\ where(sometable.c.column.startswith("foobar")) Since the operator uses ``LIKE``, wildcard characters @@ -761,7 +761,7 @@ class ColumnOperators(Operators): E.g.:: - stmt = select([sometable]).\ + stmt = select(sometable).\ where(sometable.c.column.endswith("foobar")) Since the operator uses ``LIKE``, wildcard characters @@ -840,7 +840,7 @@ class ColumnOperators(Operators): E.g.:: - stmt = select([sometable]).\ + stmt = select(sometable).\ where(sometable.c.column.contains("foobar")) Since the operator uses ``LIKE``, wildcard characters @@ -1115,7 +1115,7 @@ class ColumnOperators(Operators): expr = 5 == mytable.c.somearray.any_() # mysql '5 = ANY (SELECT value FROM table)' - expr = 5 == select([table.c.value]).scalar_subquery().any_() + expr = 5 == select(table.c.value).scalar_subquery().any_() .. seealso:: @@ -1140,7 +1140,7 @@ class ColumnOperators(Operators): expr = 5 == mytable.c.somearray.all_() # mysql '5 = ALL (SELECT value FROM table)' - expr = 5 == select([table.c.value]).scalar_subquery().all_() + expr = 5 == select(table.c.value).scalar_subquery().all_() .. seealso:: diff --git a/lib/sqlalchemy/sql/selectable.py b/lib/sqlalchemy/sql/selectable.py index d67b61743..ea3bb0512 100644 --- a/lib/sqlalchemy/sql/selectable.py +++ b/lib/sqlalchemy/sql/selectable.py @@ -263,7 +263,7 @@ class HasPrefixes(object): stmt = table.insert().prefix_with("LOW_PRIORITY", dialect="mysql") # MySQL 5.7 optimizer hints - stmt = select([table]).prefix_with( + stmt = select(table).prefix_with( "/*+ BKA(t1) */", dialect="mysql") Multiple prefixes can be specified by multiple calls @@ -315,7 +315,7 @@ class HasSuffixes(object): E.g.:: - stmt = select([col1, col2]).cte().suffix_with( + stmt = select(col1, col2).cte().suffix_with( "cycle empno set y_cycle to 1 default 0", dialect="oracle") Multiple suffixes can be specified by multiple calls @@ -396,7 +396,7 @@ class HasHints(object): the table or alias. E.g. when using Oracle, the following:: - select([mytable]).\ + select(mytable).\ with_hint(mytable, "index(%(name)s ix_mytable)") Would render SQL as:: @@ -407,7 +407,7 @@ class HasHints(object): hint to a particular backend. Such as, to add hints for both Oracle and Sybase simultaneously:: - select([mytable]).\ + select(mytable).\ with_hint(mytable, "index(%(name)s ix_mytable)", 'oracle').\ with_hint(mytable, "WITH INDEX ix_mytable", 'sybase') @@ -523,7 +523,7 @@ class FromClause(roles.AnonymizedFromClauseRole, Selectable): j = user_table.join(address_table, user_table.c.id == address_table.c.user_id) - stmt = select([user_table]).select_from(j) + stmt = select(user_table).select_from(j) would emit SQL along the lines of:: @@ -709,7 +709,7 @@ class FromClause(roles.AnonymizedFromClauseRole, Selectable): to the construction of SQL expressions using table-bound or other selectable-bound columns:: - select([mytable]).where(mytable.c.somecolumn == 5) + select(mytable).where(mytable.c.somecolumn == 5) """ @@ -923,7 +923,7 @@ class Join(roles.DMLTableRole, FromClause): j = join(user_table, address_table, user_table.c.id == address_table.c.user_id) - stmt = select([user_table]).select_from(j) + stmt = select(user_table).select_from(j) would emit SQL along the lines of:: @@ -1251,9 +1251,9 @@ class Join(roles.DMLTableRole, FromClause): from sqlalchemy import select, alias j = alias( - select([j.left, j.right]).\ + select(j.left, j.right).\ select_from(j).\ - with_labels(True).\ + apply_labels().\ correlate(False), name=name ) @@ -1635,7 +1635,7 @@ class TableSample(AliasedReturnsRows): func.bernoulli(1), name='alias', seed=func.random()) - stmt = select([selectable.c.people_id]) + stmt = select(selectable.c.people_id) Assuming ``people`` with a column ``people_id``, the above statement would render as:: @@ -1836,27 +1836,27 @@ class HasCTE(roles.HasCTERole): Column('quantity', Integer) ) - regional_sales = select([ + regional_sales = select( orders.c.region, func.sum(orders.c.amount).label('total_sales') - ]).group_by(orders.c.region).cte("regional_sales") + ).group_by(orders.c.region).cte("regional_sales") - top_regions = select([regional_sales.c.region]).\ + top_regions = select(regional_sales.c.region).\ where( regional_sales.c.total_sales > - select([ - func.sum(regional_sales.c.total_sales)/10 - ]) + select( + func.sum(regional_sales.c.total_sales) / 10 + ) ).cte("top_regions") - statement = select([ + statement = select( orders.c.region, orders.c.product, func.sum(orders.c.quantity).label("product_units"), func.sum(orders.c.amount).label("product_sales") - ]).where(orders.c.region.in_( - select([top_regions.c.region]) + ).where(orders.c.region.in_( + select(top_regions.c.region) )).group_by(orders.c.region, orders.c.product) result = conn.execute(statement).fetchall() @@ -1874,30 +1874,29 @@ class HasCTE(roles.HasCTERole): Column('quantity', Integer), ) - included_parts = select([ - parts.c.sub_part, - parts.c.part, - parts.c.quantity]).\ - where(parts.c.part=='our part').\ - cte(recursive=True) + included_parts = select(\ + parts.c.sub_part, parts.c.part, parts.c.quantity\ + ).\ + where(parts.c.part=='our part').\ + cte(recursive=True) incl_alias = included_parts.alias() parts_alias = parts.alias() included_parts = included_parts.union_all( - select([ + select( parts_alias.c.sub_part, parts_alias.c.part, parts_alias.c.quantity - ]). - where(parts_alias.c.part==incl_alias.c.sub_part) + ).\ + where(parts_alias.c.part==incl_alias.c.sub_part) ) - statement = select([ + statement = select( included_parts.c.sub_part, func.sum(included_parts.c.quantity). label('total_quantity') - ]).\ + ).\ group_by(included_parts.c.sub_part) result = conn.execute(statement).fetchall() @@ -1932,7 +1931,7 @@ class HasCTE(roles.HasCTERole): upsert = visitors.insert().from_select( [visitors.c.product_id, visitors.c.date, visitors.c.count], - select([literal(product_id), literal(day), literal(count)]) + select(literal(product_id), literal(day), literal(count)) .where(~exists(update_cte.select())) ) @@ -2585,7 +2584,7 @@ class SelectBase( Given a SELECT statement such as:: - stmt = select([table.c.id, table.c.name]) + stmt = select(table.c.id, table.c.name) The above statement might look like:: @@ -2596,7 +2595,7 @@ class SelectBase( a named sub-element:: subq = stmt.subquery() - new_stmt = select([subq]) + new_stmt = select(subq) The above renders as:: @@ -2831,7 +2830,7 @@ class GenerativeSelect(DeprecatedSelectBaseGenerations, SelectBase): E.g.:: - stmt = select([table]).with_for_update(nowait=True) + stmt = select(table).with_for_update(nowait=True) On a database like PostgreSQL or Oracle, the above would render a statement like:: @@ -3035,7 +3034,7 @@ class GenerativeSelect(DeprecatedSelectBaseGenerations, SelectBase): e.g.:: - stmt = select([table]).order_by(table.c.id, table.c.name) + stmt = select(table).order_by(table.c.id, table.c.name) :param \*clauses: a series of :class:`_expression.ColumnElement` constructs @@ -3062,7 +3061,7 @@ class GenerativeSelect(DeprecatedSelectBaseGenerations, SelectBase): e.g.:: - stmt = select([table.c.name, func.max(table.c.stat)]).\ + stmt = select(table.c.name, func.max(table.c.stat)).\ group_by(table.c.name) :param \*clauses: a series of :class:`_expression.ColumnElement` @@ -4740,7 +4739,7 @@ class Select( table1 = table('t1', column('a')) table2 = table('t2', column('b')) - s = select([table1.c.a]).\ + s = select(table1.c.a).\ select_from( table1.join(table2, table1.c.a==table2.c.b) ) @@ -4762,7 +4761,7 @@ class Select( if desired, in the case that the FROM clause cannot be fully derived from the columns clause:: - select([func.count('*')]).select_from(table1) + select(func.count('*')).select_from(table1) """ @@ -4827,7 +4826,7 @@ class Select( """ self._auto_correlate = False - if fromclauses and fromclauses[0] is None: + if fromclauses and fromclauses[0] in {None, False}: self._correlate = () else: self._correlate = self._correlate + tuple( @@ -4865,7 +4864,7 @@ class Select( """ self._auto_correlate = False - if fromclauses and fromclauses[0] is None: + if fromclauses and fromclauses[0] in {None, False}: self._correlate_except = () else: self._correlate_except = (self._correlate_except or ()) + tuple( @@ -4894,7 +4893,7 @@ class Select( col1 = column('q', Integer) col2 = column('p', Integer) - stmt = select([col1, col2]) + stmt = select(col1, col2) Above, ``stmt.selected_columns`` would be a collection that contains the ``col1`` and ``col2`` objects directly. For a statement that is diff --git a/lib/sqlalchemy/sql/sqltypes.py b/lib/sqlalchemy/sql/sqltypes.py index f1063b71a..e2edf20b5 100644 --- a/lib/sqlalchemy/sql/sqltypes.py +++ b/lib/sqlalchemy/sql/sqltypes.py @@ -239,7 +239,7 @@ class String(Concatenable, TypeEngine): E.g.:: >>> from sqlalchemy import cast, select, String - >>> print(select([cast('some string', String(collation='utf8'))])) + >>> print(select(cast('some string', String(collation='utf8')))) SELECT CAST(:param_1 AS VARCHAR COLLATE utf8) AS anon_1 :param convert_unicode: When set to ``True``, the @@ -2394,9 +2394,9 @@ class JSON(Indexable, TypeEngine): e.g.:: - stmt = select([ + stmt = select( mytable.c.json_column['some_data'].as_boolean() - ]).where( + ).where( mytable.c.json_column['some_data'].as_boolean() == True ) @@ -2410,9 +2410,9 @@ class JSON(Indexable, TypeEngine): e.g.:: - stmt = select([ + stmt = select( mytable.c.json_column['some_data'].as_string() - ]).where( + ).where( mytable.c.json_column['some_data'].as_string() == 'some string' ) @@ -2427,9 +2427,9 @@ class JSON(Indexable, TypeEngine): e.g.:: - stmt = select([ + stmt = select( mytable.c.json_column['some_data'].as_integer() - ]).where( + ).where( mytable.c.json_column['some_data'].as_integer() == 5 ) @@ -2443,9 +2443,9 @@ class JSON(Indexable, TypeEngine): e.g.:: - stmt = select([ + stmt = select( mytable.c.json_column['some_data'].as_float() - ]).where( + ).where( mytable.c.json_column['some_data'].as_float() == 29.75 ) @@ -2460,9 +2460,7 @@ class JSON(Indexable, TypeEngine): e.g.:: - stmt = select([ - mytable.c.json_column['some_data'].as_json() - ]) + stmt = select(mytable.c.json_column['some_data'].as_json()) This is typically the default behavior of indexed elements in any case. @@ -2614,7 +2612,7 @@ class ARRAY(SchemaEventTarget, Indexable, Concatenable, TypeEngine): constructs which will produce the appropriate SQL, both for SELECT statements:: - select([mytable.c.data[5], mytable.c.data[2:7]]) + select(mytable.c.data[5], mytable.c.data[2:7]) as well as UPDATE statements when the :meth:`_expression.Update.values` method @@ -2693,7 +2691,7 @@ class ARRAY(SchemaEventTarget, Indexable, Concatenable, TypeEngine): from sqlalchemy.sql import operators conn.execute( - select([table.c.data]).where( + select(table.c.data).where( table.c.data.any(7, operator=operators.lt) ) ) @@ -2733,7 +2731,7 @@ class ARRAY(SchemaEventTarget, Indexable, Concatenable, TypeEngine): from sqlalchemy.sql import operators conn.execute( - select([table.c.data]).where( + select(table.c.data).where( table.c.data.all(7, operator=operators.lt) ) ) diff --git a/lib/sqlalchemy/sql/visitors.py b/lib/sqlalchemy/sql/visitors.py index 5cb3cba70..27499b5f7 100644 --- a/lib/sqlalchemy/sql/visitors.py +++ b/lib/sqlalchemy/sql/visitors.py @@ -664,7 +664,7 @@ def traverse(obj, opts, visitors): from sqlalchemy.sql import visitors - stmt = select([some_table]).where(some_table.c.foo == 'bar') + stmt = select(some_table).where(some_table.c.foo == 'bar') def visit_bindparam(bind_param): print("found bound value: %s" % bind_param.value) |
