]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
backported 0.6 r6084 fix for oracle alias names, [ticket:1309]
authorMike Bayer <mike_mp@zzzcomputing.com>
Fri, 31 Jul 2009 23:10:46 +0000 (23:10 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Fri, 31 Jul 2009 23:10:46 +0000 (23:10 +0000)
CHANGES
lib/sqlalchemy/databases/oracle.py
test/dialect/test_oracle.py

diff --git a/CHANGES b/CHANGES
index e79049b77e8104ece303d0844c1ea2715703dc78..8abf88dffd9acd40e4032177df118caa40c55069 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -64,6 +64,10 @@ CHANGES
     - Fixed bug in Table and Column whereby passing empty
       dict for "info" argument would raise an exception.
       [ticket:1482]
+
+- oracle
+    - Backported 0.6 fix for Oracle alias names not getting
+      truncated.  [ticket:1309]
       
 - ext
    - The collection proxies produced by associationproxy are now
index 6749d8e407480b444f983509f9c7292c98c2f6f5..852cab448e78bf79c588544fedd8a72f062f1881 100644 (file)
@@ -127,7 +127,7 @@ import datetime, random, re
 
 from sqlalchemy import util, sql, schema, log
 from sqlalchemy.engine import default, base
-from sqlalchemy.sql import compiler, visitors
+from sqlalchemy.sql import compiler, visitors, expression
 from sqlalchemy.sql import operators as sql_operators, functions as sql_functions
 from sqlalchemy import types as sqltypes
 
@@ -769,7 +769,11 @@ class OracleCompiler(compiler.DefaultCompiler):
         """Oracle doesn't like ``FROM table AS alias``.  Is the AS standard SQL??"""
 
         if asfrom:
-            return self.process(alias.original, asfrom=asfrom, **kwargs) + " " + self.preparer.format_alias(alias, self._anonymize(alias.name))
+            alias_name = isinstance(alias.name, expression._generated_label) and \
+                            self._truncated_identifier("alias", alias.name) or alias.name
+            
+            return self.process(alias.original, asfrom=True, **kwargs) + " " +\
+                    self.preparer.format_alias(alias, alias_name)
         else:
             return self.process(alias.original, **kwargs)
 
index 16175c85121d44c93683ed07f882aa5c1b99c935..d9d64806e814d21efc439516cdbc5dd67f7c2889 100644 (file)
@@ -7,6 +7,7 @@ from sqlalchemy.databases import oracle
 from sqlalchemy.test import *
 from sqlalchemy.test.testing import eq_
 from sqlalchemy.test.engines import testing_engine
+from sqlalchemy.engine import default
 import os
 
 
@@ -87,7 +88,57 @@ class CompileTest(TestBase, AssertsCompiledSQL):
         self.assert_compile(s, "SELECT col1, col2 FROM (SELECT col1, col2, ROWNUM "
             "AS ora_rn FROM (SELECT sometable.col1 AS col1, sometable.col2 AS col2 FROM sometable "
             "ORDER BY sometable.col2) WHERE ROWNUM <= :ROWNUM_1) WHERE ora_rn > :ora_rn_1")
+    
+    def test_long_labels(self):
+        dialect = default.DefaultDialect()
+        dialect.max_identifier_length = 30
+        
+        ora_dialect = oracle.dialect()
+        
+        m = MetaData()
+        a_table = Table(
+            'thirty_characters_table_xxxxxx',
+            m,
+            Column('id', Integer, primary_key=True)
+        )
 
+        other_table = Table(
+            'other_thirty_characters_table_',
+            m,
+            Column('id', Integer, primary_key=True),
+            Column('thirty_characters_table_id',
+                Integer,
+                ForeignKey('thirty_characters_table_xxxxxx.id'),
+                primary_key=True
+            )
+        )
+        
+        anon = a_table.alias()
+        self.assert_compile(
+        
+            select([other_table, anon]).select_from(
+                other_table.outerjoin(anon)
+            ).apply_labels(),
+            "SELECT other_thirty_characters_table_.id AS other_thirty_characters__1, "
+            "other_thirty_characters_table_.thirty_characters_table_id AS other_thirty_characters__2, "
+            "thirty_characters_table__1.id AS thirty_characters_table__3 FROM other_thirty_characters_table_ "
+            "LEFT OUTER JOIN thirty_characters_table_xxxxxx AS thirty_characters_table__1 ON "
+            "thirty_characters_table__1.id = other_thirty_characters_table_.thirty_characters_table_id",
+            dialect=dialect
+        )
+        self.assert_compile(
+        
+            select([other_table, anon]).select_from(
+                other_table.outerjoin(anon)
+            ).apply_labels(),
+            "SELECT other_thirty_characters_table_.id AS other_thirty_characters__1, "
+            "other_thirty_characters_table_.thirty_characters_table_id AS other_thirty_characters__2, "
+            "thirty_characters_table__1.id AS thirty_characters_table__3 FROM other_thirty_characters_table_ "
+            "LEFT OUTER JOIN thirty_characters_table_xxxxxx thirty_characters_table__1 ON "
+            "thirty_characters_table__1.id = other_thirty_characters_table_.thirty_characters_table_id",
+            dialect=ora_dialect
+        )
+        
     def test_outer_join(self):
         table1 = table('mytable',
             column('myid', Integer),
@@ -357,7 +408,8 @@ class BufferedColumnTest(TestBase, AssertsCompiledSQL):
            Column('data', Binary)
         )
         meta.create_all()
-        stream = os.path.join(os.path.dirname(testenv.__file__), 'binary_data_one.dat')
+        
+        stream = os.path.join(os.path.dirname(__file__), "..", 'binary_data_one.dat')
         stream = file(stream).read(12000)
 
         for i in range(1, 11):