Commits

Mike Bayer committed 4f6ed29

- 0.7.7
- [feature] Added prefix_with() method
to Query, calls upon select().prefix_with()
to allow placement of MySQL SELECT
directives in statements. Courtesy
Diana Clarke [ticket:2443]

  • Participants
  • Parent commits 3d92c39

Comments (0)

Files changed (4)

 =======
 CHANGES
 =======
+0.7.7
+=====
+- orm
+  - [feature] Added prefix_with() method
+    to Query, calls upon select().prefix_with()
+    to allow placement of MySQL SELECT
+    directives in statements.  Courtesy
+    Diana Clarke [ticket:2443]
+
 0.7.6
 =====
 - orm

File lib/sqlalchemy/__init__.py

 __all__ = sorted(name for name, obj in locals().items()
                  if not (name.startswith('_') or inspect.ismodule(obj)))
 
-__version__ = '0.7.6'
+__version__ = '0.7.7'
 
 del inspect, sys
 

File lib/sqlalchemy/orm/query.py

     _group_by = False
     _having = None
     _distinct = False
+    _prefixes = None
     _offset = None
     _limit = None
     _statement = None
                 '_order_by', '_group_by',
                 '_limit', '_offset', 
                 '_joinpath', '_joinpoint', 
-                '_distinct', '_having'
+                '_distinct', '_having', 
+                '_prefixes',
         ):
             self.__dict__.pop(attr, None)
         self._set_select_from(fromclause)
             else: 
                 self._distinct = criterion 
 
+    @_generative()
+    def prefix_with(self, *prefixes):
+        """Apply the prefixes to the query and return the newly resulting
+        ``Query``.
+
+        :param \*prefixes: optional prefixes, typically strings, 
+        not using any commas.   In particular is useful for MySQL keywords.
+
+        e.g.::
+
+            query = sess.query(User.name).\\
+                prefix_with('HIGH_PRIORITY').\\
+                prefix_with('SQL_SMALL_RESULT', 'ALL')
+
+        Would render::
+
+            SELECT HIGH_PRIORITY SQL_SMALL_RESULT ALL users.name AS users_name 
+            FROM users
+        
+        New in 0.7.7.
+
+        """
+        if self._prefixes:
+            self._prefixes += prefixes
+        else:
+            self._prefixes = prefixes
+
     def all(self):
         """Return the results represented by this ``Query`` as a list.
 
             'limit':self._limit,
             'offset':self._offset,
             'distinct':self._distinct,
+            'prefixes':self._prefixes,
             'group_by':self._group_by or None,
             'having':self._having
         }

File test/orm/test_query.py

         self.assert_sql_count(testing.db, go, 1)
 
 
+class PrefixWithTest(QueryTest, AssertsCompiledSQL):
+
+    def test_one_prefix(self):
+        User = self.classes.User
+        sess = create_session()
+        query = sess.query(User.name)\
+            .prefix_with('PREFIX_1')
+        expected = "SELECT PREFIX_1 "\
+            "users.name AS users_name FROM users"
+        self.assert_compile(query, expected,
+            dialect=default.DefaultDialect()
+        )
+
+    def test_many_prefixes(self):
+        User = self.classes.User
+        sess = create_session()
+        query = sess.query(User.name)\
+            .prefix_with('PREFIX_1', 'PREFIX_2')
+        expected = "SELECT PREFIX_1 PREFIX_2 "\
+            "users.name AS users_name FROM users"
+        self.assert_compile(query, expected,
+            dialect=default.DefaultDialect()
+        )
+
+    def test_chained_prefixes(self):
+        User = self.classes.User
+        sess = create_session()
+        query = sess.query(User.name)\
+            .prefix_with('PREFIX_1')\
+            .prefix_with('PREFIX_2', 'PREFIX_3')
+        expected = "SELECT PREFIX_1 PREFIX_2 PREFIX_3 "\
+            "users.name AS users_name FROM users"
+        self.assert_compile(query, expected,
+            dialect=default.DefaultDialect()
+        )
+
+
 class YieldTest(QueryTest):
     def test_basic(self):
         User = self.classes.User