diff options
author | Federico Caselli <cfederico87@gmail.com> | 2023-01-10 21:39:44 +0100 |
---|---|---|
committer | Federico Caselli <cfederico87@gmail.com> | 2023-01-11 20:24:29 +0100 |
commit | dce11383f83c28f2acc0ed9ee346a56d63e9fcf8 (patch) | |
tree | 3a0196dd35fe8e87a8fadaa2611cc308d6d65914 /lib/sqlalchemy/sql | |
parent | a950402dae2a5b2448f5f4235946b2f767c7485c (diff) | |
download | sqlalchemy-dce11383f83c28f2acc0ed9ee346a56d63e9fcf8.tar.gz |
Improve sql formatting
change {opensql} to {printsql} in prints, add missing markers
Change-Id: I07b72e6620bb64e329d6b641afa27631e91c4f16
Diffstat (limited to 'lib/sqlalchemy/sql')
-rw-r--r-- | lib/sqlalchemy/sql/_elements_constructors.py | 16 | ||||
-rw-r--r-- | lib/sqlalchemy/sql/dml.py | 4 | ||||
-rw-r--r-- | lib/sqlalchemy/sql/functions.py | 30 | ||||
-rw-r--r-- | lib/sqlalchemy/sql/selectable.py | 14 | ||||
-rw-r--r-- | lib/sqlalchemy/sql/sqltypes.py | 2 |
5 files changed, 33 insertions, 33 deletions
diff --git a/lib/sqlalchemy/sql/_elements_constructors.py b/lib/sqlalchemy/sql/_elements_constructors.py index 1d8818a1f..6e5a7bc5e 100644 --- a/lib/sqlalchemy/sql/_elements_constructors.py +++ b/lib/sqlalchemy/sql/_elements_constructors.py @@ -1066,23 +1066,23 @@ def false() -> False_: >>> from sqlalchemy import false >>> print(select(t.c.x).where(false())) - SELECT x FROM t WHERE false + {printsql}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())) - SELECT x FROM t WHERE 0 = 1 + {printsql}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()))) - SELECT x FROM t WHERE true + {printsql}SELECT x FROM t WHERE true >>> print(select(t.c.x).where(and_(t.c.x > 5, false()))) - SELECT x FROM t WHERE false + {printsql}SELECT x FROM t WHERE false .. versionchanged:: 0.9 :func:`.true` and :func:`.false` feature better integrated behavior within conjunctions and on dialects @@ -1483,23 +1483,23 @@ def true() -> True_: >>> from sqlalchemy import true >>> print(select(t.c.x).where(true())) - SELECT x FROM t WHERE true + {printsql}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())) - SELECT x FROM t WHERE 1 = 1 + {printsql}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()))) - SELECT x FROM t WHERE true + {printsql}SELECT x FROM t WHERE true >>> print(select(t.c.x).where(and_(t.c.x > 5, false()))) - SELECT x FROM t WHERE false + {printsql}SELECT x FROM t WHERE false .. versionchanged:: 0.9 :func:`.true` and :func:`.false` feature better integrated behavior within conjunctions and on dialects diff --git a/lib/sqlalchemy/sql/dml.py b/lib/sqlalchemy/sql/dml.py index 355a00c60..1ab7f2ceb 100644 --- a/lib/sqlalchemy/sql/dml.py +++ b/lib/sqlalchemy/sql/dml.py @@ -681,7 +681,7 @@ class UpdateBase( ... .returning(table.c.server_flag, table.c.updated_timestamp) ... ) >>> print(stmt) - UPDATE some_table SET status=:status + {printsql}UPDATE some_table SET status=:status WHERE some_table.data = :data_1 RETURNING some_table.server_flag, some_table.updated_timestamp @@ -702,7 +702,7 @@ class UpdateBase( ... (table.c.first_name + " " + table.c.last_name).label("fullname") ... ) >>> print(stmt) - INSERT INTO some_table (first_name, last_name) + {printsql}INSERT INTO some_table (first_name, last_name) VALUES (:first_name, :last_name) RETURNING some_table.first_name || :first_name_1 || some_table.last_name AS fullname diff --git a/lib/sqlalchemy/sql/functions.py b/lib/sqlalchemy/sql/functions.py index 446cbcd3c..902811037 100644 --- a/lib/sqlalchemy/sql/functions.py +++ b/lib/sqlalchemy/sql/functions.py @@ -194,7 +194,7 @@ class FunctionElement(Executable, ColumnElement[_T], FromClause, Generative): >>> from sqlalchemy import func, select >>> fn = func.jsonb_each("{'k', 'v'}").scalar_table_valued("key") >>> print(select(fn)) - SELECT (jsonb_each(:jsonb_each_1)).key + {printsql}SELECT (jsonb_each(:jsonb_each_1)).key .. versionadded:: 1.4.0b2 @@ -222,11 +222,11 @@ class FunctionElement(Executable, ColumnElement[_T], FromClause, Generative): ... ) >>> print(select(fn)) - SELECT anon_1.value, anon_1.start, anon_1.stop, anon_1.step + {printsql}SELECT anon_1.value, anon_1.start, anon_1.stop, anon_1.step FROM generate_series(:generate_series_1, :generate_series_2) AS anon_1 >>> print(select(fn.c.value, fn.c.stop).where(fn.c.value > 2)) - SELECT anon_1.value, anon_1.stop + {printsql}SELECT anon_1.value, anon_1.stop FROM generate_series(:generate_series_1, :generate_series_2) AS anon_1 WHERE anon_1.value > :value_1 @@ -235,7 +235,7 @@ class FunctionElement(Executable, ColumnElement[_T], FromClause, Generative): >>> fn = func.generate_series(4, 1, -1).table_valued("gen", with_ordinality="ordinality") >>> print(select(fn)) - SELECT anon_1.gen, anon_1.ordinality + {printsql}SELECT anon_1.gen, anon_1.ordinality FROM generate_series(:generate_series_1, :generate_series_2, :generate_series_3) WITH ORDINALITY AS anon_1 :param \*expr: A series of string column names that will be added to the @@ -301,7 +301,7 @@ class FunctionElement(Executable, ColumnElement[_T], FromClause, Generative): >>> from sqlalchemy import select, func >>> gs = func.generate_series(1, 5, -1).column_valued() >>> print(select(gs)) - SELECT anon_1 + {printsql}SELECT anon_1 FROM generate_series(:generate_series_1, :generate_series_2, :generate_series_3) AS anon_1 This is shorthand for:: @@ -341,7 +341,7 @@ class FunctionElement(Executable, ColumnElement[_T], FromClause, Generative): >>> from sqlalchemy import column, select, func >>> stmt = select(column('x'), column('y')).select_from(func.myfunction()) >>> print(stmt) - SELECT x, y FROM myfunction() + {printsql}SELECT x, y FROM myfunction() The above form is a legacy feature that is now superseded by the fully capable :meth:`_functions.FunctionElement.table_valued` @@ -588,7 +588,7 @@ class FunctionElement(Executable, ColumnElement[_T], FromClause, Generative): >>> from sqlalchemy import func, select, column >>> data_view = func.unnest([1, 2, 3]).alias("data_view") >>> print(select(data_view.column)) - SELECT data_view + {printsql}SELECT data_view FROM unnest(:unnest_1) AS data_view The :meth:`_functions.FunctionElement.column_valued` method provides @@ -596,7 +596,7 @@ class FunctionElement(Executable, ColumnElement[_T], FromClause, Generative): >>> data_view = func.unnest([1, 2, 3]).column_valued("data_view") >>> print(select(data_view)) - SELECT data_view + {printsql}SELECT data_view FROM unnest(:unnest_1) AS data_view .. versionadded:: 1.4.0b2 Added the ``.column`` accessor @@ -772,7 +772,7 @@ class _FunctionGenerator: column-oriented SQL element like any other, and is used in that way:: >>> print(select(func.count(table.c.id))) - SELECT count(sometable.id) FROM sometable + {printsql}SELECT count(sometable.id) FROM sometable Any name can be given to :data:`.func`. If the function name is unknown to SQLAlchemy, it will be rendered exactly as is. For common SQL functions @@ -780,13 +780,13 @@ class _FunctionGenerator: function* which will be compiled appropriately to the target database:: >>> print(func.current_timestamp()) - CURRENT_TIMESTAMP + {printsql}CURRENT_TIMESTAMP To call functions which are present in dot-separated packages, specify them in the same manner:: >>> print(func.stats.yield_curve(5, 10)) - stats.yield_curve(:yield_curve_1, :yield_curve_2) + {printsql}stats.yield_curve(:yield_curve_1, :yield_curve_2) SQLAlchemy can be made aware of the return type of functions to enable type-specific lexical and result-based behavior. For example, to ensure @@ -796,7 +796,7 @@ class _FunctionGenerator: >>> print(func.my_string(u'hi', type_=Unicode) + ' ' + ... func.my_string(u'there', type_=Unicode)) - my_string(:my_string_1) || :my_string_2 || my_string(:my_string_3) + {printsql}my_string(:my_string_1) || :my_string_2 || my_string(:my_string_3) The object returned by a :data:`.func` call is usually an instance of :class:`.Function`. @@ -834,7 +834,7 @@ class _FunctionGenerator: :class:`.Function` - """ + """ # noqa def __init__(self, **opts): self.__names = [] @@ -1250,14 +1250,14 @@ class concat(GenericFunction[str]): E.g.:: >>> print(select(func.concat('a', 'b'))) - SELECT concat(:concat_2, :concat_3) AS concat_1 + {printsql}SELECT concat(:concat_2, :concat_3) AS concat_1 String concatenation in SQLAlchemy is more commonly available using the Python ``+`` operator with string datatypes, which will render a backend-specific concatenation operator, such as :: >>> print(select(literal("a") + "b")) - SELECT :param_1 || :param_2 AS anon_1 + {printsql}SELECT :param_1 || :param_2 AS anon_1 """ diff --git a/lib/sqlalchemy/sql/selectable.py b/lib/sqlalchemy/sql/selectable.py index 898b524ad..1d283e83c 100644 --- a/lib/sqlalchemy/sql/selectable.py +++ b/lib/sqlalchemy/sql/selectable.py @@ -1032,7 +1032,7 @@ class NamedFromClause(FromClause): >>> a = table("a", column("id"), column("x"), column("y")) >>> stmt = select(func.row_to_json(a.table_valued())) >>> print(stmt) - SELECT row_to_json(a) AS row_to_json_1 + {printsql}SELECT row_to_json(a) AS row_to_json_1 FROM a .. versionadded:: 1.4.0b2 @@ -1061,7 +1061,7 @@ class SelectLabelStyle(Enum): >>> table1 = table("table1", column("columna"), column("columnb")) >>> table2 = table("table2", column("columna"), column("columnc")) >>> print(select(table1, table2).join(table2, true()).set_label_style(LABEL_STYLE_NONE)) - SELECT table1.columna, table1.columnb, table2.columna, table2.columnc + {printsql}SELECT table1.columna, table1.columnb, table2.columna, table2.columnc FROM table1 JOIN table2 ON true Used with the :meth:`_sql.Select.set_label_style` method. @@ -1084,7 +1084,7 @@ class SelectLabelStyle(Enum): >>> table1 = table("table1", column("columna"), column("columnb")) >>> table2 = table("table2", column("columna"), column("columnc")) >>> print(select(table1, table2).join(table2, true()).set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL)) - SELECT table1.columna AS table1_columna, table1.columnb AS table1_columnb, table2.columna AS table2_columna, table2.columnc AS table2_columnc + {printsql}SELECT table1.columna AS table1_columna, table1.columnb AS table1_columnb, table2.columna AS table2_columna, table2.columnc AS table2_columnc FROM table1 JOIN table2 ON true Used with the :meth:`_sql.GenerativeSelect.set_label_style` method. @@ -1111,7 +1111,7 @@ class SelectLabelStyle(Enum): >>> table1 = table("table1", column("columna"), column("columnb")) >>> table2 = table("table2", column("columna"), column("columnc")) >>> print(select(table1, table2).join(table2, true()).set_label_style(LABEL_STYLE_DISAMBIGUATE_ONLY)) - SELECT table1.columna, table1.columnb, table2.columna AS columna_1, table2.columnc + {printsql}SELECT table1.columna, table1.columnb, table2.columna AS columna_1, table2.columnc FROM table1 JOIN table2 ON true Used with the :meth:`_sql.GenerativeSelect.set_label_style` method, @@ -1719,7 +1719,7 @@ class TableValuedAlias(LateralFromClause, Alias): >>> from sqlalchemy import select, func >>> fn = func.json_array_elements_text('["one", "two", "three"]').table_valued("value") >>> print(select(fn.c.value)) - SELECT anon_1.value + {printsql}SELECT anon_1.value FROM json_array_elements_text(:json_array_elements_text_1) AS anon_1 .. versionadded:: 1.4.0b2 @@ -1831,7 +1831,7 @@ class TableValuedAlias(LateralFromClause, Alias): table_valued("x", with_ordinality="o").render_derived() ... ) ... ) - SELECT anon_1.x, anon_1.o + {printsql}SELECT anon_1.x, anon_1.o FROM unnest(ARRAY[%(param_1)s, %(param_2)s, %(param_3)s]) WITH ORDINALITY AS anon_1(x, o) The ``with_types`` keyword will render column types inline within @@ -1847,7 +1847,7 @@ class TableValuedAlias(LateralFromClause, Alias): ... .render_derived(with_types=True) ... ) ... ) - SELECT anon_1.a, anon_1.b FROM json_to_recordset(:json_to_recordset_1) + {printsql}SELECT anon_1.a, anon_1.b FROM json_to_recordset(:json_to_recordset_1) AS anon_1(a INTEGER, b VARCHAR) :param name: optional string name that will be applied to the alias diff --git a/lib/sqlalchemy/sql/sqltypes.py b/lib/sqlalchemy/sql/sqltypes.py index bcdbcc804..bcbc7004c 100644 --- a/lib/sqlalchemy/sql/sqltypes.py +++ b/lib/sqlalchemy/sql/sqltypes.py @@ -206,7 +206,7 @@ class String(Concatenable, TypeEngine[str]): >>> from sqlalchemy import cast, select, String >>> print(select(cast('some string', String(collation='utf8')))) - SELECT CAST(:param_1 AS VARCHAR COLLATE utf8) AS anon_1 + {printsql}SELECT CAST(:param_1 AS VARCHAR COLLATE utf8) AS anon_1 .. note:: |