diff options
| author | Gord Thompson <gord@gordthompson.com> | 2021-01-09 14:56:38 -0700 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2021-01-26 16:52:30 -0500 |
| commit | 22f65156bbe846dea2fb9f87fe4187abe0ed790a (patch) | |
| tree | f49338a10dd2800d4d754b14d2e7fd549b4b833f /test/sql | |
| parent | 7bdb1f30f66aaea16efbcf96e314491058493e6c (diff) | |
| download | sqlalchemy-22f65156bbe846dea2fb9f87fe4187abe0ed790a.tar.gz | |
Replace with_labels() and apply_labels() in ORM/Core
Replace :meth:`_orm.Query.with_labels` and
:meth:`_sql.GenerativeSelect.apply_labels` with explicit getters and
setters ``get_label_style`` and ``set_label_style`` to accommodate the
three supported label styles: ``LABEL_STYLE_DISAMBIGUATE_ONLY`` (default),
``LABEL_STYLE_TABLENAME_PLUS_COL``, and ``LABEL_STYLE_NONE``.
In addition, for Core and "future style" ORM queries,
``LABEL_STYLE_DISAMBIGUATE_ONLY`` is now the default label style. This
style differs from the existing "no labels" style in that labeling is
applied in the case of column name conflicts; with ``LABEL_STYLE_NONE``, a
duplicate column name is not accessible via name in any case.
For legacy ORM queries using :class:`_query.Query`, the table-plus-column
names labeling style applied by ``LABEL_STYLE_TABLENAME_PLUS_COL``
continues to be used so that existing test suites and logging facilities
see no change in behavior by default, however this style of labeling is no
longer required for SQLAlchemy queries to function, as result sets are
commonly matched to columns using a positional approach since SQLAlchemy
1.0.
Within test suites, all use of apply_labels() / use_labels
now uses the new methods. New tests added to
test/sql/test_deprecations.py nad test/orm/test_deprecations.py
to cover just the old apply_labels() method call. Tests
in ORM that made explicit use apply_labels()/ etc. where it isn't needed
for the ORM to work correctly use default label style now.
Co-authored-by: Mike Bayer <mike_mp@zzzcomputing.com>
Fixes: #4757
Change-Id: I5fdcd2ed4ae8c7fe62f8be2b6d0e8f66409b6a54
Diffstat (limited to 'test/sql')
| -rw-r--r-- | test/sql/test_compare.py | 5 | ||||
| -rw-r--r-- | test/sql/test_compiler.py | 181 | ||||
| -rw-r--r-- | test/sql/test_cte.py | 61 | ||||
| -rw-r--r-- | test/sql/test_deprecations.py | 40 | ||||
| -rw-r--r-- | test/sql/test_external_traversal.py | 77 | ||||
| -rw-r--r-- | test/sql/test_functions.py | 6 | ||||
| -rw-r--r-- | test/sql/test_labels.py | 93 | ||||
| -rw-r--r-- | test/sql/test_operators.py | 13 | ||||
| -rw-r--r-- | test/sql/test_query.py | 57 | ||||
| -rw-r--r-- | test/sql/test_quote.py | 25 | ||||
| -rw-r--r-- | test/sql/test_resultset.py | 52 | ||||
| -rw-r--r-- | test/sql/test_selectable.py | 170 | ||||
| -rw-r--r-- | test/sql/test_text.py | 21 | ||||
| -rw-r--r-- | test/sql/test_type_expressions.py | 17 |
14 files changed, 570 insertions, 248 deletions
diff --git a/test/sql/test_compare.py b/test/sql/test_compare.py index 1ac3613f7..fc9fd0926 100644 --- a/test/sql/test_compare.py +++ b/test/sql/test_compare.py @@ -61,6 +61,7 @@ from sqlalchemy.sql.lambdas import LambdaOptions from sqlalchemy.sql.selectable import _OffsetLimitParam from sqlalchemy.sql.selectable import AliasedReturnsRows from sqlalchemy.sql.selectable import FromGrouping +from sqlalchemy.sql.selectable import LABEL_STYLE_TABLENAME_PLUS_COL from sqlalchemy.sql.selectable import Select from sqlalchemy.sql.selectable import Selectable from sqlalchemy.sql.selectable import SelectStatementGrouping @@ -363,7 +364,9 @@ class CoreFixtures(object): select(table_a.c.b, table_a.c.a) .limit(literal_column("foobar")) .offset(10), - select(table_a.c.b, table_a.c.a).apply_labels(), + select(table_a.c.b, table_a.c.a).set_label_style( + LABEL_STYLE_TABLENAME_PLUS_COL + ), select(table_a.c.a).where(table_a.c.b == 5), select(table_a.c.a) .where(table_a.c.b == 5) diff --git a/test/sql/test_compiler.py b/test/sql/test_compiler.py index 4429753ec..fea75d679 100644 --- a/test/sql/test_compiler.py +++ b/test/sql/test_compiler.py @@ -81,6 +81,8 @@ from sqlalchemy.sql.elements import BooleanClauseList from sqlalchemy.sql.expression import ClauseElement from sqlalchemy.sql.expression import ClauseList from sqlalchemy.sql.expression import HasPrefixes +from sqlalchemy.sql.selectable import LABEL_STYLE_NONE +from sqlalchemy.sql.selectable import LABEL_STYLE_TABLENAME_PLUS_COL from sqlalchemy.testing import assert_raises from sqlalchemy.testing import assert_raises_message from sqlalchemy.testing import AssertsCompiledSQL @@ -577,7 +579,7 @@ class SelectTest(fixtures.TestBase, AssertsCompiledSQL): sq = ( select(table1, table2) .where(and_(table1.c.myid == 7, table2.c.otherid == table1.c.myid)) - .apply_labels() + .set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) .alias("sq") ) @@ -597,7 +599,11 @@ class SelectTest(fixtures.TestBase, AssertsCompiledSQL): "sq.myothertable_othername FROM (%s) AS sq" % sqstring, ) - sq2 = select(sq).apply_labels().alias("sq2") + sq2 = ( + select(sq) + .set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) + .alias("sq2") + ) self.assert_compile( sq2.select(), @@ -622,22 +628,27 @@ class SelectTest(fixtures.TestBase, AssertsCompiledSQL): def test_use_labels(self): self.assert_compile( - select(table1.c.myid == 5).apply_labels(), + select(table1.c.myid == 5).set_label_style( + LABEL_STYLE_TABLENAME_PLUS_COL + ), "SELECT mytable.myid = :myid_1 AS anon_1 FROM mytable", ) self.assert_compile( - select(func.foo()).apply_labels(), "SELECT foo() AS foo_1" + select(func.foo()).set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL), + "SELECT foo() AS foo_1", ) # this is native_boolean=False for default dialect self.assert_compile( - select(not_(True)).apply_labels(), + select(not_(True)).set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL), "SELECT :param_1 = 0 AS anon_1", ) self.assert_compile( - select(cast("data", Integer)).apply_labels(), + select(cast("data", Integer)).set_label_style( + LABEL_STYLE_TABLENAME_PLUS_COL + ), "SELECT CAST(:param_1 AS INTEGER) AS anon_1", ) @@ -653,13 +664,17 @@ class SelectTest(fixtures.TestBase, AssertsCompiledSQL): ) self.assert_compile( - select(keyed).apply_labels(), + select(keyed).set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL), "SELECT keyed.x AS keyed_x, keyed.y AS " "keyed_y, keyed.z AS keyed_z FROM keyed", ) self.assert_compile( - select(select(keyed).apply_labels().subquery()).apply_labels(), + select( + select(keyed) + .set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) + .subquery() + ).set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL), "SELECT anon_1.keyed_x AS anon_1_keyed_x, " "anon_1.keyed_y AS anon_1_keyed_y, " "anon_1.keyed_z AS anon_1_keyed_z " @@ -768,7 +783,9 @@ class SelectTest(fixtures.TestBase, AssertsCompiledSQL): foo = table("foo", column("id"), column("bar_id")) foo_bar = table("foo_bar", column("id")) - stmt = select(foo, foo_bar).apply_labels() + stmt = select(foo, foo_bar).set_label_style( + LABEL_STYLE_TABLENAME_PLUS_COL + ) self.assert_compile( stmt, "SELECT foo.id AS foo_id, foo.bar_id AS foo_bar_id, " @@ -812,7 +829,7 @@ class SelectTest(fixtures.TestBase, AssertsCompiledSQL): foo.c.bar_id, foo_bar.c.id, foo_bar.c.id, - ).apply_labels() + ).set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) self.assert_compile( stmt, "SELECT foo.id AS foo_id, " @@ -830,7 +847,7 @@ class SelectTest(fixtures.TestBase, AssertsCompiledSQL): # of the same column are not used. only the label applied to the # first occurrence of each column is used self.assert_compile( - select(stmt.subquery()), + select(stmt.subquery()).set_label_style(LABEL_STYLE_NONE), "SELECT " "anon_1.foo_id, " # from 1st foo.id in derived (line 1) "anon_1.foo_bar_id, " # from 1st foo.bar_id in derived (line 2) @@ -856,17 +873,23 @@ class SelectTest(fixtures.TestBase, AssertsCompiledSQL): def test_dupe_columns_use_labels(self): t = table("t", column("a"), column("b")) self.assert_compile( - select(t.c.a, t.c.a, t.c.b, t.c.a).apply_labels(), + select(t.c.a, t.c.a, t.c.b, t.c.a).set_label_style( + LABEL_STYLE_TABLENAME_PLUS_COL + ), "SELECT t.a AS t_a, t.a AS t_a__1, t.b AS t_b, " "t.a AS t_a__1 FROM t", ) def test_dupe_columns_use_labels_derived_selectable(self): t = table("t", column("a"), column("b")) - stmt = select(t.c.a, t.c.a, t.c.b, t.c.a).apply_labels().subquery() + stmt = ( + select(t.c.a, t.c.a, t.c.b, t.c.a) + .set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) + .subquery() + ) self.assert_compile( - select(stmt), + select(stmt).set_label_style(LABEL_STYLE_NONE), "SELECT anon_1.t_a, anon_1.t_a, anon_1.t_b, anon_1.t_a FROM " "(SELECT t.a AS t_a, t.a AS t_a__1, t.b AS t_b, t.a AS t_a__1 " "FROM t) AS anon_1", @@ -877,19 +900,25 @@ class SelectTest(fixtures.TestBase, AssertsCompiledSQL): a, b, a_a = t.c.a, t.c.b, t.c.a._annotate({"some_orm_thing": True}) self.assert_compile( - select(a, a_a, b, a_a).apply_labels(), + select(a, a_a, b, a_a).set_label_style( + LABEL_STYLE_TABLENAME_PLUS_COL + ), "SELECT t.a AS t_a, t.a AS t_a__1, t.b AS t_b, " "t.a AS t_a__1 FROM t", ) self.assert_compile( - select(a_a, a, b, a_a).apply_labels(), + select(a_a, a, b, a_a).set_label_style( + LABEL_STYLE_TABLENAME_PLUS_COL + ), "SELECT t.a AS t_a, t.a AS t_a__1, t.b AS t_b, " "t.a AS t_a__1 FROM t", ) self.assert_compile( - select(a_a, a_a, b, a).apply_labels(), + select(a_a, a_a, b, a).set_label_style( + LABEL_STYLE_TABLENAME_PLUS_COL + ), "SELECT t.a AS t_a, t.a AS t_a__1, t.b AS t_b, " "t.a AS t_a__1 FROM t", ) @@ -897,10 +926,14 @@ class SelectTest(fixtures.TestBase, AssertsCompiledSQL): def test_dupe_columns_use_labels_derived_selectable_mix_annotations(self): t = table("t", column("a"), column("b")) a, b, a_a = t.c.a, t.c.b, t.c.a._annotate({"some_orm_thing": True}) - stmt = select(a, a_a, b, a_a).apply_labels().subquery() + stmt = ( + select(a, a_a, b, a_a) + .set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) + .subquery() + ) self.assert_compile( - select(stmt), + select(stmt).set_label_style(LABEL_STYLE_NONE), "SELECT anon_1.t_a, anon_1.t_a, anon_1.t_b, anon_1.t_a FROM " "(SELECT t.a AS t_a, t.a AS t_a__1, t.b AS t_b, t.a AS t_a__1 " "FROM t) AS anon_1", @@ -918,7 +951,7 @@ class SelectTest(fixtures.TestBase, AssertsCompiledSQL): foo_bar.c.id, foo_bar__id, foo_bar__id, - ).apply_labels() + ).set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) self.assert_compile( stmt, @@ -935,7 +968,9 @@ class SelectTest(fixtures.TestBase, AssertsCompiledSQL): # second and third occurrences of a.c.a are labeled, but are # dupes of each other. self.assert_compile( - select(a.c.a, a.c.a, a.c.b, a.c.a).apply_labels(), + select(a.c.a, a.c.a, a.c.b, a.c.a).set_label_style( + LABEL_STYLE_TABLENAME_PLUS_COL + ), "SELECT t_1.a AS t_1_a, t_1.a AS t_1_a__1, t_1.b AS t_1_b, " "t_1.a AS t_1_a__1 " "FROM t AS t_1", @@ -945,9 +980,9 @@ class SelectTest(fixtures.TestBase, AssertsCompiledSQL): """test nested anonymous label generation.""" s1 = table1.select() s2 = s1.alias() - s3 = select(s2).apply_labels() + s3 = select(s2).set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) s4 = s3.alias() - s5 = select(s4).apply_labels() + s5 = select(s4).set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) self.assert_compile( s5, "SELECT anon_1.anon_2_myid AS " @@ -966,7 +1001,7 @@ class SelectTest(fixtures.TestBase, AssertsCompiledSQL): def test_nested_label_targeting_keyed(self): s1 = keyed.select() s2 = s1.alias() - s3 = select(s2).apply_labels() + s3 = select(s2).set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) self.assert_compile( s3, "SELECT anon_1.x AS anon_1_x, " @@ -977,7 +1012,7 @@ class SelectTest(fixtures.TestBase, AssertsCompiledSQL): ) s4 = s3.alias() - s5 = select(s4).apply_labels() + s5 = select(s4).set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) self.assert_compile( s5, "SELECT anon_1.anon_2_x AS anon_1_anon_2_x, " @@ -997,7 +1032,11 @@ class SelectTest(fixtures.TestBase, AssertsCompiledSQL): jj = select(table1.c[name]).subquery() jjj = join(table1, jj, table1.c[name] == jj.c[name]) - j2 = jjj.select().apply_labels().subquery("foo") + j2 = ( + jjj.select() + .set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) + .subquery("foo") + ) self.assert_compile( j2.select(), @@ -1172,8 +1211,9 @@ class SelectTest(fixtures.TestBase, AssertsCompiledSQL): self.assert_compile( select(s, table1), "SELECT sq2.myid, sq2.name, " - "sq2.description, mytable.myid, " - "mytable.name, mytable.description FROM " + "sq2.description, mytable.myid AS myid_1, " + "mytable.name AS name_1, " + "mytable.description AS description_1 FROM " "(SELECT ta.myid AS myid, ta.name AS name, " "ta.description AS description FROM " "mytable AS ta WHERE EXISTS (SELECT 1 FROM " @@ -1934,7 +1974,7 @@ class SelectTest(fixtures.TestBase, AssertsCompiledSQL): q = ( select(table1, table2.c.otherid) .where(table1.c.myid == table2.c.otherid) - .apply_labels() + .set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) ) # make an alias of the "selectable". column names @@ -1945,7 +1985,9 @@ class SelectTest(fixtures.TestBase, AssertsCompiledSQL): # should produce two underscores. # also, reference the column "mytable_myid" off of the t2view alias. self.assert_compile( - a.select().where(a.c.mytable_myid == 9).apply_labels(), + a.select() + .where(a.c.mytable_myid == 9) + .set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL), "SELECT t2view.mytable_myid AS t2view_mytable_myid, " "t2view.mytable_name " "AS t2view_mytable_name, " @@ -2099,7 +2141,7 @@ class SelectTest(fixtures.TestBase, AssertsCompiledSQL): column("spaces % more spaces"), ) self.assert_compile( - t.select().apply_labels(), + t.select().set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL), """SELECT "table%name"."percent%" AS "table%name_percent%", """ """"table%name"."%(oneofthese)s" AS """ """"table%name_%(oneofthese)s", """ @@ -2146,7 +2188,8 @@ class SelectTest(fixtures.TestBase, AssertsCompiledSQL): users, addresses, users.c.user_id == addresses.c.user_id ).select(), "SELECT users.user_id, users.user_name, users.password, " - "addresses.address_id, addresses.user_id, addresses.street, " + "addresses.address_id, addresses.user_id AS user_id_1, " + "addresses.street, " "addresses.city, addresses.state, addresses.zip " "FROM users JOIN addresses " "ON users.user_id = addresses.user_id", @@ -2221,12 +2264,12 @@ class SelectTest(fixtures.TestBase, AssertsCompiledSQL): ), ]: stmt = select(table1).select_from(spec) - self.assert_compile( - stmt, - "SELECT mytable.myid, mytable.name, mytable.description FROM " - "mytable FULL OUTER JOIN myothertable " - "ON mytable.myid = myothertable.otherid", - ) + self.assert_compile( + stmt, + "SELECT mytable.myid, mytable.name, mytable.description FROM " + "mytable FULL OUTER JOIN myothertable " + "ON mytable.myid = myothertable.otherid", + ) def test_compound_selects(self): assert_raises_message( @@ -2375,10 +2418,14 @@ class SelectTest(fixtures.TestBase, AssertsCompiledSQL): s2 = select(table1, table2.c.otherid).where( table1.c.myid == table2.c.otherid ) + + # note myid__1 is a dedupe of same column, same table. see + # test/sql/test_labels.py for the double underscore thing self.assert_compile( union(s1, s2).order_by(s1.selected_columns.myid), "SELECT mytable.myid, mytable.name, mytable.description, " - "mytable.myid FROM mytable WHERE mytable.myid = :myid_1 " + "mytable.myid AS myid__1 FROM mytable " + "WHERE mytable.myid = :myid_1 " "UNION SELECT mytable.myid, mytable.name, mytable.description, " "myothertable.otherid FROM mytable, myothertable " "WHERE mytable.myid = myothertable.otherid ORDER BY myid", @@ -2563,12 +2610,36 @@ class SelectTest(fixtures.TestBase, AssertsCompiledSQL): # is now officially completely ridiculous AND non-obviously omits # coverage on other dialects. sel = select(tbl, cast(tbl.c.v1, Numeric)).compile(dialect=dialect) + + # TODO: another unusual result from disambiguate only + if isinstance(dialect, type(mysql.dialect())): + eq_( + str(sel), + "SELECT casttest.id, casttest.v1, casttest.v2, " + "casttest.ts, " + "CAST(casttest.v1 AS DECIMAL) AS casttest_v1__1 \n" + "FROM casttest", + ) + else: + eq_( + str(sel), + "SELECT casttest.id, casttest.v1, casttest.v2, " + "casttest.ts, CAST(casttest.v1 AS NUMERIC) AS " + "casttest_v1__1 \nFROM casttest", + ) + + sel = ( + select(tbl, cast(tbl.c.v1, Numeric)) + .set_label_style(LABEL_STYLE_NONE) + .compile(dialect=dialect) + ) if isinstance(dialect, type(mysql.dialect())): eq_( str(sel), "SELECT casttest.id, casttest.v1, casttest.v2, " "casttest.ts, " - "CAST(casttest.v1 AS DECIMAL) AS v1 \nFROM casttest", + "CAST(casttest.v1 AS DECIMAL) AS v1 \n" + "FROM casttest", ) else: eq_( @@ -4771,7 +4842,7 @@ class SchemaTest(fixtures.TestBase, AssertsCompiledSQL): s = ( table4.select() .where(and_(table4.c.datatype_id == 7, table4.c.value == "hi")) - .apply_labels() + .set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) ) self.assert_compile( s, @@ -4797,7 +4868,7 @@ class SchemaTest(fixtures.TestBase, AssertsCompiledSQL): # multi-part schema name labels - convert '.' to '_' self.assert_compile( - table5.select().apply_labels(), + table5.select().set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL), 'SELECT "dbo.remote_owner".remotetable.rem_id AS' " dbo_remote_owner_remotetable_rem_id, " '"dbo.remote_owner".remotetable.datatype_id' @@ -5012,7 +5083,9 @@ class SchemaTest(fixtures.TestBase, AssertsCompiledSQL): ) self.assert_compile( - select(t1).select_from(t1).apply_labels(), + select(t1) + .select_from(t1) + .set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL), "SELECT here.baz.id AS here_baz_id, here.baz.name AS " "here_baz_name, here.baz.meta AS here_baz_meta FROM here.baz", ) @@ -5091,7 +5164,7 @@ class CorrelateTest(fixtures.TestBase, AssertsCompiledSQL): def _assert_where_all_correlated(self, stmt): self.assert_compile( stmt, - "SELECT t1.a, t2.a FROM t1, t2 WHERE t2.a = " + "SELECT t1.a, t2.a AS a_1 FROM t1, t2 WHERE t2.a = " "(SELECT t1.a WHERE t1.a = t2.a)", ) @@ -5118,7 +5191,7 @@ class CorrelateTest(fixtures.TestBase, AssertsCompiledSQL): def _assert_column_all_correlated(self, stmt): self.assert_compile( stmt, - "SELECT t1.a, t2.a, " + "SELECT t1.a, t2.a AS a_1, " "(SELECT t1.a WHERE t1.a = t2.a) AS anon_1 FROM t1, t2", ) @@ -5132,14 +5205,14 @@ class CorrelateTest(fixtures.TestBase, AssertsCompiledSQL): def _assert_from_uncorrelated(self, stmt): self.assert_compile( stmt, - "SELECT t2.a, anon_1.a FROM t2, " + "SELECT t2.a, anon_1.a AS a_1 FROM t2, " "(SELECT t1.a AS a FROM t1, t2 WHERE t1.a = t2.a) AS anon_1", ) def _assert_from_all_uncorrelated(self, stmt): self.assert_compile( stmt, - "SELECT t1.a, t2.a, anon_1.a FROM t1, t2, " + "SELECT t1.a, t2.a AS a_1, anon_1.a AS a_2 FROM t1, t2, " "(SELECT t1.a AS a FROM t1, t2 WHERE t1.a = t2.a) AS anon_1", ) @@ -5335,7 +5408,7 @@ class CorrelateTest(fixtures.TestBase, AssertsCompiledSQL): t1, t2, s1 = self._fixture() self.assert_compile( select(t1, t2, s1.subquery()), - "SELECT t1.a, t2.a, anon_1.a FROM t1, t2, " + "SELECT t1.a, t2.a AS a_1, anon_1.a AS a_2 FROM t1, t2, " "(SELECT t1.a AS a FROM t1, t2 WHERE t1.a = t2.a) AS anon_1", ) @@ -5463,7 +5536,7 @@ class CorrelateTest(fixtures.TestBase, AssertsCompiledSQL): self.assert_compile( s3, - "SELECT t1.x, anon_1.y, anon_1.x FROM t1, " + "SELECT t1.x, anon_1.y, anon_1.x AS x_1 FROM t1, " "(SELECT t2.y AS y, anon_2.x AS x FROM t2, " "(SELECT t1.x AS x FROM t1, t2 WHERE t1.x = t2.y) " "AS anon_2) AS anon_1", @@ -5601,7 +5674,7 @@ class ResultMapTest(fixtures.TestBase): stmt = ( select(t1, t1_alias) .select_from(t1.join(union, t1.c.a == union.c.t1_a)) - .apply_labels() + .set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) ) comp = stmt.compile() eq_( @@ -5770,7 +5843,9 @@ class ResultMapTest(fixtures.TestBase): # create the statement with some duplicate columns. right now # the behavior is that these redundant columns are deduped. - stmt = select(t.c.x, t.c.y, l1, t.c.y, l2, t.c.x, l3) + stmt = select(t.c.x, t.c.y, l1, t.c.y, l2, t.c.x, l3).set_label_style( + LABEL_STYLE_NONE + ) # so the statement has 7 inner columns... eq_(len(list(stmt.selected_columns)), 7) @@ -5794,7 +5869,9 @@ class ResultMapTest(fixtures.TestBase): ).alias() # so when we wrap here we're going to have only 5 columns - wrapped_again = select(*[c for c in wrapped.c]) + wrapped_again = select(*[c for c in wrapped.c]).set_label_style( + LABEL_STYLE_NONE + ) # so the compiler logic that matches up the "wrapper" to the # "select_wraps_for" can't use inner_columns to match because diff --git a/test/sql/test_cte.py b/test/sql/test_cte.py index 4ebfdc7ac..92f3b215f 100644 --- a/test/sql/test_cte.py +++ b/test/sql/test_cte.py @@ -296,10 +296,10 @@ class CTETest(fixtures.TestBase, AssertsCompiledSQL): s2, "WITH RECURSIVE cte(x) AS " "(SELECT :param_1 AS x UNION ALL " - "SELECT cte.x + :x_1 AS anon_1 " - "FROM cte WHERE cte.x < :x_2), " + "SELECT cte.x + :x_2 AS anon_1 " + "FROM cte WHERE cte.x < :x_3), " "bar AS (SELECT cte.x AS x FROM cte) " - "SELECT cte.x, bar.x FROM cte, bar", + "SELECT cte.x, bar.x AS x_1 FROM cte, bar", ) def test_recursive_union_alias_three(self): @@ -321,10 +321,10 @@ class CTETest(fixtures.TestBase, AssertsCompiledSQL): s2, "WITH RECURSIVE cte(x) AS " "(SELECT :param_1 AS x UNION ALL " - "SELECT cte.x + :x_1 AS anon_1 " - "FROM cte WHERE cte.x < :x_2), " + "SELECT cte.x + :x_2 AS anon_1 " + "FROM cte WHERE cte.x < :x_3), " "bar AS (SELECT cs1.x AS x FROM cte AS cs1) " - "SELECT cs1.x, cs2.x FROM cte AS cs1, bar AS cs2", + "SELECT cs1.x, cs2.x AS x_1 FROM cte AS cs1, bar AS cs2", ) def test_recursive_union_no_alias_four(self): @@ -346,10 +346,10 @@ class CTETest(fixtures.TestBase, AssertsCompiledSQL): s2, "WITH RECURSIVE cte(x) AS " "(SELECT :param_1 AS x UNION ALL " - "SELECT cte.x + :x_1 AS anon_1 " - "FROM cte WHERE cte.x < :x_2), " + "SELECT cte.x + :x_2 AS anon_1 " + "FROM cte WHERE cte.x < :x_3), " "bar AS (SELECT cte.x AS x FROM cte) " - "SELECT cte.x, bar.x FROM cte, bar", + "SELECT cte.x, bar.x AS x_1 FROM cte, bar", ) # bar rendered, only includes "inner" cte, @@ -371,9 +371,9 @@ class CTETest(fixtures.TestBase, AssertsCompiledSQL): "WITH RECURSIVE bar AS (SELECT cte.x AS x FROM cte), " "cte(x) AS " "(SELECT :param_1 AS x UNION ALL " - "SELECT cte.x + :x_1 AS anon_1 " - "FROM cte WHERE cte.x < :x_2) " - "SELECT bar.x, cte.x FROM bar, cte", + "SELECT cte.x + :x_2 AS anon_1 " + "FROM cte WHERE cte.x < :x_3) " + "SELECT bar.x, cte.x AS x_1 FROM bar, cte", ) def test_recursive_union_alias_four(self): @@ -397,10 +397,10 @@ class CTETest(fixtures.TestBase, AssertsCompiledSQL): s2, "WITH RECURSIVE cte(x) AS " "(SELECT :param_1 AS x UNION ALL " - "SELECT cte.x + :x_1 AS anon_1 " - "FROM cte WHERE cte.x < :x_2), " + "SELECT cte.x + :x_2 AS anon_1 " + "FROM cte WHERE cte.x < :x_3), " "bar AS (SELECT cte.x AS x FROM cte) " - "SELECT cs2.x, cs1.x FROM cte AS cs2, bar AS cs1", + "SELECT cs2.x, cs1.x AS x_1 FROM cte AS cs2, bar AS cs1", ) # bar rendered, only includes "inner" cte, @@ -422,9 +422,9 @@ class CTETest(fixtures.TestBase, AssertsCompiledSQL): "WITH RECURSIVE bar AS (SELECT cte.x AS x FROM cte), " "cte(x) AS " "(SELECT :param_1 AS x UNION ALL " - "SELECT cte.x + :x_1 AS anon_1 " - "FROM cte WHERE cte.x < :x_2) " - "SELECT cs1.x, cs2.x FROM bar AS cs1, cte AS cs2", + "SELECT cte.x + :x_2 AS anon_1 " + "FROM cte WHERE cte.x < :x_3) " + "SELECT cs1.x, cs2.x AS x_1 FROM bar AS cs1, cte AS cs2", ) def test_conflicting_names(self): @@ -615,7 +615,7 @@ class CTETest(fixtures.TestBase, AssertsCompiledSQL): self.assert_compile( s, 'WITH "CTE" AS (SELECT :param_1 AS id) ' - "SELECT anon_1.id, anon_2.id FROM " + "SELECT anon_1.id, anon_2.id AS id_1 FROM " '(SELECT "CTE".id AS id FROM "CTE") AS anon_1, ' '(SELECT "CTE".id AS id FROM "CTE") AS anon_2', ) @@ -630,7 +630,7 @@ class CTETest(fixtures.TestBase, AssertsCompiledSQL): self.assert_compile( s, "WITH cte1 AS (SELECT :param_1 AS id) " - "SELECT anon_1.id, anon_2.id FROM " + "SELECT anon_1.id, anon_2.id AS id_1 FROM " "(SELECT aa.id AS id FROM cte1 AS aa) AS anon_1, " "(SELECT aa.id AS id FROM cte1 AS aa) AS anon_2", ) @@ -667,7 +667,8 @@ class CTETest(fixtures.TestBase, AssertsCompiledSQL): "JOIN cte1 AS aa ON b.fid = aa.id), " "cte3 AS (SELECT c.id AS id FROM c " "JOIN cte1 AS aa ON c.fid = aa.id) " - "SELECT cte3.id, cte2.id FROM cte2 JOIN cte3 ON cte2.id = cte3.id", + "SELECT cte3.id, cte2.id AS id_1 " + "FROM cte2 JOIN cte3 ON cte2.id = cte3.id", ) def test_named_alias_no_quote(self): @@ -788,13 +789,13 @@ class CTETest(fixtures.TestBase, AssertsCompiledSQL): 'FROM orders WHERE orders."order" = :1), regional_sales_2 AS ' '(SELECT orders."order" = :2 AS anon_1, ' 'anon_2."order" AS "order", ' - 'orders."order" AS "order", ' - 'regional_sales_1."order" AS "order" FROM orders, ' + 'orders."order" AS order_1, ' + 'regional_sales_1."order" AS order_2 FROM orders, ' "regional_sales_1 " "AS anon_2, regional_sales_1 " 'WHERE orders."order" = :3) SELECT regional_sales_2.anon_1, ' - 'regional_sales_2."order", regional_sales_2."order", ' - 'regional_sales_2."order" FROM regional_sales_2', + 'regional_sales_2."order", regional_sales_2.order_1, ' + "regional_sales_2.order_2 FROM regional_sales_2", checkpositional=("x", "y", "z"), dialect=dialect, ) @@ -835,13 +836,13 @@ class CTETest(fixtures.TestBase, AssertsCompiledSQL): "WHERE orders.\"order\" = 'x'), " "regional_sales_2 AS " "(SELECT orders.\"order\" = 'y' AS anon_1, " - 'anon_2."order" AS "order", orders."order" AS "order", ' - 'regional_sales_1."order" AS "order" ' + 'anon_2."order" AS "order", orders."order" AS order_1, ' + 'regional_sales_1."order" AS order_2 ' "FROM orders, regional_sales_1 AS anon_2, regional_sales_1 " "WHERE orders.\"order\" = 'z') " "SELECT regional_sales_2.anon_1, " - 'regional_sales_2."order", regional_sales_2."order", ' - 'regional_sales_2."order" FROM regional_sales_2', + 'regional_sales_2."order", regional_sales_2.order_1, ' + "regional_sales_2.order_2 FROM regional_sales_2", checkpositional=(), dialect=dialect, literal_binds=True, @@ -860,7 +861,7 @@ class CTETest(fixtures.TestBase, AssertsCompiledSQL): s2, 'WITH regional_sales AS (SELECT "order"."order" ' 'AS "order" FROM "order") ' - 'SELECT anon_1."order", anon_2."order" ' + 'SELECT anon_1."order", anon_2."order" AS order_1 ' "FROM regional_sales AS anon_1, " 'regional_sales AS anon_2 WHERE anon_1."order" > anon_2."order"', ) diff --git a/test/sql/test_deprecations.py b/test/sql/test_deprecations.py index 903da2f34..35633a1a3 100644 --- a/test/sql/test_deprecations.py +++ b/test/sql/test_deprecations.py @@ -34,12 +34,14 @@ from sqlalchemy import util from sqlalchemy import VARCHAR from sqlalchemy.engine import default from sqlalchemy.sql import coercions +from sqlalchemy.sql import LABEL_STYLE_TABLENAME_PLUS_COL from sqlalchemy.sql import literal from sqlalchemy.sql import operators from sqlalchemy.sql import quoted_name from sqlalchemy.sql import roles from sqlalchemy.sql import update from sqlalchemy.sql import visitors +from sqlalchemy.sql.selectable import LABEL_STYLE_NONE from sqlalchemy.sql.selectable import SelectStatementGrouping from sqlalchemy.testing import assert_raises from sqlalchemy.testing import assert_raises_message @@ -492,9 +494,9 @@ class SelectableTest(fixtures.TestBase, AssertsCompiledSQL): "ORDER BY table1.col1", ), ( - lambda table1, table2: table1.join(table2).select( - table1.c.col1 == 5 - ), + lambda table1, table2: table1.join(table2) + .select(table1.c.col1 == 5) + .set_label_style(LABEL_STYLE_NONE), "Join", "whereclause", "SELECT table1.col1, table1.col2, table1.col3, table1.colx, " @@ -503,9 +505,9 @@ class SelectableTest(fixtures.TestBase, AssertsCompiledSQL): "WHERE table1.col1 = :col1_1", ), ( - lambda table1, table2: table1.join(table2).select( - whereclause=table1.c.col1 == 5 - ), + lambda table1, table2: table1.join(table2) + .select(whereclause=table1.c.col1 == 5) + .set_label_style(LABEL_STYLE_NONE), "Join", "whereclause", "SELECT table1.col1, table1.col2, table1.col3, table1.colx, " @@ -514,9 +516,9 @@ class SelectableTest(fixtures.TestBase, AssertsCompiledSQL): "WHERE table1.col1 = :col1_1", ), ( - lambda table1, table2: table1.join(table2).select( - order_by=table1.c.col1 - ), + lambda table1, table2: table1.join(table2) + .select(order_by=table1.c.col1) + .set_label_style(LABEL_STYLE_NONE), "Join", "kwargs", "SELECT table1.col1, table1.col2, table1.col3, table1.colx, " @@ -758,8 +760,12 @@ class SelectableTest(fixtures.TestBase, AssertsCompiledSQL): ) .alias("analias") ) - s1 = self.table1.select().apply_labels() - s2 = self.table2.select().apply_labels() + s1 = self.table1.select().set_label_style( + LABEL_STYLE_TABLENAME_PLUS_COL + ) + s2 = self.table2.select().set_label_style( + LABEL_STYLE_TABLENAME_PLUS_COL + ) with self._c_deprecated(): assert u.corresponding_column(s1.c.table1_col2) is u.c.col2 assert u.corresponding_column(s2.c.table2_col2) is u.c.col2 @@ -823,7 +829,9 @@ class SelectableTest(fixtures.TestBase, AssertsCompiledSQL): ) def test_select_labels(self): - a = self.table1.select().apply_labels() + a = self.table1.select().set_label_style( + LABEL_STYLE_TABLENAME_PLUS_COL + ) j = join(a._implicit_subquery, self.table2) criterion = a._implicit_subquery.c.table1_col1 == self.table2.c.col2 @@ -1557,7 +1565,9 @@ class CursorResultTest(fixtures.TablesTest): self.metadata.create_all(testing.db) connection.execute(content.insert().values(type="t1")) - row = connection.execute(content.select().apply_labels()).first() + row = connection.execute( + content.select().set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) + ).first() in_(content.c.type, row._mapping) not_in(bar.c.content_type, row) with testing.expect_deprecated( @@ -1615,7 +1625,9 @@ class CursorResultTest(fixtures.TablesTest): for use_labels in False, True: stmt = users.select() if use_labels: - stmt = stmt.apply_labels() + stmt = stmt.set_label_style( + LABEL_STYLE_TABLENAME_PLUS_COL + ) result = conn.execute( stmt.order_by(users.c.user_id) diff --git a/test/sql/test_external_traversal.py b/test/sql/test_external_traversal.py index a6001ba9d..bc6013791 100644 --- a/test/sql/test_external_traversal.py +++ b/test/sql/test_external_traversal.py @@ -19,6 +19,7 @@ from sqlalchemy import tuple_ from sqlalchemy import union from sqlalchemy.sql import ClauseElement from sqlalchemy.sql import column +from sqlalchemy.sql import LABEL_STYLE_TABLENAME_PLUS_COL from sqlalchemy.sql import operators from sqlalchemy.sql import table from sqlalchemy.sql import util as sql_util @@ -807,7 +808,8 @@ class ClauseTest(fixtures.TestBase, AssertsCompiledSQL): self.assert_compile( s2, "SELECT table1.col1, table1.col2, table1.col3, " - "table2.col1, table2.col2, table2.col3 FROM table1 " + "table2.col1 AS col1_1, table2.col2 AS col2_1, " + "table2.col3 AS col3_1 FROM table1 " "JOIN table2 ON table1.col1 = table2.col2 JOIN table3 " "ON table3.col1 = table1.col1", ) @@ -821,7 +823,8 @@ class ClauseTest(fixtures.TestBase, AssertsCompiledSQL): self.assert_compile( s4, "SELECT table1.col1, table1.col2, table1.col3, " - "table2.col1, table2.col2, table2.col3 FROM table1 " + "table2.col1 AS col1_1, table2.col2 AS col2_1, " + "table2.col3 AS col3_1 FROM table1 " "JOIN table2 ON table1.col1 = table2.col2 JOIN table3 " "ON table3.col1 = table1.col1", ) @@ -833,7 +836,8 @@ class ClauseTest(fixtures.TestBase, AssertsCompiledSQL): self.assert_compile( s6, "SELECT table1.col1, table1.col2, table1.col3, " - "table2.col1, table2.col2, table2.col3 FROM table1 " + "table2.col1 AS col1_1, table2.col2 AS col2_1, " + "table2.col3 AS col3_1 FROM table1 " "JOIN table2 ON table1.col1 = table2.col2 JOIN table3 " "ON table3.col1 = table1.col1", ) @@ -851,7 +855,8 @@ class ClauseTest(fixtures.TestBase, AssertsCompiledSQL): self.assert_compile( s2, "SELECT table1.col1, table1.col2, table1.col3, " - "table2.col1, table2.col2, table2.col3 FROM table1 " + "table2.col1 AS col1_1, table2.col2 AS col2_1, " + "table2.col3 AS col3_1 FROM table1 " "JOIN table2 ON table1.col1 = table2.col2 JOIN table3 " "ON table3.col1 = table1.col1", ) @@ -863,7 +868,8 @@ class ClauseTest(fixtures.TestBase, AssertsCompiledSQL): self.assert_compile( s4, "SELECT table1.col1, table1.col2, table1.col3, " - "table2.col1, table2.col2, table2.col3 FROM table1 " + "table2.col1 AS col1_1, table2.col2 AS col2_1, " + "table2.col3 AS col3_1 FROM table1 " "JOIN table2 ON table1.col1 = table2.col2 JOIN table3 " "ON table3.col1 = table1.col1", ) @@ -875,7 +881,8 @@ class ClauseTest(fixtures.TestBase, AssertsCompiledSQL): self.assert_compile( s6, "SELECT table1.col1, table1.col2, table1.col3, " - "table2.col1, table2.col2, table2.col3 FROM table1 " + "table2.col1 AS col1_1, table2.col2 AS col2_1, " + "table2.col3 AS col3_1 FROM table1 " "JOIN table2 ON table1.col1 = table2.col2 JOIN table3 " "ON table3.col1 = table1.col1", ) @@ -1055,7 +1062,11 @@ class ColumnAdapterTest(fixtures.TestBase, AssertsCompiledSQL): t2a = t2.alias(name="t2a") a1 = sql_util.ColumnAdapter(t1a) - s1 = select(t1a.c.col1, t2a.c.col1).apply_labels().alias() + s1 = ( + select(t1a.c.col1, t2a.c.col1) + .set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) + .alias() + ) a2 = sql_util.ColumnAdapter(s1) a3 = a2.wrap(a1) a4 = a1.wrap(a2) @@ -1103,7 +1114,11 @@ class ColumnAdapterTest(fixtures.TestBase, AssertsCompiledSQL): """ - stmt = select(t1.c.col1, t2.c.col1).apply_labels().subquery() + stmt = ( + select(t1.c.col1, t2.c.col1) + .set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) + .subquery() + ) sa = stmt.alias() stmt2 = select(t2, sa).subquery() @@ -1165,7 +1180,11 @@ class ColumnAdapterTest(fixtures.TestBase, AssertsCompiledSQL): t1a, include_fn=lambda col: "a1" in col._annotations ) - s1 = select(t1a, t2a).apply_labels().alias() + s1 = ( + select(t1a, t2a) + .set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) + .alias() + ) a2 = sql_util.ColumnAdapter( s1, include_fn=lambda col: "a2" in col._annotations ) @@ -1360,7 +1379,7 @@ class ClauseAdapterTest(fixtures.TestBase, AssertsCompiledSQL): "SELECT " "(SELECT table2.col1 FROM table1 " "WHERE table2.col1 = table1.col1) AS anon_1, " - "table1.col1, table2.col1 " + "table1.col1, table2.col1 AS col1_1 " "FROM table1 " "JOIN table2 ON table1.col1 = table2.col1", ) @@ -1369,7 +1388,7 @@ class ClauseAdapterTest(fixtures.TestBase, AssertsCompiledSQL): "SELECT " "(SELECT table2.col1 FROM table1 " "WHERE table2.col1 = table1.col1) AS anon_1, " - "table1.col1, table2.col1 " + "table1.col1, table2.col1 AS col1_1 " "FROM table1 " "JOIN table2 ON table1.col1 = table2.col1", ) @@ -1384,7 +1403,7 @@ class ClauseAdapterTest(fixtures.TestBase, AssertsCompiledSQL): "(SELECT table2.col1 FROM " "table1 JOIN table1 AS t1alias ON table1.col1 = t1alias.col2 " "WHERE table2.col1 = table1.col1) AS anon_1, " - "table1.col1, table2.col1 " + "table1.col1, table2.col1 AS col1_1 " "FROM table1 JOIN table1 AS t1alias ON table1.col1 = t1alias.col2 " "JOIN table2 ON table1.col1 = table2.col1", ) @@ -1398,7 +1417,7 @@ class ClauseAdapterTest(fixtures.TestBase, AssertsCompiledSQL): "(SELECT table2.col1 FROM " "table1 JOIN table1 AS t1alias ON table1.col1 = t1alias.col2 " "WHERE table2.col1 = table1.col1) AS anon_1, " - "table1.col1, table2.col1 " + "table1.col1, table2.col1 AS col1_1 " "FROM table1 JOIN table1 AS t1alias ON table1.col1 = t1alias.col2 " "JOIN table2 ON table1.col1 = table2.col1", ) @@ -1484,7 +1503,8 @@ class ClauseAdapterTest(fixtures.TestBase, AssertsCompiledSQL): ) ), "SELECT t1alias.col1, t1alias.col2, t1alias.col3, " - "table2.col1, table2.col2, table2.col3 " + "table2.col1 AS col1_1, table2.col2 AS col2_1, " + "table2.col3 AS col3_1 " "FROM table1 AS t1alias, table2 WHERE t1alias.col1 = " "(SELECT * FROM table2 WHERE t1alias.col1 = table2.col2)", ) @@ -1504,7 +1524,8 @@ class ClauseAdapterTest(fixtures.TestBase, AssertsCompiledSQL): ) ), "SELECT t1alias.col1, t1alias.col2, t1alias.col3, " - "table2.col1, table2.col2, table2.col3 " + "table2.col1 AS col1_1, table2.col2 AS col2_1, " + "table2.col3 AS col3_1 " "FROM table1 AS t1alias, table2 " "WHERE t1alias.col1 = " "(SELECT * FROM table1 AS t1alias " @@ -1612,7 +1633,8 @@ class ClauseAdapterTest(fixtures.TestBase, AssertsCompiledSQL): ) ), "SELECT t1alias.col1, t1alias.col2, t1alias.col3, " - "t2alias.col1, t2alias.col2, t2alias.col3 " + "t2alias.col1 AS col1_1, t2alias.col2 AS col2_1, " + "t2alias.col3 AS col3_1 " "FROM table1 AS t1alias, table2 AS t2alias " "WHERE t1alias.col1 = " "(SELECT * FROM table2 AS t2alias " @@ -1720,7 +1742,11 @@ class ClauseAdapterTest(fixtures.TestBase, AssertsCompiledSQL): ) j1 = a.outerjoin(b) - j2 = select(j1).apply_labels().subquery() + j2 = ( + select(j1) + .set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) + .subquery() + ) j3 = c.join(j2, j2.c.b_id == c.c.bid) @@ -1732,7 +1758,11 @@ class ClauseAdapterTest(fixtures.TestBase, AssertsCompiledSQL): "JOIN b ON a.id = b.aid) AS anon_1 ON anon_1.b_id = c.bid " "LEFT OUTER JOIN d ON anon_1.a_id = d.aid", ) - j5 = j3.select().apply_labels().subquery("foo") + j5 = ( + j3.select() + .set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) + .subquery("foo") + ) j6 = sql_util.ClauseAdapter(j5).copy_and_process([j4])[0] # this statement takes c join(a join b), wraps it inside an @@ -1786,8 +1816,8 @@ class ClauseAdapterTest(fixtures.TestBase, AssertsCompiledSQL): self.assert_compile( sql_util.ClauseAdapter(s2).traverse(j).select(), "SELECT anon_1.col1, anon_1.col2, " - "anon_1.col3, table2.col1, table2.col2, " - "table2.col3 FROM (SELECT foo.col1 AS " + "anon_1.col3, table2.col1 AS col1_1, table2.col2 AS col2_1, " + "table2.col3 AS col3_1 FROM (SELECT foo.col1 AS " "col1, foo.col2 AS col2, foo.col3 AS col3 " "FROM (SELECT table1.col1 AS col1, " "table1.col2 AS col2, table1.col3 AS col3 " @@ -1819,7 +1849,8 @@ class ClauseAdapterTest(fixtures.TestBase, AssertsCompiledSQL): self.assert_compile( sql_util.ClauseAdapter(s2).traverse(j).select(), "SELECT anon_1.col1, anon_1.col2, " - "anon_1.col3, bar.col1, bar.col2, bar.col3 " + "anon_1.col3, bar.col1 AS col1_1, bar.col2 AS col2_1, " + "bar.col3 AS col3_1 " "FROM (SELECT foo.col1 AS col1, foo.col2 " "AS col2, foo.col3 AS col3 FROM (SELECT " "table1.col1 AS col1, table1.col2 AS col2, " @@ -1866,8 +1897,8 @@ class ClauseAdapterTest(fixtures.TestBase, AssertsCompiledSQL): ) u = union( - a.join(b).select().apply_labels(), - a.join(d).select().apply_labels(), + a.join(b).select().set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL), + a.join(d).select().set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL), ).alias() self.assert_compile( diff --git a/test/sql/test_functions.py b/test/sql/test_functions.py index 50f50f0f0..19562dade 100644 --- a/test/sql/test_functions.py +++ b/test/sql/test_functions.py @@ -28,6 +28,7 @@ from sqlalchemy.dialects import postgresql from sqlalchemy.dialects import sqlite from sqlalchemy.sql import column from sqlalchemy.sql import functions +from sqlalchemy.sql import LABEL_STYLE_TABLENAME_PLUS_COL from sqlalchemy.sql import quoted_name from sqlalchemy.sql import table from sqlalchemy.sql.compiler import BIND_TEMPLATES @@ -95,7 +96,8 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL): def test_use_labels(self): self.assert_compile( - select(func.foo()).apply_labels(), "SELECT foo() AS foo_1" + select(func.foo()).set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL), + "SELECT foo() AS foo_1", ) def test_use_labels_function_element(self): @@ -109,7 +111,7 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL): return "max(%s)" % compiler.process(element.clauses, **kw) self.assert_compile( - select(max_(5, 6)).apply_labels(), + select(max_(5, 6)).set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL), "SELECT max(:max_2, :max_3) AS max_1", ) diff --git a/test/sql/test_labels.py b/test/sql/test_labels.py index e3fdff806..3aa0a8523 100644 --- a/test/sql/test_labels.py +++ b/test/sql/test_labels.py @@ -13,11 +13,13 @@ from sqlalchemy.engine import default from sqlalchemy.ext.compiler import compiles from sqlalchemy.sql import coercions from sqlalchemy.sql import column +from sqlalchemy.sql import LABEL_STYLE_TABLENAME_PLUS_COL from sqlalchemy.sql import roles from sqlalchemy.sql import table from sqlalchemy.sql.elements import _truncated_label from sqlalchemy.sql.elements import ColumnElement from sqlalchemy.sql.elements import WrapsColumnExpression +from sqlalchemy.sql.selectable import LABEL_STYLE_NONE from sqlalchemy.testing import assert_raises from sqlalchemy.testing import assert_raises_message from sqlalchemy.testing import AssertsCompiledSQL @@ -200,7 +202,8 @@ class MaxIdentTest(fixtures.TestBase, AssertsCompiledSQL): self.assert_compile( select(table1, ta) .select_from(table1.join(ta, on)) - .where(ta.c.this_is_the_data_column == "data3"), + .where(ta.c.this_is_the_data_column == "data3") + .set_label_style(LABEL_STYLE_NONE), "SELECT " "some_large_named_table.this_is_the_primarykey_column, " "some_large_named_table.this_is_the_data_column, " @@ -261,7 +264,7 @@ class MaxIdentTest(fixtures.TestBase, AssertsCompiledSQL): table1 = self.table1 s = ( table1.select() - .apply_labels() + .set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) .order_by(table1.c.this_is_the_primarykey_column) ) @@ -275,7 +278,7 @@ class MaxIdentTest(fixtures.TestBase, AssertsCompiledSQL): # select query s = ( table1.select() - .apply_labels() + .set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) .order_by(table1.c.this_is_the_primarykey_column) .limit(2) ) @@ -312,7 +315,7 @@ class MaxIdentTest(fixtures.TestBase, AssertsCompiledSQL): .where(table1.c.this_is_the_primarykey_column == 4) .alias() ) - s = select(q).apply_labels() + s = select(q).set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) self.assert_compile( s, @@ -579,7 +582,7 @@ class LabelLengthTest(fixtures.TestBase, AssertsCompiledSQL): .where(table1.c.this_is_the_primarykey_column == 4) .alias() ) - x = select(q).apply_labels() + x = select(q).set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) compile_dialect = default.DefaultDialect(label_length=10) self.assert_compile( @@ -610,7 +613,7 @@ class LabelLengthTest(fixtures.TestBase, AssertsCompiledSQL): .where(table1.c.this_is_the_primarykey_column == 4) .alias() ) - x = select(q).apply_labels() + x = select(q).set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) compile_dialect = default.DefaultDialect(label_length=4) self.assert_compile( @@ -640,7 +643,7 @@ class LabelLengthTest(fixtures.TestBase, AssertsCompiledSQL): q = ( table1.select() .where(table1.c.this_is_the_primarykey_column == 4) - .apply_labels() + .set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) .alias("foo") ) @@ -714,7 +717,9 @@ class LabelLengthTest(fixtures.TestBase, AssertsCompiledSQL): ) self.assert_compile( - select(other_table, anon).select_from(j1).apply_labels(), + select(other_table, anon) + .select_from(j1) + .set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL), "SELECT " "other_thirty_characters_table_.id " "AS other_thirty_characters__1, " @@ -756,7 +761,7 @@ class LabelLengthTest(fixtures.TestBase, AssertsCompiledSQL): ) # column still there, but short label - s = select(a1).apply_labels() + s = select(a1).set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) self.assert_compile( s, "SELECT asdf.abcde AS _1 FROM a AS asdf", dialect=dialect ) @@ -772,7 +777,7 @@ class LabelLengthTest(fixtures.TestBase, AssertsCompiledSQL): "tablename", column("columnname_one"), column("columnn_1") ) - stmt = select(table1).apply_labels() + stmt = select(table1).set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) dialect = default.DefaultDialect(label_length=23) self.assert_compile( @@ -817,7 +822,7 @@ class ColExprLabelTest(fixtures.TestBase, AssertsCompiledSQL): return SomeColThing - def test_column_auto_label_dupes(self): + def test_column_auto_label_dupes_label_style_none(self): expr = self._fixture() table1 = self.table1 @@ -827,12 +832,29 @@ class ColExprLabelTest(fixtures.TestBase, AssertsCompiledSQL): table1.c.name, expr(table1.c.name), expr(table1.c.name), - ), + ).set_label_style(LABEL_STYLE_NONE), "SELECT some_table.name, some_table.name, " "SOME_COL_THING(some_table.name) AS name, " "SOME_COL_THING(some_table.name) AS name FROM some_table", ) + def test_column_auto_label_dupes_label_style_disambiguate(self): + expr = self._fixture() + table1 = self.table1 + + self.assert_compile( + select( + table1.c.name, + table1.c.name, + expr(table1.c.name), + expr(table1.c.name), + ), + "SELECT some_table.name, some_table.name AS name__1, " + "SOME_COL_THING(some_table.name) AS some_table_name__1, " + "SOME_COL_THING(some_table.name) AS some_table_name__2 " + "FROM some_table", + ) + def test_anon_expression_fallback(self): expr = self._fixture() table1 = self.table1 @@ -851,7 +873,7 @@ class ColExprLabelTest(fixtures.TestBase, AssertsCompiledSQL): self.assert_compile( select( table1.c.name + "foo", expr(table1.c.name + "foo") - ).apply_labels(), + ).set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL), "SELECT some_table.name || :name_1 AS anon_1, " "SOME_COL_THING(some_table.name || :name_2) AS anon_2 " "FROM some_table", @@ -871,7 +893,7 @@ class ColExprLabelTest(fixtures.TestBase, AssertsCompiledSQL): "some_table.name AS bar, some_table.value FROM some_table", ) - def test_cast_auto_label(self): + def test_cast_auto_label_label_style_none(self): table1 = self.table1 self.assert_compile( @@ -879,13 +901,27 @@ class ColExprLabelTest(fixtures.TestBase, AssertsCompiledSQL): cast(table1.c.name, Integer), cast(table1.c.name, String), table1.c.name, - ), + ).set_label_style(LABEL_STYLE_NONE), "SELECT CAST(some_table.name AS INTEGER) AS name, " "CAST(some_table.name AS VARCHAR) AS name, " "some_table.name FROM some_table", ) - def test_type_coerce_auto_label(self): + def test_cast_auto_label_label_style_disabmiguate(self): + table1 = self.table1 + + self.assert_compile( + select( + cast(table1.c.name, Integer), + cast(table1.c.name, String), + table1.c.name, + ), + "SELECT CAST(some_table.name AS INTEGER) AS name, " + "CAST(some_table.name AS VARCHAR) AS some_table_name__1, " + "some_table.name AS name_1 FROM some_table", + ) + + def test_type_coerce_auto_label_label_style_none(self): table1 = self.table1 self.assert_compile( @@ -893,13 +929,28 @@ class ColExprLabelTest(fixtures.TestBase, AssertsCompiledSQL): type_coerce(table1.c.name, Integer), type_coerce(table1.c.name, String), table1.c.name, - ), + ).set_label_style(LABEL_STYLE_NONE), # ideally type_coerce wouldn't label at all... "SELECT some_table.name AS name, " "some_table.name AS name, " "some_table.name FROM some_table", ) + def test_type_coerce_auto_label_label_style_disambiguate(self): + table1 = self.table1 + + self.assert_compile( + select( + type_coerce(table1.c.name, Integer), + type_coerce(table1.c.name, String), + table1.c.name, + ), + # ideally type_coerce wouldn't label at all... + "SELECT some_table.name AS name, " + "some_table.name AS some_table_name__1, " + "some_table.name AS name_1 FROM some_table", + ) + def test_boolean_auto_label(self): col = column("value", Boolean) @@ -919,7 +970,7 @@ class ColExprLabelTest(fixtures.TestBase, AssertsCompiledSQL): expr(table1.c.name.label("foo")), table1.c.name.label("bar"), table1.c.value, - ).apply_labels(), + ).set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL), # the expr around label is treated the same way as plain column # with label "SELECT SOME_COL_THING(some_table.name) AS foo, " @@ -937,7 +988,7 @@ class ColExprLabelTest(fixtures.TestBase, AssertsCompiledSQL): table1.c.name, expr(table1.c.name), expr(table1.c.name), - ).apply_labels(), + ).set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL), "SELECT some_table.name AS some_table_name, " "some_table.name AS some_table_name__1, " "SOME_COL_THING(some_table.name) AS some_table_name_1, " @@ -950,7 +1001,9 @@ class ColExprLabelTest(fixtures.TestBase, AssertsCompiledSQL): table1 = self.table1 self.assert_compile( - select(table1.c.name, expr(table1.c.value)).apply_labels(), + select(table1.c.name, expr(table1.c.value)).set_label_style( + LABEL_STYLE_TABLENAME_PLUS_COL + ), "SELECT some_table.name AS some_table_name, " "SOME_COL_THING(some_table.value) " "AS some_table_value FROM some_table", diff --git a/test/sql/test_operators.py b/test/sql/test_operators.py index 270e79ba1..a19eb20bc 100644 --- a/test/sql/test_operators.py +++ b/test/sql/test_operators.py @@ -34,6 +34,7 @@ from sqlalchemy.sql import column from sqlalchemy.sql import compiler from sqlalchemy.sql import desc from sqlalchemy.sql import false +from sqlalchemy.sql import LABEL_STYLE_TABLENAME_PLUS_COL from sqlalchemy.sql import literal from sqlalchemy.sql import null from sqlalchemy.sql import operators @@ -1234,15 +1235,21 @@ class ConjunctionTest(fixtures.TestBase, testing.AssertsCompiledSQL): def test_constant_render_distinct_use_labels(self): self.assert_compile( - select(null(), null()).apply_labels(), + select(null(), null()).set_label_style( + LABEL_STYLE_TABLENAME_PLUS_COL + ), "SELECT NULL AS anon_1, NULL AS anon__1", ) self.assert_compile( - select(true(), true()).apply_labels(), + select(true(), true()).set_label_style( + LABEL_STYLE_TABLENAME_PLUS_COL + ), "SELECT true AS anon_1, true AS anon__1", ) self.assert_compile( - select(false(), false()).apply_labels(), + select(false(), false()).set_label_style( + LABEL_STYLE_TABLENAME_PLUS_COL + ), "SELECT false AS anon_1, false AS anon__1", ) diff --git a/test/sql/test_query.py b/test/sql/test_query.py index 903a6f23e..913b7f4d1 100644 --- a/test/sql/test_query.py +++ b/test/sql/test_query.py @@ -26,6 +26,8 @@ from sqlalchemy import union from sqlalchemy import union_all from sqlalchemy import VARCHAR from sqlalchemy.engine import default +from sqlalchemy.sql import LABEL_STYLE_TABLENAME_PLUS_COL +from sqlalchemy.sql.selectable import LABEL_STYLE_NONE from sqlalchemy.testing import assert_raises_message from sqlalchemy.testing import eq_ from sqlalchemy.testing import fixtures @@ -383,22 +385,28 @@ class QueryTest(fixtures.TablesTest): eq_(got, wanted) for labels in False, True: + label_style = ( + LABEL_STYLE_NONE + if labels is False + else LABEL_STYLE_TABLENAME_PLUS_COL + ) def go(stmt): if labels: - stmt = stmt.apply_labels() + stmt = stmt.set_label_style(label_style) return stmt a_eq( - users.select(order_by=[users.c.user_id], use_labels=labels), + users.select(order_by=[users.c.user_id]).set_label_style( + label_style + ), [(1, "c"), (2, "b"), (3, "a")], ) a_eq( users.select( order_by=[users.c.user_name, users.c.user_id], - use_labels=labels, - ), + ).set_label_style(label_style), [(3, "a"), (2, "b"), (1, "c")], ) @@ -423,9 +431,8 @@ class QueryTest(fixtures.TablesTest): a_eq( users.select( distinct=True, - use_labels=labels, order_by=[users.c.user_id], - ), + ).set_label_style(label_style), [(1, "c"), (2, "b"), (3, "a")], ) @@ -452,9 +459,8 @@ class QueryTest(fixtures.TablesTest): a_eq( users.select( distinct=True, - use_labels=labels, order_by=[desc(users.c.user_id)], - ), + ).set_label_style(label_style), [(3, "a"), (2, "b"), (1, "c")], ) @@ -485,67 +491,64 @@ class QueryTest(fixtures.TablesTest): eq_(got, wanted) for labels in False, True: + label_style = ( + LABEL_STYLE_NONE + if labels is False + else LABEL_STYLE_TABLENAME_PLUS_COL + ) a_eq( users.select( order_by=[users.c.user_name.nulls_first()], - use_labels=labels, - ), + ).set_label_style(label_style), [(1, None), (3, "a"), (2, "b")], ) a_eq( users.select( order_by=[users.c.user_name.nulls_last()], - use_labels=labels, - ), + ).set_label_style(label_style), [(3, "a"), (2, "b"), (1, None)], ) a_eq( users.select( order_by=[asc(users.c.user_name).nulls_first()], - use_labels=labels, - ), + ).set_label_style(label_style), [(1, None), (3, "a"), (2, "b")], ) a_eq( users.select( order_by=[asc(users.c.user_name).nulls_last()], - use_labels=labels, - ), + ).set_label_style(label_style), [(3, "a"), (2, "b"), (1, None)], ) a_eq( users.select( order_by=[users.c.user_name.desc().nulls_first()], - use_labels=labels, - ), + ).set_label_style(label_style), [(1, None), (2, "b"), (3, "a")], ) a_eq( users.select( order_by=[users.c.user_name.desc().nulls_last()], - use_labels=labels, - ), + ).set_label_style(label_style), [(2, "b"), (3, "a"), (1, None)], ) a_eq( users.select( order_by=[desc(users.c.user_name).nulls_first()], - use_labels=labels, - ), + ).set_label_style(label_style), [(1, None), (2, "b"), (3, "a")], ) a_eq( users.select( order_by=[desc(users.c.user_name).nulls_last()], - use_labels=labels, - ), + ).set_label_style(label_style), [(2, "b"), (3, "a"), (1, None)], ) @@ -555,16 +558,14 @@ class QueryTest(fixtures.TablesTest): users.c.user_name.nulls_first(), users.c.user_id, ], - use_labels=labels, - ), + ).set_label_style(label_style), [(1, None), (3, "a"), (2, "b")], ) a_eq( users.select( order_by=[users.c.user_name.nulls_last(), users.c.user_id], - use_labels=labels, - ), + ).set_label_style(label_style), [(3, "a"), (2, "b"), (1, None)], ) diff --git a/test/sql/test_quote.py b/test/sql/test_quote.py index c743918c8..bf885a7f5 100644 --- a/test/sql/test_quote.py +++ b/test/sql/test_quote.py @@ -16,6 +16,7 @@ from sqlalchemy import testing from sqlalchemy import util from sqlalchemy.engine import default from sqlalchemy.sql import compiler +from sqlalchemy.sql import LABEL_STYLE_TABLENAME_PLUS_COL from sqlalchemy.sql.elements import _anonymous_label from sqlalchemy.sql.elements import quoted_name from sqlalchemy.testing import AssertsCompiledSQL @@ -176,11 +177,15 @@ class QuoteExecTest(fixtures.TablesTest): table1.c.MixedCase, table1.c.a123, ] - result = connection.execute(select(columns).apply_labels()).fetchall() + result = connection.execute( + select(columns).set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) + ).fetchall() assert result == [(1, 2, 3, 4), (2, 2, 3, 4), (4, 3, 2, 1)] columns = [table2.c.d123, table2.c.u123, table2.c.MixedCase] - result = connection.execute(select(columns).apply_labels()).all() + result = connection.execute( + select(columns).set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) + ).all() assert result == [(1, 2, 3), (2, 2, 3), (4, 3, 2)] @@ -556,7 +561,7 @@ class QuoteTest(fixtures.TestBase, AssertsCompiledSQL): self.assert_compile( t2.join(t1).select(), "SELECT " - "t2.col1, t2.t1col1, t1.col1 " + "t2.col1, t2.t1col1, t1.col1 AS col1_1 " "FROM " "t2 " "JOIN " @@ -578,7 +583,7 @@ class QuoteTest(fixtures.TestBase, AssertsCompiledSQL): self.assert_compile( t2.join(t1).select(), "SELECT " - '"t2"."col1", "t2"."t1col1", "t1"."col1" ' + '"t2"."col1", "t2"."t1col1", "t1"."col1" AS col1_1 ' "FROM " '"t2" ' "JOIN " @@ -597,7 +602,7 @@ class QuoteTest(fixtures.TestBase, AssertsCompiledSQL): self.assert_compile( t2.join(t1).select(), "SELECT " - '"T2"."Col1", "T2"."T1Col1", "T1"."Col1" ' + '"T2"."Col1", "T2"."T1Col1", "T1"."Col1" AS "Col1_1" ' "FROM " '"T2" ' "JOIN " @@ -619,7 +624,7 @@ class QuoteTest(fixtures.TestBase, AssertsCompiledSQL): self.assert_compile( t2.join(t1).select(), "SELECT " - "T2.Col1, T2.T1Col1, T1.Col1 " + 'T2.Col1, T2.T1Col1, T1.Col1 AS "Col1_1" ' "FROM " "T2 " "JOIN " @@ -756,7 +761,7 @@ class QuoteTest(fixtures.TestBase, AssertsCompiledSQL): t1 = Table("T1", metadata, Column("Col1", Integer), schema="Foo") self.assert_compile( - t1.select().apply_labels(), + t1.select().set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL), "SELECT " '"Foo"."T1"."Col1" AS "Foo_T1_Col1" ' "FROM " @@ -778,7 +783,7 @@ class QuoteTest(fixtures.TestBase, AssertsCompiledSQL): # TODO: is this what we really want here ? # what if table/schema *are* quoted? self.assert_compile( - t1.select().apply_labels(), + t1.select().set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL), "SELECT " "Foo.T1.Col1 AS Foo_T1_Col1 " "FROM " "Foo.T1", ) @@ -804,13 +809,13 @@ class QuoteTest(fixtures.TestBase, AssertsCompiledSQL): t = Table("t", m, Column("x", Integer, quote=True)) self.assert_compile( - select(t.alias()).apply_labels(), + select(t.alias()).set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL), 'SELECT t_1."x" AS "t_1_x" FROM t AS t_1', ) t2 = Table("t2", m, Column("x", Integer), quote=True) self.assert_compile( - select(t2.c.x).apply_labels(), + select(t2.c.x).set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL), 'SELECT "t2".x AS "t2_x" FROM "t2"', ) diff --git a/test/sql/test_resultset.py b/test/sql/test_resultset.py index 23aa703d3..e99ce881c 100644 --- a/test/sql/test_resultset.py +++ b/test/sql/test_resultset.py @@ -31,6 +31,8 @@ from sqlalchemy.engine import Row from sqlalchemy.ext.compiler import compiles from sqlalchemy.sql import ColumnElement from sqlalchemy.sql import expression +from sqlalchemy.sql import LABEL_STYLE_TABLENAME_PLUS_COL +from sqlalchemy.sql.selectable import LABEL_STYLE_NONE from sqlalchemy.sql.selectable import TextualSelect from sqlalchemy.sql.sqltypes import NULLTYPE from sqlalchemy.sql.util import ClauseAdapter @@ -209,7 +211,9 @@ class CursorResultTest(fixtures.TablesTest): self.metadata.create_all(connection) connection.execute(content.insert().values(type="t1")) - row = connection.execute(content.select(use_labels=True)).first() + row = connection.execute( + content.select().set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) + ).first() in_(content.c.type, row._mapping) not_in(bar.c.content_type, row._mapping) @@ -238,8 +242,12 @@ class CursorResultTest(fixtures.TablesTest): for pickle in False, True: for use_labels in False, True: result = connection.execute( - users.select(use_labels=use_labels).order_by( - users.c.user_id + users.select() + .order_by(users.c.user_id) + .set_label_style( + LABEL_STYLE_TABLENAME_PLUS_COL + if use_labels + else LABEL_STYLE_NONE ) ).fetchall() @@ -755,7 +763,11 @@ class CursorResultTest(fixtures.TablesTest): addresses = self.tables.addresses connection.execute(users.insert(), dict(user_id=1, user_name="john")) - result = connection.execute(users.outerjoin(addresses).select()) + result = connection.execute( + users.outerjoin(addresses) + .select() + .set_label_style(LABEL_STYLE_NONE) + ) r = result.first() assert_raises_message( @@ -801,9 +813,9 @@ class CursorResultTest(fixtures.TablesTest): ua = users.alias() u2 = users.alias() result = connection.execute( - select(users.c.user_id, ua.c.user_id).select_from( - users.join(ua, true()) - ) + select(users.c.user_id, ua.c.user_id) + .select_from(users.join(ua, true())) + .set_label_style(LABEL_STYLE_NONE) ) row = result.first() @@ -976,7 +988,9 @@ class CursorResultTest(fixtures.TablesTest): users.select().alias("foo"), users.select().alias(users.name), ): - row = connection.execute(s.select(use_labels=True)).first() + row = connection.execute( + s.select().set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) + ).first() eq_(row._mapping[s.c.user_id], 7) eq_(row._mapping[s.c.user_name], "ed") @@ -1499,7 +1513,9 @@ class KeyTargetingTest(fixtures.TablesTest): def test_keyed_accessor_single_labeled(self, connection): keyed1 = self.tables.keyed1 - row = connection.execute(keyed1.select().apply_labels()).first() + row = connection.execute( + keyed1.select().set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) + ).first() eq_(row.keyed1_b, "a1") eq_(row.keyed1_q, "c1") @@ -1567,7 +1583,9 @@ class KeyTargetingTest(fixtures.TablesTest): keyed2 = self.tables.keyed2 row = connection.execute( - select(keyed1, keyed2).select_from(keyed1.join(keyed2, true())) + select(keyed1, keyed2) + .select_from(keyed1.join(keyed2, true())) + .set_label_style(LABEL_STYLE_NONE) ).first() # column access is unambiguous @@ -1604,7 +1622,7 @@ class KeyTargetingTest(fixtures.TablesTest): row = connection.execute( select(keyed1, keyed2) .select_from(keyed1.join(keyed2, true())) - .apply_labels() + .set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) ).first() # column access is unambiguous @@ -1631,7 +1649,9 @@ class KeyTargetingTest(fixtures.TablesTest): keyed3 = self.tables.keyed3 row = connection.execute( - select(keyed1, keyed3).select_from(keyed1.join(keyed3, true())) + select(keyed1, keyed3) + .select_from(keyed1.join(keyed3, true())) + .set_label_style(LABEL_STYLE_NONE) ).first() eq_(row.q, "c1") @@ -1657,7 +1677,7 @@ class KeyTargetingTest(fixtures.TablesTest): row = connection.execute( select(keyed1, keyed2) .select_from(keyed1.join(keyed2, true())) - .apply_labels() + .set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) ).first() eq_(row.keyed1_b, "a1") eq_(row.keyed1_a, "a1") @@ -1696,7 +1716,7 @@ class KeyTargetingTest(fixtures.TablesTest): keyed3.c.d, ) .select_from(keyed2.join(keyed3, true())) - .apply_labels() + .set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) ) result = connection.execute(stmt) @@ -1793,7 +1813,7 @@ class KeyTargetingTest(fixtures.TablesTest): keyed1 = self.tables.keyed1 stmt = ( select(keyed1.c.b, keyed1.c.q.label("foo")) - .apply_labels() + .set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) .subquery() ) @@ -1813,7 +1833,7 @@ class KeyTargetingTest(fixtures.TablesTest): def _adapt_result_columns_fixture_four(self): keyed1 = self.tables.keyed1 - stmt1 = select(keyed1).apply_labels() + stmt1 = select(keyed1).set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) a1 = keyed1.alias() stmt2 = ClauseAdapter(a1).traverse(stmt1) diff --git a/test/sql/test_selectable.py b/test/sql/test_selectable.py index c75e8886d..ce33ed10e 100644 --- a/test/sql/test_selectable.py +++ b/test/sql/test_selectable.py @@ -32,10 +32,12 @@ from sqlalchemy.sql import annotation from sqlalchemy.sql import base from sqlalchemy.sql import column from sqlalchemy.sql import elements +from sqlalchemy.sql import LABEL_STYLE_TABLENAME_PLUS_COL from sqlalchemy.sql import operators from sqlalchemy.sql import table from sqlalchemy.sql import util as sql_util from sqlalchemy.sql import visitors +from sqlalchemy.sql.selectable import LABEL_STYLE_NONE from sqlalchemy.testing import assert_raises from sqlalchemy.testing import assert_raises_message from sqlalchemy.testing import AssertsCompiledSQL @@ -265,7 +267,7 @@ class SelectableTest( assert sel2.corresponding_column(keyed.c.z) is sel2.c.z def test_keyed_label_gen(self): - s = select(keyed).apply_labels() + s = select(keyed).set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) assert ( s.selected_columns.corresponding_column(keyed.c.colx) @@ -371,8 +373,12 @@ class SelectableTest( def test_distance_on_aliases(self): a1 = table1.alias("a1") for s in ( - select(a1, table1).apply_labels().subquery(), - select(table1, a1).apply_labels().subquery(), + select(a1, table1) + .set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) + .subquery(), + select(table1, a1) + .set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) + .subquery(), ): assert s.corresponding_column(table1.c.col1) is s.c.table1_col1 assert s.corresponding_column(a1.c.col1) is s.c.a1_col1 @@ -397,7 +403,11 @@ class SelectableTest( # test alias of the join - j2 = jjj.select().apply_labels().subquery("foo") + j2 = ( + jjj.select() + .set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) + .subquery("foo") + ) assert j2.corresponding_column(table1.c.col1) is j2.c.table1_col1 def test_clone_append_column(self): @@ -505,7 +515,11 @@ class SelectableTest( self.assert_compile(group, "b / (y * w)") def test_subquery_on_table(self): - sel = select(table1, table2).apply_labels().subquery() + sel = ( + select(table1, table2) + .set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) + .subquery() + ) assert sel.corresponding_column(table1.c.col1) is sel.c.table1_col1 assert ( @@ -609,8 +623,8 @@ class SelectableTest( table2.c.coly, ) ) - s1 = table1.select(use_labels=True) - s2 = table2.select(use_labels=True) + s1 = table1.select().set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) + s2 = table2.select().set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) assert ( u.corresponding_column(s1.selected_columns.table1_col2) @@ -705,8 +719,16 @@ class SelectableTest( ) .alias("analias") ) - s1 = table1.select(use_labels=True).subquery() - s2 = table2.select(use_labels=True).subquery() + s1 = ( + table1.select() + .set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) + .subquery() + ) + s2 = ( + table2.select() + .set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) + .subquery() + ) assert u.corresponding_column(s1.c.table1_col2) is u.c.col2 assert u.corresponding_column(s2.c.table2_col2) is u.c.col2 assert u.corresponding_column(s2.c.table2_coly) is u.c.coly @@ -837,8 +859,12 @@ class SelectableTest( d = table("d", column("id"), column("aid")) u1 = union( - a.join(b, a.c.id == b.c.aid).select().apply_labels(), - a.join(d, a.c.id == d.c.aid).select().apply_labels(), + a.join(b, a.c.id == b.c.aid) + .select() + .set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL), + a.join(d, a.c.id == d.c.aid) + .select() + .set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL), ).alias() eq_(u1.c.keys(), ["a_id", "b_id", "b_aid"]) @@ -927,8 +953,16 @@ class SelectableTest( .alias("analias") ) s = select(u).subquery() - s1 = table1.select(use_labels=True).subquery() - s2 = table2.select(use_labels=True).subquery() + s1 = ( + table1.select() + .set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) + .subquery() + ) + s2 = ( + table2.select() + .set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) + .subquery() + ) assert s.corresponding_column(s1.c.table1_col2) is s.c.col2 assert s.corresponding_column(s2.c.table2_col2) is s.c.col2 @@ -961,7 +995,7 @@ class SelectableTest( def test_join(self): a = join(table1, table2) - print(str(a.select().apply_labels())) + print(str(a.select().set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL))) b = table2.alias("b") j = join(a, b) print(str(j)) @@ -976,7 +1010,11 @@ class SelectableTest( self.assert_(criterion.compare(j.onclause)) def test_subquery_labels_join(self): - a = table1.select().apply_labels().subquery() + a = ( + table1.select() + .set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) + .subquery() + ) j = join(a, table2) criterion = a.c.table1_col1 == table2.c.col2 @@ -1020,7 +1058,11 @@ class SelectableTest( eq_(s.corresponding_column(l1), s.c.foo) def test_select_alias_labels(self): - a = table2.select(use_labels=True).alias("a") + a = ( + table2.select() + .set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) + .alias("a") + ) j = join(a, table1) criterion = table1.c.col1 == a.c.table2_col2 @@ -1046,7 +1088,11 @@ class SelectableTest( t2 = Table("t2", m, Column("id", Integer, ForeignKey("t1.id"))) t3 = Table("t3", m2, Column("id", Integer, ForeignKey("t1.id2"))) - s = select(t2, t3).apply_labels().subquery() + s = ( + select(t2, t3) + .set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) + .subquery() + ) assert_raises(exc.NoReferencedTableError, s.join, t1) @@ -1076,7 +1122,7 @@ class SelectableTest( # style to the select, eliminating the self-referential call unless # the select already had labeling applied - s = select(t).apply_labels() + s = select(t).set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) with testing.expect_deprecated("The SelectBase.c"): s.where.non_generative(s, s.c.t_x > 5) @@ -1280,7 +1326,7 @@ class RefreshForNewColTest(fixtures.TestBase): def test_select_samename_init(self): a = table("a", column("x")) b = table("b", column("y")) - s = select(a, b).apply_labels() + s = select(a, b).set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) s.selected_columns q = column("x") b.append_column(q) @@ -1290,7 +1336,11 @@ class RefreshForNewColTest(fixtures.TestBase): def test_alias_alias_samename_init(self): a = table("a", column("x")) b = table("b", column("y")) - s1 = select(a, b).apply_labels().alias() + s1 = ( + select(a, b) + .set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) + .alias() + ) s2 = s1.alias() s1.c @@ -1309,7 +1359,11 @@ class RefreshForNewColTest(fixtures.TestBase): def test_aliased_select_samename_uninit(self): a = table("a", column("x")) b = table("b", column("y")) - s = select(a, b).apply_labels().alias() + s = ( + select(a, b) + .set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) + .alias() + ) q = column("x") b.append_column(q) s._refresh_for_new_column(q) @@ -1318,7 +1372,11 @@ class RefreshForNewColTest(fixtures.TestBase): def test_aliased_select_samename_init(self): a = table("a", column("x")) b = table("b", column("y")) - s = select(a, b).apply_labels().alias() + s = ( + select(a, b) + .set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) + .alias() + ) s.c q = column("x") b.append_column(q) @@ -1329,7 +1387,11 @@ class RefreshForNewColTest(fixtures.TestBase): a = table("a", column("x")) b = table("b", column("y")) c = table("c", column("z")) - s = select(a, b).apply_labels().alias() + s = ( + select(a, b) + .set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) + .alias() + ) s.c q = column("x") c.append_column(q) @@ -1338,7 +1400,11 @@ class RefreshForNewColTest(fixtures.TestBase): def test_aliased_select_no_cols_clause(self): a = table("a", column("x")) - s = select(a.c.x).apply_labels().alias() + s = ( + select(a.c.x) + .set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) + .alias() + ) s.c q = column("q") a.append_column(q) @@ -1616,7 +1682,13 @@ class JoinConditionTest(fixtures.TestBase, AssertsCompiledSQL): (t1, t4, None), (t1t2, t2t3, None), (t5, t1, None), - (t5.select(use_labels=True).subquery(), t1, None), + ( + t5.select() + .set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) + .subquery(), + t1, + None, + ), ]: assert_raises( exc.ArgumentError, @@ -1705,7 +1777,11 @@ class JoinConditionTest(fixtures.TestBase, AssertsCompiledSQL): t1t2 = t1.join(t2) t2t3 = t2.join(t3) - st2t3 = t2t3.select().apply_labels().subquery() + st2t3 = ( + t2t3.select() + .set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) + .subquery() + ) j = t1t2.join(st2t3) assert j.onclause.compare(t2.c.id == st2t3.c.t3_t2id) self.assert_compile( @@ -2142,7 +2218,7 @@ class ReduceTest(fixtures.TestBase, AssertsExecutionResults): people.outerjoin(engineers) .outerjoin(managers) .select() - .apply_labels() + .set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) .alias("pjoin") ) eq_( @@ -2850,7 +2926,7 @@ class WithLabelsTest(fixtures.TestBase): m = MetaData() t1 = Table("t1", m, Column("x", Integer)) t2 = Table("t2", m, Column("x", Integer)) - return select(t1, t2) + return select(t1, t2).set_label_style(LABEL_STYLE_NONE) def test_names_overlap_nolabel(self): sel = self._names_overlap() @@ -2859,7 +2935,9 @@ class WithLabelsTest(fixtures.TestBase): self._assert_subq_result_keys(sel, ["x", "x_1"]) def test_names_overlap_label(self): - sel = self._names_overlap().apply_labels() + sel = self._names_overlap().set_label_style( + LABEL_STYLE_TABLENAME_PLUS_COL + ) eq_(list(sel.selected_columns.keys()), ["t1_x", "t2_x"]) eq_(list(sel.subquery().c.keys()), ["t1_x", "t2_x"]) self._assert_result_keys(sel, ["t1_x", "t2_x"]) @@ -2868,7 +2946,7 @@ class WithLabelsTest(fixtures.TestBase): m = MetaData() t1 = Table("t1", m, Column("x", Integer, key="a")) t2 = Table("t2", m, Column("x", Integer, key="b")) - return select(t1, t2) + return select(t1, t2).set_label_style(LABEL_STYLE_NONE) def test_names_overlap_keys_dont_nolabel(self): sel = self._names_overlap_keys_dont() @@ -2878,7 +2956,9 @@ class WithLabelsTest(fixtures.TestBase): self._assert_result_keys(sel, ["x"]) def test_names_overlap_keys_dont_label(self): - sel = self._names_overlap_keys_dont().apply_labels() + sel = self._names_overlap_keys_dont().set_label_style( + LABEL_STYLE_TABLENAME_PLUS_COL + ) eq_(list(sel.selected_columns.keys()), ["t1_a", "t2_b"]) eq_(list(sel.subquery().c.keys()), ["t1_a", "t2_b"]) self._assert_result_keys(sel, ["t1_x", "t2_x"]) @@ -2896,7 +2976,9 @@ class WithLabelsTest(fixtures.TestBase): self._assert_result_keys(sel, ["x_id", "id"]) def test_labels_overlap_label(self): - sel = self._labels_overlap().apply_labels() + sel = self._labels_overlap().set_label_style( + LABEL_STYLE_TABLENAME_PLUS_COL + ) eq_( list(sel.selected_columns.keys()), ["t_x_id", "t_x_id_1"], @@ -2922,7 +3004,9 @@ class WithLabelsTest(fixtures.TestBase): self._assert_result_keys(sel, ["x_id", "id"]) def test_labels_overlap_keylabels_dont_label(self): - sel = self._labels_overlap_keylabels_dont().apply_labels() + sel = self._labels_overlap_keylabels_dont().set_label_style( + LABEL_STYLE_TABLENAME_PLUS_COL + ) eq_(list(sel.selected_columns.keys()), ["t_a", "t_x_b"]) eq_(list(sel.subquery().c.keys()), ["t_a", "t_x_b"]) self._assert_result_keys(sel, ["t_x_id", "t_x_id_1"]) @@ -2940,7 +3024,9 @@ class WithLabelsTest(fixtures.TestBase): self._assert_result_keys(sel, ["a", "b"]) def test_keylabels_overlap_labels_dont_label(self): - sel = self._keylabels_overlap_labels_dont().apply_labels() + sel = self._keylabels_overlap_labels_dont().set_label_style( + LABEL_STYLE_TABLENAME_PLUS_COL + ) eq_( list(sel.selected_columns.keys()), ["t_x_id", "t_x_b_1"], @@ -2966,7 +3052,9 @@ class WithLabelsTest(fixtures.TestBase): self._assert_subq_result_keys(sel, ["x_id", "id"]) def test_keylabels_overlap_labels_overlap_label(self): - sel = self._keylabels_overlap_labels_overlap().apply_labels() + sel = self._keylabels_overlap_labels_overlap().set_label_style( + LABEL_STYLE_TABLENAME_PLUS_COL + ) eq_( list(sel.selected_columns.keys()), ["t_x_a", "t_x_id_1"], @@ -2992,7 +3080,9 @@ class WithLabelsTest(fixtures.TestBase): self._assert_result_keys(sel, ["a", "b"]) def test_keys_overlap_names_dont_label(self): - sel = self._keys_overlap_names_dont().apply_labels() + sel = self._keys_overlap_names_dont().set_label_style( + LABEL_STYLE_TABLENAME_PLUS_COL + ) eq_(list(sel.selected_columns.keys()), ["t1_x", "t2_x"]) eq_(list(sel.subquery().c.keys()), ["t1_x", "t2_x"]) self._assert_result_keys(sel, ["t1_a", "t2_b"]) @@ -3041,7 +3131,11 @@ class ResultMapTest(fixtures.TestBase): def test_select_alias_column_apply_labels(self): t = self._fixture() x, y = t.c.x, t.c.y - s = select(x, y).apply_labels().alias() + s = ( + select(x, y) + .set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) + .alias() + ) mapping = self._mapping(s) assert t.c.x in mapping @@ -3124,7 +3218,7 @@ class ResultMapTest(fixtures.TestBase): def test_unary_boolean(self): - s1 = select(not_(True)).apply_labels() + s1 = select(not_(True)).set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) eq_( [type(entry[-1]) for entry in s1.compile()._result_columns], [Boolean], diff --git a/test/sql/test_text.py b/test/sql/test_text.py index 0cd6f8cf3..6dccf3be6 100644 --- a/test/sql/test_text.py +++ b/test/sql/test_text.py @@ -20,6 +20,7 @@ from sqlalchemy import text from sqlalchemy import union from sqlalchemy import util from sqlalchemy.sql import column +from sqlalchemy.sql import LABEL_STYLE_TABLENAME_PLUS_COL from sqlalchemy.sql import quoted_name from sqlalchemy.sql import sqltypes from sqlalchemy.sql import table @@ -119,7 +120,7 @@ class SelectCompositionTest(fixtures.TestBase, AssertsCompiledSQL): table1.c.myid, ) .select_from(table1) - .apply_labels(), + .set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL), "SELECT column1, column2, column3 AS bar, " "mytable.myid AS mytable_myid " "FROM mytable", @@ -135,7 +136,7 @@ class SelectCompositionTest(fixtures.TestBase, AssertsCompiledSQL): table1.c.myid, ) .select_from(table1) - .apply_labels(), + .set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL), "SELECT column1 AS foobar, column2 AS hoho, " "mytable.myid AS mytable_myid FROM mytable", ) @@ -700,7 +701,11 @@ class OrderByLabelResolutionTest(fixtures.TestBase, AssertsCompiledSQL): def test_order_by_alias_colname(self): t1 = table1.alias() - stmt = select(t1.c.myid).apply_labels().order_by("name") + stmt = ( + select(t1.c.myid) + .set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) + .order_by("name") + ) self.assert_compile( stmt, "SELECT mytable_1.myid AS mytable_1_myid " @@ -803,7 +808,11 @@ class OrderByLabelResolutionTest(fixtures.TestBase, AssertsCompiledSQL): def test_group_by_subquery(self): stmt = select(table1).alias() - stmt = select(stmt).apply_labels().group_by("myid") + stmt = ( + select(stmt) + .set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) + .group_by("myid") + ) self.assert_compile( stmt, "SELECT anon_1.myid AS anon_1_myid, anon_1.name AS anon_1_name, " @@ -923,7 +932,7 @@ class OrderByLabelResolutionTest(fixtures.TestBase, AssertsCompiledSQL): s1 = ( select(*[adapter.columns[expr] for expr in exprs]) - .apply_labels() + .set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) .order_by("myid", "t1name", "x") ) @@ -955,7 +964,7 @@ class OrderByLabelResolutionTest(fixtures.TestBase, AssertsCompiledSQL): s1 = ( select(*[adapter.columns[expr] for expr in exprs]) - .apply_labels() + .set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) .order_by("myid", "t1name", "x") ) diff --git a/test/sql/test_type_expressions.py b/test/sql/test_type_expressions.py index e6f7dbdaf..5f278fb55 100644 --- a/test/sql/test_type_expressions.py +++ b/test/sql/test_type_expressions.py @@ -8,6 +8,7 @@ from sqlalchemy import Table from sqlalchemy import testing from sqlalchemy import TypeDecorator from sqlalchemy import union +from sqlalchemy.sql import LABEL_STYLE_TABLENAME_PLUS_COL from sqlalchemy.testing import AssertsCompiledSQL from sqlalchemy.testing import eq_ from sqlalchemy.testing import fixtures @@ -128,7 +129,7 @@ class SelectTest(_ExprFixture, fixtures.TestBase, AssertsCompiledSQL): table = self._fixture() self.assert_compile( - select(table).apply_labels(), + select(table).set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL), "SELECT test_table.x AS test_table_x, " "lower(test_table.y) AS test_table_y FROM test_table", ) @@ -136,7 +137,11 @@ class SelectTest(_ExprFixture, fixtures.TestBase, AssertsCompiledSQL): def test_select_cols_use_labels_result_map_targeting(self): table = self._fixture() - compiled = select(table).apply_labels().compile() + compiled = ( + select(table) + .set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) + .compile() + ) assert table.c.y in compiled._create_result_map()["test_table_y"][1] assert table.c.x in compiled._create_result_map()["test_table_x"][1] @@ -323,8 +328,8 @@ class DerivedTest(_ExprFixture, fixtures.TestBase, AssertsCompiledSQL): s3 = j.select() self.assert_compile( s3, - "SELECT anon_1.x, lower(anon_1.y) AS y, anon_2.x, " - "lower(anon_2.y) AS y " + "SELECT anon_1.x, lower(anon_1.y) AS y, anon_2.x AS x_1, " + "lower(anon_2.y) AS y_1 " "FROM (SELECT test_table.x AS x, test_table.y AS y " "FROM test_table) AS anon_1 JOIN (SELECT " "test_table.x AS x, test_table.y AS y " @@ -380,7 +385,9 @@ class RoundTripTestBase(object): self.tables.test_table.insert(), {"x": "X1", "y": "Y1"} ) row = connection.execute( - select(self.tables.test_table).apply_labels() + select(self.tables.test_table).set_label_style( + LABEL_STYLE_TABLENAME_PLUS_COL + ) ).first() eq_(row._mapping[self.tables.test_table.c.y], "Y1") |
